3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2019
34 * This class provides the functionality for batch profile update
36 class CRM_Core_Form_Task_Batch
extends CRM_Core_Form_Task
{
39 * The title of the group.
46 * Maximum profile fields that will be displayed.
49 protected $_maxFields = 9;
52 * @var array Fields that belong to this UF Group
57 * Must be set to entity table name (eg. civicrm_participant) by child class
60 public static $tableName = NULL;
62 * Must be set to entity shortname (eg. event)
65 public static $entityShortname = NULL;
68 * Build all the data structures needed to build the form.
72 public function preProcess() {
73 // initialize the task and row fields
76 // get the contact read only fields to display.
77 $readOnlyFields = array_merge(['sort_name' => ts('Name')],
78 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
79 'contact_autocomplete_options',
80 TRUE, NULL, FALSE, 'name', TRUE
83 // get the read only field data.
84 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
85 $contactDetails = CRM_Contact_BAO_Contact_Utils
::contactDetails($this->_entityIds
,
86 'Civi' . ucfirst($this::$entityShortname), $returnProperties
89 $this->assign('contactDetails', $contactDetails);
90 $this->assign('readOnlyFields', $readOnlyFields);
94 * Build the form object.
99 public function buildQuickForm() {
100 $ufGroupId = $this->get('ufGroupId');
103 throw new InvalidArgumentException('ufGroupId is missing');
105 $this->_title
= ts("Update multiple %1s", [1 => $this::$entityShortname]) . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
106 CRM_Utils_System
::setTitle($this->_title
);
108 $this->addDefaultButtons(ts('Save'));
109 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
111 // remove file type field and then limit fields
112 $suppressFields = FALSE;
113 $removeHtmlTypes = ['File'];
114 foreach ($this->_fields
as $name => $field) {
115 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
116 in_array($this->_fields
[$name]['html_type'], $removeHtmlTypes)
118 $suppressFields = TRUE;
119 unset($this->_fields
[$name]);
122 //fix to reduce size as we are using this field in grid
123 if (is_array($field['attributes']) && !empty($this->_fields
[$name]['attributes']['size']) && $this->_fields
[$name]['attributes']['size'] > 19) {
124 //shrink class to "form-text-medium"
125 $this->_fields
[$name]['attributes']['size'] = 19;
129 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
134 'name' => ts('Update ' . ucfirst($this::$entityShortname) . 's)'),
139 'name' => ts('Cancel'),
143 $this->assign('profileTitle', $this->_title
);
144 $this->assign('componentIds', $this->_entityIds
);
146 $customFields = CRM_Core_BAO_CustomField
::getFields(ucfirst($this::$entityShortname));
147 foreach ($this->_entityIds
as $entityId) {
148 $typeId = CRM_Core_DAO
::getFieldValue('CRM_' . ucfirst($this::$entityShortname) . '_DAO_' . ucfirst($this::$entityShortname), $entityId, $this::$entityShortname . '_type_id');
149 foreach ($this->_fields
as $name => $field) {
150 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
151 $customValue = CRM_Utils_Array
::value($customFieldID, $customFields);
152 $entityColumnValue = [];
153 if (!empty($customValue['extends_entity_column_value'])) {
154 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
155 $customValue['extends_entity_column_value']
158 if ((CRM_Utils_Array
::value($typeId, $entityColumnValue)) ||
159 CRM_Utils_System
::isNull($entityColumnValue[$typeId])
161 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $entityId);
165 // handle non custom fields
166 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $entityId);
171 $this->assign('fields', $this->_fields
);
173 // don't set the status message when form is submitted.
174 $buttonName = $this->controller
->getButtonName('submit');
175 if ($suppressFields && $buttonName != '_qf_Batch_next') {
176 CRM_Core_Session
::setStatus(ts("File type field(s) in the selected profile are not supported for Update multiple %1s", [1 => $this::$entityShortname]), ts('Unsupported Field Type'), 'error');
179 $this->addDefaultButtons(ts('Update ' . ucfirst($this::$entityShortname) . 's'));
181 $taskComponent['lc'] = $this::$entityShortname;
182 $taskComponent['ucfirst'] = ucfirst($this::$entityShortname);
183 $this->assign('taskComponent', $taskComponent);
187 * Set default values for the form.
189 * @return array $defaults
191 public function setDefaultValues() {
192 if (empty($this->_fields
)) {
197 foreach ($this->_entityIds
as $entityId) {
198 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $entityId, ucfirst($this::$entityShortname));
205 * Process the form after the input has been submitted and validated.
206 * Normally the child class will override this
210 public function postProcess() {
211 $params = $this->exportValues();
213 if (!isset($params['field'])) {
214 CRM_Core_Session
::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');