From 7ca28fe81d8ca495903e50e2e4980b9850e01411 Mon Sep 17 00:00:00 2001 From: Naomi Date: Wed, 31 Jul 2013 17:38:48 -0400 Subject: [PATCH] history item datatable is set up with configurable columns now. should add a field when configuring template field for "display name" --- .../controllers/PlayouthistoryController.php | 9 +- .../application/services/HistoryService.php | 266 ++++++++++++++---- .../js/airtime/playouthistory/historytable.js | 10 +- 3 files changed, 228 insertions(+), 57 deletions(-) diff --git a/airtime_mvc/application/controllers/PlayouthistoryController.php b/airtime_mvc/application/controllers/PlayouthistoryController.php index c37697bca..b03d554d8 100644 --- a/airtime_mvc/application/controllers/PlayouthistoryController.php +++ b/airtime_mvc/application/controllers/PlayouthistoryController.php @@ -68,6 +68,12 @@ class PlayouthistoryController extends Zend_Controller_Action $this->view->headLink()->appendStylesheet($baseUrl.'css/jquery.ui.timepicker.css?'.$CC_CONFIG['airtime_version']); $this->view->headLink()->appendStylesheet($baseUrl.'css/playouthistory.css?'.$CC_CONFIG['airtime_version']); $this->view->headLink()->appendStylesheet($baseUrl.'css/bootstrap-datetimepicker.min.css?'.$CC_CONFIG['airtime_version']); + + //set datatables columns for display of data. + $historyService = new Application_Service_HistoryService(); + $columns = json_encode($historyService->getDatatablesPlayedItemColumns()); + $script = "localStorage.setItem( 'datatables-historyitem-aoColumns', JSON.stringify($columns) );"; + $this->view->headScript()->appendScript($script); } public function aggregateHistoryFeedAction() @@ -106,7 +112,8 @@ class PlayouthistoryController extends Zend_Controller_Action $endsDT = DateTime::createFromFormat("U", $ends_epoch, new DateTimeZone("UTC")); $historyService = new Application_Service_HistoryService(); - $r = $historyService->getListView($startsDT, $endsDT, $params); + //$r = $historyService->getListView($startsDT, $endsDT, $params); + $r = $historyService->getPlayedItemData($startsDT, $endsDT, $params); $this->view->sEcho = $r["sEcho"]; $this->view->iTotalDisplayRecords = $r["iTotalDisplayRecords"]; diff --git a/airtime_mvc/application/services/HistoryService.php b/airtime_mvc/application/services/HistoryService.php index 9ce4d7db4..39d440846 100644 --- a/airtime_mvc/application/services/HistoryService.php +++ b/airtime_mvc/application/services/HistoryService.php @@ -39,58 +39,199 @@ class Application_Service_HistoryService } } } + + //opts is from datatables. + public function getPlayedItemData($startDT, $endDT, $opts) + { + $mainSqlQuery = ""; + $paramMap = array(); + + $start = $startDT->format("Y-m-d H:i:s"); + $end = $endDT->format("Y-m-d H:i:s"); + $paramMap["starts"] = $start; + $paramMap["ends"] = $end; + + $template = $this->getConfiguredItemTemplate(); + $fields = $template["fields"]; + $required = $this->mandatoryItemFields(); + + $fields_filemd = array(); + $fields_general = array(); + + foreach ($fields as $index=>$field) { + + if (in_array($field["name"], $required)) { + continue; + } + + if ($field["isFileMd"]) { + $fields_filemd[] = $field; + } + else { + $fields_general[] = $field; + } + } + + $historyRange = "(". + "SELECT history.starts, history.ends, history.id AS history_id". + " FROM cc_playout_history as history". + " WHERE history.starts >= :starts and history.starts < :ends". + ") AS history_range"; + + $manualMeta = "(". + "SELECT %KEY%.value AS %KEY%, %KEY%.history_id". + " FROM (". + " SELECT * from cc_playout_history_metadata AS phm WHERE phm.key = :meta_%KEY%". + " ) AS %KEY%". + " ) AS %KEY%_filter"; + + $mainSelect = array("history_range.starts", "history_range.ends", "history_range.history_id"); + $mdFilters = array(); + + $numFileMdFields = count($fields_filemd); + + if ($numFileMdFields > 0) { + + //these 3 selects are only needed if $fields_filemd has some fields. + $fileSelect = array("history_file.history_id"); + $nonNullFileSelect = array("file.id as file_id"); + $nullFileSelect = array("null_file.history_id"); + + $fileMdFilters = array(); + + //populate the different dynamic selects with file info. + for ($i = 0; $i < $numFileMdFields; $i++) { + + $field = $fields_filemd[$i]; + $key = $field["name"]; + + $fileSelect[] = "file_md.{$key}"; + $nonNullFileSelect[] = "file.{$key}"; + $nullFileSelect[] = "{$key}_filter.{$key}"; + $mainSelect[] = "file_info.{$key}"; + + $fileMdFilters[] = str_replace("%KEY%", $key, $manualMeta); + $paramMap["meta_{$key}"] = $key; + } + + //the files associated with scheduled playback in Airtime. + $historyFile = "(". + "SELECT history.id AS history_id, history.file_id". + " FROM cc_playout_history AS history". + " WHERE history.file_id IS NOT NULL". + ") AS history_file"; + + $fileMd = "(". + "SELECT %NON_NULL_FILE_SELECT%". + " FROM cc_files AS file". + ") AS file_md"; + + $fileMd = str_replace("%NON_NULL_FILE_SELECT%", join(", ", $nonNullFileSelect), $fileMd); + + //null files are from manually added data (filling in webstream info etc) + $nullFile = "(". + "SELECT history.id AS history_id". + " FROM cc_playout_history AS history". + " WHERE history.file_id IS NULL". + ") AS null_file"; + + + //---------------------------------- + //building the file inner query + + $fileSqlQuery = + "SELECT ".join(", ", $fileSelect). + " FROM {$historyFile}". + " LEFT JOIN {$fileMd} USING (file_id)". + " UNION". + " SELECT ".join(", ", $nullFileSelect). + " FROM {$nullFile}"; + + foreach ($fileMdFilters as $filter) { + + $fileSqlQuery.= + " LEFT JOIN {$filter} USING(history_id)"; + } + + } + + for ($i = 0, $len = count($fields_general); $i < $len; $i++) { + + $field = $fields_general[$i]; + $key = $field["name"]; + + $mdFilters[] = str_replace("%KEY%", $key, $manualMeta); + $paramMap["meta_{$key}"] = $key; + $mainSelect[] = "{$key}_filter.{$key}"; + } + + $mainSqlQuery.= + "SELECT ".join(", ", $mainSelect). + " FROM {$historyRange}"; + + if (isset($fileSqlQuery)) { + + $mainSqlQuery.= + " LEFT JOIN ( {$fileSqlQuery} ) as file_info USING(history_id)"; + } + + foreach ($mdFilters as $filter) { + + $mainSqlQuery.= + " LEFT JOIN {$filter} USING(history_id)"; + } + + Logging::info($mainSqlQuery); + + $stmt = $this->con->prepare($mainSqlQuery); + foreach ($paramMap as $param => $v) { + $stmt->bindValue($param, $v); + } + + $rows = array(); + if ($stmt->execute()) { + $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); + } + else { + $msg = implode(',', $stmt->errorInfo()); + Logging::info($msg); + throw new Exception("Error: $msg"); + } + + $totalRows = count($rows); + Logging::info($totalRows); + Logging::info($rows); + + //----------------------------------------------------------------------- + //processing results. + + $timezoneUTC = new DateTimeZone("UTC"); + $timezoneLocal = new DateTimeZone($this->timezone); + + //need to display the results in the station's timezone. + foreach ($rows 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"); + } + + return array( + "sEcho" => intval($opts["sEcho"]), + //"iTotalDisplayRecords" => intval($totalDisplayRows), + "iTotalDisplayRecords" => intval($totalRows), + "iTotalRecords" => intval($totalRows), + "history" => $rows + ); + } + public function getListView($startDT, $endDT, $opts) { - -/* - -select * from ( - -select history.id as history_id -from cc_playout_history as history -where history.file_id IS NULL -) as null_files - -LEFT JOIN - -( -select track_title.value as track_title, track_title.history_id -from (select * from cc_playout_history_metadata as phm -where key = 'track_title') -as track_title - -) as track_filter - -USING (history_id) - -LEFT JOIN - -( -select artist_name.value as artist_name, artist_name.history_id -from (select * from cc_playout_history_metadata as phm -where key = 'artist_name') -as artist_name - -) as artist_filter - -USING (history_id) - -LEFT JOIN - -( -select album_title.value as album_title, album_title.history_id -from (select * from cc_playout_history_metadata as phm -where key = 'album_title') -as album_title - -) as album_filter - -USING (history_id) - - */ - - $this->translateColumns($opts); $select = array ( @@ -575,6 +716,33 @@ USING (history_id) } } + public function getDatatablesPlayedItemColumns() { + + try { + //{"sTitle": $.i18n._("Start"), "mDataProp": "starts", "sClass": "his_starts"} + + $template = $this->getConfiguredItemTemplate(); + + $columns = array(); + + foreach ($template["fields"] as $index=>$field) { + + $key = $field["name"]; + + $columns[] = array( + "sTitle"=> $key, + "mDataProp"=> $key, + "sClass"=> "his_{$key}" + ); + } + + return $columns; + } + catch (Exception $e) { + throw $e; + } + } + public function getConfiguredItemTemplate() { try { $id = Application_Model_Preference::GetHistoryItemTemplate(); diff --git a/airtime_mvc/public/js/airtime/playouthistory/historytable.js b/airtime_mvc/public/js/airtime/playouthistory/historytable.js index fb32b459b..c096d960e 100644 --- a/airtime_mvc/public/js/airtime/playouthistory/historytable.js +++ b/airtime_mvc/public/js/airtime/playouthistory/historytable.js @@ -139,6 +139,8 @@ var AIRTIME = (function(AIRTIME) { var oTable, $historyTableDiv = $historyContentDiv.find("#history_table_list"), fnRowCallback; + + var columns = JSON.parse(localStorage.getItem('datatables-historyitem-aoColumns')); fnRowCallback = function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) { var editUrl = baseUrl+"playouthistory/edit-list-item/format/json/id/"+aData.history_id, @@ -150,13 +152,7 @@ var AIRTIME = (function(AIRTIME) { oTable = $historyTableDiv.dataTable( { - "aoColumns": [ - {"sTitle": $.i18n._("Start"), "mDataProp": "starts", "sClass": "his_starts"}, /* Starts */ - {"sTitle": $.i18n._("End"), "mDataProp": "ends", "sClass": "his_ends"}, /* Ends */ - {"sTitle": $.i18n._("Title"), "mDataProp": "title", "sClass": "his_title"}, /* Title */ - {"sTitle": $.i18n._("Creator"), "mDataProp": "artist", "sClass": "his_artist"} /* Creator */ - ], - + "aoColumns": columns, "bProcessing": true, "bServerSide": true, "sAjaxSource": baseUrl+"playouthistory/item-history-feed",