Merge pull request #18286 from sunilpawar/ui_30
[civicrm-core.git] / CRM / Contact / 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/**
ec5b3633 19 * This class provides the functionality for Update multiple contacts
6a488035
TO
20 */
21class CRM_Contact_Form_Task_PickProfile extends CRM_Contact_Form_Task {
22
23 /**
100fef9d 24 * The title of the group
6a488035
TO
25 *
26 * @var string
27 */
28 protected $_title;
29
30 /**
100fef9d 31 * Maximum contacts that should be allowed to update
69078420 32 * @var int
6a488035
TO
33 */
34 protected $_maxContacts = 100;
35
36 /**
100fef9d 37 * Maximum profile fields that will be displayed
69078420 38 * @var int
6a488035
TO
39 */
40 protected $_maxFields = 9;
41
42 /**
100fef9d 43 * Variable to store redirect path
69078420 44 * @var string
6a488035
TO
45 */
46 protected $_userContext;
47
48 /**
fe482240 49 * Build all the data structures needed to build the form.
6a488035 50 */
00be9182 51 public function preProcess() {
d424ffde 52 // initialize the task and row fields
6a488035
TO
53 parent::preProcess();
54
55 $session = CRM_Core_Session::singleton();
56 $this->_userContext = $session->readUserContext();
57
58 $validate = FALSE;
59 //validations
60 if (count($this->_contactIds) > $this->_maxContacts) {
be2fb01f 61 CRM_Core_Session::setStatus(ts("The maximum number of contacts you can select for Update multiple contacts is %1. You have selected %2. Please select fewer contacts from your search results and try again.", [
69078420
SL
62 1 => $this->_maxContacts,
63 2 => count($this->_contactIds),
64 ]), ts('Maximum Exceeded'), 'error');
6a488035
TO
65 $validate = TRUE;
66 }
67
68 if (CRM_Contact_BAO_Contact_Utils::checkContactType($this->_contactIds)) {
b581842f 69 CRM_Core_Session::setStatus(ts("Update multiple contacts requires that all selected contacts be the same basic type (e.g. all Individuals OR all Organizations...). Please modify your selection and try again."), ts('Contact Type Mismatch'), 'error');
6a488035
TO
70 $validate = TRUE;
71 }
72
73 // than redirect
74 if ($validate) {
75 CRM_Utils_System::redirect($this->_userContext);
76 }
77 }
78
79 /**
fe482240 80 * Build the form object.
6a488035 81 */
00be9182 82 public function buildQuickForm() {
b581842f 83 CRM_Utils_System::setTitle(ts('Update multiple contacts'));
6a488035
TO
84
85 foreach ($this->_contactIds as $id) {
86 $this->_contactTypes = CRM_Contact_BAO_Contact::getContactTypes($id);
87 }
88
89 //add Contact type profiles
90 $this->_contactTypes[] = 'Contact';
91
92 $profiles = CRM_Core_BAO_UFGroup::getProfiles($this->_contactTypes);
93
94 if (empty($profiles)) {
95 $types = implode(' ' . ts('or') . ' ', $this->_contactTypes);
be2fb01f 96 CRM_Core_Session::setStatus(ts("The contact type selected for Update multiple contacts does not have a corresponding profile. Please set up a profile for %1s and try again.", [1 => $types]), ts('No Profile Available'), 'error');
6a488035
TO
97 CRM_Utils_System::redirect($this->_userContext);
98 }
be2fb01f 99 $ufGroupElement = $this->add('select', 'uf_group_id', ts('Select Profile'), ['' => ts('- select profile -')] + $profiles, TRUE, ['class' => 'crm-select2 huge']);
6a488035 100
f212d37d 101 $this->addDefaultButtons(ts('Continue'));
6a488035
TO
102 }
103
104 /**
fe482240 105 * Add local and global form rules.
6a488035 106 */
00be9182 107 public function addRules() {
be2fb01f 108 $this->addFormRule(['CRM_Contact_Form_Task_PickProfile', 'formRule']);
6a488035
TO
109 }
110
111 /**
fe482240 112 * Global validation rules for the form.
6a488035 113 *
77c5b619
TO
114 * @param array $fields
115 * Posted values of the form.
6a488035 116 *
a6c01b45
CW
117 * @return array
118 * list of errors to be posted back to the form
6a488035 119 */
00be9182 120 public static function formRule($fields) {
6a488035 121 if (CRM_Core_BAO_UFField::checkProfileType($fields['uf_group_id'])) {
b581842f 122 $errorMsg['uf_group_id'] = "You cannot select a mixed profile for Update multiple contacts.";
6a488035
TO
123 }
124
125 if (!empty($errorMsg)) {
126 return $errorMsg;
127 }
128
129 return TRUE;
130 }
131
132 /**
fe482240 133 * Process the form after the input has been submitted and validated.
6a488035
TO
134 */
135 public function postProcess() {
136 $params = $this->exportValues();
137
138 $this->set('ufGroupId', $params['uf_group_id']);
139
140 // also reset the batch page so it gets new values from the db
141 $this->controller->resetPage('Batch');
142 }
96025800 143
6a488035 144}