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 * @param array $mapperKeys
24 * @param null $mapperLocType
25 * @param null $mapperPhoneType
27 public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
28 parent
::__construct();
29 $this->_mapperKeys
= &$mapperKeys;
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);
42 * The initializer code, called before the processing
46 public function init() {
48 $fields = $this->_fields
;
49 $hasLocationType = FALSE;
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);
57 $this->setActiveFields($this->_mapperKeys
);
61 * Handle the values in mapField mode
63 * @param array $values
64 * The array of values belonging to this line.
68 public function mapField(&$values) {
69 return CRM_Import_Parser
::VALID
;
73 * Handle the values in preview mode
75 * @param array $values
76 * The array of values belonging to this line.
79 * the result of this processing
81 public function preview(&$values) {
82 return $this->summary($values);
86 * @param array $values
87 * The array of values belonging to this line.
90 * the result of this processing
91 * It is called from both the preview & the import actions
93 * @see CRM_Custom_Import_Parser_BaseClass::summary()
95 public function summary(&$values) {
96 $erroneousField = NULL;
97 $response = $this->setActiveFieldValues($values, $erroneousField);
98 $errorRequired = FALSE;
100 $this->_params
= &$this->getActiveFieldParams();
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);
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);
115 if ($errorRequired) {
116 array_unshift($values, ts('Missing required field(s) :') . $missingField);
117 return CRM_Import_Parser
::ERROR
;
120 $errorMessage = NULL;
122 $contactType = $this->_contactType ?
$this->_contactType
: 'Organization';
123 CRM_Contact_Import_Parser_Contact
::isErrorInCustomData($this->_params
, $errorMessage, $contactType, NULL);
127 $tempMsg = "Invalid value for field(s) : $errorMessage";
128 array_unshift($values, $tempMsg);
129 $errorMessage = NULL;
130 return CRM_Import_Parser
::ERROR
;
132 return CRM_Import_Parser
::VALID
;
136 * Handle the values in import mode
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.
144 * the result of this processing
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)",
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);
159 $params = $this->getActiveFieldParams();
160 $contactType = $this->_contactType ?
$this->_contactType
: 'Organization';
162 'contact_type' => $contactType,
164 $session = CRM_Core_Session
::singleton();
165 $dateType = $session->get('dateTypes');
167 $formatted['id'] = $this->_params
['contact_id'];
168 $setDateFields = array_intersect_key($this->_params
, array_flip($this->_dateFields
));
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'];
174 $this->_params
['skipRecentView'] = TRUE;
175 $this->_params
['check_permissions'] = TRUE;
176 $this->_params
['entity_id'] = $formatted['id'];
178 civicrm_api3('custom_value', 'create', $this->_params
);
180 catch (CiviCRM_API3_Exception
$e) {
181 $error = $e->getMessage();
182 array_unshift($values, $error);
183 return CRM_Import_Parser
::ERROR
;
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
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
));
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]);
206 * @param string $entity
208 public function setEntity($entity) {
209 $this->_entity
= $entity;
210 $this->_multipleCustomData
= $entity;
214 * The initializer code, called before the processing
218 public function fini() {
222 * Return the field ids and names (with groups) for import purpose.
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') {
240 /* generate the key for the fields array */
242 $regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array
::value(0, $values));
243 $importableFields[$key] = array(
245 'title' => CRM_Utils_Array
::value('label', $values),
246 'headerPattern' => '/' . preg_quote($regexp, '/') . '/',
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),
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;
260 return $importableFields;