2013-07-03 21:19:05 +02:00
|
|
|
<?php
|
|
|
|
|
2013-07-03 22:00:48 +02:00
|
|
|
require_once 'formatters/LengthFormatter.php';
|
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
class Application_Service_HistoryService
|
|
|
|
{
|
|
|
|
private $con;
|
|
|
|
private $timezone;
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-09 00:00:02 +02:00
|
|
|
private $mDataPropMap = array (
|
|
|
|
"artist" => "artist_name",
|
|
|
|
"title" => "track_title",
|
|
|
|
"played" => "played",
|
|
|
|
"length" => "length",
|
|
|
|
"composer" => "composer",
|
|
|
|
"copyright" => "copyright",
|
2013-07-18 07:31:20 +02:00
|
|
|
"starts" => "starts",
|
|
|
|
"ends" => "ends"
|
2013-07-03 22:00:48 +02:00
|
|
|
);
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:20:40 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2013-07-03 21:19:05 +02:00
|
|
|
$this->con = isset($con) ? $con : Propel::getConnection(CcPlayoutHistoryPeer::DATABASE_NAME);
|
2013-07-24 00:01:43 +02:00
|
|
|
$this->timezone = Application_Model_Preference::GetTimezone();
|
2013-07-03 22:00:48 +02:00
|
|
|
}
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 22:02:17 +02:00
|
|
|
/*
|
|
|
|
* map front end mDataProp labels to proper column names for searching etc.
|
|
|
|
*/
|
|
|
|
private function translateColumns($opts)
|
|
|
|
{
|
|
|
|
for ($i = 0; $i < $opts["iColumns"]; $i++) {
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-09 00:00:02 +02:00
|
|
|
if ($opts["bSearchable_{$i}"] === "true") {
|
|
|
|
$opts["mDataProp_{$i}"] = $this->mDataPropMap[$opts["mDataProp_{$i}"]];
|
2013-07-18 07:31:20 +02:00
|
|
|
}
|
2013-07-03 22:02:17 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-18 07:31:20 +02:00
|
|
|
|
|
|
|
public function getListView($startDT, $endDT, $opts)
|
|
|
|
{
|
2013-07-18 07:37:39 +02:00
|
|
|
$this->translateColumns($opts);
|
|
|
|
|
|
|
|
$select = array (
|
|
|
|
"file.track_title as title",
|
|
|
|
"file.artist_name as artist",
|
|
|
|
"playout.starts",
|
|
|
|
"playout.ends",
|
|
|
|
"playout.history_id"
|
|
|
|
);
|
|
|
|
|
|
|
|
$start = $startDT->format("Y-m-d H:i:s");
|
|
|
|
$end = $endDT->format("Y-m-d H:i:s");
|
|
|
|
|
|
|
|
$historyTable = "(
|
2013-07-18 07:31:20 +02:00
|
|
|
select history.starts as starts, history.ends as ends,
|
2013-07-18 07:37:39 +02:00
|
|
|
history.id as history_id, history.file_id as file_id
|
|
|
|
from cc_playout_history as history
|
|
|
|
where history.starts >= '{$start}' and history.starts < '{$end}'
|
|
|
|
) AS playout
|
|
|
|
left join cc_files as file on (file.id = playout.file_id)";
|
|
|
|
|
|
|
|
$results = Application_Model_Datatables::findEntries($this->con, $select, $historyTable, $opts, "history");
|
2013-07-24 00:01:43 +02:00
|
|
|
|
|
|
|
$timezoneUTC = new DateTimeZone("UTC");
|
|
|
|
$timezoneLocal = new DateTimeZone($this->timezone);
|
|
|
|
|
|
|
|
//need to display the results in the station's timezone.
|
|
|
|
foreach ($results["history"] as $index => &$result) {
|
|
|
|
|
|
|
|
$dateTime = new DateTime($result["starts"], $timezoneUTC);
|
|
|
|
$dateTime->setTimezone($timezoneLocal);
|
|
|
|
$result["starts"] = $dateTime->format("Y-m-d H:i:s");
|
|
|
|
|
|
|
|
$dateTime = new DateTime($result["ends"], $timezoneUTC);
|
|
|
|
$dateTime->setTimezone($timezoneLocal);
|
|
|
|
$result["ends"] = $dateTime->format("Y-m-d H:i:s");
|
|
|
|
}
|
2013-07-18 07:37:39 +02:00
|
|
|
|
2013-07-18 07:31:20 +02:00
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2013-07-09 00:00:02 +02:00
|
|
|
public function getAggregateView($startDT, $endDT, $opts)
|
2013-07-03 22:02:17 +02:00
|
|
|
{
|
|
|
|
$this->translateColumns($opts);
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-09 00:00:02 +02:00
|
|
|
$select = array (
|
|
|
|
"file.track_title as title",
|
|
|
|
"file.artist_name as artist",
|
2013-07-18 07:31:20 +02:00
|
|
|
"playout.played as played",
|
2013-07-09 00:00:02 +02:00
|
|
|
"playout.file_id",
|
2013-07-18 07:31:20 +02:00
|
|
|
"file.composer as composer",
|
|
|
|
"file.copyright as copyright",
|
|
|
|
"file.length as length"
|
2013-07-03 22:02:17 +02:00
|
|
|
);
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 22:02:17 +02:00
|
|
|
$start = $startDT->format("Y-m-d H:i:s");
|
2013-07-18 07:31:20 +02:00
|
|
|
$end = $endDT->format("Y-m-d H:i:s");
|
|
|
|
|
2013-07-03 22:02:17 +02:00
|
|
|
$historyTable = "(
|
|
|
|
select count(history.file_id) as played, history.file_id as file_id
|
|
|
|
from cc_playout_history as history
|
|
|
|
where history.starts >= '{$start}' and history.starts < '{$end}'
|
|
|
|
group by history.file_id
|
2013-07-18 07:31:20 +02:00
|
|
|
) AS playout
|
2013-07-03 22:02:17 +02:00
|
|
|
left join cc_files as file on (file.id = playout.file_id)";
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 22:02:17 +02:00
|
|
|
$results = Application_Model_Datatables::findEntries($this->con, $select, $historyTable, $opts, "history");
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 22:02:17 +02:00
|
|
|
foreach ($results["history"] as &$row) {
|
|
|
|
$formatter = new LengthFormatter($row['length']);
|
|
|
|
$row['length'] = $formatter->format();
|
|
|
|
}
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 22:02:17 +02:00
|
|
|
return $results;
|
2013-07-03 21:19:05 +02:00
|
|
|
}
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
public function insertPlayedItem($schedId) {
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
$this->con->beginTransaction();
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
try {
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
$item = CcScheduleQuery::create()->findPK($schedId, $this->con);
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
//TODO figure out how to combine these all into 1 query.
|
|
|
|
$showInstance = $item->getCcShowInstances($this->con);
|
|
|
|
$show = $showInstance->getCcShow($this->con);
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
$fileId = $item->getDbFileId();
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
//don't add webstreams
|
|
|
|
if (isset($fileId)) {
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
//$starts = $item->getDbStarts(null);
|
|
|
|
//$ends = $item->getDbEnds(null);
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
$metadata = array();
|
|
|
|
//$metadata["date"] = $starts->format('Y-m-d');
|
|
|
|
//$metadata["start"] = $starts->format('H:i:s');
|
|
|
|
//$metadata["end"] = $ends->format('H:i:s');
|
|
|
|
$metadata["showname"] = $show->getDbName();
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:20:40 +02:00
|
|
|
$history = new CcPlayoutHistory();
|
2013-07-03 21:19:05 +02:00
|
|
|
$history->setDbFileId($fileId);
|
|
|
|
$history->setDbStarts($item->getDbStarts(null));
|
|
|
|
$history->setDbEnds($item->getDbEnds(null));
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-23 00:11:44 +02:00
|
|
|
/*
|
2013-07-03 21:19:05 +02:00
|
|
|
foreach ($metadata as $key => $val) {
|
2013-07-03 21:20:40 +02:00
|
|
|
$meta = new CcPlayoutHistoryMetaData();
|
|
|
|
$meta->setDbKey($key);
|
2013-07-03 21:19:05 +02:00
|
|
|
$meta->setDbValue($val);
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
$history->addCcPlayoutHistoryMetaData($meta);
|
|
|
|
}
|
2013-07-23 00:11:44 +02:00
|
|
|
*/
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
$history->save($this->con);
|
|
|
|
}
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-03 21:19:05 +02:00
|
|
|
$this->con->commit();
|
2013-07-18 07:31:20 +02:00
|
|
|
}
|
2013-07-03 21:20:40 +02:00
|
|
|
catch (Exception $e) {
|
|
|
|
$this->con->rollback();
|
|
|
|
throw $e;
|
2013-07-03 21:19:05 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-18 07:31:20 +02:00
|
|
|
|
|
|
|
/* id is an id in cc_playout_history */
|
2013-07-24 00:01:43 +02:00
|
|
|
public function makeHistoryItemForm($id) {
|
2013-07-23 00:11:44 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$form = new Application_Form_EditHistoryItem();
|
|
|
|
$template = $this->getItemTemplate();
|
|
|
|
|
|
|
|
$form->createFromTemplate($template);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
Logging::info($e);
|
|
|
|
throw $e;
|
|
|
|
}
|
2013-07-09 00:00:02 +02:00
|
|
|
}
|
2013-07-18 07:31:20 +02:00
|
|
|
|
|
|
|
/* id is an id in cc_files */
|
2013-07-09 00:00:02 +02:00
|
|
|
public function makeHistoryFileForm($id) {
|
2013-07-18 07:37:39 +02:00
|
|
|
|
2013-07-18 07:31:20 +02:00
|
|
|
try {
|
|
|
|
$form = new Application_Form_EditHistoryFile();
|
|
|
|
|
|
|
|
$file = Application_Model_StoredFile::RecallById($id, $this->con);
|
|
|
|
$md = $file->getDbColMetadata();
|
|
|
|
|
|
|
|
$form->populate(array(
|
2013-07-18 07:37:39 +02:00
|
|
|
'his_file_id' => $id,
|
|
|
|
'his_file_title' => $md[MDATA_KEY_TITLE],
|
|
|
|
'his_file_creator' => $md[MDATA_KEY_CREATOR],
|
|
|
|
'his_file_composer' => $md[MDATA_KEY_COMPOSER],
|
2013-07-18 07:31:20 +02:00
|
|
|
'his_file_copyright' => $md[MDATA_KEY_COPYRIGHT]
|
|
|
|
));
|
|
|
|
|
2013-07-18 07:37:39 +02:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
2013-07-22 16:45:39 +02:00
|
|
|
Logging::info($e);
|
2013-07-23 00:11:44 +02:00
|
|
|
throw $e;
|
2013-07-18 07:31:20 +02:00
|
|
|
}
|
2013-07-09 00:00:02 +02:00
|
|
|
}
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-23 00:11:44 +02:00
|
|
|
public function createPlayedItem($data) {
|
2013-07-22 16:45:39 +02:00
|
|
|
|
2013-07-23 00:11:44 +02:00
|
|
|
try {
|
2013-07-24 00:01:43 +02:00
|
|
|
$form = $this->makeHistoryItemForm(null);
|
|
|
|
$history_id = $form->getElement("his_item_id");
|
2013-07-23 00:11:44 +02:00
|
|
|
|
|
|
|
if ($form->isValid($data)) {
|
2013-07-24 00:01:43 +02:00
|
|
|
$history_id->setIgnore(true);
|
|
|
|
$values = $form->getValues();
|
|
|
|
|
2013-07-23 00:11:44 +02:00
|
|
|
Logging::info("created list item");
|
|
|
|
Logging::info($values);
|
|
|
|
}
|
2013-07-24 00:01:43 +02:00
|
|
|
else {
|
|
|
|
Logging::info("created list item NOT VALID");
|
|
|
|
}
|
|
|
|
|
|
|
|
Logging::info($form->getMessages());
|
2013-07-23 00:11:44 +02:00
|
|
|
|
2013-07-24 00:01:43 +02:00
|
|
|
//return $json;
|
2013-07-23 00:11:44 +02:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
Logging::info($e);
|
|
|
|
}
|
2013-07-22 16:45:39 +02:00
|
|
|
}
|
|
|
|
|
2013-07-09 00:00:02 +02:00
|
|
|
/* id is an id in cc_playout_history */
|
2013-07-24 00:01:43 +02:00
|
|
|
public function editPlayedItem($data) {
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-23 00:11:44 +02:00
|
|
|
try {
|
2013-07-24 00:01:43 +02:00
|
|
|
$id = $data["his_item_id"];
|
|
|
|
$form = $this->makeHistoryItemForm($id);
|
|
|
|
$history_id = $form->getElement("his_item_id");
|
|
|
|
$history_id->setRequired(true);
|
2013-07-23 00:11:44 +02:00
|
|
|
|
|
|
|
if ($form->isValid($data)) {
|
2013-07-24 00:01:43 +02:00
|
|
|
$values = $form->getValues();
|
|
|
|
|
|
|
|
Logging::info("edited list item");
|
|
|
|
Logging::info($values);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Logging::info("edited list item NOT VALID");
|
|
|
|
}
|
|
|
|
|
|
|
|
Logging::info($form->getMessages());
|
2013-07-23 00:11:44 +02:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
Logging::info($e);
|
|
|
|
}
|
2013-07-09 00:00:02 +02:00
|
|
|
}
|
2013-07-18 07:31:20 +02:00
|
|
|
|
2013-07-09 00:00:02 +02:00
|
|
|
/* id is an id in cc_files */
|
2013-07-18 07:31:20 +02:00
|
|
|
public function editPlayedFile($data) {
|
|
|
|
|
2013-07-24 00:01:43 +02:00
|
|
|
$this->con->beginTransaction();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$form = new Application_Form_EditHistoryFile();
|
|
|
|
|
|
|
|
$json = $form->processAjax($data);
|
|
|
|
Logging::info($json);
|
|
|
|
|
|
|
|
if ($form->isValid($data)) {
|
|
|
|
|
|
|
|
$id = $data["his_file_id"];
|
|
|
|
$file = Application_Model_StoredFile::RecallById($id, $this->con);
|
|
|
|
|
|
|
|
$md = array(
|
|
|
|
MDATA_KEY_TITLE => $data['his_file_title'],
|
|
|
|
MDATA_KEY_CREATOR => $data['his_file_creator'],
|
|
|
|
MDATA_KEY_COMPOSER => $data['his_file_composer'],
|
|
|
|
MDATA_KEY_COPYRIGHT => $data['his_file_copyright']
|
|
|
|
);
|
|
|
|
|
|
|
|
$file->setDbColMetadata($md);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->con->commit();
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$this->con->rollback();
|
|
|
|
Logging::info($e);
|
|
|
|
throw $e;
|
|
|
|
}
|
2013-07-18 07:31:20 +02:00
|
|
|
|
|
|
|
return $json;
|
2013-07-09 00:00:02 +02:00
|
|
|
}
|
2013-07-23 00:11:44 +02:00
|
|
|
|
2013-07-24 00:01:43 +02:00
|
|
|
/* id is an id in cc_playout_history */
|
|
|
|
public function deletePlayedItem($id) {
|
|
|
|
|
|
|
|
$this->con->beginTransaction();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$record = CcPlayoutHistoryQuery::create()->findPk($id, $this->con);
|
|
|
|
$record->delete($this->con);
|
|
|
|
|
|
|
|
$this->con->commit();
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$this->con->rollback();
|
|
|
|
Logging::info($e);
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 00:11:44 +02:00
|
|
|
|
|
|
|
//---------------- Following code is for History Templates --------------------------//
|
|
|
|
|
|
|
|
|
|
|
|
private function defaultItemTemplate() {
|
|
|
|
|
|
|
|
$fields = array();
|
|
|
|
|
|
|
|
//array index is the position of the item in the history template table.
|
|
|
|
$fields[] = array("name" => "start", "type" => TEMPLATE_DATETIME, "isFileMd" => false);
|
|
|
|
$fields[] = array("name" => "end", "type" => TEMPLATE_DATETIME, "isFileMd" => false);
|
|
|
|
$fields[] = array("name" => MDATA_KEY_TITLE, "type" => TEMPLATE_STRING, "isFileMd" => true); //these fields can be populated from an associated file.
|
|
|
|
$fields[] = array("name" => MDATA_KEY_CREATOR, "type" => TEMPLATE_STRING, "isFileMd" => true);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getItemTemplate() {
|
|
|
|
|
|
|
|
$template_id = Application_Model_Preference::GetHistoryItemTemplate();
|
|
|
|
|
|
|
|
if (is_numeric($template_id)) {
|
|
|
|
Logging::info("template id is: $template_id");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Logging::info("Using default template");
|
|
|
|
$template = $this->defaultItemTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $template;
|
|
|
|
}
|
2013-07-03 21:19:05 +02:00
|
|
|
|
|
|
|
}
|