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