Merge pull request #12337 from lcdservices/dev-core-190
[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 */
48 protected $_maxFields = 9;
49
50 /**
51 * @var array Fields that belong to this UF Group
52 */
53 protected $_fields;
54
55 // Must be set to entity table name (eg. civicrm_participant) by child class
56 static $tableName = NULL;
57 // Must be set to entity shortname (eg. event)
58 static $entityShortname = NULL;
59
60 /**
61 * Build all the data structures needed to build the form.
62 *
63 * @return void
64 */
65 public function preProcess() {
66 // initialize the task and row fields
67 parent::preProcess();
68
69 // get the contact read only fields to display.
70 $readOnlyFields = array_merge(array('sort_name' => ts('Name')),
71 CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
72 'contact_autocomplete_options',
73 TRUE, NULL, FALSE, 'name', TRUE
74 )
75 );
76 // get the read only field data.
77 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
78 $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_entityIds,
79 'Civi' . ucfirst($this::$entityShortname), $returnProperties
80 );
81
82 $this->assign('contactDetails', $contactDetails);
83 $this->assign('readOnlyFields', $readOnlyFields);
84 }
85
86 /**
87 * Build the form object.
88 *
89 *
90 * @return void
91 */
92 public function buildQuickForm() {
93 $ufGroupId = $this->get('ufGroupId');
94
95 if (!$ufGroupId) {
96 throw new InvalidArgumentException('ufGroupId is missing');
97 }
98 $this->_title = ts("Update multiple %1s", array(1 => $this::$entityShortname)) . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId);
99 CRM_Utils_System::setTitle($this->_title);
100
101 $this->addDefaultButtons(ts('Save'));
102 $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW);
103
104 // remove file type field and then limit fields
105 $suppressFields = FALSE;
106 $removeHtmlTypes = array('File');
107 foreach ($this->_fields as $name => $field) {
108 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) &&
109 in_array($this->_fields[$name]['html_type'], $removeHtmlTypes)
110 ) {
111 $suppressFields = TRUE;
112 unset($this->_fields[$name]);
113 }
114
115 //fix to reduce size as we are using this field in grid
116 if (is_array($field['attributes']) && !empty($this->_fields[$name]['attributes']['size']) && $this->_fields[$name]['attributes']['size'] > 19) {
117 //shrink class to "form-text-medium"
118 $this->_fields[$name]['attributes']['size'] = 19;
119 }
120 }
121
122 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
123
124 $this->addButtons(array(
125 array(
126 'type' => 'submit',
127 'name' => ts('Update ' . ucfirst($this::$entityShortname) . 's)'),
128 'isDefault' => TRUE,
129 ),
130 array(
131 'type' => 'cancel',
132 'name' => ts('Cancel'),
133 ),
134 ));
135
136 $this->assign('profileTitle', $this->_title);
137 $this->assign('componentIds', $this->_entityIds);
138
139 $customFields = CRM_Core_BAO_CustomField::getFields(ucfirst($this::$entityShortname));
140 foreach ($this->_entityIds as $entityId) {
141 $typeId = CRM_Core_DAO::getFieldValue('CRM_' . ucfirst($this::$entityShortname) . '_DAO_' . ucfirst($this::$entityShortname), $entityId, $this::$entityShortname . '_type_id');
142 foreach ($this->_fields as $name => $field) {
143 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
144 $customValue = CRM_Utils_Array::value($customFieldID, $customFields);
145 $entityColumnValue = array();
146 if (!empty($customValue['extends_entity_column_value'])) {
147 $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
148 $customValue['extends_entity_column_value']
149 );
150 }
151 if ((CRM_Utils_Array::value($typeId, $entityColumnValue)) ||
152 CRM_Utils_System::isNull($entityColumnValue[$typeId])
153 ) {
154 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $entityId);
155 }
156 }
157 else {
158 // handle non custom fields
159 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $entityId);
160 }
161 }
162 }
163
164 $this->assign('fields', $this->_fields);
165
166 // don't set the status message when form is submitted.
167 $buttonName = $this->controller->getButtonName('submit');
168 if ($suppressFields && $buttonName != '_qf_Batch_next') {
169 CRM_Core_Session::setStatus(ts("File type field(s) in the selected profile are not supported for Update multiple %1s", array(1 => $this::$entityShortname)), ts('Unsupported Field Type'), 'error');
170 }
171
172 $this->addDefaultButtons(ts('Update ' . ucfirst($this::$entityShortname) . 's'));
173
174 $taskComponent['lc'] = $this::$entityShortname;
175 $taskComponent['ucfirst'] = ucfirst($this::$entityShortname);
176 $this->assign('taskComponent', $taskComponent);
177 }
178
179 /**
180 * Set default values for the form.
181 *
182 * @return array $defaults
183 */
184 public function setDefaultValues() {
185 if (empty($this->_fields)) {
186 return array();
187 }
188
189 $defaults = array();
190 foreach ($this->_entityIds as $entityId) {
191 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $entityId, ucfirst($this::$entityShortname));
192 }
193
194 return $defaults;
195 }
196
197 /**
198 * Process the form after the input has been submitted and validated.
199 * Normally the child class will override this
200 *
201 * @return void
202 */
203 public function postProcess() {
204 $params = $this->exportValues();
205
206 if (!isset($params['field'])) {
207 CRM_Core_Session::setStatus(ts("No updates have been saved."), ts('Not Saved'), 'alert');
208 return;
209 }
210 }
211
212 }