From e991ce4426483c9302691b749f1d8b63e5162257 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 15 Oct 2020 09:19:16 -0400 Subject: [PATCH] Search ext: Add SearchDisplay database entity --- ext/search/CRM/Search/BAO/SearchDisplay.php | 17 + ext/search/CRM/Search/DAO/SearchDisplay.php | 300 ++++++++++++++++++ ext/search/Civi/Api4/SearchDisplay.php | 13 + .../SearchDisplayCreationSpecProvider.php | 34 ++ ext/search/managed/SearchDisplayType.mgd.php | 24 ++ ext/search/search.civix.php | 14 +- ext/search/search.php | 17 +- ext/search/sql/auto_install.sql | 69 ++++ ext/search/sql/auto_uninstall.sql | 22 ++ .../CRM/Search/SearchDisplay.entityType.php | 10 + .../xml/schema/CRM/Search/SearchDisplay.xml | 100 ++++++ 11 files changed, 615 insertions(+), 5 deletions(-) create mode 100644 ext/search/CRM/Search/BAO/SearchDisplay.php create mode 100644 ext/search/CRM/Search/DAO/SearchDisplay.php create mode 100644 ext/search/Civi/Api4/SearchDisplay.php create mode 100644 ext/search/Civi/Api4/Service/Spec/Provider/SearchDisplayCreationSpecProvider.php create mode 100644 ext/search/managed/SearchDisplayType.mgd.php create mode 100644 ext/search/sql/auto_install.sql create mode 100644 ext/search/sql/auto_uninstall.sql create mode 100644 ext/search/xml/schema/CRM/Search/SearchDisplay.entityType.php create mode 100644 ext/search/xml/schema/CRM/Search/SearchDisplay.xml diff --git a/ext/search/CRM/Search/BAO/SearchDisplay.php b/ext/search/CRM/Search/BAO/SearchDisplay.php new file mode 100644 index 0000000000..0d6f6651d5 --- /dev/null +++ b/ext/search/CRM/Search/BAO/SearchDisplay.php @@ -0,0 +1,17 @@ +__table = 'civicrm_search_display'; + parent::__construct(); + } + + /** + * Returns localized title of this entity. + * + * @param bool $plural + * Whether to return the plural version of the title. + */ + public static function getEntityTitle($plural = FALSE) { + return $plural ? E::ts('Search Displays') : E::ts('Search Display'); + } + + /** + * Returns foreign keys and entity references. + * + * @return array + * [CRM_Core_Reference_Interface] + */ + public static function getReferenceColumns() { + if (!isset(Civi::$statics[__CLASS__]['links'])) { + Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__); + Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'saved_search_id', 'civicrm_saved_search', 'id'); + CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']); + } + return Civi::$statics[__CLASS__]['links']; + } + + /** + * Returns all the column names of this table + * + * @return array + */ + public static function &fields() { + if (!isset(Civi::$statics[__CLASS__]['fields'])) { + Civi::$statics[__CLASS__]['fields'] = [ + 'id' => [ + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'title' => E::ts('Search Display ID'), + 'description' => E::ts('Unique SearchDisplay ID'), + 'required' => TRUE, + 'where' => 'civicrm_search_display.id', + 'table_name' => 'civicrm_search_display', + 'entity' => 'SearchDisplay', + 'bao' => 'CRM_Search_DAO_SearchDisplay', + 'localizable' => 0, + 'add' => '1.0', + ], + 'name' => [ + 'name' => 'name', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => E::ts('Search Display Name'), + 'description' => E::ts('Unique name for identifying search display'), + 'required' => TRUE, + 'maxlength' => 255, + 'size' => CRM_Utils_Type::HUGE, + 'where' => 'civicrm_search_display.name', + 'table_name' => 'civicrm_search_display', + 'entity' => 'SearchDisplay', + 'bao' => 'CRM_Search_DAO_SearchDisplay', + 'localizable' => 0, + 'html' => [ + 'type' => 'Text', + ], + 'add' => '1.0', + ], + 'label' => [ + 'name' => 'label', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => E::ts('Search Display Label'), + 'description' => E::ts('Label for identifying search display to administrators'), + 'required' => TRUE, + 'maxlength' => 255, + 'size' => CRM_Utils_Type::HUGE, + 'where' => 'civicrm_search_display.label', + 'table_name' => 'civicrm_search_display', + 'entity' => 'SearchDisplay', + 'bao' => 'CRM_Search_DAO_SearchDisplay', + 'localizable' => 0, + 'html' => [ + 'type' => 'Text', + ], + 'add' => '1.0', + ], + 'saved_search_id' => [ + 'name' => 'saved_search_id', + 'type' => CRM_Utils_Type::T_INT, + 'title' => E::ts('Saved Search ID'), + 'description' => E::ts('FK to saved search table.'), + 'required' => TRUE, + 'where' => 'civicrm_search_display.saved_search_id', + 'table_name' => 'civicrm_search_display', + 'entity' => 'SearchDisplay', + 'bao' => 'CRM_Search_DAO_SearchDisplay', + 'localizable' => 0, + 'FKClassName' => 'CRM_Contact_DAO_SavedSearch', + 'add' => '1.0', + ], + 'type' => [ + 'name' => 'type', + 'type' => CRM_Utils_Type::T_STRING, + 'title' => E::ts('Search Display Type'), + 'description' => E::ts('Type of display'), + 'required' => TRUE, + 'maxlength' => 128, + 'size' => CRM_Utils_Type::HUGE, + 'where' => 'civicrm_search_display.type', + 'table_name' => 'civicrm_search_display', + 'entity' => 'SearchDisplay', + 'bao' => 'CRM_Search_DAO_SearchDisplay', + 'localizable' => 0, + 'html' => [ + 'type' => 'Select', + ], + 'pseudoconstant' => [ + 'optionGroupName' => 'search_display_type', + 'optionEditPath' => 'civicrm/admin/options/search_display_type', + ], + 'add' => '1.0', + ], + 'settings' => [ + 'name' => 'settings', + 'type' => CRM_Utils_Type::T_TEXT, + 'title' => E::ts('Search Display Settings'), + 'description' => E::ts('Configuration data for the search display'), + 'where' => 'civicrm_search_display.settings', + 'default' => 'NULL', + 'table_name' => 'civicrm_search_display', + 'entity' => 'SearchDisplay', + 'bao' => 'CRM_Search_DAO_SearchDisplay', + 'localizable' => 0, + 'serialize' => self::SERIALIZE_JSON, + 'add' => '1.0', + ], + ]; + CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']); + } + return Civi::$statics[__CLASS__]['fields']; + } + + /** + * Return a mapping from field-name to the corresponding key (as used in fields()). + * + * @return array + * Array(string $name => string $uniqueName). + */ + public static function &fieldKeys() { + if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) { + Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields())); + } + return Civi::$statics[__CLASS__]['fieldKeys']; + } + + /** + * Returns the names of this table + * + * @return string + */ + public static function getTableName() { + return self::$_tableName; + } + + /** + * Returns if this table needs to be logged + * + * @return bool + */ + public function getLog() { + return self::$_log; + } + + /** + * Returns the list of fields that can be imported + * + * @param bool $prefix + * + * @return array + */ + public static function &import($prefix = FALSE) { + $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, 'search_display', $prefix, []); + return $r; + } + + /** + * Returns the list of fields that can be exported + * + * @param bool $prefix + * + * @return array + */ + public static function &export($prefix = FALSE) { + $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, 'search_display', $prefix, []); + return $r; + } + + /** + * Returns the list of indices + * + * @param bool $localize + * + * @return array + */ + public static function indices($localize = TRUE) { + $indices = [ + 'UI_saved_search__id_name' => [ + 'name' => 'UI_saved_search__id_name', + 'field' => [ + 0 => 'saved_search_id', + 1 => 'name', + ], + 'localizable' => FALSE, + 'unique' => TRUE, + 'sig' => 'civicrm_search_display::1::saved_search_id::name', + ], + ]; + return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices; + } + +} diff --git a/ext/search/Civi/Api4/SearchDisplay.php b/ext/search/Civi/Api4/SearchDisplay.php new file mode 100644 index 0000000000..33f44977b2 --- /dev/null +++ b/ext/search/Civi/Api4/SearchDisplay.php @@ -0,0 +1,13 @@ +getFieldByName('name')->setRequired(FALSE)->setRequiredIf('empty($values.label)'); + $spec->getFieldByName('label')->setRequired(FALSE)->setRequiredIf('empty($values.name)'); + } + + /** + * @inheritDoc + */ + public function applies($entity, $action) { + return $entity === 'SearchDisplay' && $action === 'create'; + } + +} diff --git a/ext/search/managed/SearchDisplayType.mgd.php b/ext/search/managed/SearchDisplayType.mgd.php new file mode 100644 index 0000000000..b8e3fa99b8 --- /dev/null +++ b/ext/search/managed/SearchDisplayType.mgd.php @@ -0,0 +1,24 @@ + 'SearchDisplayType', + 'entity' => 'OptionGroup', + 'params' => [ + 'name' => 'search_display_type', + 'title' => 'Search Display Type', + ], + ], + [ + 'name' => 'SearchDisplayType:table', + 'entity' => 'OptionValue', + 'params' => [ + 'option_group_id' => 'search_display_type', + 'name' => 'table', + 'value' => 'table', + 'label' => 'Table', + 'icon' => 'fa-table', + ], + ], +]; diff --git a/ext/search/search.civix.php b/ext/search/search.civix.php index 2a696e2cff..7ae23cc7de 100644 --- a/ext/search/search.civix.php +++ b/ext/search/search.civix.php @@ -7,9 +7,9 @@ * extension. */ class CRM_Search_ExtensionUtil { - const SHORT_NAME = "search"; - const LONG_NAME = "org.civicrm.search"; - const CLASS_PREFIX = "CRM_Search"; + const SHORT_NAME = 'search'; + const LONG_NAME = 'org.civicrm.search'; + const CLASS_PREFIX = 'CRM_Search'; /** * Translate a string using the extension's domain. @@ -473,5 +473,11 @@ function _search_civix_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) { * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes */ function _search_civix_civicrm_entityTypes(&$entityTypes) { - $entityTypes = array_merge($entityTypes, []); + $entityTypes = array_merge($entityTypes, [ + 'CRM_Search_DAO_SearchDisplay' => [ + 'name' => 'SearchDisplay', + 'class' => 'CRM_Search_DAO_SearchDisplay', + 'table' => 'civicrm_search_display', + ], + ]); } diff --git a/ext/search/search.php b/ext/search/search.php index 7c7f9e1a83..45b79b6aa2 100644 --- a/ext/search/search.php +++ b/ext/search/search.php @@ -121,8 +121,23 @@ function search_civicrm_entityTypes(&$entityTypes) { } /** - * Implements hook_civicrm_thems(). + * Implements hook_civicrm_themes(). */ function search_civicrm_themes(&$themes) { _search_civix_civicrm_themes($themes); } + +/** + * Implements hook_civicrm_pre(). + */ +function search_civicrm_pre($op, $entity, $id, &$params) { + // Supply default name/label when creating new SearchDisplay + if ($entity === 'SearchDisplay' && $op === 'create') { + if (empty($params['label'])) { + $params['label'] = $params['name']; + } + elseif (empty($params['name'])) { + $params['name'] = \CRM_Utils_String::munge($params['label']); + } + } +} diff --git a/ext/search/sql/auto_install.sql b/ext/search/sql/auto_install.sql new file mode 100644 index 0000000000..4bdafa82d5 --- /dev/null +++ b/ext/search/sql/auto_install.sql @@ -0,0 +1,69 @@ +-- +--------------------------------------------------------------------+ +-- | Copyright CiviCRM LLC. All rights reserved. | +-- | | +-- | This work is published under the GNU AGPLv3 license with some | +-- | permitted exceptions and without any warranty. For full license | +-- | and copyright information, see https://civicrm.org/licensing | +-- +--------------------------------------------------------------------+ +-- +-- Generated from schema.tpl +-- DO NOT EDIT. Generated by CRM_Core_CodeGen +-- + + +-- +--------------------------------------------------------------------+ +-- | Copyright CiviCRM LLC. All rights reserved. | +-- | | +-- | This work is published under the GNU AGPLv3 license with some | +-- | permitted exceptions and without any warranty. For full license | +-- | and copyright information, see https://civicrm.org/licensing | +-- +--------------------------------------------------------------------+ +-- +-- Generated from drop.tpl +-- DO NOT EDIT. Generated by CRM_Core_CodeGen +-- +-- /******************************************************* +-- * +-- * Clean up the exisiting tables +-- * +-- *******************************************************/ + +SET FOREIGN_KEY_CHECKS=0; + +DROP TABLE IF EXISTS `civicrm_search_display`; + +SET FOREIGN_KEY_CHECKS=1; +-- /******************************************************* +-- * +-- * Create new tables +-- * +-- *******************************************************/ + +-- /******************************************************* +-- * +-- * civicrm_search_display +-- * +-- * Search Kit - saved search displays +-- * +-- *******************************************************/ +CREATE TABLE `civicrm_search_display` ( + + + `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique SearchDisplay ID', + `name` varchar(255) NOT NULL COMMENT 'Unique name for identifying search display', + `label` varchar(255) NOT NULL COMMENT 'Label for identifying search display to administrators', + `saved_search_id` int unsigned NOT NULL COMMENT 'FK to saved search table.', + `type` varchar(128) NOT NULL COMMENT 'Type of display', + `settings` text DEFAULT NULL COMMENT 'Configuration data for the search display' +, + PRIMARY KEY (`id`) + + , UNIQUE INDEX `UI_saved_search__id_name`( + saved_search_id + , name + ) + +, CONSTRAINT FK_civicrm_search_display_saved_search_id FOREIGN KEY (`saved_search_id`) REFERENCES `civicrm_saved_search`(`id`) ON DELETE CASCADE +) ; + + \ No newline at end of file diff --git a/ext/search/sql/auto_uninstall.sql b/ext/search/sql/auto_uninstall.sql new file mode 100644 index 0000000000..7fc5f6b8f2 --- /dev/null +++ b/ext/search/sql/auto_uninstall.sql @@ -0,0 +1,22 @@ +-- +--------------------------------------------------------------------+ +-- | Copyright CiviCRM LLC. All rights reserved. | +-- | | +-- | This work is published under the GNU AGPLv3 license with some | +-- | permitted exceptions and without any warranty. For full license | +-- | and copyright information, see https://civicrm.org/licensing | +-- +--------------------------------------------------------------------+ +-- +-- Generated from drop.tpl +-- DO NOT EDIT. Generated by CRM_Core_CodeGen +-- +-- /******************************************************* +-- * +-- * Clean up the exisiting tables +-- * +-- *******************************************************/ + +SET FOREIGN_KEY_CHECKS=0; + +DROP TABLE IF EXISTS `civicrm_search_display`; + +SET FOREIGN_KEY_CHECKS=1; \ No newline at end of file diff --git a/ext/search/xml/schema/CRM/Search/SearchDisplay.entityType.php b/ext/search/xml/schema/CRM/Search/SearchDisplay.entityType.php new file mode 100644 index 0000000000..6629b57e20 --- /dev/null +++ b/ext/search/xml/schema/CRM/Search/SearchDisplay.entityType.php @@ -0,0 +1,10 @@ + 'SearchDisplay', + 'class' => 'CRM_Search_DAO_SearchDisplay', + 'table' => 'civicrm_search_display', + ], +]; diff --git a/ext/search/xml/schema/CRM/Search/SearchDisplay.xml b/ext/search/xml/schema/CRM/Search/SearchDisplay.xml new file mode 100644 index 0000000000..750977d933 --- /dev/null +++ b/ext/search/xml/schema/CRM/Search/SearchDisplay.xml @@ -0,0 +1,100 @@ + + + + CRM/Search + SearchDisplay + civicrm_search_display + Search Kit - saved search displays + true + + + id + Search Display ID + int unsigned + true + Unique SearchDisplay ID + 1.0 + + + id + true + 1.0 + + + + name + Search Display Name + Unique name for identifying search display + true + varchar + 255 + + Text + + 1.0 + + + + label + Search Display Label + Label for identifying search display to administrators + true + varchar + 255 + + Text + + 1.0 + + + + saved_search_id + int unsigned + Saved Search ID + FK to saved search table. + true + 1.0 + + + saved_search_id +
civicrm_saved_search
+ id + 1.0 + CASCADE + + + + UI_saved_search__id_name + saved_search_id + name + true + 1.0 + + + + type + Search Display Type + true + varchar + 128 + Type of display + + search_display_type + + 1.0 + + Select + + + + + settings + text + Search Display Settings + Configuration data for the search display + JSON + NULL + 1.0 + + + -- 2.25.1