Пример #1
0
 public function pullFromData(array $data)
 {
     $this->value = new Collection();
     $class = CrudModel::resolveClass($this->config['model']);
     foreach ($data[$this->name] as $id => $entry) {
         $valid = true;
         $obj = $id > 0 ? $class::findOrFail($id) : new $class();
         foreach ($this->getControls($obj, false) as $c) {
             $c->pullFromData($entry);
             $c->pushToModel();
             if ($c->config['required'] && !$c->getValue()) {
                 $valid = false;
             }
         }
         if ($valid) {
             $this->value->push($obj);
         }
         //$obj->saveDirect();
     }
     //        if (!empty($data['mail_id']) && !empty($data['mail_date']) && count($data['mail_id']) == count($data['mail_date']))
     //        {
     //            foreach ($data['mail_date'] as $idx => $date)
     //            {
     //                if (!empty($date) && strtotime($date) !== false)
     //                {
     //                    $this->value[] = ['id' => $data['mail_id'][$idx], 'maildate' => strtotime($date)];
     //                }
     //            }
     //        }
 }
Пример #2
0
 public function getOptions()
 {
     if (is_null($this->value)) {
         return [];
     }
     $class = CrudModel::resolveClass($this->config['model']);
     $obj = new $class();
     $coll = $obj->find($this->getValueAsArray());
     return $this->flatOptions($coll, $obj);
 }
Пример #3
0
Файл: Tags.php Проект: skvn/crud
 public function pushToModel()
 {
     $ids = [];
     $class = CrudModel::resolveClass($this->config['model']);
     $dummyModel = new $class();
     if (!empty($this->value)) {
         foreach ($this->value as $title) {
             $obj = $class::firstOrCreate([$dummyModel->confParam('title_field') => trim($title)]);
             $ids[] = $obj->getKey();
         }
     }
     $this->model->setAttribute($this->name, $ids);
 }
Пример #4
0
 public function handle()
 {
     $models = Container::getInstance()['config']->get('geo.models');
     foreach ($models as $model) {
         $class = CrudModel::resolveClass($model);
         $list = $class::geoUncoded()->take(100)->get();
         $this->log("Geocode process started for " . $list->count() . " " . $model, "geocode");
         foreach ($list as $obj) {
             try {
                 $obj->geoFetchCoordinates();
                 if ($obj->geo_coded == 1) {
                     $this->log($obj->full_text_address . " coded with [" . $obj->lng . ", " . $obj->lat . "]", "geocode");
                 } else {
                     $this->log($obj->classViewName . "#" . $obj->id . " geocode failed", "geocode");
                 }
             } catch (\ErrorException $e) {
                 $this->log($obj->classViewName . "#" . $obj->id . " geocode failed with error: " . $e->getMessage(), "geocode");
             }
         }
         $this->log("Geocode process finished", "geocode");
     }
 }
Пример #5
0
Файл: Tree.php Проект: skvn/crud
 public function getOptions()
 {
     $class = CrudModel::resolveClass($this->config['model']);
     $modelObj = new $class();
     if (!empty($this->config['find']) && !empty($this->config['model'])) {
         $method = $method = 'selectOptions' . studly_case($this->config['find']);
         $val = $this->getValue();
         if (!is_array($val)) {
             if ($val instanceof Collection) {
                 $val = $val->toArray();
             } elseif (is_scalar($val)) {
                 $val = [$val];
             }
         }
         return $modelObj->{$method}($this->getName(), $val);
     }
     if (!empty($this->config['model'])) {
         return CrudModelCollectionBuilder::createTree($modelObj)->fetch();
     } elseif (!empty($this->config['find']) && empty($this->config['model'])) {
         $method = $method = 'selectOptions' . studly_case($this->config['find']);
         return $this->model->{$method}($this->getName());
     }
 }
Пример #6
0
 private function getModelOptions()
 {
     $class = CrudModel::resolveClass($this->config['model']);
     $modelObj = CrudModel::createInstance($this->config['model'], null, is_numeric($this->value) ? $this->value : null);
     //$modelObj = new $class();
     if (!empty($this->config['find'])) {
         $method = 'selectOptions' . studly_case($this->config['find']);
         return $this->formatOptionsArray($modelObj->{$method}($this->model));
     } else {
         if ($modelObj->confParam('tree')) {
             $collection = CrudModelCollectionBuilder::create($modelObj)->fetch();
         } else {
             $query = $class::query();
             if (!empty($this->config['sort'])) {
                 foreach ($this->config['sort'] as $c => $d) {
                     $query->orderBy($c, $d);
                 }
             }
             $collection = $query->get();
         }
     }
     if ($this->isGrouped()) {
         $options = $this->groupedOptions($collection);
     } else {
         $options = $this->flatOptions($collection, $modelObj);
     }
     return $options;
 }
Пример #7
0
 public function crudDelete($model)
 {
     try {
         $class = CrudModel::resolveClass($model);
         $ids = $this->request->get('ids');
         if (is_array($ids)) {
             $class::destroy($ids);
         }
         return ['success' => true];
     } catch (Exception $e) {
         return ['success' => false, 'error' => $e->getMessage()];
     }
 }