示例#1
0
function mvc_model($model_name)
{
    $model_underscore = MvcInflector::underscore($model_name);
    $file_includer = new MvcFileIncluder();
    $file_includer->require_first_app_file('models/' . $model_underscore . '.php');
    if (class_exists($model_name)) {
        return new $model_name();
    }
    MvcError::warning('Unable to load the "' . $model_name . '" model.');
    return null;
}
示例#2
0
文件: mvc_helper.php 项目: g2r/wp-mvc
 public function render_view($path, $view_vars = array())
 {
     extract($view_vars);
     $filepath = $this->file_includer->find_first_app_file_or_core_file('views/' . $path . '.php');
     if (!$filepath) {
         $path = preg_replace('/admin\\/(?!layouts)([\\w_]+)/', 'admin', $path);
         $filepath = $this->file_includer->find_first_app_file_or_core_file('views/' . $path . '.php');
         if (!$filepath) {
             MvcError::warning('View "' . $path . '" not found.');
         }
     }
     require $filepath;
 }
示例#3
0
    public function verify_settings($model)
    {
        if (empty($model->schema)) {
            MvcError::warning('The schema for ' . $model->name . ' is empty. It\'s likely that the plugin generating this table isn\'t working, or you may need to deactivate and activate it.  Please make sure that the table "' . $model->table . '" exists.');
        } else {
            if (!isset($model->schema['post_id'])) {
                MvcError::fatal('To associate posts with ' . $model->name . ', its table needs to have a column named "post_id" of type BIGINT(20).  Please run the following SQL to add and index the column:
<pre>
ALTER TABLE ' . $model->table . ' ADD COLUMN post_id BIGINT(20);
ALTER TABLE ' . $model->table . ' ADD INDEX (post_id);
</pre>');
            }
        }
        if (isset($model->wp_post['post_type']) && $model->wp_post['post_type']) {
            $this->register_post_type($model);
        }
        return true;
    }
示例#4
0
 public function __get($property_name)
 {
     if (!empty($this->__settings['properties'][$property_name])) {
         $property = $this->__settings['properties'][$property_name];
         if ($property['type'] == 'association') {
             $objects = $this->get_associated_objects($property['association']);
             $this->{$property_name} = $objects;
             return $this->{$property_name};
         }
     }
     if ($this->__settings['model']['name'] == 'MvcPost') {
         if (!empty($this->post_type)) {
             if (substr($this->post_type, 0, 4) == 'mvc_') {
                 $model_name = MvcInflector::camelize(substr($this->post_type, 4));
                 $model = MvcModelRegistry::get_model($model_name);
                 $object = $model->find_one(array('post_id' => $this->ID, 'recursive' => 0));
                 $this->{$property_name} = $object;
                 return $this->{$property_name};
             }
         }
     }
     $class = empty($this->__model_name) ? 'MvcModelObject' : $this->__model_name;
     MvcError::warning('Undefined property: ' . $class . '::' . $property_name . '.');
 }
示例#5
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;
 }