} else { if ($name == 'create' && $_SERVER['REQUEST_METHOD'] == 'POST') { /** * Here we are assuming there is only ever one kind of POST * Hence, the controller chunk is here * * However, it would be trivial to fully implement the MVC design pattern */ $view = new Short_View('static/create.phtml'); if (isset($_POST['url']) && parse_url($_POST['url']) !== false) { require_once 'Short/Model.php'; require_once 'Short/Short.php'; $short = new Short_Short(); $short->generateName(); $short->setUrl($_POST['url']); $short->create(); $view->short = $short; } else { $view->url = false; } Short_View::wrapView($view); } else { /** * In the case where the short name was not create and the method was POST, just ignore it. * A programmer posting to a URL shortener is confusing the service in general. */ //Welcome screen $welcome = new Short_View('static/welcome.phtml'); echo Short_View::wrapView($welcome); } }
/** * Load statistics data and pass it to the statistics view */ private function statistics() { //Load log data require_once 'Short/LogEntry.php'; $select = Short_LogEntry::getSelect(); /** * Load an intial data set that can be used by the frontend */ $select->where('short_id = ?', $this->short->id); $select->order('timestamp DESC'); $select->limit(isset($_GET['count']) ? (int) $_GET['count'] : 100, isset($_GET['offset']) ? (int) $_GET['offset'] : 0); //Filter countries if passed if (isset($_GET['country']) && !empty($_GET['country'])) { $countries = explode('|', $_GET['country']); foreach ($countries as $country) { $select->orWhere('country = ?', $country); } } //Load $log_entries = Short_LogEntry::getTable()->fetchAll($select)->toArray(); /** * Count the amount of clicks overall */ $select->reset(); $select->from(Short_LogEntry::getTableName(), 'COUNT(*) as num'); $select->where('short_id = ?', $this->short->id); $count = Short_LogEntry::getTable()->fetchRow($select)->num; //Render view $view = new Short_View('short/statistics.phtml'); $view->log_entries = $log_entries; $view->short = $this->short; $view->count = $count; echo Short_View::wrapView($view); }