3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
35 * This class provides the functionality for batch profile update.
37 class CRM_Contact_Form_Task_Batch
extends CRM_Contact_Form_Task
{
40 * The title of the group.
47 * Maximum contacts that should be allowed to update.
49 protected $_maxContacts = 100;
52 * Maximum profile fields that will be displayed.
54 protected $_maxFields = 9;
57 * Variable to store redirect path.
59 protected $_userContext;
62 * When not to reset sort_name.
64 protected $_preserveDefault = TRUE;
67 * Build all the data structures needed to build the form.
69 public function preProcess() {
70 // initialize the task and row fields
75 * Build the form object.
77 public function buildQuickForm() {
78 $ufGroupId = $this->get('ufGroupId');
81 CRM_Core_Error
::fatal('ufGroupId is missing');
83 $this->_title
= ts('Update multiple contacts') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
84 CRM_Utils_System
::setTitle($this->_title
);
86 $this->addDefaultButtons(ts('Save'));
87 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
89 // remove file type field and then limit fields
90 $suppressFields = FALSE;
91 $removehtmlTypes = array('File', 'Autocomplete-Select');
92 foreach ($this->_fields
as $name => $field) {
93 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
94 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
96 $suppressFields = TRUE;
97 unset($this->_fields
[$name]);
101 //FIX ME: phone ext field is added at the end and it gets removed because of below code
102 //$this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
104 $this->addButtons(array(
107 'name' => ts('Update Contact(s)'),
112 'name' => ts('Cancel'),
117 $this->assign('profileTitle', $this->_title
);
118 $this->assign('componentIds', $this->_contactIds
);
120 // if below fields are missing we should not reset sort name / display name
122 $preserveDefaultsArray = array(
132 foreach ($this->_contactIds
as $contactId) {
133 $profileFields = $this->_fields
;
134 CRM_Core_BAO_Address
::checkContactSharedAddressFields($profileFields, $contactId);
135 foreach ($profileFields as $name => $field) {
136 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $contactId);
138 if (in_array($field['name'], $preserveDefaultsArray)) {
139 $this->_preserveDefault
= FALSE;
144 $this->assign('fields', $this->_fields
);
146 // don't set the status message when form is submitted.
147 $buttonName = $this->controller
->getButtonName('submit');
149 if ($suppressFields && $buttonName != '_qf_BatchUpdateProfile_next') {
150 CRM_Core_Session
::setStatus(ts("File or Autocomplete-Select type field(s) in the selected profile are not supported for Update multiple contacts."), ts('Some Fields Excluded'), 'info');
153 $this->addDefaultButtons(ts('Update Contacts'));
154 $this->addFormRule(array('CRM_Contact_Form_Task_Batch', 'formRule'));
158 * Set default values for the form.
163 public function setDefaultValues() {
164 if (empty($this->_fields
)) {
168 $defaults = $sortName = array();
169 foreach ($this->_contactIds
as $contactId) {
170 $details[$contactId] = array();
173 $sortName[$contactId] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
178 CRM_Core_BAO_UFGroup
::setProfileDefaults($contactId, $this->_fields
, $defaults, FALSE);
181 $this->assign('sortName', $sortName);
189 * @param array $fields
190 * The input form values.
193 * true if no errors, else array of errors
195 public static function formRule($fields) {
197 $externalIdentifiers = array();
198 foreach ($fields['field'] as $componentId => $field) {
199 foreach ($field as $fieldName => $fieldValue) {
200 if ($fieldName == 'external_identifier') {
201 if (in_array($fieldValue, $externalIdentifiers)) {
202 $errors["field[$componentId][external_identifier]"] = ts('Duplicate value for External ID.');
205 $externalIdentifiers[$componentId] = $fieldValue;
215 * Process the form after the input has been submitted and validated.
217 public function postProcess() {
218 $params = $this->exportValues();
220 $ufGroupId = $this->get('ufGroupId');
222 $inValidSubtypeCnt = 0;
223 //send profile notification email if 'notify' field is set
224 $notify = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $ufGroupId, 'notify');
225 foreach ($params['field'] as $key => $value) {
228 //validate subtype before updating
229 if (!empty($value['contact_sub_type']) && !CRM_Contact_BAO_ContactType
::isAllowEdit($key)) {
230 unset($value['contact_sub_type']);
231 $inValidSubtypeCnt++
;
234 $value['preserveDBName'] = $this->_preserveDefault
;
236 //parse street address, CRM-7768
237 self
::parseStreetAddress($value, $this);
239 CRM_Contact_BAO_Contact
::createProfileContact($value, $this->_fields
, $key, NULL, $ufGroupId, NULL, TRUE);
241 $values = CRM_Core_BAO_UFGroup
::checkFieldsEmptyValues($ufGroupId, $key, NULL);
242 CRM_Core_BAO_UFGroup
::commonSendMail($key, $values);
246 CRM_Core_Session
::setStatus('', ts("Updates Saved"), 'success');
247 if ($inValidSubtypeCnt) {
248 CRM_Core_Session
::setStatus(ts('Contact Subtype field of 1 contact has not been updated.', array(
249 'plural' => 'Contact Subtype field of %count contacts has not been updated.',
250 'count' => $inValidSubtypeCnt,
251 )), ts('Invalid Subtype'));
256 * Parse street address.
258 * @param array $contactValues
260 * @param CRM_Core_Form $form
263 public static function parseStreetAddress(&$contactValues, &$form) {
264 if (!is_array($contactValues) ||
!is_array($form->_fields
)) {
268 static $parseAddress;
269 $addressFldKey = 'street_address';
270 if (!isset($parseAddress)) {
271 $parseAddress = FALSE;
272 foreach ($form->_fields
as $key => $fld) {
273 if (strpos($key, $addressFldKey) !== FALSE) {
274 $parseAddress = CRM_Utils_Array
::value('street_address_parsing',
275 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
285 if (!$parseAddress) {
289 $allParseValues = array();
290 foreach ($contactValues as $key => $value) {
291 if (strpos($key, $addressFldKey) !== FALSE) {
292 $locTypeId = substr($key, strlen($addressFldKey) +
1);
294 // parse address field.
295 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($value);
297 //street address consider to be parsed properly,
298 //If we get street_name and street_number.
299 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
300 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
303 //merge parse values.
304 foreach ($parsedFields as $fldKey => $parseVal) {
306 $fldKey .= "-{$locTypeId}";
308 $allParseValues[$fldKey] = $parseVal;
313 //finally merge all parse values
314 if (!empty($allParseValues)) {
315 $contactValues +
= $allParseValues;