summaryrefslogtreecommitdiff
path: root/modules/comment/controllers/comment.php
blob: 67a4897bec991b6e1a8a1e01ce6ef7c70381271a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2008 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 Comment_Controller extends REST_Controller {
  protected $resource_type = "comment";

  /**
   * Return the form for adding comments.
   */
  public function _get_form($comment) {
    $form = new Forge(url::current(true), "", "post", array("id" => "gComment"));
    $group = $form->group(_("Add Comment"));
    $group->input("author")
      ->label(_("Author"))
      ->id("gAuthor")
      ->class(null)
      ->value($comment->author);
    $group->input("email")
      ->label(_("Email"))
      ->id("gEmail")
      ->class(null)
      ->value($comment->email);
    $group->textarea("text")
      ->label(_("Text"))
      ->id("gText")
      ->class(null)
      ->value($comment->text);
    $group->hidden("item_id")
      ->value($comment->item_id);
    $group->submit(_("Add"));

    $this->_add_validation_rules(ORM::factory("comment")->validation_rules, $form);

    return $form;
  }

  /**
   * @todo Refactor this into a more generic location
   */
  private function _add_validation_rules($rules, $form) {
    foreach ($form->inputs as $name => $input) {
      if (isset($input->inputs)) {
        $this->_add_validation_rules($rules, $input);
      }
      if (isset($rules[$name])) {
        $input->rules($rules[$name]);
      }
    }
  }

  public function add($item_id) {
    $comment = ORM::factory('comment');
    $comment->item_id = $item_id;

    $form = $this->_get_form($comment);
    if ($form->validate()) {
      $comment = ORM::factory('comment');
      $comment->author = $this->input->post('author');
      $comment->email = $this->input->post('email');
      $comment->text = $this->input->post('text');
      $comment->datetime = time();
      $comment->item_id = $this->input->post('item_id');
      $comment->save();

      $data = array('valid' => true, 'html' => sprintf(comment::show_comment_list($item_id)));
    } else {
      $data = array('valid' => false, 'html' => sprintf($form->render("form.html")));
    }

    if (request::method() == "get") {
      print $data['html'];
    } else if (request::method() == "post") {
      print json_encode($data);
    }
  }

  public function get_item_comments($item_id) {
    print comment::show_comment_list($item_id);
  }

  /**
   * Get an existing comment.
   *  @see Rest_Controller::_get($resource)
   */
  public function _get($user) {
    throw new Exception("@todo Comment_Controller::_get NOT IMPLEMENTED");
  }

  /**
   * Update existing comment.
   *  @see Rest_Controller::_put($resource)
   */
  public function _put($resource) {
    throw new Exception("@todo Comment_Controller::_put NOT IMPLEMENTED");
  }

  /**
   *  @see Rest_Controller::_post($resource)
   */
  public function _post($user) {
    throw new Exception("@todo Comment_Controller::_post NOT IMPLEMENTED");
  }

  /**
   * Delete existing comment.
   *  @see Rest_Controller::_delete($resource)
   */
  public function _delete($resource) {
    throw new Exception("@todo Comment_Controller::_delete NOT IMPLEMENTED");
  }
}