2008-11-16 07:14:12 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
|
/**
|
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
2013-01-21 01:22:01 -05:00
|
|
|
* Copyright (C) 2000-2013 Bharat Mediratta
|
2008-11-16 07:14:12 +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.
|
|
|
|
|
*/
|
2009-11-25 08:05:21 -08:00
|
|
|
class Comments_Controller extends Controller {
|
2008-11-16 07:14:12 +00:00
|
|
|
/**
|
2008-11-18 08:28:32 +00:00
|
|
|
* Add a new comment to the collection.
|
2008-11-16 07:14:12 +00:00
|
|
|
*/
|
2009-11-25 08:05:21 -08:00
|
|
|
public function create($id) {
|
|
|
|
|
$item = ORM::factory("item", $id);
|
2008-12-25 05:12:46 +00:00
|
|
|
access::required("view", $item);
|
2010-06-05 23:35:32 -07:00
|
|
|
if (!comment::can_comment()) {
|
|
|
|
|
access::forbidden();
|
|
|
|
|
}
|
2008-12-25 00:47:40 +00:00
|
|
|
|
2008-12-25 05:12:46 +00:00
|
|
|
$form = comment::get_add_form($item);
|
2010-01-16 22:27:07 -08:00
|
|
|
try {
|
|
|
|
|
$valid = $form->validate();
|
|
|
|
|
$comment = ORM::factory("comment");
|
|
|
|
|
$comment->item_id = $id;
|
|
|
|
|
$comment->author_id = identity::active_user()->id;
|
|
|
|
|
$comment->text = $form->add_comment->text->value;
|
|
|
|
|
$comment->guest_name = $form->add_comment->inputs["name"]->value;
|
|
|
|
|
$comment->guest_email = $form->add_comment->email->value;
|
|
|
|
|
$comment->guest_url = $form->add_comment->url->value;
|
|
|
|
|
$comment->validate();
|
|
|
|
|
} catch (ORM_Validation_Exception $e) {
|
|
|
|
|
// Translate ORM validation errors into form error messages
|
|
|
|
|
foreach ($e->validation->errors() as $key => $error) {
|
|
|
|
|
switch ($key) {
|
|
|
|
|
case "guest_name": $key = "name"; break;
|
|
|
|
|
case "guest_email": $key = "email"; break;
|
2010-09-16 14:19:28 -07:00
|
|
|
case "guest_url": $key = "url"; break;
|
2010-01-16 22:27:07 -08:00
|
|
|
}
|
|
|
|
|
$form->add_comment->inputs[$key]->add_error($error, 1);
|
2009-01-16 04:06:03 +00:00
|
|
|
}
|
2010-01-16 22:27:07 -08:00
|
|
|
$valid = false;
|
2009-01-16 04:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($valid) {
|
2010-01-16 22:27:07 -08:00
|
|
|
$comment->save();
|
2009-11-17 13:42:51 -08:00
|
|
|
$view = new Theme_View("comment.html", "other", "comment-fragment");
|
2008-12-24 04:05:47 +00:00
|
|
|
$view->comment = $comment;
|
2008-12-06 06:10:15 +00:00
|
|
|
|
2010-07-21 21:30:13 -07:00
|
|
|
json::reply(array("result" => "success",
|
2010-08-08 15:05:55 -07:00
|
|
|
"view" => (string)$view,
|
|
|
|
|
"form" => (string)comment::get_add_form($item)));
|
2008-12-25 00:47:40 +00:00
|
|
|
} else {
|
2010-05-14 16:19:53 -07:00
|
|
|
$form = comment::prefill_add_form($form);
|
2010-07-31 21:16:17 -07:00
|
|
|
json::reply(array("result" => "error", "form" => (string)$form));
|
2008-11-16 07:14:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-18 08:28:32 +00:00
|
|
|
/**
|
|
|
|
|
* Present a form for adding a new comment to this item or editing an existing comment.
|
|
|
|
|
*/
|
2009-11-25 08:05:21 -08:00
|
|
|
public function form_add($item_id) {
|
2008-12-25 05:12:46 +00:00
|
|
|
$item = ORM::factory("item", $item_id);
|
|
|
|
|
access::required("view", $item);
|
2010-06-05 23:35:32 -07:00
|
|
|
if (!comment::can_comment()) {
|
|
|
|
|
access::forbidden();
|
|
|
|
|
}
|
2008-12-25 05:12:46 +00:00
|
|
|
|
2010-05-14 16:19:53 -07:00
|
|
|
print comment::prefill_add_form(comment::get_add_form($item));
|
2008-11-18 23:40:47 +00:00
|
|
|
}
|
2008-11-16 07:14:12 +00:00
|
|
|
}
|