1
0

Added hide module.

This commit is contained in:
Jérémy Subtil 2010-10-30 20:56:09 +08:00 committed by Bharat Mediratta
parent b7c4bafe3f
commit 7728d8a90c
9 changed files with 463 additions and 0 deletions

View File

@ -0,0 +1,52 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Admin_Hide_Controller extends Admin_Controller {
public function index() {
$view = new Admin_View("admin.html");
$view->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;
}
}

View File

@ -0,0 +1,70 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Display_Controller extends Controller {
/**
* Hides the given item.
*
* @param int $id the item id
*/
public function hide($id) {
$item = model_cache::get("item", $id);
$msg = t("Hidden <b>%title</b> 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 <b>%title</b> 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();
}
}
}

View File

@ -0,0 +1,34 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class item extends item_Core {
static function viewable($model) {
$model = parent::viewable($model);
if (!hide::can_view_hidden_items($model)) {
// only fetches items that are not hidden
$model->join("hidden_items", "items.id", "hidden_items.item_id", "LEFT OUTER")
->and_where("hidden_items.item_id", "IS", NULL);
}
return $model;
}
}

View File

@ -0,0 +1,147 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class hide_Core {
/**
* Value defining no group can see hidden items.
*/
const NONE = 0;
/**
* Returns the group list for a dropdown widget.
*
* @return array the group list
*/
static function get_groups_as_dropdown_options() {
$options = ORM::factory("group")->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;
}
}

View File

@ -0,0 +1,88 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class hide_event_Core {
static function admin_menu($menu, $theme) {
$menu->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);
}
}

View File

@ -0,0 +1,38 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class hide_installer {
static function install() {
$db = Database::instance();
$db->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};");
}
}

View File

@ -0,0 +1,24 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Hidden_Item_Model extends ORM {
protected $primary_key = "item_id";
}

View File

@ -0,0 +1,3 @@
name = "Hide"
description = "Allows admins to hide some items from everyone but a given group."
version = 1

View File

@ -0,0 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<div class="g-block">
<h1> <?= $title ?> </h1>
<div class="g-block-content">
<?= $form ?>
</div>
</div>