Example #1
0
 public function execute()
 {
     $view = $this->app->views->get('bspanel/edit');
     if (count($_POST) > 0) {
         foreach ($this->fields as $field) {
             $name = $field['name'];
             $type = $field['type'];
             $value = Arr::get($_POST, $name);
             if ($type == 'boolean') {
                 $value == (bool) $value;
             }
             $model = $this->app->models->factory($this->model, array($this->key_field => $name));
             if (!$model->loaded()) {
                 $model = $this->app->models->factory($this->model);
                 $model->{$this->key_field} = $name;
             }
             $model->{$this->value_field} = $value;
             $model->save();
             $view->success = true;
         }
     }
     $builder = new FormBuilder($this->app);
     foreach ($this->fields as $field) {
         $name = $field['name'];
         $model = $this->app->models->factory($this->model, array($this->key_field => $name));
         if ($model->loaded()) {
             $builder->set_value($name, $model->{$this->value_field});
         }
         $builder->add_field($field);
     }
     $view->title = $this->title;
     $view->fields = $builder->render();
     $this->response->body($view);
     parent::execute();
 }
Example #2
0
 public function execute()
 {
     if (count($_POST) > 0) {
         $this->app->auth->login(Arr::get($_POST, 'username'), Arr::get($_POST, 'password'));
     }
     if ($this->app->auth->logged_in('admin_panel')) {
         $this->redirect("admin");
         return;
     }
     $this->add_style("bspanel/panel/login");
     $view = $this->app->views->get('bspanel/login');
     $view->is_error = count($_POST) > 0;
     $view->username = Arr::get($_POST, 'username');
     $this->response->body($view);
     parent::execute();
 }
Example #3
0
 public function execute()
 {
     $model = $this->app->models->factory($this->model);
     $filter_value = null;
     $return_uri = $this->return_uri;
     if ($this->filter_param !== null) {
         $filter_value = Arr::get($_GET, $this->filter_param);
         if ($filter_value === null) {
             throw new Exception("filter param {$this->filter_param} not specified");
         }
         $model->where($this->filter_param, '=', new MongoId($filter_value));
         $return_uri = str_replace("<id>", $filter_value, $return_uri);
     }
     if (isset($_POST['update_sort'])) {
         $this->update_sort($model->find_all()->as_array(), explode(",", $_POST['ids']));
         $this->response->body("true");
         $this->show_layout = false;
         return;
     }
     $view = $this->app->views->get("bspanel/list");
     $view->model = $model;
     $cursor = $model->find_all();
     if ($this->sortable) {
         $cursor->sort(array($this->sort_column => 1));
     } elseif ($this->sort !== null) {
         $cursor->sort($this->sort);
     }
     $view->items = $cursor->as_array();
     $view->create_button = $this->create;
     $view->uri = $this->request->uri();
     $view->return_uri = $return_uri;
     $view->buttons = $this->buttons;
     $view->columns = $this->columns;
     $view->sortable = $this->sortable;
     $view->filter_param = $this->filter_param;
     $view->filter_value = $filter_value;
     $l = $this->app->lang;
     if ($this->filter_model === null) {
         $view->title = $l->get_ucf($model->get_model_name() . ".list");
     } else {
         $filter_model = $this->app->models->factory($this->filter_model, $filter_value);
         $view->title = $filter_model->name() . ": " . $l->get_ucf($model->get_model_name() . ".list");
     }
     $this->title = $view->title;
     $this->response->body($view->render());
     parent::execute();
 }
Example #4
0
 public function execute()
 {
     if (count($_FILES) == 0) {
         return;
     }
     $field = Arr::get(array_keys($_FILES), 0);
     $filename = is_array($_FILES[$field]['tmp_name']) ? $_FILES[$field]['tmp_name'][0] : $_FILES[$field]['tmp_name'];
     $image_model = $this->app->image->from_file($filename);
     if (isset($_GET['CKEditorFuncNum'])) {
         $url = $this->app->image->url($image_model->pk());
         // Required: anonymous function reference number as explained above.
         $funcNum = $_GET['CKEditorFuncNum'];
         // Optional: instance name (might be used to load a specific configuration file or anything else).
         $CKEditor = $_GET['CKEditor'];
         // Optional: might be used to provide localized messages.
         $langCode = $_GET['langCode'];
         $this->response->body("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction({$funcNum}, '{$url}');</script>");
     } else {
         $this->response->body($image_model->pk());
     }
 }
