PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/web/sites/all/modules/civicrm/CRM/Contact/Form/Search/Custom/Base.php

https://gitlab.com/jamie/ussocialforum
PHP | 245 lines | 118 code | 29 blank | 98 comment | 15 complexity | e17cad1cdabd9c51c3bde317cb3fc32d MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, Apache-2.0, AGPL-1.0
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2014 |
  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-2014
  31. * $Id$
  32. *
  33. */
  34. class CRM_Contact_Form_Search_Custom_Base {
  35. protected $_formValues;
  36. protected $_columns;
  37. protected $_stateID;
  38. /**
  39. * @param $formValues
  40. */
  41. function __construct(&$formValues) {
  42. $this->_formValues = &$formValues;
  43. }
  44. /**
  45. * Builds the list of tasks or actions that a searcher can perform on a result set.
  46. *
  47. * The returned array completely replaces the task list, so a child class that
  48. * wants to modify the existing list should manipulate the result of this method.
  49. *
  50. * @param CRM_Core_Form_Search $form
  51. * @return array
  52. */
  53. function buildTaskList(CRM_Core_Form_Search $form) {
  54. return $form->getVar('_taskList');
  55. }
  56. /**
  57. * @return null|string
  58. */
  59. function count() {
  60. return CRM_Core_DAO::singleValueQuery($this->sql('count(distinct contact_a.id) as total'));
  61. }
  62. /**
  63. * @return null
  64. */
  65. function summary() {
  66. return NULL;
  67. }
  68. /**
  69. * @param int $offset
  70. * @param int $rowcount
  71. * @param null $sort
  72. * @param bool $returnSQL
  73. *
  74. * @return string
  75. */
  76. function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = FALSE) {
  77. $sql = $this->sql(
  78. 'contact_a.id as contact_id',
  79. $offset,
  80. $rowcount,
  81. $sort
  82. );
  83. $this->validateUserSQL($sql);
  84. if ($returnSQL) {
  85. return $sql;
  86. }
  87. return CRM_Core_DAO::composeQuery($sql, CRM_Core_DAO::$_nullArray);
  88. }
  89. /**
  90. * @param $selectClause
  91. * @param int $offset
  92. * @param int $rowcount
  93. * @param null $sort
  94. * @param bool $includeContactIDs
  95. * @param null $groupBy
  96. *
  97. * @return string
  98. */
  99. function sql(
  100. $selectClause,
  101. $offset = 0,
  102. $rowcount = 0,
  103. $sort = NULL,
  104. $includeContactIDs = FALSE,
  105. $groupBy = NULL
  106. ) {
  107. $sql = "SELECT $selectClause " . $this->from();
  108. $where = $this->where();
  109. if (!empty($where)) {
  110. $sql .= " WHERE " . $where;
  111. }
  112. if ($includeContactIDs) {
  113. $this->includeContactIDs($sql,
  114. $this->_formValues
  115. );
  116. }
  117. if ($groupBy) {
  118. $sql .= " $groupBy ";
  119. }
  120. $this->addSortOffset($sql, $offset, $rowcount, $sort);
  121. return $sql;
  122. }
  123. /**
  124. * @return null
  125. */
  126. function templateFile() {
  127. return NULL;
  128. }
  129. function &columns() {
  130. return $this->_columns;
  131. }
  132. /**
  133. * @param $sql
  134. * @param $formValues
  135. */
  136. static function includeContactIDs(&$sql, &$formValues) {
  137. $contactIDs = array();
  138. foreach ($formValues as $id => $value) {
  139. if ($value &&
  140. substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX
  141. ) {
  142. $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN);
  143. }
  144. }
  145. if (!empty($contactIDs)) {
  146. $contactIDs = implode(', ', $contactIDs);
  147. $sql .= " AND contact_a.id IN ( $contactIDs )";
  148. }
  149. }
  150. /**
  151. * @param $sql
  152. * @param $offset
  153. * @param $rowcount
  154. * @param $sort
  155. */
  156. function addSortOffset(&$sql, $offset, $rowcount, $sort) {
  157. if (!empty($sort)) {
  158. if (is_string($sort)) {
  159. $sort = CRM_Utils_Type::escape($sort, 'String');
  160. $sql .= " ORDER BY $sort ";
  161. }
  162. else {
  163. $sql .= " ORDER BY " . trim($sort->orderBy());
  164. }
  165. }
  166. if ($rowcount > 0 && $offset >= 0) {
  167. $offset = CRM_Utils_Type::escape($offset, 'Int');
  168. $rowcount = CRM_Utils_Type::escape($rowcount, 'Int');
  169. $sql .= " LIMIT $offset, $rowcount ";
  170. }
  171. }
  172. /**
  173. * @param $sql
  174. * @param bool $onlyWhere
  175. *
  176. * @throws Exception
  177. */
  178. function validateUserSQL(&$sql, $onlyWhere = FALSE) {
  179. $includeStrings = array('contact_a');
  180. $excludeStrings = array('insert', 'delete', 'update');
  181. if (!$onlyWhere) {
  182. $includeStrings += array('select', 'from', 'where', 'civicrm_contact');
  183. }
  184. foreach ($includeStrings as $string) {
  185. if (stripos($sql, $string) === FALSE) {
  186. CRM_Core_Error::fatal(ts('Could not find \'%1\' string in SQL clause.',
  187. array(1 => $string)
  188. ));
  189. }
  190. }
  191. foreach ($excludeStrings as $string) {
  192. if (preg_match('/(\s' . $string . ')|(' . $string . '\s)/i', $sql)) {
  193. CRM_Core_Error::fatal(ts('Found illegal \'%1\' string in SQL clause.',
  194. array(1 => $string)
  195. ));
  196. }
  197. }
  198. }
  199. /**
  200. * @param $where
  201. * @param $params
  202. *
  203. * @return string
  204. */
  205. function whereClause(&$where, &$params) {
  206. return CRM_Core_DAO::composeQuery($where, $params, TRUE);
  207. }
  208. // override this method to define the contact query object
  209. // used for creating $sql
  210. /**
  211. * @return null
  212. */
  213. function getQueryObj() {
  214. return NULL;
  215. }
  216. }