Merge pull request #18360 from colemanw/fixMultiInProfile
[civicrm-core.git] / CRM / Contact / Form / Task / PickProfile.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality for Update multiple contacts
20 */
21 class CRM_Contact_Form_Task_PickProfile extends CRM_Contact_Form_Task {
22
23 /**
24 * The title of the group
25 *
26 * @var string
27 */
28 protected $_title;
29
30 /**
31 * Maximum contacts that should be allowed to update
32 * @var int
33 */
34 protected $_maxContacts = 100;
35
36 /**
37 * Maximum profile fields that will be displayed
38 * @var int
39 */
40 protected $_maxFields = 9;
41
42 /**
43 * Variable to store redirect path
44 * @var string
45 */
46 protected $_userContext;
47
48 /**
49 * Build all the data structures needed to build the form.
50 */
51 public function preProcess() {
52 // initialize the task and row fields
53 parent::preProcess();
54
55 $session = CRM_Core_Session::singleton();
56 $this->_userContext = $session->readUserContext();
57
58 $validate = FALSE;
59 //validations
60 if (count($this->_contactIds) > $this->_maxContacts) {
61 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.", [
62 1 => $this->_maxContacts,
63 2 => count($this->_contactIds),
64 ]), ts('Maximum Exceeded'), 'error');
65 $validate = TRUE;
66 }
67
68 if (CRM_Contact_BAO_Contact_Utils::checkContactType($this->_contactIds)) {
69 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');
70 $validate = TRUE;
71 }
72
73 // than redirect
74 if ($validate) {
75 CRM_Utils_System::redirect($this->_userContext);
76 }
77 }
78
79 /**
80 * Build the form object.
81 */
82 public function buildQuickForm() {
83 CRM_Utils_System::setTitle(ts('Update multiple contacts'));
84
85 foreach ($this->_contactIds as $id) {
86 $this->_contactTypes = CRM_Contact_BAO_Contact::getContactTypes($id);
87 }
88
89 //add Contact type profiles
90 $this->_contactTypes[] = 'Contact';
91
92 $profiles = CRM_Core_BAO_UFGroup::getProfiles($this->_contactTypes);
93
94 if (empty($profiles)) {
95 $types = implode(' ' . ts('or') . ' ', $this->_contactTypes);
96 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');
97 CRM_Utils_System::redirect($this->_userContext);
98 }
99 $ufGroupElement = $this->add('select', 'uf_group_id', ts('Select Profile'), ['' => ts('- select profile -')] + $profiles, TRUE, ['class' => 'crm-select2 huge']);
100
101 $this->addDefaultButtons(ts('Continue'));
102 }
103
104 /**
105 * Add local and global form rules.
106 */
107 public function addRules() {
108 $this->addFormRule(['CRM_Contact_Form_Task_PickProfile', 'formRule']);
109 }
110
111 /**
112 * Global validation rules for the form.
113 *
114 * @param array $fields
115 * Posted values of the form.
116 *
117 * @return array
118 * list of errors to be posted back to the form
119 */
120 public static function formRule($fields) {
121 if (CRM_Core_BAO_UFField::checkProfileType($fields['uf_group_id'])) {
122 $errorMsg['uf_group_id'] = "You cannot select a mixed profile for Update multiple contacts.";
123 }
124
125 if (!empty($errorMsg)) {
126 return $errorMsg;
127 }
128
129 return TRUE;
130 }
131
132 /**
133 * Process the form after the input has been submitted and validated.
134 */
135 public function postProcess() {
136 $params = $this->exportValues();
137
138 $this->set('ufGroupId', $params['uf_group_id']);
139
140 // also reset the batch page so it gets new values from the db
141 $this->controller->resetPage('Batch');
142 }
143
144 }