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