(NFC) Update CRM/Contact to match new coder style
[civicrm-core.git] / CRM / Contact / 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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class provides the functionality for Update multiple contacts
36 */
37 class CRM_Contact_Form_Task_PickProfile extends CRM_Contact_Form_Task {
38
39 /**
40 * The title of the group
41 *
42 * @var string
43 */
44 protected $_title;
45
46 /**
47 * Maximum contacts that should be allowed to update
48 * @var int
49 */
50 protected $_maxContacts = 100;
51
52 /**
53 * Maximum profile fields that will be displayed
54 * @var int
55 */
56 protected $_maxFields = 9;
57
58 /**
59 * Variable to store redirect path
60 * @var string
61 */
62 protected $_userContext;
63
64 /**
65 * Build all the data structures needed to build the form.
66 */
67 public function preProcess() {
68 // initialize the task and row fields
69 parent::preProcess();
70
71 $session = CRM_Core_Session::singleton();
72 $this->_userContext = $session->readUserContext();
73
74 $validate = FALSE;
75 //validations
76 if (count($this->_contactIds) > $this->_maxContacts) {
77 CRM_Core_Session::setStatus(ts("The maximum number of contacts you can select for Update multiple contacts is %1. You have selected %2. Please select fewer contacts from your search results and try again.", [
78 1 => $this->_maxContacts,
79 2 => count($this->_contactIds),
80 ]), ts('Maximum Exceeded'), 'error');
81 $validate = TRUE;
82 }
83
84 if (CRM_Contact_BAO_Contact_Utils::checkContactType($this->_contactIds)) {
85 CRM_Core_Session::setStatus(ts("Update multiple contacts requires that all selected contacts be the same basic type (e.g. all Individuals OR all Organizations...). Please modify your selection and try again."), ts('Contact Type Mismatch'), 'error');
86 $validate = TRUE;
87 }
88
89 // than redirect
90 if ($validate) {
91 CRM_Utils_System::redirect($this->_userContext);
92 }
93 }
94
95 /**
96 * Build the form object.
97 */
98 public function buildQuickForm() {
99 CRM_Utils_System::setTitle(ts('Update multiple contacts'));
100
101 foreach ($this->_contactIds as $id) {
102 $this->_contactTypes = CRM_Contact_BAO_Contact::getContactTypes($id);
103 }
104
105 //add Contact type profiles
106 $this->_contactTypes[] = 'Contact';
107
108 $profiles = CRM_Core_BAO_UFGroup::getProfiles($this->_contactTypes);
109
110 if (empty($profiles)) {
111 $types = implode(' ' . ts('or') . ' ', $this->_contactTypes);
112 CRM_Core_Session::setStatus(ts("The contact type selected for Update multiple contacts does not have a corresponding profile. Please set up a profile for %1s and try again.", [1 => $types]), ts('No Profile Available'), 'error');
113 CRM_Utils_System::redirect($this->_userContext);
114 }
115 $ufGroupElement = $this->add('select', 'uf_group_id', ts('Select Profile'), ['' => ts('- select profile -')] + $profiles, TRUE, ['class' => 'crm-select2 huge']);
116
117 $this->addDefaultButtons(ts('Continue'));
118 }
119
120 /**
121 * Add local and global form rules.
122 */
123 public function addRules() {
124 $this->addFormRule(['CRM_Contact_Form_Task_PickProfile', 'formRule']);
125 }
126
127 /**
128 * Global validation rules for the form.
129 *
130 * @param array $fields
131 * Posted values of the form.
132 *
133 * @return array
134 * list of errors to be posted back to the form
135 */
136 public static function formRule($fields) {
137 if (CRM_Core_BAO_UFField::checkProfileType($fields['uf_group_id'])) {
138 $errorMsg['uf_group_id'] = "You cannot select a mixed profile for Update multiple contacts.";
139 }
140
141 if (!empty($errorMsg)) {
142 return $errorMsg;
143 }
144
145 return TRUE;
146 }
147
148 /**
149 * Process the form after the input has been submitted and validated.
150 */
151 public function postProcess() {
152 $params = $this->exportValues();
153
154 $this->set('ufGroupId', $params['uf_group_id']);
155
156 // also reset the batch page so it gets new values from the db
157 $this->controller->resetPage('Batch');
158 }
159
160 }