Last active
September 3, 2016 17:46
-
-
Save gordinmitya/2dc9f9903fe37358da097e22a9dbe23e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Models; | |
abstract class EventSubject { | |
public static abstract function subjectName(); | |
public static function reverseName(){} | |
/*public static function subjectId() { | |
return $this->subjectName().'_id'; | |
}*/ | |
public static function subjectTable() { | |
return static::subjectName().'s'; | |
} | |
/*public static function reverseId() { | |
return $this->reverseName().'_id'; | |
} | |
public static function reverseTable() { | |
return static::reverseName().'s'; | |
}*/ | |
public static function list() { | |
$q = 'SELECT id, name FROM '.EventSubject::subjectTable(); | |
echo "$q"; | |
$smbt = \App\PdoHelper::get()->query($q); | |
$res = $smbt->fetchAll(\PDO::FETCH_ASSOC); | |
for ($i=0;$i<count($res);$i++){ | |
$res[$i]['id'] = intval($res[$i]['id']); | |
} | |
return $res; | |
} | |
public static function search($prefix) | |
{ | |
$q = "SELECT id, name FROM $this->subjectTable() WHERE name LIKE :name"; | |
$smbt = \App\PdoHelper::get()->prepare($q); | |
$smbt->execute(array('name' => $prefix.'%')); | |
$res = $smbt->fetchAll(\PDO::FETCH_ASSOC); | |
for ($i=0;$i<count($res);$i++){ | |
$res[$i]['id'] = intval($res[$i]['id']); | |
} | |
return $res; | |
} | |
public static function details($id) | |
{ | |
return "details for $this->subjectName() with id=$id"; | |
} | |
public static function schedule($id) { | |
$weeks = WeekType::all(); | |
$days = DaysOfWeek::all(); | |
$types = EventType::all(); | |
$smbt = \App\PdoHelper::get()->query("SELECT | |
`week_id`, | |
`day_id`, | |
`event_index`, | |
`course_id`, (SELECT `name` FROM `courses` WHERE `id` = e.`course_id`) course, | |
`type_id`, | |
`group_id`, (SELECT `name` FROM `groups` WHERE `id` = e.`group_id`) group, | |
`subgroup`, | |
`teacher_id`, (SELECT `name` FROM `teachers` WHERE `id` = e.`teacher_id`) teacher, | |
`location` | |
FROM event_templates e WHERE `$this->subjectId()` = $id | |
ORDER BY `week_id`, `day_id`, `event_index`, `subgroup`"); | |
$db_res = $smbt->fetchAll(\PDO::FETCH_ASSOC); | |
if (count($db_res) == 0) { | |
throw new Exception("$this->subjectName() with id=$id not found", 404); | |
} | |
$raw_result = array(1=>array(), array()); | |
$intFields = array("event_index", "course_id", "type_id", | |
"type", "group_id", "subgroup", "teacher_id"); | |
$intFieldsCount = count($intFields); | |
for ($i = 0; $i < count($db_res); $i++) { | |
for ($j=0;$j<$intFieldsCount;$j++) { | |
$db_res[$i][intFields[$j]] = intval($db_res[$i][intFields[$j]]); | |
} | |
$weekId = $db_res[$i]['week_id']; | |
$dayId = $db_res[$i]['day_id']; | |
if ($weekId == 3) { | |
$raw_result[1][$dayId][] = $db_res[$i]; | |
$raw_result[2][$dayId][] = $db_res[$i]; | |
} else { | |
$raw_result[$weekId][$dayId][] = $db_res[$i]; | |
} | |
} | |
$result = array(); | |
foreach ($raw_result as $week_id => $week) { | |
$days_of_week = array(); | |
foreach ($days as $day_id => $day_name) { | |
$days_of_week[] = [ | |
'day_id' => $day_id, | |
'day' => $day_name, | |
'events' => [] | |
]; | |
} | |
foreach ($week as $day_id => $events) { | |
$days_of_week[$day_id-1]['events'] = $events; | |
} | |
$result[] = [ | |
"week_id" => $week_id, | |
"week" => $weeks[$week_id], | |
"days" => $days_of_week, | |
]; | |
} | |
return $result; | |
} | |
public static function scheduleUpdates($id) { | |
$q = "SELECT `updated_at` FROM `$this->subjectTable()` WHERE `id`=$id"; | |
$smbt = \App\PdoHelper::get()->query($q); | |
$res = $smbt->fetch(\PDO::FETCH_ASSOC); | |
if (!$res){ | |
throw new \Exception("$this->subjectName() with id=$id not found", 404); | |
} | |
$updated = strtotime($res['updated_at']); | |
return $updated; | |
} | |
} |
lartie
commented
Sep 3, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment