Exemple #1
0
 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);
     }
 }
Exemple #2
0
 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;
 }
Exemple #3
0
 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 . '"');
 }
Exemple #4
0
 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.');
 }
Exemple #5
0
 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;
 }
Exemple #6
0
 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));
         }
     }
 }
Exemple #7
0
 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.');
     }
 }
Exemple #8
0
 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);
 }
Exemple #10
0
 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.');
 }
Exemple #11
0
 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);
     }
 }
Exemple #12
0
 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;
 }
Exemple #13
0
 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 . '.');
 }
Exemple #14
0
 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 . '.');
 }