Dotclear

source: plugins/periodical/_public.php @ 2160

Revision 2160, 4.1 KB checked in by JcDenis, 14 years ago (diff)

periodical 0.1:

  • First lab release
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of periodical, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009-2010 JC Denis and contributors
6# jcdenis@gdwd.com
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# Publish periodical only on home page and feeds
16if (!$core->plugins->moduleExists('metadata') || in_array($core->url->type,array('default','feed'))) {
17     $core->addBehavior('publicBeforeDocument',array('publicBehaviorPeriodical','publishPeriodicalEntries'));
18}
19
20class publicBehaviorPeriodical
21{
22     public static function publishPeriodicalEntries($core)
23     {
24          $per = new periodical($core);
25
26          # Get periods
27          $periods =  $core->auth->sudo(array($per,'getPeriods'));
28
29          # No period
30          if ($periods->isEmpty()) {
31               return;
32          }
33
34          $now = dt::toUTC(time());
35          $posts_order = $core->blog->settings->periodical_pub_order;
36          if (!preg_match('/^(post_dt|post_creadt|post_id) (asc|desc)$/',$posts_order)) {
37               $posts_order = 'post_dt asc';
38          }
39          $cur_period = $core->con->openCursor($core->prefix.'periodical');
40
41          while($periods->fetch())
42          {
43               # Check if period is ongoing
44               $cur_tz = strtotime($periods->periodical_curdt);
45               $end_tz = strtotime($periods->periodical_enddt);
46               $now_tz = $now + dt::getTimeOffset($periods->periodical_tz,$now);
47               if ($now_tz > $cur_tz && $now_tz < $end_tz)
48               {
49                    $last_nb = 0;
50                    $last_tz = $cur_tz;
51
52                    $max_nb = $periods->periodical_pub_nb;
53                    $max_tz = $end_tz < $now_tz ? $end_tz : $now_tz;
54
55                    # Calculate nb of posts to get
56                    $loop_tz = $cur_tz;
57                    $limit = 0;
58                    try {
59                         while(1)
60                         {
61                              if ($loop_tz > $max_tz) {
62                                   break;
63                              }
64                              $loop_tz = $per->getNextTime($loop_tz,$periods->periodical_pub_int);
65                              $limit += 1;
66                         }
67                    }
68                    catch (Exception $e) { }
69
70                    # If period need update
71                    if ($limit > 0)
72                    {
73                         # Get posts to publish related to this period
74                         $posts_params = array();
75                         $posts_params['periodical_id'] = $periods->periodical_id;
76                         $posts_params['post_status'] = '-2';
77                         $posts_params['order'] = $posts_order;
78                         $posts_params['limit'] = $limit * $max_nb;
79                         $posts_params['no_content'] = true;
80                         $posts =  $core->auth->sudo(array($per,'getPosts'),$posts_params);
81
82                         if (!$posts->isEmpty()) {
83
84                              $cur_post = $core->con->openCursor($core->prefix.'post');
85
86                              while($posts->fetch())
87                              {
88                                   # Publish post with right date
89                                   $cur_post->clean();
90                                   $cur_post->post_status = 1;
91
92                                   # Update post date with right date
93                                   if ($core->blog->settings->periodical_upddate) {
94                                        $cur_post->post_dt = date('Y-m-d H:i:s',$last_tz);
95                                        $cur_post->post_tz = $periods->periodical_tz;
96                                   }
97                                   else {
98                                        $cur_post->post_dt = $posts->post_dt;
99                                   }
100
101                                   # Also update post url with right date
102                                   if ($core->blog->settings->periodical_updurl) {
103                                        $cur_post->post_url = $core->blog->getPostURL('',$cur_post->post_dt,$posts->post_title,$posts->post_id);
104                                   }
105
106                                   $cur_post->update(
107                                        'WHERE post_id = '.$posts->post_id.' '.
108                                        "AND blog_id = '".$core->con->escape($core->blog->id)."' "
109                                   );
110
111                                   # Delete post relation to this period
112                                   $per->delPost($posts->post_id);
113
114                                   $last_nb++;
115
116                                   # Increment upddt if nb of publishing is to the max
117                                   if ($last_nb == $max_nb) {
118                                        $last_tz = $per->getNextTime($last_tz,$periods->periodical_pub_int);
119                                        $last_nb = 0;
120                                   }
121                              }
122
123                              $core->blog->triggerBlog();
124                         }
125                    }
126
127                    # Update last published date of this period even if there's no post to publish
128                    $cur_period->clean();
129                    $cur_period->periodical_curdt = date('Y-m-d H:i:s',$loop_tz);
130                    $cur_period->update(
131                         'WHERE periodical_id = '.$periods->periodical_id.' '.
132                         "AND blog_id = '".$core->con->escape($core->blog->id)."' "
133                    );
134               }
135          }
136     }
137}
138?>
Note: See TracBrowser for help on using the repository browser.

Sites map