INFRA-132 - CRM/ - PHPStorm cleanup
[civicrm-core.git] / CRM / Custom / Import / Parser / Api.php
1 <?php
2
3 /**
4 * Class CRM_Custom_Import_Parser_Api
5 */
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();
19
20 /**
21 * Class constructor
22 */
23 public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
24 parent::__construct();
25 $this->_mapperKeys = &$mapperKeys;
26 }
27
28 public function setFields() {
29 $customGroupID = $this->_multipleCustomData;
30 $importableFields = $this->getGroupFieldsForImport($customGroupID, $this);
31 $this->_fields = array_merge(array(
32 'do_not_import' => array('title' => ts('- do not import -')),
33 'contact_id' => array('title' => ts('Contact ID'))
34 ), $importableFields);
35 }
36
37 /**
38 * The initializer code, called before the processing
39 *
40 * @return void
41 */
42 public function init() {
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 /**
57 * Handle the values in mapField mode
58 *
59 * @param array $values
60 * The array of values belonging to this line.
61 *
62 * @return boolean
63 */
64 public function mapField(&$values) {
65 return CRM_Import_Parser::VALID;
66 }
67
68 /**
69 * Handle the values in preview mode
70 *
71 * @param array $values
72 * The array of values belonging to this line.
73 *
74 * @return boolean
75 * the result of this processing
76 */
77 public function preview(&$values) {
78 return $this->summary($values);
79 }
80
81 /**
82 * @param array $values
83 * The array of values belonging to this line.
84 *
85 * @return boolean
86 * the result of this processing
87 * It is called from both the preview & the import actions
88 * (non-PHPdoc)
89 * @see CRM_Custom_Import_Parser_BaseClass::summary()
90 */
91 public function summary(&$values) {
92 $erroneousField = NULL;
93 $response = $this->setActiveFieldValues($values, $erroneousField);
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;
129 }
130
131 /**
132 * Handle the values in import mode
133 *
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.
138 *
139 * @return boolean
140 * the result of this processing
141 */
142 public function import($onDuplicate, &$values) {
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
166 CRM_Contact_Import_Parser_Contact::formatCommonData($this->_params, $formatted, $formatted);
167 foreach ($formatted['custom'] as $key => $val) {
168 $this->_params['custom_' . $key] = $val[-1]['value'];
169 }
170 $this->_params['skipRecentView'] = TRUE;
171 $this->_params['check_permissions'] = TRUE;
172 $this->_params['entity_id'] = $formatted['id'];
173 try {
174 civicrm_api3('custom_value', 'create', $this->_params);
175 }
176 catch (CiviCRM_API3_Exception $e) {
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
188 */
189 public function formatDateParams() {
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 */
204 public function setEntity($entity) {
205 $this->_entity = $entity;
206 $this->_multipleCustomData = $entity;
207 }
208
209 /**
210 * The initializer code, called before the processing
211 *
212 * @return void
213 */
214 public function fini() {
215 }
216
217 /**
218 * Return the field ids and names (with groups) for import purpose.
219 *
220 * @param int $id
221 * Custom group ID.
222 *
223 * @return array
224 *
225 * @static
226 */
227 public function getGroupFieldsForImport($id) {
228 $importableFields = array();
229 $params = array('custom_group_id' => $id);
230 $allFields = civicrm_api3('custom_field', 'get', $params);
231 $fields = $allFields['values'];
232 foreach ($fields as $id => $values) {
233 $datatype = CRM_Utils_Array::value('data_type', $values);
234 if ($datatype == 'File') {
235 continue;
236 }
237 /* generate the key for the fields array */
238 $key = "custom_$id";
239 $regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array::value(0, $values));
240 $importableFields[$key] = array(
241 'name' => $key,
242 'title' => CRM_Utils_Array::value('label', $values),
243 'headerPattern' => '/' . preg_quote($regexp, '/') . '/',
244 'import' => 1,
245 'custom_field_id' => $id,
246 'options_per_line' => CRM_Utils_Array::value('options_per_line', $values),
247 'data_type' => CRM_Utils_Array::value('data_type', $values),
248 'html_type' => CRM_Utils_Array::value('html_type', $values),
249 'is_search_range' => CRM_Utils_Array::value('is_search_range', $values),
250 );
251 if (CRM_Utils_Array::value('html_type', $values) == 'Select Date') {
252 $importableFields[$key]['date_format'] = CRM_Utils_Array::value('date_format', $values);
253 $importableFields[$key]['time_format'] = CRM_Utils_Array::value('time_format', $values);
254 $this->_dateFields[] = $key;
255 }
256 }
257 return $importableFields;
258 }
259 }