2009-09-02 14:28:00 -07:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
|
/**
|
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
2010-03-03 10:15:34 -08:00
|
|
|
* Copyright (C) 2000-2010 Bharat Mediratta
|
2009-09-02 14:28:00 -07:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2009-09-02 21:29:22 -07:00
|
|
|
class Albums_Controller extends Items_Controller {
|
2009-11-30 11:10:58 -08:00
|
|
|
public function index() {
|
2009-12-01 00:08:12 -08:00
|
|
|
$this->show(ORM::factory("item", 1));
|
2009-11-30 11:10:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show($album) {
|
|
|
|
|
if (!is_object($album)) {
|
|
|
|
|
// show() must be public because we route to it in url::parse_url(), so make
|
|
|
|
|
// sure that we're actually receiving an object
|
2010-02-12 19:05:44 -08:00
|
|
|
throw new Kohana_404_Exception();
|
2009-11-30 11:10:58 -08:00
|
|
|
}
|
2010-02-10 08:45:14 -08:00
|
|
|
|
2010-02-12 16:40:44 -08:00
|
|
|
access::required("view", $album);
|
2009-09-02 21:29:22 -07:00
|
|
|
|
2010-02-10 08:45:14 -08:00
|
|
|
$page_size = module::get_var("gallery", "page_size", 9);
|
2009-12-21 21:27:43 -08:00
|
|
|
$input = Input::instance();
|
|
|
|
|
$show = $input->get("show");
|
2009-09-02 21:29:22 -07:00
|
|
|
|
|
|
|
|
if ($show) {
|
2009-09-21 11:35:27 -07:00
|
|
|
$child = ORM::factory("item", $show);
|
|
|
|
|
$index = $album->get_position($child);
|
|
|
|
|
if ($index) {
|
|
|
|
|
$page = ceil($index / $page_size);
|
|
|
|
|
if ($page == 1) {
|
|
|
|
|
url::redirect($album->abs_url());
|
|
|
|
|
} else {
|
|
|
|
|
url::redirect($album->abs_url("page=$page"));
|
|
|
|
|
}
|
2009-09-02 21:29:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-21 21:27:43 -08:00
|
|
|
$page = $input->get("page", "1");
|
2009-09-02 21:29:22 -07:00
|
|
|
$children_count = $album->viewable()->children_count();
|
|
|
|
|
$offset = ($page - 1) * $page_size;
|
|
|
|
|
$max_pages = max(ceil($children_count / $page_size), 1);
|
|
|
|
|
|
|
|
|
|
// Make sure that the page references a valid offset
|
|
|
|
|
if ($page < 1) {
|
2009-09-10 21:31:50 -07:00
|
|
|
url::redirect($album->abs_url());
|
2009-09-02 21:29:22 -07:00
|
|
|
} else if ($page > $max_pages) {
|
2009-09-10 21:31:50 -07:00
|
|
|
url::redirect($album->abs_url("page=$max_pages"));
|
2009-09-02 21:29:22 -07:00
|
|
|
}
|
|
|
|
|
|
2009-11-17 13:42:51 -08:00
|
|
|
$template = new Theme_View("page.html", "collection", "album");
|
2009-11-14 14:25:39 -08:00
|
|
|
$template->set_global("page", $page);
|
2009-11-25 13:22:24 -08:00
|
|
|
$template->set_global("page_title", null);
|
2009-11-14 14:25:39 -08:00
|
|
|
$template->set_global("max_pages", $max_pages);
|
2009-09-02 21:29:22 -07:00
|
|
|
$template->set_global("page_size", $page_size);
|
|
|
|
|
$template->set_global("item", $album);
|
|
|
|
|
$template->set_global("children", $album->viewable()->children($page_size, $offset));
|
|
|
|
|
$template->set_global("children_count", $children_count);
|
|
|
|
|
$template->set_global("parents", $album->parents());
|
|
|
|
|
$template->content = new View("album.html");
|
|
|
|
|
|
|
|
|
|
// We can't use math in ORM or the query builder, so do this by hand. It's important
|
|
|
|
|
// that we do this with math, otherwise concurrent accesses will damage accuracy.
|
2010-06-11 08:15:25 -07:00
|
|
|
db::query("UPDATE {items} SET `view_count` = `view_count` + 1 WHERE `id` = $album->id")
|
|
|
|
|
->execute();
|
2009-09-02 21:29:22 -07:00
|
|
|
|
|
|
|
|
print $template;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-25 12:41:01 -08:00
|
|
|
public function create($parent_id) {
|
2009-09-02 21:29:22 -07:00
|
|
|
access::verify_csrf();
|
2009-11-25 12:41:01 -08:00
|
|
|
$album = ORM::factory("item", $parent_id);
|
2009-09-02 21:29:22 -07:00
|
|
|
access::required("view", $album);
|
|
|
|
|
access::required("add", $album);
|
|
|
|
|
|
|
|
|
|
$form = album::get_add_form($album);
|
2010-01-15 12:15:20 -08:00
|
|
|
try {
|
|
|
|
|
$valid = $form->validate();
|
|
|
|
|
$album = ORM::factory("item");
|
|
|
|
|
$album->type = "album";
|
|
|
|
|
$album->parent_id = $parent_id;
|
|
|
|
|
$album->name = $form->add_album->inputs["name"]->value;
|
|
|
|
|
$album->title = $form->add_album->title->value ?
|
|
|
|
|
$form->add_album->title->value : $form->add_album->inputs["name"]->value;
|
|
|
|
|
$album->description = $form->add_album->description->value;
|
|
|
|
|
$album->slug = $form->add_album->slug->value;
|
|
|
|
|
$album->validate();
|
|
|
|
|
} catch (ORM_Validation_Exception $e) {
|
|
|
|
|
// Translate ORM validation errors into form error messages
|
|
|
|
|
foreach ($e->validation->errors() as $key => $error) {
|
|
|
|
|
$form->add_album->inputs[$key]->add_error($error, 1);
|
|
|
|
|
}
|
|
|
|
|
$valid = false;
|
|
|
|
|
}
|
2009-09-02 21:29:22 -07:00
|
|
|
|
2010-01-15 12:15:20 -08:00
|
|
|
if ($valid) {
|
|
|
|
|
$album->save();
|
2010-04-17 12:10:01 -07:00
|
|
|
module::event("album_add_form_completed", $album, $form);
|
2009-09-02 21:29:22 -07:00
|
|
|
log::success("content", "Created an album",
|
2010-01-15 12:15:20 -08:00
|
|
|
html::anchor("albums/$album->id", "view album"));
|
2009-09-02 21:29:22 -07:00
|
|
|
message::success(t("Created album %album_title",
|
2010-01-15 12:15:20 -08:00
|
|
|
array("album_title" => html::purify($album->title))));
|
2008-12-25 05:12:46 +00:00
|
|
|
|
2010-01-15 12:15:20 -08:00
|
|
|
print json_encode(array("result" => "success", "location" => $album->url()));
|
2008-12-24 00:20:26 +00:00
|
|
|
} else {
|
2010-01-15 12:15:20 -08:00
|
|
|
print json_encode(array("result" => "error", "form" => (string) $form));
|
2008-12-24 00:20:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-25 12:41:01 -08:00
|
|
|
public function update($album_id) {
|
2009-06-01 22:40:22 -07:00
|
|
|
access::verify_csrf();
|
2009-11-25 12:41:01 -08:00
|
|
|
$album = ORM::factory("item", $album_id);
|
2009-06-01 22:40:22 -07:00
|
|
|
access::required("view", $album);
|
2008-12-24 00:20:26 +00:00
|
|
|
access::required("edit", $album);
|
|
|
|
|
|
2009-07-28 20:40:28 +08:00
|
|
|
$form = album::get_edit_form($album);
|
2010-01-14 21:04:09 -08:00
|
|
|
try {
|
|
|
|
|
$valid = $form->validate();
|
2009-07-20 08:51:12 -07:00
|
|
|
$album->title = $form->edit_item->title->value;
|
|
|
|
|
$album->description = $form->edit_item->description->value;
|
|
|
|
|
$album->sort_column = $form->edit_item->sort_order->column->value;
|
|
|
|
|
$album->sort_order = $form->edit_item->sort_order->direction->value;
|
2010-01-19 19:24:46 -08:00
|
|
|
$album->name = $form->edit_item->inputs["name"]->value;
|
2009-09-07 21:09:52 -07:00
|
|
|
$album->slug = $form->edit_item->slug->value;
|
2010-01-14 21:04:09 -08:00
|
|
|
$album->validate();
|
|
|
|
|
} catch (ORM_Validation_Exception $e) {
|
|
|
|
|
// Translate ORM validation errors into form error messages
|
|
|
|
|
foreach ($e->validation->errors() as $key => $error) {
|
|
|
|
|
$form->edit_item->inputs[$key]->add_error($error, 1);
|
|
|
|
|
}
|
|
|
|
|
$valid = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($valid) {
|
2008-12-24 00:20:26 +00:00
|
|
|
$album->save();
|
2009-07-20 08:51:12 -07:00
|
|
|
module::event("item_edit_form_completed", $album, $form);
|
2009-07-19 17:02:20 +08:00
|
|
|
|
2008-12-25 23:43:44 +00:00
|
|
|
log::success("content", "Updated album", "<a href=\"albums/$album->id\">view</a>");
|
2009-08-31 02:12:01 -07:00
|
|
|
message::success(t("Saved album %album_title",
|
|
|
|
|
array("album_title" => html::purify($album->title))));
|
2008-12-25 05:12:46 +00:00
|
|
|
|
2009-12-31 17:21:19 -08:00
|
|
|
if ($form->from_id->value == $album->id) {
|
|
|
|
|
// Use the new url; it might have changed.
|
|
|
|
|
print json_encode(array("result" => "success", "location" => $album->url()));
|
|
|
|
|
} else {
|
|
|
|
|
// Stay on the same page
|
|
|
|
|
print json_encode(array("result" => "success"));
|
|
|
|
|
}
|
2008-12-24 00:20:26 +00:00
|
|
|
} else {
|
2010-01-14 21:04:09 -08:00
|
|
|
print json_encode(array("result" => "error", "form" => (string) $form));
|
2008-12-24 00:20:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-25 12:41:01 -08:00
|
|
|
public function form_add($album_id) {
|
2008-12-24 00:20:26 +00:00
|
|
|
$album = ORM::factory("item", $album_id);
|
2009-06-01 22:40:22 -07:00
|
|
|
access::required("view", $album);
|
2009-05-13 18:03:50 +00:00
|
|
|
access::required("add", $album);
|
2008-12-24 00:20:26 +00:00
|
|
|
|
2009-11-25 12:41:01 -08:00
|
|
|
print album::get_add_form($album);
|
2008-12-02 21:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
2009-11-25 12:41:01 -08:00
|
|
|
public function form_edit($album_id) {
|
|
|
|
|
$album = ORM::factory("item", $album_id);
|
2009-06-01 22:40:22 -07:00
|
|
|
access::required("view", $album);
|
2008-12-24 00:20:26 +00:00
|
|
|
access::required("edit", $album);
|
|
|
|
|
|
|
|
|
|
print album::get_edit_form($album);
|
|
|
|
|
}
|
2008-11-03 05:55:34 +00:00
|
|
|
}
|