4 * Class CRM_Custom_Import_Parser_Api
6 class CRM_Custom_Import_Parser_Api
extends CRM_Custom_Import_Parser
{
8 protected $_entity = '';
9 protected $_fields = array();
10 protected $_requiredFields = array();
11 protected $_dateFields = array();
12 protected $_multipleCustomData = '';
15 * Params for the current entity being prepared for the api
18 protected $_params = array();
23 public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
24 parent
::__construct();
25 $this->_mapperKeys
= &$mapperKeys;
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);
38 * The initializer code, called before the processing
42 public function init() {
44 $fields = $this->_fields
;
45 $hasLocationType = FALSE;
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);
53 $this->setActiveFields($this->_mapperKeys
);
57 * Handle the values in mapField mode
59 * @param array $values
60 * The array of values belonging to this line.
64 public function mapField(&$values) {
65 return CRM_Import_Parser
::VALID
;
69 * Handle the values in preview mode
71 * @param array $values
72 * The array of values belonging to this line.
75 * the result of this processing
77 public function preview(&$values) {
78 return $this->summary($values);
82 * @param array $values
83 * The array of values belonging to this line.
86 * the result of this processing
87 * It is called from both the preview & the import actions
89 * @see CRM_Custom_Import_Parser_BaseClass::summary()
91 public function summary(&$values) {
92 $erroneousField = NULL;
93 $response = $this->setActiveFieldValues($values, $erroneousField);
94 $errorRequired = FALSE;
96 $this->_params
= &$this->getActiveFieldParams();
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);
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);
111 if ($errorRequired) {
112 array_unshift($values, ts('Missing required field(s) :') . $missingField);
113 return CRM_Import_Parser
::ERROR
;
116 $errorMessage = NULL;
118 $contactType = $this->_contactType ?
$this->_contactType
: 'Organization';
119 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($this->_params
, $errorMessage, $contactType, NULL);
123 $tempMsg = "Invalid value for field(s) : $errorMessage";
124 array_unshift($values, $tempMsg);
125 $errorMessage = NULL;
126 return CRM_Import_Parser
::ERROR
;
128 return CRM_Import_Parser
::VALID
;
132 * Handle the values in import mode
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.
140 * the result of this processing
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)",
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);
155 $params = $this->getActiveFieldParams();
156 $contactType = $this->_contactType ?
$this->_contactType
: 'Organization';
158 'contact_type' => $contactType,
160 $session = CRM_Core_Session
::singleton();
161 $dateType = $session->get('dateTypes');
163 $formatted['id'] = $this->_params
['contact_id'];
164 $setDateFields = array_intersect_key($this->_params
, array_flip($this->_dateFields
));
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'];
170 $this->_params
['skipRecentView'] = TRUE;
171 $this->_params
['check_permissions'] = TRUE;
172 $this->_params
['entity_id'] = $formatted['id'];
174 civicrm_api3('custom_value', 'create', $this->_params
);
176 catch (CiviCRM_API3_Exception
$e) {
177 $error = $e->getMessage();
178 array_unshift($values, $error);
179 return CRM_Import_Parser
::ERROR
;
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
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
));
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]);
202 * @param string $entity
204 public function setEntity($entity) {
205 $this->_entity
= $entity;
206 $this->_multipleCustomData
= $entity;
210 * The initializer code, called before the processing
214 public function fini() {
218 * Return the field ids and names (with groups) for import purpose.
226 public function getGroupFieldsForImport($id) {
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);
233 if ($datatype == 'File') {
236 /* generate the key for the fields array */
238 $regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array
::value(0, $values));
239 $importableFields[$key] = array(
241 'title' => CRM_Utils_Array
::value('label', $values),
242 'headerPattern' => '/' . preg_quote($regexp, '/') . '/',
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),
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;
256 return $importableFields;