CC-3636: Media Folder: dir path in cc_music_dir should be in consistent format
- DateHelper.php is moved from model to common folder - created OsPath.php file which contains normpath() - normpath() is used instead of realpath()
This commit is contained in:
parent
a6f44226b9
commit
ffc17ba705
8 changed files with 86 additions and 39 deletions
|
@ -1,308 +0,0 @@
|
|||
<?php
|
||||
|
||||
class Application_Model_DateHelper
|
||||
{
|
||||
private $_dateTime;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->_dateTime = date("U");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get time of object construction in the format
|
||||
* YYYY-MM-DD HH:mm:ss
|
||||
*/
|
||||
function getTimestamp()
|
||||
{
|
||||
return date("Y-m-d H:i:s", $this->_dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get time of object construction in the format
|
||||
* YYYY-MM-DD HH:mm:ss
|
||||
*/
|
||||
function getUtcTimestamp()
|
||||
{
|
||||
return gmdate("Y-m-d H:i:s", $this->_dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get date of object construction in the format
|
||||
* YYYY-MM-DD
|
||||
*/
|
||||
function getDate()
|
||||
{
|
||||
return gmdate("Y-m-d", $this->_dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get time of object construction in the format
|
||||
* HH:mm:ss
|
||||
*/
|
||||
function getTime()
|
||||
{
|
||||
return gmdate("H:i:s", $this->_dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the week start date of this week in the format
|
||||
* YYYY-MM-DD
|
||||
*
|
||||
* @return String - week start date
|
||||
*/
|
||||
function getWeekStartDate()
|
||||
{
|
||||
$startDate = date('w') == 0 ? date('Y-m-d') : date('Y-m-d', strtotime('last sunday'));
|
||||
$startDateTime = new DateTime($startDate);
|
||||
return $startDateTime->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the internal timestamp of the object.
|
||||
*/
|
||||
function setDate($dateString)
|
||||
{
|
||||
$dateTime = new DateTime($dateString, new DateTimeZone("UTC"));
|
||||
$this->_dateTime = $dateTime->getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate and return the timestamp for end of day today
|
||||
* in local time.
|
||||
*
|
||||
* For example, if local time is 2PM on 2011-11-01,
|
||||
* then the function would return 2011-11-02 00:00:00
|
||||
*
|
||||
* @return End of day timestamp in local timezone
|
||||
*/
|
||||
public static function GetDayEndTimestamp($time = "") {
|
||||
$dateTime = $time == "" ? new DateTime(date("Y-m-d")) : new DateTime($time);
|
||||
$dateTime->add(new DateInterval('P1D'));
|
||||
return $dateTime->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
public static function GetDayEndTimestampInUtc($time = "") {
|
||||
$dayEndTimestamp = Application_Model_DateHelper::GetDayEndTimestamp($time);
|
||||
return Application_Model_DateHelper::ConvertToUtcDateTimeString($dayEndTimestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the epoch timestamp difference from "now" to the beginning of today.
|
||||
*/
|
||||
function getNowDayStartDiff()
|
||||
{
|
||||
$dayStartTs = ((int)($this->_dateTime/86400))*86400;
|
||||
return $this->_dateTime - $dayStartTs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the epoch timestamp difference from "now" to the end of today.
|
||||
*/
|
||||
function getNowDayEndDiff()
|
||||
{
|
||||
$dayEndTs = ((int)(($this->_dateTime+86400)/86400))*86400;
|
||||
return $dayEndTs - $this->_dateTime;
|
||||
}
|
||||
|
||||
function getEpochTime()
|
||||
{
|
||||
return $this->_dateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the offset in seconds, between local and UTC timezones.
|
||||
* E.g., if local timezone is -4, this function
|
||||
* returns -14400.
|
||||
*
|
||||
* @return type offset in int, between local and UTC timezones
|
||||
*/
|
||||
function getLocalTimeZoneOffset() {
|
||||
$dateTime = new DateTime("@".$this->_dateTime, new DateTimeZone("UTC"));
|
||||
$timezone = new DateTimeZone(date_default_timezone_get());
|
||||
return $timezone->getOffset($dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the offset hour in int, between local and UTC timezones.
|
||||
* E.g., if local timezone is -4:30, this function
|
||||
* returns -4.
|
||||
*
|
||||
* @return type offset hour in int, between local and UTC timezones
|
||||
*/
|
||||
function getLocalOffsetHour() {
|
||||
$offset = $this->getLocalTimeZoneOffset();
|
||||
return (int)($offset / 3600);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the offset minute in int, between local and UTC timezones.
|
||||
* E.g., if local timezone is -4:30, this function
|
||||
* returns -30.
|
||||
*
|
||||
* @return type offset minute in int, between local and UTC timezones
|
||||
*/
|
||||
function getLocalOffsetMinute() {
|
||||
$offset = $this->getLocalTimeZoneOffset();
|
||||
return (int)(($offset % 3600) / 60);
|
||||
}
|
||||
|
||||
public static function TimeDiff($time1, $time2)
|
||||
{
|
||||
return strtotime($time2) - strtotime($time1);
|
||||
}
|
||||
|
||||
public static function ConvertMSToHHMMSSmm($time)
|
||||
{
|
||||
$hours = floor($time / 3600000);
|
||||
$time -= 3600000*$hours;
|
||||
|
||||
$minutes = floor($time / 60000);
|
||||
$time -= 60000*$minutes;
|
||||
|
||||
$seconds = floor($time / 1000);
|
||||
$time -= 1000*$seconds;
|
||||
|
||||
$ms = $time;
|
||||
|
||||
if (strlen($hours) == 1)
|
||||
$hours = "0".$hours;
|
||||
if (strlen($minutes) == 1)
|
||||
$minutes = "0".$minutes;
|
||||
if (strlen($seconds) == 1)
|
||||
$seconds = "0".$seconds;
|
||||
|
||||
return $hours.":".$minutes.":".$seconds.".".$ms;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function formats a time by removing seconds
|
||||
*
|
||||
* When we receive a time from the database we get the
|
||||
* format "hh:mm:ss". But when dealing with show times, we
|
||||
* do not care about the seconds.
|
||||
*
|
||||
* @param int $p_dateTime
|
||||
* The value which to format.
|
||||
* @return int
|
||||
* The timestamp with the new format "hh:mm", or
|
||||
* the original input parameter, if it does not have
|
||||
* the correct format.
|
||||
*/
|
||||
public static function removeSecondsFromTime($p_dateTime)
|
||||
{
|
||||
//Format is in hh:mm:ss. We want hh:mm
|
||||
$timeExplode = explode(":", $p_dateTime);
|
||||
|
||||
if (count($timeExplode) == 3)
|
||||
return $timeExplode[0].":".$timeExplode[1];
|
||||
else
|
||||
return $p_dateTime;
|
||||
}
|
||||
|
||||
public static function getDateFromTimestamp($p_dateTime){
|
||||
$explode = explode(" ", $p_dateTime);
|
||||
return $explode[0];
|
||||
}
|
||||
|
||||
public static function getTimeFromTimestamp($p_dateTime){
|
||||
$explode = explode(" ", $p_dateTime);
|
||||
return $explode[1];
|
||||
}
|
||||
|
||||
/* Given a track length in the format HH:MM:SS.mm, we want to
|
||||
* convert this to seconds. This is useful for Liquidsoap which
|
||||
* likes input parameters give in seconds.
|
||||
* For example, 00:06:31.444, should be converted to 391.444 seconds
|
||||
* @param int $p_time
|
||||
* The time interval in format HH:MM:SS.mm we wish to
|
||||
* convert to seconds.
|
||||
* @return float
|
||||
* The input parameter converted to seconds.
|
||||
*/
|
||||
public static function calculateLengthInSeconds($p_time){
|
||||
|
||||
if (2 !== substr_count($p_time, ":")){
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (1 === substr_count($p_time, ".")){
|
||||
list($hhmmss, $ms) = explode(".", $p_time);
|
||||
} else {
|
||||
$hhmmss = $p_time;
|
||||
$ms = 0;
|
||||
}
|
||||
|
||||
list($hours, $minutes, $seconds) = explode(":", $hhmmss);
|
||||
|
||||
// keep ms in 3 digits
|
||||
$ms = substr($ms, 0, 3);
|
||||
|
||||
$totalSeconds = $hours*3600 + $minutes*60 + $seconds + $ms/1000;
|
||||
|
||||
return $totalSeconds;
|
||||
}
|
||||
|
||||
public static function ConvertToUtcDateTime($p_dateString, $timezone=null){
|
||||
if (isset($timezone)) {
|
||||
$dateTime = new DateTime($p_dateString, new DateTimeZone($timezone));
|
||||
}
|
||||
else {
|
||||
$dateTime = new DateTime($p_dateString, new DateTimeZone(date_default_timezone_get()));
|
||||
}
|
||||
$dateTime->setTimezone(new DateTimeZone("UTC"));
|
||||
|
||||
return $dateTime;
|
||||
}
|
||||
|
||||
public static function ConvertToSpecificTimezoneDateTime($p_dateString, $timezone){
|
||||
$dateTime = new DateTime($p_dateString, new DateTimeZone("UTC"));
|
||||
$dateTime->setTimezone(new DateTimeZone($timezone));
|
||||
|
||||
return $dateTime;
|
||||
}
|
||||
|
||||
public static function ConvertToLocalDateTime($p_dateString){
|
||||
$dateTime = new DateTime($p_dateString, new DateTimeZone("UTC"));
|
||||
$dateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
||||
|
||||
return $dateTime;
|
||||
}
|
||||
|
||||
/* Convenience method to return a date formatted into a String rather than a
|
||||
* DateTime object. Note that if an empty string is provided for $p_dateString
|
||||
* then the current time is provided.
|
||||
*
|
||||
* @param $p_dateString
|
||||
* Date string in UTC timezone.
|
||||
* @param $p_format
|
||||
* Format which the string should be returned in.
|
||||
*
|
||||
* @return string
|
||||
* Date String in localtime
|
||||
* */
|
||||
public static function ConvertToLocalDateTimeString($p_dateString, $p_format="Y-m-d H:i:s"){
|
||||
if (is_null($p_dateString) || strlen($p_dateString) == 0)
|
||||
return $p_dateString;
|
||||
return self::ConvertToLocalDateTime($p_dateString)->format($p_format);
|
||||
}
|
||||
|
||||
public static function ConvertToUtcDateTimeString($p_dateString, $p_format="Y-m-d H:i:s"){
|
||||
if (is_null($p_dateString) || strlen($p_dateString) == 0)
|
||||
return $p_dateString;
|
||||
return self::ConvertToUtcDateTime($p_dateString)->format($p_format);
|
||||
}
|
||||
|
||||
/*
|
||||
* Example input: "00:02:32.746562". Output is a DateInterval object
|
||||
* representing that 2 minute, 32.746562 second interval.
|
||||
*
|
||||
*/
|
||||
public static function getDateIntervalFromString($p_interval){
|
||||
list($hour_min_sec, $subsec) = explode(".", $p_interval);
|
||||
list($hour, $min, $sec) = explode(":", $hour_min_sec);
|
||||
|
||||
return new DateInterval("PT{$hour}H{$min}M{$sec}S");
|
||||
}
|
||||
}
|
||||
|
|
@ -175,7 +175,7 @@ class Application_Model_MusicDir {
|
|||
if(!is_dir($p_path)){
|
||||
return array("code"=>2, "error"=>"'$p_path' is not a valid directory.");
|
||||
}
|
||||
$real_path = realpath($p_path)."/";
|
||||
$real_path = Application_Common_OsPath::normpath($p_path)."/";
|
||||
if($real_path != "/"){
|
||||
$p_path = $real_path;
|
||||
}
|
||||
|
@ -190,7 +190,7 @@ class Application_Model_MusicDir {
|
|||
}
|
||||
|
||||
$dir->setType($p_type);
|
||||
$p_path = realpath($p_path)."/";
|
||||
$p_path = Application_Common_OsPath::normpath($p_path)."/";
|
||||
|
||||
|
||||
try {
|
||||
|
@ -245,7 +245,7 @@ class Application_Model_MusicDir {
|
|||
|
||||
//newly added watched directory object
|
||||
$propel_new_watch = CcMusicDirsQuery::create()
|
||||
->filterByDirectory(realpath($p_path)."/")
|
||||
->filterByDirectory(Application_Common_OsPath::normpath($p_path)."/")
|
||||
->findOne();
|
||||
|
||||
//any files of the deprecated "link" type.
|
||||
|
@ -344,7 +344,7 @@ class Application_Model_MusicDir {
|
|||
{
|
||||
// we want to be consistent when storing dir path.
|
||||
// path should always ends with trailing '/'
|
||||
$p_dir = realpath($p_dir)."/";
|
||||
$p_dir = Application_Common_OsPath::normpath($p_dir)."/";
|
||||
if(!is_dir($p_dir)){
|
||||
return array("code"=>2, "error"=>"'$p_dir' is not a valid directory.");
|
||||
}else if(Application_Model_Preference::GetImportTimestamp()+10 > time()){
|
||||
|
@ -396,7 +396,7 @@ class Application_Model_MusicDir {
|
|||
* otherwise, it will set "Exists" flag to true
|
||||
**/
|
||||
public static function removeWatchedDir($p_dir, $userAddedWatchedDir=true){
|
||||
$real_path = realpath($p_dir)."/";
|
||||
$real_path = Application_Common_OsPath::normpath($p_dir)."/";
|
||||
if($real_path != "/"){
|
||||
$p_dir = $real_path;
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ class Application_Model_Schedule {
|
|||
return array();
|
||||
}
|
||||
|
||||
$date = new Application_Model_DateHelper;
|
||||
$date = new Application_Common_DateHelper;
|
||||
$timeNow = $date->getTimestamp();
|
||||
$utcTimeNow = $date->getUtcTimestamp();
|
||||
|
||||
|
@ -563,8 +563,8 @@ class Application_Model_Schedule {
|
|||
'uri' => $uri,
|
||||
'fade_in' => Application_Model_Schedule::WallTimeToMillisecs($item["fade_in"]),
|
||||
'fade_out' => Application_Model_Schedule::WallTimeToMillisecs($item["fade_out"]),
|
||||
'cue_in' => Application_Model_DateHelper::CalculateLengthInSeconds($item["cue_in"]),
|
||||
'cue_out' => Application_Model_DateHelper::CalculateLengthInSeconds($item["cue_out"]),
|
||||
'cue_in' => Application_Common_DateHelper::CalculateLengthInSeconds($item["cue_in"]),
|
||||
'cue_out' => Application_Common_DateHelper::CalculateLengthInSeconds($item["cue_out"]),
|
||||
'start' => $start,
|
||||
'end' => Application_Model_Schedule::AirtimeTimeToPypoTime($item["end"]),
|
||||
'show_name' => $showName
|
||||
|
|
|
@ -520,7 +520,7 @@ class Application_Model_Show {
|
|||
$timestamp = gmdate("Y-m-d H:i:s");
|
||||
|
||||
if(is_null($p_date)) {
|
||||
$date = new Application_Model_DateHelper;
|
||||
$date = new Application_Common_DateHelper;
|
||||
$p_date = $date->getDate();
|
||||
}
|
||||
|
||||
|
@ -663,7 +663,7 @@ class Application_Model_Show {
|
|||
* true if the StartDate is in the past, false otherwise
|
||||
*/
|
||||
public function isStartDateTimeInPast(){
|
||||
$date = new Application_Model_DateHelper;
|
||||
$date = new Application_Common_DateHelper;
|
||||
$current_timestamp = $date->getUtcTimestamp();
|
||||
return ($current_timestamp > ($this->getStartDate()." ".$this->getStartTime()));
|
||||
}
|
||||
|
@ -678,7 +678,7 @@ class Application_Model_Show {
|
|||
public function getAllFutureInstanceIds(){
|
||||
global $CC_DBC;
|
||||
|
||||
$date = new Application_Model_DateHelper;
|
||||
$date = new Application_Common_DateHelper;
|
||||
$timestamp = $date->getTimestamp();
|
||||
|
||||
$showId = $this->getId();
|
||||
|
@ -708,7 +708,7 @@ class Application_Model_Show {
|
|||
|
||||
global $CC_DBC;
|
||||
|
||||
$date = new Application_Model_DateHelper;
|
||||
$date = new Application_Common_DateHelper;
|
||||
$timestamp = $date->getTimestamp();
|
||||
|
||||
$sql = "UPDATE cc_show_days "
|
||||
|
@ -729,7 +729,7 @@ class Application_Model_Show {
|
|||
|
||||
global $CC_DBC;
|
||||
|
||||
$date = new Application_Model_DateHelper;
|
||||
$date = new Application_Common_DateHelper;
|
||||
$timestamp = $date->getTimestamp();
|
||||
|
||||
//TODO fix this from overwriting info.
|
||||
|
@ -1313,9 +1313,9 @@ class Application_Model_Show {
|
|||
$start = $first_show." ".$start_time;
|
||||
}
|
||||
|
||||
$utcStartDateTime = Application_Model_DateHelper::ConvertToUtcDateTime($start, $timezone);
|
||||
$utcStartDateTime = Application_Common_DateHelper::ConvertToUtcDateTime($start, $timezone);
|
||||
//convert $last_show into a UTC DateTime object, or null if there is no last show.
|
||||
$utcLastShowDateTime = $last_show ? Application_Model_DateHelper::ConvertToUtcDateTime($last_show, $timezone) : null;
|
||||
$utcLastShowDateTime = $last_show ? Application_Common_DateHelper::ConvertToUtcDateTime($last_show, $timezone) : null;
|
||||
|
||||
$sql = "SELECT * FROM cc_show_rebroadcast WHERE show_id={$show_id}";
|
||||
$rebroadcasts = $CC_DBC->GetAll($sql);
|
||||
|
@ -1736,7 +1736,7 @@ class Application_Model_Show {
|
|||
{
|
||||
global $CC_CONFIG, $CC_DBC;
|
||||
if($timeNow == null){
|
||||
$date = new Application_Model_DateHelper;
|
||||
$date = new Application_Common_DateHelper;
|
||||
$timeNow = $date->getUtcTimestamp();
|
||||
}
|
||||
//TODO, returning starts + ends twice (once with an alias). Unify this after the 2.0 release. --Martin
|
||||
|
@ -1896,7 +1896,7 @@ class Application_Model_Show {
|
|||
}
|
||||
foreach($rows as &$row) {
|
||||
foreach($columnsToConvert as $column) {
|
||||
$row[$column] = Application_Model_DateHelper::ConvertToLocalDateTimeString($row[$column]);
|
||||
$row[$column] = Application_Common_DateHelper::ConvertToLocalDateTimeString($row[$column]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue