SAAS-732: Clean up weekly program widget

This commit is contained in:
drigato 2015-05-05 15:07:12 -04:00
parent f375115825
commit 9ad0628efa
6 changed files with 288 additions and 101 deletions

View File

@ -0,0 +1,169 @@
<?php
class WidgetHelper
{
public static function getWeekInfo($timezone)
{
//weekStart is in station time.
$weekStartDateTime = Application_Common_DateHelper::getWeekStartDateTime();
$dow = array("monday", "tuesday", "wednesday", "thursday", "friday",
"saturday", "sunday", "nextmonday", "nexttuesday", "nextwednesday",
"nextthursday", "nextfriday", "nextsaturday", "nextsunday");
$result = array();
// default to the station timezone
$timezone = Application_Model_Preference::GetDefaultTimezone();
$userDefinedTimezone = strtolower($timezone);
// if the timezone defined by the user exists, use that
if (array_key_exists($userDefinedTimezone, timezone_abbreviations_list())) {
$timezone = $userDefinedTimezone;
}
$utcTimezone = new DateTimeZone("UTC");
$weekStartDateTime->setTimezone($utcTimezone);
$utcDayStart = $weekStartDateTime->format("Y-m-d H:i:s");
for ($i = 0; $i < 14; $i++) {
//have to be in station timezone when adding 1 day for daylight savings.
$weekStartDateTime->setTimezone(new DateTimeZone($timezone));
$weekStartDateTime->add(new DateInterval('P1D'));
//convert back to UTC to get the actual timestamp used for search.
$weekStartDateTime->setTimezone($utcTimezone);
$utcDayEnd = $weekStartDateTime->format("Y-m-d H:i:s");
$shows = Application_Model_Show::getNextShows($utcDayStart, "ALL", $utcDayEnd);
$utcDayStart = $utcDayEnd;
// convert to user-defined timezone, or default to station
Application_Common_DateHelper::convertTimestampsToTimezone(
$shows,
array("starts", "ends", "start_timestamp","end_timestamp"),
$timezone
);
$result[$dow[$i]] = $shows;
// XSS exploit prevention
self::convertSpecialChars($result, array("name", "url"));
// convert image paths to point to api endpoints
self::findAndConvertPaths($result);
}
return $result;
}
// Second version of this function.
// Removing "next" days and creating two weekly arrays
public static function getWeekInfoV2($timezone)
{
//weekStart is in station time.
$weekStartDateTime = Application_Common_DateHelper::getWeekStartDateTime();
$dow = array("monday", "tuesday", "wednesday", "thursday", "friday",
"saturday", "sunday");
$maxNumOFWeeks = 2;
$result = array();
// default to the station timezone
$timezone = Application_Model_Preference::GetDefaultTimezone();
$userDefinedTimezone = strtolower($timezone);
// if the timezone defined by the user exists, use that
if (array_key_exists($userDefinedTimezone, timezone_abbreviations_list())) {
$timezone = $userDefinedTimezone;
}
$utcTimezone = new DateTimeZone("UTC");
$weekStartDateTime->setTimezone($utcTimezone);
$utcDayStart = $weekStartDateTime->format("Y-m-d H:i:s");
$weekCounter = 0;
while ($weekCounter < $maxNumOFWeeks) {
for ($i = 0; $i < 7; $i++) {
$dateParse = date_parse($weekStartDateTime->format("Y-m-d H:i:s"));
//have to be in station timezone when adding 1 day for daylight savings.
$weekStartDateTime->setTimezone(new DateTimeZone($timezone));
$weekStartDateTime->add(new DateInterval('P1D'));
//convert back to UTC to get the actual timestamp used for search.
$weekStartDateTime->setTimezone($utcTimezone);
$utcDayEnd = $weekStartDateTime->format("Y-m-d H:i:s");
$shows = Application_Model_Show::getNextShows($utcDayStart, "ALL", $utcDayEnd);
$utcDayStart = $utcDayEnd;
// convert to user-defined timezone, or default to station
Application_Common_DateHelper::convertTimestampsToTimezone(
$shows,
array("starts", "ends", "start_timestamp", "end_timestamp"),
$timezone
);
foreach($shows as &$show) {
$startParseDate = date_parse($show['starts']);
$show["show_start_hour"] = str_pad($startParseDate["hour"], 2, "0").":".str_pad($startParseDate["minute"], 2, 0);
$endParseDate = date_parse($show['ends']);
$show["show_end_hour"] = str_pad($endParseDate["hour"], 2, 0).":".str_pad($endParseDate["minute"],2, 0);
}
$result[$weekCounter][$dow[$i]]["dayOfMonth"] = $dateParse["day"];
$result[$weekCounter][$dow[$i]]["dayOfWeek"] = strtoupper(substr($dow[$i], 0, 3));
$result[$weekCounter][$dow[$i]]["shows"] = $shows;
// XSS exploit prevention
self::convertSpecialChars($result, array("name", "url"));
// convert image paths to point to api endpoints
self::findAndConvertPaths($result);
}
$weekCounter += 1;
}
return $result;
}
/**
* Go through a given array and sanitize any potentially exploitable fields
* by passing them through htmlspecialchars
*
* @param unknown $arr the array to sanitize
* @param unknown $keys indexes of values to be sanitized
*/
public static function convertSpecialChars(&$arr, $keys)
{
foreach ($arr as &$a) {
if (is_array($a)) {
foreach ($keys as &$key) {
if (array_key_exists($key, $a)) {
$a[$key] = htmlspecialchars($a[$key]);
}
}
self::convertSpecialChars($a, $keys);
}
}
}
/**
* Recursively find image_path keys in the various $result subarrays,
* and convert them to point to the show-logo endpoint
*
* @param unknown $arr the array to search
*/
public static function findAndConvertPaths(&$arr)
{
$CC_CONFIG = Config::getConfig();
$baseDir = Application_Common_OsPath::formatDirectoryWithDirectorySeparators($CC_CONFIG['baseDir']);
foreach ($arr as &$a) {
if (is_array($a)) {
if (array_key_exists("image_path", $a)) {
$a["image_path"] = $a["image_path"] && $a["image_path"] !== '' ?
"http://".$_SERVER['HTTP_HOST'].$baseDir."api/show-logo?id=".$a["id"] : '';
} else {
self::findAndConvertPaths($a);
}
}
}
}
}

