Commit | Line | Data |
---|---|---|
9ff5f6c0 | 1 | <?php |
4c6ce474 EM |
2 | |
3 | /** | |
4 | * Class CRM_Custom_Import_Parser_Api | |
5 | */ | |
9ff5f6c0 N |
6 | class CRM_Custom_Import_Parser_Api extends CRM_Custom_Import_Parser { |
7 | ||
8 | protected $_entity = ''; | |
9 | protected $_fields = array(); | |
10 | protected $_requiredFields = array(); | |
11 | protected $_dateFields = array(); | |
12 | protected $_multipleCustomData = ''; | |
13 | ||
14 | /** | |
fe482240 | 15 | * Params for the current entity being prepared for the api. |
9ff5f6c0 N |
16 | * @var array |
17 | */ | |
18 | protected $_params = array(); | |
353ffa53 | 19 | |
9ff5f6c0 | 20 | /** |
7a9ab499 EM |
21 | * Class constructor. |
22 | * | |
23 | * @param array $mapperKeys | |
24 | * @param null $mapperLocType | |
25 | * @param null $mapperPhoneType | |
9ff5f6c0 | 26 | */ |
00be9182 | 27 | public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) { |
9ff5f6c0 N |
28 | parent::__construct(); |
29 | $this->_mapperKeys = &$mapperKeys; | |
30 | } | |
353ffa53 | 31 | |
00be9182 | 32 | public function setFields() { |
9ff5f6c0 N |
33 | $customGroupID = $this->_multipleCustomData; |
34 | $importableFields = $this->getGroupFieldsForImport($customGroupID, $this); | |
353ffa53 TO |
35 | $this->_fields = array_merge(array( |
36 | 'do_not_import' => array('title' => ts('- do not import -')), | |
408b79bf | 37 | 'contact_id' => array('title' => ts('Contact ID')), |
353ffa53 | 38 | ), $importableFields); |
9ff5f6c0 N |
39 | } |
40 | ||
41 | /** | |
100fef9d | 42 | * The initializer code, called before the processing |
9ff5f6c0 N |
43 | * |
44 | * @return void | |
9ff5f6c0 | 45 | */ |
00be9182 | 46 | public function init() { |
9ff5f6c0 N |
47 | $this->setFields(); |
48 | $fields = $this->_fields; | |
49 | $hasLocationType = FALSE; | |
50 | ||
51 | foreach ($fields as $name => $field) { | |
52 | $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT); | |
53 | $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//'); | |
54 | $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//'); | |
55 | $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern'], $hasLocationType); | |
56 | } | |
57 | $this->setActiveFields($this->_mapperKeys); | |
58 | } | |
59 | ||
60 | /** | |
fe482240 | 61 | * Handle the values in mapField mode. |
9ff5f6c0 | 62 | * |
c4ca4892 TO |
63 | * @param array $values |
64 | * The array of values belonging to this line. | |
9ff5f6c0 | 65 | * |
408b79bf | 66 | * @return bool |
9ff5f6c0 | 67 | */ |
00be9182 | 68 | public function mapField(&$values) { |
9ff5f6c0 N |
69 | return CRM_Import_Parser::VALID; |
70 | } | |
71 | ||
72 | /** | |
fe482240 | 73 | * Handle the values in preview mode. |
9ff5f6c0 | 74 | * |
c4ca4892 TO |
75 | * @param array $values |
76 | * The array of values belonging to this line. | |
9ff5f6c0 | 77 | * |
408b79bf | 78 | * @return bool |
a6c01b45 | 79 | * the result of this processing |
9ff5f6c0 | 80 | */ |
00be9182 | 81 | public function preview(&$values) { |
9ff5f6c0 N |
82 | return $this->summary($values); |
83 | } | |
84 | ||
85 | /** | |
c4ca4892 TO |
86 | * @param array $values |
87 | * The array of values belonging to this line. | |
9ff5f6c0 | 88 | * |
408b79bf | 89 | * @return bool |
a6c01b45 | 90 | * the result of this processing |
16b10e64 CW |
91 | * It is called from both the preview & the import actions |
92 | * | |
9ff5f6c0 N |
93 | * @see CRM_Custom_Import_Parser_BaseClass::summary() |
94 | */ | |
00be9182 | 95 | public function summary(&$values) { |
e8646905 | 96 | $erroneousField = NULL; |
353ffa53 | 97 | $response = $this->setActiveFieldValues($values, $erroneousField); |
e8646905 TO |
98 | $errorRequired = FALSE; |
99 | $missingField = ''; | |
100 | $this->_params = &$this->getActiveFieldParams(); | |
101 | ||
102 | $formatted = $this->_params; | |
103 | $this->_updateWithId = FALSE; | |
104 | $this->_parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options'), FALSE); | |
105 | ||
106 | $this->_params = $this->getActiveFieldParams(); | |
107 | foreach ($this->_requiredFields as $requiredField) { | |
108 | if (empty($this->_params[$requiredField])) { | |
109 | $errorRequired = TRUE; | |
110 | $missingField .= ' ' . $requiredField; | |
111 | CRM_Contact_Import_Parser_Contact::addToErrorMsg($this->_entity, $requiredField); | |
112 | } | |
113 | } | |
114 | ||
115 | if ($errorRequired) { | |
116 | array_unshift($values, ts('Missing required field(s) :') . $missingField); | |
117 | return CRM_Import_Parser::ERROR; | |
118 | } | |
119 | ||
120 | $errorMessage = NULL; | |
121 | ||
122 | $contactType = $this->_contactType ? $this->_contactType : 'Organization'; | |
123 | CRM_Contact_Import_Parser_Contact::isErrorInCustomData($this->_params, $errorMessage, $contactType, NULL); | |
124 | ||
125 | // pseudoconstants | |
126 | if ($errorMessage) { | |
127 | $tempMsg = "Invalid value for field(s) : $errorMessage"; | |
128 | array_unshift($values, $tempMsg); | |
129 | $errorMessage = NULL; | |
130 | return CRM_Import_Parser::ERROR; | |
131 | } | |
132 | return CRM_Import_Parser::VALID; | |
9ff5f6c0 N |
133 | } |
134 | ||
135 | /** | |
fe482240 | 136 | * Handle the values in import mode. |
9ff5f6c0 | 137 | * |
c4ca4892 TO |
138 | * @param int $onDuplicate |
139 | * The code for what action to take on duplicates. | |
140 | * @param array $values | |
141 | * The array of values belonging to this line. | |
9ff5f6c0 | 142 | * |
408b79bf | 143 | * @return bool |
a6c01b45 | 144 | * the result of this processing |
9ff5f6c0 | 145 | */ |
00be9182 | 146 | public function import($onDuplicate, &$values) { |
9ff5f6c0 N |
147 | $response = $this->summary($values); |
148 | if ($response != CRM_Import_Parser::VALID) { | |
149 | $importRecordParams = array( | |
150 | $statusFieldName => 'INVALID', | |
151 | "${statusFieldName}Msg" => "Invalid (Error Code: $response)", | |
152 | ); | |
153 | return $response; | |
154 | } | |
155 | ||
156 | $this->_updateWithId = FALSE; | |
157 | $this->_parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options'), FALSE); | |
158 | ||
159 | $params = $this->getActiveFieldParams(); | |
160 | $contactType = $this->_contactType ? $this->_contactType : 'Organization'; | |
161 | $formatted = array( | |
162 | 'contact_type' => $contactType, | |
163 | ); | |
164 | $session = CRM_Core_Session::singleton(); | |
165 | $dateType = $session->get('dateTypes'); | |
166 | ||
167 | $formatted['id'] = $this->_params['contact_id']; | |
168 | $setDateFields = array_intersect_key($this->_params, array_flip($this->_dateFields)); | |
169 | ||
e8646905 | 170 | CRM_Contact_Import_Parser_Contact::formatCommonData($this->_params, $formatted, $formatted); |
22e263ad | 171 | foreach ($formatted['custom'] as $key => $val) { |
92fcb95f | 172 | $this->_params['custom_' . $key] = $val[-1]['value']; |
9ff5f6c0 N |
173 | } |
174 | $this->_params['skipRecentView'] = TRUE; | |
175 | $this->_params['check_permissions'] = TRUE; | |
176 | $this->_params['entity_id'] = $formatted['id']; | |
92e4c2a5 | 177 | try { |
9ff5f6c0 N |
178 | civicrm_api3('custom_value', 'create', $this->_params); |
179 | } | |
353ffa53 | 180 | catch (CiviCRM_API3_Exception $e) { |
9ff5f6c0 N |
181 | $error = $e->getMessage(); |
182 | array_unshift($values, $error); | |
183 | return CRM_Import_Parser::ERROR; | |
184 | } | |
185 | } | |
186 | ||
187 | /** | |
fe482240 | 188 | * Format Date params. |
9ff5f6c0 N |
189 | * |
190 | * Although the api will accept any strtotime valid string CiviCRM accepts at least one date format | |
191 | * not supported by strtotime so we should run this through a conversion | |
9ff5f6c0 | 192 | */ |
00be9182 | 193 | public function formatDateParams() { |
9ff5f6c0 N |
194 | $session = CRM_Core_Session::singleton(); |
195 | $dateType = $session->get('dateTypes'); | |
196 | $setDateFields = array_intersect_key($this->_params, array_flip($this->_dateFields)); | |
197 | ||
198 | foreach ($setDateFields as $key => $value) { | |
199 | CRM_Utils_Date::convertToDefaultDate($this->_params, $dateType, $key); | |
200 | $this->_params[$key] = CRM_Utils_Date::processDate($this->_params[$key]); | |
201 | } | |
202 | } | |
203 | ||
204 | /** | |
fe482240 | 205 | * Set import entity. |
9ff5f6c0 N |
206 | * @param string $entity |
207 | */ | |
00be9182 | 208 | public function setEntity($entity) { |
9ff5f6c0 N |
209 | $this->_entity = $entity; |
210 | $this->_multipleCustomData = $entity; | |
211 | } | |
212 | ||
213 | /** | |
100fef9d | 214 | * The initializer code, called before the processing |
9ff5f6c0 N |
215 | * |
216 | * @return void | |
9ff5f6c0 | 217 | */ |
e8646905 TO |
218 | public function fini() { |
219 | } | |
9ff5f6c0 N |
220 | |
221 | /** | |
222 | * Return the field ids and names (with groups) for import purpose. | |
223 | * | |
c4ca4892 TO |
224 | * @param int $id |
225 | * Custom group ID. | |
9ff5f6c0 | 226 | * |
a6c01b45 | 227 | * @return array |
9ff5f6c0 | 228 | * |
9ff5f6c0 | 229 | */ |
e8646905 | 230 | public function getGroupFieldsForImport($id) { |
9ff5f6c0 N |
231 | $importableFields = array(); |
232 | $params = array('custom_group_id' => $id); | |
233 | $allFields = civicrm_api3('custom_field', 'get', $params); | |
234 | $fields = $allFields['values']; | |
235 | foreach ($fields as $id => $values) { | |
236 | $datatype = CRM_Utils_Array::value('data_type', $values); | |
481a74f4 | 237 | if ($datatype == 'File') { |
9ff5f6c0 N |
238 | continue; |
239 | } | |
240 | /* generate the key for the fields array */ | |
241 | $key = "custom_$id"; | |
242 | $regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array::value(0, $values)); | |
243 | $importableFields[$key] = array( | |
244 | 'name' => $key, | |
245 | 'title' => CRM_Utils_Array::value('label', $values), | |
246 | 'headerPattern' => '/' . preg_quote($regexp, '/') . '/', | |
247 | 'import' => 1, | |
248 | 'custom_field_id' => $id, | |
249 | 'options_per_line' => CRM_Utils_Array::value('options_per_line', $values), | |
250 | 'data_type' => CRM_Utils_Array::value('data_type', $values), | |
251 | 'html_type' => CRM_Utils_Array::value('html_type', $values), | |
252 | 'is_search_range' => CRM_Utils_Array::value('is_search_range', $values), | |
253 | ); | |
254 | if (CRM_Utils_Array::value('html_type', $values) == 'Select Date') { | |
255 | $importableFields[$key]['date_format'] = CRM_Utils_Array::value('date_format', $values); | |
256 | $importableFields[$key]['time_format'] = CRM_Utils_Array::value('time_format', $values); | |
257 | $this->_dateFields[] = $key; | |
258 | } | |
259 | } | |
260 | return $importableFields; | |
261 | } | |
96025800 | 262 | |
fd31fa4c | 263 | } |