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