View File

@ -1,4 +1,5 @@
<?php
require_once('WidgetHelper.php');
class ApiController extends Zend_Controller_Action
{
@ -198,7 +199,7 @@ class ApiController extends Zend_Controller_Action
}
// XSS exploit prevention
$this->convertSpecialChars($result, array("name", "url"));
WidgetHelper::convertSpecialChars($result, array("name", "url"));
// apply user-defined timezone, or default to station
Application_Common_DateHelper::convertTimestampsToTimezone(
$result['currentShow'],
@ -216,7 +217,7 @@ class ApiController extends Zend_Controller_Action
$result["timezone"] = $upcase ? strtoupper($timezone) : $timezone;
$result["timezoneOffset"] = Application_Common_DateHelper::getTimezoneOffset($timezone);
// convert image paths to point to api endpoints
$this->findAndConvertPaths($result);
WidgetHelper::findAndConvertPaths($result);
// used by caller to determine if the airtime they are running or widgets in use is out of date.
$result['AIRTIME_API_VERSION'] = AIRTIME_API_VERSION;
@ -286,11 +287,11 @@ class ApiController extends Zend_Controller_Action
$result = Application_Model_Schedule::GetPlayOrderRange($utcTimeEnd, $showsToRetrieve);
// XSS exploit prevention
$this->convertSpecialChars($result, array("name", "url"));
WidgetHelper::convertSpecialChars($result, array("name", "url"));
// apply user-defined timezone, or default to station
$this->applyLiveTimezoneAdjustments($result, $timezone, $upcase);
// convert image paths to point to api endpoints
$this->findAndConvertPaths($result);
WidgetHelper::findAndConvertPaths($result);
// used by caller to determine if the airtime they are running or widgets in use is out of date.
$result["station"]["AIRTIME_API_VERSION"] = AIRTIME_API_VERSION;
@ -364,55 +365,11 @@ class ApiController extends Zend_Controller_Action
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
//weekStart is in station time.
$weekStartDateTime = Application_Common_DateHelper::getWeekStartDateTime();
$dow = array("monday", "tuesday", "wednesday", "thursday", "friday",
"saturday", "sunday", "nextmonday", "nexttuesday", "nextwednesday",
"nextthursday", "nextfriday", "nextsaturday", "nextsunday");
$result = WidgetHelper::getWeekInfo($this->getRequest()->getParam("timezone"));
$result = array();
// default to the station timezone
$timezone = Application_Model_Preference::GetDefaultTimezone();
$userDefinedTimezone = strtolower($this->getRequest()->getParam("timezone"));
// if the timezone defined by the user exists, use that
if (array_key_exists($userDefinedTimezone, timezone_abbreviations_list())) {
$timezone = $userDefinedTimezone;
}
$utcTimezone = new DateTimeZone("UTC");
$weekStartDateTime->setTimezone($utcTimezone);
$utcDayStart = $weekStartDateTime->format("Y-m-d H:i:s");
for ($i = 0; $i < 14; $i++) {
//have to be in station timezone when adding 1 day for daylight savings.
$weekStartDateTime->setTimezone(new DateTimeZone($timezone));
$weekStartDateTime->add(new DateInterval('P1D'));
//convert back to UTC to get the actual timestamp used for search.
$weekStartDateTime->setTimezone($utcTimezone);
$utcDayEnd = $weekStartDateTime->format("Y-m-d H:i:s");
$shows = Application_Model_Show::getNextShows($utcDayStart, "ALL", $utcDayEnd);
$utcDayStart = $utcDayEnd;
// convert to user-defined timezone, or default to station
Application_Common_DateHelper::convertTimestampsToTimezone(
$shows,
array("starts", "ends", "start_timestamp","end_timestamp"),
$timezone
);
$result[$dow[$i]] = $shows;
}
// XSS exploit prevention
$this->convertSpecialChars($result, array("name", "url"));
// convert image paths to point to api endpoints
$this->findAndConvertPaths($result);
//used by caller to determine if the airtime they are running or widgets in use is out of date.
$result['AIRTIME_API_VERSION'] = AIRTIME_API_VERSION;
header("Content-type: text/javascript");
if (version_compare(phpversion(), '5.4.0', '<')) {
@ -429,50 +386,6 @@ class ApiController extends Zend_Controller_Action
}
}
/**
* Go through a given array and sanitize any potentially exploitable fields
* by passing them through htmlspecialchars
*
* @param unknown $arr the array to sanitize
* @param unknown $keys indexes of values to be sanitized
*/
private function convertSpecialChars(&$arr, $keys)
{
foreach ($arr as &$a) {
if (is_array($a)) {
foreach ($keys as &$key) {
if (array_key_exists($key, $a)) {
$a[$key] = htmlspecialchars($a[$key]);
}
}
$this->convertSpecialChars($a, $keys);
}
}
}
/**
* Recursively find image_path keys in the various $result subarrays,
* and convert them to point to the show-logo endpoint
*
* @param unknown $arr the array to search
*/
private function findAndConvertPaths(&$arr)
{
$CC_CONFIG = Config::getConfig();
$baseDir = Application_Common_OsPath::formatDirectoryWithDirectorySeparators($CC_CONFIG['baseDir']);
foreach ($arr as &$a) {
if (is_array($a)) {
if (array_key_exists("image_path", $a)) {
$a["image_path"] = $a["image_path"] && $a["image_path"] !== '' ?
"http://".$_SERVER['HTTP_HOST'].$baseDir."api/show-logo?id=".$a["id"] : '';
} else {
$this->findAndConvertPaths($a);
}
}
}
}
/**
* API endpoint to display the show logo
*/

