diff options
| author | Eric Poirier | 2019-07-19 17:49:56 +0000 |
|---|---|---|
| committer | Eric Poirier | 2019-07-22 13:36:34 +0000 |
| commit | f59a11c7ee8edb1dd3b920074821332d26dcea9f (patch) | |
| tree | add4321b13e16dc0ef16a907f7873dcd91c25401 | |
| parent | 2e80a6f2efcde136aacb3047e02270ac9a7487cf (diff) | |
| download | eclipse.org-common-f59a11c7ee8edb1dd3b920074821332d26dcea9f.tar.gz eclipse.org-common-f59a11c7ee8edb1dd3b920074821332d26dcea9f.tar.xz eclipse.org-common-f59a11c7ee8edb1dd3b920074821332d26dcea9f.zip | |
Bug 534152 - Create Featured Story class
Change-Id: I46b67ee700a83f7af6762bc60079e510da548975
Signed-off-by: Eric Poirier <eric.poirier@eclipse-foundation.org>
| -rw-r--r-- | classes/ads/featuredStory.class.php | 96 | ||||
| -rw-r--r-- | classes/themes/baseTheme.class.php | 21 |
2 files changed, 117 insertions, 0 deletions
diff --git a/classes/ads/featuredStory.class.php b/classes/ads/featuredStory.class.php new file mode 100644 index 00000000..04d4e0f4 --- /dev/null +++ b/classes/ads/featuredStory.class.php @@ -0,0 +1,96 @@ +<?php + +/** + * Copyright (c) 2019 Eclipse Foundation and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * Contributors: + * Eric Poirier (Eclipse Foundation) - initial API and implementation + * + * SPDX-License-Identifier: EPL-2.0 + */ + +class FeaturedStory { + + public $data = array(); + + /** + * Get a featured story based on start and end date + * + * @return array + */ + public function getFeaturedStory() { + + $data = $this->getXmlData(); + if (empty($data['item'])) { + return array(); + } + + $default_item = array(); + $valid_items = array(); + foreach ($data['item'] as $item) { + if (empty($item['start_date']) && empty($item['end_date'])) { + $default_item = $item; + continue; + } + if(time() >= strtotime($item['start_date']) && time() < strtotime($item['end_date'])) { + $valid_items[] = $item; + } + } + + // Return the featured items if the array is not empty + if (!empty($valid_items)) { + $featured_item = $valid_items[array_rand($valid_items)]; + if (!empty($featured_item)) { + return $featured_item; + } + } + + // Otherwise return the default item + return $default_item; + } + + /** + * Get the featured story data from the source file + * + * @return array + */ + public function getXmlData() { + return $this->data; + } + + /** + * Set the featured story data from the source file + * + * @param string $xml_file_path + */ + public function setXmlData($xml_file_path) { + + if (empty($xml_file_path)) { + return FALSE; + } + + $fileContents = file_get_contents($xml_file_path); + if (empty($fileContents)) { + return FALSE; + } + + $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents); + $fileContents = trim(str_replace('"', "'", $fileContents)); + $simpleXml = simplexml_load_string($fileContents, 'SimpleXMLElement', LIBXML_NOCDATA); + if (empty($simpleXml)) { + return FALSE; + } + + $featured_stories_json = json_encode($simpleXml); + if (empty($featured_stories_json)) { + return FALSE; + } + + $this->data = json_decode($featured_stories_json,TRUE); + return TRUE; + } +}
\ No newline at end of file diff --git a/classes/themes/baseTheme.class.php b/classes/themes/baseTheme.class.php index f994df15..bc00a7f6 100644 --- a/classes/themes/baseTheme.class.php +++ b/classes/themes/baseTheme.class.php @@ -342,6 +342,27 @@ class BaseTheme { } /** + * Get a featured story from the provided xml file + * + * @param $xml_file + * + * @return array + */ + public function getFeaturedStory($xml_file) { + $App = $this->_getApp(); + require_once($App->getBasePath() . '/classes/ads/featuredStory.class.php'); + $FeaturedStory = new FeaturedStory(); + if ($FeaturedStory instanceof FeaturedStory) { + $FeaturedStory->setXmlData('featured-story.xml'); + $featured_story = $FeaturedStory->getFeaturedStory(); + if (!empty($featured_story)) { + return $featured_story; + } + } + return array(); + } + + /** * Get Google Tag Manager * * Insert this code as high in the <head> of the page as possible. |
