com web/php: Created some new classes to produce the articles view on the homepage. I'm trying to improve the backend code where I can and the
print_news function is pretty complex, so I w ent this route instead.: Entity/NewsItem.php Ga teway/NewsFileSystemGateway.php Gateway/NewsGateway.php
View/HomepageNewsView.php index-beta.php

From: Date: Tue, 19 Jun 2012 22:41:45 +0000
Subject: com web/php: Created some new classes to produce the articles view on the homepage. I'm trying to improve the backend code where I can and the
print_news function is pretty complex, so I w ent this route instead.: Entity/NewsItem.php Ga teway/NewsFileSystemGateway.php Gateway/NewsGateway.php
View/HomepageNewsView.php index-beta.php
Groups: php.webmaster 
Request: Send a blank email to php-webmaster+get-13734@lists.php.net to get a copy of this message
Commit: 9530a8571f96ce34bb5b5574740f6a3ffee2e09c Author: Levi Morrison <levim@php.net> Tue, 19 Jun 2012 16:41:45 -0600 Parents: 5b38097f43541ef7a76787090ef9d1d7d0b6c697 Branches: master Link: http://git.php.net/?p=web/php.git;a=commitdiff;h=9530a8571f96ce34bb5b5574740f6a3ffee2e09c Log: Created some new classes to produce the articles view on the homepage. I'm trying to improve the backend code where I can and the print_news function is pretty complex, so I went this route instead. Changed paths: A Entity/NewsItem.php A Gateway/NewsFileSystemGateway.php A Gateway/NewsGateway.php A View/HomepageNewsView.php M index-beta.php

