CC-5450 : Refactor Media Management (Classes/DB) in Airtime

finishing up last relative date rules.
This commit is contained in:
Naomi 2014-03-25 14:11:10 -04:00
parent 5d236c5311
commit b15e1e638c
3 changed files with 185 additions and 45 deletions

View File

@ -156,12 +156,18 @@ class PlaylistController extends Zend_Controller_Action
{ {
$content = $this->_getParam('content'); $content = $this->_getParam('content');
$con = Propel::getConnection(PlaylistPeer::DATABASE_NAME);
$con->beginTransaction();
try { try {
$playlist = $this->getPlaylist(); $playlist = $this->getPlaylist();
$playlist->savePlaylistContent($content); $playlist->savePlaylistContent($con, $content);
$this->createUpdateResponse($playlist); $this->createUpdateResponse($playlist);
$con->commit();
} }
catch (Exception $e) { catch (Exception $e) {
$con->rollBack();
$this->view->error = $e->getMessage(); $this->view->error = $e->getMessage();
} }
} }

View File

@ -66,6 +66,10 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
protected function getCriteriaRules(&$query) { protected function getCriteriaRules(&$query) {
$displayTimezone = new DateTimeZone(\Application_Model_Preference::GetUserTimezone());
$utcTimezone = new DateTimeZone("UTC");
$now = new DateTime("now", $displayTimezone);
/* /*
* @param DateTime $now * @param DateTime $now
*/ */
@ -116,15 +120,13 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
return $yearStart; return $yearStart;
}; };
$displayTimezone = new DateTimeZone(\Application_Model_Preference::GetUserTimezone());
$utcTimezone = new DateTimeZone("UTC");
$now = new DateTime("now", $displayTimezone);
//$pattern is like "%VALUE%", or just "VALUE" if % is not needed. //$pattern is like "%VALUE%", or just "VALUE" if % is not needed.
function createRule(&$query, $comparison, $pattern = "VALUE") { function createRule(&$query, $comparison, $pattern = "VALUE") {
return function($col, $value1, $value2 = null) use (&$query, $comparison, $pattern) { return function($col, $data) use (&$query, $comparison, $pattern) {
$m = $query->getModelName(); $m = $query->getModelName();
$value1 = $data["input1"];
$name = mt_rand(10000, 99999); $name = mt_rand(10000, 99999);
$cond = "{$m}.{$col} {$comparison} ?"; $cond = "{$m}.{$col} {$comparison} ?";
@ -146,12 +148,15 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$isGreaterThanEqualTo = createRule($query, Criteria::GREATER_EQUAL); $isGreaterThanEqualTo = createRule($query, Criteria::GREATER_EQUAL);
$isLessThanEqualTo = createRule($query, Criteria::LESS_EQUAL); $isLessThanEqualTo = createRule($query, Criteria::LESS_EQUAL);
$range = function ($col, $value1, $value2) use (&$query) { $range = function ($col, $data) use (&$query) {
$name1 = mt_rand(10000, 99999); $name1 = mt_rand(10000, 99999);
$name2 = mt_rand(10000, 99999); $name2 = mt_rand(10000, 99999);
$name3 = mt_rand(10000, 99999); $name3 = mt_rand(10000, 99999);
$value1 = $data["input1"];
$value2 = $data["input2"];
$m = $query->getModelName(); $m = $query->getModelName();
$comparison1 = Criteria::GREATER_EQUAL; $comparison1 = Criteria::GREATER_EQUAL;
@ -182,7 +187,12 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$from = $now->format('Y-m-d H:i:s'); $from = $now->format('Y-m-d H:i:s');
$to = $tomorrow->format('Y-m-d H:i:s'); $to = $tomorrow->format('Y-m-d H:i:s');
return $range($col, $from, $to); $data = array(
"input1" => $from,
"input2" => $to
);
return $range($col, $data);
}; };
$yesterday = function($col) use (&$query, $utcTimezone, $now, $range) { $yesterday = function($col) use (&$query, $utcTimezone, $now, $range) {
@ -199,7 +209,12 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$from = $yesterday->format('Y-m-d H:i:s'); $from = $yesterday->format('Y-m-d H:i:s');
$to = $now->format('Y-m-d H:i:s'); $to = $now->format('Y-m-d H:i:s');
return $range($col, $from, $to); $data = array(
"input1" => $from,
"input2" => $to
);
return $range($col, $data);
}; };
$thisWeek = function($col) use (&$query, $utcTimezone, $now, $range, $getWeekStartDateTime) { $thisWeek = function($col) use (&$query, $utcTimezone, $now, $range, $getWeekStartDateTime) {
@ -212,7 +227,12 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$from = $weekStart->format('Y-m-d H:i:s'); $from = $weekStart->format('Y-m-d H:i:s');
$to = $now->format('Y-m-d H:i:s'); $to = $now->format('Y-m-d H:i:s');
return $range($col, $from, $to); $data = array(
"input1" => $from,
"input2" => $to
);
return $range($col, $data);
}; };
$lastWeek = function($col) use (&$query, $utcTimezone, $now, $range, $getWeekStartDateTime) { $lastWeek = function($col) use (&$query, $utcTimezone, $now, $range, $getWeekStartDateTime) {
@ -229,7 +249,12 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$from = $weekStart->format('Y-m-d H:i:s'); $from = $weekStart->format('Y-m-d H:i:s');
$to = $weekEnd->format('Y-m-d H:i:s'); $to = $weekEnd->format('Y-m-d H:i:s');
return $range($col, $from, $to); $data = array(
"input1" => $from,
"input2" => $to
);
return $range($col, $data);
}; };
$thisMonth = function($col) use (&$query, $utcTimezone, $now, $range, $getMonthStartDateTime) { $thisMonth = function($col) use (&$query, $utcTimezone, $now, $range, $getMonthStartDateTime) {
@ -242,7 +267,12 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$from = $monthStart->format('Y-m-d H:i:s'); $from = $monthStart->format('Y-m-d H:i:s');
$to = $now->format('Y-m-d H:i:s'); $to = $now->format('Y-m-d H:i:s');
return $range($col, $from, $to); $data = array(
"input1" => $from,
"input2" => $to
);
return $range($col, $data);
}; };
$lastMonth = function($col) use (&$query, $utcTimezone, $now, $range, $getMonthStartDateTime) { $lastMonth = function($col) use (&$query, $utcTimezone, $now, $range, $getMonthStartDateTime) {
@ -259,7 +289,12 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$from = $monthStart->format('Y-m-d H:i:s'); $from = $monthStart->format('Y-m-d H:i:s');
$to = $monthEnd->format('Y-m-d H:i:s'); $to = $monthEnd->format('Y-m-d H:i:s');
return $range($col, $from, $to); $data = array(
"input1" => $from,
"input2" => $to
);
return $range($col, $data);
}; };
$thisYear = function($col) use (&$query, $utcTimezone, $now, $range, $getYearStartDateTime) { $thisYear = function($col) use (&$query, $utcTimezone, $now, $range, $getYearStartDateTime) {
@ -272,7 +307,12 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$from = $yearStart->format('Y-m-d H:i:s'); $from = $yearStart->format('Y-m-d H:i:s');
$to = $now->format('Y-m-d H:i:s'); $to = $now->format('Y-m-d H:i:s');
return $range($col, $from, $to); $data = array(
"input1" => $from,
"input2" => $to
);
return $range($col, $data);
}; };
$lastYear = function($col) use (&$query, $utcTimezone, $now, $range, $getYearStartDateTime) { $lastYear = function($col) use (&$query, $utcTimezone, $now, $range, $getYearStartDateTime) {
@ -289,27 +329,115 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$from = $yearStart->format('Y-m-d H:i:s'); $from = $yearStart->format('Y-m-d H:i:s');
$to = $yearEnd->format('Y-m-d H:i:s'); $to = $yearEnd->format('Y-m-d H:i:s');
return $range($col, $from, $to); $data = array(
"input1" => $from,
"input2" => $to
);
return $range($col, $data);
}; };
//time is like hh:mm(:ss) /*
$inTheLast = function($col, $value, $unit) use (&$query, $utcTimezone, $now, $range) { 1 => _("seconds"),
2 => _("minutes"),
$info = explode(":", $now); 3 => _("hours"),
$hours = $info[0]; 4 => _("days"),
$minutes = isset($info[1]) ? intval($info[1]) : 0; 5 => _("weeks"),
$seconds = isset($info[2]) ? intval($info[2]) : 0; 6 => _("months"),
7 => _("years"),
*/
$convertRelativeUnit = function($duration, $unit) {
$timespan = $seconds + $minutes*60 + $hours*3600; $timespan = intval($duration);
$interval = new DateInterval("PT{$timespan}S"); switch($unit) {
$last = clone $now;
$last->sub($interval); case 1:
$format = "PT{$timespan}S";
break;
case 2:
$format = "PT{$timespan}M";
break;
case 3:
$format = "PT{$timespan}H";
break;
case 4:
$format = "P{$timespan}D";
break;
case 5:
$timespan *= 7;
$format = "P{$timespan}D";
break;
case 6:
$format = "P{$timespan}M";
break;
case 7:
$format = "P{$timespan}Y";
break;
}
return new DateInterval($format);
};
$from = $last->format('Y-m-d H:i:s'); $getRelativeDateTimes = function($now, $data, $utcTimezone, $convertRelativeUnit) {
$to = $now->format('Y-m-d H:i:s');
$value1 = $data["input1"];
$unit1 = $data["unit1"];
$interval1 = $convertRelativeUnit($value1, $unit1);
$from = clone $now;
$to = clone $now;
//relative range rule.
if (isset($data["input2"]) && isset($data["unit2"])) {
$value2 = $data["input2"];
$unit2 = $data["unit2"];
$interval2 = $convertRelativeUnit($value2, $unit2);
$from->sub($interval2);
$to->sub($interval1);
}
else {
$from->sub($interval1);
}
$from->setTimezone($utcTimezone);
$to->setTimezone($utcTimezone);
return array($from, $to);
};
$inTheLast = function($col, $data) use (&$query, $utcTimezone, $now, $getRelativeDateTimes, $convertRelativeUnit, $range) {
return $range($col, $from, $to); list($from, $to) = $getRelativeDateTimes($now, $data, $utcTimezone, $convertRelativeUnit);
$data = array(
"input1" => $from->format('Y-m-d H:i:s'),
"input2" => $to->format('Y-m-d H:i:s')
);
return $range($col, $data);
};
$notInTheLast = function($col, $data) use (&$query, $utcTimezone, $now, $getRelativeDateTimes, $convertRelativeUnit, $isLessThan, $isGreaterThan) {
list($from, $to) = $getRelativeDateTimes($now, $data, $utcTimezone, $convertRelativeUnit);
$data1 = array(
"input1" => $from->format('Y-m-d H:i:s')
);
$data2 = array(
"input1" => $to->format('Y-m-d H:i:s')
);
$name1 = $isLessThan($col, $data1);
$name2 = $isGreaterThan($col, $data2);
$name3 = mt_rand(10000, 99999);
$query->combine(array($name1, $name2), 'or', $name3);
return $name3;
}; };
return array( return array(
@ -333,7 +461,8 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$lastMonth, $lastMonth,
$thisYear, $thisYear,
$lastYear, $lastYear,
$inTheLast $inTheLast,
$notInTheLast
); );
} }
@ -439,10 +568,7 @@ abstract class Playlist extends BasePlaylist implements \Interface_Playlistable
$rule = $criteriaRules[$orBlock["modifier"]]; $rule = $criteriaRules[$orBlock["modifier"]];
$column = $orBlock["criteria"]; $column = $orBlock["criteria"];
$input1 = isset($orBlock["input1"]) ? $orBlock["input1"] : null; $condition = $rule($column, $orBlock);
$input2 = isset($orBlock["input2"]) ? $orBlock["input2"] : null;
$condition = $rule($column, $input1, $input2);
$conditionOr[] = $condition; $conditionOr[] = $condition;
} }

