Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
0cf587a7 EM |
27 | |
28 | /** | |
29 | * Class CRM_Event_Import_Field | |
30 | */ | |
6a488035 TO |
31 | class CRM_Event_Import_Field { |
32 | ||
90b461f1 SL |
33 | /** |
34 | * #@+ | |
6a488035 TO |
35 | * @var string |
36 | */ | |
37 | ||
38 | /** | |
100fef9d | 39 | * Name of the field |
90b461f1 | 40 | * @var string |
6a488035 TO |
41 | */ |
42 | public $_name; | |
43 | ||
44 | /** | |
100fef9d | 45 | * Title of the field to be used in display |
90b461f1 | 46 | * @var string |
6a488035 TO |
47 | */ |
48 | public $_title; | |
49 | ||
50 | /** | |
100fef9d | 51 | * Type of field |
6a488035 TO |
52 | * @var enum |
53 | */ | |
54 | public $_type; | |
55 | ||
56 | /** | |
100fef9d | 57 | * Regexp to match the CSV header of this column/field |
6a488035 TO |
58 | * @var string |
59 | */ | |
60 | public $_headerPattern; | |
61 | ||
62 | /** | |
100fef9d | 63 | * Regexp to match the pattern of data from various column/fields |
6a488035 TO |
64 | * @var string |
65 | */ | |
66 | public $_dataPattern; | |
67 | ||
68 | /** | |
100fef9d | 69 | * Value of this field |
6a488035 TO |
70 | * @var object |
71 | */ | |
72 | public $_value; | |
0cf587a7 EM |
73 | |
74 | /** | |
100fef9d | 75 | * @param string $name |
0cf587a7 EM |
76 | * @param $title |
77 | * @param int $type | |
78 | * @param string $headerPattern | |
79 | * @param string $dataPattern | |
80 | */ | |
00be9182 | 81 | public function __construct($name, $title, $type = CRM_Utils_Type::T_INT, $headerPattern = '//', $dataPattern = '//') { |
6a488035 TO |
82 | $this->_name = $name; |
83 | $this->_title = $title; | |
84 | $this->_type = $type; | |
85 | $this->_headerPattern = $headerPattern; | |
86 | $this->_dataPattern = $dataPattern; | |
87 | ||
88 | $this->_value = NULL; | |
89 | } | |
90 | ||
00be9182 | 91 | public function resetValue() { |
6a488035 TO |
92 | $this->_value = NULL; |
93 | } | |
94 | ||
95 | /** | |
ad37ac8e | 96 | * Convert the value to the type of this field and set the field value with the appropriate type. |
97 | * | |
98 | * @param string $value | |
6a488035 | 99 | */ |
00be9182 | 100 | public function setValue($value) { |
6a488035 TO |
101 | $this->_value = $value; |
102 | } | |
103 | ||
0cf587a7 EM |
104 | /** |
105 | * @return bool | |
106 | */ | |
00be9182 | 107 | public function validate() { |
6a488035 TO |
108 | if (CRM_Utils_System::isNull($this->_value)) { |
109 | return TRUE; | |
110 | } | |
111 | ||
112 | switch ($this->_name) { | |
113 | case 'contact_id': | |
114 | // note: we validate extistence of the contact in API, upon | |
115 | // insert (it would be too costlty to do a db call here) | |
116 | return CRM_Utils_Rule::integer($this->_value); | |
117 | ||
118 | case 'register_date': | |
119 | return CRM_Utils_Rule::date($this->_value); | |
ea100cb5 | 120 | |
e70a7fc0 | 121 | /* @codingStandardsIgnoreStart |
6a488035 TO |
122 | case 'event_id': |
123 | static $events = null; | |
124 | if (!$events) { | |
125 | $events = CRM_Event_PseudoConstant::event(); | |
126 | } | |
127 | if (in_array($this->_value, $events)) { | |
128 | return true; | |
0db6c3e1 TO |
129 | } |
130 | else { | |
6a488035 TO |
131 | return false; |
132 | } | |
133 | break; | |
e70a7fc0 | 134 | @codingStandardsIgnoreEnd */ |
6a488035 TO |
135 | |
136 | default: | |
137 | break; | |
138 | } | |
139 | ||
140 | // check whether that's a valid custom field id | |
141 | // and if so, check the contents' validity | |
142 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($this->_name)) { | |
143 | static $customFields = NULL; | |
144 | if (!$customFields) { | |
719a6fec | 145 | $customFields = CRM_Core_BAO_CustomField::getFields('Participant'); |
6a488035 TO |
146 | } |
147 | if (!array_key_exists($customFieldID, $customFields)) { | |
148 | return FALSE; | |
149 | } | |
150 | return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value); | |
151 | } | |
152 | return TRUE; | |
153 | } | |
96025800 | 154 | |
6a488035 | 155 | } |