From 546d9fa0180a9e17fae266fdd43cdd617925e8f8 Mon Sep 17 00:00:00 2001 From: shadlaws Date: Sat, 2 Jun 2012 14:21:19 +0200 Subject: [PATCH] Short_Search_Fix Version 1 - first commit to github. --- .../controllers/admin_short_search_fix.php | 63 +++++++++++++++++++ .../short_search_fix/helpers/MY_search.php | 58 +++++++++++++++++ .../helpers/short_search_fix_event.php | 45 +++++++++++++ .../helpers/short_search_fix_installer.php | 41 ++++++++++++ 3.0/modules/short_search_fix/module.info | 7 +++ .../views/admin_short_search_fix.html.php | 10 +++ 6 files changed, 224 insertions(+) create mode 100644 3.0/modules/short_search_fix/controllers/admin_short_search_fix.php create mode 100644 3.0/modules/short_search_fix/helpers/MY_search.php create mode 100644 3.0/modules/short_search_fix/helpers/short_search_fix_event.php create mode 100644 3.0/modules/short_search_fix/helpers/short_search_fix_installer.php create mode 100644 3.0/modules/short_search_fix/module.info create mode 100644 3.0/modules/short_search_fix/views/admin_short_search_fix.html.php diff --git a/3.0/modules/short_search_fix/controllers/admin_short_search_fix.php b/3.0/modules/short_search_fix/controllers/admin_short_search_fix.php new file mode 100644 index 00000000..a5620e89 --- /dev/null +++ b/3.0/modules/short_search_fix/controllers/admin_short_search_fix.php @@ -0,0 +1,63 @@ +page_title = t("Short search fix settings"); + $view->content = new View("admin_short_search_fix.html"); + $view->content->form = $this->_get_admin_form(); + print $view; + } + + public function save() { + access::verify_csrf(); + $form = $this->_get_admin_form(); + $form->validate(); + module::set_var("short_search_fix", "search_prefix", + $form->short_search_fix_settings->search_prefix->value); + message::success(t("Short search fix settings updated")); + if ($form->short_search_fix_settings->mark_rebuild_search_records->value) { + $db = Database::instance(); + $db->query("UPDATE {search_records} SET dirty=1;"); + } + if ($form->short_search_fix_settings->mark_uptodate_search_records->value) { + $db = Database::instance(); + $db->query("UPDATE {search_records} SET dirty=0;"); + } + url::redirect("admin/short_search_fix"); + } + + private function _get_admin_form() { + $form = new Forge("admin/short_search_fix/save", "", "post", + array("id" => "g-short-search-fix-admin-form")); + $short_search_fix_settings = $form->group("short_search_fix_settings")->label(t("Prefix and search record rebuild")); + $short_search_fix_settings->input("search_prefix") + ->label(t("Enter the prefix to be added to the start of every search word (Default: 1Z)")) + ->value(module::get_var("short_search_fix", "search_prefix")); + $short_search_fix_settings->checkbox("mark_rebuild_search_records") + ->label(t("Mark all search records for rebuild. This is needed when the prefix is changed. Afterward, go to Maintenace | Update search records.")) + ->checked(false); + $short_search_fix_settings->checkbox("mark_uptodate_search_records") + ->label(t("Mark all search records as up-to-date. This is a pseudo-undo of the above.")) + ->checked(false); + $short_search_fix_settings->submit("save")->value(t("Save")); + return $form; + } +} \ No newline at end of file diff --git a/3.0/modules/short_search_fix/helpers/MY_search.php b/3.0/modules/short_search_fix/helpers/MY_search.php new file mode 100644 index 00000000..8e4b8830 --- /dev/null +++ b/3.0/modules/short_search_fix/helpers/MY_search.php @@ -0,0 +1,58 @@ + 0) { + $terms = ' ' . $terms; + $terms = str_replace(' ', ' '.$prefix, $terms); + $terms = str_replace(' '.$prefix.'"', ' '.'"'.$prefix, $terms); + $terms = substr($terms,1); + } + return $terms; + } +} diff --git a/3.0/modules/short_search_fix/helpers/short_search_fix_event.php b/3.0/modules/short_search_fix/helpers/short_search_fix_event.php new file mode 100644 index 00000000..db418ebd --- /dev/null +++ b/3.0/modules/short_search_fix/helpers/short_search_fix_event.php @@ -0,0 +1,45 @@ +get("settings_menu") + ->append(Menu::factory("link") + ->id("short_search_fix") + ->label(t("Short search fix")) + ->url(url::site("admin/short_search_fix"))); + } + + // This is the function that changes what's written to the search_records database + static function item_index_data($item, $data) { + $prefix = module::get_var("short_search_fix","search_prefix"); + foreach ($data as &$terms) { + // strip leading, trailing, and extra whitespaces + $terms = preg_replace('/^\s+/', '', $terms); + $terms = preg_replace('/\s+$/', '', $terms); + $terms = preg_replace('/\s\s+/', ' ', $terms); + // add the prefixes + if (preg_match('/\w/',$terms) > 0) { + $terms = $prefix . str_replace(' ', ' '.$prefix, $terms); + } + } + } + +} diff --git a/3.0/modules/short_search_fix/helpers/short_search_fix_installer.php b/3.0/modules/short_search_fix/helpers/short_search_fix_installer.php new file mode 100644 index 00000000..1db9cb27 --- /dev/null +++ b/3.0/modules/short_search_fix/helpers/short_search_fix_installer.php @@ -0,0 +1,41 @@ +query("UPDATE {search_records} SET dirty=1;"); + } + + static function deactivate() { + $db = Database::instance(); + $db->query("UPDATE {search_records} SET dirty=1;"); + } + + static function uninstall() { + $db = Database::instance(); + module::clear_var("short_search_fix", "search_prefix"); + } +} diff --git a/3.0/modules/short_search_fix/module.info b/3.0/modules/short_search_fix/module.info new file mode 100644 index 00000000..b24d10d2 --- /dev/null +++ b/3.0/modules/short_search_fix/module.info @@ -0,0 +1,7 @@ +name = "Short Search Fix" +description = "Allows 2-3 letter searches to be performed without requiring SQL system variable modification (useful for shared hosting)." +version = 1 +author_name = "Shad Laws" +author_url = "" +info_url = "http://codex.gallery2.org/Gallery3:Modules:short_search_fix" +discuss_url = "http://gallery.menalto.com/node/105935" \ No newline at end of file diff --git a/3.0/modules/short_search_fix/views/admin_short_search_fix.html.php b/3.0/modules/short_search_fix/views/admin_short_search_fix.html.php new file mode 100644 index 00000000..81ecea86 --- /dev/null +++ b/3.0/modules/short_search_fix/views/admin_short_search_fix.html.php @@ -0,0 +1,10 @@ + +
+

+

+ +

+
+ +
+