3e8c20552cb104fcfe5ef11c60558e3db0e328b2
[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 // 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.
80 */
81 public function preProcess() {
82 $this->_entityIds = array();
83
84 $values = $this->controller->exportValues($this->get('searchFormName'));
85
86 $this->_task = $values['task'];
87 $className = 'CRM_' . ucfirst($this::$entityShortname) . '_Task';
88 $entityTasks = $className::tasks();
89 $this->assign('taskName', $entityTasks[$this->_task]);
90
91 $ids = array();
92 if ($values['radio_ts'] == 'ts_sel') {
93 foreach ($values as $name => $value) {
94 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
95 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
96 }
97 }
98 }
99 else {
100 $queryParams = $this->get('queryParams');
101 $sortOrder = NULL;
102 if ($this->get(CRM_Utils_Sort::SORT_ORDER)) {
103 $sortOrder = $this->get(CRM_Utils_Sort::SORT_ORDER);
104 }
105
106 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
107 CRM_Contact_BAO_Query::MODE_CASE
108 );
109 $query->_distinctComponentClause = " ( " . $this::$tableName . ".id )";
110 $query->_groupByComponentClause = " GROUP BY " . $this::$tableName . ".id ";
111 $result = $query->searchQuery(0, 0, $sortOrder);
112 $selector = $this::$entityShortname . '_id';
113 while ($result->fetch()) {
114 $ids[] = $result->$selector;
115 }
116 }
117
118 if (!empty($ids)) {
119 $this->_componentClause = ' ' . $this::$tableName . '.id IN ( ' . implode(',', $ids) . ' ) ';
120 $this->assign('totalSelected' . ucfirst($this::$entityShortname) . 's', count($ids));
121 }
122
123 $this->_entityIds = $this->_componentIds = $ids;
124
125 //set the context for redirection for any task actions
126 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
127 $urlParams = 'force=1';
128 if (CRM_Utils_Rule::qfKey($qfKey)) {
129 $urlParams .= "&qfKey=$qfKey";
130 }
131
132 $session = CRM_Core_Session::singleton();
133 $searchFormName = strtolower($this->get('searchFormName'));
134 if ($searchFormName == 'search') {
135 $session->replaceUserContext(CRM_Utils_System::url('civicrm/' . $this::$entityShortname . '/search', $urlParams));
136 }
137 else {
138 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
139 $urlParams
140 ));
141 }
142 }
143
144 /**
145 * Given the signer id, compute the contact id
146 * since its used for things like send email
147 */
148 public function setContactIDs() {
149 $this->_contactIds = &CRM_Core_DAO::getContactIDsFromComponent($this->_entityIds,
150 $this::$tableName
151 );
152 }
153
154 /**
155 * Simple shell that derived classes can call to add buttons to
156 * the form with a customized title for the main Submit
157 *
158 * @param string $title
159 * Title of the main button.
160 * @param string $nextType
161 * Button type for the form after processing.
162 * @param string $backType
163 * @param bool $submitOnce
164 */
165 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
166 $this->addButtons(array(
167 array(
168 'type' => $nextType,
169 'name' => $title,
170 'isDefault' => TRUE,
171 ),
172 array(
173 'type' => $backType,
174 'name' => ts('Cancel'),
175 ),
176 )
177 );
178 }
179
180 }