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 * Fields that belong to this UF Group.
59 * Must be set to entity table name (eg. civicrm_participant) by child class
62 public static $tableName = NULL;
64 * Must be set to entity shortname (eg. event)
67 public static $entityShortname = NULL;
70 * Build all the data structures needed to build the form.
74 public function preProcess() {
75 // initialize the task and row fields
78 // get the contact read only fields to display.
79 $readOnlyFields = array_merge(['sort_name' => ts('Name')],
80 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
81 'contact_autocomplete_options',
82 TRUE, NULL, FALSE, 'name', TRUE
85 // get the read only field data.
86 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
87 $contactDetails = CRM_Contact_BAO_Contact_Utils
::contactDetails($this->_entityIds
,
88 'Civi' . ucfirst($this::$entityShortname), $returnProperties
91 $this->assign('contactDetails', $contactDetails);
92 $this->assign('readOnlyFields', $readOnlyFields);
96 * Build the form object.
101 public function buildQuickForm() {
102 $ufGroupId = $this->get('ufGroupId');
105 throw new InvalidArgumentException('ufGroupId is missing');
107 $this->_title
= ts("Update multiple %1s", [1 => $this::$entityShortname]) . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
108 CRM_Utils_System
::setTitle($this->_title
);
110 $this->addDefaultButtons(ts('Save'));
111 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
113 // remove file type field and then limit fields
114 $suppressFields = FALSE;
115 $removeHtmlTypes = ['File'];
116 foreach ($this->_fields
as $name => $field) {
117 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
118 in_array($this->_fields
[$name]['html_type'], $removeHtmlTypes)
120 $suppressFields = TRUE;
121 unset($this->_fields
[$name]);
124 //fix to reduce size as we are using this field in grid
125 if (is_array($field['attributes']) && !empty($this->_fields
[$name]['attributes']['size']) && $this->_fields
[$name]['attributes']['size'] > 19) {
126 //shrink class to "form-text-medium"
127 $this->_fields
[$name]['attributes']['size'] = 19;
131 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
136 'name' => ts('Update'),
141 'name' => ts('Cancel'),
145 $this->assign('profileTitle', $this->_title
);
146 $this->assign('componentIds', $this->_entityIds
);
148 $customFields = CRM_Core_BAO_CustomField
::getFields(ucfirst($this::$entityShortname));
149 foreach ($this->_entityIds
as $entityId) {
150 $typeId = CRM_Core_DAO
::getFieldValue('CRM_' . ucfirst($this::$entityShortname) . '_DAO_' . ucfirst($this::$entityShortname), $entityId, $this::$entityShortname . '_type_id');
151 foreach ($this->_fields
as $name => $field) {
152 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
153 $customValue = CRM_Utils_Array
::value($customFieldID, $customFields);
154 $entityColumnValue = [];
155 if (!empty($customValue['extends_entity_column_value'])) {
156 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
157 $customValue['extends_entity_column_value']
160 if ((CRM_Utils_Array
::value($typeId, $entityColumnValue)) ||
161 CRM_Utils_System
::isNull($entityColumnValue[$typeId])
163 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $entityId);
167 // handle non custom fields
168 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $entityId);
173 $this->assign('fields', $this->_fields
);
175 // don't set the status message when form is submitted.
176 $buttonName = $this->controller
->getButtonName('submit');
177 if ($suppressFields && $buttonName != '_qf_Batch_next') {
178 CRM_Core_Session
::setStatus(ts("File type fields in the selected profile are not supported for Update multiple %1s", [1 => $this::$entityShortname]), ts('Unsupported Field Type'), 'error');
181 $this->addDefaultButtons(ts('Update'));
183 $taskComponent['lc'] = $this::$entityShortname;
184 $taskComponent['ucfirst'] = ucfirst($this::$entityShortname);
185 $this->assign('taskComponent', $taskComponent);
189 * Set default values for the form.
191 * @return array $defaults
193 public function setDefaultValues() {
194 if (empty($this->_fields
)) {
199 foreach ($this->_entityIds
as $entityId) {
200 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $entityId, ucfirst($this::$entityShortname));
207 * Process the form after the input has been submitted and validated.
208 * Normally the child class will override this
212 public function postProcess() {
213 $params = $this->exportValues();
215 if (!isset($params['field'])) {
216 CRM_Core_Session
::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');