R
RadoXX
Всем привет! Подскажите пожалуйста что нужно дописать(подправить) в скрипте чтобы выводилось расписание программ на всю неделю? На данный момент выводится программа на текущий день.Например если сегодня вторник то выводится на вторник.Скажите пожалуйста что нужно подправить в скрипте?
PHP:
class BroadcastModelSchedule extends JModel {
protected $db;
public $date_for_db;
public $program_starts;
function __construct() {
parent::__construct();
$this->db = JFactory::getDBO();
}
/*
* Function name: getSchedule()
* Description: Gets all the shows from the database and sorts them by the 'showstart' key,
* then continues with calculating remaining, runtimes and executes the onair functions
* to set one of the elements as being currently on-air.
*/
public function getSchedule() {
// API
// API
$app = JFactory::getApplication('site');
$this->appAPI = $app;
$componentParams = $this->appAPI->getParams('com_cmbroadcastscheduler');
$this->params = $componentParams;
// Init some vars
$this->program_starts = $this->params->get('daystart');
$now = date("H:i:s");
$nowDate = date("Y-m-d");
// Calculate Current internal program date (not real date)
if($now < $this->program_starts) {
$this->date_for_db = date("Y-m-d", mktime(0, 0, 0, date('m'), date('d')-1, date('Y')));
} else {
$this->date_for_db = date("Y-m-d");
}
$today = TRUE;
$postFlag = FALSE;
$postFlag = JRequest::getVar('ddate', '', 'post', 'string');
if($postFlag) {
// Check for request forgeries
JRequest::checkToken() or jexit( 'Invalid Token' );
// This will get pasted by the user
$posted_Date = $postFlag;
$date_parts = explode('-', $posted_Date);
if((count($date_parts) == 3) && checkdate((int)$date_parts[1], (int)$date_parts[2], (int)$date_parts[0])) {
if($this->date_for_db == $posted_Date) {
$this->date_for_db = $posted_Date;
$today = TRUE;
} else {
$this->date_for_db = $posted_Date;
$today = FALSE;
}
} else { // If the input date was wrong get current program
$this->date_for_db = date('Y-m-d');
$today = TRUE;
}
}
// Main query
$q = "SELECT `show_id`,
`air_date`,
`show_name`,
`show_desc`,
`#__cmbroadcastsched_shows`.`alias`,
`#__cmbroadcastsched_shows`.`s_id`,
`showstart`,
`showend`,
`rated_icon_link`,
`rated_desc`,
`genre`
FROM `#__cmbroadcastsched`
JOIN `#__cmbroadcastsched_shows`
ON `#__cmbroadcastsched`.`show_id` = `#__cmbroadcastsched_shows`.`s_id`
JOIN `#__cmbroadcastsched_rated`
ON `#__cmbroadcastsched_shows`.`rated_id` = `#__cmbroadcastsched_rated`.`rated_id`
JOIN `#__cmbroadcastsched_genre`
ON `#__cmbroadcastsched_shows`.`genre_id` = `#__cmbroadcastsched_genre`.`id`
WHERE `#__cmbroadcastsched`.`air_date` = " . $this->db->quote($this->date_for_db) . "
AND `#__cmbroadcastsched_shows`.`published` = 1
AND `#__cmbroadcastsched`.`published` = 1
ORDER BY `showstart` ASC;";
$this->db->setQuery($q);
$this->db->query();
$data = $this->db->loadAssocList();
// Get persistant shows
$q = "SELECT
`pers_id`,
`#__cmbroadcastsched_persistent`.`show_id`,
`#__cmbroadcastsched_shows`.`alias`,
`#__cmbroadcastsched_shows`.`s_id`,
`showstart`,
`showend`,
`days`,
`#__cmbroadcastsched_persistent`.`published`,
`#__cmbroadcastsched_shows`.`show_name`,
`#__cmbroadcastsched_shows`.`show_desc`,
`#__cmbroadcastsched_shows`.`genre_id`,
`#__cmbroadcastsched_rated`.`rated_desc`,
`#__cmbroadcastsched_rated`.`rated_icon_link`,
`#__cmbroadcastsched_genre`.`genre`
FROM `#__cmbroadcastsched_persistent`
INNER JOIN `#__cmbroadcastsched_shows`
ON `#__cmbroadcastsched_persistent`.`show_id` = `#__cmbroadcastsched_shows`.`s_id`
INNER JOIN `#__cmbroadcastsched_rated`
ON `#__cmbroadcastsched_shows`.`rated_id` = `#__cmbroadcastsched_rated`.`rated_id`
INNER JOIN `#__cmbroadcastsched_genre`
ON `#__cmbroadcastsched_shows`.`genre_id` = `#__cmbroadcastsched_genre`.`id`
WHERE `#__cmbroadcastsched_persistent`.`published` = 1;";
$this->db->setQuery($q);
$this->db->query();
$pers_data = $this->db->loadAssocList();
if($pers_data) {
foreach($pers_data as $persistent) {
$data[] = $persistent;
}
}
if($data) {
// Sorting Data
foreach($data as $key=>$row) {
$showstart[$key] = $row['showstart'];
}
array_multisort($showstart, SORT_ASC, $data);
// More data processing
$afterDay = array();
$beforeDay = array();
// Seperate program to After Day Start and Before Day Start
// and skip shows that are not for today.
foreach($data as $program) {
if($program['showstart'] >= $this->program_starts) { // After day start
// If the item is for today continue
if(!empty($program['days'])) {
if($dayName = $this->DayResolver($program['days']) ) {
$program['dayString'] = $dayName;
$afterDay[] = $program;
}
} else {
$dayName = $this->DayResolver();
$program['dayString'] = $dayName;
$afterDay[] = $program;
}
} else { // Before day start
if(!empty($program['days'])) {
if($dayName = $this->DayResolver($program['days']) ) {
$program['dayString'] = $dayName;
$beforeDay[] = $program;
}
} else {
$dayName = $this->DayResolver();
$program['dayString'] = $dayName;
$beforeDay[] = $program;
}
}
}
// Decide which function to run, if the user selected a day other than today
if($today) {
$newArr = $this->CombineToday($afterDay, $beforeDay, $now);
} else {
$newArr = $this->CombineNotToday($afterDay, $beforeDay, $now);
}
return $newArr;
}
}
/*
* Function name: CombineToday()
* Description: Combines the data into the correct order and sets
* one of the item as "onair".
*/
protected function CombineToday($afterDay, $beforeDay, $now) {
// Recombine with the correct order
$newArr = array();
foreach($afterDay as $ad) {
// Calculate runtime, remaining and check for and set the current show on-air
$thisShowStart = $this->TimeArrangerStart($ad['showstart'], $this->program_starts);
$thisShowEnd = $this->TimeArrangerEnd($ad['showstart'], $ad['showend']);
$remain = (int) ( ($thisShowEnd - strtotime($now)) / 60);
$runtime = (int) ( ($thisShowEnd - $thisShowStart) / 60);
if($thisShowEnd >= strtotime($now) && $remain < $runtime) {
$ad['onair'] = 1;
$ad['remain'] = $remain;
}
$ad['runtime'] = $runtime;
$newArr[] = $ad;
}
foreach($beforeDay as $bd) {
// Calculate runtime, remaining and check for and set the current show on-air
$thisShowStart = $this->TimeArrangerStart($bd['showstart'], $this->program_starts);
$thisShowEnd = $this->TimeArrangerEnd($bd['showstart'], $bd['showend']);
$remain = (int) ( ($thisShowEnd - strtotime($now)) / 60);
$runtime = (int) ( ($thisShowEnd - $thisShowStart) / 60);
if($thisShowEnd >= strtotime($now) && $remain < $runtime) {
$bd['onair'] = 1;
$bd['remain'] = $remain;
}
$bd['runtime'] = $runtime;
$newArr[] = $bd;
}
// ^^ The code above is really awesome
return $newArr;
}
/*
* Function name: CombineNotToday()
* Description: Combines the data into the correct order and doesn't
* set "onair".
*/
protected function CombineNotToday($afterDay, $beforeDay, $now) {
// Recombine with the correct order
$newArr = array();
foreach($afterDay as $ad) {
// Calculate runtime, remaining and check for and set the current show on-air
$thisShowStart = $this->TimeArrangerStart($ad['showstart'], $this->program_starts);
$thisShowEnd = $this->TimeArrangerEnd($ad['showstart'], $ad['showend']);
$runtime = (int) ( ($thisShowEnd - $thisShowStart) / 60);
$ad['runtime'] = $runtime;
$newArr[] = $ad;
}
foreach($beforeDay as $bd) {
// Calculate runtime, remaining and check for and set the current show on-air
$thisShowStart = $this->TimeArrangerStart($bd['showstart'], $this->program_starts);
$thisShowEnd = $this->TimeArrangerEnd($bd['showstart'], $bd['showend']);
$runtime = (int) ( ($thisShowEnd - $thisShowStart) / 60);
$bd['runtime'] = $runtime;
$newArr[] = $bd;
}
// ^^ The code above is really awesome
return $newArr;
}
/*
* Function name: DayResolver()
* Description: Sets the day name string for the items,
* if nothing gets provided then the item is dynamic
* and will get the current day name.
*/
protected function DayResolver($dayArray = null) {
if($dayArray) {
$stored_days = explode(",", $dayArray);
if( in_array(date("D", strtotime($this->date_for_db)), $stored_days) ) {
return date("D", strtotime($this->date_for_db));
} else {
return false;
}
} else {
return date("D", strtotime($this->date_for_db) );
}
}
/*
* Function name: TimeArrangerStart()
* Description: Takes the start and end time of a show and calculates the correct
* play times.
*/
protected function TimeArrangerStart($start, $starting_time) {
//returns the correct time
$start_str = strtotime($start);
$reformated_end = $start_str;
return $reformated_end;
}
/*
* Function name: TimeArrangerEnd()
* Description: Takes the start and end time of a show and calculates the correct
* play times.
*/
protected function TimeArrangerEnd($start, $end) {
//returns the correct time
$start_str = strtotime($start);
$end_str = strtotime($end);
if($start_str > $end_str) {
$end_str = $end_str + 86400;
}
$reformated_end = $end_str;
return $reformated_end;
}
}