Esempio n. 1
0
 public static function createDataTables(CrudModel $model, $args = [])
 {
     $listType = $model->getList()->getParam('list_type');
     if ($listType && $listType == 'dt_tree') {
         $args['view_type'] = 'data_tables_tree';
     } else {
         $args['view_type'] = 'data_tables';
     }
     return self::create($model, $args);
 }
Esempio n. 2
0
 public function save()
 {
     if ($this->dirtyValue) {
         $oldIds = $this->getIds();
         $ids = [];
         foreach ($this->dirtyValue as $obj) {
             if ($obj->exists) {
                 $ids[] = $obj->getKey();
             }
             $this->relation->save($obj);
         }
         $toUnlink = array_diff($oldIds, $ids);
     } else {
         $toUnlink = $this->getIds();
     }
     if ($toUnlink && is_array($toUnlink)) {
         foreach ($toUnlink as $id) {
             $col_id = $this->relation->getForeignKey();
             $col_class = $this->relation->getPlainMorphType();
             $obj = CrudModel::createInstance($this->config['model'], null, $id);
             if (($this->config['on_delete'] ?? false) === 'delete') {
                 $obj->delete();
             } else {
                 $obj->{$col_id} = null;
                 $obj->{$col_class} = null;
                 $obj->save();
             }
         }
     }
 }
Esempio n. 3
0
 public function save()
 {
     $class = CrudModel::resolveClass($this->config['model']);
     $titles = $this->model->getApp()['request']->get($this->config['name'] . '_title');
     foreach ($this->dirtyValue as $idx => $file) {
         if ($file instanceof UploadedFile) {
             $obj = new $class();
             $fileInfo = $obj->attachStoreTmpFile($file);
             $fileInfo['title'] = $titles[$idx] ?? $fileInfo['originalName'];
             if (!empty($fileInfo['originalPath'])) {
                 $obj->setAttribute($this->relation->getForeignKey(), $this->model->getKey());
                 $obj->attachStoreFile($fileInfo, $this->model->getFilesConfig($fileInfo['originalName']));
             }
         }
     }
     $titles = $this->model->getApp()['request']->get($this->config['name'] . '_title');
     if ($titles && !empty($titles)) {
         foreach ($titles as $iid => $title) {
             if ($iid > 0) {
                 $obj = $class::findOrFail($iid);
                 $obj->title = $title;
                 $obj->save();
             }
         }
     }
 }
Esempio n. 4
0
 public function save()
 {
     if (is_array($this->dirtyValue)) {
         $oldIds = $this->getIds();
         foreach ($this->dirtyValue as $id) {
             $obj = CrudModel::createInstance($this->config['model'], null, $id);
             $this->relation->save($obj);
         }
         $toUnlink = array_diff($oldIds, $this->dirtyValue);
     } else {
         $toUnlink = $this->getIds();
     }
     if ($toUnlink && is_array($toUnlink)) {
         foreach ($toUnlink as $id) {
             $col = $this->relation->getForeignKey();
             //                if (!empty($field['ref_column']))
             //                {
             //                    $col = $field['ref_column'];
             //                }
             //                else
             //                {
             //                    $col = $this->classViewName . '_id';
             //                }
             $obj = CrudModel::createInstance($this->config['model'], null, $id);
             if (($this->config['on_delete'] ?? false) === 'delete') {
                 $obj->delete();
             } else {
                 $obj->{$col} = null;
                 $obj->save();
             }
         }
     }
 }
Esempio n. 5
0
 public function get()
 {
     $val = parent::get();
     if (is_null($val)) {
         return CrudModel::createInstance($this->config['model']);
     }
     return $val;
 }
Esempio n. 6
0
 public function onBeforeCreate()
 {
     $count_all = self::count();
     if (!$count_all) {
         $this->acl_role = 'root';
     }
     return parent::onBeforeCreate();
 }
Esempio n. 7
0
 public function create()
 {
     $table = $this->config['pivot_table'] ?? null;
     $self = $this->config['pivot_self_key'] ?? null;
     $foreign = $this->config['pivot_foreign_key'] ?? null;
     $this->relation = $this->model->belongsToMany(CrudModel::resolveClass($this->config['model']), $table, $self, $foreign, $this->config['name']);
     $this->sort();
     return $this;
 }
Esempio n. 8
0
 public function save()
 {
     $class = CrudModel::resolveClass($this->config['model']);
     if ($this->dirtyValue instanceof UploadedFile) {
         $obj = $class::findOrNew($this->model->getAttribute($this->config['field']));
         $fileInfo = $obj->attachStoreTmpFile($this->dirtyValue);
         if (!empty($fileInfo['originalPath'])) {
             $obj->attachStoreFile($fileInfo, $this->model->getFilesConfig($fileInfo['originalName']));
             $this->model->setAttribute($this->relation->getForeignKey(), $obj->getKey());
             $this->model->save();
         }
     }
 }
Esempio n. 9
0
 public function createRelatedModel()
 {
     return CrudModel::createInstance($this->config['model']);
 }
Esempio n. 10
0
 public function save()
 {
     $obj = CrudModel::createInstance($this->config['model'], null, $this->dirtyValue);
     $obj->setAttribute($this->config['field'], $this->model->getKey());
     $obj->save();
 }