diff --git a/Entity/NewsItem.php b/Entity/NewsItem.php new file mode 100644 index 0000000..7120c15 --- /dev/null +++ b/Entity/NewsItem.php @@ -0,0 +1,140 @@ +<?php + +class NewsItem { + + /** + * @var int + */ + protected $id; + + /** + * @var string + */ + protected $title; + + /** + * @var string + */ + protected $published; + + /** + * @var DateTime + */ + protected $updated; + + /** + * @var array + */ + protected $category = array(); + + /** + * @var array + */ + protected $link = array(); + + /** + * @var string + */ + protected $content = ''; + + /** + * @var string + */ + protected $newsImage; + + + /** + * @param string $title + * @param int $id + * @param string $published + * @param string $updated + * @param array $category + * @param array $link + * @param string $content + * @param string $newsImage + */ + function __construct($title, $id, $published, $updated, $category, $link, $content, $newsImage = NULL) { + $this->title = $title; + $this->id = $id; + $this->published = $published; + $this->updated = new DateTime($updated); + $this->category = $category; + $this->link = $link; + $this->content = $content; + $this->newsImage = $newsImage; + } + + /** + * @return array + */ + public function getCategories() { + return $this->category; + } + + /** + * @return string + */ + public function getContent() { + return $this->content; + } + + /** + * @return mixed + */ + public function getId() { + return $this->id; + } + + /** + * @return array + */ + public function getLink() { + return $this->link; + } + + public function getNewsImage() { + return $this->newsImage; + } + + /** + * @return DateTime + */ + public function getPublishedDate() { + return new DateTime($this->published); + } + + /** + * @return string + */ + public function getPublishedString() { + return $this->published; + } + + /** + * @return mixed + */ + public function getTitle() { + return $this->title; + } + + /** + * @return DateTime + */ + public function getUpdatedDate() { + return $this->updated; + } + + /** + * @param $category + * @return bool + */ + public function hasCategory($category) { + foreach ($this->category as $cat) { + if ($cat['term'] === $category) { + return true; + } + } + return false; + } + +} diff --git a/Gateway/NewsFileSystemGateway.php b/Gateway/NewsFileSystemGateway.php new file mode 100644 index 0000000..5739b7c --- /dev/null +++ b/Gateway/NewsFileSystemGateway.php @@ -0,0 +1,71 @@ +<?php + +require_once __DIR__ . '/../Entity/NewsItem.php'; +require_once __DIR__ . '/NewsGateway.php'; + +class NewsFileSystemGateway implements NewsGateway { + + /** + * @var array + */ + protected $articles; + + public function __construct() { + + $NEWS_ENTRIES = array(); + + //will overwrite $NEWS_ENTRIES. + require __DIR__ . '/../include/pregen-news.inc'; + + $this->articles = array(); + + foreach ($NEWS_ENTRIES as $article) { + $this->articles[] = new NewsItem( + $article['title'], + $article['id'], + $article['published'], + $article['updated'], + $article['category'], + $article['link'], + $article['content'], + isset($article['newsImage']) ? $article['newsImage'] : NULL + ); + } + + } + + /** + * @param int $max [optional] + * @return Traversable + */ + public function getLatestArticles($max = 5) { + return new ArrayIterator(array_slice($this->articles, 0, $max)); + } + + /** + * @param Traversable $categories + * @param int $limit + * @return Traversable + */ + public function getArticlesForCategories(Traversable $categories, $limit = 5) { + $result = array(); + + foreach ($this->articles as $item) { + /** + * @var NewsItem $item + */ + $articleCategories = array(); + + foreach ($categories as $category) { + if ($item->hasCategory($category)) { + $result[] = $item; + } + } + + } + + return new ArrayIterator(array_slice($result, 0 , $limit)); + + } + +} diff --git a/Gateway/NewsGateway.php b/Gateway/NewsGateway.php new file mode 100644 index 0000000..991cac4 --- /dev/null +++ b/Gateway/NewsGateway.php @@ -0,0 +1,20 @@ +<?php + +interface NewsGateway { + + /** + * @abstract + * @param int $max [optional] + * @return Traversable + */ + function getLatestArticles($max = 5); + + /** + * @abstract + * @param Traversable $categories + * @param int $limit [optional] + * @return Traversable + */ + function getArticlesForCategories(Traversable $categories, $limit = 5); + +} diff --git a/View/HomepageNewsView.php b/View/HomepageNewsView.php new file mode 100644 index 0000000..04ad60c --- /dev/null +++ b/View/HomepageNewsView.php @@ -0,0 +1,98 @@ +<?php + +class HomepageNewsView { + + /** + * @var Traversable + */ + protected $articles; + + /** + * @param Traversable $articles + */ + public function __construct(Traversable $articles) { + $this->articles = $articles; + } + + public function render() { + + ob_start(); + ?> + <div class='recentNewsEntries'> + <h1>Recent News</h1> + <a class="newsArchiveLink" href="/archive/">(News Archive)</a> + + <?php + foreach ($this->articles as $article) : + /** + * @var NewsItem $article + */ + + $id = parse_url($article->getId(), PHP_URL_FRAGMENT); + $title = $article->getTitle(); + + $publishedDate = $article->getPublishedString(); + $newsDate = $article->getPublishedDate()->format('d-M-Y'); + + $content = $article->getContent(); + + $permanentLink = "#" .$id; + foreach($article->getLink() as $link) { + if ($link["rel"] === "via") { + $permanentLink = $link["href"]; + break; + } + } + + $image = ""; + $newsImage = $article->getNewsImage(); + if(isset($newsImage["link"]) && $newsImage["content"]) { + $image = "<a href=\"{$newsImage["link"]}\">" . $this->renderImage($newsImage) . "</a>"; + } + + $itemCategories = $article->getCategories(); + + $event = ''; + if ($article->hasCategory('conferences') || $article->hasCategory('cfp')) { + $event = " vevent"; + } + echo "<div class='newsItem hentry{$event}''> + <div class='newsImage'>{$image}</div> + <h2 class='summary entry-title'><a name='{$id}' id='{$id}' href='{$permanentLink}' rel='bookmark' class='bookmark'>{$title}</a></h2> + <div class='entry-content description'> + <abbr class='published newsdate' title='{$publishedDate}''>{$newsDate}</abbr> + {$content} + </div> + </div>"; + endforeach; + ?> + + </div> + + + <?php + + return ob_get_clean(); + + } + + /** + * @param array $image + * @return string + */ + protected function renderImage(array $image) { + $imageHtml = "<img src='/images/news/{$image['content']}' alt='{$image['title']}' style='float:right'"; + + //getimagesize will give the HTML width and height attributes in offset 3 of the return value + $imageDetails = @getimagesize($_SERVER['DOCUMENT_ROOT'] . '/images/news/' . $image['content']); + + if (count($imageDetails) >= 4) { + $imageHtml .= $imageDetails[3]; + } + + $imageHtml .= "/>"; + + return $imageHtml; + } + +} diff --git a/index-beta.php b/index-beta.php index fb7ed00..8c77e9a 100644 --- a/index-beta.php +++ b/index-beta.php @@ -43,11 +43,11 @@ else { } $_SERVER['BASE_PAGE'] = 'index.php'; -include_once $_SERVER['DOCUMENT_ROOT'] . '/include/prepend.inc'; -include_once $_SERVER['DOCUMENT_ROOT'] . '/include/pregen-events.inc'; -include_once $_SERVER['DOCUMENT_ROOT'] . '/include/pregen-confs.inc'; -include_once $_SERVER['DOCUMENT_ROOT'] . '/include/pregen-news.inc'; -include_once $_SERVER['DOCUMENT_ROOT'] . '/include/version.inc'; +include_once __DIR__ . '/include/prepend.inc'; +include_once __DIR__ . '/include/pregen-events.inc'; +include_once __DIR__ . '/include/pregen-confs.inc'; +include_once __DIR__ . '/include/pregen-news.inc'; +include_once __DIR__ . '/include/version.inc'; // Prepare announcements. if (is_array($CONF_TEASER) && $CONF_TEASER) { @@ -117,16 +117,22 @@ $features = " <br style='clear: left;' /> "; -$_SERVER['BASE_PAGE'] = 'archive/2012.php'; -include_once $_SERVER['DOCUMENT_ROOT'] . '/include/prepend.inc'; -include_once $_SERVER['DOCUMENT_ROOT'] . '/include/pregen-news.inc'; +require_once './Gateway/NewsFileSystemGateway.php'; + +$NewsGateway = new NewsFileSystemGateway(); +$RecentNews = $NewsGateway->getArticlesForCategories(new ArrayIterator(array( + "conferences", + "cfp", + "frontpage" +))); + ob_start(); -echo "<div class='recentNewsEntries'> - <h1>Recent News</h1> - <a class='newsArchiveLink' href='/archive/'>(News Archive)</a>"; -print_news($NEWS_ENTRIES, array("conferences", "cfp", "frontpage"), 5, 2012); -echo "</div>\n"; +require_once './View/HomepageNewsView.php'; +$RecentNewsView = new HomepageNewsView($RecentNews); + +echo $RecentNewsView->render(); + $news = ob_get_clean(); // Wrap announcements and features in a content element @@ -169,7 +175,6 @@ site_header("Hypertext Preprocessor", // Print body of home page. print_view('homepage/sidebar.php', array( - 'news' => $NEWS_ENTRIES, 'announcements' => $announcements )); print $content;
« previous php.webmaster (#13734) next »