View File

@ -1,4 +1,5 @@
<?php
require_once('WidgetHelper.php');
class EmbedController extends Zend_Controller_Action
{
@ -76,6 +77,11 @@ class EmbedController extends Zend_Controller_Action
$this->view->css = Application_Common_HTTPHelper::getStationUrl() . "widgets/css/airtime-widgets.css?".$CC_CONFIG['airtime_version'];
$this->view->jquery = Application_Common_HTTPHelper::getStationUrl() . "widgets/js/jquery-1.6.1.min.js?".$CC_CONFIG['airtime_version'];
$this->view->jquery_custom = Application_Common_HTTPHelper::getStationUrl() . "widgets/js/jquery-ui-1.8.10.custom.min.js?".$CC_CONFIG['airtime_version'];
$this->view->widget_js = Application_Common_HTTPHelper::getStationUrl() . "widgets/js/jquery.showinfo.js?".$CC_CONFIG['airtime_version'];
//$this->view->widget_js = Application_Common_HTTPHelper::getStationUrl() . "widgets/js/jquery.showinfo.js?".$CC_CONFIG['airtime_version'];
$result = WidgetHelper::getWeekInfoV2($this->getRequest()->getParam("timezone"));
//Logging::info($result);
$this->view->scheduleDataWeek1 = $result[0];
$this->view->scheduleDataWeek2 = $result[1];
}
}

View File

