Merge pull request #14467 from mlutfy/ts-fixes-5-16
[civicrm-core.git] / CRM / Core / Form / Task / Batch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2019
31 */
32
33 /**
34 * This class provides the functionality for batch profile update
35 */
36 class CRM_Core_Form_Task_Batch extends CRM_Core_Form_Task {
37
38 /**
39 * The title of the group.
40 *
41 * @var string
42 */
43 protected $_title;
44
45 /**
46 * Maximum profile fields that will be displayed.
47 * @var int
48 */
49 protected $_maxFields = 9;
50
51 /**
52 * Fields that belong to this UF Group.
53 *
54 * @var array
55 */
56 protected $_fields;
57
58 /**
59 * Must be set to entity table name (eg. civicrm_participant) by child class
60 * @var string
61 */
62 public static $tableName = NULL;
63 /**
64 * Must be set to entity shortname (eg. event)
65 * @var string
66 */
67 public static $entityShortname = NULL;
68
69 /**
70 * Build all the data structures needed to build the form.
71 *
72 * @return void
73 */
74 public function preProcess() {
75 // initialize the task and row fields
76 parent::preProcess();
77
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
83 )
84 );
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
89 );
90
91 $this->assign('contactDetails', $contactDetails);
92 $this->assign('readOnlyFields', $readOnlyFields);
93 }
94
95 /**
96 * Build the form object.
97 *
98 *
99 * @return void
100 */
101 public function buildQuickForm() {
102 $ufGroupId = $this->get('ufGroupId');
103
104 if (!$ufGroupId) {
105 throw new InvalidArgumentException('ufGroupId is missing');
106 }
107 $this->_title = ts("Update multiple %1s", [1 => $this::$entityShortname]) . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
108 CRM_Utils_System::setTitle($this->_title);
109
110 $this->addDefaultButtons(ts('Save'));
111 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
112
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)
119 ) {
120 $suppressFields = TRUE;
121 unset($this->_fields[$name]);
122 }
123
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;
128 }
129 }
130
131 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
132
133 $this->addButtons([
134 [
135 'type' => 'submit',
136 'name' => ts('Update'),
137 'isDefault' => TRUE,
138 ],
139 [
140 'type' => 'cancel',
141 'name' => ts('Cancel'),
142 ],
143 ]);
144
145 $this->assign('profileTitle', $this->_title);
146 $this->assign('componentIds', $this->_entityIds);
147
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']
158 );
159 }
160 if ((CRM_Utils_Array::value($typeId, $entityColumnValue)) ||
161 CRM_Utils_System::isNull($entityColumnValue[$typeId])
162 ) {
163 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $entityId);
164 }
165 }
166 else {
167 // handle non custom fields
168 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $entityId);
169 }
170 }
171 }
172
173 $this->assign('fields', $this->_fields);
174
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');
179 }
180
181 $this->addDefaultButtons(ts('Update'));
182
183 $taskComponent['lc'] = $this::$entityShortname;
184 $taskComponent['ucfirst'] = ucfirst($this::$entityShortname);
185 $this->assign('taskComponent', $taskComponent);
186 }
187
188 /**
189 * Set default values for the form.
190 *
191 * @return array $defaults
192 */
193 public function setDefaultValues() {
194 if (empty($this->_fields)) {
195 return [];
196 }
197
198 $defaults = [];
199 foreach ($this->_entityIds as $entityId) {
200 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $entityId, ucfirst($this::$entityShortname));
201 }
202
203 return $defaults;
204 }
205
206 /**
207 * Process the form after the input has been submitted and validated.
208 * Normally the child class will override this
209 *
210 * @return void
211 */
212 public function postProcess() {
213 $params = $this->exportValues();
214
215 if (!isset($params['field'])) {
216 CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');
217 return;
218 }
219 }
220
221 }