Merge pull request #1493 from davecivicrm/CRM-13257
[civicrm-core.git] / CRM / Contact / Selector / Custom.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id: Selector.php 11510 2007-09-18 09:21:34Z lobo $
33 *
34 */
35
36 /**
37 * This class is used to retrieve and display a range of
38 * contacts that match the given criteria (specifically for
39 * results of advanced search options.
40 *
41 */
42 class CRM_Contact_Selector_Custom extends CRM_Contact_Selector {
43
44 /**
45 * This defines two actions- View and Edit.
46 *
47 * @var array
48 * @static
49 */
50 static $_links = NULL;
51
52 /**
53 * we use desc to remind us what that column is, name is used in the tpl
54 *
55 * @var array
56 * @static
57 */
58 static $_columnHeaders;
59
60 /**
61 * Properties of contact we're interested in displaying
62 * @var array
63 * @static
64 */
65 static $_properties = array('contact_id', 'contact_type', 'display_name');
66
67 /**
68 * formValues is the array returned by exportValues called on
69 * the HTML_QuickForm_Controller for that page.
70 *
71 * @var array
72 * @access protected
73 */
74 public $_formValues;
75
76 /**
77 * params is the array in a value used by the search query creator
78 *
79 * @var array
80 * @access protected
81 */
82 public $_params;
83
84 /**
85 * represent the type of selector
86 *
87 * @var int
88 * @access protected
89 */
90 protected $_action;
91
92 protected $_query;
93
94 /**
95 * the public visible fields to be shown to the user
96 *
97 * @var array
98 * @access protected
99 */
100 protected $_fields;
101
102 /**
103 * The object that implements the search interface
104 */
105 protected $_search;
106
107 protected $_customSearchClass;
108
109 /**
110 * Class constructor
111 *
112 * @param array $formValues array of form values imported
113 * @param array $params array of parameters for query
114 * @param int $action - action of search basic or advanced.
115 *
116 * @return CRM_Contact_Selector
117 * @access public
118 */
119 function __construct(
120 $customSearchClass,
121 $formValues = NULL,
122 $params = NULL,
123 $returnProperties = NULL,
124 $action = CRM_Core_Action::NONE,
125 $includeContactIds = FALSE,
126 $searchChildGroups = TRUE,
127 $searchContext = 'search',
128 $contextMenu = NULL
129 ) {
130 $this->_customSearchClass = $customSearchClass;
131 $this->_formValues = $formValues;
132 $this->_includeContactIds = $includeContactIds;
133
134 $ext = CRM_Extension_System::singleton()->getMapper();
135
136 if (!$ext->isExtensionKey($customSearchClass)) {
137 if ($ext->isExtensionClass($customSearchClass)) {
138 $customSearchFile = $ext->classToPath($customSearchClass);
139 require_once ($customSearchFile);
140 }
141 else {
142 require_once (str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php');
143 }
144 eval('$this->_search = new ' . $customSearchClass . '( $formValues );');
145 }
146 else {
147 $customSearchFile = $ext->keyToPath($customSearchClass, 'search');
148 require_once ($customSearchFile);
149 eval('$this->_search = new ' . $ext->keyToClass($customSearchClass, 'search') . '( $formValues );');
150 }
151 }
152 //end of constructor
153
154 /**
155 * This method returns the links that are given for each search row.
156 * currently the links added for each row are
157 *
158 * - View
159 * - Edit
160 *
161 * @return array
162 * @access public
163 *
164 */
165 static function &links() {
166 if (!(self::$_links)) {
167 self::$_links = array(
168 CRM_Core_Action::VIEW => array(
169 'name' => ts('View'),
170 'url' => 'civicrm/contact/view',
171 'qs' => 'reset=1&cid=%%id%%',
172 'title' => ts('View Contact Details'),
173 ),
174 CRM_Core_Action::UPDATE => array(
175 'name' => ts('Edit'),
176 'url' => 'civicrm/contact/add',
177 'qs' => 'reset=1&action=update&cid=%%id%%',
178 'title' => ts('Edit Contact Details'),
179 ),
180 );
181
182 $config = CRM_Core_Config::singleton();
183 if ($config->mapAPIKey && $config->mapProvider) {
184 self::$_links[CRM_Core_Action::MAP] = array(
185 'name' => ts('Map'),
186 'url' => 'civicrm/contact/map',
187 'qs' => 'reset=1&cid=%%id%%&searchType=custom',
188 'title' => ts('Map Contact'),
189 );
190 }
191 }
192 return self::$_links;
193 }
194 //end of function
195
196 /**
197 * getter for array of the parameters required for creating pager.
198 *
199 * @param
200 * @access public
201 */
202 function getPagerParams($action, &$params) {
203 $params['status'] = ts('Contact %%StatusMessage%%');
204 $params['csvString'] = NULL;
205 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
206
207 $params['buttonTop'] = 'PagerTopButton';
208 $params['buttonBottom'] = 'PagerBottomButton';
209 }
210 //end of function
211
212 /**
213 * returns the column headers as an array of tuples:
214 * (name, sortName (key to the sort array))
215 *
216 * @param string $action the action being performed
217 * @param enum $output what should the result set include (web/email/csv)
218 *
219 * @return array the column headers that need to be displayed
220 * @access public
221 */
222 function &getColumnHeaders($action = NULL, $output = NULL) {
223 $columns = $this->_search->columns();
224 if ($output == CRM_Core_Selector_Controller::EXPORT) {
225 return array_keys($columns);
226 }
227 else {
228 $headers = array();
229 foreach ($columns as $name => $key) {
230 if (!empty($name)) {
231 $headers[] = array(
232 'name' => $name,
233 'sort' => $key,
234 'direction' => CRM_Utils_Sort::ASCENDING,
235 );
236 }
237 else {
238 $headers[] = array();
239 }
240 }
241 return $headers;
242 }
243 }
244
245 /**
246 * Returns total number of rows for the query.
247 *
248 * @param
249 *
250 * @return int Total number of rows
251 * @access public
252 */
253 function getTotalCount($action) {
254 return $this->_search->count();
255 }
256
257 /**
258 * returns all the rows in the given offset and rowCount
259 *
260 * @param enum $action the action being performed
261 * @param int $offset the row number to start from
262 * @param int $rowCount the number of rows to return
263 * @param string $sort the sql string that describes the sort order
264 * @param enum $output what should the result set include (web/email/csv)
265 *
266 * @return int the total number of rows for this action
267 */
268 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
269
270 $includeContactIDs = FALSE;
271 if (($output == CRM_Core_Selector_Controller::EXPORT ||
272 $output == CRM_Core_Selector_Controller::SCREEN
273 ) &&
274 $this->_formValues['radio_ts'] == 'ts_sel'
275 ) {
276 $includeContactIDs = TRUE;
277 }
278
279 $sql = $this->_search->all($offset, $rowCount, $sort, $includeContactIDs);
280
281 $dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
282
283 $columns = $this->_search->columns();
284 $columnNames = array_values($columns);
285 $links = self::links();
286
287 $permissions = array(CRM_Core_Permission::getPermission());
288 if (CRM_Core_Permission::check('delete contacts')) {
289 $permissions[] = CRM_Core_Permission::DELETE;
290 }
291 $mask = CRM_Core_Action::mask($permissions);
292
293 $alterRow = FALSE;
294 if (method_exists($this->_customSearchClass,
295 'alterRow'
296 )) {
297 $alterRow = TRUE;
298 }
299 $image = FALSE;
300 if (is_a($this->_search, 'CRM_Contact_Form_Search_Custom_Basic')) {
301 $image = TRUE;
302 }
303 // process the result of the query
304 $rows = array();
305 while ($dao->fetch()) {
306 $row = array();
307 $empty = TRUE;
308
309 // the columns we are interested in
310 foreach ($columnNames as $property) {
311 $row[$property] = $dao->$property;
312 if (!empty($dao->$property)) {
313 $empty = FALSE;
314 }
315 }
316 if (!$empty) {
317 $contactID = isset($dao->contact_id) ? $dao->contact_id : NULL;
318
319 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $contactID;
320 $row['action'] = CRM_Core_Action::formLink($links,
321 $mask,
322 array('id' => $contactID)
323 );
324 $row['contact_id'] = $contactID;
325
326 if ($alterRow) {
327 $this->_search->alterRow($row);
328 }
329
330 if ($image) {
331 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ?
332 $dao->contact_sub_type : $dao->contact_type, FALSE, $contactID
333 );
334 }
335 $rows[] = $row;
336 }
337 }
338
339 $this->buildPrevNextCache($sort);
340
341 return $rows;
342 }
343
344 /**
345 * Given the current formValues, gets the query in local
346 * language
347 *
348 * @param array(
349 reference) $formValues submitted formValues
350 *
351 * @return array $qill which contains an array of strings
352 * @access public
353 */
354 public function getQILL() {
355 return NULL;
356 }
357
358 public function getSummary() {
359 return $this->_search->summary();
360 }
361
362 /**
363 * name of export file.
364 *
365 * @param string $output type of output
366 *
367 * @return string name of the file
368 */
369 function getExportFileName($output = 'csv') {
370 return ts('CiviCRM Custom Search');
371 }
372
373 function alphabetQuery() {
374 return NULL;
375 }
376
377 function &contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
378 $params = array();
379 $sql = $this->_search->contactIDs($params);
380
381 return CRM_Core_DAO::executeQuery($sql, $params);
382 }
383
384 function addActions(&$rows) {
385 $links = self::links();
386
387 $permissions = array(CRM_Core_Permission::getPermission());
388 if (CRM_Core_Permission::check('delete contacts')) {
389 $permissions[] = CRM_Core_Permission::DELETE;
390 }
391 $mask = CRM_Core_Action::mask($permissions);
392
393 foreach ($rows as $id => & $row) {
394 $row['action'] = CRM_Core_Action::formLink($links,
395 $mask,
396 array('id' => $row['contact_id'])
397 );
398 }
399 }
400
401 function removeActions(&$rows) {
402 foreach ($rows as $rid => & $rValue) {
403 unset($rValue['action']);
404 }
405 }
406 }
407