public function action_index() { $model = $this->request->param('model'); $action = 'action_' . ($this->request->param('model_action') ? $this->request->param('model_action') : 'index'); $api = 'Api_' . ORM::get_model_name($model); $model_api = new $api(); $this->response->json($model_api->{$action}()); }
/** * Return formated of query request, with childs * * @param string $table_name * @return array */ public function all_as_array($table_name = NULL, $filter = NULL, $callback = NULL, $deep = 0) { if ($deep > self::orm_deep()) { return 'Too Deep :('; } $deep++; if ($filter) { $filter($this); } $has_many = $this->has_many(); $belongs_to = $this->belongs_to(); // accept loaded item $models = !$this->_loaded ? $this->find_all() : array($this); $results = array(); foreach ($models as $item) { $result = $item->as_array(); foreach ($result as $key => $value) { if (is_string($key) and is_string($value) and preg_match('/^(image|thumb|file_|upload_)/', $key)) { $result[$key] = $item->get_url($key); } if (!isset($result['slug']) and is_string($key) and is_string($value) and preg_match('/^(name|title)/', $key)) { $result['slug'] = Huia_URL::slug($value . ' ' . $result['id']); } } if ($callback) { $table = $table_name ? $table_name : $this->table_name(); $result = $callback($table, $result); // ignore row if ($result === NULL) { continue; } } foreach ($belongs_to as $key => $values) { // schmittless if (ORM::get_model_name($key) === ORM::get_model_name($table_name)) { continue; } $model_name = Arr::get($values, 'model'); $foreign_key = Arr::get($values, 'foreign_key'); $model = ORM::factory($model_name)->where('id', '=', $item->{$foreign_key}); $result[$key] = $model->all_as_array($this->table_name(), $filter, $callback, $deep); } foreach ($has_many as $key => $values) { // schmittless if ($key === $table_name) { continue; } $result[$key] = $item->{$key}->all_as_array($this->table_name(), $filter, $callback, $deep); } $results[] = $result; } return $results; }
public function before() { if (!Can::show()) { // Record only when invalid session, prevent ximite. if (!Auth::instance()->logged_in()) { Session::instance()->set('manager_login_reference', URL::current()); } return HTTP::redirect('manager/login'); } $success = Session::instance()->get_once('success'); View::set_global('success', $success); if (!$this->model_name and class_exists('Model_' . $this->request->controller())) { $this->model_name = $this->request->controller(); } if ($this->title === NULL) { $this->title = $this->model_name; } if (!$this->parents) { $this->parents = $this->request->param('parents'); $this->parents = explode('/', $this->parents); $parents = array(); if (count($this->parents) > 1) { foreach ($this->parents as $index => $value) { if ($index % 2) { continue; } $parents[] = array('model' => $value, 'table' => Inflector::plural($value), 'model_id' => $this->parents[$index + 1]); } } $this->parents = array_reverse($parents); } if (!$this->parent) { $this->parent = $this->request->param('parent'); } if (!$this->parent_id) { $this->parent_id = $this->request->param('parent_id'); } $boolean_fields = array(); $image_fields = array(); $upload_fields = array(); $text_fields = array(); $date_fields = array(); if ($this->model_name) { $this->model = ORM::factory(ORM::get_model_name($this->model_name), $this->request->param('id')); if ($this->parents) { $current_parent_table = strtolower($this->model_name); foreach ($this->parents as $index => $values) { $this->model->join(Arr::get($values, 'table')); $this->model->on(Arr::get($values, 'table') . '.id', '=', $current_parent_table . '.' . Arr::get($values, 'model') . '_id'); $this->model->where(Arr::get($values, 'table') . '.id', '=', Arr::get($values, 'model_id')); $current_parent_table = Arr::get($values, 'table'); } } if ($this->parent_id) { $this->foreign_key = strtolower($this->parent) . '_id'; $this->parent_model = ORM::factory(ORM::get_model_name($this->parent), $this->parent_id); $model_has_many = Inflector::plural(strtolower($this->model_name)); if (in_array($this->foreign_key, array_keys($this->model->as_array()))) { $this->model->where($this->foreign_key, '=', $this->parent_id); } else { if (in_array($model_has_many, array_keys($this->parent_model->as_array()))) { $this->model = $this->parent_model->{$model_has_many}; } } } $text_field_formats = Kohana::$config->load('huia/model.models'); if ($text_field_formats) { $this->text_field_formats = Arr::merge($this->text_field_formats, $text_field_formats); } $this->model->reload_columns(TRUE); foreach ($this->model->table_columns() as $column => $values) { if (Arr::get($values, 'data_type') === 'text') { $format = Arr::path($this->text_field_formats, $this->model->object_name() . '.' . $column, 'ckeditor'); $text_fields[$column] = array('format' => $format); } else { if (Arr::get($values, 'data_type') === 'tinyint' and Arr::get($values, 'display') == 1) { $boolean_fields[] = $column; } else { if (preg_match('/^(image|thumb)/', $column)) { $image_fields[] = $column; } else { if (preg_match('/^(file|upload)/', $column)) { $upload_fields[] = $column; } else { if (Arr::get($values, 'data_type') === 'date') { $date_fields[] = $column; } } } } } } View::set_global('date_fields', $date_fields); View::set_global('text_fields', $text_fields); $this->belongs_to = Arr::merge($this->belongs_to, $this->model->belongs_to()); $this->has_many = Arr::merge($this->has_many, $this->model->has_many()); $model_labels = $this->model->labels(); foreach ($model_labels as $key => $value) { // ignore through secundary $has_many_key = Arr::get($this->has_many, $key); if ($has_many_key) { $through = Arr::get($has_many_key, 'through'); $is_secundary = preg_match('/^' . $key . '_/', $through); $same_table = $through === $key . '_' . $key; if ($is_secundary and !$same_table) { unset($model_labels[$key]); } } // ignore composite if (preg_match('/^id_/', $key)) { unset($model_labels[$key]); } } $this->labels = Arr::merge($this->labels, $model_labels); } // auto upload if ($this->upload_fields === NULL) { $this->upload_fields = $upload_fields; } // auto booleans if ($this->boolean_fields === NULL) { $this->boolean_fields = $boolean_fields; } // auto images if ($this->image_fields === NULL) { $this->image_fields = $image_fields; } foreach ($this->boolean_fields as $field) { if (!isset($this->boolean_fields_labels[$field])) { $this->boolean_fields_labels[$field] = $this->boolean_fields_labels['default']; } } $model_classes = ORM_Autogen::get_models(); View::set_global('model_classes', $model_classes); parent::before(); // autogen controllers if (Kohana::$environment === Kohana::DEVELOPMENT) { self::generate_controllers($model_classes); } }
public static function generate_model($model, $force = FALSE) { $class_name = 'Model_' . $model; $file = str_replace('_', DIRECTORY_SEPARATOR, $model); $base_dir = 'Base'; $model_dir = APPPATH . 'classes' . DIRECTORY_SEPARATOR . 'Model' . DIRECTORY_SEPARATOR; $model_base = $model_dir . $base_dir . DIRECTORY_SEPARATOR; $file_name = Kohana::find_file('classes/model', $file); $file_base_name = Kohana::find_file('classes/model/base', $file); $table_name = strtolower(Inflector::plural($model)); $table_id = strtolower($model) . '_id'; $rules = array(); $labels = array(); $has_many = array(); $belongs_to = array(); $columns = Database::instance()->list_columns($table_name); foreach ($columns as $field) { $name = Arr::get($field, 'column_name'); $key = Arr::get($field, 'key'); $type = Arr::get($field, 'type'); $low_name = str_replace('_id', '', $name); $maximum_length = Arr::get($field, 'character_maximum_length', Arr::get($field, 'display')); $title = ucfirst($name); // ignore id and _at$ if ($name === 'id' or preg_match('@_at$@', $name)) { continue; } $field_rules = array(); // unique if ($key === 'UNI') { $field_rules[] = "array(array(\$this, 'unique'), array(':field', ':value')),"; } // unique if ($type === 'int') { $field_rules[] = "array('numeric'),"; } // not null if (Arr::get($field, 'is_nullable') === FALSE) { $field_rules[] = "array('not_empty'),"; } // max length if ($maximum_length) { $field_rules[] = "array('max_length', array(':value', {$maximum_length})),"; } // cpf if ($name === 'cpf') { $field_rules[] = "array('cpf', array(':value')),"; } // email if (preg_match('@email@', $name)) { $field_rules[] = "array('email', array(':value')),"; } // rules if (!empty($field_rules)) { $rules[$name] = $field_rules; } // labels if (!preg_match('@_id$@', $name)) { $labels[$name] = $title; } // belongs to if (preg_match('@_id$@', $name)) { $model_name = ORM::get_model_name($low_name); $belongs_to[$low_name] = "array(" . "'model' => '" . $model_name . "'" . "),"; $labels[$low_name] = ucfirst($low_name); } } foreach (Database::instance()->list_tables() as $name) { $_columns = array_keys(Database::instance()->list_columns($name)); // has many through if (preg_match('/(^' . $table_name . '_(.*)|(.*)_' . $table_name . '$)/', $name, $matchs)) { $related = $matchs[count($matchs) - 1]; $has_many[$related] = "array(" . "'model' => '" . ORM::get_model_name($related) . "', " . "'through' => '" . $name . "'" . "),"; $labels[$related] = ucfirst($related); } // has many if (in_array('id', $_columns) and in_array($table_id, $_columns)) { $related = Inflector::singular($name); $field_name = Inflector::singular($table_name); $model_name = ORM::get_model_name($related); $has_many[str_replace($field_name . '_', '', $name)] = "array(" . "'model' => '" . $model_name . "'" . "),"; } } $class_extends = 'Model_App'; $columns = self::format_columns($columns); $full_class_name = 'Model_' . $base_dir . '_' . $model; $view = View::factory('huia/orm/base'); $view->set('class_name', $full_class_name); $view->set('class_extends', $class_extends); $view->set('table_name', $class_name); $view->set('rules', $rules); $view->set('labels', $labels); $view->set('has_many', $has_many); $view->set('belongs_to', $belongs_to); $view->set('columns', $columns); $render_view = $view->render(); $hash_current = $file_base_name ? preg_replace("/[^A-Za-z0-9]/", "", @file_get_contents($file_base_name)) : NULL; $hash_new = preg_replace("/[^A-Za-z0-9]/", "", $render_view); if ($hash_current !== $hash_new) { $file_base_name = $model_base . $file . EXT; create_dir(dirname($file_base_name)); file_put_contents($file_base_name, $view->render()); } // Create if dont exists if (!$file_name) { $view = View::factory('huia/orm'); $view->set('class_name', $class_name); $view->set('class_extends', 'Model_' . $base_dir . '_' . $model); $file_name = $model_dir . $file . EXT; create_dir(dirname($file_name)); file_put_contents($file_name, $view->render()); } }
public function get() { $_caching = $this->request->post('_caching'); $caching = Kohana::$caching and !Session::instance()->get('auth_user') and $_caching; $key = 'api.' . $this->model_name . '.' . Request::current()->uri() . '.' . http_build_query(Request::current()->query()) . '.' . http_build_query(Request::current()->post()); if ($caching) { if ($result = Cache::instance()->get($key)) { $this->response->headers('From-Cache', '1'); return $this->json($result); } } if ($this->config('permissions', 'query')) { $queries = $this->request->post('_query'); $queries = $queries ? $queries : array(); } else { $queries = array(); } if ($this->request->param('id')) { $queries[] = array('where', 'id', '=', $this->request->param('id')); $queries[] = array('limit', 1); } if ($filter_queries = $this->config('filters', 'query')) { $queries = Arr::merge($queries, $filter_queries); } $this->model = $this->query($this->model, $queries, 100); $count = clone $this->model; if ($this->request->param('id') and !$count->count_all()) { throw HTTP_Exception::factory(404, 'Not found!'); } if (!$this->request->param('id') and empty($queries) and !$this->config('permissions', 'list')) { throw HTTP_Exception::factory(403, __('Cant list this object.')); } $read = $this->config('permissions', 'read'); if (!$read and $role_read = $this->config('permissions', 'role_read')) { $user = Auth::instance()->get_user(); $roles = ORM::factory('Role')->where('name', 'IN', $role_read)->find_all()->as_array('id', NULL); if (!$user or !$user->has('roles', $roles)) { throw HTTP_Exception::factory(403, 'This object require role_read permission!'); } $read = TRUE; } else { if (!$read and $this->has_user()) { $self_read = $this->config('permissions', 'self_read'); $user = Auth::instance()->get_user(); if (!$user and $self_read) { throw HTTP_Exception::factory(403, 'This object require self_read permission!'); } if ($self_read) { $this->model->where('user_id', '=', $user ? $user->id : NULL); } $read = TRUE; } } if (!$read) { throw HTTP_Exception::factory(403, 'This object require read permission!'); } if ($this->request->post('_count_all')) { $result = $this->model->count_all(); } else { $result = $this->model->all_as_array(NULL, function (&$model) { $queries = $this->config('filters', 'query', $model->table_name()); $model = $this->query($model, $queries); }, function ($table_name, $object) { $api = 'Api_' . ORM::get_model_name($table_name); if (class_exists($api) and method_exists($api, 'get')) { $model_api = new $api(); $object = $model_api->get($object); } return $object; }); $result = $this->filter_expected($result); // filter all $api = 'Api_' . ORM::get_model_name($this->model->table_name()); if (class_exists($api) and method_exists($api, 'get_all')) { $model_api = new $api(); $result = $model_api->get_all($result); } } if ($caching) { Cache::instance()->set($key, $result, $_caching); } return $this->json($result); }
?> , <?php } ?> <?php } elseif (Arr::get($belongs_to, $name)) { ?> <?php $column_name = NULL; $is_model = method_exists($row->{$name}, 'list_columns'); $belongs_to_model = $row->{$name}; if (!$is_model) { $belongs_to_model = Model_App::factory(ORM::get_model_name($row->object_name()), $row->{$name}); } $name = $belongs_to_model->object_name(); foreach ($belongs_to_model->list_columns() as $column => $values) { if (Arr::get($values, 'type') === 'string' and $column_name === NULL) { $column_name = $column; } } ?> <a href="<?php echo URL::site('manager/' . $name . '/edit/' . $belongs_to_model->id); ?> "><?php echo $belongs_to_model->{$column_name}; ?> </a>
} } $belongs_to_values = Arr::merge(array('' => 'Selecione'), $parent_belongs->find_all()->as_array('id', $column_name)); $select_name = Arr::path($model->belongs_to(), $name . '.foreign_key'); ?> <?php echo Form::select($select_name, $belongs_to_values, $model->{$select_name}, array('class' => 'form-control')); ?> <?php } elseif (!empty($has_many) and Arr::get($has_many, $name) and Arr::path($has_many, $name . '.through')) { ?> <?php $column_name = NULL; $model_name = ORM::get_model_name($name); $far_primary_key = Arr::path($has_many, $name . '.far_primary_key', 'id'); if (!class_exists($model_name)) { $model_name = Arr::path($has_many, $name . '.model'); } $parent_has_many = Model_App::factory($model_name); foreach ($parent_has_many->list_columns() as $column => $values) { if (Arr::get($values, 'type') === 'string' and $column_name === NULL) { $column_name = $column; } } $selects = $parent_has_many->find_all()->as_array('id', $column_name); $selected = $model->{$name}->find_all()->as_array(NULL, $far_primary_key); echo '<div style="max-height: 400px; overflow-x: auto; border: 1px solid #ddd; border-radius: 3px; padding: 0 10px;">'; foreach ($selects as $select_id => $select_name) { echo '<div class="checkbox">';
<?php defined('SYSPATH') or die('No direct script access.'); Route::set('api_user', 'api/user(/<action>)', array('id' => '\\d+'))->defaults(array('controller' => 'user', 'action' => 'index', 'directory' => 'api')); Route::set('api_model_actions', 'api(/<model>(/<model_action>(/<id>)))', array('id' => '\\d+'))->filter(function ($route, $params, $request) { $api = 'Api_' . ORM::get_model_name(Arr::get($params, 'model')); if (!class_exists($api)) { return FALSE; } if (!method_exists($api, 'action_' . Arr::get($params, 'model_action', 'index'))) { return FALSE; } })->defaults(array('controller' => 'custom', 'action' => 'index', 'model_action' => 'index', 'directory' => 'api')); Route::set('api_actions', 'api(/<model>(/<id>))', array('id' => '\\d+'))->defaults(array('controller' => 'app', 'action' => 'index', 'directory' => 'api')); Route::set('api', 'api(/<controller>(/<action>(/<id>)))', array('id' => '\\d+'))->defaults(array('controller' => 'app', 'action' => 'index', 'directory' => 'api'));