@ -8,20 +8,116 @@
<script src="<?php echo $this->widget_js ?>" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#scheduleTabs").airtimeWeekSchedule({
/*var data = <?php //echo $this->scheduleData ?>;
console.log(data);*/
/*$("#scheduleTabs").airtimeWeekSchedule({
sourceDomain:"http://localhost",
dowText:{monday:"Monday", tuesday:"Tuesday", wednesday:"Wednesday", thursday:"Thursday", friday:"Friday", saturday:"Saturday", sunday:"Sunday", nextmonday:"Next Monday", nexttuesday:"Next Tuesday",nextwednesday:"Next Wednesday", nextthursday:"Next Thursday",nextfriday:"Next Friday", nextsaturday:"Next Saturday", nextsunday:"NextSunday"},
miscText:{time:"Time", programName:"Program Name", details:"Details", readMore:"Read More"},
updatePeriod: 600 //seconds
});
});*/
var d = new Date().getDay();
$('#scheduleTabs').tabs({selected: d === 0 ? 6 : d-1, fx: { opacity: 'toggle' }});
$('#scheduleTabs.scheduleTabsWeek1').tabs({selected: d === 0 ? 6 : d-1, fx: { opacity: 'toggle' }});
$('#scheduleTabs.scheduleTabsWeek2').tabs({selected: d === 0 ? 6 : d-1, fx: { opacity: 'toggle' }});
$('.scheduleWidgetToggle').click(function() {
$('#scheduleTabs.scheduleTabsWeek1').toggle();
$('#scheduleTabs.scheduleTabsWeek2').toggle();
});
});
</script>
</head>
<body>
<div id="scheduleTabs"></div>
<div id="scheduleTabs" class="ui-tabs embedScheduleWidget scheduleTabsWeek1">
<ul>
<?php foreach($this->scheduleDataWeek1 as $day => $data) {
echo "<li><a href='#" . $day . "'>" . $data["dayOfWeek"] . "</a></li>";
} ?>
<li><a class="scheduleWidgetToggle" href="#">Next Week</a></li>
</ul>
<?php
foreach($this->scheduleDataWeek1 as $day => $data) {
echo "<div id='".$day."' class='ui-tabs-hide'>";
echo '<table class="widget widget now-playing-list">
<colgroup>
<col width="150" />
<col width="350" />
<col width="240" />
</colgroup>
<thead>
<tr>
<td>Time</td>
<td>Program Name</td>
<td>Details</td>
</tr>
</thead>
<tfoot>
<tr>
<td></td>
</tr>
</tfoot>
<tbody>';
foreach ($data["shows"] as $show => $data) {
echo '<tr>
<td>'.$data["show_start_hour"].' - '.$data["show_end_hour"].'</td>
<td><h4>'.$data["name"].'</h4></td>
<td><ul><li><a href="'.$data["url"].'" target="_blank">Read more</a></li></ul></td>
</tr>';
}
echo '</tbody>
</table>';
echo "</div>";
} ?>
</div>
<div id="scheduleTabs" class="ui-tabs embedScheduleWidget scheduleTabsWeek2" style="display:none">
<ul>
<?php foreach($this->scheduleDataWeek2 as $day => $data) {
echo "<li><a href='#" . $day . "'>" . $data["dayOfWeek"] . "</a></li>";
} ?>
<li><a class="scheduleWidgetToggle" href="#">Previous Week</a></li>
</ul>
<?php foreach($this->scheduleDataWeek2 as $day => $data) {
echo "<div id='".$day."' class='ui-tabs-hide'>";
echo '<table class="widget widget now-playing-list">
<colgroup>
<col width="150" />
<col width="350" />
<col width="240" />
</colgroup>
<thead>
<tr>
<td>Time</td>
<td>Program Name</td>
<td>Details</td>
</tr>
</thead>
<tfoot>
<tr>
<td></td>
</tr>
</tfoot>
<tbody>';
foreach ($data["shows"] as $show => $data) {
echo '<tr>
<td>'.$data["show_start_hour"].' - '.$data["show_end_hour"].'</td>
<td><h4>'.$data["name"].'</h4></td>
<td><ul><li><a href="'.$data["url"].'" target="_blank">Read more</a></li></ul></td>
</tr>';
}
echo '</tbody>
</table>';
echo "</div>";
} ?>
</div>
</body>
</html>

View File

@ -233,4 +233,7 @@
#scheduleTabs.ui-tabs .ui-tabs-hide {
display: none;
}
.embedScheduleWidget {
width: 500px;
}

View File

@ -250,7 +250,7 @@
obj.attr("class", "ui-tabs");
var dow = ["monday", "tuesday", "wednesday", "thursday", "friday","saturday", "sunday", "nextmonday", "nexttuesday", "nextwednesday","nextthursday", "nextfriday", "nextsaturday", "nextsunday"];
var dow = ["monday", "tuesday", "wednesday", "thursday", "friday","saturday", "sunday", "nextmonday", "nexttuesday", "nextwednesday","nextthursday", "nextfriday", "nextsaturday", "nextsunday"];
var date = new Date();
//subtract 1 because javascript date function returns