From 7728d8a90cfa0ae19447ae9bd40865f3dee90e27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Subtil?= Date: Sat, 30 Oct 2010 20:56:09 +0800 Subject: [PATCH] Added hide module. --- 3.0/modules/hide/controllers/admin_hide.php | 52 +++++++ 3.0/modules/hide/controllers/display.php | 70 ++++++++++ 3.0/modules/hide/helpers/MY_item.php | 34 +++++ 3.0/modules/hide/helpers/hide.php | 147 ++++++++++++++++++++ 3.0/modules/hide/helpers/hide_event.php | 88 ++++++++++++ 3.0/modules/hide/helpers/hide_installer.php | 38 +++++ 3.0/modules/hide/models/hidden_item.php | 24 ++++ 3.0/modules/hide/module.info | 3 + 3.0/modules/hide/views/admin_hide.html.php | 7 + 9 files changed, 463 insertions(+) create mode 100644 3.0/modules/hide/controllers/admin_hide.php create mode 100644 3.0/modules/hide/controllers/display.php create mode 100644 3.0/modules/hide/helpers/MY_item.php create mode 100644 3.0/modules/hide/helpers/hide.php create mode 100644 3.0/modules/hide/helpers/hide_event.php create mode 100644 3.0/modules/hide/helpers/hide_installer.php create mode 100644 3.0/modules/hide/models/hidden_item.php create mode 100644 3.0/modules/hide/module.info create mode 100644 3.0/modules/hide/views/admin_hide.html.php diff --git a/3.0/modules/hide/controllers/admin_hide.php b/3.0/modules/hide/controllers/admin_hide.php new file mode 100644 index 00000000..a613d550 --- /dev/null +++ b/3.0/modules/hide/controllers/admin_hide.php @@ -0,0 +1,52 @@ +page_title = t("Item hiding settings"); + $view->content = new View("admin_hide.html"); + $view->content->form = $this->_get_admin_form(); + $view->content->title = $view->page_title; + print $view; + } + + public function save() { + access::verify_csrf(); + $form = $this->_get_admin_form(); + $form->validate(); + module::set_var("hide", "access_permissions", + $form->access_permissions->value); + message::success(t("Item hiding settings updated")); + url::redirect("admin/hide"); + } + + private function _get_admin_form() { + $form = new Forge("admin/hide/save", "", "post", + array("id" => "g-hide-admin-form")); + $form->dropdown("access_permissions") + ->label(t("Who can see hidden items?")) + ->options(hide::get_groups_as_dropdown_options()) + ->selected(module::get_var("hide", "access_permissions")); + $form->submit("save")->value(t("Save")); + return $form; + } +} diff --git a/3.0/modules/hide/controllers/display.php b/3.0/modules/hide/controllers/display.php new file mode 100644 index 00000000..6dd2dfed --- /dev/null +++ b/3.0/modules/hide/controllers/display.php @@ -0,0 +1,70 @@ +%title item", array("title" => html::purify($item->title))); + + $this->_check_hide_permissions($item); + hide::hide($item); + message::success($msg); + + json::reply(array("result" => "success", "reload" => 1)); + } + + /** + * Allows the given item to be displayed again. + * + * @param int $id the item id + */ + public function show($id) { + $item = model_cache::get("item", $id); + $msg = t("Displayed %title item", array("title" => html::purify($item->title))); + + $this->_check_hide_permissions($item); + hide::show($item); + message::success($msg); + + json::reply(array("result" => "success", "reload" => 1)); + } + + /** + * Checks whether the given object can be hidden by the active user. + * + * @param Item_Model $item the item + */ + private function _check_hide_permissions(Item_Model $item) { + access::verify_csrf(); + + access::required("view", $item); + access::required("edit", $item); + + if (!hide::can_hide()) { + access::forbidden(); + } + } +} diff --git a/3.0/modules/hide/helpers/MY_item.php b/3.0/modules/hide/helpers/MY_item.php new file mode 100644 index 00000000..da546c39 --- /dev/null +++ b/3.0/modules/hide/helpers/MY_item.php @@ -0,0 +1,34 @@ +join("hidden_items", "items.id", "hidden_items.item_id", "LEFT OUTER") + ->and_where("hidden_items.item_id", "IS", NULL); + } + + return $model; + } +} diff --git a/3.0/modules/hide/helpers/hide.php b/3.0/modules/hide/helpers/hide.php new file mode 100644 index 00000000..38bd50fc --- /dev/null +++ b/3.0/modules/hide/helpers/hide.php @@ -0,0 +1,147 @@ +select_list("id", "name"); + return array_merge(array(self::NONE => t("Nobody")), $options); + } + + /** + * Returns the hidden_item model related to the given item. + * + * There is an attempt to fetch the model from the database through the model + * cache. If it fails, a new unsaved model is created. + * + * @param Item_Model $item the item + * @return Hidden_Item_Model the related hidden_item model + */ + static function get_hidden_item_model(Item_Model $item) { + try { + $model = model_cache::get("item", $id); + } + catch (Exception $e) { + $model = ORM::factory("hidden_item"); + $model->item_id = $item->id; + $model->validate(); + } + + return $model; + } + + /** + * Returns whether the given item can be hidden. + * + * @param Item_Model $item the item + * @return bool + */ + static function can_be_hidden(Item_Model $item) { + if (empty($item)) { + return false; + } + + if ($item->type == "album") { + return false; + } + + return true; + } + + /** + * Returns whether the given item is hidden. + * + * @param Item_Model $item the item + * @return bool + */ + static function is_hidden(Item_Model $item) { + $model = self::get_hidden_item_model($item); + return $model->loaded(); + } + + /** + * Hides the given item. + * + * @param Item_Model $item the item to hide + */ + static function hide(Item_Model $item) { + if (self::is_hidden($item)) { + return; + } + + $hidden_item = self::get_hidden_item_model($item); + $hidden_item->save(); + } + + /** + * Allows the given item to be displayed again. + * + * @param Item_Model $item the item to display + */ + static function show(Item_Model $item) { + if (!self::is_hidden($item)) { + return; + } + + $hidden_item = self::get_hidden_item_model($item); + $hidden_item->delete(); + } + + /** + * Returns whether the active user can view hidden items. + * + * @return bool + */ + static function can_view_hidden_items() { + if (identity::active_user()->admin) { + return true; + } + + $authorized_group = module::get_var("hide", "access_permissions"); + if (in_array($authorized_group, identity::group_ids_for_active_user())) { + return true; + } + + return false; + } + + /** + * Returns whether the active user can hide any items. + * + * @return bool + */ + static function can_hide() { + if (identity::active_user()->admin) { + return true; + } + + return false; + } +} diff --git a/3.0/modules/hide/helpers/hide_event.php b/3.0/modules/hide/helpers/hide_event.php new file mode 100644 index 00000000..da5962fa --- /dev/null +++ b/3.0/modules/hide/helpers/hide_event.php @@ -0,0 +1,88 @@ +get("settings_menu") + ->append(Menu::factory("link") + ->label(t("Item hiding")) + ->url(url::site("admin/hide"))); + } + + static function site_menu($menu, $theme, $item_css_selector) { + $item = $theme->item(); + + if (!empty($item) && hide::can_be_hidden($item) && hide::can_hide($item)) { + $csrf = access::csrf_token(); + $link = self::_get_hide_link_data($item); + + $menu->get("options_menu") + ->append(Menu::factory("ajax_link") + ->label($link["text"]) + ->ajax_handler("function(data) { window.location.reload() }") + ->url(url::site("display/".$link["action"]."/$item->id?csrf=$csrf"))); + } + } + + static function context_menu($menu, $theme, $item, $thumb_css_selector) { + if (hide::can_be_hidden($item) && hide::can_hide($item)) { + $csrf = access::csrf_token(); + $link = self::_get_hide_link_data($item); + + $menu + ->get("options_menu") + ->append(Menu::factory("ajax_link") + ->label($link["text"]) + ->ajax_handler("function(data) { window.location.reload() }") + ->url(url::site("display/".$link["action"]."/$item->id?csrf=$csrf"))); + } + } + + /** + * Returns some data used to create a hide link. + * + * @param Item_Model $item the related item + * @return array + */ + private static function _get_hide_link_data(Item_Model $item) { + if (hide::is_hidden($item)) { + $action = "show"; + $action_label = "Show"; + } + else { + $action = "hide"; + $action_label = "Hide"; + } + + switch ($item->type) { + case "movie": + $item_type_label = "movie"; + break; + default: + $item_type_label = "photo"; + break; + } + + $label = t("$action_label this $item_type_label"); + + return array("text" => $label, "action" => $action); + } +} diff --git a/3.0/modules/hide/helpers/hide_installer.php b/3.0/modules/hide/helpers/hide_installer.php new file mode 100644 index 00000000..9bac8cbc --- /dev/null +++ b/3.0/modules/hide/helpers/hide_installer.php @@ -0,0 +1,38 @@ +query("CREATE TABLE IF NOT EXISTS {hidden_items} ( + `item_id` int(9) NOT NULL, + PRIMARY KEY (`item_id`)) + DEFAULT CHARSET=utf8;"); + + module::set_var("hide", "access_permissions", hide::NONE); + module::set_version("hide", 1); + } + + static function uninstall() { + $db = Database::instance(); + $db->query("DROP TABLE IF EXISTS {hidden_items};"); + } +} diff --git a/3.0/modules/hide/models/hidden_item.php b/3.0/modules/hide/models/hidden_item.php new file mode 100644 index 00000000..a779a2dc --- /dev/null +++ b/3.0/modules/hide/models/hidden_item.php @@ -0,0 +1,24 @@ + +
+

+
+ +
+