Esempio n. 1
0
 /**
  * Factory for fetching the ViewModel
  *
  * @param   string  ViewModel classname without View_ prefix or full classname
  * @param   string  Method to execute
  * @return  ViewModel
  */
 public static function forge($view, $method = 'view', $auto_filter = null)
 {
     // strip any extensions from the view name to determine the viewmodel to load
     $viewmodel = \Inflector::words_to_upper(str_replace(array('/', DS), '_', strpos($view, '.') === false ? $view : substr($view, 0, -strlen(strrchr($view, '.')))));
     // determine the viewmodel namespace from the current request context
     $namespace = \Request::active() ? ucfirst(\Request::active()->module) : '';
     // list of possible viewmodel classnames, start with the namespaced one
     $classes = array($namespace . '\\View_' . $viewmodel);
     // add the global version if needed
     empty($namespace) or $classes[] = 'View_' . $viewmodel;
     /**
      * Add non View_ prefixed classnames to the list, for BC reasons
      *
      * @deprecated 1.6
      */
     $classes[] = $namespace . '\\' . $viewmodel;
     // and add the global version of that if needed
     empty($namespace) or $classes[] = $viewmodel;
     // check if we can find one
     foreach ($classes as $class) {
         if (class_exists($class)) {
             return new $class($method, $auto_filter, $view);
         }
     }
     throw new \OutOfBoundsException('ViewModel "' . reset($classes) . '" could not be found.');
 }
Esempio n. 2
0
 public function action_index($related_model_key, $selected_id)
 {
     $post_input = Input::post();
     $this->request_data = $post_input['request_data'];
     $this->base_form_name = $post_input['form_name'];
     $this->base_model_name = 'Model_' . \Inflector::words_to_upper(ltrim($this->base_form_name, '_'));
     $this->related_model_key = $related_model_key;
     $this->related_model_name = 'Model_' . \Inflector::words_to_upper(str_replace('_id', '', ltrim($this->related_model_key, '_')));
     // Process conditions for what options to get
     $find_conditions_array = $relations_array = $order_by_array = [];
     if (array_key_exists('conditions', $this->request_data)) {
         if (array_key_exists('where', $this->request_data['conditions'])) {
             foreach ($this->request_data['conditions']['where'] as $key => $value) {
                 if (strpos('_value', $value) !== false) {
                     $value = $selected_id;
                 }
                 $find_conditions_array[$key] = $value;
             }
         }
     }
     // Get related models with above conditions
     $model_object = new $this->related_model_name();
     $this->related_model = $model_object->find('all', ['where' => $find_conditions_array, 'related' => $relations_array, 'order_by' => $order_by_array]);
     // Work out here what colour it is and if it's selected
     // @todo code the conditional stuff!
     if (array_key_exists('selected', $this->request_data)) {
         $selected_array = $this->request_data['selected'];
     } else {
         $selected_array = [];
     }
     $colour = '';
     // Process the returned related models into options using the template (if specified),
     // and setting highlighting/selected attributes where appropriate.
     foreach ($this->related_model as $related_model) {
         in_array($related_model->id, $selected_array) ? $selected = 'selected="selected"' : ($selected = '');
         // Prepare the option's text from the specified template
         $text_object = new \EBS\Form_Option_Text($related_model, $this->request_data['template']);
         $text = $text_object->get_text();
         // Generate the option html and concatenate it to the options string
         $option_html = "<option value=\"{$related_model->id}\"{$selected}{$colour}>{$text}</option>";
         $this->options_html .= $option_html;
     }
     // Prepare the updated view for the response object.
     $updated_view = new \EBS\Response_View();
     $updated_view->html = $this->options_html;
     $updated_view->context = $post_input['response_target'];
     $this->response->updated_views[] = $updated_view;
     // Encode the response object as JSON and send it back to the UI!
     return Response::forge(json_encode($this->response));
 }
