Dotclear

source: plugins/pageMaker/_public.php @ 1552

Revision 1552, 11.1 KB checked in by Tomtom33, 14 years ago (diff)

pageMaker 0.4 :

  • changes name from splitPost to pageMaker
  • adds comment list pagination
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of pageMaker, a plugin for Dotclear.
4#
5# Copyright (c) 2009 Tomtom
6# http://blog.zenstyle.fr/
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11# -- END LICENSE BLOCK ------------------------------------
12
13if (!defined('DC_RC_PATH')) { return; }
14
15$core->tpl->addValue('PostPagination',array('pageMakerTpl','PostPagination'));
16$core->tpl->addValue('EntryContent',array('pageMakerTpl','EntryContent'));
17$core->tpl->addValue('CommentPagination',array('pageMakerTpl','CommentPagination'));
18$core->tpl->addValue('CommentOrderNumber',array('pageMakerTpl','CommentOrderNumber'));
19$core->tpl->addBlock('Comments',array('pageMakerTpl','Comments'));
20
21$core->url->register('post','post','^post/(.+)$',array('pageMakerUrl','post'));
22
23$core->addBehavior('coreBlogGetPosts',array('pageMakerBehaviors','coreBlogGetPosts'));
24
25class pageMakerUrl extends dcUrlHandlers
26{
27     public static function post($args)
28     {
29          if ($args == '') {
30               self::p404();
31          }
32         
33          $_ctx = $GLOBALS['_ctx'];
34          $core = $GLOBALS['core'];
35         
36          $core->blog->withoutPassword(false);
37         
38          # Create pagination pattern
39          for ($i=0 ; $i < count(explode('/',$core->blog->settings->post_url_format)) ; $i++) {
40               $post_format[] = '[^/]*';
41          }
42         
43          $pattern = '#^('.implode('/',$post_format).')((/page/([0-9]+))?(/c/([0-9]+))?)$#';
44         
45          preg_match($pattern,$args,$matches);
46
47          $params = new ArrayObject();
48          $params['post_url'] = $matches[1];
49         
50          $_ctx->posts = $core->blog->getPosts($params);
51         
52          # Post page
53          if ($core->blog->settings->pagemaker_post_enable) {
54               $_ctx->post_page_count = count(preg_split($core->post_page_pattern,$_ctx->posts->post_content_xhtml));
55               $_ctx->post_page_current = !empty($matches[4]) ? (int) $matches[4] : 1;
56               if (
57                    $_ctx->post_page_current === 0 ||
58                    $_ctx->post_page_current > $_ctx->post_page_count
59               ) {
60                    self::p404();
61                    return;
62               }
63          }
64          # Comment page
65          if ($core->blog->settings->pagemaker_comment_enable) {
66               $_ctx->post_comment_count = ceil($core->blog->getComments(array('no_content' => 1,'comment_trackback' => 0, 'post_id' => $_ctx->posts->post_id),true)->f(0)/$core->blog->settings->pagemaker_comment_nb_per_page);
67               $_ctx->post_comment_current = !empty($matches[6]) ? (int) $matches[6] : 1;
68               if (
69                    $_ctx->post_comment_count === 0 ||
70                    $_ctx->post_comment_current > $_ctx->post_comment_count
71               ) {
72                    self::p404();
73                    return;
74               }
75          }
76         
77          $_ctx->comment_preview = new ArrayObject();
78          $_ctx->comment_preview['content'] = '';
79          $_ctx->comment_preview['rawcontent'] = '';
80          $_ctx->comment_preview['name'] = '';
81          $_ctx->comment_preview['mail'] = '';
82          $_ctx->comment_preview['site'] = '';
83          $_ctx->comment_preview['preview'] = false;
84          $_ctx->comment_preview['remember'] = false;
85         
86          $core->blog->withoutPassword(true);
87         
88         
89          if ($_ctx->posts->isEmpty())
90          { 
91               # No entry
92               self::p404();
93               return;
94          }
95         
96          $post_id = $_ctx->posts->post_id;
97          $post_password = $_ctx->posts->post_password;
98         
99          # Password protected entry
100          if ($post_password != '')
101          {
102               # Get passwords cookie
103               if (isset($_COOKIE['dc_passwd'])) {
104                    $pwd_cookie = unserialize($_COOKIE['dc_passwd']);
105               } else {
106                    $pwd_cookie = array();
107               }
108               
109               # Check for match
110               if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
111               || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password))
112               {
113                    $pwd_cookie[$post_id] = $post_password;
114                    setcookie('dc_passwd',serialize($pwd_cookie),0,'/');
115               }
116               else
117               {
118                    self::serveDocument('password-form.html','text/html',false);
119                    return;
120               }
121          }
122         
123          $post_comment =
124               isset($_POST['c_name']) && isset($_POST['c_mail']) &&
125               isset($_POST['c_site']) && isset($_POST['c_content']) &&
126               $_ctx->posts->commentsActive();
127         
128          # Posting a comment
129          if ($post_comment)
130          {
131               # Spam trap
132               if (!empty($_POST['f_mail'])) {
133                    http::head(412,'Precondition Failed');
134                    header('Content-Type: text/plain');
135                    echo "So Long, and Thanks For All the Fish";
136                    exit;
137               }
138               
139               $name = $_POST['c_name'];
140               $mail = $_POST['c_mail'];
141               $site = $_POST['c_site'];
142               $content = $_POST['c_content'];
143               $preview = !empty($_POST['preview']);
144               
145               if ($content != '')
146               {
147                    if ($core->blog->settings->wiki_comments) {
148                         $core->initWikiComment();
149                    } else {
150                         $core->initWikiSimpleComment();
151                    }
152                    $content = $core->wikiTransform($content);
153                    $content = $core->HTMLfilter($content);
154               }
155               
156               $_ctx->comment_preview['content'] = $content;
157               $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
158               $_ctx->comment_preview['name'] = $name;
159               $_ctx->comment_preview['mail'] = $mail;
160               $_ctx->comment_preview['site'] = $site;
161               
162               if ($preview)
163               {
164                    # --BEHAVIOR-- publicBeforeCommentPreview
165                    $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
166                   
167                    $_ctx->comment_preview['preview'] = true;
168               }
169               else
170               {
171                    # Post the comment
172                    $cur = $core->con->openCursor($core->prefix.'comment');
173                    $cur->comment_author = $name;
174                    $cur->comment_site = html::clean($site);
175                    $cur->comment_email = html::clean($mail);
176                    $cur->comment_content = $content;
177                    $cur->post_id = $_ctx->posts->post_id;
178                    $cur->comment_status = $core->blog->settings->comments_pub ? 1 : -1;
179                    $cur->comment_ip = http::realIP();
180                   
181                    $redir = $_ctx->posts->getURL();
182                    $redir .= strpos($redir,'?') !== false ? '&' : '?';
183                   
184                    try
185                    {
186                         if (!text::isEmail($cur->comment_email)) {
187                              throw new Exception(__('You must provide a valid email address.'));
188                         }
189
190                         # --BEHAVIOR-- publicBeforeCommentCreate
191                         $core->callBehavior('publicBeforeCommentCreate',$cur);
192                         if ($cur->post_id) {                         
193                              $comment_id = $core->blog->addComment($cur);
194                         
195                              # --BEHAVIOR-- publicAfterCommentCreate
196                              $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
197                         }
198                         
199                         if ($cur->comment_status == 1) {
200                              $redir_arg = 'pub=1';
201                         } else {
202                              $redir_arg = 'pub=0';
203                         }
204                         
205                         header('Location: '.$redir.$redir_arg);
206                         exit;
207                    }
208                    catch (Exception $e)
209                    {
210                         $_ctx->form_error = $e->getMessage();
211                         $_ctx->form_error;
212                    }
213               }
214          }
215         
216          # The entry
217          self::serveDocument('post.html');
218     }
219}
220
221class pageMakerTpl
222{
223     public static function EntryContent($attr)
224     {
225          $urls = '0';
226          if (!empty($attr['absolute_urls'])) {
227               $urls = '1';
228          }
229         
230          $f = $GLOBALS['core']->tpl->getFilters($attr);
231         
232          $res = '';
233         
234         
235          if (!empty($attr['full'])) {
236               $res = '<?php echo '.sprintf($f,
237                    '$_ctx->posts->getExcerpt('.$urls.')." ".$_ctx->posts->getContent('.$urls.')').'; ?>';
238          } else {
239               $res = '<?php echo '.sprintf($f,'$_ctx->posts->getContent('.$urls.')').'; ?>';
240          }
241         
242          $res .= "<?php if (\$core->blog->settings->pagemaker_post_auto_insert) : ?>\n";
243          $res .= pageMakerTpl::PostPagination($attr);
244          $res .= "<?php endif; ?>\n";
245
246          return $res;
247     }
248     
249     public static function Comments($attr,$content)
250     {
251          $p =
252          "if (\$_ctx->posts !== null) { ".
253               "\$params['post_id'] = \$_ctx->posts->post_id; ".
254               "\$core->blog->withoutPassword(false);\n".
255          "}\n";
256         
257          if (empty($attr['with_pings'])) {
258               $p .= "\$params['comment_trackback'] = false;\n";
259          }
260         
261          $lastn = 0;
262          if (isset($attr['lastn'])) {
263               $lastn = abs((integer) $attr['lastn'])+0;
264          }
265         
266          if ($lastn > 0) {
267               $p .= "\$params['limit'] = ".$lastn.";\n";
268          } else {
269               $p .= "if (\$_ctx->nb_comment_per_page !== null) { \$params['limit'] = \$_ctx->nb_comment_per_page; }\n";
270          }
271         
272          if (empty($attr['no_context']))
273          {
274               $p .=
275               'if ($_ctx->exists("categories")) { '.
276                    "\$params['cat_id'] = \$_ctx->categories->cat_id; ".
277               "}\n";
278               
279               $p .=
280               'if ($_ctx->exists("langs")) { '.
281                    "\$params['sql'] = \"AND P.post_lang = '\".\$core->blog->con->escape(\$_ctx->langs->post_lang).\"' \"; ".
282               "}\n";
283          }
284         
285          $order = 'asc';
286          if (isset($attr['order']) && preg_match('/^(desc|asc)$/i',$attr['order'])) {
287               $order = $attr['order'];
288          }
289         
290          $p .= "\$params['order'] = 'comment_dt ".$order."';\n";
291         
292          if (isset($attr['no_content']) && $attr['no_content']) {
293               $p .= "\$params['no_content'] = true;\n";
294          }
295     
296          $p .= 'if ($_ctx->post_comment_current !== null) {'. 
297               $p .= "\$params['limit'] = array(((\$_ctx->post_comment_current-1)*\$core->blog->settings->pagemaker_comment_nb_per_page),\$core->blog->settings->pagemaker_comment_nb_per_page); ";
298          $p .= "}\n";
299         
300          $res = "<?php\n";
301          $res .= $p;
302          $res .= '$_ctx->comments = $core->blog->getComments($params); unset($params);'."\n";
303          $res .= "if (\$_ctx->posts !== null) { \$core->blog->withoutPassword(true);}\n";
304         
305          if (!empty($attr['with_pings'])) {
306               $res .= '$_ctx->pings = $_ctx->comments;'."\n";
307          }
308         
309          $res .= "?>\n";
310         
311          $res .= '<?php while ($_ctx->comments->fetch()) : ?>'.$content.'<?php endwhile; ?>';
312          $res .= "<?php if (\$core->blog->settings->pagemaker_comment_auto_insert) : ?>\n";
313          $res .= pageMakerTpl::CommentPagination($attr);
314          $res .= "<?php endif; ?>\n";
315          $res .= '<?php $_ctx->comments = null; ?>';
316         
317          return $res;
318     }
319     
320     public static function CommentOrderNumber($attr)
321     {
322          return '<?php echo $_ctx->comments->index()+1+((\$_ctx->post_comment_current-1)*\$core->blog->settings->pagemaker_comment_nb_per_page); ?>';
323     }
324     
325     public static function PostPagination($attr)
326     {
327          $f = $GLOBALS['core']->tpl->getFilters($attr);
328         
329          $max = isset($attr['max']) ? (int) $attr['max'] : 20;
330         
331          $p = "\$params = array();\n";
332          if (isset($attr['current'])) {
333               $p .= "\$params['current'] = '".$attr['current']."';\n";
334          }
335          if (isset($attr['prev'])) {
336               $p .= "\$params['prev'] = '".$attr['prev']."';\n";
337          }
338          if (isset($attr['next'])) {
339               $p .= "\$params['next'] = '".$attr['next']."';\n";
340          }
341         
342          $p .= "\$params['type'] = 'post';\n";
343         
344          $res = "<?php\n";
345          $res .= $p;
346          $res .= "\$pager = new pageMakerPager(\$_ctx->post_page_current,\$_ctx->post_page_count,".$max.");\n";
347          $res .= "\$pager->init(\$params);\n";
348          $res .= "echo ".sprintf($f,'$pager->getLinks()').";\n";
349          $res .= "?>\n";
350         
351          return $res;
352     }
353
354     public static function CommentPagination($attr)
355     {
356          $f = $GLOBALS['core']->tpl->getFilters($attr);
357         
358          $max = isset($attr['max']) ? (int) $attr['max'] : 20;
359         
360          $p = "\$params = array();\n";
361          if (isset($attr['current'])) {
362               $p .= "\$params['current'] = '".$attr['current']."';\n";
363          }
364          if (isset($attr['prev'])) {
365               $p .= "\$params['prev'] = '".$attr['prev']."';\n";
366          }
367          if (isset($attr['next'])) {
368               $p .= "\$params['next'] = '".$attr['next']."';\n";
369          }
370         
371          $p .= "\$params['type'] = 'comment';\n";
372         
373          $res = "<?php\n";
374          $res .= $p;
375          $res .= "\$pager = new pageMakerPager(\$_ctx->post_comment_current,\$_ctx->post_comment_count,".$max.");\n";
376          $res .= "\$pager->init(\$params);\n";
377          $res .= "echo ".sprintf($f,'$pager->getLinks()').";\n";
378          $res .= "?>\n";
379         
380          return $res;
381     }
382}
383
384?>
Note: See TracBrowser for help on using the repository browser.

Sites map