2013-07-03 21:19:05 +02:00
< ? php
class Application_Service_HistoryService
{
2021-10-11 16:10:47 +02:00
private $con ;
private $timezone ;
public const TEMPLATE_TYPE_ITEM = 'item' ;
public const TEMPLATE_TYPE_FILE = 'file' ;
public function __construct ()
{
$this -> con = isset ( $con ) ? $con : Propel :: getConnection ( CcPlayoutHistoryPeer :: DATABASE_NAME );
$this -> timezone = Application_Model_Preference :: GetTimezone ();
}
public function getSupportedTemplateTypes ()
{
return [ self :: TEMPLATE_TYPE_ITEM , self :: TEMPLATE_TYPE_FILE ];
}
2022-03-14 11:15:04 +01:00
// opts is from datatables.
2021-10-11 16:10:47 +02:00
public function getPlayedItemData ( $startDT , $endDT , $opts , $instanceId = null )
{
$mainSqlQuery = '' ;
$paramMap = [];
$sqlTypes = $this -> getSqlTypes ();
$start = $startDT -> format ( DEFAULT_TIMESTAMP_FORMAT );
$end = $endDT -> format ( DEFAULT_TIMESTAMP_FORMAT );
$template = $this -> getConfiguredItemTemplate ();
$fields = $template [ 'fields' ];
$required = $this -> mandatoryItemFields ();
$fields_filemd = [];
$filemd_keys = [];
$fields_general = [];
$general_keys = [];
foreach ( $fields as $index => $field ) {
if ( in_array ( $field [ 'name' ], $required )) {
continue ;
}
if ( $field [ 'isFileMd' ]) {
$fields_filemd [] = $field ;
$filemd_keys [] = $field [ 'name' ];
} else {
$fields_general [] = $field ;
$general_keys [] = $field [ 'name' ];
}
}
2022-03-14 11:15:04 +01:00
// -----------------------------------------------------------------------
// Using the instance_id to filter the data.
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$historyRange = '(' .
2022-07-07 20:01:15 +02:00
'SELECT history.starts, history.ends, history.id AS history_id, history.instance_id' .
' FROM cc_playout_history as history' ;
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( isset ( $instanceId )) {
$historyRange .= ' WHERE history.instance_id = :instance' ;
$paramMap [ 'instance' ] = $instanceId ;
} else {
$historyRange .= ' WHERE history.starts >= :starts and history.starts < :ends' ;
$paramMap [ 'starts' ] = $start ;
$paramMap [ 'ends' ] = $end ;
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$historyRange .= ') AS history_range' ;
$manualMeta = '(' .
2022-07-07 20:01:15 +02:00
'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' ;
2021-10-11 16:10:47 +02:00
$mainSelect = [
'history_range.starts' ,
'history_range.ends' ,
'history_range.history_id' ,
'history_range.instance_id' ,
];
$mdFilters = [];
$numFileMdFields = count ( $fields_filemd );
if ( $numFileMdFields > 0 ) {
2022-03-14 11:15:04 +01:00
// these 3 selects are only needed if $fields_filemd has some fields.
2021-10-11 16:10:47 +02:00
$fileSelect = [ 'history_file.history_id' ];
$nonNullFileSelect = [ 'file.id as file_id' ];
$nullFileSelect = [ 'null_file.history_id' ];
$fileMdFilters = [];
2022-03-14 11:15:04 +01:00
// populate the different dynamic selects with file info.
2021-10-11 16:10:47 +02:00
for ( $i = 0 ; $i < $numFileMdFields ; ++ $i ) {
$field = $fields_filemd [ $i ];
$key = $field [ 'name' ];
$type = $sqlTypes [ $field [ 'type' ]];
$fileSelect [] = " file_md. { $key } :: { $type } " ;
$nonNullFileSelect [] = " file. { $key } :: { $type } " ;
$nullFileSelect [] = " { $key } _filter. { $key } :: { $type } " ;
$mainSelect [] = " file_info. { $key } :: { $type } " ;
$fileMdFilters [] = str_replace ( '%KEY%' , $key , $manualMeta );
$paramMap [ " meta_ { $key } " ] = $key ;
}
2013-08-20 05:19:13 +02:00
2022-03-14 11:15:04 +01:00
// the files associated with scheduled playback in Airtime.
2021-10-11 16:10:47 +02:00
$historyFile = '(' .
2022-07-07 20:01:15 +02:00
'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' ;
2021-10-11 16:10:47 +02:00
$fileMd = '(' .
2022-07-07 20:01:15 +02:00
'SELECT %NON_NULL_FILE_SELECT%' .
' FROM cc_files AS file' .
') AS file_md' ;
2021-10-11 16:10:47 +02:00
Feature: Support php7.4 (#1354)
* Run CI tests against php 7.4
* Sort composer dependencies
* Remove unused Aws S3 php library
* Pin simplepie dependency to ^1.5
* Pin getid3 dependency to ^1.9
* Pin composer semver to ^3.2
* Pin php-amqplib to ^2.12
* Drop sentry logging support
* Update composer dependencies
* Move propel regenerate to Makefile
* Regenerate propel files with v1.7.0
* Pin propel orm to ^1.7
* Regenerate propel files with v1.7.2
* fix: generator_version in airtime-conf-production.php
* Replace propel/propel1 with jooola/propel1
* Regenerate propel files with v1.7.3-dev
* Fix php7.4 compatibility
Using php-cs-fixer:
'@PhpCsFixer' => true,
'concat_space' => ['spacing' => 'one'],
'ordered_class_elements' => false,
'yoda_style' => false,
'@PHP74Migration' => true,
'assign_null_coalescing_to_coalesce_equal' => false,
'ternary_to_null_coalescing' => false,
'heredoc_indentation' => false,
'@PHP74Migration:risky' => true,
'declare_strict_types' => false,
'void_return' => false,
'use_arrow_functions' => false,
* Fix pre-commit
2021-10-17 17:19:53 +02:00
$fileMd = str_replace ( '%NON_NULL_FILE_SELECT%' , implode ( ', ' , $nonNullFileSelect ), $fileMd );
2021-10-11 16:10:47 +02:00
2022-03-14 11:15:04 +01:00
// null files are from manually added data (filling in webstream info etc)
2021-10-11 16:10:47 +02:00
$nullFile = '(' .
2022-07-07 20:01:15 +02:00
'SELECT history.id AS history_id' .
' FROM cc_playout_history AS history' .
' WHERE history.file_id IS NULL' .
') AS null_file' ;
2021-10-11 16:10:47 +02:00
2022-03-14 11:15:04 +01:00
// ----------------------------------
// building the file inner query
2021-10-11 16:10:47 +02:00
$fileSqlQuery =
2022-07-07 20:01:15 +02:00
'SELECT ' . implode ( ', ' , $fileSelect ) .
" FROM { $historyFile } " .
" LEFT JOIN { $fileMd } USING (file_id) " .
' UNION' .
' SELECT ' . implode ( ', ' , $nullFileSelect ) .
" FROM { $nullFile } " ;
2021-10-11 16:10:47 +02:00
foreach ( $fileMdFilters as $filter ) {
$fileSqlQuery .=
2022-07-07 20:01:15 +02:00
" LEFT JOIN { $filter } USING(history_id) " ;
2021-10-11 16:10:47 +02:00
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
for ( $i = 0 , $len = count ( $fields_general ); $i < $len ; ++ $i ) {
$field = $fields_general [ $i ];
$key = $field [ 'name' ];
$type = $sqlTypes [ $field [ 'type' ]];
2013-08-30 00:32:42 +02:00
2021-10-11 16:10:47 +02:00
$mdFilters [] = str_replace ( '%KEY%' , $key , $manualMeta );
$paramMap [ " meta_ { $key } " ] = $key ;
$mainSelect [] = " { $key } _filter. { $key } :: { $type } " ;
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$mainSqlQuery .=
2022-07-07 20:01:15 +02:00
'SELECT ' . implode ( ', ' , $mainSelect ) .
" FROM { $historyRange } " ;
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( isset ( $fileSqlQuery )) {
$mainSqlQuery .=
2022-07-07 20:01:15 +02:00
" LEFT JOIN ( { $fileSqlQuery } ) as file_info USING(history_id) " ;
2021-10-11 16:10:47 +02:00
}
foreach ( $mdFilters as $filter ) {
$mainSqlQuery .=
2022-07-07 20:01:15 +02:00
" LEFT JOIN { $filter } USING(history_id) " ;
2021-10-11 16:10:47 +02:00
}
2022-03-14 11:15:04 +01:00
// ----------------------------------------------------------------------
// need to count the total rows to tell Datatables.
2021-10-11 16:10:47 +02:00
$stmt = $this -> con -> prepare ( $mainSqlQuery );
foreach ( $paramMap as $param => $v ) {
$stmt -> bindValue ( $param , $v );
}
if ( $stmt -> execute ()) {
$totalRows = $stmt -> rowCount ();
2014-11-17 21:53:31 +01:00
} else {
2021-10-11 16:10:47 +02:00
$msg = implode ( ',' , $stmt -> errorInfo ());
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
throw new Exception ( " Error: { $msg } " );
}
2013-08-20 05:19:13 +02:00
2022-03-14 11:15:04 +01:00
// ------------------------------------------------------------------------
// Using Datatables parameters to sort the data.
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( empty ( $opts [ 'iSortingCols' ])) {
$orderBys = [];
} else {
$numOrderColumns = $opts [ 'iSortingCols' ];
$orderBys = [];
2013-08-01 23:10:04 +02:00
2021-10-11 16:10:47 +02:00
for ( $i = 0 ; $i < $numOrderColumns ; ++ $i ) {
$colNum = $opts [ 'iSortCol_' . $i ];
$key = $opts [ 'mDataProp_' . $colNum ];
$sortDir = $opts [ 'sSortDir_' . $i ];
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( in_array ( $key , $required )) {
$orderBys [] = " history_range. { $key } { $sortDir } " ;
} elseif ( in_array ( $key , $filemd_keys )) {
2014-11-17 21:53:31 +01:00
$orderBys [] = " file_info. { $key } { $sortDir } " ;
2021-10-11 16:10:47 +02:00
} elseif ( in_array ( $key , $general_keys )) {
2014-11-17 21:53:31 +01:00
$orderBys [] = " { $key } _filter. { $key } { $sortDir } " ;
}
2021-10-11 16:10:47 +02:00
2022-03-14 11:15:04 +01:00
// throw new Exception("Error: $key is not part of the template.");
2021-10-11 16:10:47 +02:00
}
}
if ( count ( $orderBys ) > 0 ) {
Feature: Support php7.4 (#1354)
* Run CI tests against php 7.4
* Sort composer dependencies
* Remove unused Aws S3 php library
* Pin simplepie dependency to ^1.5
* Pin getid3 dependency to ^1.9
* Pin composer semver to ^3.2
* Pin php-amqplib to ^2.12
* Drop sentry logging support
* Update composer dependencies
* Move propel regenerate to Makefile
* Regenerate propel files with v1.7.0
* Pin propel orm to ^1.7
* Regenerate propel files with v1.7.2
* fix: generator_version in airtime-conf-production.php
* Replace propel/propel1 with jooola/propel1
* Regenerate propel files with v1.7.3-dev
* Fix php7.4 compatibility
Using php-cs-fixer:
'@PhpCsFixer' => true,
'concat_space' => ['spacing' => 'one'],
'ordered_class_elements' => false,
'yoda_style' => false,
'@PHP74Migration' => true,
'assign_null_coalescing_to_coalesce_equal' => false,
'ternary_to_null_coalescing' => false,
'heredoc_indentation' => false,
'@PHP74Migration:risky' => true,
'declare_strict_types' => false,
'void_return' => false,
'use_arrow_functions' => false,
* Fix pre-commit
2021-10-17 17:19:53 +02:00
$orders = implode ( ', ' , $orderBys );
2021-10-11 16:10:47 +02:00
$mainSqlQuery .=
2022-07-07 20:01:15 +02:00
" ORDER BY { $orders } " ;
2021-10-11 16:10:47 +02:00
}
2022-03-14 11:15:04 +01:00
// ---------------------------------------------------------------
// using Datatables parameters to add limits/offsets
2021-10-11 16:10:47 +02:00
$displayLength = empty ( $opts [ 'iDisplayLength' ]) ? - 1 : intval ( $opts [ 'iDisplayLength' ]);
2022-03-14 11:15:04 +01:00
// limit the results returned.
2021-10-11 16:10:47 +02:00
if ( $displayLength !== - 1 ) {
$mainSqlQuery .=
2022-07-07 20:01:15 +02:00
' OFFSET :offset LIMIT :limit' ;
2021-10-11 16:10:47 +02:00
$paramMap [ 'offset' ] = intval ( $opts [ 'iDisplayStart' ]);
$paramMap [ 'limit' ] = $displayLength ;
}
$stmt = $this -> con -> prepare ( $mainSqlQuery );
foreach ( $paramMap as $param => $v ) {
$stmt -> bindValue ( $param , $v );
}
$rows = [];
if ( $stmt -> execute ()) {
$rows = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
} else {
$msg = implode ( ',' , $stmt -> errorInfo ());
throw new Exception ( " Error: { $msg } " );
}
2022-03-14 11:15:04 +01:00
// -----------------------------------------------------------------------
// processing results.
2021-10-11 16:10:47 +02:00
$timezoneUTC = new DateTimeZone ( 'UTC' );
$timezoneLocal = new DateTimeZone ( $this -> timezone );
$boolCast = [];
foreach ( $fields as $index => $field ) {
if ( $field [ 'type' ] == TEMPLATE_BOOLEAN ) {
$boolCast [] = $field ;
}
}
foreach ( $rows as $index => & $result ) {
foreach ( $boolCast as $field ) {
$result [ $field [ 'label' ]] = ( bool ) $result [ $field [ 'name' ]];
}
2022-03-14 11:15:04 +01:00
// need to display the results in the station's timezone.
2021-10-11 16:10:47 +02:00
$dateTime = new DateTime ( $result [ 'starts' ], $timezoneUTC );
$dateTime -> setTimezone ( $timezoneLocal );
$result [ 'starts' ] = $dateTime -> format ( DEFAULT_TIMESTAMP_FORMAT );
2022-03-14 11:15:04 +01:00
// if ends is null we don't want it to default to "now"
2021-10-11 16:10:47 +02:00
if ( isset ( $result [ 'ends' ])) {
$dateTime = new DateTime ( $result [ 'ends' ], $timezoneUTC );
$dateTime -> setTimezone ( $timezoneLocal );
$result [ 'ends' ] = $dateTime -> format ( DEFAULT_TIMESTAMP_FORMAT );
}
if ( isset ( $result [ MDATA_KEY_DURATION ])) {
$formatter = new LengthFormatter ( $result [ MDATA_KEY_DURATION ]);
$result [ MDATA_KEY_DURATION ] = $formatter -> format ();
2014-11-17 21:53:31 +01:00
}
2021-10-11 16:10:47 +02:00
2022-03-14 11:15:04 +01:00
// need to add a checkbox..
2021-10-11 16:10:47 +02:00
$result [ 'checkbox' ] = '' ;
2022-03-14 11:15:04 +01:00
// $unicodeChar = '\u2612';
// $result["new"] = json_decode('"'.$unicodeChar.'"');
// $result["new"] = "U+2612";
2021-10-11 16:10:47 +02:00
}
return [
'sEcho' => empty ( $opts [ 'sEcho' ]) ? null : intval ( $opts [ 'sEcho' ]),
2022-03-14 11:15:04 +01:00
// "iTotalDisplayRecords" => intval($totalDisplayRows),
2021-10-11 16:10:47 +02:00
'iTotalDisplayRecords' => intval ( $totalRows ),
'iTotalRecords' => intval ( $totalRows ),
'history' => $rows ,
];
}
public function getFileSummaryData ( $startDT , $endDT , $opts )
{
$select = [
'summary.played' ,
'summary.file_id' ,
'summary.' . MDATA_KEY_TITLE ,
'summary.' . MDATA_KEY_CREATOR ,
];
$mainSqlQuery = '' ;
$paramMap = [];
$start = $startDT -> format ( DEFAULT_TIMESTAMP_FORMAT );
$end = $endDT -> format ( DEFAULT_TIMESTAMP_FORMAT );
$paramMap [ 'starts' ] = $start ;
$paramMap [ 'ends' ] = $end ;
$template = $this -> getConfiguredFileTemplate ();
$fields = $template [ 'fields' ];
$required = $this -> mandatoryFileFields ();
foreach ( $fields as $index => $field ) {
$key = $field [ 'name' ];
if ( in_array ( $field [ 'name' ], $required )) {
continue ;
}
$select [] = " summary. { $key } " ;
}
$fileSummaryTable = ' ((
2013-08-02 22:15:32 +02:00
SELECT COUNT ( history . file_id ) as played , history . file_id as file_id
FROM cc_playout_history AS history
WHERE history . starts >= : starts AND history . starts < : ends
AND history . file_id IS NOT NULL
GROUP BY history . file_id
) AS playout
2021-10-11 16:10:47 +02:00
LEFT JOIN cc_files AS file ON ( file . id = playout . file_id )) AS summary ' ;
$mainSqlQuery .=
2022-07-07 20:01:15 +02:00
'SELECT ' . implode ( ', ' , $select ) .
" FROM { $fileSummaryTable } " ;
2021-10-11 16:10:47 +02:00
2022-03-14 11:15:04 +01:00
// -------------------------------------------------------------------------
// need to count the total rows to tell Datatables.
2021-10-11 16:10:47 +02:00
$stmt = $this -> con -> prepare ( $mainSqlQuery );
foreach ( $paramMap as $param => $v ) {
$stmt -> bindValue ( $param , $v );
}
if ( $stmt -> execute ()) {
$totalRows = $stmt -> rowCount ();
} else {
$msg = implode ( ',' , $stmt -> errorInfo ());
throw new Exception ( " Error: { $msg } " );
}
2022-03-14 11:15:04 +01:00
// ------------------------------------------------------------------------
// Using Datatables parameters to sort the data.
2021-10-11 16:10:47 +02:00
$numOrderColumns = $opts [ 'iSortingCols' ];
$orderBys = [];
for ( $i = 0 ; $i < $numOrderColumns ; ++ $i ) {
$colNum = $opts [ 'iSortCol_' . $i ];
$key = $opts [ 'mDataProp_' . $colNum ];
$sortDir = $opts [ 'sSortDir_' . $i ];
$orderBys [] = " summary. { $key } { $sortDir } " ;
}
if ( $numOrderColumns > 0 ) {
Feature: Support php7.4 (#1354)
* Run CI tests against php 7.4
* Sort composer dependencies
* Remove unused Aws S3 php library
* Pin simplepie dependency to ^1.5
* Pin getid3 dependency to ^1.9
* Pin composer semver to ^3.2
* Pin php-amqplib to ^2.12
* Drop sentry logging support
* Update composer dependencies
* Move propel regenerate to Makefile
* Regenerate propel files with v1.7.0
* Pin propel orm to ^1.7
* Regenerate propel files with v1.7.2
* fix: generator_version in airtime-conf-production.php
* Replace propel/propel1 with jooola/propel1
* Regenerate propel files with v1.7.3-dev
* Fix php7.4 compatibility
Using php-cs-fixer:
'@PhpCsFixer' => true,
'concat_space' => ['spacing' => 'one'],
'ordered_class_elements' => false,
'yoda_style' => false,
'@PHP74Migration' => true,
'assign_null_coalescing_to_coalesce_equal' => false,
'ternary_to_null_coalescing' => false,
'heredoc_indentation' => false,
'@PHP74Migration:risky' => true,
'declare_strict_types' => false,
'void_return' => false,
'use_arrow_functions' => false,
* Fix pre-commit
2021-10-17 17:19:53 +02:00
$orders = implode ( ', ' , $orderBys );
2021-10-11 16:10:47 +02:00
$mainSqlQuery .=
2022-07-07 20:01:15 +02:00
" ORDER BY { $orders } " ;
2021-10-11 16:10:47 +02:00
}
2022-03-14 11:15:04 +01:00
// ------------------------------------------------------------
// using datatables params to add limits/offsets
2021-10-11 16:10:47 +02:00
$displayLength = intval ( $opts [ 'iDisplayLength' ]);
if ( $displayLength !== - 1 ) {
$mainSqlQuery .=
2022-07-07 20:01:15 +02:00
' OFFSET :offset LIMIT :limit' ;
2021-10-11 16:10:47 +02:00
$paramMap [ 'offset' ] = $opts [ 'iDisplayStart' ];
$paramMap [ 'limit' ] = $displayLength ;
}
$stmt = $this -> con -> prepare ( $mainSqlQuery );
foreach ( $paramMap as $param => $v ) {
$stmt -> bindValue ( $param , $v );
}
$rows = [];
if ( $stmt -> execute ()) {
$rows = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
} else {
$msg = implode ( ',' , $stmt -> errorInfo ());
throw new Exception ( " Error: { $msg } " );
}
2022-03-14 11:15:04 +01:00
// -----------------------------------------------------------------
// processing the results
2021-10-11 16:10:47 +02:00
foreach ( $rows as & $row ) {
if ( isset ( $row [ MDATA_KEY_DURATION ])) {
$formatter = new LengthFormatter ( $row [ MDATA_KEY_DURATION ]);
$row [ MDATA_KEY_DURATION ] = $formatter -> format ();
}
}
return [
'sEcho' => intval ( $opts [ 'sEcho' ]),
2022-03-14 11:15:04 +01:00
// "iTotalDisplayRecords" => intval($totalDisplayRows),
2021-10-11 16:10:47 +02:00
'iTotalDisplayRecords' => intval ( $totalRows ),
'iTotalRecords' => intval ( $totalRows ),
'history' => $rows ,
];
}
public function getShowList ( $startDT , $endDT , $userId = null )
{
2014-11-17 21:53:31 +01:00
if ( empty ( $userId )) {
2021-10-11 16:10:47 +02:00
$user = Application_Model_User :: getCurrentUser ();
2014-11-17 21:53:31 +01:00
} else {
$user = new Application_Model_User ( $userId );
}
2021-10-11 16:10:47 +02:00
$shows = Application_Model_Show :: getShows ( $startDT , $endDT );
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
Logging :: info ( $startDT -> format ( DEFAULT_TIMESTAMP_FORMAT ));
Logging :: info ( $endDT -> format ( DEFAULT_TIMESTAMP_FORMAT ));
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
Logging :: info ( $shows );
2013-08-29 05:30:15 +02:00
2022-03-14 11:15:04 +01:00
// need to filter the list to only their shows
2022-07-11 17:07:30 +02:00
if (( ! empty ( $user )) && $user -> isHost ()) {
2021-10-11 16:10:47 +02:00
$showIds = [];
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $shows as $show ) {
$showIds [] = $show [ 'show_id' ];
}
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
$showIds = array_unique ( $showIds );
Logging :: info ( $showIds );
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
$hostRecords = CcShowHostsQuery :: create ()
-> filterByDbHost ( $user -> getId ())
-> filterByDbShow ( $showIds )
2022-01-23 19:15:55 +01:00
-> find ( $this -> con );
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
$filteredShowIds = [];
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $hostRecords as $record ) {
$filteredShowIds [] = $record -> getDbShow ();
}
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
Logging :: info ( $filteredShowIds );
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
$filteredShows = [];
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $shows as $show ) {
if ( in_array ( $show [ 'show_id' ], $filteredShowIds )) {
$filteredShows [] = $show ;
}
}
} else {
$filteredShows = $shows ;
}
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
$timezoneUTC = new DateTimeZone ( 'UTC' );
$timezoneLocal = new DateTimeZone ( $this -> timezone );
2013-08-30 00:32:42 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $filteredShows as & $result ) {
2022-03-14 11:15:04 +01:00
// need to display the results in the station's timezone.
2021-10-11 16:10:47 +02:00
$dateTime = new DateTime ( $result [ 'starts' ], $timezoneUTC );
$dateTime -> setTimezone ( $timezoneLocal );
$result [ 'starts' ] = $dateTime -> format ( DEFAULT_TIMESTAMP_FORMAT );
2013-08-30 00:32:42 +02:00
2021-10-11 16:10:47 +02:00
$dateTime = new DateTime ( $result [ 'ends' ], $timezoneUTC );
$dateTime -> setTimezone ( $timezoneLocal );
$result [ 'ends' ] = $dateTime -> format ( DEFAULT_TIMESTAMP_FORMAT );
}
2013-08-30 00:32:42 +02:00
2021-10-11 16:10:47 +02:00
return $filteredShows ;
}
2013-08-30 00:32:42 +02:00
2021-10-11 16:10:47 +02:00
public function insertWebstreamMetadata ( $schedId , $startDT , $data )
{
$this -> con -> beginTransaction ();
2013-08-30 00:32:42 +02:00
2021-10-11 16:10:47 +02:00
try {
$item = CcScheduleQuery :: create () -> findPK ( $schedId , $this -> con );
2013-08-29 05:30:15 +02:00
2022-03-14 11:15:04 +01:00
// TODO figure out how to combine these all into 1 query.
2021-10-11 16:10:47 +02:00
$showInstance = $item -> getCcShowInstances ( $this -> con );
$show = $showInstance -> getCcShow ( $this -> con );
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
$webstream = $item -> getCcWebstream ( $this -> con );
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
$metadata = [];
$metadata [ 'showname' ] = $show -> getDbName ();
$metadata [ MDATA_KEY_TITLE ] = $data -> title ;
$metadata [ MDATA_KEY_CREATOR ] = $webstream -> getDbName ();
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
$history = new CcPlayoutHistory ();
$history -> setDbStarts ( $startDT );
$history -> setDbEnds ( null );
$history -> setDbInstanceId ( $item -> getDbInstanceId ());
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $metadata as $key => $val ) {
$meta = new CcPlayoutHistoryMetaData ();
$meta -> setDbKey ( $key );
$meta -> setDbValue ( $val );
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
$history -> addCcPlayoutHistoryMetaData ( $meta );
}
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
$history -> save ( $this -> con );
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
$this -> con -> commit ();
} catch ( Exception $e ) {
$this -> con -> rollback ();
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
public function insertPlayedItem ( $schedId )
{
$this -> con -> beginTransaction ();
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
try {
$item = CcScheduleQuery :: create () -> findPK ( $schedId , $this -> con );
if ( is_null ( $item )) {
throw new Exception ( 'Invalid schedule id: ' . $schedId );
}
2019-10-17 02:20:34 +02:00
2022-03-14 11:15:04 +01:00
// TODO figure out how to combine these all into 1 query.
2021-10-11 16:10:47 +02:00
$showInstance = $item -> getCcShowInstances ( $this -> con );
$show = $showInstance -> getCcShow ( $this -> con );
$fileId = $item -> getDbFileId ();
2022-03-14 11:15:04 +01:00
// don't add webstreams
2021-10-11 16:10:47 +02:00
if ( isset ( $fileId )) {
$metadata = [];
$metadata [ 'showname' ] = $show -> getDbName ();
$instanceEnd = $showInstance -> getDbEnds ( null );
$itemEnd = $item -> getDbEnds ( null );
$recordStart = $item -> getDbStarts ( null );
$recordEnd = ( $instanceEnd < $itemEnd ) ? $instanceEnd : $itemEnd ;
2022-03-14 11:15:04 +01:00
// first check if this is a duplicate
2021-10-11 16:10:47 +02:00
// (caused by restarting liquidsoap)
$prevRecord = CcPlayoutHistoryQuery :: create ()
-> filterByDbStarts ( $recordStart )
-> filterByDbEnds ( $recordEnd )
-> filterByDbFileId ( $fileId )
2022-01-23 19:15:55 +01:00
-> findOne ( $this -> con );
2021-10-11 16:10:47 +02:00
if ( empty ( $prevRecord )) {
$history = new CcPlayoutHistory ();
$history -> setDbFileId ( $fileId );
$history -> setDbStarts ( $recordStart );
$history -> setDbEnds ( $recordEnd );
$history -> setDbInstanceId ( $item -> getDbInstanceId ());
foreach ( $metadata as $key => $val ) {
$meta = new CcPlayoutHistoryMetaData ();
$meta -> setDbKey ( $key );
$meta -> setDbValue ( $val );
$history -> addCcPlayoutHistoryMetaData ( $meta );
}
$history -> save ( $this -> con );
$this -> con -> commit ();
}
}
} catch ( Exception $e ) {
$this -> con -> rollback ();
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
// id is an id in cc_playout_history
public function makeHistoryItemForm ( $id , $populate = false )
{
try {
$form = new Application_Form_EditHistoryItem ();
$template = $this -> getConfiguredItemTemplate ();
$required = $this -> mandatoryItemFields ();
$form -> createFromTemplate ( $template [ 'fields' ], $required );
if ( $populate ) {
$formValues = [];
$historyRecord = CcPlayoutHistoryQuery :: create () -> findPk ( $id , $this -> con );
$file = $historyRecord -> getCcFiles ( $this -> con );
$instance = $historyRecord -> getCcShowInstances ( $this -> con );
if ( isset ( $instance )) {
$show = $instance -> getCcShow ( $this -> con );
$selOpts = [];
$instance_id = $instance -> getDbId ();
$selOpts [ $instance_id ] = $show -> getDbName ();
$form -> populateShowInstances ( $selOpts , $instance_id );
}
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
if ( isset ( $file )) {
$f = Application_Model_StoredFile :: createWithFile ( $file , $this -> con );
$filemd = $f -> getDbColMetadata ();
}
$metadata = [];
$mds = $historyRecord -> getCcPlayoutHistoryMetaDatas ();
foreach ( $mds as $md ) {
$metadata [ $md -> getDbKey ()] = $md -> getDbValue ();
}
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
$prefix = Application_Form_EditHistoryItem :: ID_PREFIX ;
$formValues [ " { $prefix } id " ] = $id ;
foreach ( $template [ 'fields' ] as $index => $field ) {
$key = $field [ 'name' ];
$value = '' ;
if ( in_array ( $key , $required )) {
$method = 'getDb' . ucfirst ( $key );
$value = $historyRecord -> { $method }();
} elseif ( isset ( $filemd ) && $field [ 'isFileMd' ]) {
$value = $filemd [ $key ];
} elseif ( isset ( $metadata [ $key ])) {
$value = $metadata [ $key ];
}
2022-03-14 11:15:04 +01:00
// need to convert to the station's local time first.
2021-10-11 16:10:47 +02:00
if ( $field [ 'type' ] == TEMPLATE_DATETIME && ! is_null ( $value )) {
$timezoneUTC = new DateTimeZone ( 'UTC' );
$timezoneLocal = new DateTimeZone ( $this -> timezone );
$dateTime = new DateTime ( $value , $timezoneUTC );
$dateTime -> setTimezone ( $timezoneLocal );
$value = $dateTime -> format ( DEFAULT_TIMESTAMP_FORMAT );
}
$formValues [ " { $prefix } { $key } " ] = $value ;
}
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
$form -> populate ( $formValues );
}
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
return $form ;
} catch ( Exception $e ) {
Logging :: info ( $e );
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
// id is an id in cc_files
public function makeHistoryFileForm ( $id )
{
try {
$form = new Application_Form_EditHistoryFile ();
$template = $this -> getConfiguredFileTemplate ();
$required = $this -> mandatoryFileFields ();
$form -> createFromTemplate ( $template [ 'fields' ], $required );
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
$file = Application_Model_StoredFile :: RecallById ( $id , $this -> con );
$md = $file -> getDbColMetadata ();
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
$prefix = Application_Form_EditHistoryFile :: ID_PREFIX ;
$formValues = [];
$formValues [ " { $prefix } id " ] = $id ;
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $template [ 'fields' ] as $index => $field ) {
$key = $field [ 'name' ];
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
if ( in_array ( $key , $required )) {
continue ;
}
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
$value = $md [ $key ];
$formValues [ " { $prefix } { $key } " ] = $value ;
}
2019-10-17 02:20:34 +02:00
2021-10-11 16:10:47 +02:00
$form -> populate ( $formValues );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
return $form ;
} catch ( Exception $e ) {
Logging :: info ( $e );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
public function populateTemplateFile ( $values , $id )
{
$this -> con -> beginTransaction ();
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
try {
$file = Application_Model_StoredFile :: RecallById ( $id , $this -> con );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$prefix = Application_Form_EditHistoryFile :: ID_PREFIX ;
$prefix_len = strlen ( $prefix );
$templateValues = $values [ $prefix . 'template' ];
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$md = [];
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $templateValues as $index => $value ) {
$key = substr ( $index , $prefix_len );
$md [ $key ] = $value ;
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$file -> setDbColMetadata ( $md );
$this -> con -> commit ();
} catch ( Exception $e ) {
$this -> con -> rollback ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
public function populateTemplateItem ( $values , $id = null , $instance_id = null )
{
$this -> con -> beginTransaction ();
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
try {
$template = $this -> getConfiguredItemTemplate ();
$prefix = Application_Form_EditHistoryItem :: ID_PREFIX ;
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( isset ( $id )) {
$historyRecord = CcPlayoutHistoryQuery :: create () -> findPk ( $id , $this -> con );
} else {
$historyRecord = new CcPlayoutHistory ();
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( isset ( $instance_id )) {
$historyRecord -> setDbInstanceId ( $instance_id );
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$timezoneUTC = new DateTimeZone ( 'UTC' );
$timezoneLocal = new DateTimeZone ( $this -> timezone );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$dateTime = new DateTime ( $values [ $prefix . 'starts' ], $timezoneLocal );
$dateTime -> setTimezone ( $timezoneUTC );
$historyRecord -> setDbStarts ( $dateTime -> format ( DEFAULT_TIMESTAMP_FORMAT ));
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$dateTime = new DateTime ( $values [ $prefix . 'ends' ], $timezoneLocal );
$dateTime -> setTimezone ( $timezoneUTC );
$historyRecord -> setDbEnds ( $dateTime -> format ( DEFAULT_TIMESTAMP_FORMAT ));
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$templateValues = $values [ $prefix . 'template' ];
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$file = $historyRecord -> getCcFiles ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$md = [];
$metadata = [];
$fields = $template [ 'fields' ];
$required = $this -> mandatoryItemFields ();
$phpCasts = $this -> getPhpCasts ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
for ( $i = 0 , $len = count ( $fields ); $i < $len ; ++ $i ) {
$field = $fields [ $i ];
$key = $field [ 'name' ];
2013-07-26 17:36:58 +02:00
2022-03-14 11:15:04 +01:00
// required is delt with before this loop.
2021-10-11 16:10:47 +02:00
if ( in_array ( $key , $required )) {
continue ;
}
2013-07-26 17:36:58 +02:00
2021-10-11 16:10:47 +02:00
$isFileMd = $field [ 'isFileMd' ];
$entry = $phpCasts [ $field [ 'type' ]]( $templateValues [ $prefix . $key ]);
2013-07-29 14:57:31 +02:00
2021-10-11 16:10:47 +02:00
if ( $isFileMd && isset ( $file )) {
Logging :: info ( " adding metadata associated to a file for { $key } = { $entry } " );
$md [ $key ] = $entry ;
} else {
Logging :: info ( " adding metadata for { $key } = { $entry } " );
$metadata [ $key ] = $entry ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( count ( $md ) > 0 ) {
$f = Application_Model_StoredFile :: createWithFile ( $file , $this -> con );
$f -> setDbColMetadata ( $md );
}
2013-08-30 00:32:42 +02:00
2022-03-14 11:15:04 +01:00
// Use this array to update existing values.
2021-10-11 16:10:47 +02:00
$mds = $historyRecord -> getCcPlayoutHistoryMetaDatas ();
foreach ( $mds as $md ) {
$prevmd [ $md -> getDbKey ()] = $md ;
}
foreach ( $metadata as $key => $val ) {
if ( isset ( $prevmd [ $key ])) {
$meta = $prevmd [ $key ];
$meta -> setDbValue ( $val );
} else {
$meta = new CcPlayoutHistoryMetaData ();
$meta -> setDbKey ( $key );
$meta -> setDbValue ( $val );
$historyRecord -> addCcPlayoutHistoryMetaData ( $meta );
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$historyRecord -> save ( $this -> con );
$this -> con -> commit ();
} catch ( Exception $e ) {
$this -> con -> rollback ();
2013-07-29 14:57:31 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
2013-07-29 14:57:31 +02:00
2022-03-14 11:15:04 +01:00
// start,end timestamp strings in local timezone.
2021-10-11 16:10:47 +02:00
public function populateShowInstances ( $start , $end )
{
$timezoneLocal = new DateTimeZone ( $this -> timezone );
2013-07-29 14:57:31 +02:00
2021-10-11 16:10:47 +02:00
$startDT = new DateTime ( $start , $timezoneLocal );
$endDT = new DateTime ( $end , $timezoneLocal );
2013-07-29 14:57:31 +02:00
2021-10-11 16:10:47 +02:00
$shows = $this -> getShowList ( $startDT , $endDT );
2013-07-29 14:57:31 +02:00
2021-10-11 16:10:47 +02:00
$select = [];
2013-07-31 01:24:05 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $shows as & $show ) {
$select [ $show [ 'instance_id' ]] = $show [ 'name' ];
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
return $select ;
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
private function validateHistoryItem ( $instanceId , $form )
{
/*
$userService = new Application_Service_UserService ();
$currentUser = $userService -> getCurrentUser ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( ! $currentUser -> isAdminOrPM ()) {
if ( empty ( $instance_id ) ) {
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
}
}
*/
2013-07-29 14:59:04 +02:00
2021-10-11 16:10:47 +02:00
$valid = true ;
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
$recordStartsEl = $form -> getElement ( 'his_item_starts' );
$recordStarts = $recordStartsEl -> getValue ();
$recordEndsEl = $form -> getElement ( 'his_item_starts' );
$recordEnds = $recordEndsEl -> getValue ();
2013-07-26 17:36:58 +02:00
2021-10-11 16:10:47 +02:00
$timezoneLocal = new DateTimeZone ( $this -> timezone );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$startDT = new DateTime ( $recordStarts , $timezoneLocal );
$endDT = new DateTime ( $recordEnds , $timezoneLocal );
2013-08-30 06:43:43 +02:00
2021-10-11 16:10:47 +02:00
if ( $recordStarts > $recordEnds ) {
$valid = false ;
$recordEndsEl -> addErrorMessage ( 'End time must be after start time' );
}
2013-07-18 07:31:20 +02:00
2021-10-11 16:10:47 +02:00
if ( isset ( $instanceId )) {
$instance = CcShowInstancesQuery :: create () -> findPk ( $instanceId , $this -> con );
$inStartsDT = $instance -> getDbStarts ( null );
$inEndsDT = $instance -> getDbEnds ( null );
if ( $startDT < $inStartsDT ) {
$valid = false ;
$form -> addErrorMessage ( 'History item begins before show.' );
} elseif ( $startDT > $inEndsDT ) {
$valid = false ;
$form -> addErrorMessage ( 'History item begins after show.' );
}
}
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
return $valid ;
}
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
public function createPlayedItem ( $data )
{
try {
$form = $this -> makeHistoryItemForm ( null );
$history_id = $form -> getElement ( 'his_item_id' );
$instanceId = isset ( $data [ 'instance_id' ]) ? $data [ 'instance_id' ] : null ;
$json = [];
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
if ( $form -> isValid ( $data ) && $this -> validateHistoryItem ( $instanceId , $form )) {
$history_id -> setIgnore ( true );
$values = $form -> getValues ();
2013-07-26 17:36:58 +02:00
2021-10-11 16:10:47 +02:00
$this -> populateTemplateItem ( $values , null , $instanceId );
} else {
$json [ 'form' ] = $form ;
}
2013-07-26 17:36:58 +02:00
2021-10-11 16:10:47 +02:00
return $json ;
} catch ( Exception $e ) {
throw $e ;
}
}
// id is an id in cc_playout_history
public function editPlayedItem ( $data )
{
try {
$id = $data [ 'his_item_id' ];
$instanceId = isset ( $data [ 'instance_id' ]) ? $data [ 'instance_id' ] : null ;
$form = $this -> makeHistoryItemForm ( $id );
$history_id = $form -> getElement ( 'his_item_id' );
$history_id -> setRequired ( true );
$json = [];
if ( $form -> isValid ( $data ) && $this -> validateHistoryItem ( $instanceId , $form )) {
$history_id -> setIgnore ( true );
$values = $form -> getValues ();
$this -> populateTemplateItem ( $values , $id , $instanceId );
} else {
$json [ 'form' ] = SecurityHelper :: htmlescape_recursive ( $form );
}
2013-07-26 17:36:58 +02:00
2021-10-11 16:10:47 +02:00
return $json ;
} catch ( Exception $e ) {
throw $e ;
}
}
// id is an id in cc_files
public function editPlayedFile ( $data )
{
try {
$id = $data [ 'his_file_id' ];
$form = $form = $this -> makeHistoryFileForm ( $id );
$history_id = $form -> getElement ( 'his_file_id' );
$history_id -> setRequired ( true );
$json = [];
if ( $form -> isValid ( $data )) {
$history_id -> setIgnore ( true );
$values = $form -> getValues ();
$this -> populateTemplateFile ( $values , $id );
} else {
$json [ 'error' ] = $form -> getErrorMessages ();
$json [ 'error' ] = SecurityHelper :: htmlescape_recursive ( $json [ 'error' ]);
}
2013-07-29 14:57:31 +02:00
2021-10-11 16:10:47 +02:00
return $json ;
$this -> con -> commit ();
} catch ( Exception $e ) {
$this -> con -> rollback ();
Logging :: info ( $e );
2013-07-29 14:57:31 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
return $json ;
}
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
// id is an id in cc_playout_history
public function deletePlayedItem ( $id )
{
$this -> con -> beginTransaction ();
2013-08-29 05:30:15 +02:00
2021-10-11 16:10:47 +02:00
try {
$record = CcPlayoutHistoryQuery :: create () -> findPk ( $id , $this -> con );
$record -> delete ( $this -> con );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$this -> con -> commit ();
} catch ( Exception $e ) {
$this -> con -> rollback ();
Logging :: info ( $e );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
// id is an id in cc_playout_history
public function deletePlayedItems ( $ids )
{
$this -> con -> beginTransaction ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
try {
$records = CcPlayoutHistoryQuery :: create () -> findPks ( $ids , $this -> con );
$records -> delete ( $this -> con );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$this -> con -> commit ();
} catch ( Exception $e ) {
$this -> con -> rollback ();
Logging :: info ( $e );
2013-07-26 17:36:58 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
2022-03-14 11:15:04 +01:00
// ---------------- Following code is for History Templates --------------------------//
2021-10-11 16:10:47 +02:00
public function getFieldTypes ()
{
return [
2022-03-14 11:15:04 +01:00
// TEMPLATE_DATE,
// TEMPLATE_TIME,
// TEMPLATE_DATETIME,
2021-10-11 16:10:47 +02:00
TEMPLATE_STRING ,
TEMPLATE_BOOLEAN ,
TEMPLATE_INT ,
TEMPLATE_FLOAT ,
];
}
private function getPhpCasts ()
{
return [
TEMPLATE_DATE => 'strval' ,
TEMPLATE_TIME => 'strval' ,
TEMPLATE_DATETIME => 'strval' ,
TEMPLATE_STRING => 'strval' ,
2022-03-14 11:15:04 +01:00
TEMPLATE_BOOLEAN => 'intval' , // boolval only exists in php 5.5+
2021-10-11 16:10:47 +02:00
TEMPLATE_INT => 'intval' ,
TEMPLATE_FLOAT => 'floatval' ,
];
}
private function getSqlTypes ()
{
return [
TEMPLATE_DATE => 'date' ,
TEMPLATE_TIME => 'time' ,
TEMPLATE_DATETIME => 'datetime' ,
TEMPLATE_STRING => 'text' ,
TEMPLATE_BOOLEAN => 'boolean' ,
TEMPLATE_INT => 'integer' ,
TEMPLATE_FLOAT => 'float' ,
];
}
public function getFileMetadataTypes ()
{
return [
[ 'name' => MDATA_KEY_TITLE , 'label' => _ ( 'Title' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_CREATOR , 'label' => _ ( 'Creator' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_SOURCE , 'label' => _ ( 'Album' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_DURATION , 'label' => _ ( 'Length' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_GENRE , 'label' => _ ( 'Genre' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_MOOD , 'label' => _ ( 'Mood' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_LABEL , 'label' => _ ( 'Label' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_COMPOSER , 'label' => _ ( 'Composer' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_ISRC , 'label' => _ ( 'ISRC' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_COPYRIGHT , 'label' => _ ( 'Copyright' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_YEAR , 'label' => _ ( 'Year' ), 'type' => TEMPLATE_INT ],
[ 'name' => MDATA_KEY_TRACKNUMBER , 'label' => _ ( 'Track' ), 'type' => TEMPLATE_INT ],
[ 'name' => MDATA_KEY_CONDUCTOR , 'label' => _ ( 'Conductor' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_LANGUAGE , 'label' => _ ( 'Language' ), 'type' => TEMPLATE_STRING ],
[ 'name' => MDATA_KEY_TRACK_TYPE , 'label' => _ ( 'Track Type' ), 'type' => TEMPLATE_STRING ],
];
}
public function mandatoryItemFields ()
{
return [ 'starts' , 'ends' ];
}
public function mandatoryFileFields ()
{
return [ 'played' ];
}
private function defaultItemTemplate ()
{
$template = [];
$fields = [];
$fields [] = [ 'name' => 'starts' , 'label' => _ ( 'Start Time' ), 'type' => TEMPLATE_DATETIME , 'isFileMd' => false ];
$fields [] = [ 'name' => 'ends' , 'label' => _ ( 'End Time' ), 'type' => TEMPLATE_DATETIME , 'isFileMd' => false ];
2022-03-14 11:15:04 +01:00
$fields [] = [ 'name' => MDATA_KEY_TITLE , 'label' => _ ( 'Title' ), 'type' => TEMPLATE_STRING , 'isFileMd' => true ]; // these fields can be populated from an associated file.
2021-10-11 16:10:47 +02:00
$fields [] = [ 'name' => MDATA_KEY_CREATOR , 'label' => _ ( 'Creator' ), 'type' => TEMPLATE_STRING , 'isFileMd' => true ];
$template [ 'name' ] = 'Log Sheet ' . date ( DEFAULT_TIMESTAMP_FORMAT ) . ' Template' ;
$template [ 'fields' ] = $fields ;
return $template ;
}
// Default File Summary Template. Taken from The Czech radio requirements (customer requested this in the past).
private function defaultFileTemplate ()
{
$template = [];
$fields = [];
$fields [] = [ 'name' => MDATA_KEY_TITLE , 'label' => _ ( 'Title' ), 'type' => TEMPLATE_STRING , 'isFileMd' => true ];
$fields [] = [ 'name' => MDATA_KEY_CREATOR , 'label' => _ ( 'Creator' ), 'type' => TEMPLATE_STRING , 'isFileMd' => true ];
$fields [] = [ 'name' => 'played' , 'label' => _ ( 'Played' ), 'type' => TEMPLATE_INT , 'isFileMd' => false ];
$fields [] = [ 'name' => MDATA_KEY_DURATION , 'label' => _ ( 'Length' ), 'type' => TEMPLATE_STRING , 'isFileMd' => true ];
$fields [] = [ 'name' => MDATA_KEY_COMPOSER , 'label' => _ ( 'Composer' ), 'type' => TEMPLATE_STRING , 'isFileMd' => true ];
$fields [] = [ 'name' => MDATA_KEY_COPYRIGHT , 'label' => _ ( 'Copyright' ), 'type' => TEMPLATE_STRING , 'isFileMd' => true ];
$template [ 'name' ] = 'File Summary ' . date ( DEFAULT_TIMESTAMP_FORMAT ) . ' Template' ;
$template [ 'fields' ] = $fields ;
return $template ;
}
public function loadTemplate ( $id )
{
try {
if ( ! is_numeric ( $id )) {
throw new Exception ( " Error: { $id } is not numeric. " );
}
2013-07-26 17:36:58 +02:00
2021-10-11 16:10:47 +02:00
$template = CcPlayoutHistoryTemplateQuery :: create () -> findPk ( $id , $this -> con );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( empty ( $template )) {
throw new Exception ( " Error: Template { $id } does not exist. " );
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$c = new Criteria ();
$c -> addAscendingOrderByColumn ( CcPlayoutHistoryTemplateFieldPeer :: POSITION );
$config = $template -> getCcPlayoutHistoryTemplateFields ( $c , $this -> con );
$fields = [];
foreach ( $config as $item ) {
$fields [] = [
'name' => $item -> getDbName (),
'label' => $item -> getDbLabel (),
'type' => $item -> getDbType (),
'isFileMd' => $item -> getDbIsFileMD (),
'id' => $item -> getDbId (),
];
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$data = [];
$data [ 'id' ] = $template -> getDbId ();
$data [ 'name' ] = $template -> getDbName ();
$data [ 'fields' ] = $fields ;
$data [ 'type' ] = $template -> getDbType ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
return $data ;
} catch ( Exception $e ) {
throw $e ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
public function getItemTemplate ( $id )
{
if ( is_numeric ( $id )) {
Logging :: info ( " template id is: { $id } " );
$template = $this -> loadTemplate ( $id );
} else {
Logging :: info ( 'Using default template' );
$template = $this -> defaultItemTemplate ();
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
return $template ;
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
public function getTemplates ( $type )
{
$list = [];
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
try {
$query = CcPlayoutHistoryTemplateQuery :: create ()
2022-01-23 19:15:55 +01:00
-> setFormatter ( ModelCriteria :: FORMAT_ON_DEMAND );
2013-07-26 17:36:58 +02:00
2021-10-11 16:10:47 +02:00
if ( isset ( $type )) {
$templates = $query -> findByDbType ( $type );
} else {
$templates = $query -> find ();
}
2013-07-26 17:36:58 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $templates as $template ) {
$list [ $template -> getDbId ()] = $template -> getDbName ();
}
2013-07-26 17:36:58 +02:00
2021-10-11 16:10:47 +02:00
return $list ;
} catch ( Exception $e ) {
throw $e ;
}
}
public function getListItemTemplates ()
{
return $this -> getTemplates ( self :: TEMPLATE_TYPE_ITEM );
}
public function getFileTemplates ()
{
return $this -> getTemplates ( self :: TEMPLATE_TYPE_FILE );
}
private function datatablesColumns ( $fields )
{
$columns = [];
foreach ( $fields as $field ) {
$label = $field [ 'label' ];
$key = $field [ 'name' ];
$columns [] = [
'sTitle' => $label ,
'mDataProp' => $key ,
'sClass' => " his_ { $key } " ,
'sDataType' => $field [ 'type' ],
];
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
return $columns ;
}
public function getDatatablesLogSheetColumns ()
{
2022-03-14 11:15:04 +01:00
// need to prepend a checkbox column.
2021-10-11 16:10:47 +02:00
$checkbox = [
'sTitle' => '' ,
'mDataProp' => 'checkbox' ,
'sClass' => 'his_checkbox' ,
'bSortable' => false ,
];
try {
$template = $this -> getConfiguredItemTemplate ();
$fields = $template [ 'fields' ];
$columns = $this -> datatablesColumns ( $fields );
array_unshift ( $columns , $checkbox );
return $columns ;
} catch ( Exception $e ) {
throw $e ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
public function getDatatablesFileSummaryColumns ()
{
try {
$template = $this -> getConfiguredFileTemplate ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
return $this -> datatablesColumns ( $template [ 'fields' ]);
} catch ( Exception $e ) {
throw $e ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
public function getConfiguredItemTemplate ()
{
try {
$id = Application_Model_Preference :: GetHistoryItemTemplate ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( is_numeric ( $id )) {
$template = $this -> loadTemplate ( $id );
} else {
$template = $this -> defaultItemTemplate ();
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
return $template ;
} catch ( Exception $e ) {
throw $e ;
}
}
public function setConfiguredItemTemplate ( $id )
{
try {
Application_Model_Preference :: SetHistoryItemTemplate ( $id );
} catch ( Exception $e ) {
throw $e ;
}
}
public function getConfiguredFileTemplate ()
{
try {
$id = Application_Model_Preference :: GetHistoryFileTemplate ();
if ( is_numeric ( $id )) {
$template = $this -> loadTemplate ( $id );
} else {
$template = $this -> defaultFileTemplate ();
}
return $template ;
} catch ( Exception $e ) {
throw $e ;
}
}
public function setConfiguredFileTemplate ( $id )
{
try {
Application_Model_Preference :: SetHistoryFileTemplate ( $id );
} catch ( Exception $e ) {
throw $e ;
}
}
public function setConfiguredTemplate ( $id )
{
try {
$template = $this -> loadTemplate ( $id );
$type = $template [ 'type' ];
$setTemplate = 'setConfigured' . ucfirst ( $type ) . 'Template' ;
$this -> { $setTemplate }( $id );
} catch ( Exception $e ) {
throw $e ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
public function getConfiguredTemplateIds ()
{
try {
$id = Application_Model_Preference :: GetHistoryItemTemplate ();
$id2 = Application_Model_Preference :: GetHistoryFileTemplate ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$configured = [];
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( is_numeric ( $id )) {
$configured [] = $id ;
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( is_numeric ( $id2 )) {
$configured [] = $id2 ;
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
return $configured ;
} catch ( Exception $e ) {
throw $e ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
public function createTemplate ( $config )
{
$this -> con -> beginTransaction ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
try {
$type = $config [ 'type' ];
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$method = 'default' . ucfirst ( $type ) . 'Template' ;
$default = $this -> { $method }();
2013-08-01 22:36:18 +02:00
2021-10-11 16:10:47 +02:00
$name = isset ( $config [ 'name' ]) ? $config [ 'name' ] : $default [ 'name' ];
$fields = isset ( $config [ 'fields' ]) ? $config [ 'fields' ] : $default [ 'fields' ];
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$doSetDefault = isset ( $config [ 'setDefault' ]) ? $config [ 'setDefault' ] : false ;
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$template = new CcPlayoutHistoryTemplate ();
$template -> setDbName ( $name );
$template -> setDbType ( $type );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $fields as $index => $field ) {
$isMd = ( $field [ 'isFileMd' ] == 'true' ) ? true : false ;
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$templateField = new CcPlayoutHistoryTemplateField ();
$templateField -> setDbName ( $field [ 'name' ]);
$templateField -> setDbLabel ( $field [ 'label' ]);
$templateField -> setDbType ( $field [ 'type' ]);
$templateField -> setDbIsFileMD ( $isMd );
$templateField -> setDbPosition ( $index );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$template -> addCcPlayoutHistoryTemplateField ( $templateField );
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$template -> save ( $this -> con );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( $doSetDefault ) {
$this -> setConfiguredItemTemplate ( $template -> getDbid ());
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$this -> con -> commit ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
return $template -> getDbid ();
} catch ( Exception $e ) {
$this -> con -> rollback ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
public function updateItemTemplate ( $id , $name , $fields , $doSetDefault = false )
{
$this -> con -> beginTransaction ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
try {
$template = CcPlayoutHistoryTemplateQuery :: create () -> findPk ( $id , $this -> con );
$template -> setDbName ( $name );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( count ( $fields ) === 0 ) {
$t = $this -> defaultItemTemplate ();
$fields = $t [ 'fields' ];
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$template -> getCcPlayoutHistoryTemplateFields () -> delete ( $this -> con );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
foreach ( $fields as $index => $field ) {
$isMd = ( $field [ 'isFileMd' ] == 'true' ) ? true : false ;
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$templateField = new CcPlayoutHistoryTemplateField ();
$templateField -> setDbName ( $field [ 'name' ]);
$templateField -> setDbType ( $field [ 'type' ]);
$templateField -> setDbLabel ( $field [ 'label' ]);
$templateField -> setDbIsFileMD ( $isMd );
$templateField -> setDbPosition ( $index );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$template -> addCcPlayoutHistoryTemplateField ( $templateField );
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$template -> save ( $this -> con );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
if ( $doSetDefault ) {
$this -> setConfiguredItemTemplate ( $template -> getDbid ());
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$this -> con -> commit ();
} catch ( Exception $e ) {
$this -> con -> rollback ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
public function deleteTemplate ( $id )
{
$this -> con -> beginTransaction ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
try {
$template = CcPlayoutHistoryTemplateQuery :: create () -> findPk ( $id , $this -> con );
$template -> delete ( $this -> con );
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
$this -> con -> commit ();
} catch ( Exception $e ) {
$this -> con -> rollback ();
2013-08-20 05:19:13 +02:00
2021-10-11 16:10:47 +02:00
throw $e ;
}
}
2014-11-17 21:53:31 +01:00
}