summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorJozef Selesi <jozefs@users.sourceforge.net>2008-11-15 16:52:23 +0000
committerJozef Selesi <jozefs@users.sourceforge.net>2008-11-15 16:52:23 +0000
commitf9eaa8c220630578664fb6b46d082fce42726ca5 (patch)
treea9bac48a58c68d575ef46eed149d9daf4f6a0093 /modules
parent66bd9d85b3820df8e4393019754d6a11294d4d4e (diff)
Initial add comment implementation.
Diffstat (limited to 'modules')
-rw-r--r--modules/comment/controllers/comment.php100
-rw-r--r--modules/comment/models/comment.php4
-rw-r--r--modules/comment/views/comment_form.html.php61
-rw-r--r--modules/comment/views/comment_list.html.php26
-rw-r--r--modules/comment/views/show_comments.html.php4
5 files changed, 156 insertions, 39 deletions
diff --git a/modules/comment/controllers/comment.php b/modules/comment/controllers/comment.php
index dd2b44f9..fab18498 100644
--- a/modules/comment/controllers/comment.php
+++ b/modules/comment/controllers/comment.php
@@ -17,6 +17,104 @@
* 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 Controller {
+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();
+ } else {
+ print $form->render("form.html");
+ }
+ }
+
+ public function get_item_comments($item_id) {
+ $v = comment::show_comment_list($item_id);
+ print $v;
+ }
+
+ /**
+ * 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");
+ }
}
diff --git a/modules/comment/models/comment.php b/modules/comment/models/comment.php
index 4db09a5b..2ed2b83c 100644
--- a/modules/comment/models/comment.php
+++ b/modules/comment/models/comment.php
@@ -18,4 +18,8 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Comment_Model extends ORM {
+ var $validation_rules = array(
+ "author" => "required",
+ "email" => "required|valid_email",
+ "text" => "required");
}
diff --git a/modules/comment/views/comment_form.html.php b/modules/comment/views/comment_form.html.php
index 1561035f..cc2c4028 100644
--- a/modules/comment/views/comment_form.html.php
+++ b/modules/comment/views/comment_form.html.php
@@ -1,25 +1,40 @@
<? defined("SYSPATH") or die("No direct script access."); ?>
-<form id="gCommentAdd">
- <fieldset>
- <legend>Add comment</legend>
- <ul>
- <li>
- <label for="gCommentAuthor"><?= _("Your Name") ?></label>
- <input type="text" name="author" id="gCommentAuthor" />
- </li>
- <li>
- <label for="gCommentEmail"><?= _("Your Email (not displayed)") ?></label>
- <input type="text" name="email" id="gCommentEmail" />
- </li>
- <li>
- <label for="gCommentText"><?= _("Comment") ?></label>
- <textarea name="text" id="gCommentText"></textarea>
- </li>
- <li>
- <input type="hidden" id="gItemId" name="item_id" value="<?= $item_id ?>" />
- <input type="submit" id="gCommentSubmit" value="<?= _("Add") ?>" />
- </li>
- </ul>
- </fieldset>
-</form>
+<script type="text/javascript">
+ // <![CDATA[
+function show_comment_add_form(url) {
+ $("#gCommentAddLink").hide();
+ $.get(url, function(data) {
+ $("#gAddCommentFormContainer").html(data);
+ ajaxify_comment_add_form();
+ });
+}
+
+function ajaxify_comment_add_form() {
+ $("#gLoginMenu form ul").addClass("gInline");
+ $("form#gComment").ajaxForm({
+ target: "#gAddCommentFormContainer",
+ success: function(responseText, statusText) {
+ if (!responseText) {
+ reload_comments();
+ $("#gCommentAddLink").show();
+ } else {
+ ajaxify_comment_add_form();
+ }
+ },
+ });
+}
+
+function reload_comments() {
+ $.get("<?= url::site("photo/{$item_id}/comments") ?>", function(data) {
+ $("#gCommentThread").html(data);
+ });
+}
+ // ]]>
+</script>
+<span id="gCommentAddLink">
+ <a href="javascript:show_comment_add_form('<?= url::site("photo/{$item_id}/comments/add") ?>')">
+ <?= _("Add Comment") ?>
+ </a>
+</span>
+<div id="gAddCommentFormContainer"></div>
diff --git a/modules/comment/views/comment_list.html.php b/modules/comment/views/comment_list.html.php
index ea824597..81c5691e 100644
--- a/modules/comment/views/comment_list.html.php
+++ b/modules/comment/views/comment_list.html.php
@@ -1,15 +1,13 @@
<? defined("SYSPATH") or die("No direct script access."); ?>
-<ul id="gCommentThread">
- <? foreach (array_reverse($comments) as $index => $comment): ?>
- <li id="gComment-<?= $index; ?>" class="gComment <?= $index % 2 ? 'gOdd' : 'gEven' ?>">
- <p>
- <a href="#" class="gAuthor"><?= $comment->author ?></a>
- <?= comment::format_elapsed_time($comment->datetime) ?>,
- <span class="gUnderstate"><?= strftime('%c', $comment->datetime) ?></span>
- </p>
- <div>
- <?= $comment->text ?>
- </div>
- </li>
- <? endforeach; ?>
-</ul>
+<? foreach (array_reverse($comments) as $index => $comment): ?>
+<li id="gComment-<?= $index; ?>" class="gComment <?= $index % 2 ? 'gOdd' : 'gEven' ?>">
+ <p>
+ <a href="#" class="gAuthor"><?= $comment->author ?></a>
+ <?= comment::format_elapsed_time($comment->datetime) ?>,
+ <span class="gUnderstate"><?= strftime('%c', $comment->datetime) ?></span>
+ </p>
+ <div>
+ <?= $comment->text ?>
+ </div>
+</li>
+<? endforeach; ?>
diff --git a/modules/comment/views/show_comments.html.php b/modules/comment/views/show_comments.html.php
index 5ba45883..35b2c67a 100644
--- a/modules/comment/views/show_comments.html.php
+++ b/modules/comment/views/show_comments.html.php
@@ -2,7 +2,9 @@
<div id="gComments">
<? if ($comment_list): ?>
<h2><?= _("Comments") ?></h2>
- <?= $comment_list ?>
+ <ul id="gCommentThread">
+ <?= $comment_list ?>
+ </ul>
<? endif ?>
<?= $comment_form ?>