2008-11-05 07:42:52 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
|
/**
|
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
2009-05-13 20:04:58 +00:00
|
|
|
* Copyright (C) 2000-2009 Bharat Mediratta
|
2008-11-05 07:42:52 +00: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.
|
|
|
|
|
*/
|
2008-11-19 00:12:25 +00:00
|
|
|
class Photos_Controller extends Items_Controller {
|
2008-11-11 06:18:45 +00:00
|
|
|
|
2008-11-16 07:14:12 +00:00
|
|
|
/**
|
2008-12-25 02:38:53 +00:00
|
|
|
* @see REST_Controller::_show($resource)
|
2008-11-16 07:14:12 +00:00
|
|
|
*/
|
2008-12-24 00:20:26 +00:00
|
|
|
public function _show($photo) {
|
|
|
|
|
access::required("view", $photo);
|
2008-12-09 10:14:09 +00:00
|
|
|
|
2009-11-04 09:50:49 -08:00
|
|
|
$where = array("type != " => "album");
|
|
|
|
|
$position = $photo->parent()->get_position($photo, $where);
|
2009-06-04 22:11:08 -07:00
|
|
|
if ($position > 1) {
|
|
|
|
|
list ($previous_item, $ignore, $next_item) =
|
2009-11-04 09:50:49 -08:00
|
|
|
$photo->parent()->children(3, $position - 2, $where);
|
2009-06-04 22:11:08 -07:00
|
|
|
} else {
|
|
|
|
|
$previous_item = null;
|
2009-11-04 09:50:49 -08:00
|
|
|
list ($next_item) = $photo->parent()->viewable()->children(1, $position, $where);
|
2009-06-04 22:11:08 -07:00
|
|
|
}
|
2008-12-31 07:06:10 +00:00
|
|
|
|
2009-11-17 13:42:51 -08:00
|
|
|
$template = new Theme_View("page.html", "item", "photo");
|
2008-12-31 06:44:38 +00:00
|
|
|
$template->set_global("item", $photo);
|
|
|
|
|
$template->set_global("children", array());
|
2009-09-21 21:22:07 -07:00
|
|
|
$template->set_global("children_count", 0);
|
2008-12-31 06:44:38 +00:00
|
|
|
$template->set_global("parents", $photo->parents());
|
2009-06-04 22:11:08 -07:00
|
|
|
$template->set_global("next_item", $next_item);
|
|
|
|
|
$template->set_global("previous_item", $previous_item);
|
2009-11-04 09:50:49 -08:00
|
|
|
$template->set_global("sibling_count", $photo->parent()->viewable()->children_count($where));
|
2008-12-31 07:06:10 +00:00
|
|
|
$template->set_global("position", $position);
|
2008-11-27 16:19:07 +00:00
|
|
|
|
2008-11-09 19:20:23 +00:00
|
|
|
$template->content = new View("photo.html");
|
2008-12-20 20:04:57 +00:00
|
|
|
|
2008-12-24 00:20:26 +00:00
|
|
|
$photo->view_count++;
|
|
|
|
|
$photo->save();
|
2008-12-20 20:11:51 +00:00
|
|
|
|
|
|
|
|
print $template;
|
2008-11-05 07:42:52 +00:00
|
|
|
}
|
2008-12-06 21:14:36 +00:00
|
|
|
|
2009-06-01 22:40:22 -07:00
|
|
|
|
2008-12-06 21:14:36 +00:00
|
|
|
/**
|
2008-12-25 02:38:53 +00:00
|
|
|
* @see REST_Controller::_update($resource)
|
2008-12-06 21:14:36 +00:00
|
|
|
*/
|
2008-12-24 00:20:26 +00:00
|
|
|
public function _update($photo) {
|
2009-06-01 22:40:22 -07:00
|
|
|
access::verify_csrf();
|
|
|
|
|
access::required("view", $photo);
|
2008-12-24 00:20:26 +00:00
|
|
|
access::required("edit", $photo);
|
|
|
|
|
|
2009-07-28 20:40:28 +08:00
|
|
|
$form = photo::get_edit_form($photo);
|
2009-09-07 21:01:51 -07:00
|
|
|
$valid = $form->validate();
|
2009-10-13 10:36:50 -07:00
|
|
|
|
|
|
|
|
if ($valid) {
|
|
|
|
|
$new_ext = pathinfo($form->edit_item->filename->value, PATHINFO_EXTENSION);
|
|
|
|
|
$old_ext = pathinfo($photo->name, PATHINFO_EXTENSION);
|
|
|
|
|
if (strcasecmp($new_ext, $old_ext)) {
|
|
|
|
|
$form->edit_item->filename->add_error("illegal_extension", 1);
|
|
|
|
|
$valid = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($valid) {
|
2009-09-07 21:01:51 -07:00
|
|
|
if ($form->edit_item->filename->value != $photo->name ||
|
|
|
|
|
$form->edit_item->slug->value != $photo->slug) {
|
|
|
|
|
// Make sure that there's not a name or slug conflict
|
2009-09-07 21:09:52 -07:00
|
|
|
if ($row = Database::instance()
|
|
|
|
|
->select(array("name", "slug"))
|
|
|
|
|
->from("items")
|
|
|
|
|
->where("parent_id", $photo->parent_id)
|
|
|
|
|
->where("id <>", $photo->id)
|
|
|
|
|
->open_paren()
|
|
|
|
|
->where("name", $form->edit_item->filename->value)
|
|
|
|
|
->orwhere("slug", $form->edit_item->slug->value)
|
|
|
|
|
->close_paren()
|
|
|
|
|
->get()
|
|
|
|
|
->current()) {
|
|
|
|
|
if ($row->name == $form->edit_item->filename->value) {
|
|
|
|
|
$form->edit_item->filename->add_error("name_conflict", 1);
|
|
|
|
|
}
|
|
|
|
|
if ($row->slug == $form->edit_item->slug->value) {
|
|
|
|
|
$form->edit_item->slug->add_error("slug_conflict", 1);
|
|
|
|
|
}
|
|
|
|
|
$valid = false;
|
2009-05-16 04:32:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($valid) {
|
2009-10-22 10:09:25 -07:00
|
|
|
$watching_album = $photo->url() != ($location = parse_url(request::referrer(), PHP_URL_PATH));
|
|
|
|
|
|
2009-07-20 08:51:12 -07:00
|
|
|
$photo->title = $form->edit_item->title->value;
|
|
|
|
|
$photo->description = $form->edit_item->description->value;
|
2009-09-07 21:09:52 -07:00
|
|
|
$photo->slug = $form->edit_item->slug->value;
|
2009-07-20 08:51:12 -07:00
|
|
|
$photo->rename($form->edit_item->filename->value);
|
2008-12-24 00:20:26 +00:00
|
|
|
$photo->save();
|
2009-07-20 08:51:12 -07:00
|
|
|
module::event("item_edit_form_completed", $photo, $form);
|
2009-07-19 17:02:20 +08:00
|
|
|
|
2009-09-08 13:44:52 -07:00
|
|
|
log::success("content", "Updated photo", "<a href=\"{$photo->url()}\">view</a>");
|
2009-07-01 17:57:39 -07:00
|
|
|
message::success(
|
2009-08-31 02:12:01 -07:00
|
|
|
t("Saved photo %photo_title",
|
|
|
|
|
array("photo_title" => html::purify($photo->title))));
|
2008-12-24 00:20:26 +00:00
|
|
|
|
2008-12-25 05:12:46 +00:00
|
|
|
print json_encode(
|
2009-10-22 07:37:14 -07:00
|
|
|
array("result" => "success",
|
2009-10-22 10:09:25 -07:00
|
|
|
"location" => $watching_album ? $location : $photo->url()));
|
2008-12-24 00:20:26 +00:00
|
|
|
} else {
|
2008-12-25 05:12:46 +00:00
|
|
|
print json_encode(
|
|
|
|
|
array("result" => "error",
|
2009-07-28 20:40:28 +08:00
|
|
|
"form" => $form->__toString()));
|
2008-12-24 00:20:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-12-25 02:38:53 +00:00
|
|
|
* @see REST_Controller::_form_edit($resource)
|
2008-12-24 00:20:26 +00:00
|
|
|
*/
|
|
|
|
|
public function _form_edit($photo) {
|
2009-06-01 22:40:22 -07:00
|
|
|
access::required("view", $photo);
|
2008-12-24 00:20:26 +00:00
|
|
|
access::required("edit", $photo);
|
2009-06-01 22:40:22 -07:00
|
|
|
|
2008-12-24 00:20:26 +00:00
|
|
|
print photo::get_edit_form($photo);
|
2008-12-06 21:14:36 +00:00
|
|
|
}
|
2008-11-05 07:42:52 +00:00
|
|
|
}
|