Esempio n. 3
0
 /**
  * Generates a new request.  The request is then set to be the active
  * request.  If this is the first request, then save that as the main
  * request for the app.
  *
  * Usage:
  *
  *     Request::forge('hello/world');
  *
  * @param   string   The URI of the request
  * @param   mixed    Internal: whether to use the routes; external: driver type or array with settings (driver key must be set)
  * @param   string   request method
  * @return  Request  The new request object
  */
 public static function forge($uri = null, $options = true, $method = null)
 {
     is_bool($options) and $options = array('route' => $options);
     is_string($options) and $options = array('driver' => $options);
     if (!empty($options['driver'])) {
         $class = \Inflector::words_to_upper('Request_' . $options['driver']);
         return $class::forge($uri, $options, $method);
     }
     $request = new static($uri, isset($options['route']) ? $options['route'] : true, $method);
     if (static::$active) {
         $request->parent = static::$active;
         static::$active->children[] = $request;
     }
     return $request;
 }
Esempio n. 4
0
 private function deal_relation($action)
 {
     foreach (array_keys($this->id_keys) as $key) {
         // If the key doesn't contain the name of the parent model we're creating, it's the relations name
         if (strpos($key, $this->item_name) === false) {
             $relation_name = str_replace('_id', '', $key);
             $relation_model_name = 'Model_' . \Inflector::words_to_upper($relation_name);
             $relation_model = new $relation_model_name();
             // E.g. note->person[] = person
             $this->models_and_actions[0][0]->{$relation_name}[] = $relation_model::find($this->id_keys[$key]);
         }
     }
     if ($action !== 'delete' && $relation_model !== null) {
     }
 }
Esempio n. 5
0
 /**
  * Generates a new request.  The request is then set to be the active
  * request.  If this is the first request, then save that as the main
  * request for the app.
  *
  * Usage:
  *
  *     Request::forge('hello/world');
  *
  * @param   string   The URI of the request
  * @param   mixed    Internal: whether to use the routes; external: driver type or array with settings (driver key must be set)
  * @param   string   request method
  * @return  Request  The new request object
  */
 public static function forge($uri = null, $options = true, $method = null)
 {
     is_bool($options) and $options = array('route' => $options);
     is_string($options) and $options = array('driver' => $options);
     if (!empty($options['driver'])) {
         $class = \Inflector::words_to_upper('Request_' . $options['driver']);
         return $class::forge($uri, $options, $method);
     }
     $request = new static($uri, isset($options['route']) ? $options['route'] : true, $method);
     if (static::$active) {
         $request->parent = static::$active;
         static::$active->children[] = $request;
     }
     // fire any request created events
     \Event::instance()->has_events('request_created') and \Event::instance()->trigger('request_created', '', 'none');
     return $request;
 }
Esempio n. 6
0
 /**
  * Factory for fetching the Presenter
  *
  * @param   string  Presenter classname without View_ prefix or full classname
  * @param   string  Method to execute
  * @param   bool    Auto filter the view data
  * @param   string  View to associate with this persenter
  * @return  Presenter
  */
 public static function forge($presenter, $method = 'view', $auto_filter = null, $view = null)
 {
     // determine the presenter namespace from the current request context
     $namespace = \Request::active() ? ucfirst(\Request::active()->module) : '';
     // create the list of possible class prefixes
     $prefixes = array(static::$ns_prefix, $namespace . '\\');
     /**
      * Add non prefixed classnames to the list as well, for BC reasons
      *
      * @deprecated 1.6
      */
     if (!empty($namespace)) {
         array_unshift($prefixes, $namespace . '\\' . static::$ns_prefix);
         $prefixes[] = '';
     }
     // loading from a specific namespace?
     if (strpos($presenter, '::') !== false) {
         $split = explode('::', $presenter, 2);
         if (isset($split[1])) {
             array_unshift($prefixes, ucfirst($split[0]) . '\\' . static::$ns_prefix);
             $presenter = $split[1];
         }
     }
     // if no custom view is given, make it equal to the presenter name
     is_null($view) and $view = $presenter;
     // strip any extensions from the view name to determine the presenter to load
     $presenter = \Inflector::words_to_upper(str_replace(array('/', DS), '_', strpos($presenter, '.') === false ? $presenter : substr($presenter, 0, -strlen(strrchr($presenter, '.')))));
     // create the list of possible presenter classnames, start with the namespaced one
     $classes = array();
     foreach ($prefixes as $prefix) {
         $classes[] = $prefix . $presenter;
     }
     // check if we can find one
     foreach ($classes as $class) {
         if (class_exists($class)) {
             return new $class($method, $auto_filter, $view);
         }
     }
     throw new \OutOfBoundsException('Presenter "' . reset($classes) . '" could not be found.');
 }
