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