1
0

Initial commit of star module. No changes since 11 Dec 2010 forum post.

This commit is contained in:
pez252 2011-01-03 00:43:36 +08:00 committed by Bharat Mediratta
parent 6383a1c075
commit 7aec401592
13 changed files with 750 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_Star_Controller extends Admin_Controller {
public function index() {
$view = new Admin_View("admin.html");
$view->page_title = t("Star settings");
$view->content = new View("admin_star.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("star", "show",
$form->show->value);
message::success(t("Star settings updated"));
url::redirect("admin/star");
}
private function _get_admin_form() {
$form = new Forge("admin/star/save", "", "post",
array("id" => "g-star-admin-form"));
$form->dropdown("show")
->label(t("Default to showing..."))
->options(array(0 => "All",1 => "Starred"))
->selected(module::get_var("star", "show"));
$form->submit("save")->value(t("Save"));
return $form;
}
}

View File

@ -0,0 +1,94 @@
<?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 {
/**
* Stars the given item.
*
* @param int $id the item id
*/
public function star($id) {
$item = model_cache::get("item", $id);
$msg = t("Starred <b>%title</b> item", array("title" => html::purify($item->title)));
$this->_check_star_permissions($item);
star::star($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 unstar($id) {
$item = model_cache::get("item", $id);
$msg = t("Un-starred <b>%title</b> item", array("title" => html::purify($item->title)));
$this->_check_star_permissions($item);
star::unstar($item);
message::success($msg);
json::reply(array("result" => "success", "reload" => 1));
}
public function star_only_on() {
//$item = model_cache::get("item", $id);
access::verify_csrf();
$msg = t("Showing starred items.");
//$this->_check_star_permissions($item);
star::star_only_on();
message::success($msg);
json::reply(array("result" => "success", "reload" => 1));
}
public function star_only_off() {
//$item = model_cache::get("item", $id);
access::verify_csrf();
$msg = t("Showing all items.");
//$this->_check_star_permissions($item);
star::star_only_off();
message::success($msg);
json::reply(array("result" => "success", "reload" => 1));
}
/**
* Checks whether the given object can be starred by the active user.
*
* @param Item_Model $item the item
*/
private function _check_star_permissions(Item_Model $item) {
access::verify_csrf();
access::required("view", $item);
access::required("edit", $item);
if (!star::can_star()) {
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 (star::show_only_starred_items($model)) {
// only fetches items that are starred
$model->join("starred_items", "items.id", "starred_items.item_id", "LEFT OUTER")
->and_where("starred_items.item_id", "IS", TRUE);
}
return $model;
}
}

View File

@ -0,0 +1,163 @@
<?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 star_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 starred_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 Starred_Item_Model the related starred_item model
*/
static function get_starred_item_model(Item_Model $item) {
try {
$model = model_cache::get("item", $id);
}
catch (Exception $e) {
$model = ORM::factory("starred_item");
$model->item_id = $item->id;
$model->validate();
}
return $model;
}
static function get_star_user_model() {
$model = ORM::factory("starred_only_user");
$model->user_id = identity::active_user()->id;
$model->validate();
return $model;
}
/**
* Returns whether the given item can be starred.
*
* @param Item_Model $item the item
* @return bool
*/
static function can_be_starred(Item_Model $item) {
if (empty($item)) {
return false;
}
//if ($item->type == "album") {
// return false;
//}
return true;
}
/**
* Returns whether the given item is starred.
*
* @param Item_Model $item the item
* @return bool
*/
static function is_starred(Item_Model $item) {
$model = self::get_starred_item_model($item);
return $model->loaded();
}
/**
* Stars the given item.
*
* @param Item_Model $item the item to star
*/
static function star(Item_Model $item) {
if (self::is_starred($item)) {
return;
}
$starred_item = self::get_starred_item_model($item);
$starred_item->save();
}
/**
* Allows the given item to be unstarred.
*
* @param Item_Model $item the item to display
*/
static function unstar(Item_Model $item) {
if (!self::is_starred($item)) {
return;
}
$starred_item = self::get_starred_item_model($item);
$starred_item->delete();
}
static function star_only_on() {
if (self::show_only_starred_items()) {
return;
}
$star_user = self::get_star_user_model();
$star_user->save();
}
static function star_only_off() {
if (!self::show_only_starred_items()) {
return;
}
$star_user = self::get_star_user_model();
$star_user->delete();
}
/**
* Returns whether the active user shows only starred items.
*
* @return bool
*/
static function show_only_starred_items() {
$model = self::get_star_user_model();
return $model->loaded();
}
/**
* Returns whether the active user can star any items.
*
* @return bool
*/
static function can_star() {
if (identity::active_user()->admin) {
return true;
}
return false;
}
}

View File

@ -0,0 +1,51 @@
<?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 star_block {
static function get_site_list() {
return array("star" => t("Star Item"));
}
static function get($block_id, $theme) {
$item = $theme->item;
switch ($block_id) {
case "star":
// If Item is movie then...
//if ($item && $item->is_movie() && access::can("view_full", $item)) {
$block = new Block();
$block->css_id = "g-star-options";
$block->title = t("Star");
$block->content = new View("star_block.html");
return $block;
//}
// If Item is photo then...
//if ($item && $item->is_photo() && access::can("view_full", $item)) {
// $block = new Block();
// $block->css_id = "g-download-fullsize";
// $block->title = t("Download Photo");
// $block->content = new View("downloadfullsize_block.html");
// return $block;
//}
}
return "";
}
}

View File

@ -0,0 +1,91 @@
<?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 star_event_Core {
static function admin_menu($menu, $theme) {
$menu->get("settings_menu")
->append(Menu::factory("link")
->label(t("Star"))
->url(url::site("admin/star")));
}
static function site_menu($menu, $theme, $item_css_selector) {
$item = $theme->item();
if (!empty($item) && star::can_be_starred($item) && star::can_star($item)) {
$csrf = access::csrf_token();
$link = self::_get_star_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 (star::can_be_starred($item) && star::can_star($item)) {
$csrf = access::csrf_token();
$link = self::_get_star_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 star link.
*
* @param Item_Model $item the related item
* @return array
*/
private static function _get_star_link_data(Item_Model $item) {
if (star::is_starred($item)) {
$action = "unstar";
$action_label = "Unstar";
}
else {
$action = "star";
$action_label = "Star";
}
switch ($item->type) {
case "movie":
$item_type_label = "movie";
break;
case "album":
$item_type_label = "album";
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,43 @@
<?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 star_installer {
static function install() {
$db = Database::instance();
$db->query("CREATE TABLE IF NOT EXISTS {starred_items} (
`item_id` int(9) NOT NULL,
PRIMARY KEY (`item_id`))
DEFAULT CHARSET=utf8;");
$db->query("CREATE TABLE IF NOT EXISTS {starred_only_users} (
`user_id` int(9) NOT NULL,
PRIMARY KEY (`user_id`))
DEFAULT CHARSET=utf8;");
module::set_var("star", "access_permissions", 0);
module::set_version("star", 1);
}
static function uninstall() {
$db = Database::instance();
$db->query("DROP TABLE IF EXISTS {starred_items};");
$db->query("DROP TABLE IF EXISTS {starred_only_users};");
}
}

View File

@ -0,0 +1,23 @@
<?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 star_theme {
static function album_blocks($theme) {
}
}

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 Starred_Item_Model extends ORM {
protected $primary_key = "item_id";
}

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 Starred_Only_User_Model extends ORM {
protected $primary_key = "user_id";
}

View File

@ -0,0 +1,3 @@
name = "Star"
description = "Star favourite photos/albums. Toggle between hiding the rest or showing them."
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>

View File

@ -0,0 +1,141 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<? if ($theme->item->is_photo()) { ?>
<? if (star::is_starred($theme->item)) { ?>
<? $csrf = access::csrf_token(); ?>
<script type="text/javascript">
function unstar_item() {
var http = false;
if(navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http = new XMLHttpRequest();
}
http.open("GET", "<?= url::site("display/unstar/{$theme->item->id}?csrf={$csrf}") ?>");
http.onreadystatechange=function() { }
http.send(null);
window.location.reload();
}
</script>
<div class="g-star-block">
<a href="javascript: unstar_item()"
title="<?= t("Unstar Image") ?>"
class="g-button ui-icon-left ui-state-default ui-corner-all"><?= t("Unstar Image") ?></a>
</div>
<? } ?>
<? } ?>
<? if ($theme->item->is_photo()) { ?>
<? if (!star::is_starred($theme->item)) { ?>
<? $csrf = access::csrf_token(); ?>
<script type="text/javascript">
function star_item() {
var http = false;
if(navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http = new XMLHttpRequest();
}
http.open("GET", "<?= url::site("display/star/{$theme->item->id}?csrf={$csrf}") ?>");
http.onreadystatechange=function() { }
http.send(null);
window.location.reload();
}
</script>
<div class="g-star-block">
<a href="javascript:star_item()"
title="<?= t("Star Image") ?>"
class="g-button ui-icon-left ui-state-default ui-corner-all"><?= t("Star Image") ?></a>
</div>
<? } ?>
<? } ?>
<? if (false) { //old style. Notifications worked, but gave you a download popup and required manual refresh to see change.?>
<? if (!star::is_starred($theme->item)) { ?>
<? $csrf = access::csrf_token(); ?>
<div class="g-star-block">
<a href="<?= url::site("display/star/{$theme->item->id}?csrf={$csrf}") ?>"
ajax_handler="function(data) { window.location.reload() }"
title="<?= t("Star options") ?>"
class="g-button ui-icon-left ui-state-default ui-corner-all"><?= t("Star Image") ?></a>
</div>
<? } ?>
<? } ?>
<? if (star::show_only_starred_items()) { ?>
<? $csrf = access::csrf_token(); ?>
<div class="g-download-fullsize-block">
<script type="text/javascript">
function star_only_off() {
var http = false;
if(navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http = new XMLHttpRequest();
}
http.open("GET", "<?= url::site("display/star_only_off/?csrf={$csrf}") ?>");
http.onreadystatechange=function() { }
http.send(null);
window.location.reload();
}
</script>
<a href="javascript:star_only_off()"
title="<?= t("Show all items.") ?>"
class="g-button ui-icon-left ui-state-default ui-corner-all"><?= t("Show all items.") ?></a>
</div>
<? } ?>
<? if (!star::show_only_starred_items()) { ?>
<? $csrf = access::csrf_token(); ?>
<div class="g-download-fullsize-block">
<script type="text/javascript">
function star_only_on() {
var http = false;
if(navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http = new XMLHttpRequest();
}
http.open("GET", "<?= url::site("display/star_only_on/?csrf={$csrf}") ?>");
http.onreadystatechange=function() { }
http.send(null);
window.location.reload();
}
</script>
<a href="javascript:star_only_on()"
title="<?= t("Show only starred.") ?>"
class="g-button ui-icon-left ui-state-default ui-corner-all"><?= t("Show only starred.") ?></a>
</div>
<? } ?>
<? //Buttons were like the below. The notification message worked when doing this, but you got a popup to download the response, and had to manually reload. Something was wrong.?>
<? if (false) { ?>
<? $csrf = access::csrf_token(); ?>
<div class="g-download-fullsize-block">
<a href="<?= url::site("display/star_only_on/?csrf={$csrf}") ?>"
ajax_handler="function(data) { window.location.reload() }"
title="<?= t("Show only starred.") ?>"
class="g-ajax-link g-button ui-icon-left ui-state-default ui-corner-all"><?= t("Show only starred.") ?></a>
</div>
<? } ?>