3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
33 class CRM_Contact_Form_Search_Custom_Base
{
35 protected $_formValues;
44 * @param array $formValues
46 public function __construct(&$formValues) {
47 $this->_formValues
= &$formValues;
51 * Builds the list of tasks or actions that a searcher can perform on a result set.
53 * The returned array completely replaces the task list, so a child class that
54 * wants to modify the existing list should manipulate the result of this method.
56 * @param CRM_Core_Form_Search $form
59 public function buildTaskList(CRM_Core_Form_Search
$form) {
60 return $form->getVar('_taskList');
66 public function count() {
67 return CRM_Core_DAO
::singleValueQuery($this->sql('count(distinct contact_a.id) as total'));
73 public function summary() {
79 * @param int $rowcount
81 * @param bool $returnSQL
85 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
87 'contact_a.id as contact_id',
92 $this->validateUserSQL($sql);
98 return CRM_Core_DAO
::composeQuery($sql, CRM_Core_DAO
::$_nullArray);
102 * @param $selectClause
104 * @param int $rowcount
106 * @param bool $includeContactIDs
107 * @param null $groupBy
116 $includeContactIDs = FALSE,
120 $sql = "SELECT $selectClause " . $this->from();
121 $where = $this->where();
122 if (!empty($where)) {
123 $sql .= " WHERE " . $where;
126 if ($includeContactIDs) {
127 $this->includeContactIDs($sql,
133 $sql .= " $groupBy ";
136 $this->addSortOffset($sql, $offset, $rowcount, $sort);
143 public function templateFile() {
147 public function &columns() {
148 return $this->_columns
;
155 public static function includeContactIDs(&$sql, &$formValues) {
156 $contactIDs = array();
157 foreach ($formValues as $id => $value) {
159 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
161 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
165 if (!empty($contactIDs)) {
166 $contactIDs = implode(', ', $contactIDs);
167 $sql .= " AND contact_a.id IN ( $contactIDs )";
177 public function addSortOffset(&$sql, $offset, $rowcount, $sort) {
179 if (is_string($sort)) {
180 $sort = CRM_Utils_Type
::escape($sort, 'String');
181 $sql .= " ORDER BY $sort ";
184 $sql .= " ORDER BY " . trim($sort->orderBy());
188 if ($rowcount > 0 && $offset >= 0) {
189 $offset = CRM_Utils_Type
::escape($offset, 'Int');
190 $rowcount = CRM_Utils_Type
::escape($rowcount, 'Int');
192 $sql .= " LIMIT $offset, $rowcount ";
198 * @param bool $onlyWhere
202 public function validateUserSQL(&$sql, $onlyWhere = FALSE) {
203 $includeStrings = array('contact_a');
204 $excludeStrings = array('insert', 'delete', 'update');
207 $includeStrings +
= array('select', 'from', 'where', 'civicrm_contact');
210 foreach ($includeStrings as $string) {
211 if (stripos($sql, $string) === FALSE) {
212 CRM_Core_Error
::fatal(ts('Could not find \'%1\' string in SQL clause.',
218 foreach ($excludeStrings as $string) {
219 if (preg_match('/(\s' . $string . ')|(' . $string . '\s)/i', $sql)) {
220 CRM_Core_Error
::fatal(ts('Found illegal \'%1\' string in SQL clause.',
229 * @param array $params
233 public function whereClause(&$where, &$params) {
234 return CRM_Core_DAO
::composeQuery($where, $params, TRUE);
238 * override this method to define the contact query object
239 * used for creating $sql
242 public function getQueryObj() {
249 * @param string $title
251 public function setTitle($title) {
253 CRM_Utils_System
::setTitle($title);
256 CRM_Utils_System
::setTitle(ts('Search'));