示例#1
0
 public function public_url($options = array())
 {
     $routes = self::get_public_routes();
     $controller = $options['controller'];
     $action = empty($options['action']) ? 'index' : $options['action'];
     $matched_route = null;
     if (!empty($options['object']) && is_object($options['object'])) {
         $model_name = MvcInflector::camelize(MvcInflector::singularize($controller));
         $model = MvcModelRegistry::get_model($model_name);
         if (!empty($model) && method_exists($model, 'to_url')) {
             $url = site_url('/');
             $url .= $model->to_url($options['object']);
             return $url;
         }
         if (empty($options['id']) && !empty($options['object']->__id)) {
             $options['id'] = $options['object']->__id;
         }
     }
     foreach ($routes as $route) {
         $route_path = $route[0];
         $route_defaults = $route[1];
         if (!empty($route_defaults['controller']) && $route_defaults['controller'] == $controller) {
             if (!empty($route_defaults['action']) && $route_defaults['action'] == $action) {
                 $matched_route = $route;
             }
         }
     }
     $url = site_url('/');
     if ($matched_route) {
         $path_pattern = $matched_route[0];
         preg_match_all('/{:([\\w]+).*?}/', $path_pattern, $matches, PREG_SET_ORDER);
         $path = $path_pattern;
         foreach ($matches as $match) {
             $pattern = $match[0];
             $option_key = $match[1];
             if (isset($options[$option_key])) {
                 $value = $options[$option_key];
                 $path = preg_replace('/' . preg_quote($pattern) . '/', $value, $path, 1);
             }
         }
         $path = rtrim($path, '/') . '/';
         $url .= $path;
     } else {
         $url .= $options['controller'] . '/';
         if (!empty($options['action']) && $options['action'] != 'show') {
             $url .= $options['action'] . '/';
         }
         if (!empty($options['id'])) {
             $url .= $options['id'] . '/';
         }
     }
     return $url;
 }
