dwing

annotate inc/comment.class.php @ 186:cfd4d1dcece4

theoretically support OpenID AX with both axschema and schema.openid.net, thank god that no provider has support for this yet somehow
author Arpad Borsos <arpad.borsos@googlemail.com>
date Thu Aug 05 12:04:49 2010 +0200 (21 months ago)
parents 2df784358c9f
children
rev   line source
arpad@0 1 <?php
arpad@0 2 /*
arpad@76 3 * dWing - a cms aimed to be as bleeding edge as possible
arpad@76 4 * Copyright (C) 2007-2008 Arpad Borsos <arpad.borsos@googlemail.com>
arpad@0 5 *
arpad@76 6 * This program is free software: you can redistribute it and/or modify
arpad@0 7 * it under the terms of the GNU General Public License as published by
arpad@76 8 * the Free Software Foundation, either version 3 of the License, or
arpad@76 9 * (at your option) any later version.
arpad@76 10 *
arpad@76 11 * This program is distributed in the hope that it will be useful,
arpad@76 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
arpad@76 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
arpad@76 14 * GNU General Public License for more details.
arpad@76 15 *
arpad@0 16 * You should have received a copy of the GNU General Public License
arpad@76 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
arpad@0 18 */
arpad@110 19
arpad@110 20 /*
arpad@110 21 * Comment Object
arpad@110 22 */
arpad@174 23 class Comment extends ActiveItem implements ContentItem, ContentProvider
arpad@169 24 {
arpad@169 25 public static function ContentType()
arpad@169 26 {
arpad@169 27 return 4;
arpad@169 28 }
arpad@110 29
arpad@110 30 protected $tableName = 'comments';
arpad@110 31 protected $primaryKey = 'comment_id';
arpad@116 32 protected $definition = array('text' => 'html', 'user_id' => 'user',
arpad@121 33 'time' => 'time', 'content_id' => 'required', 'content_type' => 'required');
arpad@121 34
arpad@121 35 public function __construct($obj = null)
arpad@121 36 {
arpad@172 37 if(is_object($obj) && $obj instanceof ContentItem)
arpad@121 38 {
arpad@121 39 $this->data['content_id'] = $obj->id;
arpad@172 40 $this->data['content_type'] = $obj->ContentType();
arpad@121 41 }
arpad@121 42 parent::__construct($obj);
arpad@121 43 }
arpad@173 44
arpad@173 45 /**
arpad@173 46 * ContentProvider Interface
arpad@173 47 */
arpad@173 48 public static function addAllFor(ContentItem $aItem, $aItems, $aUseTransaction = false)
arpad@173 49 {
arpad@173 50 // this does not apply for Comment
arpad@173 51 return false;
arpad@173 52 }
arpad@173 53 public static function getAllFor(ContentItem $aItem)
arpad@173 54 {
arpad@173 55 return new CommentIterator($aItem);
arpad@173 56 }
arpad@173 57 public static function deleteAllFor(ContentItem $aItem, $aUseTransaction = false)
arpad@173 58 {
arpad@173 59 if($aUseTransaction)
arpad@175 60 Core::$db->beginTransaction();
arpad@173 61 $all = new CommentIterator($aItem);
arpad@173 62 $all->delete();
arpad@173 63 if($aUseTransaction)
arpad@173 64 Core::$db->commit();
arpad@173 65 return true;
arpad@173 66 }
arpad@110 67 }
arpad@110 68
arpad@110 69 /*
arpad@110 70 * Comment Iterator for a Content Item
arpad@110 71 */
arpad@110 72 class CommentIterator implements Iterator, Countable
arpad@110 73 {
arpad@110 74 protected $elements = null;
arpad@110 75 protected $position = 0;
arpad@110 76
arpad@110 77 protected $contentId;
arpad@110 78 protected $contentType;
arpad@110 79
arpad@110 80 private static $countStmt;
arpad@110 81 private static $selectStmt;
arpad@110 82
arpad@172 83 public function __construct(ContentItem $aItem)
arpad@110 84 {
arpad@110 85 if(empty(self::$selectStmt))
arpad@110 86 {
arpad@110 87 self::$selectStmt = Core::$db->prepare(
arpad@159 88 'SELECT comments.* FROM '.Core::$prefix.'comments AS comments
arpad@110 89 WHERE content_id=:contentId AND content_type=:contentType
arpad@110 90 ORDER BY time ASC;');
arpad@110 91 }
arpad@110 92 if(empty(self::$countStmt))
arpad@110 93 {
arpad@110 94 self::$countStmt = Core::$db->prepare('
arpad@159 95 SELECT COUNT(*) as commentnum FROM '.Core::$prefix.'comments
arpad@110 96 WHERE content_id=:contentId AND content_type=:contentType;');
arpad@110 97 }
arpad@172 98 $this->contentId = (int)$aItem->id;
arpad@172 99 $this->contentType = (int)$aItem->ContentType();
arpad@110 100 }
arpad@110 101 protected function lazyFetch()
arpad@110 102 {
arpad@110 103 if($this->elements != null)
arpad@110 104 return;
arpad@110 105 $statement = self::$selectStmt;
arpad@110 106 $statement->bindValue(':contentId', $this->contentId, PDO::PARAM_INT);
arpad@110 107 $statement->bindValue(':contentType', $this->contentType, PDO::PARAM_INT);
arpad@110 108 $statement->execute();
arpad@110 109 $statement->setFetchMode(PDO::FETCH_CLASS, 'Comment');
arpad@110 110 $this->elements = $statement->fetchAll();
arpad@110 111 }
arpad@128 112 public function delete()
arpad@128 113 {
arpad@128 114 $this->lazyFetch();
arpad@128 115 foreach($this->elements as $element)
arpad@128 116 {
arpad@128 117 // the CRUD object can delete all the associated subobjects
arpad@128 118 $element->delete();
arpad@128 119 }
arpad@128 120 return true;
arpad@128 121 }
arpad@110 122
arpad@110 123 // Countable Interface:
arpad@110 124 public function count()
arpad@110 125 {
arpad@110 126 $statement = self::$countStmt;
arpad@110 127 $statement->bindValue(':contentId', $this->contentId, PDO::PARAM_INT);
arpad@110 128 $statement->bindValue(':contentType', $this->contentType, PDO::PARAM_INT);
arpad@61 129 $statement->execute();
arpad@61 130 return $statement->fetchColumn();
arpad@110 131 }
arpad@110 132
arpad@110 133 // Iterator Interface:
arpad@110 134 public function current()
arpad@110 135 {
arpad@110 136 $this->lazyFetch();
arpad@110 137 return $this->elements[$this->position];
arpad@110 138 }
arpad@110 139 public function key()
arpad@110 140 {
arpad@110 141 $this->lazyFetch();
arpad@110 142 return $this->elements[$this->position]->id;
arpad@110 143 }
arpad@110 144 public function next()
arpad@110 145 {
arpad@110 146 ++$this->position;
arpad@110 147 }
arpad@110 148 public function rewind()
arpad@110 149 {
arpad@110 150 $this->position = 0;
arpad@110 151 }
arpad@110 152 public function valid()
arpad@110 153 {
arpad@110 154 $this->lazyFetch();
arpad@110 155 return isset($this->elements[$this->position]);
arpad@110 156 }
arpad@118 157 }
arpad@118 158
arpad@118 159 class CommentDispatcher extends REST
arpad@118 160 {
arpad@167 161 public static function doGET(RESTDispatcher $dispatcher)
arpad@122 162 {
arpad@122 163 $current = $dispatcher->current();
arpad@122 164 if(empty($current['id']))
arpad@122 165 throw new NotImplementedException(); // listing not implemented
arpad@122 166 $obj = new $current['resource']($current['id']);
arpad@122 167 $dispatcher->assignObject($obj);
arpad@122 168
arpad@122 169 $child = $dispatcher->next();
arpad@122 170 if(!$child)
arpad@122 171 return $obj;
arpad@122 172 else
arpad@122 173 return $dispatcher->dispatch();
arpad@122 174 }
arpad@167 175 public static function doPOST(RESTDispatcher $dispatcher)
arpad@118 176 {
arpad@118 177 $current = $dispatcher->current();
arpad@118 178 $child = $dispatcher->next();
arpad@122 179 if($child)
arpad@118 180 {
arpad@118 181 $obj = new Comment($current['id']);
arpad@122 182 $dispatcher->previous();
arpad@122 183 $dispatcher->assignObject($obj);
arpad@122 184 $dispatcher->next(); // assign the object to the right resource
arpad@122 185 return $dispatcher->dispatch();
arpad@118 186 }
arpad@121 187 $parent = $dispatcher->previous();
arpad@122 188 if(!$parent)
arpad@122 189 throw new NotImplementedException();
arpad@129 190 if(!Core::$user->authed)
arpad@129 191 throw new UnauthorizedException();
arpad@122 192
arpad@122 193 $obj = new Comment($parent['obj']); // so the Comment has info about the
arpad@122 194 // parent Id and ContentType
arpad@167 195 $obj->assignData($dispatcher->getJSON());
arpad@122 196 $obj->save();
arpad@125 197 $dispatcher->next(); // dispatcher has the right resource
arpad@122 198 return $obj;
arpad@118 199 }
arpad@167 200 public static function doPUT(RESTDispatcher $dispatcher)
arpad@134 201 {
arpad@134 202 // TODO: implement
arpad@134 203 throw new NotImplementedException();
arpad@134 204 }
arpad@167 205 public static function doDELETE(RESTDispatcher $dispatcher)
arpad@128 206 {
arpad@129 207 if(!($child = $dispatcher->next()))
arpad@129 208 if(!Core::$user->hasRight('news'))
arpad@129 209 throw new UnauthorizedException();
arpad@129 210 else
arpad@129 211 $dispatcher->previous();
arpad@128 212 return parent::DELETE($dispatcher);
arpad@128 213 }
arpad@0 214 }
arpad@106 215 ?>