Beispiel #1
0
 /**
  * Build the list data.
  *
  * @access public
  * @return mixed|void
  */
 public function build()
 {
     $listMapper = $this->getMapper();
     $model = $this->getModel();
     $items = $model::with([]);
     $primaryKey = (new $model())->getKeyName();
     // Field ordering
     if (Input::has('_order_by')) {
         $items->orderBy(Input::get('_order_by'), Input::get('_order'));
     }
     // Field filters
     foreach (Input::all() as $key => $value) {
         if (empty($value) || substr($key, 0, 1) == '_' || $key == 'page') {
             continue;
         }
         $items->where($key, 'LIKE', '%' . $value . '%');
     }
     // Result scopes
     if (Input::has('_scope')) {
         $items->{Input::get('_scope')}();
     }
     $items = $items->paginate($listMapper->getAdmin()->getPerPage());
     $this->setPaginator($items);
     $result = [];
     foreach ($items as $item) {
         $row = new ListResult();
         $row->setIdentifier($item->{$primaryKey});
         foreach ($listMapper->getFields() as $field) {
             $clone = clone $field;
             $name = $clone->getName();
             $value = $item->{$name};
             if ($clone->hasBefore()) {
                 $before = $clone->getBefore();
                 $value = $before($value);
             }
             if ($clone->isMultiple()) {
                 $value = Value::decode(Config::get('bauhaus::admin.multiple-serializer'), $value);
                 $value = implode(', ', $value);
             }
             $clone->setContext(BaseField::CONTEXT_LIST)->setRowId($item->{$primaryKey})->setValue($value);
             $row->addField($name, $clone);
         }
         $result[] = $row;
     }
     $this->setResult($result);
 }
Beispiel #2
0
 /**
  * Update a model from input.
  * 
  * @param  Input $input
  *
  * @access public
  * @return FormBuilder
  */
 public function update($input)
 {
     $mapper = $this->getMapper();
     $admin = $mapper->getAdmin();
     $model = $this->getModel();
     $this->setInput($input);
     // Field pre update
     foreach ($this->getMapper()->getFields() as $field) {
         $field->preUpdate();
         // Is this a multiple field?
         if ($field->isMultiple()) {
             $value = Value::encode(Config::get('bauhaus::admin.multiple-serializer'), $input[$field->getName()]);
             $this->setInputVariable($field->getName(), $value);
         }
         if ($field->hasSaving()) {
             $saving = $field->getSaving();
             $this->setInputVariable($field->getName(), $saving($input[$field->getName()]));
         }
     }
     // Model before update hook
     if (method_exists($admin, 'beforeUpdate')) {
         $admin->beforeUpdate($input);
     }
     // Validate
     if (property_exists($model, 'rules')) {
         $validator = Validator::make($this->getInput(), $model::$rules);
         if ($validator->fails()) {
             return $validator;
         }
     }
     // Model update hook
     if (method_exists($this->getMapper()->getAdmin(), 'update')) {
         $this->getMapper()->getAdmin()->update($this->getInput());
     } else {
         $model::find($this->getIdentifier())->update($this->getInput());
     }
     // Field post update
     foreach ($this->getMapper()->getFields() as $field) {
         $field->postUpdate($this->getInput());
     }
     // Model after update hook
     if (method_exists($admin, 'afterCreate')) {
         $result = $admin->afterUpdate($this->getInput());
         if ($result instanceof \Illuminate\Http\RedirectResponse) {
             $result->send();
         }
     }
     return $this;
 }