Remove all eval instances where eval is being used to instantiate a new object using...
[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 $this->_search = new $customSearchClass( $formValues );
145 }
146 else {
147 $customSearchFile = $ext->keyToPath($customSearchClass, 'search');
148 $this->_search = new $ext->keyToClass($customSearchClass, 'search')( $formValues );
149 }
150 }
151 //end of constructor
152
153 /**
154 * This method returns the links that are given for each search row.
155 * currently the links added for each row are
156 *
157 * - View
158 * - Edit
159 *
160 * @return array
161 * @access public
162 *
163 */
164 static function &links() {
165 if (!(self::$_links)) {
166 self::$_links = array(
167 CRM_Core_Action::VIEW => array(
168 'name' => ts('View'),
169 'url' => 'civicrm/contact/view',
170 'qs' => 'reset=1&cid=%%id%%',
171 'title' => ts('View Contact Details'),
172 ),
173 CRM_Core_Action::UPDATE => array(
174 'name' => ts('Edit'),
175 'url' => 'civicrm/contact/add',
176 'qs' => 'reset=1&action=update&cid=%%id%%',
177 'title' => ts('Edit Contact Details'),
178 ),
179 );
180
181 $config = CRM_Core_Config::singleton();
182 if ($config->mapAPIKey && $config->mapProvider) {
183 self::$_links[CRM_Core_Action::MAP] = array(
184 'name' => ts('Map'),
185 'url' => 'civicrm/contact/map',
186 'qs' => 'reset=1&cid=%%id%%&searchType=custom',
187 'title' => ts('Map Contact'),
188 );
189 }
190 }
191 return self::$_links;
192 }
193 //end of function
194
195 /**
196 * getter for array of the parameters required for creating pager.
197 *
198 * @param
199 * @access public
200 */
201 function getPagerParams($action, &$params) {
202 $params['status'] = ts('Contact %%StatusMessage%%');
203 $params['csvString'] = NULL;
204 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
205
206 $params['buttonTop'] = 'PagerTopButton';
207 $params['buttonBottom'] = 'PagerBottomButton';
208 }
209 //end of function
210
211 /**
212 * returns the column headers as an array of tuples:
213 * (name, sortName (key to the sort array))
214 *
215 * @param string $action the action being performed
216 * @param enum $output what should the result set include (web/email/csv)
217 *
218 * @return array the column headers that need to be displayed
219 * @access public
220 */
221 function &getColumnHeaders($action = NULL, $output = NULL) {
222 $columns = $this->_search->columns();
223 if ($output == CRM_Core_Selector_Controller::EXPORT) {
224 return array_keys($columns);
225 }
226 else {
227 $headers = array();
228 foreach ($columns as $name => $key) {
229 if (!empty($name)) {
230 $headers[] = array(
231 'name' => $name,
232 'sort' => $key,
233 'direction' => CRM_Utils_Sort::ASCENDING,
234 );
235 }
236 else {
237 $headers[] = array();
238 }
239 }
240 return $headers;
241 }
242 }
243
244 /**
245 * Returns total number of rows for the query.
246 *
247 * @param
248 *
249 * @return int Total number of rows
250 * @access public
251 */
252 function getTotalCount($action) {
253 return $this->_search->count();
254 }
255
256 /**
257 * returns all the rows in the given offset and rowCount
258 *
259 * @param enum $action the action being performed
260 * @param int $offset the row number to start from
261 * @param int $rowCount the number of rows to return
262 * @param string $sort the sql string that describes the sort order
263 * @param enum $output what should the result set include (web/email/csv)
264 *
265 * @return int the total number of rows for this action
266 */
267 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
268
269 $includeContactIDs = FALSE;
270 if (($output == CRM_Core_Selector_Controller::EXPORT ||
271 $output == CRM_Core_Selector_Controller::SCREEN
272 ) &&
273 $this->_formValues['radio_ts'] == 'ts_sel'
274 ) {
275 $includeContactIDs = TRUE;
276 }
277
278 $sql = $this->_search->all($offset, $rowCount, $sort, $includeContactIDs);
279
280 $dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
281
282 $columns = $this->_search->columns();
283 $columnNames = array_values($columns);
284 $links = self::links();
285
286 $permissions = array(CRM_Core_Permission::getPermission());
287 if (CRM_Core_Permission::check('delete contacts')) {
288 $permissions[] = CRM_Core_Permission::DELETE;
289 }
290 $mask = CRM_Core_Action::mask($permissions);
291
292 $alterRow = FALSE;
293 if (method_exists($this->_customSearchClass,
294 'alterRow'
295 )) {
296 $alterRow = TRUE;
297 }
298 $image = FALSE;
299 if (is_a($this->_search, 'CRM_Contact_Form_Search_Custom_Basic')) {
300 $image = TRUE;
301 }
302 // process the result of the query
303 $rows = array();
304 while ($dao->fetch()) {
305 $row = array();
306 $empty = TRUE;
307
308 // the columns we are interested in
309 foreach ($columnNames as $property) {
310 $row[$property] = $dao->$property;
311 if (!empty($dao->$property)) {
312 $empty = FALSE;
313 }
314 }
315 if (!$empty) {
316 $contactID = isset($dao->contact_id) ? $dao->contact_id : NULL;
317
318 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $contactID;
319 $row['action'] = CRM_Core_Action::formLink($links,
320 $mask,
321 array('id' => $contactID)
322 );
323 $row['contact_id'] = $contactID;
324
325 if ($alterRow) {
326 $this->_search->alterRow($row);
327 }
328
329 if ($image) {
330 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ?
331 $dao->contact_sub_type : $dao->contact_type, FALSE, $contactID
332 );
333 }
334 $rows[] = $row;
335 }
336 }
337
338 $this->buildPrevNextCache($sort);
339
340 return $rows;
341 }
342
343 /**
344 * Given the current formValues, gets the query in local
345 * language
346 *
347 * @param array(
348 reference) $formValues submitted formValues
349 *
350 * @return array $qill which contains an array of strings
351 * @access public
352 */
353 public function getQILL() {
354 return NULL;
355 }
356
357 public function getSummary() {
358 return $this->_search->summary();
359 }
360
361 /**
362 * name of export file.
363 *
364 * @param string $output type of output
365 *
366 * @return string name of the file
367 */
368 function getExportFileName($output = 'csv') {
369 return ts('CiviCRM Custom Search');
370 }
371
372 function alphabetQuery() {
373 return NULL;
374 }
375
376 function &contactIDQuery($params, $action, $sortID, $displayRelationshipType = NULL, $queryOperator = 'AND') {
377 $params = array();
378 $sql = $this->_search->contactIDs($params);
379
380 return CRM_Core_DAO::executeQuery($sql, $params);
381 }
382
383 function addActions(&$rows) {
384 $links = self::links();
385
386 $permissions = array(CRM_Core_Permission::getPermission());
387 if (CRM_Core_Permission::check('delete contacts')) {
388 $permissions[] = CRM_Core_Permission::DELETE;
389 }
390 $mask = CRM_Core_Action::mask($permissions);
391
392 foreach ($rows as $id => & $row) {
393 $row['action'] = CRM_Core_Action::formLink($links,
394 $mask,
395 array('id' => $row['contact_id'])
396 );
397 }
398 }
399
400 function removeActions(&$rows) {
401 foreach ($rows as $rid => & $rValue) {
402 unset($rValue['action']);
403 }
404 }
405 }
406