Example #5
0
 public function param($param, $default = null)
 {
     return Arr::get($this->params, $param, $default);
 }
Example #6
0
 /**
  * get request parameter
  * @return mixed
  */
 protected function param($name)
 {
     return Arr::get($this->params, $name);
 }
Example #7
0
 public function get_presentation($name)
 {
     return Arr::get($this->presentations, $name);
 }
Example #8
0
 public function execute()
 {
     $return_uri = $this->return_uri;
     $id = $this->param('id');
     if (!$this->app->is_id($id)) {
         $id = null;
     }
     $model = $this->app->models->factory($this->model, $id);
     if ($id !== null && false == $model->loaded()) {
         $this->redirect($this->return_uri);
         return;
     }
     $filter_value = null;
     if ($this->filter_param !== null) {
         $filter_param = $this->filter_param;
         if ($id === null) {
             $filter_value = Arr::get($_GET, $this->filter_param);
             if ($filter_value === null) {
                 throw new Exception("filter value not set");
             }
             $model->{$filter_param} = $filter_value;
         } else {
             $filter_value = $model->{$filter_param};
         }
         $return_uri = str_replace("<id>", $filter_value, $return_uri);
     }
     $view = $this->app->views->get("bspanel/edit");
     $view->model = $model;
     $view->return_uri = $return_uri;
     $view->filter_param = $this->filter_param;
     $view->filter_value = $filter_value;
     $l = $this->app->lang;
     if ($this->filter_model === null) {
         $view->title = $l->get_ucf($model->get_model_name()) . ": " . $l->get("editing");
     } else {
         $filter_model = $this->app->models->factory($this->filter_model, $filter_value);
         $view->title = $filter_model->name() . " : " . $model->name() . " : " . $l->get("editing");
     }
     $this->title = $view->title;
     $builder = new FormBuilder($this->app);
     $builder->set_model($model);
     $builder->add_fields($this->edit_fields);
     $builder->data('view_images_uri', $this->view_images_uri);
     $builder->data('upload_image_uri', $this->upload_image_uri);
     $builder->data('upload_images_uri', $this->upload_images_uri);
     if (count($_POST) > 0) {
         $field_types = $builder->get_fields_types();
         foreach ($field_types as $name => $type) {
             if ($type instanceof IField) {
                 $type->fill_model($model, $_POST);
             } elseif (!array_key_exists($name, $_POST)) {
                 if ($type == 'boolean') {
                     $model->{$name} = false;
                 } elseif ($type == 'tags') {
                     $model->{$name} = array();
                 }
             }
         }
         $fields_keys = array_keys($model->fields());
         $relations_keys = array_keys($model->relations());
         $model->values($_POST, $fields_keys);
         if (!$model->check()) {
             $view->errors = true;
         } elseif (!$model->loaded()) {
             $model->save();
             $model->values($_POST, $relations_keys);
             $model->save();
             $this->redirect($return_uri);
         } else {
             $model->save();
             $model->values($_POST, $relations_keys);
             $model->save();
             $view->success = true;
         }
     }
     $view->fields = $builder->render();
     $this->response->body($view->render());
     $this->add_script('ckeditor/ckeditor');
     parent::execute();
 }
Example #9
0
 /**
  * Set and get cookies values for this response.
  *
  *     // Get the cookies set to the response
  *     $cookies = $response->cookie();
  *
  *     // Set a cookie to the response
  *     $response->cookie('session', $value, 12352234);
  *
  * @param   mixed   $key    cookie name, or array of cookie values
  * @param   string  $value  value to set to cookie
  * @return  string
  * @return  void
  * @return  [Response]
  */
 public function cookie($key = NULL, $value = NULL, $expiration = 0)
 {
     // Handle the get cookie calls
     if ($key === NULL) {
         return $this->cookies;
     } elseif (!is_array($key) and !$value) {
         return Arr::get($this->cookies, $key);
     }
     // Handle the set cookie calls
     if (is_array($key)) {
         reset($key);
         while (list($_key, $_value) = each($key)) {
             $this->cookie($_key, $_value);
         }
     } else {
         $this->cookies[$key] = array('value' => $value, 'expiration' => $expiration);
     }
     return $this;
 }
