public
Description: Blog Modules for CMS Made Simple 2.0
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsms-blog.git
wishy (author)
Tue Apr 08 07:38:46 -0700 2008
commit  152d572f05e09fde11a744326fe200ced0fe35a7
tree    97757d18effd003c36428e9c00af1e6e7a98efcd
parent  3aef452680351b0d4f2343fd4fbd0aa68bbf9255
cmsms-blog / Blog.module.php
100644 291 lines (256 sloc) 7.608 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
 
class Blog extends CmsModuleBase
{
  function __construct()
  {
    parent::__construct();
  }
  
  function get_name()
  {
    return 'Blog';
  }
  
  function get_version()
  {
    return '0.1';
  }
  
  function is_plugin_module()
  {
    return true;
  }
 
  function has_admin()
  {
    return true;
  }
  
  function minimum_core_version()
  {
    return '2.0-svn';
  }
  
  function get_admin_description()
  {
    return $this->Lang('description');
  }
 
  function get_admin_section()
  {
    return 'content';
  }
  
  function setup()
  {
    $this->register_data_object('BlogPost');
    $this->register_data_object('BlogCategory');
    
    $this->register_module_plugin('blog');
 
    $this->register_route('/blog\/rss\.xml$/', array('action' => 'default', 'rss' => true, 'showtemplate' => false));
    $this->register_route('/blog\/category\/(?P<category>[a-zA-Z\-_\ ]+)\.xml$/', array('action' => 'list_by_category', 'rss' => true, 'showtemplate' => false));
    $this->register_route('/blog\/category\/(?P<category>[a-zA-Z\-_\ ]+)$/', array('action' => 'list_by_category'));
    $this->register_route('/blog\/(?P<url>[0-9]{4}\/[0-9]{2}\/[0-9]{2}\/.*?)$/', array('action' => 'detail'));
    $this->register_route('/blog\/(?P<year>[0-9]{4})$/', array('action' => 'filter_list', 'month' => '-1', 'day' => '-1'));
    $this->register_route('/blog\/(?P<year>[0-9]{4})\/(?P<month>[0-9]{2})$/', array('action' => 'filter_list', 'day' => '-1'));
    $this->register_route('/blog\/(?P<year>[0-9]{4})\/(?P<month>[0-9]{2})\/(?P<day>[0-9]{2})$/', array('action' => 'filter_list'));
    
    $this->add_xmlrpc_method('getRecentPosts', 'metaWeblog');
    $this->add_xmlrpc_method('getCategories', 'metaWeblog');
    $this->add_xmlrpc_method('getPost', 'metaWeblog');
    $this->add_xmlrpc_method('newPost', 'metaWeblog');
    $this->add_xmlrpc_method('editPost', 'metaWeblog');
    
    $this->add_temp_event_handler('Core', 'HeaderTagRender', 'handle_header_callback');
    $this->add_temp_event_handler('Core', 'SearchReindex', 'reindex');
  }
  
  public function handle_header_callback($modulename, $eventname, &$params)
  {
    //We only add this link if another action hasn't done so already -- view by category does it's own thing, for example.
    if (!$this->has_added_header_text())
      $params['content'] .= '<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="' . $this->generate_rss_link() . '" />' . "\n";
  }
  
  protected function generate_rss_link($params = array(), $category = '')
  {
    $pretty_url = 'blog/rss.xml';
    if ($category != '')
    {
      $pretty_url = 'blog/category/' . $category . '.xml';
      if (!array_key_exists('category', $params))
        $params['category'] = $category;
    }
    return $this->create_link('cntnt01', 'rss', $this->get_default_return_id(), '', $params, '', true, false, '', false, $pretty_url);
  }
  
  private function get_default_return_id()
  {
    return cmsms()->get('pageinfo')->return_id;
  }
  
  public function getRecentPosts($blog_id, $username, $password, $number_of_posts)
  {
    $posts = cms_orm('BlogPost')->find_all(array('limit' => array(0, $number_of_posts), 'order' => 'id DESC'));
    $result = array();
    foreach ($posts as $post)
    {
      $result[] = $post->create_xmlrpc_array();
    }
    return $result;
  }
  
