Non functional changes towards shared functions in Core_Form_Task
[civicrm-core.git] / CRM / Core / Form / Task.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2018
31 */
32
33 /**
34 * This is a shared parent class for form task actions.
35 */
36 abstract class CRM_Core_Form_Task extends CRM_Core_Form {
37
38 /**
39 * The task being performed
40 *
41 * @var int
42 */
43 protected $_task;
44
45 /**
46 * The additional clause that we restrict the search with
47 *
48 * @var string
49 */
50 protected $_componentClause = NULL;
51
52 /**
53 * The array that holds all the component ids
54 *
55 * @var array
56 */
57 protected $_componentIds;
58
59 /**
60 * The array that holds all the case ids
61 *
62 * @var array
63 */
64 public $_entityIds;
65
66 /**
67 * The array that holds all the contact ids
68 *
69 * @var array
70 */
71 public $_contactIds;
72
73 /**
74 * Must be set to entity table name (eg. civicrm_participant) by child class
75 *
76 * @var string
77 */
78 static $tableName = NULL;
79
80 /**
81 * Must be set to entity shortname (eg. event)
82 *
83 * @var string
84 */
85 static $entityShortname = NULL;
86
87 /**
88 * Build all the data structures needed to build the form.
89 *
90 * @throws \CRM_Core_Exception
91 */
92 public function preProcess() {
93 self::preProcessCommon($this);
94 }
95
96 /**
97 * Common pre-processing function.
98 *
99 * @param CRM_Core_Form_Task $form
100 *
101 * @throws \CRM_Core_Exception
102 */
103 public static function preProcessCommon(&$form) {
104 $form->_entityIds = array();
105
106 $searchFormValues = $form->controller->exportValues($form->get('searchFormName'));
107
108 $form->_task = $searchFormValues['task'];
109 $className = 'CRM_' . ucfirst($form::$entityShortname) . '_Task';
110 $entityTasks = $className::tasks();
111 $form->assign('taskName', $entityTasks[$form->_task]);
112
113 $entityIds = array();
114 if ($searchFormValues['radio_ts'] == 'ts_sel') {
115 foreach ($searchFormValues as $name => $value) {
116 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
117 $entityIds[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
118 }
119 }
120 }
121 else {
122 $queryParams = $form->get('queryParams');
123 $sortOrder = NULL;
124 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
125 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
126 }
127
128 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE, $form->getQueryMode());
129 $query->_distinctComponentClause = " ( " . $form::$tableName . ".id )";
130 $query->_groupByComponentClause = " GROUP BY " . $form::$tableName . ".id ";
131 $result = $query->searchQuery(0, 0, $sortOrder);
132 $selector = $form::$entityShortname . '_id';
133 while ($result->fetch()) {
134 $entityIds[] = $result->$selector;
135 }
136 }
137
138 if (!empty($entityIds)) {
139 $form->_componentClause = ' ' . $form::$tableName . '.id IN ( ' . implode(',', $entityIds) . ' ) ';
140 $form->assign('totalSelected' . ucfirst($form::$entityShortname) . 's', count($entityIds));
141 }
142
143 $form->_entityIds = $form->_componentIds = $entityIds;
144
145 // Some functions (eg. PDF letter tokens) rely on Ids being in specific fields rather than the generic $form->_entityIds
146 // So we set that specific field here (eg. for cases $form->_caseIds = $form->_entityIds).
147 // FIXME: This is really to handle legacy code that should probably be updated to use $form->_entityIds
148 $entitySpecificIdsName = '_' . $form::$entityShortname . 'Ids';
149 $form->$entitySpecificIdsName = $form->_entityIds;
150
151 //set the context for redirection for any task actions
152 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
153 $urlParams = 'force=1';
154 if (CRM_Utils_Rule::qfKey($qfKey)) {
155 $urlParams .= "&qfKey=$qfKey";
156 }
157
158 $session = CRM_Core_Session::singleton();
159 $searchFormName = strtolower($form->get('searchFormName'));
160 if ($searchFormName == 'search') {
161 $session->replaceUserContext(CRM_Utils_System::url('civicrm/' . $form::$entityShortname . '/search', $urlParams));
162 }
163 else {
164 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
165 $urlParams
166 ));
167 }
168 }
169
170 /**
171 * Given the entity id, compute the contact id since its used for things like send email
172 * For example, for cases we need to override this function as the table name is civicrm_case_contact
173 */
174 public function setContactIDs() {
175 $this->_contactIds = &CRM_Core_DAO::getContactIDsFromComponent($this->_entityIds,
176 $this::$tableName
177 );
178 }
179
180 /**
181 * Simple shell that derived classes can call to add buttons to
182 * the form with a customized title for the main Submit
183 *
184 * @param string $title
185 * Title of the main button.
186 * @param string $nextType
187 * Button type for the form after processing.
188 * @param string $backType
189 * @param bool $submitOnce
190 */
191 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
192 $this->addButtons(array(
193 array(
194 'type' => $nextType,
195 'name' => $title,
196 'isDefault' => TRUE,
197 ),
198 array(
199 'type' => $backType,
200 'name' => ts('Cancel'),
201 ),
202 )
203 );
204 }
205
206 /**
207 * Get the query mode (eg. CRM_Core_BAO_Query::MODE_CASE)
208 * Should be overridden by child classes in most cases
209 *
210 * @return int
211 */
212 public function getQueryMode() {
213 return CRM_Contact_BAO_Query::MODE_CONTACTS;
214 }
215
216 }