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