CMS  Version 3.9
blog.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 Fakoli::using("search");
39 
40 class Blog extends DataItem // implements Searchable
41 {
42  // Fields
43  var $fields = array("blog_id" => Number,
44  "title" => String,
45  "identifier" => String,
46  "image_id" => Number,
47  "description" => HTML,
48  "owner_id" => Number,
49  "read_access" => String,
50  "write_access" => String,
51  "default_article_order" => String,
52  "articles_per_page" => Number,
53  "blog_type" => String,
54  "allow_comments" => Boolean,
55  "comment_moderators" => String,
56  "published" => Boolean,
57  "created_date" => DateTime,
58  "allow_subscriptions" => Boolean,
59  "enable_rss_feed" => Boolean,
60  "max_rss_articles" => Number);
61 
62  var $versioned_fields = array("title", "description", "image_id");
63 
64  // Relations
65 
66  var $relations = array(
67  "Owner" => "",
68  "Articles" => Article,
69  "Image" => ImageRecord,
70  "Subscribers" => BlogSubscriber
71  );
72 
73  function Owner()
74  {
75  $mgr = new UserManager();
76  return $mgr->getUser($this->owner_id);
77  }
78 
79  function getOwnerName()
80  {
81  return $this->Owner()->getFullName();
82  }
83 
84  function Image()
85  {
86  return $this->getRelated(ImageRecord);
87  }
88 
89  function Subscribers($constraint = "")
90  {
91  return $this->getRelatedList(BlogSubscriber, "", $constraint);
92  }
93 
94  function allowDelete()
95  {
96  if(!checkRole("admin"))
97  {
98  return false;
99  }
100 
101  $articles = Query::create(Article, "WHERE article_type=:article_type")
102  ->bind(":article_type", $this->title)
103  ->executeValue("COUNT(1)");
104 
105  return $articles > 0 ? false : true;
106  }
107 
109  {
110  $constraint = "";
111  switch($this->default_article_order)
112  {
113  case "DESC":
114  $constraint = " ORDER BY publish_date DESC";
115  break;
116 
117  case "ASC":
118  $constraint = " ORDER BY publish_date ASC";
119  break;
120 
121  case "SORT":
122  default:
123  $constraint = " ORDER BY sort_order";
124  break;
125  }
126 
127  return $constraint;
128  }
129 
130  function Articles($constraint = "")
131  {
132  if (!$constraint || stripos($constraint, "ORDER") === false)
133  {
135  }
136 
137  $constraint = preg_replace("/^\s*WHERE\s+/i", "AND ", $constraint);
138 
139  $constraint = "WHERE article_type=:a $constraint";
140 
141  $articles = Query::create(Article, $constraint)
142  ->bind(":a", $this->title)
143  ->execute();
144 
145  return $articles;
146  }
147 
148  function countArticles()
149  {
150  return Query::create(Article, "WHERE article_type=:a")
151  ->bind(":a", $this->title)
152  ->executeValue("COUNT(1)");
153  }
154 
155  static function findByIdentifier($identifier)
156  {
157  $match = Query::create(Blog, "WHERE identifier=:id")
158  ->bind(":id", $identifier)
159  ->executeSingle();
160 
161  return $match;
162  }
163 
164  static function findByTitle($title)
165  {
166  $match = Query::create(Blog, "WHERE title=:title")
167  ->bind(":title", $title)
168  ->executeSingle();
169 
170  return $match;
171  }
172 
173 
177  function getURL()
178  {
179  global $config;
180 
181  $url = "";
182 
183  if ($this->identifier)
184  {
185  $contents = SectionContent::getMappedContent($this->identifier);
186  if (count($contents))
187  {
188  $section = $contents[0]->Section();
189 
190  $url = $section->getSectionURL() . $this->identifier;
191  }
192  }
193 
194  return $url;
195  }
196 
197  static function getArticleTypeOptions()
198  {
199  $blogs = Query::create(Blog, "ORDER BY title")->execute();
200  $options = array();
201  foreach($blogs as $blog)
202  {
203  $options[$blog->title] = $blog->title;
204  }
205 
206  return $options;
207  }
208 
209 
211  {
212  $articles = $this->Articles("WHERE published=1 ORDER BY sort_order, title");
213  $links = array();
214  foreach($articles as $article)
215  {
216  $links[$article->title] = $this->identifier."?article_id=".$article->article_id;
217  }
218 
219  return $links;
220  }
221 
222  function Blog()
223  {
224  $this->primary_key = "blog_id";
225  $this->table = "blog";
226 
227  $this->default_format = "{title}";
228 
229  // Patch in the user class, since this can be overridden by the application
230  $mgr = new UserManager();
231  $this->relations["Owner"] = $mgr->getUserClass();
232 
233  $this->DataItem(func_get_args());
234  }
235 
236  // Searchable
237 
238  function search($params, $range = null)
239  {
240  trace("Searching Blogs", 3);
241 
242  if ($range)
243  {
244  $range = " AND {$this->primary_key} IN (".implode($range, ", ").")";
245  }
246 
247  if (is_object($params))
248  {
249  $search = clone $params;
250  $search->target = $this;
251 
252  $search->remapField("text", "description");
253 
254  $constraint = $search->generateConstraint();
255  $constraint .= $constraint ? " AND published=1" : " WHERE published=1";
256 
257  $constraint .= $range;
258 
259  $blogs = Query::create(Blog, $constraint)->execute();
260  }
261  else
262  {
263  $params = "%$params%";
264  $blogs = Query::create(Blog, "WHERE (title like :a OR description LIKE :b) AND published=1 $range")
265  ->bind(":a", $params, ":b", $params)
266  ->execute();
267  }
268 
270  }
271 }
272 
274 {
275  var $item;
276 
278  {
279  $this->item = $item;
280  }
281 
282  function getPrimaryKey() { return $this->item->getPrimaryKey(); }
283  function get($field) { return $this->item->get($field); }
284  function prettifyClassName($plural = false) { return $this->item->prettifyClassName($plural); }
285  function format($format) { return $this->item->format($format); }
286 
287  function relevance()
288  {
289  return 0.5;
290  }
291 
292  function title()
293  {
294  return $this->item->title;
295  }
296 
297  function date()
298  {
299  return $this->item->created_date;
300  }
301 
302  function summary()
303  {
304  if(Settings::getValue("search", "show_text_fragment"))
305  {
306  return $this->item->format("<h4>{title}</h4><p>{description:200}</p><a href='{getURL()}'>Read More</a>");
307  }
308  else
309  {
310  return $this->item->format("<h4><a href='{getURL()}'>{title}</a></h4>");
311  }
312  }
313 }
314 
315 
316 class BlogSolrAdapter // Implements ISolrAdapter
317 {
318 
319  function getClass()
320  {
321  return Blog;
322  }
323 
324  function getFilter()
325  {
326  return "WHERE published=1";
327  }
328 
329  function getTitleFormat()
330  {
331  return "{title:xml}";
332  }
333 
334  function getContentFormat()
335  {
336  return "{description:xml}";
337  }
338 
339  function getAuthorFormat()
340  {
341  return "{Owner.last_name:xml}";
342  }
343 
344  function getKeywordFormat()
345  {
346  return "";
347  }
348 
349  function wrap($item)
350  {
351  return new BlogSearchResult($item);
352  }
353 }?>
$constraint
$article
$calendar owner_id
return false
$range
Definition: error_log.inc:13
$section
Definition: event_form.inc:44
$bookmark title
Defines the Article class.
Definition: article.inc:45
Definition: blog.inc:41
$versioned_fields
Definition: blog.inc:62
static getArticleTypeOptions()
Definition: blog.inc:197
$fields
Definition: blog.inc:43
static findByTitle($title)
Definition: blog.inc:164
enumerateContentLinks()
Definition: blog.inc:210
Subscribers($constraint="")
Definition: blog.inc:89
getOwnerName()
Definition: blog.inc:79
Articles($constraint="")
Definition: blog.inc:130
countArticles()
Definition: blog.inc:148
Image()
Definition: blog.inc:84
Blog()
Definition: blog.inc:222
getSortOrderConstraint()
Definition: blog.inc:108
search($params, $range=null)
Definition: blog.inc:238
allowDelete()
Definition: blog.inc:94
static findByIdentifier($identifier)
Definition: blog.inc:155
$relations
Definition: blog.inc:66
getURL()
Determines the URL for the blog.
Definition: blog.inc:177
Owner()
Definition: blog.inc:73
BlogSearchResult($item)
Definition: blog.inc:277
summary()
Display the item title and any other essential details for the item such as author and a create date.
Definition: blog.inc:302
format($format)
Definition: blog.inc:285
prettifyClassName($plural=false)
Definition: blog.inc:284
getAuthorFormat()
Definition: blog.inc:339
getContentFormat()
Definition: blog.inc:334
getTitleFormat()
Definition: blog.inc:329
wrap($item)
Definition: blog.inc:349
getKeywordFormat()
Definition: blog.inc:344
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static wrap($items, $resultClass)
static getMappedContent($identifier)
Definition: section.inc:324
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
Provides the interface to the user model for the application.
global $config
Definition: import.inc:4
$blog
Definition: blog.inc:44
$articles
Definition: rss.inc:62
$identifier
Definition: rss.inc:37
if(! $blog->published||! $blog->enable_rss_feed||!checkRole($blog->allow_read)) $url
Definition: rss.inc:58