From 99e9587ab901cc70b87c356995b4b57773467823 Mon Sep 17 00:00:00 2001 From: Deepak Srivastava Date: Fri, 12 Jul 2013 15:10:30 +0530 Subject: [PATCH] CRM-12940, extending core search via hook_civicrm_queryObjects ---------------------------------------- * CRM-12940: Allow extensions to participate in CRM_*_BAO_Query http://issues.civicrm.org/jira/browse/CRM-12940 --- CRM/Contact/BAO/Query.php | 11 ++-- CRM/Contact/BAO/Query/Hook.php | 97 +++++++++++++++++++++++++++++ CRM/Contact/BAO/Query/Interface.php | 48 ++++++++++++++ CRM/Extension/Manager.php | 41 ------------ CRM/Utils/Hook.php | 11 ++++ 5 files changed, 162 insertions(+), 46 deletions(-) create mode 100644 CRM/Contact/BAO/Query/Hook.php create mode 100644 CRM/Contact/BAO/Query/Interface.php diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index cffcf2d71e..573c6b8c67 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -442,8 +442,8 @@ class CRM_Contact_BAO_Query { $fields = CRM_Activity_BAO_Activity::exportableFields(); $this->_fields = array_merge($this->_fields, $fields); - // add any fields supported by extensions - $extFields = CRM_Extension_System::singleton()->getManager()->getSearchQueryFields(); + // add any fields provided by hook implementers + $extFields = CRM_Contact_BAO_Query_Hook::singleton()->getFields(); $this->_fields = array_merge($this->_fields, $extFields); } @@ -772,7 +772,7 @@ class CRM_Contact_BAO_Query { //fix for CRM-951 CRM_Core_Component::alterQuery($this, 'select'); - CRM_Extension_System::singleton()->getManager()->alterSearchQuery($this, 'select'); + CRM_Contact_BAO_Query_Hook::singleton()->alterSearchQuery($this, 'select'); if (!empty($this->_cfIDs)) { $this->_customQuery = new CRM_Core_BAO_CustomQuery($this->_cfIDs, TRUE); @@ -1633,7 +1633,7 @@ class CRM_Contact_BAO_Query { CRM_Core_Component::alterQuery($this, 'where'); - CRM_Extension_System::singleton()->getManager()->alterSearchQuery($this, 'where'); + CRM_Contact_BAO_Query_Hook::singleton()->alterSearchQuery($this, 'where'); } if ($this->_customQuery) { @@ -2358,7 +2358,8 @@ class CRM_Contact_BAO_Query { default: $from .= CRM_Core_Component::from($name, $mode, $side); - $from .= CRM_Extension_System::singleton()->getManager()->buildSearchfrom($name, $mode, $side); + $from .= CRM_Contact_BAO_Query_Hook::singleton()->buildSearchfrom($name, $mode, $side); + continue; } } diff --git a/CRM/Contact/BAO/Query/Hook.php b/CRM/Contact/BAO/Query/Hook.php new file mode 100644 index 0000000000..f9b9ee03a5 --- /dev/null +++ b/CRM/Contact/BAO/Query/Hook.php @@ -0,0 +1,97 @@ +_queryObjects === NULL) { + $this->_queryObjects = array(); + CRM_Utils_Hook::queryObjects($this->_queryObjects); + CRM_Core_Error::debug_var( '$this->_queryObjects', $this->_queryObjects ); + } + return $this->_queryObjects; + } + + public function &getFields() { + $extFields = array(); + foreach (self::getSearchQueryObjects() as $obj) { + $flds = $obj->getFields(); + $extFields = array_merge($extFields, $flds); + } + return $extFields; + } + + public function alterSearchQuery(&$query, $fnName) { + foreach (self::getSearchQueryObjects() as $obj) { + $obj->$fnName($query); + } + } + + public function buildSearchfrom($fieldName, $mode, $side) { + $from = ''; + foreach (self::getSearchQueryObjects() as $obj) { + $from .= $obj->from($fieldName, $mode, $side); + } + return $from; + } +} \ No newline at end of file diff --git a/CRM/Contact/BAO/Query/Interface.php b/CRM/Contact/BAO/Query/Interface.php new file mode 100644 index 0000000000..18f0077c04 --- /dev/null +++ b/CRM/Contact/BAO/Query/Interface.php @@ -0,0 +1,48 @@ +mapper->refresh(); } - public function &getSearchQueryFields() { - $extFields = array(); - foreach (self::getActiveSearchQueryObjects() as $obj) { - $flds = $obj->getFields(); - $extFields = array_merge($extFields, $flds); - } - return $extFields; - } - - public function alterSearchQuery(&$query, $fnName) { - foreach (self::getActiveSearchQueryObjects() as $obj) { - $obj->$fnName($query); - } - } - - public function buildSearchfrom($fieldName, $mode, $side) { - foreach (self::getActiveSearchQueryObjects() as $obj) { - $obj->from($fieldName, $mode, $side); - } - } - - public function getActiveSearchQueryObjects() { - $queryObjects = array(); - $extStatus = $this->getStatuses(); - foreach ($extStatus as $key => $status) { - if ($status == CRM_Extension_Manager::STATUS_INSTALLED) { - $path = $this->mapper->keyToBasePath($key); - $info = $this->mapper->keyToInfo($key); - $classFile = $info->civix['namespace'] . DIRECTORY_SEPARATOR . 'BAO' . DIRECTORY_SEPARATOR . 'Query'; - $queryFile = $path . DIRECTORY_SEPARATOR . "{$classFile}.php"; - if (file_exists($queryFile)) { - require_once $queryFile; - $className = str_replace(DIRECTORY_SEPARATOR, '_', $classFile); - $obj = new $className(); - $queryObjects[] = $obj; - } - } - } - return $queryObjects; - } - // ---------------------- /** diff --git a/CRM/Utils/Hook.php b/CRM/Utils/Hook.php index 12d92b1f93..97888baa91 100644 --- a/CRM/Utils/Hook.php +++ b/CRM/Utils/Hook.php @@ -1285,4 +1285,15 @@ abstract class CRM_Utils_Hook { static function searchProfile($name) { return self::singleton()->invoke(1, $name, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, 'civicrm_searchProfile'); } + + /** + * This hook is called while building the core search query, + * so hook implementers can provide their own query objects which alters/extends core search. + * + * @param Array $queryObjects + * @return void + */ + static function queryObjects(&$queryObjects) { + return self::singleton()->invoke(1, $queryObjects, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, 'civicrm_queryObjects'); + } } -- 2.25.1