Merge pull request #15921 from civicrm/5.20
[civicrm-core.git] / CRM / Activity / 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 batch profile update for Activity.
20 */
21 class CRM_Activity_Form_Task_PickProfile extends CRM_Activity_Form_Task {
22
23 /**
24 * The title of the group.
25 *
26 * @var string
27 */
28 protected $_title;
29
30 /**
31 * Maximum Activities that should be allowed to update.
32 * @var int
33 */
34 protected $_maxActivities = 100;
35
36 /**
37 * Variable to store redirect path.
38 * @var string
39 */
40 protected $_userContext;
41
42 /**
43 * Build all the data structures needed to build the form.
44 */
45 public function preProcess() {
46
47 // Initialize the task and row fields.
48 parent::preProcess();
49 $session = CRM_Core_Session::singleton();
50 $this->_userContext = $session->readUserContext();
51
52 CRM_Utils_System::setTitle(ts('Update multiple activities'));
53
54 $validate = FALSE;
55 // Validations.
56 if (count($this->_activityHolderIds) > $this->_maxActivities) {
57 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.", [
58 1 => $this->_maxActivities,
59 2 => count($this->_activityHolderIds),
60 ]), ts('Maximum Exceeded'), 'error');
61 $validate = TRUE;
62 }
63
64 // Then redirect.
65 if ($validate) {
66 CRM_Utils_System::redirect($this->_userContext);
67 }
68 }
69
70 /**
71 * Build the form object.
72 */
73 public function buildQuickForm() {
74 $types = ['Activity'];
75 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types, TRUE);
76
77 $activityTypeIds = array_flip(CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'));
78 $nonEditableActivityTypeIds = [
79 $activityTypeIds['Email'],
80 $activityTypeIds['Bulk Email'],
81 $activityTypeIds['Contribution'],
82 $activityTypeIds['Inbound Email'],
83 $activityTypeIds['Pledge Reminder'],
84 $activityTypeIds['Membership Signup'],
85 $activityTypeIds['Membership Renewal'],
86 $activityTypeIds['Event Registration'],
87 $activityTypeIds['Pledge Acknowledgment'],
88 ];
89 $notEditable = FALSE;
90 foreach ($this->_activityHolderIds as $activityId) {
91 $typeId = CRM_Core_DAO::getFieldValue("CRM_Activity_DAO_Activity", $activityId, 'activity_type_id');
92 if (in_array($typeId, $nonEditableActivityTypeIds)) {
93 $notEditable = TRUE;
94 break;
95 }
96 }
97
98 if (empty($profiles)) {
99 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");
100 CRM_Utils_System::redirect($this->_userContext);
101 }
102 elseif ($notEditable) {
103 CRM_Core_Session::setStatus("", ts("Some of the selected activities are not editable."), "alert");
104 CRM_Utils_System::redirect($this->_userContext);
105 }
106
107 $ufGroupElement = $this->add('select', 'uf_group_id', ts('Select Profile'),
108 [
109 '' => ts('- select profile -'),
110 ] + $profiles, TRUE
111 );
112 $this->addDefaultButtons(ts('Continue'));
113 }
114
115 /**
116 * Add local and global form rules.
117 */
118 public function addRules() {
119 $this->addFormRule(['CRM_Activity_Form_Task_PickProfile', 'formRule']);
120 }
121
122 /**
123 * Global validation rules for the form.
124 *
125 * @param array $fields
126 * Posted values of the form.
127 *
128 * @return array
129 * list of errors to be posted back to the form
130 */
131 public static function formRule($fields) {
132 return TRUE;
133 }
134
135 /**
136 * Process the form after the input has been submitted and validated.
137 */
138 public function postProcess() {
139 $params = $this->exportValues();
140
141 $this->set('ufGroupId', $params['uf_group_id']);
142
143 // also reset the batch page so it gets new values from the db
144 $this->controller->resetPage('Batch');
145 }
146
147 }