3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * This class provides the functionality for batch profile update
39 class CRM_Contact_Form_Task_Batch
extends CRM_Contact_Form_Task
{
42 * the title of the group
49 * maximum contacts that should be allowed to update
52 protected $_maxContacts = 100;
55 * maximum profile fields that will be displayed
58 protected $_maxFields = 9;
61 * variable to store redirect path
64 protected $_userContext;
67 * when not to reset sort_name
69 protected $_preserveDefault = TRUE;
72 * the internal QF names for the state/country/county fields
75 protected $_stateCountryCountyFields = array();
78 * build all the data structures needed to build the form
83 function preProcess() {
85 * initialize the task and row fields
97 function buildQuickForm() {
98 $ufGroupId = $this->get('ufGroupId');
101 CRM_Core_Error
::fatal('ufGroupId is missing');
103 $this->_title
= ts('Batch Update') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
104 CRM_Utils_System
::setTitle($this->_title
);
106 $this->addDefaultButtons(ts('Save'));
107 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
109 // remove file type field and then limit fields
110 $suppressFields = FALSE;
111 $removehtmlTypes = array('File', 'Autocomplete-Select');
112 foreach ($this->_fields
as $name => $field) {
113 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
114 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
116 $suppressFields = TRUE;
117 unset($this->_fields
[$name]);
121 //FIX ME: phone ext field is added at the end and it gets removed because of below code
122 //$this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
124 $this->addButtons(array(
127 'name' => ts('Update Contact(s)'),
132 'name' => ts('Cancel'),
138 $this->assign('profileTitle', $this->_title
);
139 $this->assign('componentIds', $this->_contactIds
);
141 // if below fields are missing we should not reset sort name / display name
143 $preserveDefaultsArray = array(
144 'first_name', 'last_name', 'middle_name',
149 $stateCountryMap = array();
151 foreach ($this->_contactIds
as $contactId) {
152 $profileFields = $this->_fields
;
153 CRM_Core_BAO_Address
::checkContactSharedAddressFields($profileFields, $contactId);
154 foreach ($profileFields as $name => $field) {
156 // Link state to country, county to state per location per contact
157 list($prefixName, $index) = CRM_Utils_System
::explode('-', $name, 2);
158 if ($prefixName == 'state_province' ||
$prefixName == 'country' ||
$prefixName == 'county') {
159 $stateCountryMap["$index-$contactId"][$prefixName] = "field_{$contactId}_{$field['name']}";
160 $this->_stateCountryCountyFields
["$index-$contactId"][$prefixName] = "field[{$contactId}][{$field['name']}]";
163 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $contactId);
165 if (in_array($field['name'], $preserveDefaultsArray)) {
166 $this->_preserveDefault
= FALSE;
171 CRM_Core_BAO_Address
::addStateCountryMap($stateCountryMap);
175 $this->assign('fields', $this->_fields
);
177 // don't set the status message when form is submitted.
178 $buttonName = $this->controller
->getButtonName('submit');
180 if ($suppressFields && $buttonName != '_qf_BatchUpdateProfile_next') {
181 CRM_Core_Session
::setStatus(ts("File or Autocomplete Select type field(s) in the selected profile are not supported for Batch Update and have been excluded."), ts('Some Fields Excluded'), 'info');
184 $this->addDefaultButtons(ts('Update Contacts'));
185 $this->addFormRule(array('CRM_Contact_Form_Task_Batch', 'formRule'));
189 * This function sets the default values for the form.
195 function setDefaultValues() {
196 if (empty($this->_fields
)) {
200 $defaults = $sortName = array();
201 foreach ($this->_contactIds
as $contactId) {
202 $details[$contactId] = array();
205 $sortName[$contactId] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
210 CRM_Core_BAO_UFGroup
::setProfileDefaults($contactId, $this->_fields
, $defaults, FALSE);
213 $this->assign('sortName', $sortName);
215 // now fix all state country selectors
216 CRM_Core_BAO_Address
::fixAllStateSelects($this, $defaults, $this->_stateCountryCountyFields
);
224 * @param array $fields the input form values
226 * @return true if no errors, else array of errors
230 static function formRule($fields) {
232 $externalIdentifiers = array();
233 foreach ($fields['field'] as $componentId => $field) {
234 foreach ($field as $fieldName => $fieldValue) {
235 if ($fieldName == 'external_identifier') {
236 if (in_array($fieldValue, $externalIdentifiers)) {
237 $errors["field[$componentId][external_identifier]"] = ts('Duplicate value for External Identifier.');
240 $externalIdentifiers[$componentId] = $fieldValue;
250 * process the form after the input has been submitted and validated
256 public function postProcess() {
257 $params = $this->exportValues();
259 $ufGroupId = $this->get('ufGroupId');
261 $inValidSubtypeCnt = 0;
262 //send profile notification email if 'notify' field is set
263 $notify = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $ufGroupId, 'notify');
264 foreach ($params['field'] as $key => $value) {
267 //validate subtype before updating
268 if (CRM_Utils_Array
::value('contact_sub_type', $value) && !CRM_Contact_BAO_ContactType
::isAllowEdit($key)) {
269 unset($value['contact_sub_type']);
270 $inValidSubtypeCnt++
;
273 $value['preserveDBName'] = $this->_preserveDefault
;
275 //parse street address, CRM-7768
276 self
::parseStreetAddress($value, $this);
278 CRM_Contact_BAO_Contact
::createProfileContact($value, $this->_fields
, $key, NULL, $ufGroupId);
280 $values = CRM_Core_BAO_UFGroup
::checkFieldsEmptyValues($ufGroupId, $key, NULL);
281 CRM_Core_BAO_UFGroup
::commonSendMail($key, $values);
285 CRM_Core_Session
::setStatus('', ts("Updates Saved"), 'success');
286 if ($inValidSubtypeCnt) {
287 CRM_Core_Session
::setStatus(ts('Contact SubType field of 1 contact has not been updated.', array('plural' => 'Contact SubType field of %count contacts has not been updated.', 'count' => $inValidSubtypeCnt)), ts('Invalid Subtype'));
293 * parse street address
294 * @param array $contactValues contact values
295 * @param object $form form object
297 public static function parseStreetAddress(&$contactValues, &$form) {
298 if (!is_array($contactValues) ||
!is_array($form->_fields
)) {
302 static $parseAddress;
303 $addressFldKey = 'street_address';
304 if (!isset($parseAddress)) {
305 $parseAddress = FALSE;
306 foreach ($form->_fields
as $key => $fld) {
307 if (strpos($key, $addressFldKey) !== FALSE) {
308 $parseAddress = CRM_Utils_Array
::value('street_address_parsing',
309 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
319 if (!$parseAddress) {
323 $allParseValues = array();
324 foreach ($contactValues as $key => $value) {
325 if (strpos($key, $addressFldKey) !== FALSE) {
326 $locTypeId = substr($key, strlen($addressFldKey) +
1);
328 // parse address field.
329 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($value);
331 //street address consider to be parsed properly,
332 //If we get street_name and street_number.
333 if (!CRM_Utils_Array
::value('street_name', $parsedFields) ||
334 !CRM_Utils_Array
::value('street_number', $parsedFields)
336 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
339 //merge parse values.
340 foreach ($parsedFields as $fldKey => $parseVal) {
342 $fldKey .= "-{$locTypeId}";
344 $allParseValues[$fldKey] = $parseVal;
349 //finally merge all parse values
350 if (!empty($allParseValues)) {
351 $contactValues +
= $allParseValues;