com.lokorin.lokorin.lib
[ class tree: com.lokorin.lokorin.lib ] [ index: com.lokorin.lokorin.lib ] [ all elements ]

Source for file lib_news.php

Documentation is available at lib_news.php

  1. <?php
  2. /*
  3. * Lokorin.com
  4. * Copyright 2004-2006
  5. * Licensed under the GNU LGPL. See COPYING for full terms.
  6. */
  7. /**
  8. * A library that contains everything that is related to the site's news
  9. * system.
  10. * @author Andreas Launila
  11. * @version $Revision: 1.34 $
  12. * @package com.lokorin.lokorin.lib
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  14. */
  15.  
  16. /**
  17. * For access to common constants and functions.
  18. */
  19. require_once('common.php');
  20. CssHandler::getInstance()->includeCss('news');
  21.  
  22. /**
  23. * Gets all news items that matches the specified selector.
  24. * @param string $sqlSelector The selector that specifies all matching news
  25. * items, e.g. "WHERE id > 5" or "ORDER BY id".
  26. * @return array An array where the key is the order in which the news entries
  27. * were retrieved. The values are News instances.
  28. * @access public
  29. */
  30. function getNews($sqlSelector) {
  31. global $db;
  32. $rows = $db->querySelect(TABLE_NEWS, array('id'), $sqlSelector);
  33. $news = array();
  34. foreach($rows as $row) {
  35. $news[] = new News($row['id']);
  36. }
  37. return $news;
  38. }
  39.  
  40. /**
  41. * Gets the captions of all available news entries.
  42. * @return array Key is news id, value as news title.
  43. * @access public
  44. */
  45. function getAllNewsCaptions() {
  46. global $db;
  47. $rows = $db->querySelect(TABLE_NEWS, array('id', 'caption'));
  48. $captions = array();
  49. foreach($rows as $row) {
  50. $captions[$row['id']] = $row['caption'];
  51. }
  52. return $captions;
  53. }
  54.  
  55. /**
  56. * Describes a news comment.
  57. * @author Andreas Launila
  58. * @package com.lokorin.lokorin.lib
  59. */
  60. class Comment extends TableEntry {
  61. /**
  62. * The unique identifier of the news item that the comment is attached to.
  63. * @var integer
  64. */
  65. private $newsId;
  66. /**
  67. * The author of the comment.
  68. * @var string
  69. */
  70. private $author;
  71. /**
  72. * The contents of the comment.
  73. * @var string
  74. */
  75. private $contents;
  76. /**
  77. * The time when the comment was made.
  78. * @var integer
  79. */
  80. private $timestamp;
  81. /**
  82. * Constructor for Comment.
  83. * @param integer $id The unique identifier for the comment that should be
  84. * created.
  85. */
  86. public function __construct($id) {
  87. $fields = array(
  88. "news_id",
  89. "author",
  90. "contents",
  91. "timestamp"
  92. );
  93. parent::__construct(TABLE_COMMENTS, $id, $fields);
  94. }
  95. /**
  96. * @see com.lokorin.dfcrafters.TableEntry.readRow($row)
  97. */
  98. protected function readRow($row) {
  99. $this->newsId = $row['news_id'];
  100. $this->author = htmlspecialchars($row['author']);
  101. $this->contents = htmlspecialchars($row['contents']);
  102. $this->timestamp = $row['timestamp'];
  103. }
  104. /**
  105. * Gets the news entry that the comment is attached to.
  106. * @return News A new entry.
  107. */
  108. public function getNewsEntry() {
  109. return new News($this->newsId);
  110. }
  111. /**
  112. * Gets the comment as (X)HTML.
  113. * @return string An XHTML compliant representation.
  114. */
  115. public function getAsHTML() {
  116. return '<span class="commentAuthor">'.$this->author.'</span>
  117. <span class="commentDate">('.
  118. date(DATE_COMMENT, $this->timestamp).')</span>
  119. <span class="commentBody">'.$this->contents.'</span>';
  120. }
  121. }
  122.  
  123. /**
  124. * Describes a news item.
  125. * @author Andreas Launila
  126. * @package com.lokorin.lokorin.lib
  127. */
  128. class News extends TableEntry {
  129. /**
  130. * The caption of the news item.
  131. * @var string
  132. */
  133. private $caption;
  134. /**
  135. * The unix timestamp of when the news was submitted.
  136. * @var integer
  137. */
  138. private $timestamp;
  139. /**
  140. * The unix timestamp of when the contents of the news was last generated.
  141. * @var integer
  142. */
  143. private $lastGenerated;
  144. /**
  145. * The actual processed XHTML entry of the news item.
  146. * @var string
  147. */
  148. private $entry;
  149. /**
  150. * The unique identifiers of the projects that the news item is connected
  151. * to.
  152. * @var array
  153. */
  154. private $projectIds;
  155. /**
  156. * Constructor for News.
  157. * @param integer $id The unique identifier of the news entry.
  158. */
  159. public function News($id) {
  160. $fields = array(
  161. 'caption',
  162. 'timestamp',
  163. 'processed_entry',
  164. 'project_id1',
  165. 'project_id2',
  166. 'project_id3',
  167. 'timestamp_generated'
  168. );
  169. parent::__construct(TABLE_NEWS, $id, $fields);
  170. }
  171. /**
  172. * @see com.lokorin.lokorin.TableEntry.readRow($row)
  173. */
  174. protected function readRow($row) {
  175. $this->caption = $row['caption'];
  176. $this->timestamp = $row['timestamp'];
  177. $this->entry = $row['processed_entry'];
  178. $this->projectIds = array();
  179. if($row['project_id3'] != 0) {
  180. $this->projectIds[] = $row['project_id3'];
  181. }
  182. if($row['project_id2'] != 0) {
  183. $this->projectIds[] = $row['project_id2'];
  184. }
  185. if($row['project_id1'] != 0) {
  186. $this->projectIds[] = $row['project_id1'];
  187. }
  188. $this->lastGenerated = $row['timestamp_generated'];
  189. }
  190.  
  191. /**
  192. * Gets the caption of the news entry.
  193. * @return string A news caption.
  194. */
  195. public function getCaption() {
  196. return $this->caption;
  197. }
  198. /**
  199. * Gets the timestamp that represents when the news entry was added.
  200. * @return integer A unix timestamp.
  201. */
  202. public function getTimeAdded() {
  203. return $this->timestamp;
  204. }
  205. /**
  206. * Gets the (X)HTML representing the news entry as it should be displayed
  207. * in a generic enviorment (it includes the project references etc).
  208. * @return string An XHTML compliant representation.
  209. */
  210. public function getAsHTMLGeneric() {
  211. doInclude('lib_projects');
  212. $output = '<div class="news">' .
  213. '<h2>'.$this->caption.'</h2>' .
  214. '<div class="titleFooter">' .
  215. '<ul class="horizontal">' .
  216. '<li class="last"><a href="'.$this->getPermanentUrl().'">Permanent link</a></li>' .
  217. '<li>'.date(DATE_LONG, $this->timestamp).'</li>';
  218. foreach($this->projectIds as $projId) {
  219. try {
  220. $project = new Project($projId);
  221. $output .= '<li>'.$project->getLink().'</li>';
  222. } catch(Exception $e) {}
  223. }
  224. $output .= '</ul></div>' .
  225. $this->getEntryAsHTML().$this->getCommentLink().
  226. '</div>';
  227. return $output;
  228. }
  229. /**
  230. * Gets the (X)HTML representing the news entry as it should be displayed
  231. * in an archive (it includes the project references, but not a comment
  232. * link).
  233. * @return string An XHTML compliant representation.
  234. */
  235. public function getAsHTMLArchive() {
  236. doInclude('lib_projects');
  237. $output = '<div class="news">' .
  238. '<h2>'.$this->caption.'</h2>' .
  239. $this->getEntryAsHTML().
  240. '<div class="newsFooter">' .
  241. '<ul class="horizontal">' .
  242. '<li class="last"><a href="'.$this->getPermanentUrl().'">Permanent link</a></li>' .
  243. '<li>'.date(DATE_LONG, $this->timestamp).'</li>';
  244. foreach($this->projectIds as $projId) {
  245. try {
  246. $project = new Project($projId);
  247. $output .= '<li>'.$project->getLink().'</li>';
  248. } catch(Exception $e) {}
  249. }
  250. $output .= '</ul></div>' .
  251. '</div>';
  252. return $output;
  253. }
  254. /**
  255. * Gets the (X)HTML representing the news entry as it should be displayed
  256. * in a project specific enviorment (it doesn't include the project
  257. * references etc).
  258. * @return string An XHTML compliant representation.
  259. */
  260. public function getAsHTMLProject() {
  261. return '<div class="news">' .
  262. '<h2>'.$this->caption.'</h2>'.
  263. $this->getEntryAsHTML().$this->getCommentLink().
  264. '<div class="newsFooter">' .
  265. '<ul class="horizontal">' .
  266. '<li class="last"><a href="'.$this->getPermanentUrl().'">Permanent link</a></li>' .
  267. '<li>'.date(DATE_LONG, $this->timestamp).'</li>' .
  268. '</ul></div>' .
  269. '</div>';
  270. }
  271. /**
  272. * Gets the (X)HTML representing the news entry as it should be displayed
  273. * in a comment enviorment. It doesn't include comment links nor an
  274. * individual header.
  275. * @return string An XHTML compliant representation.
  276. */
  277. public function getAsHTMLComment() {
  278. doInclude('lib_projects');
  279. $output = '<div class="news">' .
  280. '<div class="titleFooter">' .
  281. '<ul class="horizontal">' .
  282. '<li class="last">'.date(DATE_LONG, $this->timestamp).'</li>';
  283. foreach($this->projectIds as $projId) {
  284. try {
  285. $project = new Project($projId);
  286. $output .= '<li>'.$project->getLink().'</li>';
  287. } catch(Exception $e) {}
  288. }
  289. $output .= '</ul>' .
  290. '</div>'.
  291. $this->getEntryAsHTML().
  292. '</div>';
  293. return $output;
  294. }
  295. /**
  296. * Gets the (X)HTML representating the news entry as it should be
  297. * displayed when displayed as a preview.
  298. * @param integer $maxCharacter The maximum number of characters that
  299. * should remain in the preview version.
  300. * @return string An XHTML compliant representation.
  301. */
  302. public function getAsHTMLPreview($maxCharacters) {
  303. doInclude('lib_projects');
  304. $entry = $this->getEntryAsHTML();
  305. $entry = preg_replace("/<.*?>/","",$entry);
  306. $entry = preg_replace("/<.*?$/","",$entry);
  307. $entry = preg_replace("/\[.*?]/","",$entry);
  308. $entry = preg_replace("/\[.*?$/","",$entry);
  309. while(($maxCharacters < strlen($entry)) && ($entry[$maxCharacters] != ' ')) {
  310. $maxCharacters++;
  311. }
  312. if(strlen($entry) >= $maxCharacters) {
  313. $entry = substr($entry, 0, $maxCharacters).'...';
  314. }
  315. $output = '<div class="news">' .
  316. '<h2>'.$this->caption.'</h2>' .
  317. '<div class="titleFooter">' .
  318. '<ul class="horizontal">' .
  319. '<li class="last">'.date(DATE_LONG, $this->timestamp).'</li>';
  320. foreach($this->projectIds as $projId) {
  321. try {
  322. $project = new Project($projId);
  323. $output .= '<li>'.$project->getLink().'</li>';
  324. } catch(Exception $e) {}
  325. }
  326. $output .= '</ul>' .
  327. '</div>' .
  328. $entry.' '.'<a href="'.$this->getPermanentUrl().'">more</a>'.
  329. '</div>';
  330. return $output;
  331. }
  332. /**
  333. * Gets the (X)HTML representating the news entry in a search engine
  334. * friendly form.
  335. * @param integer $maxCharacter The maximum number of characters that
  336. * should remain in the preview version.
  337. * @return string An XHTML compliant representation.
  338. */
  339. public function getAsHTMLIndex($maxCharacters) {
  340. doInclude('lib_projects');
  341. $entry = $this->getEntryAsHTML();
  342. $entry = preg_replace("/<.*?>/","",$entry);
  343. $entry = preg_replace("/<.*?$/","",$entry);
  344. $entry = preg_replace("/\[.*?]/","",$entry);
  345. $entry = preg_replace("/\[.*?$/","",$entry);
  346. while(($maxCharacters < strlen($entry)) && ($entry[$maxCharacters] != ' ')) {
  347. $maxCharacters++;
  348. }
  349. if(strlen($entry) >= $maxCharacters) {
  350. $entry = substr($entry, 0, $maxCharacters).'...';
  351. }
  352. $output = '<div class="news">' .
  353. '<h2><a href="'.$this->getPermanentUrl().'">'.$this->caption.'</a></h2>' .
  354. $entry.
  355. '</div>';
  356. return $output;
  357. }
  358. /**
  359. * Gets the news entry as it should displayed in an RSS feed.
  360. * @return string An RSS compliant item element.
  361. */
  362. public function getAsRSS() {
  363. return '<item>' .
  364. '<title>'.$this->caption.'</title>' .
  365. '<link>http://'.$_SERVER['HTTP_HOST'].$this->getPermanentUrl().'</link>' .
  366. '<description>'.htmlspecialchars(strip_tags($this->entry)).'</description>' .
  367. '<pubDate>'.date(DATE_RSS, $this->timestamp).'</pubDate>' .
  368. '<guid isPermaLink="true">http://'.$_SERVER['HTTP_HOST'].$this->getPermanentUrl().'</guid>' .
  369. '</item>';
  370. }
  371. /**
  372. * Gets the news' entry with markup code in it (e.g. [img]...[/img]) to the
  373. * XHTML equivelant.
  374. * @return string An XHTML representation.
  375. */
  376. private function getEntryAsHTML() {
  377. if(strlen($this->entry) == 0) {
  378. $this->updateProcessedEntry();
  379. }
  380. return $this->entry;
  381. }
  382. /**
  383. * Processes the raw form of the news entry and updates the processed form
  384. * with the new version. This should be done whenever the entry changes.
  385. */
  386. public function updateProcessedEntry() {
  387. global $db;
  388. doInclude("lib_markup");
  389. $entry = $db->querySelectFirst(TABLE_NEWS, 'entry',
  390. $db->buildSqlSelector(array('id' => parent::$this->getId())));
  391.  
  392. $entry = convertMarkupToHTML($entry);
  393. //Update the timestamp of the last contents generation.
  394. $this->lastGenerated = time();
  395. //Store the new processed version.
  396. $vars = array(
  397. 'processed_entry' => $entry,
  398. 'timestamp_generated' => $this->lastGenerated
  399. );
  400. $db->queryUpdate(TABLE_NEWS, $vars,
  401. $db->buildSqlSelector(array('id' => parent::$this->getId())));
  402. $this->entry = $entry;
  403. }
  404. /**
  405. * Gets the premanent url to the news item.
  406. * @return string An url path from the http host.
  407. */
  408. private function getPermanentUrl() {
  409. return ROOT_PATH.$this->getPermanentRelativeUrl();
  410. }
  411. /**
  412. * Gets the permanent url to the news item relative to the site root.
  413. * @return string An url path relative to the site root.
  414. */
  415. private function getPermanentRelativeUrl() {
  416. return $this->getTitleRelativeUrl();//"news/entry".parent::$this->getId();
  417. }
  418. /**
  419. * Gets the title url to the news item relative to the site root.
  420. * @return string An url path relative to the site root.
  421. */
  422. private function getTitleRelativeUrl() {
  423. doInclude('lib_util');
  424. return "news/".urlEncodeReadable($this->caption)."_".parent::$this->getId();
  425. }
  426. /**
  427. * Gets the url to the news' comments page.
  428. * @return string An url path from the http host.
  429. */
  430. private function getCommentUrl() {
  431. return ROOT_PATH."news/entry".parent::$this->getId().'#comments';
  432. }
  433. /**
  434. * Gets all comment instance that are related to the news entry.
  435. * @return array An array of Comment instances.
  436. */
  437. public function getAssociatedComments() {
  438. global $db;
  439. $comments = array();
  440. $rows = $db->querySelect(TABLE_COMMENTS, array('id'),
  441. "WHERE news_id=".parent::$this->getId()." ORDER BY timestamp DESC");
  442. foreach($rows as $row) {
  443. $comments[] = new Comment($row['id']);
  444. }
  445. return $comments;
  446. }
  447. /**
  448. * Gets a link to the news iterm's comment page.
  449. * @return An XHTML compliant version of the link.
  450. */
  451. private function getCommentLink() {
  452. $commentCount = sizeof($this->getAssociatedComments());
  453. return '<p class="commentLink"><a href="'.$this->getCommentUrl().'">' .
  454. 'Comments ('.$commentCount.')</a></p>';
  455. }
  456. /**
  457. * Gets the news entry as a sitemap entry.
  458. * @return string A well-formed XML url entry as specified by the google
  459. * sitemap schema.
  460. */
  461. public function getAsSitemapEntry() {
  462. doInclude('lib_sitemap');
  463. return formatSitemapElement(
  464. $this->getTitleRelativeUrl(),
  465. $this->lastGenerated, '0.7', 'never');
  466. }
  467. }
  468. ?>

Documentation generated on Sun, 16 Apr 2006 21:03:43 +0200 by phpDocumentor 1.3.0RC4