Skip to content

Commit

Permalink
rm show time concept
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwbradw committed Dec 30, 2024
1 parent 993b8d0 commit c578d69
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 280 deletions.
53 changes: 10 additions & 43 deletions app/api2/app/Http/Routes/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
use App\Show as Show;

//Helpers
use App\Showtime as Showtime;
use App\Host as Host;
use App\Social as Social;
use App\Member as Member;
use App\Upload as Upload;
use App\Socan as Socan;

use Illuminate\Support\Facades\Log;

Route::group(array('prefix' => 'show'), function () {
//Creating new Show
Route::post('/', function () {
$show = Show::create((array) Input::get()['show']);
$owners = Input::get()['owners'];
$social = Input::get()['social'];
$showtimes = Input::get()['showtimes'];

$socials = array();
$show_times = array();
//Create owners
foreach ($owners as $owner) {
Show::find($show->id)->members()->attach($owner['id']);
Expand All @@ -29,12 +28,8 @@
$s['show_id'] = $show['id'];
$socials[] = Social::create($s);
}
//Create Showtimes
foreach ($showtimes as $key => $showtime) {
$showtime['show_id'] = $show['id'];
$show_times[] = Showtime::create((array) $showtime);
}
return Response::json(array('show' => $show, 'social' => $socials, 'owners' => $owners, 'showtimes' => $show_times));

return Response::json(array('show' => $show, 'social' => $socials, 'owners' => $owners));
});

//Return shows that are active
Expand All @@ -46,18 +41,13 @@
Route::group(array('prefix' => '{id}'), function ($id = id) {


//Returns show object including social, showtimes
//Returns show object including social
Route::get('/', function ($id) {
if (!$show = Show::find($id)) return null;

$show->social = Show::find($id)->social;
$show->show_times = Show::find($id)->showtimes;
return Response::json($show);
});
Route::get('times', function ($id) {
//return Showtimes::where('show_id','=',$show_id)->get();
return Show::find($id)->showtimes;
});
Route::post('/', function ($id) {
return Response::json(Show::find($id)->update((array) Input::get()['show']));
});
Expand Down Expand Up @@ -108,12 +98,10 @@
$show = Input::get()['show'];
$social = Input::get()['social'];
$owners = Input::has('owners') ? Input::get()['owners'] : null;
$showtimes = Input::get()['showtimes'];
$s = Show::find($id);
$s->update($show);

$socials = array();
$show_times = array();

if ($owners) {
//Detach current owners
Expand Down Expand Up @@ -150,30 +138,7 @@
$socials[] = Social::create($item);
}
}
//Find out which entries no longer exist. (This is because entries aren't deleted when the button is pressed in UI, ie. not RESTful)
$existing_to_delete = Show::find($id)->showtimes;
foreach ($existing_to_delete as $key => $e) {
$still_exists = false;
foreach ($showtimes as $item) {
if (isset($item['id']) && $e['id'] == $item['id']) {
$still_exists = true;
}
}
if (!$still_exists) {
Showtime::find($e['id'])->delete();
}
}
//Create or update social entries
foreach ($showtimes as $item) {
if (isset($item['id'])) {
$s = Showtime::find($item['id']);
unset($item['id']);
$show_times[] = $s->update($item);
} else {
$show_times[] = Showtime::create($item);
}
}
return Response::json(array('show' => $show, 'social' => $socials, 'owners' => $owners, 'showtimes' => $show_times));
return Response::json(array('show' => $show, 'social' => $socials, 'owners' => $owners));
});
Route::get('episodes/{offset}', function ($id, $offset = offset) {
$podcasts = Show::find($id)->podcasts()->orderBy('id', 'desc')->limit(50)->offset($offset)->get();
Expand Down Expand Up @@ -230,8 +195,10 @@
});


Route::get('nextshow/{current_time}', function ($id, $time = current_time) {
return Response::json(Show::find($id)->nextShowTime($time));
Route::get('nextshow', function ($id) {
$show = Show::find($id);

return Response::json($show->nextShowTime());
});
Route::get('xml', function ($id) {
return Show::find($id)->make_show_xml();
Expand Down
138 changes: 18 additions & 120 deletions app/api2/app/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class Show extends Model
{
Expand Down Expand Up @@ -37,133 +38,30 @@ public function images()
}
public function nextShowTime()
{
date_default_timezone_set('America/Los_Angeles');

$showtimes = $this->showtimes;
# request to "https://new.citr.ca/api/schedule?title="+show_title); using curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://new.citr.ca/api/schedule?title=" . urlencode($this->name));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file_contents = curl_exec($ch);
curl_close($ch);

//Get Today
$time = strtotime('now');
//Get Day of Week (0-6)
$day_of_week = date('w', $time);
//Get mod 2 of (current unix minus days to last sunday) then divide by 8.64E7 * 7 to get number of weeks elapsed since epoch start.
$current_week = floor(($time - intval($day_of_week * 60 * 60 * 24)) / (60 * 60 * 24 * 7));
if ((int) $current_week % 2 == 0) {
$current_week = 1;
} else {
$current_week = 2;
};

//Get Current Time (0-23:0-59:0-59)
$current_time = date('H:i:s', strtotime('now'));

//Making sure if today is sunday, it does not get last sunday instead of today.
if ($day_of_week == 0) {
$week_0_start = strtotime('today');
$week_1_start = strtotime('+1 week', $week_0_start);
$week_2_start = strtotime('+1 week', $week_1_start);
} else {
$week_0_start = strtotime('last sunday 00:00:00');
$week_1_start = strtotime('+1 week', $week_0_start);
$week_2_start = strtotime('+1 week', $week_1_start);
}

//Constants (second conversions)
$one_day = 24 * 60 * 60;
$one_hour = 60 * 60;
$one_minute = 60;

foreach ($showtimes as $show_time) {
$show_time_day_offset = ($show_time['start_day']) * $one_day;
$show_time_hour_offset = date_parse($show_time['start_time'])['hour'] * $one_hour;
$show_time_minute_offset = date_parse($show_time['start_time'])['minute'] * $one_minute;
$show_time_unix_offset = $show_time_day_offset + $show_time_hour_offset + $show_time_minute_offset;

if ($show_time['start_day'] != $show_time['end_day']) {
$show_duration = (24 - date_parse($show_time['start_time'])['hour'] + date_parse($show_time['end_time'])['hour']) * $one_hour + (60 - date_parse($show_time['start_time'])['minute'] + date_parse($show_time['end_time'])['minute']) * $one_minute;
} else {
$show_end_time_unix_offset = $show_time['end_day'] * $one_day + date_parse($show_time['end_time'])['hour'] * $one_hour + date_parse($show_time['end_time'])['minute'] * $one_minute;
$show_duration = abs($show_end_time_unix_offset - $show_time_unix_offset);
}

//Unix timestamp of possible show start times
$week_0_show_unix = $week_0_start + $show_time_unix_offset;
$week_1_show_unix = $week_1_start + $show_time_unix_offset;
$week_2_show_unix = $week_2_start + $show_time_unix_offset;

//DST Offset
if ((date('I', $week_0_start) == '1') && (date('I', $week_0_show_unix) == '0')) {
$week_0_show_unix += 3600;
}
if ((date('I', $week_0_start) == '0') && (date('I', $week_0_show_unix) == '1')) {
$week_0_show_unix -= 3600;
}
if ((date('I', $week_1_start) == '1') && (date('I', $week_1_show_unix) == '0')) {
$week_1_show_unix += 3600;
}
if ((date('I', $week_1_start) == '0') && (date('I', $week_1_show_unix) == '1')) {
$week_1_show_unix -= 3600;
}
if ((date('I', $week_2_start) == '1') && (date('I', $week_2_show_unix) == '0')) {
$week_2_show_unix += 3600;
}
if ((date('I', $week_2_start) == '0') && (date('I', $week_2_show_unix) == '1')) {
$week_2_show_unix -= 3600;
}
$schedule = json_decode($file_contents);

$default_start = strtotime(date('Y-m-d H:00:00'));
$default_end = $default_start + 3600;

// if a showtime's day has already been passed. If no, add it to week 0, if yes we have to add it to week 2 instead of week 0
if ($show_time['start_day'] == $day_of_week || $show_time['start_day'] > $day_of_week) {
//Hasn't happened yet, look at weeks 0 and 1
if ($show_time['alternating'] == '0') {
//Occurs Weekly, add to this week
$next_show = $week_0_show_unix;
} elseif ($show_time['alternating'] == $current_week) {
//Occurs this week, add to remainder of this week
$next_show = $week_0_show_unix;
} else {
//Doesn't occur this week, add to week 1
$next_show = $week_1_show_unix;
}
} else {
//Already occured this week
if ($show_time['alternating'] == '0') {
//Occurs weekly, add to week 1
$next_show = $week_1_show_unix;
} elseif ($show_time['alternating'] == $current_week) {
//Occurs this week, add to week 2
$next_show = $week_2_show_unix;
} else {
//Doesn't occur this week, add to week 1
$next_show = $week_1_show_unix;
}
}

$end = $next_show + $show_duration;
$candidates[] = array('start' => $next_show, 'end' => $end, 'week' => $current_week);
}
//Find the minimum start time
if (isset($candidates)) {
$min = $candidates[0];
foreach ($candidates as $i => $v) {
if ($v['start'] < $min['start']) {
$min = $candidates[$i];
}
}
} else {
//No showtimes for this show, create one.
$seconds_elapsed = strtotime('now') % $one_hour;
$length = is_array($schedule) ? count($schedule) : 0;

if ($seconds_elapsed <= 15 * $one_minute) {
$start = strtotime('now') - $seconds_elapsed;
} elseif ($seconds_elapsed > 15 * $one_minute && $seconds_elapsed <= 45 * $one_minute) {
$start = strtotime('now') - $seconds_elapsed + 30 * $one_minute;
if ($length > 0){
$start = strtotime($schedule[0]->schedule[0]->start);
$end = strtotime($schedule[0]->schedule[0]->end);
} else {
$start = strtotime('now') - $seconds_elapsed + $one_hour;
$start = $default_start;
$end = $default_end;
}
$end = $start + $one_hour;
$min = array('start' => $start, 'end' => $end, 'mins' => $seconds_elapsed / $one_minute);
}
return $min;

return array('start' => $start, 'end' => $end);
}
public function make_show_xml()
{
Expand Down
12 changes: 6 additions & 6 deletions app/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ angular.module('djland.api', []).factory('call', function ($http, $location) {
getUploads: function(){
return $http.get(API_URL_BASE+'/upload');
},
getNextShowTime: function(show_id,current_time){
return $http.get(API_URL_BASE+"/show/"+show_id+"/nextshow/"+current_time);
getNextShowTime: function(show_id){
return $http.get(API_URL_BASE+"/show/"+show_id+"/nextshow");
},
getMemberPermissions: function(member_id){
return $http.get(API_URL_BASE+'/member/'+member_id+'/permission');
Expand Down Expand Up @@ -86,11 +86,11 @@ angular.module('djland.api', []).factory('call', function ($http, $location) {
saveBroadcasts: function(specialbroadcasts){
return $http.post(API_URL_BASE + '/specialbroadcasts', angular.toJson({'specialbroadcasts':specialbroadcasts}));
},
saveShow: function(show_object,social_objects,owner_objects,show_time_objects){
return $http.post(API_URL_BASE+'/show/'+show_object.id,angular.toJson({'show':show_object,'social':social_objects,'owners':owner_objects,'showtimes':show_time_objects}) );
saveShow: function(show_object,social_objects,owner_objects){
return $http.post(API_URL_BASE+'/show/'+show_object.id,angular.toJson({'show':show_object,'social':social_objects,'owners':owner_objects}) );
},
saveNewShow: function(show_object,social_objects,owner_objects,show_time_objects){
return $http.post(API_URL_BASE+'/show',angular.toJson({'show':show_object,'social':social_objects,'owners':owner_objects,'showtimes':show_time_objects}) );
saveNewShow: function(show_object,social_objects,owner_objects){
return $http.post(API_URL_BASE+'/show',angular.toJson({'show':show_object,'social':social_objects,'owners':owner_objects}) );
},
savePlaysheet: function(playsheet,playitems,podcast,ads){
//this one could have failed
Expand Down
3 changes: 1 addition & 2 deletions app/js/playsheet/playsheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@
}

this.updateTime = function () {
var now = new Date();
call.getNextShowTime(this.active_show.id, now).then(
call.getNextShowTime(this.active_show.id).then(
(
function (response) {
var start_unix = response.data.start;
Expand Down
Loading

0 comments on commit c578d69

Please sign in to comment.