Dotclear

source: inc/core/class.dc.blog.php @ 873:f9ef04edef05

Revision 873:f9ef04edef05, 28.9 KB checked in by Dsls <dsls@…>, 13 years ago (diff)

Turned db fetches into foreach, upgraded jquery

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14/**
15@ingroup DC_CORE
16@nosubgrouping
17@brief Dotclear blog class.
18
19Dotclear blog class instance is provided by dcCore $blog property.
20*/
21class dcBlog
22{
23     /** @var dcCore dcCore instance */
24     protected $core;
25     /** @var connection Database connection object */
26     public $con;
27     /** @var string Database table prefix */
28     public $prefix;
29     
30     /** @var string Blog ID */
31     public $id;
32     /** @var string Blog unique ID */
33     public $uid;
34     /** @var string Blog name */
35     public $name;
36     /** @var string Blog description */
37     public $desc;
38     /** @var string Blog URL */
39     public $url;
40     /** @var string Blog host */
41     public $host;
42     /** @var string Blog creation date */
43     public $creadt;
44     /** @var string Blog last update date */
45     public $upddt;
46     /** @var string Blog status */
47     public $status;
48     
49     /** @var dcSettings dcSettings object */
50     public $settings;
51     /** @var string Blog theme path */
52     public $themes_path;
53     /** @var string Blog public path */
54     public $public_path;
55     
56     private $post_status = array();
57     
58     /** @var boolean Disallow entries password protection */
59     public $without_password = true;
60     
61     /**
62     Inits dcBlog object
63     
64     @param    core      <b>dcCore</b>       Dotclear core reference
65     @param    id        <b>string</b>       Blog ID
66     */
67     public function __construct($core, $id)
68     {
69          $this->con =& $core->con;
70          $this->prefix = $core->prefix;
71          $this->core =& $core;
72         
73          if (($b = $this->core->getBlog($id)) !== false)
74          {
75               $this->id = $id;
76               $this->uid = $b->blog_uid;
77               $this->name = $b->blog_name;
78               $this->desc = $b->blog_desc;
79               $this->url = $b->blog_url;
80               $this->host = preg_replace('|^([a-z]{3,}://)(.*?)/.*$|','$1$2',$this->url);
81               $this->creadt = strtotime($b->blog_creadt);
82               $this->upddt = strtotime($b->blog_upddt);
83               $this->status = $b->blog_status;
84               
85               $this->settings = new dcSettings($this->core,$this->id);
86               
87               //$this->themes_path = path::fullFromRoot($this->settings->system->themes_path,DC_ROOT);
88               //$this->public_path = path::fullFromRoot($this->settings->system->public_path,DC_ROOT);
89               
90               $this->post_status['-2'] = __('pending');
91               $this->post_status['-1'] = __('scheduled');
92               $this->post_status['0'] = __('unpublished');
93               $this->post_status['1'] = __('published');
94                             
95               # --BEHAVIOR-- coreBlogConstruct
96               $this->core->callBehavior('coreBlogConstruct',$this);
97          }
98     }
99     
100     /// @name Common public methods
101     //@{
102     /**
103     Returns blog URL ending with a question mark.
104     */
105     public function getQmarkURL()
106     {
107          if (substr($this->url,-1) != '?') {
108               return $this->url.'?';
109          }
110         
111          return $this->url;
112     }
113     
114     /**
115     Returns an entry status name given to a code. Status are translated, never
116     use it for tests. If status code does not exist, returns <i>unpublished</i>.
117     
118     @param    s    <b>integer</b> Status code
119     @return   <b>string</b> Blog status name
120     */
121     public function getPostStatus($s)
122     {
123          if (isset($this->post_status[$s])) {
124               return $this->post_status[$s];
125          }
126          return $this->post_status['0'];
127     }
128     
129     /**
130     Returns an array of available entry status codes and names.
131     
132     @return   <b>array</b> Simple array with codes in keys and names in value
133     */
134     public function getAllPostStatus()
135     {
136          return $this->post_status;
137     }
138     
139     
140     /**
141     Disallows entries password protection. You need to set it to
142     <var>false</var> while serving a public blog.
143     
144     @param    v         <b>boolean</b>
145     */
146     public function withoutPassword($v)
147     {
148          $this->without_password = (boolean) $v;
149     }
150     //@}
151     
152     /// @name Triggers methods
153     //@{
154     /**
155     Updates blog last update date. Should be called every time you change
156     an element related to the blog.
157     */
158     public function triggerBlog()
159     {
160          $cur = $this->con->openCursor($this->prefix.'blog');
161         
162          $cur->blog_upddt = date('Y-m-d H:i:s');
163         
164          $cur->update("WHERE blog_id = '".$this->con->escape($this->id)."' ");
165         
166          # --BEHAVIOR-- coreBlogAfterTriggerBlog
167          $this->core->callBehavior('coreBlogAfterTriggerBlog',$cur);
168     }
169     
170     /// @name Entries management methods
171     //@{
172     /**
173     Retrieves entries. <b>$params</b> is an array taking the following
174     optionnal parameters:
175     
176     - no_content: Don't retrieve entry content (excerpt and content)
177     - post_type: Get only entries with given type (default "post", array for many types and '' for no type)
178     - post_id: (integer) Get entry with given post_id
179     - post_url: Get entry with given post_url field
180     - user_id: (integer) Get entries belonging to given user ID
181     - post_status: (integer) Get entries with given post_status
182     - post_selected: (boolean) Get select flaged entries
183     - post_year: (integer) Get entries with given year
184     - post_month: (integer) Get entries with given month
185     - post_day: (integer) Get entries with given day
186     - post_lang: Get entries with given language code
187     - search: Get entries corresponding of the following search string
188     - columns: (array) More columns to retrieve
189     - sql: Append SQL string at the end of the query
190     - from: Append SQL string after "FROM" statement in query
191     - order: Order of results (default "ORDER BY post_dt DES")
192     - limit: Limit parameter
193     - sql_only : return the sql request instead of results. Only ids are selected
194     
195     @param    params         <b>array</b>        Parameters
196     @param    count_only     <b>boolean</b>      Only counts results
197     @return   <b>record</b>  A record with some more capabilities or the SQL request
198     */
199     public function getPosts($params=array(),$count_only=false)
200     {
201          # --BEHAVIOR-- coreBlogBeforeGetPosts
202          $params = new ArrayObject($params);
203          $this->core->callBehavior('coreBlogBeforeGetPosts',$params);
204
205          if ($count_only)
206          {
207               $strReq = 'SELECT count(P.post_id) ';
208          }
209          elseif (!empty($params['sql_only'])) 
210          {
211               $strReq = 'SELECT P.post_id ';
212          }
213          else
214          {
215               if (!empty($params['no_content'])) {
216                    $content_req = '';
217               } else {
218                    $content_req =
219                    'post_excerpt, post_excerpt_xhtml, '.
220                    'post_content, post_content_xhtml, post_notes, ';
221               }
222               
223               if (!empty($params['columns']) && is_array($params['columns'])) {
224                    $content_req .= implode(', ',$params['columns']).', ';
225               }
226               
227               $strReq =
228               'SELECT P.post_id, P.blog_id, P.user_id, post_dt, '.
229               'post_tz, post_creadt, post_upddt, post_format, post_password, '.
230               'post_url, post_lang, post_title, '.$content_req.
231               'post_type, post_meta, post_status, post_selected, post_position, '.
232               'U.user_name, U.user_firstname, U.user_displayname, U.user_email, '.
233               'U.user_url ';
234          }
235         
236          $strReq .=
237          'FROM '.$this->prefix.'post P '.
238          'INNER JOIN '.$this->prefix.'user U ON U.user_id = P.user_id ';
239         
240          if (!empty($params['from'])) {
241               $strReq .= $params['from'].' ';
242          }
243         
244          $strReq .=
245          "WHERE P.blog_id = '".$this->con->escape($this->id)."' ";
246         
247          if (!$this->core->auth->check('contentadmin',$this->id)) {
248               $strReq .= 'AND ((post_status = 1 ';
249               
250               if ($this->without_password) {
251                    $strReq .= 'AND post_password IS NULL ';
252               }
253               $strReq .= ') ';
254               
255               if ($this->core->auth->userID()) {
256                    $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')";
257               } else {
258                    $strReq .= ') ';
259               }
260          }
261         
262          #Adding parameters
263          if (isset($params['post_type']))
264          {
265               if (is_array($params['post_type']) || $params['post_type'] != '') {
266                    $strReq .= 'AND post_type '.$this->con->in($params['post_type']);
267               }
268          }
269          else
270          {
271               $strReq .= "AND post_type = 'post' ";
272          }
273         
274          if (isset($params['post_id']) && $params['post_id'] !== '') {
275               if (is_array($params['post_id'])) {
276                    array_walk($params['post_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
277               } else {
278                    $params['post_id'] = array((integer) $params['post_id']);
279               }
280               $strReq .= 'AND P.post_id '.$this->con->in($params['post_id']);
281          }
282         
283          if (isset($params['post_url']) && $params['post_url'] !== '') {
284               $strReq .= "AND post_url = '".$this->con->escape($params['post_url'])."' ";
285          }
286         
287          if (!empty($params['user_id'])) {
288               $strReq .= "AND U.user_id = '".$this->con->escape($params['user_id'])."' ";
289          }
290         
291          /* Other filters */
292          if (isset($params['post_status'])) {
293               $strReq .= 'AND post_status = '.(integer) $params['post_status'].' ';
294          }
295         
296          if (isset($params['post_selected'])) {
297               $strReq .= 'AND post_selected = '.(integer) $params['post_selected'].' ';
298          }
299         
300          if (!empty($params['post_year'])) {
301               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y').' = '.
302               "'".sprintf('%04d',$params['post_year'])."' ";
303          }
304         
305          if (!empty($params['post_month'])) {
306               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m').' = '.
307               "'".sprintf('%02d',$params['post_month'])."' ";
308          }
309         
310          if (!empty($params['post_day'])) {
311               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d').' = '.
312               "'".sprintf('%02d',$params['post_day'])."' ";
313          }
314         
315          if (!empty($params['post_lang'])) {
316               $strReq .= "AND P.post_lang = '".$this->con->escape($params['post_lang'])."' ";
317          }
318         
319          if (!empty($params['search']))
320          {
321               $words = text::splitWords($params['search']);
322               
323               if (!empty($words))
324               {
325                    # --BEHAVIOR-- corePostSearch
326                    if ($this->core->hasBehavior('corePostSearch')) {
327                         $this->core->callBehavior('corePostSearch',$this->core,array(&$words,&$strReq,&$params));
328                    }
329                   
330                    if ($words)
331                    {
332                         foreach ($words as $i => $w) {
333                              $words[$i] = "post_words LIKE '%".$this->con->escape($w)."%'";
334                         }
335                         $strReq .= 'AND '.implode(' AND ',$words).' ';
336                    }
337               }
338          }
339         
340          if (!empty($params['sql'])) {
341               $strReq .= $params['sql'].' ';
342          }
343         
344          if (!$count_only)
345          {
346               if (!empty($params['order'])) {
347                    $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' ';
348               } else {
349                    $strReq .= 'ORDER BY post_dt DESC ';
350               }
351          }
352         
353          if (!$count_only && !empty($params['limit'])) {
354               $strReq .= $this->con->limit($params['limit']);
355          }
356         
357          if (!empty($params['sql_only'])) {
358               return $strReq;
359          }
360         
361          $posts = $this->con->select($strReq);
362          $posts->core = $this->core;
363          $posts->extend('rsExtPost');
364         
365          # --BEHAVIOR-- coreBlogGetPosts
366          $this->core->callBehavior('coreBlogGetPosts',$posts);
367         
368          return $posts;
369     }
370     
371     /**
372     Returns a record with post id, title and date for next or previous post
373     according to the post ID.
374     $dir could be 1 (next post) or -1 (previous post).
375     
376     @param    post_id                  <b>integer</b>      Post ID
377     @param    dir                      <b>integer</b>      Search direction
378     @param    restrict_to_lang         <b>boolean</b>      Restrict to post with same lang
379     @return   record
380     */
381     public function getNextPost($post, $dir, $restrict_to_lang=false)
382     {
383          $dt = $post->post_dt;
384          $post_id = (integer) $post->post_id;
385         
386          if($dir > 0) {
387               $sign = '>';
388               $order = 'ASC';
389          }
390          else {
391               $sign = '<';
392               $order = 'DESC';
393          }
394         
395          $params['post_type'] = $post->post_type;
396          $params['limit'] = 1;
397          $params['order'] = 'post_dt '.$order.', P.post_id '.$order;
398          $params['sql'] =
399          'AND ( '.
400          "    (post_dt = '".$this->con->escape($dt)."' AND P.post_id ".$sign." ".$post_id.") ".
401          "    OR post_dt ".$sign." '".$this->con->escape($dt)."' ".
402          ') ';
403         
404          if ($restrict_to_lang) {
405               $params['sql'] .= $post->post_lang ? 'AND P.post_lang = \''. $this->con->escape($post->post_lang) .'\' ': 'AND P.post_lang IS NULL ';
406          }
407         
408          $posts = $this->getPosts($params);
409         
410          if (count($posts) == 0) {
411               return null;
412          }
413         
414          return $posts;
415     }
416     
417     /**
418     Retrieves different languages and post count on blog, based on post_lang
419     field. <var>$params</var> is an array taking the following optionnal
420     parameters:
421     
422     - post_type: Get only entries with given type (default "post", '' for no type)
423     - lang: retrieve post count for selected lang
424     - order: order statement (default post_lang DESC)
425     
426     @param    params    <b>array</b>        Parameters
427     @return   record
428     */
429     public function getLangs($params=array())
430     {
431          $strReq = 'SELECT COUNT(post_id) as nb_post, post_lang '.
432                    'FROM '.$this->prefix.'post '.
433                    "WHERE blog_id = '".$this->con->escape($this->id)."' ".
434                    "AND post_lang <> '' ".
435                    "AND post_lang IS NOT NULL ";
436         
437          if (!$this->core->auth->check('contentadmin',$this->id)) {
438               $strReq .= 'AND ((post_status = 1 ';
439               
440               if ($this->without_password) {
441                    $strReq .= 'AND post_password IS NULL ';
442               }
443               $strReq .= ') ';
444               
445               if ($this->core->auth->userID()) {
446                    $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."')";
447               } else {
448                    $strReq .= ') ';
449               }
450          }
451         
452          if (isset($params['post_type'])) {
453               if ($params['post_type'] != '') {
454                    $strReq .= "AND post_type = '".$this->con->escape($params['post_type'])."' ";
455               }
456          } else {
457               $strReq .= "AND post_type = 'post' ";
458          }
459         
460          if (isset($params['lang'])) {
461               $strReq .= "AND post_lang = '".$this->con->escape($params['lang'])."' ";
462          }
463         
464          $strReq .= 'GROUP BY post_lang ';
465         
466          $order = 'desc';
467          if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) {
468               $order = $params['order'];
469          }
470          $strReq .= 'ORDER BY post_lang '.$order.' ';
471         
472          return $this->con->select($strReq);
473     }
474     
475     /**
476     Returns a record with all distinct blog dates and post count.
477     <var>$params</var> is an array taking the following optionnal parameters:
478     
479     - type: (day|month|year) Get days, months or years
480     - year: (integer) Get dates for given year
481     - month: (integer) Get dates for given month
482     - day: (integer) Get dates for given day
483     - post_lang: lang of the posts
484     - next: Get date following match
485     - previous: Get date before match
486     - order: Sort by date "ASC" or "DESC"
487     
488     @param    params    <b>array</b>        Parameters array
489     @return   record
490     */
491     public function getDates($params=array())
492     {
493          $dt_f = '%Y-%m-%d';
494          $dt_fc = '%Y%m%d';
495          if (isset($params['type'])) {
496               if ($params['type'] == 'year') {
497                    $dt_f = '%Y-01-01';
498                    $dt_fc = '%Y0101';
499               } elseif ($params['type'] == 'month') {
500                    $dt_f = '%Y-%m-01';
501                    $dt_fc = '%Y%m01';
502               }
503          }
504          $dt_f .= ' 00:00:00';
505          $dt_fc .= '000000';
506         
507          $cat_field = $catReq = $limit = '';
508         
509          if (!empty($params['post_lang'])) {
510               $catReq = 'AND P.post_lang = \''. $params['post_lang'].'\' ';
511          }
512         
513          $strReq = 'SELECT DISTINCT('.$this->con->dateFormat('post_dt',$dt_f).') AS dt '.
514                    ',COUNT(P.post_id) AS nb_post '.
515                    'FROM '.$this->prefix.'post P '.
516                    "WHERE P.blog_id = '".$this->con->escape($this->id)."' ".
517                    $catReq;
518         
519          if (!$this->core->auth->check('contentadmin',$this->id)) {
520               $strReq .= 'AND ((post_status = 1 ';
521               
522               if ($this->without_password) {
523                    $strReq .= 'AND post_password IS NULL ';
524               }
525               $strReq .= ') ';
526               
527               if ($this->core->auth->userID()) {
528                    $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')";
529               } else {
530                    $strReq .= ') ';
531               }
532          }
533         
534          if (!empty($params['post_type'])) {
535               $strReq .= "AND post_type ".$this->con->in($params['post_type'])." ";
536          } else {
537               $strReq .= "AND post_type = 'post' ";
538          }
539         
540          if (!empty($params['year'])) {
541               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y')." = '".sprintf('%04d',$params['year'])."' ";
542          }
543         
544          if (!empty($params['month'])) {
545               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m')." = '".sprintf('%02d',$params['month'])."' ";
546          }
547         
548          if (!empty($params['day'])) {
549               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d')." = '".sprintf('%02d',$params['day'])."' ";
550          }
551         
552          # Get next or previous date
553          if (!empty($params['next']) || !empty($params['previous']))
554          {
555               if (!empty($params['next'])) {
556                    $pdir = ' > ';
557                    $params['order'] = 'asc';
558                    $dt = $params['next'];
559               } else {
560                    $pdir = ' < ';
561                    $params['order'] = 'desc';
562                    $dt = $params['previous'];
563               }
564               
565               $dt = date('YmdHis',strtotime($dt));
566               
567               $strReq .= 'AND '.$this->con->dateFormat('post_dt',$dt_fc).$pdir."'".$dt."' ";
568               $limit = $this->con->limit(1);
569          }
570         
571          $order = 'desc';
572          if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) {
573               $order = $params['order'];
574          }
575         
576          $strReq .=
577          'ORDER BY dt '.$order.' '.
578          $limit;
579         
580          $dates = $this->con->select($strReq);
581          $dates->extend('rsExtDates');
582          return $dates;
583     }
584     
585     /**
586     Creates a new entry. Takes a cursor as input and returns the new entry
587     ID.
588     
589     @param    cur       <b>cursor</b>       Post cursor
590     @return   <b>integer</b>      New post ID
591     */
592     public function addPost($cur)
593     {
594          if (!$this->core->auth->check('usage,contentadmin',$this->id)) {
595               throw new Exception(__('You are not allowed to create an entry'));
596          }
597         
598          $this->con->writeLock($this->prefix.'post');
599          try
600          {
601               # Get ID
602               $max = $this->con->select(
603                    'SELECT MAX(post_id) '.
604                    'FROM '.$this->prefix.'post ' 
605                    );
606               
607               $max = $max->current();
608               
609               $cur->post_id = (integer) $max->f(0) + 1;
610               $cur->blog_id = (string) $this->id;
611               $cur->post_creadt = date('Y-m-d H:i:s');
612               $cur->post_upddt = date('Y-m-d H:i:s');
613               $cur->post_tz = $this->core->auth->getInfo('user_tz');
614               
615               # Post excerpt and content
616               $this->getPostContent($cur,$cur->post_id);
617               
618               $this->getPostCursor($cur);
619               
620               $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$cur->post_id);
621               
622               if (!$this->core->auth->check('publish,contentadmin',$this->id)) {
623                    $cur->post_status = -2;
624               }
625               
626               # --BEHAVIOR-- coreBeforePostCreate
627               $this->core->callBehavior('coreBeforePostCreate',$this,$cur);
628               
629               $cur->insert();
630               $this->con->unlock();
631          }
632          catch (Exception $e)
633          {
634               $this->con->unlock();
635               throw $e;
636          }
637         
638          # --BEHAVIOR-- coreAfterPostCreate
639          $this->core->callBehavior('coreAfterPostCreate',$this,$cur);
640         
641          $this->triggerBlog();
642         
643          return $cur->post_id;
644     }
645     
646     /**
647     Updates an existing post.
648     
649     @param    id        <b>integer</b>      Post ID
650     @param    cur       <b>cursor</b>       Post cursor
651     */
652     public function updPost($id,$cur)
653     {
654          if (!$this->core->auth->check('usage,contentadmin',$this->id)) {
655               throw new Exception(__('You are not allowed to update entries'));
656          }
657         
658          $id = (integer) $id;
659         
660          if (empty($id)) {
661               throw new Exception(__('No such entry ID'));
662          }
663         
664          # Post excerpt and content
665          $this->getPostContent($cur,$id);
666         
667          $this->getPostCursor($cur);
668         
669          if ($cur->post_url !== null) {
670               $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$id);
671          }
672         
673          if (!$this->core->auth->check('publish,contentadmin',$this->id)) {
674               $cur->unsetField('post_status');
675          }
676         
677          $cur->post_upddt = date('Y-m-d H:i:s');
678         
679          #If user is only "usage", we need to check the post's owner
680          if (!$this->core->auth->check('contentadmin',$this->id))
681          {
682               $strReq = 'SELECT post_id '.
683                         'FROM '.$this->prefix.'post '.
684                         'WHERE post_id = '.$id.' '.
685                         "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
686               
687               $posts = $this->con->select($strReq);
688               
689               if (count($posts) == 0) {
690                    throw new Exception(__('You are not allowed to edit this entry'));
691               }
692          }
693         
694          # --BEHAVIOR-- coreBeforePostUpdate
695          $this->core->callBehavior('coreBeforePostUpdate',$this,$cur);
696         
697          $cur->update('WHERE post_id = '.$id.' ');
698         
699          # --BEHAVIOR-- coreAfterPostUpdate
700          $this->core->callBehavior('coreAfterPostUpdate',$this,$cur);
701         
702          $this->triggerBlog();
703     }
704     
705     /**
706     Updates post status.
707     
708     @param    id        <b>integer</b>      Post ID
709     @param    status    <b>integer</b>      Post status
710     */
711     public function updPostStatus($id,$status)
712     {
713          if (!$this->core->auth->check('publish,contentadmin',$this->id)) {
714               throw new Exception(__('You are not allowed to change this entry status'));
715          }
716         
717          $id = (integer) $id;
718          $status = (integer) $status;
719         
720          #If user can only publish, we need to check the post's owner
721          if (!$this->core->auth->check('contentadmin',$this->id))
722          {
723               $strReq = 'SELECT post_id '.
724                         'FROM '.$this->prefix.'post '.
725                         'WHERE post_id = '.$id.' '.
726                         "AND blog_id = '".$this->con->escape($this->id)."' ".
727                         "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
728               
729               $posts = $this->con->select($strReq);
730               
731               if (count($posts) == 0) {
732                    throw new Exception(__('You are not allowed to change this entry status'));
733               }
734          }
735         
736          $cur = $this->con->openCursor($this->prefix.'post');
737         
738          $cur->post_status = $status;
739          $cur->post_upddt = date('Y-m-d H:i:s');
740         
741          $cur->update(
742               'WHERE post_id = '.$id.' '.
743               "AND blog_id = '".$this->con->escape($this->id)."' "
744               );
745          $this->triggerBlog();
746     }
747     
748     public function updPostSelected($id,$selected)
749     {
750          $id = (integer) $id;
751          $selected = (boolean) $selected;
752         
753          # If user is only usage, we need to check the post's owner
754          if (!$this->core->auth->check('contentadmin',$this->id))
755          {
756               $strReq = 'SELECT post_id '.
757                         'FROM '.$this->prefix.'post '.
758                         'WHERE post_id = '.$id.' '.
759                         "AND blog_id = '".$this->con->escape($this->id)."' ".
760                         "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
761               
762               $posts = $this->con->select($strReq);
763               
764               if (count($posts) == 0) {
765                    throw new Exception(__('You are not allowed to mark this entry as selected'));
766               }
767          }
768         
769          $cur = $this->con->openCursor($this->prefix.'post');
770         
771          $cur->post_selected = (integer) $selected;
772          $cur->post_upddt = date('Y-m-d H:i:s');
773         
774          $cur->update(
775               'WHERE post_id = '.$id.' '.
776               "AND blog_id = '".$this->con->escape($this->id)."' "
777          );
778          $this->triggerBlog();
779     }
780     
781     /**
782     Deletes a post.
783     
784     @param    id        <b>integer</b>      Post ID
785     */
786     public function delPost($id)
787     {
788          if (!$this->core->auth->check('delete,contentadmin',$this->id)) {
789               throw new Exception(__('You are not allowed to delete entries'));
790          }
791         
792          $id = (integer) $id;
793         
794          if (empty($id)) {
795               throw new Exception(__('No such entry ID'));
796          }
797         
798          #If user can only delete, we need to check the post's owner
799          if (!$this->core->auth->check('contentadmin',$this->id))
800          {
801               $strReq = 'SELECT post_id '.
802                         'FROM '.$this->prefix.'post '.
803                         'WHERE post_id = '.$id.' '.
804                         "AND blog_id = '".$this->con->escape($this->id)."' ".
805                         "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
806               
807               $posts = $this->con->select($strReq);
808               
809               if (count($posts) == 0) {
810                    throw new Exception(__('You are not allowed to delete this entry'));
811               }
812          }
813         
814         
815          $strReq = 'DELETE FROM '.$this->prefix.'post '.
816                    'WHERE post_id = '.$id.' '.
817                    "AND blog_id = '".$this->con->escape($this->id)."' ";
818         
819          $this->con->execute($strReq);
820          $this->triggerBlog();
821     }
822     
823     /**
824     Publishes all entries flaged as "scheduled".
825     */
826     public function publishScheduledEntries()
827     {
828          $strReq = 'SELECT post_id, post_dt, post_tz '.
829                    'FROM '.$this->prefix.'post '.
830                    'WHERE post_status = -1 '.
831                    "AND blog_id = '".$this->con->escape($this->id)."' ";
832         
833          $posts = $this->con->select($strReq);
834         
835          $now = dt::toUTC(time());
836          $to_change = new ArrayObject();
837
838          if (count($posts) == 0) {
839               return;
840          }
841         
842          foreach ($posts as $post)
843          {
844               # Now timestamp with post timezone
845               $now_tz = $now + dt::getTimeOffset($post->post_tz,$now);
846               
847               # Post timestamp
848               $post_ts = strtotime($post->post_dt);
849               
850               # If now_tz >= post_ts, we publish the entry
851               if ($now_tz >= $post_ts) {
852                    $to_change[] = (integer) $post->post_id;
853               }
854          }
855          if (count($to_change))
856          {
857               # --BEHAVIOR-- coreBeforeScheduledEntriesPublish
858               $this->core->callBehavior('coreBeforeScheduledEntriesPublish',$this,$to_change);
859
860               $strReq =
861               'UPDATE '.$this->prefix.'post SET '.
862               'post_status = 1 '.
863               "WHERE blog_id = '".$this->con->escape($this->id)."' ".
864               'AND post_id '.$this->con->in((array)$to_change).' ';
865               $this->con->execute($strReq);
866               $this->triggerBlog();
867
868               # --BEHAVIOR-- coreAfterScheduledEntriesPublish
869               $this->core->callBehavior('coreAfterScheduledEntriesPublish',$this,$to_change);
870          }
871         
872     }
873     
874     /**
875     Retrieves all users having posts on current blog.
876     
877     @param    post_type      <b>string</b>       post_type filter (post)
878     @return   record
879     */
880     public function getPostsUsers($post_type='post')
881     {
882          $strReq = 'SELECT P.user_id, user_name, user_firstname, '.
883                    'user_displayname, user_email '.
884                    'FROM '.$this->prefix.'post P, '.$this->prefix.'user U '.
885                    'WHERE P.user_id = U.user_id '.
886                    "AND blog_id = '".$this->con->escape($this->id)."' ";
887         
888          if ($post_type) {
889               $strReq .= "AND post_type = '".$this->con->escape($post_type)."' ";
890          }
891         
892          $strReq .= 'GROUP BY P.user_id, user_name, user_firstname, user_displayname, user_email ';
893         
894          return $this->con->select($strReq);
895     }
896     
897     private function getPostCursor($cur,$post_id=null)
898     {
899          if ($cur->post_title == '') {
900               throw new Exception(__('No entry title'));
901          }
902         
903          if ($cur->post_content == '') {
904               throw new Exception(__('No entry content'));
905          }
906         
907          if ($cur->post_password === '') {
908               $cur->post_password = null;
909          }
910         
911          if ($cur->post_dt == '') {
912               $offset = dt::getTimeOffset($this->core->auth->getInfo('user_tz'));
913               $now = time() + $offset;
914               $cur->post_dt = date('Y-m-d H:i:00',$now);
915          }
916         
917          $post_id = is_int($post_id) ? $post_id : $cur->post_id;
918         
919          if ($cur->post_content_xhtml == '') {
920               throw new Exception(__('No entry content'));
921          }
922         
923          # Words list
924          if ($cur->post_title !== null && $cur->post_excerpt_xhtml !== null
925          && $cur->post_content_xhtml !== null)
926          {
927               $words =
928               $cur->post_title.' '.
929               $cur->post_excerpt_xhtml.' '.
930               $cur->post_content_xhtml;
931               
932               $cur->post_words = implode(' ',text::splitWords($words));
933          }
934     }
935     
936     private function getPostContent($cur,$post_id)
937     {
938          $post_excerpt = $cur->post_excerpt;
939          $post_excerpt_xhtml = $cur->post_excerpt_xhtml;
940          $post_content = $cur->post_content;
941          $post_content_xhtml = $cur->post_content_xhtml;
942         
943          $this->setPostContent(
944               $post_id,$cur->post_format,$cur->post_lang,
945               $post_excerpt,$post_excerpt_xhtml,
946               $post_content,$post_content_xhtml
947          );
948         
949          $cur->post_excerpt = $post_excerpt;
950          $cur->post_excerpt_xhtml = $post_excerpt_xhtml;
951          $cur->post_content = $post_content;
952          $cur->post_content_xhtml = $post_content_xhtml;
953     }
954     
955     /**
956     Creates post HTML content, taking format and lang into account.
957     
958     @param         post_id        <b>integer</b>      Post ID
959     @param         format         <b>string</b>       Post format
960     @param         lang           <b>string</b>       Post lang
961     @param         excerpt        <b>string</b>       Post excerpt
962     @param[out]    excerpt_xhtml  <b>string</b>       Post excerpt HTML
963     @param         content        <b>string</b>       Post content
964     @param[out]    content_xhtml  <b>string</b>       Post content HTML
965     */
966     public function setPostContent($post_id,$format,$lang,&$excerpt,&$excerpt_xhtml,&$content,&$content_xhtml)
967     {
968          if ($format == 'wiki')
969          {
970               $this->core->initWikiPost();
971               $this->core->wiki2xhtml->setOpt('note_prefix','pnote-'.$post_id);
972               if (strpos($lang,'fr') === 0) {
973                    $this->core->wiki2xhtml->setOpt('active_fr_syntax',1);
974               }
975          }
976         
977          if ($excerpt) {
978               $excerpt_xhtml = $this->core->callFormater($format,$excerpt);
979               $excerpt_xhtml = $this->core->HTMLfilter($excerpt_xhtml);
980          } else {
981               $excerpt_xhtml = '';
982          }
983         
984          if ($content) {
985               $content_xhtml = $this->core->callFormater($format,$content);
986               $content_xhtml = $this->core->HTMLfilter($content_xhtml);
987          } else {
988               $content_xhtml = '';
989          }
990         
991          # --BEHAVIOR-- coreAfterPostContentFormat
992          $this->core->callBehavior('coreAfterPostContentFormat',array(
993               'excerpt' => &$excerpt,
994               'content' => &$content,
995               'excerpt_xhtml' => &$excerpt_xhtml,
996               'content_xhtml' => &$content_xhtml
997          ));
998     }
999     
1000     /**
1001     Returns URL for a post according to blog setting <var>post_url_format</var>.
1002     It will try to guess URL and append some figures if needed.
1003     
1004     @param    url            <b>string</b>       Origin URL, could be empty
1005     @param    post_dt        <b>string</b>       Post date (in YYYY-MM-DD HH:mm:ss)
1006     @param    post_title     <b>string</b>       Post title
1007     @param    post_id        <b>integer</b>      Post ID
1008     @return   <b>string</b>  result URL
1009     */
1010     public function getPostURL($url,$post_dt,$post_title,$post_id)
1011     {
1012          $url = trim($url);
1013         
1014          $url_patterns = array(
1015          '{y}' => date('Y',strtotime($post_dt)),
1016          '{m}' => date('m',strtotime($post_dt)),
1017          '{d}' => date('d',strtotime($post_dt)),
1018          '{t}' => text::tidyURL($post_title),
1019          '{id}' => (integer) $post_id
1020          );
1021         
1022          # If URL is empty, we create a new one
1023          if ($url == '')
1024          {
1025               # Transform with format
1026               $url = str_replace(
1027                    array_keys($url_patterns),
1028                    array_values($url_patterns),
1029                    $this->settings->system->post_url_format
1030               );
1031          }
1032          else
1033          {
1034               $url = text::tidyURL($url);
1035          }
1036         
1037          # Let's check if URL is taken...
1038          $strReq = 'SELECT post_url FROM '.$this->prefix.'post '.
1039                    "WHERE post_url = '".$this->con->escape($url)."' ".
1040                    'AND post_id <> '.(integer) $post_id. ' '.
1041                    "AND blog_id = '".$this->con->escape($this->id)."' ".
1042                    'ORDER BY post_url DESC';
1043         
1044          $urls = $this->con->select($strReq);
1045         
1046          if (count($urls) > 0)
1047          {
1048               if ($this->con->driver() == 'mysql') {
1049                    $clause = "REGEXP '^".$this->con->escape($url)."[0-9]+$'";
1050               } elseif ($this->con->driver() == 'pgsql') {
1051                    $clause = "~ '^".$this->con->escape($url)."[0-9]+$'";
1052               } else {
1053                    $clause = "LIKE '".$this->con->escape($url)."%'";
1054               }
1055               $strReq = 'SELECT post_url FROM '.$this->prefix.'post '.
1056                         "WHERE post_url ".$clause.' '.
1057                         'AND post_id <> '.(integer) $post_id.' '.
1058                         "AND blog_id = '".$this->con->escape($this->id)."' ".
1059                         'ORDER BY post_url DESC ';
1060               
1061               $urls = $this->con->select($strReq);
1062               $a = array();
1063               foreach ($urls as $u) {
1064                    $a[] = $u->post_url;
1065               }
1066               
1067               natsort($a);
1068               $t_url = end($a);
1069               
1070               if (preg_match('/(.*?)([0-9]+)$/',$t_url,$m)) {
1071                    $i = (integer) $m[2];
1072                    $url = $m[1];
1073               } else {
1074                    $i = 1;
1075               }
1076               
1077               return $url.($i+1);
1078          }
1079         
1080          # URL is empty?
1081          if ($url == '') {
1082               throw new Exception(__('Empty entry URL'));
1083          }
1084         
1085          return $url;
1086     }
1087     //@}
1088}
1089?>
Note: See TracBrowser for help on using the repository browser.

Sites map