(NFC) Update CRM/Core CRM/Custom CRM/Dedupe to match the new coder style
[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 * @var array Fields that belong to this UF Group
53 */
54 protected $_fields;
55
56 /**
57 * Must be set to entity table name (eg. civicrm_participant) by child class
58 * @var string
59 */
60 public static $tableName = NULL;
61 /**
62 * Must be set to entity shortname (eg. event)
63 * @var string
64 */
65 public static $entityShortname = NULL;
66
67 /**
68 * Build all the data structures needed to build the form.
69 *
70 * @return void
71 */
72 public function preProcess() {
73 // initialize the task and row fields
74 parent::preProcess();
75
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
81 )
82 );
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
87 );
88
89 $this->assign('contactDetails', $contactDetails);
90 $this->assign('readOnlyFields', $readOnlyFields);
91 }
92
93 /**
94 * Build the form object.
95 *
96 *
97 * @return void
98 */
99 public function buildQuickForm() {
100 $ufGroupId = $this->get('ufGroupId');
101
102 if (!$ufGroupId) {
103 throw new InvalidArgumentException('ufGroupId is missing');
104 }
105 $this->_title = ts("Update multiple %1s", [1 => $this::$entityShortname]) . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
106 CRM_Utils_System::setTitle($this->_title);
107
108 $this->addDefaultButtons(ts('Save'));
109 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
110
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)
117 ) {
118 $suppressFields = TRUE;
119 unset($this->_fields[$name]);
120 }
121
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;
126 }
127 }
128
129 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
130
131 $this->addButtons([
132 [
133 'type' => 'submit',
134 'name' => ts('Update ' . ucfirst($this::$entityShortname) . 's)'),
135 'isDefault' => TRUE,
136 ],
137 [
138 'type' => 'cancel',
139 'name' => ts('Cancel'),
140 ],
141 ]);
142
143 $this->assign('profileTitle', $this->_title);
144 $this->assign('componentIds', $this->_entityIds);
145
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']
156 );
157 }
158 if ((CRM_Utils_Array::value($typeId, $entityColumnValue)) ||
159 CRM_Utils_System::isNull($entityColumnValue[$typeId])
160 ) {
161 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $entityId);
162 }
163 }
164 else {
165 // handle non custom fields
166 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $entityId);
167 }
168 }
169 }
170
171 $this->assign('fields', $this->_fields);
172
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');
177 }
178
179 $this->addDefaultButtons(ts('Update ' . ucfirst($this::$entityShortname) . 's'));
180
181 $taskComponent['lc'] = $this::$entityShortname;
182 $taskComponent['ucfirst'] = ucfirst($this::$entityShortname);
183 $this->assign('taskComponent', $taskComponent);
184 }
185
186 /**
187 * Set default values for the form.
188 *
189 * @return array $defaults
190 */
191 public function setDefaultValues() {
192 if (empty($this->_fields)) {
193 return [];
194 }
195
196 $defaults = [];
197 foreach ($this->_entityIds as $entityId) {
198 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $entityId, ucfirst($this::$entityShortname));
199 }
200
201 return $defaults;
202 }
203
204 /**
205 * Process the form after the input has been submitted and validated.
206 * Normally the child class will override this
207 *
208 * @return void
209 */
210 public function postProcess() {
211 $params = $this->exportValues();
212
213 if (!isset($params['field'])) {
214 CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');
215 return;
216 }
217 }
218
219 }