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