PageRenderTime 71ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/CRM/Custom/Import/Parser/Api.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 272 lines | 162 code | 27 blank | 83 comment | 13 complexity | 010311c288aa008f7f38a2a19dca0427 MD5 | raw file
  1. <?php
  2. /**
  3. * Class CRM_Custom_Import_Parser_Api
  4. */
  5. class CRM_Custom_Import_Parser_Api extends CRM_Custom_Import_Parser {
  6. protected $_entity = '';
  7. protected $_fields = [];
  8. protected $_requiredFields = [];
  9. protected $_dateFields = [];
  10. protected $_multipleCustomData = '';
  11. /**
  12. * Params for the current entity being prepared for the api.
  13. * @var array
  14. */
  15. protected $_params = [];
  16. /**
  17. * Class constructor.
  18. *
  19. * @param array $mapperKeys
  20. * @param null $mapperLocType
  21. * @param null $mapperPhoneType
  22. */
  23. public function __construct(&$mapperKeys, $mapperLocType = NULL, $mapperPhoneType = NULL) {
  24. parent::__construct();
  25. $this->_mapperKeys = &$mapperKeys;
  26. }
  27. public function setFields() {
  28. $customGroupID = $this->_multipleCustomData;
  29. $importableFields = $this->getGroupFieldsForImport($customGroupID, $this);
  30. $this->_fields = array_merge([
  31. 'do_not_import' => ['title' => ts('- do not import -')],
  32. 'contact_id' => ['title' => ts('Contact ID')],
  33. 'external_identifier' => ['title' => ts('External Identifier')],
  34. ], $importableFields);
  35. }
  36. /**
  37. * The initializer code, called before the processing
  38. *
  39. * @return void
  40. */
  41. public function init() {
  42. $this->setFields();
  43. $fields = $this->_fields;
  44. $hasLocationType = FALSE;
  45. foreach ($fields as $name => $field) {
  46. $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT);
  47. $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//');
  48. $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//');
  49. $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern'], $hasLocationType);
  50. }
  51. $this->setActiveFields($this->_mapperKeys);
  52. }
  53. /**
  54. * Handle the values in mapField mode.
  55. *
  56. * @param array $values
  57. * The array of values belonging to this line.
  58. *
  59. * @return bool
  60. */
  61. public function mapField(&$values) {
  62. return CRM_Import_Parser::VALID;
  63. }
  64. /**
  65. * Handle the values in preview mode.
  66. *
  67. * @param array $values
  68. * The array of values belonging to this line.
  69. *
  70. * @return bool
  71. * the result of this processing
  72. */
  73. public function preview(&$values) {
  74. return $this->summary($values);
  75. }
  76. /**
  77. * @param array $values
  78. * The array of values belonging to this line.
  79. *
  80. * @return bool
  81. * the result of this processing
  82. * It is called from both the preview & the import actions
  83. *
  84. * @see CRM_Custom_Import_Parser_BaseClass::summary()
  85. */
  86. public function summary(&$values) {
  87. $erroneousField = NULL;
  88. $response = $this->setActiveFieldValues($values, $erroneousField);
  89. $errorRequired = FALSE;
  90. $missingField = '';
  91. $this->_params = &$this->getActiveFieldParams();
  92. $formatted = $this->_params;
  93. $this->_updateWithId = FALSE;
  94. $this->_parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options'), FALSE);
  95. $this->_params = $this->getActiveFieldParams();
  96. foreach ($this->_requiredFields as $requiredField) {
  97. if (empty($this->_params[$requiredField])) {
  98. $errorRequired = TRUE;
  99. $missingField .= ' ' . $requiredField;
  100. CRM_Contact_Import_Parser_Contact::addToErrorMsg($this->_entity, $requiredField);
  101. }
  102. }
  103. if ($errorRequired) {
  104. array_unshift($values, ts('Missing required field(s) :') . $missingField);
  105. return CRM_Import_Parser::ERROR;
  106. }
  107. $errorMessage = NULL;
  108. $contactType = $this->_contactType ? $this->_contactType : 'Organization';
  109. CRM_Contact_Import_Parser_Contact::isErrorInCustomData($this->_params + ['contact_type' => $contactType], $errorMessage, $this->_contactSubType, NULL);
  110. // pseudoconstants
  111. if ($errorMessage) {
  112. $tempMsg = "Invalid value for field(s) : $errorMessage";
  113. array_unshift($values, $tempMsg);
  114. $errorMessage = NULL;
  115. return CRM_Import_Parser::ERROR;
  116. }
  117. return CRM_Import_Parser::VALID;
  118. }
  119. /**
  120. * Handle the values in import mode.
  121. *
  122. * @param int $onDuplicate
  123. * The code for what action to take on duplicates.
  124. * @param array $values
  125. * The array of values belonging to this line.
  126. *
  127. * @return bool
  128. * the result of this processing
  129. */
  130. public function import($onDuplicate, &$values) {
  131. $response = $this->summary($values);
  132. if ($response != CRM_Import_Parser::VALID) {
  133. $importRecordParams = [
  134. $statusFieldName => 'INVALID',
  135. "${statusFieldName}Msg" => "Invalid (Error Code: $response)",
  136. ];
  137. return $response;
  138. }
  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);
  141. $params = $this->getActiveFieldParams();
  142. $contactType = $this->_contactType ? $this->_contactType : 'Organization';
  143. $formatted = [
  144. 'contact_type' => $contactType,
  145. ];
  146. $session = CRM_Core_Session::singleton();
  147. $dateType = $session->get('dateTypes');
  148. if (isset($this->_params['external_identifier']) && !isset($this->_params['contact_id'])) {
  149. $checkCid = new CRM_Contact_DAO_Contact();
  150. $checkCid->external_identifier = $this->_params['external_identifier'];
  151. $checkCid->find(TRUE);
  152. $formatted['id'] = $checkCid->id;
  153. }
  154. else {
  155. $formatted['id'] = $this->_params['contact_id'];
  156. }
  157. $setDateFields = array_intersect_key($this->_params, array_flip($this->_dateFields));
  158. $this->formatCommonData($this->_params, $formatted, $formatted);
  159. foreach ($formatted['custom'] as $key => $val) {
  160. $this->_params['custom_' . $key] = $val[-1]['value'];
  161. }
  162. $this->_params['skipRecentView'] = TRUE;
  163. $this->_params['check_permissions'] = TRUE;
  164. $this->_params['entity_id'] = $formatted['id'];
  165. try {
  166. civicrm_api3('custom_value', 'create', $this->_params);
  167. }
  168. catch (CiviCRM_API3_Exception $e) {
  169. $error = $e->getMessage();
  170. array_unshift($values, $error);
  171. return CRM_Import_Parser::ERROR;
  172. }
  173. }
  174. /**
  175. * Format Date params.
  176. *
  177. * Although the api will accept any strtotime valid string CiviCRM accepts at least one date format
  178. * not supported by strtotime so we should run this through a conversion
  179. */
  180. public function formatDateParams() {
  181. $session = CRM_Core_Session::singleton();
  182. $dateType = $session->get('dateTypes');
  183. $setDateFields = array_intersect_key($this->_params, array_flip($this->_dateFields));
  184. foreach ($setDateFields as $key => $value) {
  185. CRM_Utils_Date::convertToDefaultDate($this->_params, $dateType, $key);
  186. $this->_params[$key] = CRM_Utils_Date::processDate($this->_params[$key]);
  187. }
  188. }
  189. /**
  190. * Set import entity.
  191. * @param string $entity
  192. */
  193. public function setEntity($entity) {
  194. $this->_entity = $entity;
  195. $this->_multipleCustomData = $entity;
  196. }
  197. /**
  198. * The initializer code, called before the processing
  199. *
  200. * @return void
  201. */
  202. public function fini() {
  203. }
  204. /**
  205. * Return the field ids and names (with groups) for import purpose.
  206. *
  207. * @param int $id
  208. * Custom group ID.
  209. *
  210. * @return array
  211. *
  212. */
  213. public function getGroupFieldsForImport($id) {
  214. $importableFields = [];
  215. $params = ['custom_group_id' => $id];
  216. $allFields = civicrm_api3('custom_field', 'get', $params);
  217. $fields = $allFields['values'];
  218. foreach ($fields as $id => $values) {
  219. $datatype = CRM_Utils_Array::value('data_type', $values);
  220. if ($datatype == 'File') {
  221. continue;
  222. }
  223. /* generate the key for the fields array */
  224. $key = "custom_$id";
  225. $regexp = preg_replace('/[.,;:!?]/', '', CRM_Utils_Array::value(0, $values));
  226. $importableFields[$key] = [
  227. 'name' => $key,
  228. 'title' => CRM_Utils_Array::value('label', $values),
  229. 'headerPattern' => '/' . preg_quote($regexp, '/') . '/',
  230. 'import' => 1,
  231. 'custom_field_id' => $id,
  232. 'options_per_line' => CRM_Utils_Array::value('options_per_line', $values),
  233. 'data_type' => CRM_Utils_Array::value('data_type', $values),
  234. 'html_type' => CRM_Utils_Array::value('html_type', $values),
  235. 'is_search_range' => CRM_Utils_Array::value('is_search_range', $values),
  236. ];
  237. if (CRM_Utils_Array::value('html_type', $values) == 'Select Date') {
  238. $importableFields[$key]['date_format'] = CRM_Utils_Array::value('date_format', $values);
  239. $importableFields[$key]['time_format'] = CRM_Utils_Array::value('time_format', $values);
  240. $this->_dateFields[] = $key;
  241. }
  242. }
  243. return $importableFields;
  244. }
  245. }