- <?php
- /*
- * Lokorin.com
- * Copyright 2004-2006
- * Licensed under the GNU LGPL. See COPYING for full terms.
- */
- /**
- * A library that contains everything that is related to the site's news
- * system.
- * @author Andreas Launila
- * @version $Revision: 1.34 $
- * @package com.lokorin.lokorin.lib
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- */
-
- /**
- * For access to common constants and functions.
- */
- require_once('common.php');
- CssHandler::getInstance()->includeCss('news');
-
- /**
- * Gets all news items that matches the specified selector.
- * @param string $sqlSelector The selector that specifies all matching news
- * items, e.g. "WHERE id > 5" or "ORDER BY id".
- * @return array An array where the key is the order in which the news entries
- * were retrieved. The values are News instances.
- * @access public
- */
- function getNews($sqlSelector) {
- global $db;
-
- $rows = $db->querySelect(TABLE_NEWS, array('id'), $sqlSelector);
- $news = array();
- foreach($rows as $row) {
- $news[] = new News($row['id']);
- }
- return $news;
- }
-
- /**
- * Gets the captions of all available news entries.
- * @return array Key is news id, value as news title.
- * @access public
- */
- function getAllNewsCaptions() {
- global $db;
- $rows = $db->querySelect(TABLE_NEWS, array('id', 'caption'));
- $captions = array();
- foreach($rows as $row) {
- $captions[$row['id']] = $row['caption'];
- }
- return $captions;
- }
-
- /**
- * Describes a news comment.
- * @author Andreas Launila
- * @package com.lokorin.lokorin.lib
- */
- class Comment extends TableEntry {
- /**
- * The unique identifier of the news item that the comment is attached to.
- * @var integer
- */
- private $newsId;
- /**
- * The author of the comment.
- * @var string
- */
- private $author;
- /**
- * The contents of the comment.
- * @var string
- */
- private $contents;
- /**
- * The time when the comment was made.
- * @var integer
- */
- private $timestamp;
-
- /**
- * Constructor for Comment.
- * @param integer $id The unique identifier for the comment that should be
- * created.
- */
- public function __construct($id) {
- $fields = array(
- "news_id",
- "author",
- "contents",
- "timestamp"
- );
- parent::__construct(TABLE_COMMENTS, $id, $fields);
- }
-
- /**
- * @see com.lokorin.dfcrafters.TableEntry.readRow($row)
- */
- protected function readRow($row) {
- $this->newsId = $row['news_id'];
- $this->author = htmlspecialchars($row['author']);
- $this->contents = htmlspecialchars($row['contents']);
- $this->timestamp = $row['timestamp'];
- }
-
- /**
- * Gets the news entry that the comment is attached to.
- * @return News A new entry.
- */
- public function getNewsEntry() {
- return new News($this->newsId);
- }
-
- /**
- * Gets the comment as (X)HTML.
- * @return string An XHTML compliant representation.
- */
- public function getAsHTML() {
- return '<span class="commentAuthor">'.$this->author.'</span>
- <span class="commentDate">('.
- date(DATE_COMMENT, $this->timestamp).')</span>
- <span class="commentBody">'.$this->contents.'</span>';
- }
- }
-
- /**
- * Describes a news item.
- * @author Andreas Launila
- * @package com.lokorin.lokorin.lib
- */
- class News extends TableEntry {
- /**
- * The caption of the news item.
- * @var string
- */
- private $caption;
- /**
- * The unix timestamp of when the news was submitted.
- * @var integer
- */
- private $timestamp;
- /**
- * The unix timestamp of when the contents of the news was last generated.
- * @var integer
- */
- private $lastGenerated;
- /**
- * The actual processed XHTML entry of the news item.
- * @var string
- */
- private $entry;
- /**
- * The unique identifiers of the projects that the news item is connected
- * to.
- * @var array
- */
- private $projectIds;
-
- /**
- * Constructor for News.
- * @param integer $id The unique identifier of the news entry.
- */
- public function News($id) {
- $fields = array(
- 'caption',
- 'timestamp',
- 'processed_entry',
- 'project_id1',
- 'project_id2',
- 'project_id3',
- 'timestamp_generated'
- );
- parent::__construct(TABLE_NEWS, $id, $fields);
- }
-
- /**
- * @see com.lokorin.lokorin.TableEntry.readRow($row)
- */
- protected function readRow($row) {
- $this->caption = $row['caption'];
- $this->timestamp = $row['timestamp'];
- $this->entry = $row['processed_entry'];
- $this->projectIds = array();
- if($row['project_id3'] != 0) {
- $this->projectIds[] = $row['project_id3'];
- }
- if($row['project_id2'] != 0) {
- $this->projectIds[] = $row['project_id2'];
- }
- if($row['project_id1'] != 0) {
- $this->projectIds[] = $row['project_id1'];
- }
- $this->lastGenerated = $row['timestamp_generated'];
- }
-
- /**
- * Gets the caption of the news entry.
- * @return string A news caption.
- */
- public function getCaption() {
- return $this->caption;
- }
-
- /**
- * Gets the timestamp that represents when the news entry was added.
- * @return integer A unix timestamp.
- */
- public function getTimeAdded() {
- return $this->timestamp;
- }
-
- /**
- * Gets the (X)HTML representing the news entry as it should be displayed
- * in a generic enviorment (it includes the project references etc).
- * @return string An XHTML compliant representation.
- */
- public function getAsHTMLGeneric() {
- doInclude('lib_projects');
-
- $output = '<div class="news">' .
- '<h2>'.$this->caption.'</h2>' .
- '<div class="titleFooter">' .
- '<ul class="horizontal">' .
- '<li class="last"><a href="'.$this->getPermanentUrl().'">Permanent link</a></li>' .
- '<li>'.date(DATE_LONG, $this->timestamp).'</li>';
- foreach($this->projectIds as $projId) {
- try {
- $project = new Project($projId);
- $output .= '<li>'.$project->getLink().'</li>';
- } catch(Exception $e) {}
- }
- $output .= '</ul></div>' .
- $this->getEntryAsHTML().$this->getCommentLink().
- '</div>';
-
- return $output;
- }
-
- /**
- * Gets the (X)HTML representing the news entry as it should be displayed
- * in an archive (it includes the project references, but not a comment
- * link).
- * @return string An XHTML compliant representation.
- */
- public function getAsHTMLArchive() {
- doInclude('lib_projects');
-
- $output = '<div class="news">' .
- '<h2>'.$this->caption.'</h2>' .
- $this->getEntryAsHTML().
- '<div class="newsFooter">' .
- '<ul class="horizontal">' .
- '<li class="last"><a href="'.$this->getPermanentUrl().'">Permanent link</a></li>' .
- '<li>'.date(DATE_LONG, $this->timestamp).'</li>';
- foreach($this->projectIds as $projId) {
- try {
- $project = new Project($projId);
- $output .= '<li>'.$project->getLink().'</li>';
- } catch(Exception $e) {}
- }
- $output .= '</ul></div>' .
- '</div>';
-
- return $output;
- }
-
- /**
- * Gets the (X)HTML representing the news entry as it should be displayed
- * in a project specific enviorment (it doesn't include the project
- * references etc).
- * @return string An XHTML compliant representation.
- */
- public function getAsHTMLProject() {
- return '<div class="news">' .
- '<h2>'.$this->caption.'</h2>'.
- $this->getEntryAsHTML().$this->getCommentLink().
- '<div class="newsFooter">' .
- '<ul class="horizontal">' .
- '<li class="last"><a href="'.$this->getPermanentUrl().'">Permanent link</a></li>' .
- '<li>'.date(DATE_LONG, $this->timestamp).'</li>' .
- '</ul></div>' .
- '</div>';
- }
-
- /**
- * Gets the (X)HTML representing the news entry as it should be displayed
- * in a comment enviorment. It doesn't include comment links nor an
- * individual header.
- * @return string An XHTML compliant representation.
- */
- public function getAsHTMLComment() {
- doInclude('lib_projects');
-
- $output = '<div class="news">' .
- '<div class="titleFooter">' .
- '<ul class="horizontal">' .
- '<li class="last">'.date(DATE_LONG, $this->timestamp).'</li>';
- foreach($this->projectIds as $projId) {
- try {
- $project = new Project($projId);
- $output .= '<li>'.$project->getLink().'</li>';
- } catch(Exception $e) {}
- }
- $output .= '</ul>' .
- '</div>'.
- $this->getEntryAsHTML().
- '</div>';
-
- return $output;
- }
-
- /**
- * Gets the (X)HTML representating the news entry as it should be
- * displayed when displayed as a preview.
- * @param integer $maxCharacter The maximum number of characters that
- * should remain in the preview version.
- * @return string An XHTML compliant representation.
- */
- public function getAsHTMLPreview($maxCharacters) {
- doInclude('lib_projects');
-
- $entry = $this->getEntryAsHTML();
- $entry = preg_replace("/<.*?>/","",$entry);
- $entry = preg_replace("/<.*?$/","",$entry);
- $entry = preg_replace("/\[.*?]/","",$entry);
- $entry = preg_replace("/\[.*?$/","",$entry);
- while(($maxCharacters < strlen($entry)) && ($entry[$maxCharacters] != ' ')) {
- $maxCharacters++;
- }
- if(strlen($entry) >= $maxCharacters) {
- $entry = substr($entry, 0, $maxCharacters).'...';
- }
-
- $output = '<div class="news">' .
- '<h2>'.$this->caption.'</h2>' .
- '<div class="titleFooter">' .
- '<ul class="horizontal">' .
- '<li class="last">'.date(DATE_LONG, $this->timestamp).'</li>';
- foreach($this->projectIds as $projId) {
- try {
- $project = new Project($projId);
- $output .= '<li>'.$project->getLink().'</li>';
- } catch(Exception $e) {}
- }
- $output .= '</ul>' .
- '</div>' .
- $entry.' '.'<a href="'.$this->getPermanentUrl().'">more</a>'.
- '</div>';
-
- return $output;
- }
-
- /**
- * Gets the (X)HTML representating the news entry in a search engine
- * friendly form.
- * @param integer $maxCharacter The maximum number of characters that
- * should remain in the preview version.
- * @return string An XHTML compliant representation.
- */
- public function getAsHTMLIndex($maxCharacters) {
- doInclude('lib_projects');
-
- $entry = $this->getEntryAsHTML();
- $entry = preg_replace("/<.*?>/","",$entry);
- $entry = preg_replace("/<.*?$/","",$entry);
- $entry = preg_replace("/\[.*?]/","",$entry);
- $entry = preg_replace("/\[.*?$/","",$entry);
- while(($maxCharacters < strlen($entry)) && ($entry[$maxCharacters] != ' ')) {
- $maxCharacters++;
- }
- if(strlen($entry) >= $maxCharacters) {
- $entry = substr($entry, 0, $maxCharacters).'...';
- }
-
- $output = '<div class="news">' .
- '<h2><a href="'.$this->getPermanentUrl().'">'.$this->caption.'</a></h2>' .
- $entry.
- '</div>';
-
- return $output;
- }
-
- /**
- * Gets the news entry as it should displayed in an RSS feed.
- * @return string An RSS compliant item element.
- */
- public function getAsRSS() {
- return '<item>' .
- '<title>'.$this->caption.'</title>' .
- '<link>http://'.$_SERVER['HTTP_HOST'].$this->getPermanentUrl().'</link>' .
- '<description>'.htmlspecialchars(strip_tags($this->entry)).'</description>' .
- '<pubDate>'.date(DATE_RSS, $this->timestamp).'</pubDate>' .
- '<guid isPermaLink="true">http://'.$_SERVER['HTTP_HOST'].$this->getPermanentUrl().'</guid>' .
- '</item>';
- }
-
- /**
- * Gets the news' entry with markup code in it (e.g. [img]...[/img]) to the
- * XHTML equivelant.
- * @return string An XHTML representation.
- */
- private function getEntryAsHTML() {
- if(strlen($this->entry) == 0) {
- $this->updateProcessedEntry();
- }
- return $this->entry;
- }
-
- /**
- * Processes the raw form of the news entry and updates the processed form
- * with the new version. This should be done whenever the entry changes.
- */
- public function updateProcessedEntry() {
- global $db;
- doInclude("lib_markup");
-
- $entry = $db->querySelectFirst(TABLE_NEWS, 'entry',
- $db->buildSqlSelector(array('id' => parent::$this->getId())));
-
- $entry = convertMarkupToHTML($entry);
-
- //Update the timestamp of the last contents generation.
- $this->lastGenerated = time();
-
- //Store the new processed version.
- $vars = array(
- 'processed_entry' => $entry,
- 'timestamp_generated' => $this->lastGenerated
- );
- $db->queryUpdate(TABLE_NEWS, $vars,
- $db->buildSqlSelector(array('id' => parent::$this->getId())));
- $this->entry = $entry;
- }
-
- /**
- * Gets the premanent url to the news item.
- * @return string An url path from the http host.
- */
- private function getPermanentUrl() {
- return ROOT_PATH.$this->getPermanentRelativeUrl();
- }
-
- /**
- * Gets the permanent url to the news item relative to the site root.
- * @return string An url path relative to the site root.
- */
- private function getPermanentRelativeUrl() {
- return $this->getTitleRelativeUrl();//"news/entry".parent::$this->getId();
- }
-
- /**
- * Gets the title url to the news item relative to the site root.
- * @return string An url path relative to the site root.
- */
- private function getTitleRelativeUrl() {
- doInclude('lib_util');
- return "news/".urlEncodeReadable($this->caption)."_".parent::$this->getId();
- }
-
- /**
- * Gets the url to the news' comments page.
- * @return string An url path from the http host.
- */
- private function getCommentUrl() {
- return ROOT_PATH."news/entry".parent::$this->getId().'#comments';
- }
-
- /**
- * Gets all comment instance that are related to the news entry.
- * @return array An array of Comment instances.
- */
- public function getAssociatedComments() {
- global $db;
-
- $comments = array();
- $rows = $db->querySelect(TABLE_COMMENTS, array('id'),
- "WHERE news_id=".parent::$this->getId()." ORDER BY timestamp DESC");
- foreach($rows as $row) {
- $comments[] = new Comment($row['id']);
- }
- return $comments;
- }
-
- /**
- * Gets a link to the news iterm's comment page.
- * @return An XHTML compliant version of the link.
- */
- private function getCommentLink() {
- $commentCount = sizeof($this->getAssociatedComments());
- return '<p class="commentLink"><a href="'.$this->getCommentUrl().'">' .
- 'Comments ('.$commentCount.')</a></p>';
- }
-
- /**
- * Gets the news entry as a sitemap entry.
- * @return string A well-formed XML url entry as specified by the google
- * sitemap schema.
- */
- public function getAsSitemapEntry() {
- doInclude('lib_sitemap');
- return formatSitemapElement(
- $this->getTitleRelativeUrl(),
- $this->lastGenerated, '0.7', 'never');
- }
- }
- ?>