Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Sked schedule parser algorithm
- * Developed by Hauke Marquardt
- *
- * License: CC BY-SA
- * Uses: http://sourceforge.net/projects/simplehtmldom/
- */
- class Day
- {
- public $name;
- public $date;
- public $startCol;
- public $endCol;
- public function __construct($_startCol = 0, $_endCol = 0) {
- $this->startCol = $_startCol;
- $this->endCol = $_endCol;
- }
- public function dateAsTimestamp() {
- return DateTime::createFromFormat('d.m.Y h:i:s', $this->date . '00:00:00')->getTimestamp();
- }
- }
- class Event
- {
- public $startRow;
- public $rowspan;
- public $description;
- public $startTime;
- public $endTime;
- public $day;
- public $isImportant;
- public function __construct($_startRow = 0, $_rowspan = 0, $_description = '') {
- $this->startRow = $_startRow;
- $this->rowspan = $_rowspan;
- $this->description = htmlentities($_description);
- $this->startTime = ($this->startRow - 1) * 5 * 60 + 7 * 60 * 60;
- $this->endTime = $this->startTime + $this->rowspan * 5 * 60;
- }
- }
- class SkedScheduleParser {
- public $url = null;
- public $days = null;
- public $events = null;
- private $html = null;
- private $curl = null;
- public function __construct($_url) {
- $this->url = utf8_decode($_url);
- $this->curl = new Curl();
- }
- public function loadHTML() {
- $this->html = str_get_html($this->curl->get($this->url));
- }
- public function parseTable($tableNumber = 0) {
- $table = $this->html->find('table', $tableNumber);
- $this->days = array();
- $cols = $table->find('colgroup', 0)->find('col');
- $newDay = false;
- $tmpDay = new Day();
- // block first 2 and last 2 colums
- for($i = 2; $i < count($cols) - 2; $i++) {
- if($cols[$i]->width == 5 && !$newDay) {
- $tmpDay->startCol = $i;
- $newDay = true;
- }
- else if($cols[$i]->width == 5 && $newDay) {
- $tmpDay->endCol = $i;
- $this->days[] = new Day($tmpDay->startCol, $tmpDay->endCol);
- $newDay = false;
- // end loop if sunday reached
- if(count($this->days) == 7) {
- break;
- }
- }
- }
- $daysCol = $table->find('tr', 0)->find('td');
- // block first 2 and last 2 colums
- $colIterator = 0;
- for($i = 0; $i < count($daysCol); $i++) {
- foreach($this->days as &$day) {
- if($day->startCol == $colIterator) {
- $day->name = trim(substr($daysCol[$i]->plaintext, 0, strpos($daysCol[$i]->plaintext, ',')));
- $day->date = trim(substr($daysCol[$i]->plaintext, strrpos($daysCol[$i]->plaintext, ',') + 1, strlen($daysCol[$i]->plaintext)));
- }
- }
- if(isset($daysCol[$i]->colspan)) {
- $colIterator += intval($daysCol[$i]->colspan);
- }
- else {
- $colIterator ++;
- }
- }
- // initialize row iterator for every colum
- $rowIterator = array();
- for($i = 0; $i < count($cols); $i++) {
- $rowIterator[$i] = 0;
- }
- // initialize array for all events
- $this->events = array();
- foreach($this->days as $day) {
- $this->events[] = array();
- }
- // reset colIterator
- $tableRows = $table->find('tr');
- $currentRowIndex = 0;
- foreach($tableRows as $tableRow) {
- $tableRowCols = $tableRow->find('td');
- foreach($tableRowCols as $tableRowCol) {
- $rowspan = 1;
- if(isset($tableRowCol->rowspan)) {
- $rowspan = intval($tableRowCol->rowspan);
- }
- $colspan = 1;
- if(isset($tableRowCol->colspan)) {
- $colspan = intval($tableRowCol->colspan);
- }
- for($i = 0; $i < count($rowIterator); $i++) {
- if($rowIterator[$i] <= $currentRowIndex) {
- for($o = 0; $o < $colspan; $o++) {
- $rowIterator[$i + $o] += $rowspan;
- }
- // find events
- if(isset($tableRowCol->id) && isset($tableRowCol->class) && $tableRowCol->class == 'v') {
- for($d = 0; $d < count($this->days); $d++) {
- if($this->days[$d]->startCol <= $i && $this->days[$d]->endCol >= $i) {
- $this->events[$d][] = new Event($currentRowIndex,
- $rowspan,
- $tableRowCol->innertext
- );
- break;
- }
- }
- }
- break;
- }
- }
- }
- $currentRowIndex++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement