Merge branch '4.5' of https://github.com/civicrm/civicrm-core
[civicrm-core.git] / CRM / Contact / BAO / Query / Hook.php
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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Delegate query functions based on hook system
38 */
39 class CRM_Contact_BAO_Query_Hook {
40
41 /**
42 * @var array of CRM_Contact_BAO_Query_Interface objects
43 */
44 protected $_queryObjects = NULL;
45
46 /**
47 * Singleton function used to manage this object
48 *
49 * @return object
50 * @static
51 *
52 */
53 public static function singleton() {
54 static $singleton = NULL;
55 if (!$singleton) {
56 $singleton = new CRM_Contact_BAO_Query_Hook();
57 }
58 return $singleton;
59 }
60
61 /**
62 * Get or build the list of search objects (via hook)
63 *
64 * @return array of CRM_Contact_BAO_Query_Interface objects
65 */
66 public function getSearchQueryObjects() {
67 if ($this->_queryObjects === NULL) {
68 $this->_queryObjects = array();
69 CRM_Utils_Hook::queryObjects($this->_queryObjects, 'Contact');
70 }
71 return $this->_queryObjects;
72 }
73
74 /**
75 * @return array
76 */
77 public function &getFields() {
78 $extFields = array();
79 foreach (self::getSearchQueryObjects() as $obj) {
80 $flds = $obj->getFields();
81 $extFields = array_merge($extFields, $flds);
82 }
83 return $extFields;
84 }
85
86 /**
87 * @param $apiEntities
88 * @param $fieldOptions
89 */
90 public function alterSearchBuilderOptions(&$apiEntities, &$fieldOptions) {
91 foreach (self::getSearchQueryObjects() as $obj) {
92 $obj->alterSearchBuilderOptions($apiEntities, $fieldOptions);
93 }
94 }
95
96 /**
97 * @param $query
98 * @param string $fnName
99 */
100 public function alterSearchQuery(&$query, $fnName) {
101 foreach (self::getSearchQueryObjects() as $obj) {
102 $obj->$fnName($query);
103 }
104 }
105
106 /**
107 * @param string $fieldName
108 * @param $mode
109 * @param $side
110 *
111 * @return string
112 */
113 public function buildSearchfrom($fieldName, $mode, $side) {
114 $from = '';
115 foreach (self::getSearchQueryObjects() as $obj) {
116 $from .= $obj->from($fieldName, $mode, $side);
117 }
118 return $from;
119 }
120
121 /**
122 * @param $tables
123 */
124 public function setTableDependency(&$tables) {
125 foreach (self::getSearchQueryObjects() as $obj) {
126 $obj->setTableDependency($tables);
127 }
128 }
129
130 /**
131 * @param $panes
132 */
133 public function registerAdvancedSearchPane(&$panes) {
134 foreach (self::getSearchQueryObjects() as $obj) {
135 $obj->registerAdvancedSearchPane($panes);
136 }
137 }
138
139 /**
140 * @param $panes
141 */
142 public function getPanesMapper(&$panes) {
143 foreach (self::getSearchQueryObjects() as $obj) {
144 $obj->getPanesMapper($panes);
145 }
146 }
147
148 /**
149 * @param CRM_Core_Form $form
150 * @param $type
151 */
152 public function buildAdvancedSearchPaneForm(&$form, $type) {
153 foreach (self::getSearchQueryObjects() as $obj) {
154 $obj->buildAdvancedSearchPaneForm($form, $type);
155 }
156 }
157
158 /**
159 * @param $paneTemplatePathArray
160 * @param $type
161 */
162 public function setAdvancedSearchPaneTemplatePath(&$paneTemplatePathArray, $type) {
163 foreach (self::getSearchQueryObjects() as $obj) {
164 $obj->setAdvancedSearchPaneTemplatePath($paneTemplatePathArray, $type);
165 }
166 }
167 }