View File

@ -20,25 +20,33 @@
<?php echo $this->element->getElement("sp_criteria_field_{$suffix}") ?> <?php echo $this->element->getElement("sp_criteria_field_{$suffix}") ?>
<?php echo $this->element->getElement("sp_criteria_modifier_{$suffix}") ?> <?php echo $this->element->getElement("sp_criteria_modifier_{$suffix}") ?>
<?php $input = $this->element->getElement("sp_criteria_value_{$suffix}") ?> <?php
<?php if(isset($input)): ?> $input1 = $this->element->getElement("sp_criteria_value_{$suffix}");
<?php echo $input; ?> $input2 = $this->element->getElement("sp_criteria_extra_{$suffix}");
$unit1 = $this->element->getElement("sp_rel_date_unit_1_{$suffix}");
$unit2 = $this->element->getElement("sp_rel_date_unit_2_{$suffix}")
?>
<?php if (isset($input1)): ?>
<?php echo $input1; ?>
<?php endif; ?> <?php endif; ?>
<?php $unit1 = $this->element->getElement("sp_rel_date_unit_1_{$suffix}") ?> <?php if (isset($unit1)): ?>
<?php if(isset($unit1)): ?>
<?php echo $unit1; ?> <?php echo $unit1; ?>
<?php endif; ?> <?php endif; ?>
<?php $extra = $this->element->getElement("sp_criteria_extra_{$suffix}") ?> <?php if (isset($input2)): ?>
<?php if(isset($extra)): ?> <span class='sp_text_font' id="extra_criteria">
<span class='sp_text_font' id="extra_criteria"><?php echo _("to"); ?> <?php echo _("to"); ?>
<?php echo $extra; ?> <?php echo $input2; ?>
</span> </span>
<?php else: ?>
<a class="btn btn-small btn-range">
<span>TO</span>
</a>
<?php endif; ?> <?php endif; ?>
<?php $unit2 = $this->element->getElement("sp_rel_date_unit_2_{$suffix}") ?> <?php if (isset($unit2)): ?>
<?php if(isset($unit2)): ?>
<?php echo $unit2; ?> <?php echo $unit2; ?>
<?php endif; ?> <?php endif; ?>