Merge branch 'CRM-15714' of https://github.com/adixon/civicrm-core into adixon-CRM...
[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 * @param array $mapperKeys
24 * @param null $mapperLocType
25 * @param null $mapperPhoneType
26 */
27 public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
28 parent::__construct();
29 $this->_mapperKeys = &$mapperKeys;
30 }
31
32 public function setFields() {
33 $customGroupID = $this->_multipleCustomData;
34 $importableFields = $this->getGroupFieldsForImport($customGroupID, $this);
35 $this->_fields = array_merge(array(
36 'do_not_import' => array('title' => ts('- do not import -')),
37 'contact_id' => array('title' => ts('Contact ID')),
38 ), $importableFields);
39 }
40
41 /**
42 * The initializer code, called before the processing
43 *
44 * @return void
45 */
46 public function init() {
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 /**
61 * Handle the values in mapField mode.
62 *
63 * @param array $values
64 * The array of values belonging to this line.
65 *
66 * @return bool
67 */
68 public function mapField(&$values) {
69 return CRM_Import_Parser::VALID;
70 }
71
72 /**
73 * Handle the values in preview mode.
74 *
75 * @param array $values
76 * The array of values belonging to this line.
77 *
78 * @return bool
79 * the result of this processing
80 */
81 public function preview(&$values) {
82 return $this->summary($values);
83 }
84
85 /**
86 * @param array $values
87 * The array of values belonging to this line.
88 *
89 * @return bool
90 * the result of this processing
91 * It is called from both the preview & the import actions
92 *
93 * @see CRM_Custom_Import_Parser_BaseClass::summary()
94 */
95 public function summary(&$values) {
96 $erroneousField = NULL;
97 $response = $this->setActiveFieldValues($values, $erroneousField);
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;
133 }
134
135 /**
136 * Handle the values in import mode.
137 *
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.
142 *
143 * @return bool
144 * the result of this processing
145 */
146 public function import($onDuplicate, &$values) {
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
170 CRM_Contact_Import_Parser_Contact::formatCommonData($this->_params, $formatted, $formatted);
171 foreach ($formatted['custom'] as $key => $val) {
172 $this->_params['custom_' . $key] = $val[-1]['value'];
173 }
174 $this->_params['skipRecentView'] = TRUE;
175 $this->_params['check_permissions'] = TRUE;
176 $this->_params['entity_id'] = $formatted['id'];
177 try {
178 civicrm_api3('custom_value', 'create', $this->_params);
179 }
180 catch (CiviCRM_API3_Exception $e) {
181 $error = $e->getMessage();
182 array_unshift($values, $error);
183 return CRM_Import_Parser::ERROR;
184 }
185 }
186
187 /**
188 * Format Date params.
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
192 */
193 public function formatDateParams() {
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 /**
205 * Set import entity.
206 * @param string $entity
207 */
208 public function setEntity($entity) {
209 $this->_entity = $entity;
210 $this->_multipleCustomData = $entity;
211 }
212
213 /**
214 * The initializer code, called before the processing
215 *
216 * @return void
217 */
218 public function fini() {
219 }
220
221 /**
222 * Return the field ids and names (with groups) for import purpose.
223 *
224 * @param int $id
225 * Custom group ID.
226 *
227 * @return array
228 *
229 */
230 public function getGroupFieldsForImport($id) {
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);
237 if ($datatype == 'File') {
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 }
262
263 }