Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
ec5b3633 | 35 | * This class provides the functionality for Update multiple contacts |
6a488035 TO |
36 | */ |
37 | class CRM_Contact_Form_Task_PickProfile extends CRM_Contact_Form_Task { | |
38 | ||
39 | /** | |
100fef9d | 40 | * The title of the group |
6a488035 TO |
41 | * |
42 | * @var string | |
43 | */ | |
44 | protected $_title; | |
45 | ||
46 | /** | |
100fef9d | 47 | * Maximum contacts that should be allowed to update |
6a488035 TO |
48 | */ |
49 | protected $_maxContacts = 100; | |
50 | ||
51 | /** | |
100fef9d | 52 | * Maximum profile fields that will be displayed |
6a488035 TO |
53 | */ |
54 | protected $_maxFields = 9; | |
55 | ||
56 | /** | |
100fef9d | 57 | * Variable to store redirect path |
6a488035 TO |
58 | */ |
59 | protected $_userContext; | |
60 | ||
61 | /** | |
fe482240 | 62 | * Build all the data structures needed to build the form. |
6a488035 | 63 | */ |
00be9182 | 64 | public function preProcess() { |
d424ffde | 65 | // initialize the task and row fields |
6a488035 TO |
66 | parent::preProcess(); |
67 | ||
68 | $session = CRM_Core_Session::singleton(); | |
69 | $this->_userContext = $session->readUserContext(); | |
70 | ||
71 | $validate = FALSE; | |
72 | //validations | |
73 | if (count($this->_contactIds) > $this->_maxContacts) { | |
b581842f | 74 | 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.", array( |
353ffa53 | 75 | 1 => $this->_maxContacts, |
3bdca100 | 76 | 2 => count($this->_contactIds), |
353ffa53 | 77 | )), ts('Maximum Exceeded'), 'error'); |
6a488035 TO |
78 | $validate = TRUE; |
79 | } | |
80 | ||
81 | if (CRM_Contact_BAO_Contact_Utils::checkContactType($this->_contactIds)) { | |
b581842f | 82 | 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'); |
6a488035 TO |
83 | $validate = TRUE; |
84 | } | |
85 | ||
86 | // than redirect | |
87 | if ($validate) { | |
88 | CRM_Utils_System::redirect($this->_userContext); | |
89 | } | |
90 | } | |
91 | ||
92 | /** | |
fe482240 | 93 | * Build the form object. |
6a488035 | 94 | */ |
00be9182 | 95 | public function buildQuickForm() { |
b581842f | 96 | CRM_Utils_System::setTitle(ts('Update multiple contacts')); |
6a488035 TO |
97 | |
98 | foreach ($this->_contactIds as $id) { | |
99 | $this->_contactTypes = CRM_Contact_BAO_Contact::getContactTypes($id); | |
100 | } | |
101 | ||
102 | //add Contact type profiles | |
103 | $this->_contactTypes[] = 'Contact'; | |
104 | ||
105 | $profiles = CRM_Core_BAO_UFGroup::getProfiles($this->_contactTypes); | |
106 | ||
107 | if (empty($profiles)) { | |
108 | $types = implode(' ' . ts('or') . ' ', $this->_contactTypes); | |
b581842f | 109 | 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.", array(1 => $types)), ts('No Profile Available'), 'error'); |
6a488035 TO |
110 | CRM_Utils_System::redirect($this->_userContext); |
111 | } | |
24431f7b | 112 | $ufGroupElement = $this->add('select', 'uf_group_id', ts('Select Profile'), array('' => ts('- select profile -')) + $profiles, TRUE, array('class' => 'crm-select2 huge')); |
6a488035 | 113 | |
f212d37d | 114 | $this->addDefaultButtons(ts('Continue')); |
6a488035 TO |
115 | } |
116 | ||
117 | /** | |
fe482240 | 118 | * Add local and global form rules. |
6a488035 | 119 | */ |
00be9182 | 120 | public function addRules() { |
6a488035 TO |
121 | $this->addFormRule(array('CRM_Contact_Form_Task_PickProfile', 'formRule')); |
122 | } | |
123 | ||
124 | /** | |
fe482240 | 125 | * Global validation rules for the form. |
6a488035 | 126 | * |
77c5b619 TO |
127 | * @param array $fields |
128 | * Posted values of the form. | |
6a488035 | 129 | * |
a6c01b45 CW |
130 | * @return array |
131 | * list of errors to be posted back to the form | |
6a488035 | 132 | */ |
00be9182 | 133 | public static function formRule($fields) { |
6a488035 | 134 | if (CRM_Core_BAO_UFField::checkProfileType($fields['uf_group_id'])) { |
b581842f | 135 | $errorMsg['uf_group_id'] = "You cannot select a mixed profile for Update multiple contacts."; |
6a488035 TO |
136 | } |
137 | ||
138 | if (!empty($errorMsg)) { | |
139 | return $errorMsg; | |
140 | } | |
141 | ||
142 | return TRUE; | |
143 | } | |
144 | ||
145 | /** | |
fe482240 | 146 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
147 | */ |
148 | public function postProcess() { | |
149 | $params = $this->exportValues(); | |
150 | ||
151 | $this->set('ufGroupId', $params['uf_group_id']); | |
152 | ||
153 | // also reset the batch page so it gets new values from the db | |
154 | $this->controller->resetPage('Batch'); | |
155 | } | |
96025800 | 156 | |
6a488035 | 157 | } |