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