| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Class CRM_Event_Import_Field |
| 14 | */ |
| 15 | class CRM_Event_Import_Field { |
| 16 | |
| 17 | /** |
| 18 | * #@+ |
| 19 | * @var string |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Name of the field |
| 24 | * @var string |
| 25 | */ |
| 26 | public $_name; |
| 27 | |
| 28 | /** |
| 29 | * Title of the field to be used in display |
| 30 | * @var string |
| 31 | */ |
| 32 | public $_title; |
| 33 | |
| 34 | /** |
| 35 | * Type of field |
| 36 | * @var enum |
| 37 | */ |
| 38 | public $_type; |
| 39 | |
| 40 | /** |
| 41 | * Regexp to match the CSV header of this column/field |
| 42 | * @var string |
| 43 | */ |
| 44 | public $_headerPattern; |
| 45 | |
| 46 | /** |
| 47 | * Regexp to match the pattern of data from various column/fields |
| 48 | * @var string |
| 49 | */ |
| 50 | public $_dataPattern; |
| 51 | |
| 52 | /** |
| 53 | * Value of this field |
| 54 | * @var object |
| 55 | */ |
| 56 | public $_value; |
| 57 | |
| 58 | /** |
| 59 | * @param string $name |
| 60 | * @param $title |
| 61 | * @param int $type |
| 62 | * @param string $headerPattern |
| 63 | * @param string $dataPattern |
| 64 | */ |
| 65 | public function __construct($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { |
| 66 | $this->_name = $name; |
| 67 | $this->_title = $title; |
| 68 | $this->_type = $type; |
| 69 | $this->_headerPattern = $headerPattern; |
| 70 | $this->_dataPattern = $dataPattern; |
| 71 | |
| 72 | $this->_value = NULL; |
| 73 | } |
| 74 | |
| 75 | public function resetValue() { |
| 76 | $this->_value = NULL; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Convert the value to the type of this field and set the field value with the appropriate type. |
| 81 | * |
| 82 | * @param string $value |
| 83 | */ |
| 84 | public function setValue($value) { |
| 85 | $this->_value = $value; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return bool |
| 90 | */ |
| 91 | public function validate() { |
| 92 | if (CRM_Utils_System::isNull($this->_value)) { |
| 93 | return TRUE; |
| 94 | } |
| 95 | |
| 96 | switch ($this->_name) { |
| 97 | case 'contact_id': |
| 98 | // note: we validate extistence of the contact in API, upon |
| 99 | // insert (it would be too costlty to do a db call here) |
| 100 | return CRM_Utils_Rule::integer($this->_value); |
| 101 | |
| 102 | case 'register_date': |
| 103 | return CRM_Utils_Rule::date($this->_value); |
| 104 | |
| 105 | /* @codingStandardsIgnoreStart |
| 106 | case 'event_id': |
| 107 | static $events = null; |
| 108 | if (!$events) { |
| 109 | $events = CRM_Event_PseudoConstant::event(); |
| 110 | } |
| 111 | if (in_array($this->_value, $events)) { |
| 112 | return true; |
| 113 | } |
| 114 | else { |
| 115 | return false; |
| 116 | } |
| 117 | break; |
| 118 | @codingStandardsIgnoreEnd */ |
| 119 | |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | // check whether that's a valid custom field id |
| 125 | // and if so, check the contents' validity |
| 126 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($this->_name)) { |
| 127 | static $customFields = NULL; |
| 128 | if (!$customFields) { |
| 129 | $customFields = CRM_Core_BAO_CustomField::getFields('Participant'); |
| 130 | } |
| 131 | if (!array_key_exists($customFieldID, $customFields)) { |
| 132 | return FALSE; |
| 133 | } |
| 134 | return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value); |
| 135 | } |
| 136 | return TRUE; |
| 137 | } |
| 138 | |
| 139 | } |