CMS  Version 3.9
article.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 
39 Fakoli::using("blog", "user");
40 
44 class Article extends DataItem implements Searchable
45 {
46  var $fields = array("article_id" => Number,
47  "title" => String,
48  "message" => HTML,
49  "article_type" => String,
50  "identifier" => String,
51  "image_id" => Number,
52  "teaser" => HTML,
53  "created_date" => Date,
54  "post_date" => Date,
55  "archive_date" => Date,
56  "publish_date" => Date,
57  "last_modified" => Timestamp,
58  "published" => Boolean,
59  "allow_comment" => Boolean,
60  "headline" => Boolean,
61  "tags" => String,
62  "author_id" => Number,
63  "author" => String,
64  "publication" => String,
65  "sort_order" => Number
66  );
67 
68  var $relations = array( "Image" => ImageRecord,
69  "Sites" => Site,
70  "Comments" => Comment,
71  "Author" => "",
72  "Blog" => Blog,
73  );
74 
75  var $fieldAliases = array("message" => "Article Content");
76 
77  var $versioned_fields = array("title", "message", "image_id", "teaser", "tags", "author", "publication");
78 
79  function Image()
80  {
81  return $this->getRelated(ImageRecord);
82  }
83 
84  function Sites($constraint = "")
85  {
86  return $this->crossReference(Site, ArticleSiteXref, $constraint);
87  }
88 
89  function Comments($constraint = "")
90  {
91  return $this->crossReference(Comment, ArticleCommentXref, $constraint);
92  }
93 
101  function Blog()
102  {
103  $blogs = Query::create(Blog, "WHERE title=:article_type")
104  ->bind(":article_type", $this->article_type)
105  ->execute();
106 
107  return count($blogs) ? $blogs[0] : null;
108  }
109 
110  function Author()
111  {
112  $mgr = new UserManager();
113  return $mgr->getUser($this->author_id);
114  }
115 
116  function getAuthorName()
117  {
118  if ($this->author_id)
119  {
120  return $this->Author()->getFullName();
121  }
122  else return $this->author;
123  }
124 
125  function getBlog()
126  {
127  $blogs = Query::create(Blog, "WHERE title=:type")
128  ->bind(":type", $this->article_type)
129  ->execute();
130 
131  return count($blogs) ? $blogs[0] : null;
132  }
133 
134  static function fromIdentifier($identifier)
135  {
136  $articles = Query::create(Article, "WHERE identifier=:i")
137  ->bind(":i", $identifier)
138  ->execute();
139 
140  return count($articles) > 0 ? $articles[0] : null;
141  }
142 
143  function getURL()
144  {
145  $blog = $this->getBlog();
146  if($blog)
147  {
148  return $blog->getURL() . "?article_id=" . $this->article_id;
149  }
150  else
151  {
152  return "article?article_id=" . $this->article_id;
153  }
154  }
155 
157  {
158  $blogSubscribers = Query::create(BlogSubscriber, "where blog_id=:blog_id")
159  ->bind(":blog_id", $this->getBlog()->blog_id)
160  ->execute();
161  $email_list_array = array();
162  foreach($blogSubscribers as $blogSubscriber)
163  {
164  $email_list_array[] = $blogSubscriber->subscriber_email;
165  }
166  $email_list = implode(",", $email_list_array);
167 
168  return $email_list;
169  }
170 
171  function getViewIdentifier()
172  {
173  try
174  {
175  $blog = $this->getBlog();
176  return $blog->identifier;
177  }
178  catch(DataNotFoundException $e)
179  {
180  return "article_view";
181  }
182  }
183 
184  function formatPostDate()
185  {
186  if ($this->post_date)
187  {
188  return $this->format("{post_date:daylong}");
189  }
190 
191  if ($this->publish_date)
192  {
193  return $this->format("{publish_date:daylong}");
194  }
195 
196  return $this->format("{created_date:daylong}");
197  }
198 
199  function Article()
200  {
201  $this->table = "article";
202  $this->primary_key = "article_id";
203  $this->pretty_class_name = "Article";
204 
205  // Patch in the user class, since this can be overridden by the application
206  $mgr = new UserManager();
207  $this->relations["Owner"] = $mgr->getUserClass();
208 
209  $this->DataItem(func_get_args());
210  }
211 
212  function search($params, $range = null)
213  {
214  trace("Searching Articles", 3);
215 
216  if ($range)
217  {
218  $range = " AND {$this->primary_key} IN (".implode($range, ", ").")";
219  }
220  else
221  {
222  $range = "";
223  }
224 
225  if (is_object($params))
226  {
227  $search = clone $params;
228  $search->target = $this;
229 
230  $search->remapField("keywords", "tags");
231 
232  if (!$search->get("text", "like"))
233  {
234  $search->secondaryFields("tags", "message");
235  }
236  else
237  {
238  $search->remapField("text", "message");
239  }
240 
241  $search->remapField("publication_date", "publish_date");
242 
243  $constraint = $search->generateConstraint();
244  $constraint .= $constraint ? " AND published=1" : " WHERE published=1";
245 
246  $constraint .= $range;
247 
248  $articles = Query::create(Article, $constraint)
249  ->execute();
250  }
251  else
252  {
253  $params = "%$params%";
254  $articles = Query::create(Article, "WHERE (title like :a OR teaser LIKE :b OR message like :c) AND published=1 $range")
255  ->bind(":a", $params, ":b", $params, ":c", $params)
256  ->execute();
257  }
258 
260  }
261 }
262 
264 {
265  var $item;
266 
268  {
269  $this->item = $item;
270  }
271 
272  function getPrimaryKey() { return $this->item->getPrimaryKey(); }
273  function get($field) { return $this->item->get($field); }
274  function prettifyClassName($plural = false) { return $this->item->prettifyClassName($plural); }
275  function format($format) { return $this->item->format($format); }
276 
277  function relevance()
278  {
279  return 0.5;
280  }
281 
282  function title()
283  {
284  return $this->item->title;
285  }
286 
287  function date()
288  {
289  return $this->item->created_date;
290  }
291 
292  function summary()
293  {
294  $details = "<p><em>{author} {created_date:short}</em></p>";
295  $href = "href='{getViewIdentifier()}?article_id={article_id}'";
296 
297  if(Settings::getValue("search", "show_text_fragment"))
298  {
299  return $this->item->format("<h4>{title}</h4>{$details}{teaser}<a {$href}>Read More</a>");
300  }
301  else
302  {
303  return $this->item->format("<h4><a {$href}>{title}</a></h4>{$details}");
304  }
305  }
306 }
307 
308 class ArticleSiteXref extends DataItem
309 {
310  var $fields = array("article_site_xref_id" => Number,
311  "article_id" => Number,
312  "site_id" => Number);
313 
314  var $relations = array( "Article" => Article,
315  "Site" => Site);
316 
317  function Article()
318  {
319  return $this->getRelated(Article);
320  }
321 
322  function Site()
323  {
324  return $this->getRelated(Site);
325  }
326 
327  function ArticleSiteXref()
328  {
329  $this->table = "article_site_xref";
330  $this->primary_key = "article_site_xref_id";
331  $this->DataItem(func_get_args());
332  }
333 }
334 
335 class ArticleCommentXref extends DataItem
336 {
337  var $fields = array("article_comment_xref_id" => Number,
338  "article_id" => Number,
339  "comment_id" => Number);
340 
341  var $relations = array( "Article" => Article,
342  "Comment" => Comment);
343 
345  {
346  if (!$article_id) return false;
347 
348  try
349  {
350  $article = $this->Article();
351  // returns null if no related blog
352  $blog = $article->getBlog();
353 
354  return ($blog) ? $blog->allow_comments : $article->allow_comment;
355  }
356  catch(DataNotFoundException $e)
357  {
358  return false;
359  }
360  }
361 
362  function getURL()
363  {
364  if(!$this->article_id) return "";
365 
366  $url = $this->Article()->getUrl();
367 
368  return $url . $this->format("#comment{comment_id}");
369  }
370 
371  function Article()
372  {
373  return $this->getRelated(Article);
374  }
375 
376  function Comment()
377  {
378  return $this->getRelated(Comment);
379  }
380 
382  {
383  $this->table = "article_comment_xref";
384  $this->primary_key = "article_comment_xref_id";
385  $this->DataItem(func_get_args());
386  }
387 }
388 
389 
390 class ArticleSolrAdapter // Implements ISolrAdapter
391 {
392 
393  function getClass()
394  {
395  return Article;
396  }
397 
398  function getFilter()
399  {
400  return "WHERE published=1 AND publish_date <= curdate()";
401  }
402 
403  function getTitleFormat()
404  {
405  return "{title:xml}";
406  }
407 
408  function getContentFormat()
409  {
410  return "{teaser:xml} {message:xml}";
411  }
412 
413  function getAuthorFormat()
414  {
415  return "{author:xml}";
416  }
417 
418  function getKeywordFormat()
419  {
420  return "{tags:xml}";
421  }
422 
423  function wrap($item)
424  {
425  return new ArticleSearchResult($item);
426  }
427 }
428 ?>
$constraint
$article publish_date
$blog
$article article_type
$range
Definition: error_log.inc:13
Definition: article.inc:336
Comment()
Definition: article.inc:376
verifyEnabled($article_id)
Definition: article.inc:344
$fields
Definition: article.inc:337
ArticleCommentXref()
Definition: article.inc:381
Article()
Definition: article.inc:371
$relations
Definition: article.inc:341
getURL()
Definition: article.inc:362
Defines the Article class.
Definition: article.inc:45
formatPostDate()
Definition: article.inc:184
Author()
Definition: article.inc:110
Image()
Definition: article.inc:79
$relations
Definition: article.inc:68
getBlog()
Definition: article.inc:125
search($params, $range=null)
DataItems must implement this method to provide search functionality.
Definition: article.inc:212
Comments($constraint="")
Definition: article.inc:89
$fields
Definition: article.inc:46
Blog()
We retrieve the blog related to the article by matching the article_type field to the blog's title fi...
Definition: article.inc:101
$versioned_fields
Definition: article.inc:77
getViewIdentifier()
Definition: article.inc:171
getAuthorName()
Definition: article.inc:116
Sites($constraint="")
Definition: article.inc:84
Article()
Definition: article.inc:199
getURL()
Definition: article.inc:143
getBlogSubscribers()
Definition: article.inc:156
static fromIdentifier($identifier)
Definition: article.inc:134
$fieldAliases
Definition: article.inc:75
Definition: article.inc:264
getPrimaryKey()
Definition: article.inc:272
$item
Definition: article.inc:265
ArticleSearchResult($item)
Definition: article.inc:267
summary()
Display the item title and any other essential details for the item such as author and a create date.
Definition: article.inc:292
relevance()
Definition: article.inc:277
prettifyClassName($plural=false)
Definition: article.inc:274
format($format)
Definition: article.inc:275
title()
Definition: article.inc:282
date()
Definition: article.inc:287
Definition: article.inc:309
$relations
Definition: article.inc:314
Article()
Definition: article.inc:317
ArticleSiteXref()
Definition: article.inc:327
Site()
Definition: article.inc:322
$fields
Definition: article.inc:310
Definition: article.inc:391
getClass()
Definition: article.inc:393
getKeywordFormat()
Definition: article.inc:418
getFilter()
Definition: article.inc:398
getAuthorFormat()
Definition: article.inc:413
getTitleFormat()
Definition: article.inc:403
wrap($item)
Definition: article.inc:423
getContentFormat()
Definition: article.inc:408
Definition: blog.inc:41
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static wrap($items, $resultClass)
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
Definition: site.inc:40
Provides the interface to the user model for the application.
The Searchable interface must be implemented by any DataItem classes that want to be searchable via t...
Definition: searchable.inc:45
if(! $article_id) $article
Definition: article.inc:44
$article_id
Definition: article.inc:37
$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
$blogSubscriber blog_id