示例#2
0
 public function init()
 {
     $this->load_helper('Html');
     $models = MvcModelRegistry::get_models();
     foreach ($models as $model_name => $model) {
         $underscore = MvcInflector::underscore($model_name);
         $tableize = MvcInflector::tableize($model_name);
         // Add dynamicly created methods to HtmlHelper in the form speaker_url($object), speaker_link($object)
         $method = $underscore . '_url';
         $this->html->{$method} = create_function('$object, $options=array()', '
             $defaults = array("controller" => "' . $tableize . '", "action" => "show");
             $options = array_merge($defaults, $options);
             return HtmlHelper::object_url($object, $options);
         ');
         $method = $underscore . '_link';
         $this->html->{$method} = create_function('$object, $options=array()', '
             $defaults = array("controller" => "' . $tableize . '", "action" => "show");
             $options = array_merge($defaults, $options);
             return HtmlHelper::object_link($object, $options);
         ');
     }
     if (is_admin()) {
         $this->load_helper('Form');
     }
     if (empty($this->model)) {
         $helper_name = class_exists('AppHelper') ? 'AppHelper' : 'MvcHelper';
     } else {
         $model = $this->model->name;
         if (class_exists($model . 'Helper')) {
             $helper_name = $model . 'Helper';
         } else {
             if (class_exists('AppHelper')) {
                 $helper_name = 'AppHelper';
             } else {
                 $helper_name = 'MvcHelper';
             }
         }
     }
     $this->helper = new $helper_name();
     if (is_string($this->before)) {
         $this->before = array($this->before);
     }
     if (is_string($this->after)) {
         $this->after = array($this->after);
     }
 }
示例#3
0
 private function get_associated_objects($association)
 {
     $model_name = $association['class'];
     $model = MvcModelRegistry::get_model($model_name);
     switch ($association['type']) {
         case 'belongs_to':
             $associated_object = $model->find_by_id($this->{$association['foreign_key']}, array('recursive' => 0));
             return $associated_object;
         case 'has_many':
             $associated_objects = $model->find(array('selects' => $association['fields'], 'conditions' => array($association['foreign_key'] => $this->__id), 'recursive' => 0));
             return $associated_objects;
         case 'has_and_belongs_to_many':
             $join_alias = 'JoinTable';
             $associated_objects = $model->find(array('selects' => $association['fields'], 'joins' => array('table' => $this->process_table_name($association['join_table']), 'on' => $join_alias . '.' . $association['association_foreign_key'] . ' = ' . $model_name . '.' . $model->primary_key, 'alias' => $join_alias), 'conditions' => array($join_alias . '.' . $association['foreign_key'] => $this->__id), 'recursive' => 0));
             return $associated_objects;
     }
 }
示例#4
0
 public function create($model_name, $options = array())
 {
     $defaults = array('action' => $this->controller->action, 'controller' => MvcInflector::tableize($model_name), 'public' => false);
     $options = array_merge($defaults, $options);
     $this->model_name = $model_name;
     $this->object = MvcObjectRegistry::get_object($model_name);
     $this->model = MvcModelRegistry::get_model($model_name);
     $this->schema = $this->model->schema;
     $object_id = !empty($this->object) && !empty($this->object->__id) ? $this->object->__id : null;
     $router_options = array('controller' => $options['controller'], 'action' => $options['action']);
     if ($object_id) {
         $router_options['id'] = $object_id;
     }
     $enctype = isset($options['enctype']) ? ' enctype="' . $options['enctype'] . '"' : '';
     $url = $options['public'] ? MvcRouter::public_url($router_options) : MvcRouter::admin_url($router_options);
     $html = '<form action="' . $url . '" method="post"' . $enctype . '>';
     if ($object_id) {
         $html .= '<input type="hidden" id="' . $this->input_id('hidden_id') . '" name="' . $this->input_name('id') . '" value="' . $object_id . '" />';
     }
     return $html;
 }
示例#5
0
 protected function load_models()
 {
     $models = array();
     foreach ($this->plugin_app_paths as $plugin_app_path) {
         $model_filenames = $this->file_includer->require_php_files_in_directory($plugin_app_path . 'models/');
         foreach ($model_filenames as $filename) {
             $models[] = MvcInflector::class_name_from_filename($filename);
         }
     }
     $this->model_names = array();
     foreach ($models as $model) {
         $this->model_names[] = $model;
         $model_class = MvcInflector::camelize($model);
         $model_instance = new $model_class();
         MvcModelRegistry::add_model($model, $model_instance);
     }
 }
示例#6
0
 public function public_url($options = array())
 {
     $options = apply_filters('mvc_before_public_url', $options);
     $defaults = array('action' => 'index', 'controller' => null);
     $options = array_merge($defaults, $options);
     $routes = self::get_public_routes();
     $controller = $options['controller'];
     $action = $options['action'];
     $matched_route = null;
     if (!empty($options['object']) && is_object($options['object'])) {
         if (!empty($options['object']->__model_name) && !$controller) {
             $model_name = $options['object']->__model_name;
             $controller = MvcInflector::tableize($model_name);
         } else {
             if ($controller) {
                 $model_name = MvcInflector::camelize(MvcInflector::singularize($controller));
             } else {
                 MvcError::warning('Incomplete arguments for MvcRouter::public_url().');
             }
         }
         $model = MvcModelRegistry::get_model($model_name);
         if (!empty($model) && method_exists($model, 'to_url')) {
             $url = site_url('/');
             $method = new ReflectionMethod(get_class($model), 'to_url');
             $parameter_count = $method->getNumberOfParameters();
             if ($parameter_count == 2) {
                 $url .= $model->to_url($options['object'], $options);
             } else {
                 $url .= $model->to_url($options['object']);
             }
             return $url;
         }
         if (empty($options['id']) && !empty($options['object']->__id)) {
             $options['id'] = $options['object']->__id;
         }
     }
     foreach ($routes as $route) {
         $route_path = $route[0];
         $route_defaults = $route[1];
         if (!empty($route_defaults['controller']) && $route_defaults['controller'] == $controller) {
             if (!empty($route_defaults['action']) && $route_defaults['action'] == $action) {
                 $matched_route = $route;
             }
         }
     }
     $url = site_url('/');
     if ($matched_route) {
         $path_pattern = $matched_route[0];
         preg_match_all('/{:([\\w]+).*?}/', $path_pattern, $matches, PREG_SET_ORDER);
         $path = $path_pattern;
         foreach ($matches as $match) {
             $pattern = $match[0];
             $option_key = $match[1];
             if (isset($options[$option_key])) {
                 $value = $options[$option_key];
                 $path = preg_replace('/' . preg_quote($pattern) . '/', $value, $path, 1);
             } else {
                 if (isset($options['object']) && is_object($options['object'])) {
                     if (isset($options['object']->{$option_key})) {
                         $value = $options['object']->{$option_key};
                         $path = preg_replace('/' . preg_quote($pattern) . '/', $value, $path, 1);
                         $path = rtrim($path, '.*?');
                     }
                 }
             }
         }
         $path = rtrim($path, '/') . '/';
         $url .= $path;
     } else {
         $url .= $controller . '/';
         if (!empty($action) && !in_array($action, array('show', 'index'))) {
             $url .= $action . '/';
         }
         if (!empty($options['id'])) {
             $url .= $options['id'] . '/';
         }
     }
     return $url;
 }
示例#7
0
 private function process_objects($objects, $options = array())
 {
     if (!is_array($objects) && !is_object($objects)) {
         return null;
     }
     $single_object = false;
     if (is_object($objects)) {
         $objects = array($objects);
         $single_object = true;
     }
     $includes = array_key_exists('includes', $options) ? $options['includes'] : $this->includes;
     if (is_string($includes)) {
         $includes = array($includes);
     }
     $recursive = isset($options['recursive']) ? $options['recursive'] - 1 : 2;
     foreach ($objects as $key => $object) {
         if (get_class($object) != 'MvcModelObject') {
             $array = get_object_vars($object);
             $object = $this->new_object($array);
         }
         if (!empty($this->primary_key)) {
             $object->__id = $object->{$this->primary_key};
         }
         if (!empty($includes) && $recursive != 0) {
             foreach ($includes as $include_key => $include) {
                 if (is_string($include)) {
                     $model_name = $include;
                     $association = $this->associations[$model_name];
                 } else {
                     $model_name = $include_key;
                     $association = $include;
                     if (!empty($this->associations[$model_name])) {
                         if (!empty($association['selects'])) {
                             if (is_string($association['selects'])) {
                                 $association['selects'] = array($association['selects']);
                             }
                             $association['fields'] = $association['selects'];
                         }
                         $association = array_merge($this->associations[$model_name], $association);
                     }
                 }
                 if (empty($association['fields'])) {
                     $association['fields'] = array($association['name'] . '.*');
                 }
                 $model = MvcModelRegistry::get_model($model_name);
                 switch ($association['type']) {
                     case 'belongs_to':
                         $associated_object = $model->find_by_id($object->{$association['foreign_key']}, array('recursive' => $recursive));
                         $object->{MvcInflector::underscore($model_name)} = $associated_object;
                         break;
                     case 'has_many':
                         $associated_objects = $model->find(array('selects' => $association['fields'], 'conditions' => array($association['foreign_key'] => $object->__id), 'recursive' => $recursive));
                         $object->{MvcInflector::tableize($model_name)} = $associated_objects;
                         break;
                     case 'has_and_belongs_to_many':
                         $join_alias = 'JoinTable';
                         $associated_objects = $model->find(array('selects' => $association['fields'], 'joins' => array('table' => $this->process_table_name($association['join_table']), 'on' => $join_alias . '.' . $association['association_foreign_key'] . ' = ' . $model_name . '.' . $model->primary_key, 'alias' => $join_alias), 'conditions' => array($join_alias . '.' . $association['foreign_key'] => $object->__id), 'recursive' => $recursive));
                         $object->{MvcInflector::tableize($model_name)} = $associated_objects;
                         break;
                 }
             }
         }
         if (method_exists($this, 'after_find')) {
             $this->after_find($object);
         }
         // Set this after after_find, in case after_find sets this field
         if (!empty($this->display_field)) {
             $object->__name = empty($object->{$this->display_field}) ? null : $object->{$this->display_field};
         }
         $objects[$key] = $object;
     }
     if ($single_object) {
         return $objects[0];
     }
     return $objects;
 }
示例#8
0
 public function add_menu_pages()
 {
     global $_registered_pages;
     $menu_position = 12;
     $menu_position = apply_filters('mvc_menu_position', $menu_position);
     foreach ($this->model_names as $model_name) {
         $model = MvcModelRegistry::get_model($model_name);
         if (!$model->hide_menu) {
             $tableized = MvcInflector::tableize($model_name);
             $pluralized = MvcInflector::pluralize($model_name);
             $titleized = MvcInflector::titleize($model_name);
             $pluralize_titleized = MvcInflector::pluralize_titleize($model_name);
             $controller_name = 'admin_' . $tableized;
             $top_level_handle = 'mvc_' . $tableized;
             $admin_pages = $model->admin_pages;
             $method = $controller_name . '_index';
             $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "' . $controller_name . '", "action" => "index"));');
             add_menu_page($pluralize_titleized, $pluralize_titleized, 'administrator', $top_level_handle, array($this->dispatcher, $method), null, $menu_position);
             foreach ($admin_pages as $key => $admin_page) {
                 $method = $controller_name . '_' . $admin_page['action'];
                 if (!method_exists($this->dispatcher, $method)) {
                     $this->dispatcher->{$method} = create_function('', 'MvcDispatcher::dispatch(array("controller" => "' . $controller_name . '", "action" => "' . $admin_page['action'] . '"));');
                 }
                 $page_handle = $top_level_handle . '-' . $key;
                 if ($admin_page['in_menu']) {
                     add_submenu_page($top_level_handle, $admin_page['label'] . ' &lsaquo; ' . $pluralize_titleized, $admin_page['label'], $admin_page['capability'], $page_handle, array($this->dispatcher, $method));
                 } else {
                     // It looks like there isn't a more native way of creating an admin page without
                     // having it show up in the menu, but if there is, it should be implemented here.
                     // To do: set up capability handling and page title handling for these pages that aren't in the menu
                     $hookname = get_plugin_page_hookname($page_handle, '');
                     if (!empty($hookname)) {
                         add_action($hookname, array($this->dispatcher, $method));
                     }
                     $_registered_pages[$hookname] = true;
                 }
             }
             $menu_position++;
         }
     }
 }
示例#9
0
 public function init()
 {
     $this->version = displayed_documentation_version();
     $this->documentation_node_model = MvcModelRegistry::get_model('DocumentationNode');
 }