From 4438b033cbec62cb5262d268285ffb8630c2e788 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Sat, 15 Nov 2014 15:39:34 +0100 Subject: [PATCH] Added an option to set each simple page searchable or not. --- SimplePagesPlugin.php | 20 +++++++++++++++++++- controllers/IndexController.php | 11 +++++++++++ models/Api/SimplePagesPage.php | 1 + models/SimplePagesPage.php | 12 ++++++++---- models/SimplePagesPageTable.php | 1 + 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/SimplePagesPlugin.php b/SimplePagesPlugin.php index 4eb8540..03a3014 100644 --- a/SimplePagesPlugin.php +++ b/SimplePagesPlugin.php @@ -48,6 +48,7 @@ public function hookInstall() `modified_by_user_id` int(10) unsigned NOT NULL, `created_by_user_id` int(10) unsigned NOT NULL, `is_published` tinyint(1) NOT NULL, + `is_searchable` tinyint(1) NOT NULL, `title` tinytext COLLATE utf8_unicode_ci NOT NULL, `slug` tinytext COLLATE utf8_unicode_ci NOT NULL, `text` mediumtext COLLATE utf8_unicode_ci, @@ -67,12 +68,13 @@ public function hookInstall() KEY `parent_id` (`parent_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $db->query($sql); - + // Save an example page. $page = new SimplePagesPage; $page->modified_by_user_id = current_user()->id; $page->created_by_user_id = current_user()->id; $page->is_published = 1; + $page->is_searchable = 1; $page->parent_id = 0; $page->title = 'About'; $page->slug = 'about'; @@ -154,6 +156,22 @@ public function hookUpgrade($args) if ($oldVersion < '3.0.2') { $db->query("ALTER TABLE `$db->SimplePagesPage` MODIFY `text` MEDIUMTEXT COLLATE utf8_unicode_ci"); } + + if ($oldVersion < '3.0.4') { + // Check if "is_searchable" exists, because the patch is rebased. + $sql = "SHOW columns FROM `$db->SimplePagesPage` WHERE `Field` = 'is_searchable';"; + $result = $db->query($sql)->fetchAll(); + if (empty($result)) { + $sql = " + ALTER TABLE `$db->SimplePagesPage` + ADD `is_searchable` tinyint(1) NOT NULL AFTER `is_published` + "; + $db->query($sql); + // Set all existing pages as searchable. + $sql = "UPDATE `$db->SimplePagesPage` SET `is_searchable` = '1';"; + $db->query($sql); + } + } } /** diff --git a/controllers/IndexController.php b/controllers/IndexController.php index 93ee2e1..b23ebbf 100644 --- a/controllers/IndexController.php +++ b/controllers/IndexController.php @@ -148,6 +148,17 @@ protected function _getForm($page = null) $form->addElement('sessionCsrfToken', 'csrf_token'); } + $form->addElementToSaveGroup( + 'checkbox', 'is_searchable', + array( + 'id' => 'simple_pages_is_searchable', + 'values' => array(1, 0), + 'checked' => $page->is_searchable, + 'label' => __('Is this page searchable?'), + 'description' => __('Checking this box will make this page searchable') + ) + ); + return $form; } diff --git a/models/Api/SimplePagesPage.php b/models/Api/SimplePagesPage.php index f68a3ec..13c4ca5 100644 --- a/models/Api/SimplePagesPage.php +++ b/models/Api/SimplePagesPage.php @@ -8,6 +8,7 @@ public function getRepresentation(Omeka_Record_AbstractRecord $record) 'id' =>$record->id, 'url' => $this->getResourceUrl("/simple_pages/{$record->id}"), 'is_published' => (bool)$record->is_published, + 'is_searchable' => (bool)$record->is_searchable, 'title' => $record->title, 'slug' => $record->slug, 'text' => $record->text, diff --git a/models/SimplePagesPage.php b/models/SimplePagesPage.php index 4e3cf88..bef6037 100644 --- a/models/SimplePagesPage.php +++ b/models/SimplePagesPage.php @@ -16,6 +16,8 @@ class SimplePagesPage extends Omeka_Record_AbstractRecord implements Zend_Acl_Re public $modified_by_user_id; public $created_by_user_id; public $is_published = 0; + // A page is searchable by default. + public $is_searchable = 1; public $title; public $slug; public $text = null; @@ -116,12 +118,14 @@ protected function beforeSave($args) protected function afterSave($args) { - if (!$this->is_published) { + if (!$this->is_published || !$this->is_searchable) { $this->setSearchTextPrivate(); } - $this->setSearchTextTitle($this->title); - $this->addSearchText($this->title); - $this->addSearchText($this->text); + if ($this->is_searchable) { + $this->setSearchTextTitle($this->title); + $this->addSearchText($this->title); + $this->addSearchText($this->text); + } } /** diff --git a/models/SimplePagesPageTable.php b/models/SimplePagesPageTable.php index 594eaac..0f96d97 100644 --- a/models/SimplePagesPageTable.php +++ b/models/SimplePagesPageTable.php @@ -29,6 +29,7 @@ public function applySearchFilters($select, $params) $alias = $this->getTableAlias(); $paramNames = array('parent_id', 'is_published', + 'is_searchable', 'title', 'slug', 'created_by_user_id',