/** * Delete user * * @param integer $id */ public function deleteAction($id) { $user = User::find($id); // Make sure the user exists if (!$user) { return $this->show404(); } // Delete user if ($user->delete()) { Request::redirectTo('/admin/users'); } }
/** * Runs the application and routes the request. * * @param object $app Instantiated application object. */ public static function run($app) { $route = Router::process(new Request()); // Route to 404 if controller and/or method cannot be found if (!class_exists($route['controller']) or !method_exists($route['controller'], "{$route['method']}Action")) { if ($app->environment() == 'development') { throw new Exception("Unable to find controller for '" . Request::pathInfo() . "'"); } else { $route = Router::set404(); } } // Get method parameters $r = new ReflectionMethod("{$route['controller']}::{$route['method']}Action"); $params = []; foreach ($r->getParameters() as $param) { if (isset($route['params'][$param->getName()])) { $params[] = $route['params'][$param->getName()]; } } unset($r, $param); static::$controller = new $route['controller'](); // Run before filters $beforeAll = EventDispatcher::dispatch("before." . $route['controller'] . "::*"); $beforeAction = EventDispatcher::dispatch("before." . $route['controller'] . "::{$route['method']}"); if ($beforeAll instanceof Response || $beforeAction instanceof Response) { static::$controller->executeAction = false; $response = $beforeAll ?: $beforeAction; } // Execute action if (static::$controller->executeAction) { $response = call_user_func_array(array(static::$controller, $route['method'] . 'Action'), $params); } // Run after filters $afterAll = EventDispatcher::dispatch("after." . $route['controller'] . "::*"); $afterAction = EventDispatcher::dispatch("after." . $route['controller'] . "::{$route['method']}"); if ($afterAll instanceof Response || $afterAction instanceof Response) { $response = $afterAll instanceof Response ? $afterAll : $afterAction; } // Send response if (!$response instanceof Response) { throw new Exception("The controller [{$route['controller']}::{$route['method']}] returned an invalid response."); } else { $response->send(); } // Shutdown the controller static::$controller->__shutdown(); }
public function indexAction() { // Check if the form has been submitted if (Request::method() == 'post') { foreach (Request::$post['settings'] as $setting => $value) { // Get setting $setting = Setting::find('setting', $setting); // If found, set value and save if ($setting) { $setting->value = $value; $setting->save(); } } // Redirect Request::redirectTo('/admin/settings'); } }
/** * Password page. */ public function passwordAction() { // Check if form has been submitted if (Request::method() == 'post') { // Verify current password if ($this->user->verifyPassword(Request::$post['current_password'])) { // Update users password $this->user->updatePassword(Request::$post['new_password']); // Save and redirect if ($this->user->save()) { Request::redirectTo('/account/password'); } } else { $this->user->addError('password', l('errors.current_password_is_invalid')); } } }
/** * Creates the URI for the specified page. * * @param integer $page * * @return string */ public function createUri($page) { $queryString = $this->query; $queryString[] = "page={$page}"; $queryString = implode('&', $queryString); return Request::pathInfo() . "?{$queryString}"; }
/** * Delete status. * * @param integer $id */ public function deleteAction($id) { $status = Status::find($id); // Make sure the status exists if (!$status) { return $this->show404(); } // Delete user if ($status->delete()) { Request::redirectTo('/admin/statuses'); } }
/** * Update ticket. * * @param integer $id */ public function updateAction($id) { if (Request::method() != 'post') { Request::redirectTo('/'); } // Get the ticket $ticket = Ticket::find($id); // Ticket changes $changes = []; foreach (['department', 'status', 'priority'] as $property) { if ($ticket->{$property . '_id'} != Request::$post[$property]) { $change = ['property' => $property]; switch ($property) { case 'department': case 'status': $class = "\\Ticketer\\Models\\" . Inflector::classify($property); $change['from'] = $class::find($ticket->{"{$property}_id"})->name; $change['to'] = $class::find(Request::$post[$property])->name; break; case 'priority': $change['from'] = getPriorityName($ticket->priority_id); $change['to'] = getPriorityName(Request::$post['priority']); break; } $changes[] = $change; } } // Update ticket properties $ticket->set(['department_id' => Request::$post['department'], 'status_id' => Request::$post['status'], 'priority_id' => Request::$post['priority']]); // Ticket reply $reply = new TicketReply(['message' => Request::$post['message'], 'user_id' => $this->currentUser->id, 'ticket_id' => $ticket->id, 'changes' => json_encode($changes)]); if (count($changes) or Request::$post['message'] != '') { if ($reply->save() and $ticket->save()) { Request::redirectTo($ticket->href()); } } else { Request::redirectTo($ticket->href()); } $this->set(compact('ticket', 'reply')); $this->render['view'] = 'tickets/view'; }
/** * Logout page. */ public function logoutAction() { setcookie('ticketer', NULL, time(), '/'); Request::redirectTo('/'); }
/** * Redirects to the specified path. */ public function redirectTo($path) { Request::redirectTo($path); }
/** * Routes the request to the controller. * * @param Request $request */ public static function process(Request $request) { $uri = "/" . trim($request->pathInfo(), '/'); // Check if this is root page if (isset(static::$routes['root']) and $request->pathInfo() == '/') { return static::setRoute(static::$routes['root']); } // The fun begins foreach (static::$routes as $route) { // Replace tokens $route->route = str_replace(array_keys(static::$tokens), array_values(static::$tokens), $route->route); // Does the route match the request? $pattern = "#^{$route->route}" . '(?<extension>' . implode('|', static::$extensions) . ")?\$#"; if (preg_match($pattern, $uri, $params)) { unset($params[0]); $route->params = array_merge($route->params, $params); $route->destination = preg_replace($pattern, $route->destination, $uri); // Merge params with defaults $route->params = array_merge($route->defaults, $route->params); if (in_array(strtolower($request->method()), $route->method)) { return static::setRoute($route); } } } // No matches, try 404 route if (isset(static::$routes['404'])) { return static::set404(); } else { throw new Exception("No routes found for '{$uri}'"); } }
/** * Returns the code for a link unless the current request matches the URL. * * @param string $label The label * @param string $url The URL * @param array $options Options for the URL code (class, title, etc) * * @return string */ public static function LinkToUnlessCurrent($label, $url, array $attributes = array()) { if (Request::matches($url)) { return $label; } else { return static::link($label, $url, $attributes); } }
/** * Delete department * * @param integer $id */ public function deleteAction($id) { $department = Department::find($id); // Make sure the department exists if (!$department) { return $this->show404(); } // Delete user if ($department->delete()) { Request::redirectTo('/admin/departments'); } }