Merge pull request #15785 from eileenmcnaughton/contribution_url_params
[civicrm-core.git] / CRM / Activity / Form / Task / PickProfile.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
b6c94f42 19 * This class provides the functionality for batch profile update for Activity.
6a488035
TO
20 */
21class CRM_Activity_Form_Task_PickProfile extends CRM_Activity_Form_Task {
22
23 /**
fe482240 24 * The title of the group.
6a488035
TO
25 *
26 * @var string
27 */
28 protected $_title;
29
30 /**
fe482240 31 * Maximum Activities that should be allowed to update.
62d3ee27 32 * @var int
6a488035
TO
33 */
34 protected $_maxActivities = 100;
35
36 /**
fe482240 37 * Variable to store redirect path.
62d3ee27 38 * @var string
6a488035
TO
39 */
40 protected $_userContext;
41
42 /**
fe482240 43 * Build all the data structures needed to build the form.
6a488035 44 */
00be9182 45 public function preProcess() {
f813f78e 46
7808aae6 47 // Initialize the task and row fields.
6a488035
TO
48 parent::preProcess();
49 $session = CRM_Core_Session::singleton();
50 $this->_userContext = $session->readUserContext();
51
b581842f 52 CRM_Utils_System::setTitle(ts('Update multiple activities'));
6a488035
TO
53
54 $validate = FALSE;
7808aae6 55 // Validations.
6a488035 56 if (count($this->_activityHolderIds) > $this->_maxActivities) {
be2fb01f 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.", [
c5c263ca
AH
58 1 => $this->_maxActivities,
59 2 => count($this->_activityHolderIds),
be2fb01f 60 ]), ts('Maximum Exceeded'), 'error');
6a488035
TO
61 $validate = TRUE;
62 }
63
7808aae6 64 // Then redirect.
6a488035
TO
65 if ($validate) {
66 CRM_Utils_System::redirect($this->_userContext);
67 }
68 }
69
70 /**
fe482240 71 * Build the form object.
6a488035 72 */
00be9182 73 public function buildQuickForm() {
be2fb01f 74 $types = ['Activity'];
6a488035
TO
75 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types, TRUE);
76
77 $activityTypeIds = array_flip(CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'));
be2fb01f 78 $nonEditableActivityTypeIds = [
6a488035
TO
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'],
be2fb01f 88 ];
6a488035
TO
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)) {
be2fb01f 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");
6a488035
TO
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'),
be2fb01f 108 [
389bcebf 109 '' => ts('- select profile -'),
be2fb01f 110 ] + $profiles, TRUE
6a488035 111 );
f212d37d 112 $this->addDefaultButtons(ts('Continue'));
6a488035
TO
113 }
114
115 /**
fe482240 116 * Add local and global form rules.
6a488035 117 */
00be9182 118 public function addRules() {
be2fb01f 119 $this->addFormRule(['CRM_Activity_Form_Task_PickProfile', 'formRule']);
6a488035
TO
120 }
121
122 /**
fe482240 123 * Global validation rules for the form.
6a488035 124 *
041ab3d1
TO
125 * @param array $fields
126 * Posted values of the form.
6a488035 127 *
a6c01b45
CW
128 * @return array
129 * list of errors to be posted back to the form
6a488035 130 */
00be9182 131 public static function formRule($fields) {
6a488035
TO
132 return TRUE;
133 }
134
135 /**
fe482240 136 * Process the form after the input has been submitted and validated.
6a488035
TO
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 }
96025800 146
6a488035 147}