Merge pull request #5473 from aydun/CRM-16160
[civicrm-core.git] / CRM / Activity / Form / Task / PickProfile.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This class provides the functionality for batch profile update for Activity
38 */
39 class CRM_Activity_Form_Task_PickProfile extends CRM_Activity_Form_Task {
40
41 /**
42 * The title of the group.
43 *
44 * @var string
45 */
46 protected $_title;
47
48 /**
49 * Maximum Activities that should be allowed to update.
50 */
51 protected $_maxActivities = 100;
52
53 /**
54 * Variable to store redirect path.
55 */
56 protected $_userContext;
57
58 /**
59 * Build all the data structures needed to build the form.
60 *
61 * @return void
62 */
63 public function preProcess() {
64 /*
65 * initialize the task and row fields
66 */
67
68 parent::preProcess();
69 $session = CRM_Core_Session::singleton();
70 $this->_userContext = $session->readUserContext();
71
72 CRM_Utils_System::setTitle(ts('Batch Profile Update for Activities'));
73
74 $validate = FALSE;
75 //validations
76 if (count($this->_activityHolderIds) > $this->_maxActivities) {
77 CRM_Core_Session::setStatus(ts("The maximum number of Activities you can select for Batch Update is %1. You have selected %2. Please select fewer Activities from your search results and try again.", array(
78 1 => $this->_maxActivities,
79 2 => count($this->_activityHolderIds),
80 )), ts('Maximum Exceeded'), 'error');
81 $validate = TRUE;
82 }
83
84 // than redirect
85 if ($validate) {
86 CRM_Utils_System::redirect($this->_userContext);
87 }
88 }
89
90 /**
91 * Build the form object.
92 *
93 *
94 * @return void
95 */
96 public function buildQuickForm() {
97 $types = array('Activity');
98 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types, TRUE);
99
100 $activityTypeIds = array_flip(CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'));
101 $nonEditableActivityTypeIds = array(
102 $activityTypeIds['Email'],
103 $activityTypeIds['Bulk Email'],
104 $activityTypeIds['Contribution'],
105 $activityTypeIds['Inbound Email'],
106 $activityTypeIds['Pledge Reminder'],
107 $activityTypeIds['Membership Signup'],
108 $activityTypeIds['Membership Renewal'],
109 $activityTypeIds['Event Registration'],
110 $activityTypeIds['Pledge Acknowledgment'],
111 );
112 $notEditable = FALSE;
113 foreach ($this->_activityHolderIds as $activityId) {
114 $typeId = CRM_Core_DAO::getFieldValue("CRM_Activity_DAO_Activity", $activityId, 'activity_type_id');
115 if (in_array($typeId, $nonEditableActivityTypeIds)) {
116 $notEditable = TRUE;
117 break;
118 }
119 }
120
121 if (empty($profiles)) {
122 CRM_Core_Session::setStatus(ts("You will need to create a Profile containing the %1 fields you want to edit before you can use Batch Update via Profile. Navigate to Administer > Customize Data and Screens > Profiles to configure a Profile. Consult the online Administrator documentation for more information.", array(1 => $types[0])), ts("No Profile Configured"), "alert");
123 CRM_Utils_System::redirect($this->_userContext);
124 }
125 elseif ($notEditable) {
126 CRM_Core_Session::setStatus("", ts("Some of the selected activities are not editable."), "alert");
127 CRM_Utils_System::redirect($this->_userContext);
128 }
129
130 $ufGroupElement = $this->add('select', 'uf_group_id', ts('Select Profile'),
131 array(
132 '' => ts('- select profile -'),
133 ) + $profiles, TRUE
134 );
135 $this->addDefaultButtons(ts('Continue'));
136 }
137
138 /**
139 * Add local and global form rules.
140 *
141 *
142 * @return void
143 */
144 public function addRules() {
145 $this->addFormRule(array('CRM_Activity_Form_Task_PickProfile', 'formRule'));
146 }
147
148 /**
149 * Global validation rules for the form.
150 *
151 * @param array $fields
152 * Posted values of the form.
153 *
154 * @return array
155 * list of errors to be posted back to the form
156 */
157 public static function formRule($fields) {
158 return TRUE;
159 }
160
161 /**
162 * Process the form after the input has been submitted and validated.
163 *
164 *
165 * @return void
166 */
167 public function postProcess() {
168 $params = $this->exportValues();
169
170 $this->set('ufGroupId', $params['uf_group_id']);
171
172 // also reset the batch page so it gets new values from the db
173 $this->controller->resetPage('Batch');
174 }
175
176 }