2 class CRM_Custom_Import_Parser_Api
extends CRM_Custom_Import_Parser
{
4 protected $_entity = '';
5 protected $_fields = array();
6 protected $_requiredFields = array();
7 protected $_dateFields = array();
8 protected $_multipleCustomData = '';
11 * Params for the current entity being prepared for the api
14 protected $_params = array();
18 function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
19 parent
::__construct();
20 $this->_mapperKeys
= &$mapperKeys;
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);
29 * the initializer code, called before the processing
36 $fields = $this->_fields
;
37 $hasLocationType = FALSE;
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);
45 $this->setActiveFields($this->_mapperKeys
);
49 * handle the values in mapField mode
51 * @param array $values the array of values belonging to this line
56 function mapField(&$values) {
57 return CRM_Import_Parser
::VALID
;
61 * handle the values in preview mode
63 * @param array $values the array of values belonging to this line
65 * @return boolean the result of this processing
68 function preview(&$values) {
69 return $this->summary($values);
73 * @param array $values the array of values belonging to this line
75 * @return boolean the result of this processing
76 * It is called from both the preview & the import actions
78 * @see CRM_Custom_Import_Parser_BaseClass::summary()
80 function summary(&$values) {
81 $erroneousField = NULL;
82 $response = $this->setActiveFieldValues($values, $erroneousField);
83 $errorRequired = FALSE;
85 $this->_params
= &$this->getActiveFieldParams();
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);
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);
100 if ($errorRequired) {
101 array_unshift($values, ts('Missing required field(s) :') . $missingField);
102 return CRM_Import_Parser
::ERROR
;
105 $errorMessage = NULL;
107 $contactType = $this->_contactType ?
$this->_contactType
: 'Organization';
108 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($this->_params
, $errorMessage, $contactType, NULL);
112 $tempMsg = "Invalid value for field(s) : $errorMessage";
113 array_unshift($values, $tempMsg);
114 $errorMessage = NULL;
115 return CRM_Import_Parser
::ERROR
;
117 return CRM_Import_Parser
::VALID
;
121 * handle the values in import mode
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
126 * @return boolean the result of this processing
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)",
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);
142 $params = $this->getActiveFieldParams();
143 $contactType = $this->_contactType ?
$this->_contactType
: 'Organization';
145 'contact_type' => $contactType,
147 $session = CRM_Core_Session
::singleton();
148 $dateType = $session->get('dateTypes');
150 $formatted['id'] = $this->_params
['contact_id'];
151 $setDateFields = array_intersect_key($this->_params
, array_flip($this->_dateFields
));
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'];
157 $this->_params
['skipRecentView'] = TRUE;
158 $this->_params
['check_permissions'] = TRUE;
159 $this->_params
['entity_id'] = $formatted['id'];
161 civicrm_api3('custom_value', 'create', $this->_params
);
163 catch(CiviCRM_API3_Exception
$e) {
164 $error = $e->getMessage();
165 array_unshift($values, $error);
166 return CRM_Import_Parser
::ERROR
;
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
175 * @param unknown $params
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
));
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]);
190 * @param string $entity
192 function setEntity($entity) {
193 $this->_entity
= $entity;
194 $this->_multipleCustomData
= $entity;
198 * the initializer code, called before the processing
206 * Return the field ids and names (with groups) for import purpose.
208 * @param int $id Custom group ID
210 * @return array $importableFields
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' ) {
225 /* generate the key for the fields array */
227 $regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array
::value(0, $values));
228 $importableFields[$key] = array(
230 'title' => CRM_Utils_Array
::value('label', $values),
231 'headerPattern' => '/' . preg_quote($regexp, '/') . '/',
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),
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;
245 return $importableFields;