root/devel/mod/rpc/lib/class_post.php

Revision 762, 9.3 kB (checked in by sven, 2 years ago)

rpc: fix hardcoded table prefix

  • Property svn:eol-style set to native
Line 
1 <?php
2     Class Post extends ElggObject
3     {
4         var $weblog;
5         var $blog_id;
6         var $access = 'PUBLIC';
7         var $posted;
8         var $title;
9         var $body;
10         var $tags;
11         var $comments;
12         var $type = 'post';
13
14         var $exists;
15
16         /**
17          *
18          */
19         function Post($post_id = null)
20         {
21             $this->exists = false;
22
23             // Parameter passed, assume an existing post
24             if ($post_id != null)
25             {
26                 if ($post = get_record('weblog_posts','ident',$post_id)) {
27                     $this->ident     = $post->ident;
28                     $this->owner     = $post->owner;
29                     $this->blog_id   = $post->weblog;
30                     $this->access    = $post->access;
31                     $this->posted    = $post->posted;
32                     $this->title     = $post->title;
33                     $this->body      = $post->body;
34                     
35                     // Get the weblog context
36                     $this->weblog = run('weblogs:instance', array('user_id' => $this->owner,
37                                                                   'blog_id' => $this->blog_id));
38                     
39                     // Does the requested id exist
40                     $this->exists = true;
41                 }
42                 
43                 if ($post_tags = get_records('tags','tagtype','weblog','ref',$post_id)) {
44                     // An aray of Tag objects
45                     foreach ($post_tags as $tag) {
46                         $this->tags[] = $tag->ident;
47                     }
48                 }
49                 
50                 if ($post_comments = get_records('weblog_comments','post_id',$post_id)) {
51                     foreach ($post_comments as $comment) {
52                         $this->comments[] = $comment->ident;
53                     }
54                 }
55             }
56         }
57
58         /**
59          *
60          */
61         function getCommunity()
62         {
63             return $this->community;
64         }
65
66         /**
67          *
68          */
69         function getAccess()
70         {
71             return $this->access;
72         }
73
74         /**
75          *
76          */
77         function getPosted()
78         {
79             return $this->posted;
80         }
81
82         /**
83          *
84          */
85         function getTitle()
86         {
87             return $this->title;
88         }
89
90         /**
91          *
92          */
93         function getBody()
94         {
95             return $this->body;
96         }
97
98         /**
99          *
100          */
101         function getWeblog()
102         {
103             return $this->blog_id;
104         }
105
106         /**
107          *
108          */
109         function getTags()
110         {
111             return $this->tags;
112         }
113
114         /**
115          *
116          */
117         function deleteTags()
118         {
119             $value = false;
120
121             foreach ($this->tags as $tag_id)
122             {
123                 $tag = run('tags:instance', array('id' => $tag_id));
124                 $value = $tag->delete();
125             }
126
127             return $value;
128         }
129
130         /**
131          *
132          */
133         function getTag($tag_id)
134         {
135             return run('tags:instance', array("id" => $tag_id));
136         }
137
138         /**
139          *
140          */
141         function getComments()
142         {
143             return $this->comments;
144         }
145         
146         /**
147          *
148          */
149         function getComment($comment_id)
150         {
151             return run('comments:instance', array('id' => $comment_id));
152         }
153
154         /**
155          *
156          */
157         function deleteComments()
158         {
159             $value = false;
160
161             foreach ($this->comments as $comment_id)
162             {
163                 $comment = run('comments:instance', array('id' => $comment_id));
164                 $value = $comment->delete();
165             }
166
167             return $value;
168         }
169
170         /**
171          *
172          */
173         function getUrl()
174         {
175             return $this->weblog->getUrl() ."#" . $this->ident;
176         }
177
178         /**
179          *
180          */
181         function getPermaLink()
182         {
183             return $this->weblog->getUrl() . $this->ident . ".html";
184         }
185
186         /**
187          *
188          */
189         function setOwner($val)
190         {
191             $this->owner = $val;
192         }
193
194         /**
195          *
196          */
197         function setCommunity($val)
198         {
199             $this->community = $val;
200         }
201
202         /**
203          *
204          */
205         function setAccess($val)
206         {
207             $this->access = $val;
208         }
209
210         /**
211          *
212          */
213         function setPosted($val)
214         {
215             $this->posted = $val;
216         }
217
218         /**
219          *
220          */
221         function setTitle($val)
222         {
223             $this->title = $val;
224         }
225
226         /**
227          *
228          */
229         function setBody($val)
230         {
231             $this->body = $val;
232         }
233
234         /**
235          *
236          */
237         function setWeblog($val)
238         {
239             $this->blog_id = $val;
240         }
241
242         function validate($action = null)
243         {
244             // $action parameter currently not used
245         
246             // Get the weblog object
247             if (!isset($this->weblog))
248             {
249                 // Get the weblog context
250                 $this->weblog = run('weblogs:instance', array('user_id' => $this->owner,
251                                                               'blog_id' => $this->blog_id));
252             }
253
254             // Always post to your own blog :)
255             if ($this->weblog->community == false && $this->owner == $this->blog_id)
256             {
257                 return true;
258             }
259
260             // A community can't post to itself
261             if ($this->weblog->community == true && $this->owner == $this->blog_id)
262             {
263                 return false;
264             }
265
266             // Only a member or community owner can post to a community
267             if ($this->weblog->community == true)
268             {
269                 if ($this->weblog->owner != $this->owner)
270                 {
271                     // Not community owner, is it a member?
272                     if ($result = get_records_sql('SELECT DISTINCT u.ident, 1
273                                                    FROM ' . $CFG->prefix . 'friends f JOIN ' . $CFG->prefix . 'users u
274                                                    ON u.ident = f.owner
275                                                    WHERE f.friend = ? AND u.ident = ? ',
276                                                    array($this->blog_id,$this->owner)))
277                     {
278                         // Memberships found
279                         return true;
280                     }
281                     else
282                     {
283                         // Not a friend, nor owner, deny this save
284                         return false;
285                     }
286                 }
287                 else
288                 {
289                     return true;
290                 }
291             }
292         }
293
294         /**
295          *
296          */
297         function delete()
298         {
299             if (!$this->validate('delete'))
300             {
301                 return false;
302             }
303
304             if ($this->exists)
305             {
306                 // Check ownership
307                 if ($this->weblog->isOwner() != true)
308                 {
309                     // Not weblog owner, check at post level
310                     if ($this->owner != $this->weblog->getOwner())
311                     {
312                         return false;
313                     }
314                 }
315
316                 // Remove related objects
317
318                 // Remove tags
319                 $this->deleteTags();
320
321                 // Remove comments
322                 $this->deleteComments();
323                 $rssresult = run("weblogs:rss:publish", array($this->owner, false));
324                 $rssresult = run("profile:rss:publish", array($this->owner, false));
325                 return delete_records('weblog_posts','ident',$this->ident);
326             }
327         }
328
329         /**
330          *
331          */
332         function save()
333         {
334             if (!$this->validate('save'))
335             {
336                 return false;
337             }
338             
339             $wp = new StdClass;
340             $wp->title = $this->title;
341             $wp->body = $this->body;
342             $wp->access = $this->access;
343             $wp->ident = $this->ident;
344
345             if ($this->exists == true)
346             {
347                 if (update_record('weblog_posts',$wp)) {
348                     $rssresult = run("weblogs:rss:publish", array($this->owner, false));
349                     $rssresult = run("profile:rss:publish", array($this->owner, false));
350                     return $this->ident;
351                 }
352                 return false;
353             }
354             else
355             {
356                 // Post doesn't exist
357
358                 $wp->weblog = $this->blog_id;
359                 $wp->posted = time();
360                 $wp->owner = $this->owner;
361
362                 if ($this->ident = insert_record('weblog_posts',$wp)) {
363                     $this->exists = true;
364                     $rssresult = run("weblogs:rss:publish", array($this->owner, false));
365                     $rssresult = run("profile:rss:publish", array($this->owner, false));
366                     return $this->ident;
367                 }
368                 else
369                 {
370                     return false;
371                 }
372             }
373         }
374     }
375
376 ?>
377
Note: See TracBrowser for help on using the browser.