summaryrefslogtreecommitdiff
path: root/www/crm/wp-content/plugins/civicrm/civicrm/ext/api4/Civi/Api4/Event/Subscriber/CustomFieldPreCreationSubscriber.php
blob: 289e105715934b9853d363eef09f6bda957050fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

namespace Civi\Api4\Event\Subscriber;

use Civi\Api4\Generic\DAOCreateAction;

class CustomFieldPreCreationSubscriber extends PreCreationSubscriber {

  const OPTION_TYPE_NEW = 1;
  const OPTION_STATUS_ACTIVE = 1;

  /**
   * @param DAOCreateAction $request
   */
  public function modify(DAOCreateAction $request) {
    $this->formatOptionParams($request);
    $this->setDefaults($request);
  }

  /**
   * @param DAOCreateAction $request
   *
   * @return bool
   */
  protected function applies(DAOCreateAction $request) {
    return $request->getEntityName() === 'CustomField';
  }

  /**
   * Sets defaults required for option group and value creation
   * @see CRM_Core_BAO_CustomField::create()
   *
   * @param DAOCreateAction $request
   */
  protected function formatOptionParams(DAOCreateAction $request) {
    $options = $request->getValue('options');

    if (!is_array($options)) {
      return;
    }

    $dataTypeKey = 'data_type';
    $optionLabelKey = 'option_label';
    $optionWeightKey = 'option_weight';
    $optionStatusKey = 'option_status';
    $optionValueKey = 'option_value';
    $optionTypeKey = 'option_type';

    $dataType = $request->getValue($dataTypeKey);
    $optionLabel = $request->getValue($optionLabelKey);
    $optionWeight = $request->getValue($optionWeightKey);
    $optionStatus = $request->getValue($optionStatusKey);
    $optionValue = $request->getValue($optionValueKey);
    $optionType = $request->getValue($optionTypeKey);

    if (!$optionType) {
      $request->addValue($optionTypeKey, self::OPTION_TYPE_NEW);
    }

    if (!$dataType) {
      $request->addValue($dataTypeKey, 'String');
    }

    if (!$optionLabel) {
      $request->addValue($optionLabelKey, array_values($options));
    }

    if (!$optionValue) {
      $request->addValue($optionValueKey, array_keys($options));
    }

    if (!$optionStatus) {
      $statuses = array_fill(0, count($options), self::OPTION_STATUS_ACTIVE);
      $request->addValue($optionStatusKey, $statuses);
    }

    if (!$optionWeight) {
      $request->addValue($optionWeightKey, range(1, count($options)));
    }
  }

  /**
   * @param DAOCreateAction $request
   */
  private function setDefaults(DAOCreateAction $request) {
    if (!$request->getValue('option_type')) {
      $request->addValue('option_type', NULL);
    }
  }

}