  public function getPost($post_id, $username, $password)
  {
    $post = cms_orm('BlogPost')->find_by_id($post_id);
    if ($post != null)
      return $post->create_xmlrpc_array();
    return null;
  }
  
  public function getCategories($blog_id, $username, $password)
  {
    return array_values($this->get_categories());
  }
  
  public function newPost($blog_id, $username, $password, $struct, $publish)
  {
    $post = new BlogPost();
    $post->title = $struct['title'];
    $post->content = $struct['description'];
    $post->status = ($publish ? 'publish' : 'draft');
    if ($post->save())
    {
      if (isset($struct['categories']))
      {
        foreach ($struct['categories'] as $one_cat)
        {
          $post->set_category_by_name($one_cat);
        }
      }
      return $post->id;
    }
    return null;
  }
  
  public function editPost($post_id, $username, $password, $struct, $publish)
  {
    $post = cms_orm('BlogPost')->find_by_id($post_id);
    if ($post != null)
    {
      $post->title = $struct['title'];
      $post->content = $struct['description'];
      $post->status = ($publish ? 'publish' : 'draft');
      if ($post->save())
      {
        if (isset($struct['categories']))
        {
          $post->clear_categories();
          foreach ($struct['categories'] as $one_cat)
          {
            $post->set_category_by_name($one_cat);
          }
        }
        return true;
      }
    }
    return null;
  }
  
  public function get_categories($add_any = false)
  {
    $result = array();
    if ($add_any)
    {
      $result[''] = $this->lang('any');
    }
    $categories = cms_orm('BlogCategory')->find_all(array('order' => 'name ASC'));
    if ($categories != null)
    {
      foreach ($categories as $one_category)
      {
        $result[$one_category->id] = $one_category->name;
      }
    }
    return $result;
  }
  
  public function get_statuses($add_any = false)
  {
    $result = array();
    if ($add_any)
    {
      $result[''] = $this->lang('any');
    }
    $result['draft'] = $this->lang('draft');
    $result['publish'] = $this->lang('published');
    return $result;
  }
  
  public function reindex()
  {
    $posts = cms_orm('BlogPost')->find_all_by_status('publish');
    foreach ($posts as $one_post)
    {
      $one_post->index();
    }
  }
  
  public function get_default_summary_template()
  {
    return '{foreach from=$posts item=entry}
<h3><a href="{$entry->url}">{$entry->title}</a></h3>
<small>
  {$entry->post_date}
  {if $entry->author ne null}
    {mod_lang string=by} {$entry->author->full_name()}
  {/if}
</small>
 
<div>
{$entry->get_summary_for_frontend()}
</div>
 
{if $entry->has_more() eq true}
  <a href="{$entry->url}">{mod_lang string=hasmore} &gt;&gt;</a>
{/if}
 
{/foreach}';
  }
  
  public function get_default_detail_template()
  {
    return '{if $post ne null}
<h3>{$post->title}</h3>
<small>
  {$post->post_date}
  {if $post->author ne null}
    {mod_lang string=by} {$post->author->full_name()}
  {/if}
</small>
 
<div>
{$post->content}
</div>
 
<hr />
 
<p>
{cms_module module="comments" module_name="blog" content_id=$post->id}
</p>
 
{else}
{mod_lang string=postnotfound}
{/if}';
  }
  
  public function get_default_rss_template()
  {
    return '<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>{$sitename|escape}</title>
    <link>{root_url}</link>
    {foreach from=$posts item=entry}
    <item>
      <title><![CDATA[{$entry->title}]]></title>
      <link>{$entry->url}</link>
      <guid>{$entry->url}</guid>
      <pubDate>{$entry->post_date}</pubDate>
      <category><![CDATA[]]></category>
      <description><![CDATA[{$entry->get_summary_for_frontend()}]]></description>
    </item>
    {/foreach}
  </channel>
</rss>
';
  }
}
 
# vim:ts=4 sw=4 noet
?>