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();
22 public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
23 parent
::__construct();
24 $this->_mapperKeys
= &$mapperKeys;
26 public function setFields() {
27 $customGroupID = $this->_multipleCustomData
;
28 $importableFields = $this->getGroupFieldsForImport($customGroupID, $this);
29 $this->_fields
= array_merge(array('do_not_import' => array('title' => ts('- do not import -')), 'contact_id' => array('title' => ts('Contact ID'))), $importableFields);
33 * The initializer code, called before the processing
37 public function init() {
39 $fields = $this->_fields
;
40 $hasLocationType = FALSE;
42 foreach ($fields as $name => $field) {
43 $field['type'] = CRM_Utils_Array
::value('type', $field, CRM_Utils_Type
::T_INT
);
44 $field['dataPattern'] = CRM_Utils_Array
::value('dataPattern', $field, '//');
45 $field['headerPattern'] = CRM_Utils_Array
::value('headerPattern', $field, '//');
46 $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern'], $hasLocationType);
48 $this->setActiveFields($this->_mapperKeys
);
52 * Handle the values in mapField mode
54 * @param array $values the array of values belonging to this line
58 public function mapField(&$values) {
59 return CRM_Import_Parser
::VALID
;
63 * Handle the values in preview mode
65 * @param array $values the array of values belonging to this line
67 * @return boolean the result of this processing
69 public function preview(&$values) {
70 return $this->summary($values);
74 * @param array $values the array of values belonging to this line
76 * @return boolean the result of this processing
77 * It is called from both the preview & the import actions
79 * @see CRM_Custom_Import_Parser_BaseClass::summary()
81 public function summary(&$values) {
82 $erroneousField = NULL;
83 $response = $this->setActiveFieldValues($values, $erroneousField);
84 $errorRequired = FALSE;
86 $this->_params
= &$this->getActiveFieldParams();
88 $formatted = $this->_params
;
89 $this->_updateWithId
= FALSE;
90 $this->_parseStreetAddress
= CRM_Utils_Array
::value('street_address_parsing', CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'address_options'), FALSE);
92 $this->_params
= $this->getActiveFieldParams();
93 foreach ($this->_requiredFields
as $requiredField) {
94 if (empty($this->_params
[$requiredField])) {
95 $errorRequired = TRUE;
96 $missingField .= ' ' . $requiredField;
97 CRM_Contact_Import_Parser_Contact
::addToErrorMsg($this->_entity
, $requiredField);
101 if ($errorRequired) {
102 array_unshift($values, ts('Missing required field(s) :') . $missingField);
103 return CRM_Import_Parser
::ERROR
;
106 $errorMessage = NULL;
108 $contactType = $this->_contactType ?
$this->_contactType
: 'Organization';
109 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($this->_params
, $errorMessage, $contactType, NULL);
113 $tempMsg = "Invalid value for field(s) : $errorMessage";
114 array_unshift($values, $tempMsg);
115 $errorMessage = NULL;
116 return CRM_Import_Parser
::ERROR
;
118 return CRM_Import_Parser
::VALID
;
122 * Handle the values in import mode
124 * @param int $onDuplicate the code for what action to take on duplicates
125 * @param array $values the array of values belonging to this line
127 * @return boolean the result of this processing
129 public 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
176 public function formatDateParams() {
177 $session = CRM_Core_Session
::singleton();
178 $dateType = $session->get('dateTypes');
179 $setDateFields = array_intersect_key($this->_params
, array_flip($this->_dateFields
));
181 foreach ($setDateFields as $key => $value) {
182 CRM_Utils_Date
::convertToDefaultDate($this->_params
, $dateType, $key);
183 $this->_params
[$key] = CRM_Utils_Date
::processDate($this->_params
[$key]);
189 * @param string $entity
191 public function setEntity($entity) {
192 $this->_entity
= $entity;
193 $this->_multipleCustomData
= $entity;
197 * The initializer code, called before the processing
201 public function fini() {}
204 * Return the field ids and names (with groups) for import purpose.
206 * @param int $id Custom group ID
208 * @return array $importableFields
212 public function getGroupFieldsForImport( $id ) {
213 $importableFields = array();
214 $params = array('custom_group_id' => $id);
215 $allFields = civicrm_api3('custom_field', 'get', $params);
216 $fields = $allFields['values'];
217 foreach ($fields as $id => $values) {
218 $datatype = CRM_Utils_Array
::value('data_type', $values);
219 if ( $datatype == 'File' ) {
222 /* generate the key for the fields array */
224 $regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array
::value(0, $values));
225 $importableFields[$key] = array(
227 'title' => CRM_Utils_Array
::value('label', $values),
228 'headerPattern' => '/' . preg_quote($regexp, '/') . '/',
230 'custom_field_id' => $id,
231 'options_per_line' => CRM_Utils_Array
::value('options_per_line', $values),
232 'data_type' => CRM_Utils_Array
::value('data_type', $values),
233 'html_type' => CRM_Utils_Array
::value('html_type', $values),
234 'is_search_range' => CRM_Utils_Array
::value('is_search_range', $values),
236 if (CRM_Utils_Array
::value('html_type', $values) == 'Select Date') {
237 $importableFields[$key]['date_format'] = CRM_Utils_Array
::value('date_format', $values);
238 $importableFields[$key]['time_format'] = CRM_Utils_Array
::value('time_format', $values);
239 $this->_dateFields
[] = $key;
242 return $importableFields;