Esempio n. 7
0
 /**
  * Used in the 'behind the scenes' logic during a frontend request, this checks whether
  * there is a controller associated with a particular template
  * 
  * @param string $template Path to the template, relative to the root of the views directory
  * @return boolean
  */
 public static function hasController($template)
 {
     static::$path = strpos($template, '.') === false ? $template : substr($template, 0, -strlen(strrchr($template, '.')));
     // determine the viewmodel namespace and classname
     if (empty(static::$module)) {
         static::$module = \Request::active() ? ucfirst(\Request::active()->module) : '';
     }
     // Strip the first part of the path off for module templates
     if (!empty(static::$module) && strpos(static::$path, static::$module) === 0) {
         static::$path = str_replace(static::$module . '/', '', static::$path);
     }
     $controller_class = ucfirst(static::$module) . '\\Controller_' . \Inflector::words_to_upper(ucfirst(str_replace(array('/', DS), '_', static::$path)));
     return class_exists($controller_class);
 }
Esempio n. 8
0
 protected static function parse_segments($segments, $namespace = '', $module = false)
 {
     $temp_segments = $segments;
     foreach (array_reverse($segments, true) as $key => $segment) {
         $class = $namespace . 'Controller_' . \Inflector::words_to_upper(implode('_', $temp_segments));
         array_pop($temp_segments);
         if (class_exists($class)) {
             return array('controller' => $class, 'action' => isset($segments[$key + 1]) ? $segments[$key + 1] : null, 'method_params' => array_slice($segments, $key + 2));
         }
     }
     // Fall back for default module controllers
     if ($module) {
         $class = $namespace . 'Controller_' . ucfirst($module);
         if (class_exists($class)) {
             return array('controller' => $class, 'action' => isset($segments[0]) ? $segments[0] : null, 'method_params' => array_slice($segments, 1));
         }
     }
     return false;
 }
Esempio n. 9
0
 /**
  * Find the controller that matches the route requested
  *
  * @param	Route  $match  the given Route object
  * @return	mixed  the match array or false
  */
 protected static function find_controller($match)
 {
     // First port of call: request for a module?
     if (\Fuel::module_exists($match->segments[0])) {
         // make the module known to the autoloader
         \Fuel::add_module($match->segments[0]);
         $segments = $match->segments;
         // first check if the controller is in a directory.
         $match->module = array_shift($segments);
         $match->directory = count($segments) ? array_shift($segments) : null;
         $match->controller = count($segments) ? array_shift($segments) : $match->module;
         // does the module controller exist?
         if (class_exists(Inflector::words_to_upper($match->module . '\\Controller_' . $match->directory . '_' . $match->controller))) {
             $match->action = count($segments) ? array_shift($segments) : null;
             $match->method_params = $segments;
             return $match;
         }
         $segments = $match->segments;
         // then check if it's a module controller
         $match->module = array_shift($segments);
         $match->directory = null;
         $match->controller = count($segments) ? array_shift($segments) : $match->module;
         // does the module controller exist?
         if (class_exists(Inflector::words_to_upper($match->module . '\\Controller_' . $match->controller))) {
             $match->action = count($segments) ? array_shift($segments) : null;
             $match->method_params = $segments;
             return $match;
         }
         $segments = $match->segments;
         // do we have a module controller with the same name as the module?
         if ($match->controller != $match->module) {
             array_shift($segments);
             $match->controller = $match->module;
             if (class_exists(Inflector::words_to_upper($match->module . '\\Controller_' . $match->controller))) {
                 $match->action = count($segments) ? array_shift($segments) : null;
                 $match->method_params = $segments;
                 return $match;
             }
         }
     }
     $segments = $match->segments;
     // It's not a module, first check if the controller is in a directory.
     $match->directory = array_shift($segments);
     $match->controller = count($segments) ? array_shift($segments) : $match->directory;
     if (class_exists(\Inflector::words_to_upper('Controller_' . $match->directory . '_' . $match->controller))) {
         $match->action = count($segments) ? array_shift($segments) : null;
         $match->method_params = $segments;
         return $match;
     }
     $segments = $match->segments;
     // It's not in a directory, so check for app controllers
     $match->directory = null;
     $match->controller = count($segments) ? array_shift($segments) : $match->directory;
     // We first want to check if the controller is in a directory.
     if (class_exists(\Inflector::words_to_upper('Controller_' . $match->controller))) {
         $match->action = count($segments) ? array_shift($segments) : null;
         $match->method_params = $segments;
         return $match;
     }
     // none of the above. I give up. We've found ziltch...
     $match->action = null;
     $match->controller = null;
     return $match;
 }
