示例#1
0
 public static function show($controller = NULL, $action = NULL)
 {
     $controller = $controller ? $controller : Request::current()->controller();
     $action = $action ? $action : Request::current()->action();
     if ($controller === 'Login') {
         return TRUE;
     } else {
         if (!Auth::instance()->logged_in('admin')) {
             return FALSE;
         }
     }
     // is manager
     if (Can::role_exists('manager') and Auth::instance()->logged_in('manager')) {
         return TRUE;
     }
     $require_controller = 'manager-' . strtolower($controller);
     // can action
     if (Can::check($require_controller . '-' . $action)) {
         return TRUE;
     }
     // can *
     if (Can::check($require_controller)) {
         return TRUE;
     }
     // is protected
     return !Can::role_exists($require_controller);
 }
示例#2
0
 public function action_save()
 {
     if (!Can::show()) {
         exit;
     }
     $page_id = $this->request->post('page_id');
     $blocks = $this->request->post('blocks');
     $actived = $this->request->post('draft') === 'false';
     $result = Model_Page::draft($page_id, $blocks, $actived);
     $image = $this->save_image($page_id, $this->request->post('image'));
     $this->response->json($result);
 }
示例#3
0
function render_menu($items, $parent = NULL)
{
    $html = '';
    $ignored = array();
    foreach ($items as $item) {
        $controller_name = 'Controller_Manager_' . $item;
        if (class_exists($controller_name)) {
            $reflector = new ReflectionClass($controller_name);
            if ($reflector->isAbstract()) {
                continue;
            }
        }
        if (in_array($item, $ignored)) {
            continue;
        }
        $matches = preg_grep('/^' . $item . '_/i', $items);
        if ($matches and !$parent) {
            $html .= '<li><a href="#" class="dropdown-toggle" data-toggle="dropdown">';
            $html .= __(Inflector::plural($item)) . ' <span class="caret"></span>';
            $html .= '</a>';
            $html .= '<ul class="dropdown-menu">';
            if (Can::show($item, 'index')) {
                $html .= '<li><a href="./manager/' . strtolower($item) . '">' . __(Inflector::plural($item)) . '</a></li>';
            }
            $html .= render_menu($matches, $item);
            $html .= '</ul></li>';
            $ignored = array_merge($ignored, $matches);
        } else {
            if (!Can::show($item, 'index')) {
                continue;
            }
            $prepend = '';
            if ($parent) {
                $underlines = count(explode('_', $item));
                if ($underlines >= 3) {
                    for ($i = $underlines; $i > 3; $i--) {
                        $prepend .= '<i class="glyphicon glyphicon-option-horizontal"></i>';
                    }
                    $prepend .= '<i class="glyphicon glyphicon-triangle-right"></i>';
                }
            }
            $html .= '<li><a href="./manager/' . strtolower($item) . '">' . $prepend . __(Inflector::plural($item)) . '</a></li>';
        }
    }
    return $html;
}
示例#4
0
 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);
     }
 }