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); } }