Example #10
0
 protected function get_list_buttons(array $params)
 {
     $list_buttons = Arr::get($params, 'list_buttons', array());
     for ($i = 0; $i < count($list_buttons); $i++) {
         if (is_array($list_buttons[$i])) {
             $list_buttons[$i] = new Action($list_buttons[$i]['name'], $this->uri($list_buttons[$i]['uri']), $list_buttons[$i]['icon'], Arr::get($list_buttons[$i], 'attributes', array()));
         }
     }
     return $list_buttons;
 }
Example #11
0
 /**
  * @param boolean $as_string
  * @return null
  * @return MongoId
  * @return string
  */
 public function pk($as_string = false)
 {
     $id = Arr::get($this->_object, '_id');
     if ($id === null) {
         return null;
     }
     return $as_string ? (string) $id : $id;
 }
Example #12
0
 protected function field_cfg_from_string($name)
 {
     $edit_field_cfg = array('name' => $name);
     if ($this->model == null) {
         throw new Exception('model not set');
     }
     $field = Arr::get($this->model->fields(), $name);
     $relation = Arr::get($this->model->get_relations(), $name);
     if ($field !== null) {
         switch ($field['type']) {
             case Type::BOOLEAN:
                 $edit_field_cfg['type'] = 'boolean';
                 break;
             case Type::DOUBLE:
             case Type::INTEGER:
             case Type::STRING:
                 $edit_field_cfg['type'] = 'text';
                 break;
             case Type::DATE:
                 $edit_field_cfg['type'] = 'date';
                 break;
             default:
                 throw new Exception('not implemented');
         }
     } elseif ($relation !== null && $relation['model'] == 'Image') {
         if ($relation['type'] == Relation::MANY_TO_ONE) {
             $edit_field_cfg['type'] = 'image';
         } elseif ($relation['type'] == Relation::MANY_TO_MANY) {
             $edit_field_cfg['type'] = 'gallery';
         } else {
             throw new Exception('bad relation ' . $name);
         }
     } elseif ($relation !== null) {
         switch ($relation['type']) {
             case Relation::MANY_TO_MANY:
                 $edit_field_cfg['type'] = 'tags';
                 break;
             default:
                 throw new Exception('relation not implemented');
         }
     } else {
         throw new Exception('property not found');
     }
     return $edit_field_cfg;
 }