Esempio n. 10
0
 protected static function parse_segments($segments, $namespace = '', $module = false)
 {
     $temp_segments = $segments;
     $prefix = static::get_prefix();
     foreach (array_reverse($segments, true) as $key => $segment) {
         // determine which classes to check. First, all underscores, or all namespaced
         $classes = array($namespace . $prefix . \Inflector::words_to_upper(implode(substr($prefix, -1, 1), $temp_segments), substr($prefix, -1, 1)));
         // if we're namespacing, check a hybrid version too
         $classes[] = $namespace . $prefix . \Inflector::words_to_upper(implode('_', $temp_segments));
         array_pop($temp_segments);
         foreach ($classes as $class) {
             if (static::check_class($class)) {
                 return array('controller' => $class, 'action' => isset($segments[$key + 1]) ? $segments[$key + 1] : null, 'method_params' => array_slice($segments, $key + 2));
             }
         }
     }
     // Fall back for default module controllers
     if ($module) {
         $class = $namespace . $prefix . ucfirst($module);
         if (static::check_class($class)) {
             return array('controller' => $class, 'action' => isset($segments[0]) ? $segments[0] : null, 'method_params' => array_slice($segments, 1));
         }
     }
     return false;
 }
Esempio n. 11
0
 /**
  * Translates a driver name to a class name
  * 
  * @param string $name Name of the driver (eg, 'facebook')
  * @return string 'Ethanol\Auth_Driver_Facebook'
  */
 public function translate_driver_name($name)
 {
     return 'Ethanol\\Auth_Driver_' . \Inflector::words_to_upper($name);
 }
Esempio n. 12
0
 /**
  * Generates a new request.  The request is then set to be the active
  * request.  If this is the first request, then save that as the main
  * request for the app.
  *
  * Usage:
  *
  *     Request::forge('hello/world');
  *
  * @param   string   The URI of the request
  * @param   mixed    Internal: whether to use the routes; external: driver type or array with settings (driver key must be set)
  * @return  Request  The new request object
  */
 public static function forge($uri = null, $options = true)
 {
     logger(\Fuel::L_INFO, 'Creating a new Request with URI = "' . $uri . '"', __METHOD__);
     is_bool($options) and $options = array('route' => $options);
     is_string($options) and $options = array('driver' => $options);
     if (!empty($options['driver'])) {
         $class = \Inflector::words_to_upper('Request_' . $options['driver']);
         return $class::forge($uri, $options);
     }
     $request = new static($uri, isset($options['route']) ? $options['route'] : true);
     if (static::$active) {
         $request->parent = static::$active;
         static::$active->children[] = $request;
     }
     return $request;
 }