Merge pull request #22555 from eileenmcnaughton/stricter
[civicrm-core.git] / CRM / Core / Form / Task / PickProfile.php
CommitLineData
888da08c
MW
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
888da08c 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 |
888da08c
MW
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
888da08c
MW
15 */
16
17/**
18 * This class provides the functionality for batch profile update for case
19 */
20abstract class CRM_Core_Form_Task_PickProfile extends CRM_Core_Form_Task {
21
22 /**
23 * The title of the group
24 *
25 * @var string
26 */
27 protected $_title;
28
29 /**
30 * Maximum entities that should be allowed to update
31 *
32 * @var int
33 */
34 protected $_maxEntities = 100;
35
36 /**
37 * Variable to store redirect path
38 *
39 * @var string
40 */
41 protected $_userContext;
42
43 /**
44 * Must be set to entity table name (eg. civicrm_participant) by child class
45 *
46 * @var string
47 */
518fa0ee 48 public static $tableName = NULL;
888da08c
MW
49
50 /**
51 * Must be set to entity shortname (eg. event)
52 *
53 * @var string
54 */
518fa0ee 55 public static $entityShortname = NULL;
888da08c
MW
56
57 /**
58 * Build all the data structures needed to build the form.
59 *
60 * @return void
61 */
62 public function preProcess() {
63 // initialize the task and row fields
64 parent::preProcess();
65 $session = CRM_Core_Session::singleton();
66 $this->_userContext = $session->readUserContext();
67
94fd9d17 68 $this->setTitle(ts('Update multiple ' . $this::$entityShortname . 's'));
888da08c
MW
69
70 // validations
71 if (count($this->_entityIds) > $this->_maxEntities) {
be2fb01f 72 CRM_Core_Session::setStatus(ts("The maximum number of %3 you can select for Update multiple %3 is %1. You have selected %2. Please select fewer %3 from your search results and try again.", [
888da08c
MW
73 1 => $this->_maxEntities,
74 2 => count($this->_entityIds),
75 3 => $this::$entityShortname . 's',
be2fb01f 76 ]), ts('Update multiple records error'), 'error');
888da08c
MW
77 CRM_Utils_System::redirect($this->_userContext);
78 }
79 }
80
81 /**
82 * Build the form object.
83 *
84 * @return void
85 */
86 public function buildQuickForm() {
be2fb01f 87 $types = [ucfirst($this::$entityShortname)];
888da08c
MW
88 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types, TRUE);
89
90 if (empty($profiles)) {
91 CRM_Core_Session::setStatus(
92 ts("You will need to create a Profile containing the %1 fields you want to edit before you can use Update multiple %2. Navigate to Administer > Customize Data and Screens > Profiles to configure a Profile. Consult the online Administrator documentation for more information.",
be2fb01f 93 [
888da08c
MW
94 1 => $types[0],
95 2 => $this::$entityShortname . 's',
be2fb01f 96 ]),
888da08c
MW
97 ts('Update multiple records error'),
98 'error'
99 );
100 CRM_Utils_System::redirect($this->_userContext);
101 }
102
103 $this->add('select',
104 'uf_group_id',
105 ts('Select Profile'),
be2fb01f 106 [
888da08c 107 '' => ts('- select profile -'),
be2fb01f 108 ] + $profiles,
888da08c
MW
109 TRUE
110 );
111 $this->addDefaultButtons(ts('Continue'));
112
113 $taskComponent['lc'] = $this::$entityShortname;
114 $taskComponent['ucfirst'] = ucfirst($this::$entityShortname);
115 $this->assign('taskComponent', $taskComponent);
116 }
117
118 /**
119 * Add local and global form rules.
120 *
121 * @return void
122 */
123 public function addRules() {
be2fb01f 124 $this->addFormRule(['CRM_' . ucfirst($this::$entityShortname) . '_Form_Task_PickProfile', 'formRule']);
888da08c
MW
125 }
126
127 /**
128 * Global validation rules for the form.
129 *
130 * @param array $fields
131 * Posted values of the form.
132 *
133 * @return bool|array
134 * true if no errors, else array of errors
135 */
136 public static function formRule($fields) {
137 return TRUE;
138 }
139
140 /**
141 * Process the form after the input has been submitted and validated.
142 *
143 * @return void
144 */
145 public function postProcess() {
146 $params = $this->exportValues();
147
148 $this->set('ufGroupId', $params['uf_group_id']);
149
150 // also reset the batch page so it gets new values from the db
151 $this->controller->resetPage('Batch');
152 }
153
154}