Example #13
0
 /**
  * Executes all validation rules. This should
  * typically be called within an if/else block.
  *
  *     if ($validation->check())
  *     {
  *          // The data is valid, do something here
  *     }
  *
  * @return  boolean
  */
 public function check()
 {
     // New data set
     $data = $this->_errors = array();
     // Store the original data because this class should not modify it post-validation
     $original = $this->_data;
     // Get a list of the expected fields
     $expected = Arr::merge(array_keys($original), array_keys($this->_labels));
     // Import the rules locally
     $rules = $this->_rules;
     foreach ($expected as $field) {
         // Use the submitted value or NULL if no data exists
         $data[$field] = Arr::get($this, $field);
         if (isset($rules[TRUE])) {
             if (!isset($rules[$field])) {
                 // Initialize the rules for this field
                 $rules[$field] = array();
             }
             // Append the rules
             $rules[$field] = array_merge($rules[$field], $rules[TRUE]);
         }
     }
     // Overload the current array with the new one
     $this->_data = $data;
     // Remove the rules that apply to every field
     unset($rules[TRUE]);
     // Bind the validation object to :validation
     $this->bind(':validation', $this);
     // Bind the data to :data
     $this->bind(':data', $this->_data);
     // Execute the rules
     foreach ($rules as $field => $set) {
         // Get the field value
         $value = $this[$field];
         // Bind the field name and value to :field and :value respectively
         $this->bind(array(':field' => $field, ':value' => $value));
         foreach ($set as $array) {
             // Rules are defined as array($rule, $params)
             list($rule, $params) = $array;
             foreach ($params as $key => $param) {
                 if (is_string($param) and array_key_exists($param, $this->_bound)) {
                     // Replace with bound value
                     $params[$key] = $this->_bound[$param];
                 }
             }
             // Default the error name to be the rule (except array and lambda rules)
             $error_name = $rule;
             if (is_array($rule)) {
                 // Allows rule('field', array(':model', 'some_rule'));
                 if (is_string($rule[0]) and array_key_exists($rule[0], $this->_bound)) {
                     // Replace with bound value
                     $rule[0] = $this->_bound[$rule[0]];
                 }
                 // This is an array callback, the method name is the error name
                 $error_name = $rule[1];
                 $passed = call_user_func_array($rule, $params);
             } elseif (!is_string($rule)) {
                 // This is a lambda function, there is no error name (errors must be added manually)
                 $error_name = FALSE;
                 $passed = call_user_func_array($rule, $params);
             } elseif (method_exists(__NAMESPACE__ . '\\Valid', $rule)) {
                 // Use a method in this object
                 $method = new ReflectionMethod(__NAMESPACE__ . '\\Valid', $rule);
                 // Call static::$rule($this[$field], $param, ...) with Reflection
                 $passed = $method->invokeArgs(NULL, $params);
             } elseif (strpos($rule, '::') === FALSE) {
                 // Use a function call
                 $function = new ReflectionFunction($rule);
                 // Call $function($this[$field], $param, ...) with Reflection
                 $passed = $function->invokeArgs($params);
             } else {
                 // Split the class and method of the rule
                 list($class, $method) = explode('::', $rule, 2);
                 // Use a static method call
                 $method = new ReflectionMethod($class, $method);
                 // Call $Class::$method($this[$field], $param, ...) with Reflection
                 $passed = $method->invokeArgs(NULL, $params);
             }
             // Ignore return values from rules when the field is empty
             if (!in_array($rule, $this->_empty_rules) and !Valid::not_empty($value)) {
                 continue;
             }
             if ($passed === FALSE and $error_name !== FALSE) {
                 // Add the rule to the errors
                 $this->error($field, $error_name, $params);
                 // This field has an error, stop executing rules
                 break;
             } elseif (isset($this->_errors[$field])) {
                 // The callback added the error manually, stop checking rules
                 break;
             }
         }
     }
     // Restore the data to its original form
     $this->_data = $original;
     return empty($this->_errors);
 }
Example #14
0
 /**
  * 
  * @return integer
  * @throws Kohana_Exception
  */
 public function count()
 {
     if (Arr::get($this->relation1, 'store') !== false) {
         if (isset($this->relation1['field'])) {
             $field = $this->relation1['field'];
             $arr = $this->model1->{$field};
             return count($arr);
         } else {
             $id = "{$this->model1->get_model_name()}:{$this->model1->pk()}:{$this->field1}";
             $self_collection = self::COLLECTION;
             $code = "function(){return db.{$self_collection}.find({'_id':'{$id}'}).limit(1)[0].ids.length}";
             $return_value = 0;
             try {
                 $return_value = intval($this->model1->db()->execute($code));
             } catch (Exception $ex) {
             }
             return $return_value;
         }
     } elseif (isset($this->relation2) && Arr::get($this->relation2, 'store') !== false) {
         if (isset($this->relation2['field'])) {
             return $this->model2->get_collection()->count(array($this->relation2['field'] => $this->model1->pk()));
         } else {
             return $this->model2->db()->selectCollection(self::COLLECTION)->count(array("ids" => $this->model1->pk(), 'model' => $this->relation2['model'], 'field' => $this->field2));
         }
     } else {
         throw new Kohana_Exception("can`t get ids for this relation");
     }
 }
Example #15
0
 /**
  * get configuration
  * @param string $name
  * @return Array
  */
 public function config($name = 'default')
 {
     return Arr::get($this->configs, $name);
 }