function dispatch($options = array()) { $controller_name = $options['controller']; $action = $options['action']; $object_id = empty($options['id']) ? null : $options['id']; $controller_class = MvcInflector::camelize($controller_name) . 'Controller'; $controller = new $controller_class(); $controller->name = $controller_name; $controller->action = $action; $controller->init(); if (!method_exists($controller, $action)) { MvcError::fatal('A method for the action "' . $action . '" doesn\'t exist in "' . $controller_class . '"'); } $params = $_REQUEST; $params = self::escape_params($params); if (is_admin()) { unset($params['page']); } else { if (empty($params['id']) && !empty($object_id)) { $params['id'] = $object_id; } } $controller->params = $params; $controller->set('this', $controller); $controller->{$action}(); $controller->after_action($action); if (!$controller->view_rendered) { $controller->render_view($controller->views_path . $action); } }
private function get_plugin_model_args($args) { if (empty($args[0]) || empty($args[1])) { MvcError::fatal('Please specify a plugin and name for the model.'); } $args[0] = str_replace('_', '-', MvcInflector::underscore($args[0])); return $args; }
public function require_first_app_file($filepath) { foreach ($this->plugin_app_paths as $plugin_app_path) { if ($this->include_file($plugin_app_path . $filepath)) { return true; } } MvcError::fatal('The file "' . $filepath . '" couldn\'t be found in any apps.'); }
protected function get_plugin_name_from_file_path($file_path) { $basename = plugin_basename($file_path); $basename_split = explode('/', $basename); if (!empty($basename_split[0])) { return $basename_split[0]; } MvcError::fatal('A plugin name couldn\'t be derived from the file path "' . $file_path . '"'); }
static function dispatch($options = array()) { $controller_name = $options['controller']; $action = $options['action']; $params = $options; $controller_class = MvcInflector::camelize($controller_name) . 'Controller'; $controller = new $controller_class(); $controller->name = $controller_name; $controller->action = $action; $controller->init(); if (!method_exists($controller, $action)) { MvcError::fatal('A method for the action "' . $action . '" doesn\'t exist in "' . $controller_class . '"'); } $request_params = $_REQUEST; $request_params = self::escape_params($request_params); $params = array_merge($request_params, $params); if (is_admin()) { unset($params['page']); } $controller->params = $params; $controller->set('this', $controller); if (!empty($controller->before)) { foreach ($controller->before as $method) { $controller->{$method}(); } } // If we can access the response from our controller's actions (methods) // we can use them (the actions) as Widgets in the both sides - outside of wp-mvc plugin and inside of it. // The action should return whatever you want, except $this->render_view('view_name'). // Example: Let's say we have 'UserController' and action 'list' and we want somewhere in Wordpress or another // plugin to get all users and to reuse UserController list method, so: // class UserController extends MvcPublicController { // public function list() { // $this->view_rendered = true; // $this->set_objects(); // $response = this->render_to_string('users/list'); // return $response; // } // } // and where we want to reuse it, we can get it in the following way: // $widget = MvcDispatcher::dispatch(array( // 'controller' => 'users', // 'action' => 'list' // )); $response = $controller->{$action}(); if (!empty($controller->after)) { foreach ($controller->after as $method) { $controller->{$method}(); } } $controller->after_action($action); if (!$controller->view_rendered) { $controller->render_view($controller->views_path . $action, $options); } return $response; }
public function save_post($model, $object) { $default_fields = array('post_type' => $this->get_post_type_key($model), 'post_status' => 'publish', 'post_title' => '$' . $model->display_field); if (is_array($object)) { $array = $object; $object = new stdClass(); foreach ($array as $key => $value) { $object->{$key} = $value; } } $post_id = empty($object->post_id) ? null : $object->post_id; if ($post_id) { // Check to make sure that the post exists $post = get_post($post_id); if (!$post) { $post_id = null; } } $fields = isset($model->wp_post['post_type']['fields']) ? $model->wp_post['post_type']['fields'] : null; if (!is_array($fields)) { $fields = array(); } $fields = array_merge($default_fields, $fields); $post_data = array(); foreach ($fields as $key => $value) { if (substr($value, 0, 1) == '$') { $attribute = substr($value, 1); if (!isset($object->{$attribute})) { MvcError::fatal('The attribute "' . $attribute . '" was not present when trying to save the post for a ' . $model->name . ' object.'); } $post_data[$key] = $object->{$attribute}; } else { if (substr($value, -2) == '()') { $method = substr($value, 0, strlen($value) - 2); if (!method_exists($model, $method)) { MvcError::fatal('The method "' . $method . '" was not present when trying to save the post for a ' . $model->name . ' object.'); } $post_data[$key] = $model->{$method}($object); } else { $post_data[$key] = $value; } } } if ($post_id) { $post_data['ID'] = $post_id; wp_update_post($post_data); } else { $post_id = wp_insert_post($post_data, true); if (is_wp_error($post_id)) { MvcError::fatal('The post for ' . $model->name . ' could not be saved (' . $post_id->get_error_message() . ').'); } else { $model->update($object->{$model->primary_key}, array('post_id' => $post_id), array('bypass_save_post' => true)); } } }
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; }
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; }
public function create($template, $target_path, $vars = array()) { if (!empty($vars)) { $this->set($vars); } $template_path = $this->template_directory . $template . '.php'; if (file_exists($template_path)) { extract($this->template_vars); ob_start(); ob_implicit_flush(0); include $template_path; $content = ob_get_clean(); $this->create_file($target_path, $content); } else { MvcError::fatal('A template at "' . $template_path . '" could not be found.'); } }
public function input($field_name, $options = array()) { if (!empty($this->schema[$field_name])) { $schema = $this->schema[$field_name]; $type = $this->get_type_from_sql_schema($schema); $defaults = array('type' => $type, 'label' => MvcInflector::titleize($schema['field']), 'value' => empty($this->object->{$field_name}) ? '' : $this->object->{$field_name}); if ($type == 'checkbox') { unset($defaults['value']); } $options = array_merge($defaults, $options); $options['type'] = empty($options['type']) ? 'text' : $options['type']; $html = $this->{$options['type'] . '_input'}($field_name, $options); return $html; } else { MvcError::fatal('Field "' . $field_name . '" not found for use in a form input.'); return ''; } }
private function dispatch($args) { MvcConfiguration::set('ExecutionContext', 'shell'); if (empty($args[1])) { MvcError::fatal('Please provide the name of the shell as the first argument.'); } $shell_name = $args[1]; $shell_name .= '_shell'; $shell_class_name = MvcInflector::camelize($shell_name); $this->file_includer->require_first_app_file_or_core_file('shells/' . $shell_name . '.php'); $args = array_slice($args, 2); if (empty($args[0])) { $args = array('main'); } $shell = new $shell_class_name($args); $method = $args[0]; $args = array_slice($args, 1); $shell->{$method}($args); }
public function validate($field, $value, $rule) { if (is_string($rule)) { if (method_exists($this, $rule)) { $result = $this->{$rule}($value); if ($result === true) { return true; } $message = $result; $message = $this->process_message($message, $field); $error = new MvcDataValidationError($field, $message); return $error; } else { MvcError::fatal('The validation rule "' . $rule . '" wasn\'t found.'); } } if (is_array($rule)) { if (!empty($rule['pattern']) || !empty($rule['rule'])) { return $this->validate_using_array_rule($field, $value, $rule); } } MvcError::fatal('The validation rule "' . print_r($rule, true) . '" wasn\'t defined correctly.'); }
function dispatch($options = array()) { $controller_name = $options['controller']; $action = $options['action']; $params = $options; $controller_class = MvcInflector::camelize($controller_name) . 'Controller'; $controller = new $controller_class(); $controller->name = $controller_name; $controller->action = $action; $controller->init(); if (!method_exists($controller, $action)) { MvcError::fatal('A method for the action "' . $action . '" doesn\'t exist in "' . $controller_class . '"'); } $request_params = $_REQUEST; $request_params = self::escape_params($request_params); $params = array_merge($request_params, $params); if (is_admin()) { unset($params['page']); } $controller->params = $params; $controller->set('this', $controller); if (!empty($controller->before)) { foreach ($controller->before as $method) { $controller->{$method}(); } } $controller->{$action}(); if (!empty($controller->after)) { foreach ($controller->after as $method) { $controller->{$method}(); } } $controller->after_action($action); if (!$controller->view_rendered) { $controller->render_view($controller->views_path . $action, $options); } }
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 . '.'); }
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; }
public function __call($method_name, $arguments) { if (substr($method_name, 0, 14) == 'display_field_') { $setting_key = substr($method_name, 14); $this->display_field($setting_key); return true; } if (substr($method_name, 0, 15) == 'validate_field_') { $setting_key = substr($method_name, 15); $this->validate_field($setting_key, $arguments[0]); return true; } MvcError::fatal('Undefined method: ' . get_class($this) . '::' . $method_name . '.'); }
protected function init_default_columns() { if (empty($this->default_columns)) { MvcError::fatal('No columns defined for this view. Please define them in the controller, like this: <pre> class ' . MvcInflector::camelize($this->name) . 'Controller extends MvcAdminController { var $default_columns = array(\'id\', \'name\'); } </pre>'); } $admin_columns = array(); foreach ($this->default_columns as $key => $value) { if (is_array($value)) { if (!isset($value['label'])) { $value['label'] = MvcInflector::titleize($key); } } else { if (is_integer($key)) { $key = $value; if ($value == 'id') { $value = array('label' => 'ID'); } else { $value = array('label' => MvcInflector::titleize($value)); } } else { $value = array('label' => $value); } } $value['key'] = $key; $admin_columns[$key] = $value; } $this->default_columns = $admin_columns; }
public function __call($method, $args) { if (substr($method, 0, 8) == 'find_by_') { $attribute = substr($method, 8); if (isset($this->schema[$attribute])) { $object = $this->find(array('conditions' => array($attribute => $args[0]))); return $object; } } if (substr($method, 0, 12) == 'find_one_by_') { $attribute = substr($method, 12); if (isset($this->schema[$attribute])) { $object = $this->find_one(array('conditions' => array($attribute => $args[0]))); return $object; } } MvcError::fatal('Undefined method: ' . $class . '::' . $method . '.'); }