(NFC) Update CRM/Core CRM/Custom CRM/Dedupe to match the new coder style
[civicrm-core.git] / CRM / Core / Form / Task / PickProfile.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 for case
35 */
36 abstract class CRM_Core_Form_Task_PickProfile 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 entities that should be allowed to update
47 *
48 * @var int
49 */
50 protected $_maxEntities = 100;
51
52 /**
53 * Variable to store redirect path
54 *
55 * @var string
56 */
57 protected $_userContext;
58
59 /**
60 * Must be set to entity table name (eg. civicrm_participant) by child class
61 *
62 * @var string
63 */
64 public static $tableName = NULL;
65
66 /**
67 * Must be set to entity shortname (eg. event)
68 *
69 * @var string
70 */
71 public static $entityShortname = NULL;
72
73 /**
74 * Build all the data structures needed to build the form.
75 *
76 * @return void
77 */
78 public function preProcess() {
79 // initialize the task and row fields
80 parent::preProcess();
81 $session = CRM_Core_Session::singleton();
82 $this->_userContext = $session->readUserContext();
83
84 CRM_Utils_System::setTitle(ts('Update multiple ' . $this::$entityShortname . 's'));
85
86 // validations
87 if (count($this->_entityIds) > $this->_maxEntities) {
88 CRM_Core_Session::setStatus(ts("The maximum number of %3 you can select for Update multiple %3 is %1. You have selected %2. Please select fewer %3 from your search results and try again.", [
89 1 => $this->_maxEntities,
90 2 => count($this->_entityIds),
91 3 => $this::$entityShortname . 's',
92 ]), ts('Update multiple records error'), 'error');
93 CRM_Utils_System::redirect($this->_userContext);
94 }
95 }
96
97 /**
98 * Build the form object.
99 *
100 * @return void
101 */
102 public function buildQuickForm() {
103 $types = [ucfirst($this::$entityShortname)];
104 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types, TRUE);
105
106 if (empty($profiles)) {
107 CRM_Core_Session::setStatus(
108 ts("You will need to create a Profile containing the %1 fields you want to edit before you can use Update multiple %2. Navigate to Administer > Customize Data and Screens > Profiles to configure a Profile. Consult the online Administrator documentation for more information.",
109 [
110 1 => $types[0],
111 2 => $this::$entityShortname . 's',
112 ]),
113 ts('Update multiple records error'),
114 'error'
115 );
116 CRM_Utils_System::redirect($this->_userContext);
117 }
118
119 $this->add('select',
120 'uf_group_id',
121 ts('Select Profile'),
122 [
123 '' => ts('- select profile -'),
124 ] + $profiles,
125 TRUE
126 );
127 $this->addDefaultButtons(ts('Continue'));
128
129 $taskComponent['lc'] = $this::$entityShortname;
130 $taskComponent['ucfirst'] = ucfirst($this::$entityShortname);
131 $this->assign('taskComponent', $taskComponent);
132 }
133
134 /**
135 * Add local and global form rules.
136 *
137 * @return void
138 */
139 public function addRules() {
140 $this->addFormRule(['CRM_' . ucfirst($this::$entityShortname) . '_Form_Task_PickProfile', 'formRule']);
141 }
142
143 /**
144 * Global validation rules for the form.
145 *
146 * @param array $fields
147 * Posted values of the form.
148 *
149 * @return bool|array
150 * true if no errors, else array of errors
151 */
152 public static function formRule($fields) {
153 return TRUE;
154 }
155
156 /**
157 * Process the form after the input has been submitted and validated.
158 *
159 * @return void
160 */
161 public function postProcess() {
162 $params = $this->exportValues();
163
164 $this->set('ufGroupId', $params['uf_group_id']);
165
166 // also reset the batch page so it gets new values from the db
167 $this->controller->resetPage('Batch');
168 }
169
170 }