/** * Hooked perform method in order to inject body element id creation. * * In order to avoid clashes, these body element id will be joined * with a minus sign. Otherwise the controller "x" with action * "y_z" would be given the same id as the controller "x/y" with * the action "z", namely "x_y_z". With the minus sign this will * result in the ids "x-y_z" and "x_y-z". * * Plugins will always have a leading 'plugin-' and the decamelized * plugin name in front of the id. * * @param String $unconsumed_path Path segment containing action and * optionally arguments or format * @return Trails_Response from parent controller */ public function perform($unconsumed_path) { // Extract action from unconsumed path segment list($action) = $this->extract_action_and_args($unconsumed_path); // Extract decamelized controller name from class name $controller = preg_replace('/Controller$/', '', get_class($this)); $controller = Trails_Inflector::underscore($controller); // Build main parts of the body element id $body_id_parts = explode('/', $controller); $body_id_parts[] = $action; // If the controller is from a plugin, Inject plugin identifier // and name of plugin if (basename($this->dispatcher->trails_uri, '.php') === 'plugins') { $plugin = basename($this->dispatcher->trails_root); $plugin = Trails_Inflector::underscore($plugin); array_unshift($body_id_parts, $plugin); array_unshift($body_id_parts, 'plugin'); } // Create and set body element id $body_id = implode('-', $body_id_parts); PageLayout::setBodyElementId($body_id); return parent::perform($unconsumed_path); }
function get_default_template($action) { $class = get_class($this); $controller_name = Trails_Inflector::underscore(substr($class, 0, -10)); return $controller_name . '/' . $action; }
/** * Redirects to another location. * * @param String $to New location */ public function redirect($to) { $arguments = func_get_args(); if (Request::isXhr()) { $url = call_user_func_array('parent::url_for', $arguments); $url_chunk = Trails_Inflector::underscore(substr(get_class($this), 0, -10)); $index_url = $url_chunk . '/index'; if (strpos($url, $index_url) !== false) { $this->flash['update-times'] = $this->course->id; } } return call_user_func_array('parent::redirect', $arguments); }
/** * Renders the template of the given action as the response's body. * * @param string the action * * @return void */ function render_action($action) { $class = get_class($this); $controller_name = Trails_Inflector::underscore(substr($class, 0, -10)); $this->render_template($controller_name . '/' . $action, $this->layout); }
function test_underscore() { foreach ($this->inflector_strings() as $lower_case => $camelized) { $this->assertEqual($lower_case, Trails_Inflector::underscore($camelized)); } }
/** * Gets the default template for an action. * * @param String $action Which action was invoked * @return String File name of the template */ public function get_default_template($action) { $class = get_class($this); $controller_name = Trails_Inflector::underscore(substr($class, 0, -10)); return file_exists($this->dispatcher->trails_root . '/views/' . $controller_name . '.php') ? $controller_name : $controller_name . '/' . $action; }
/** * Loads the controller file for a given controller path and return an * instance of that controller. If an error occures, an exception will be * thrown. * * @param string the relative controller path * * @return TrailsController an instance of that controller */ function load_controller($controller) { require_once "{$this->trails_root}/controllers/{$controller}.php"; $class = Trails_Inflector::camelize($controller) . 'Controller'; if (!class_exists($class)) { throw new Trails_UnknownController("Controller missing: '{$class}'"); } return new $class($this); }