diff --git a/src/Abstractor/Eloquent/Field.php b/src/Abstractor/Eloquent/Field.php index 383104b..9f810cc 100644 --- a/src/Abstractor/Eloquent/Field.php +++ b/src/Abstractor/Eloquent/Field.php @@ -180,7 +180,6 @@ public function getFormField() return $this->formField; } - public function hideValue($value = null) { if (!is_null($value)) { @@ -190,7 +189,6 @@ public function hideValue($value = null) return $this->hideValue; } - public function saveIfEmpty($value = null) { if (!is_null($value)) { @@ -200,7 +198,6 @@ public function saveIfEmpty($value = null) return $this->saveIfEmpty; } - public function noValidate($value = null) { if (!is_null($value)) { diff --git a/src/Abstractor/Eloquent/FieldFactory.php b/src/Abstractor/Eloquent/FieldFactory.php index 2eace97..46935f6 100644 --- a/src/Abstractor/Eloquent/FieldFactory.php +++ b/src/Abstractor/Eloquent/FieldFactory.php @@ -53,7 +53,6 @@ public function __construct(FormManagerFactory $factory) $this->factory = $factory; } - public function setColumn(Column $column) { $this->column = $column; @@ -61,7 +60,6 @@ public function setColumn(Column $column) return $this; } - public function setConfig(array $config) { $this->config = $config; @@ -69,7 +67,6 @@ public function setConfig(array $config) return $this; } - public function get() { $formElement = $this->getFormElement(); diff --git a/src/Abstractor/Eloquent/Model.php b/src/Abstractor/Eloquent/Model.php index cc84da4..0e1d0d9 100644 --- a/src/Abstractor/Eloquent/Model.php +++ b/src/Abstractor/Eloquent/Model.php @@ -1,24 +1,23 @@ getConfigValue($action, 'display'); - $customHiddenColumns = $this->getConfigValue($action, 'hide') ? : []; + $customHiddenColumns = $this->getConfigValue($action, 'hide') ?: []; - $columns = array(); - if (! empty($customDisplayedColumns) && is_array($customDisplayedColumns)) { + $columns = []; + if (!empty($customDisplayedColumns) && is_array($customDisplayedColumns)) { foreach ($customDisplayedColumns as $customColumn) { if (strpos($customColumn, '.')) { $customColumnRelation = explode('.', $customColumn); @@ -161,14 +160,14 @@ public function getColumns($action, $withForeignKeys = false) $relationColumns = $nestedRelation->getModelAbstractor()->getColumns($action); - if (! array_key_exists($customColumnRelationFieldName, $relationColumns)) { - throw new AbstractorException("Column " . $customColumnRelationFieldName . " does not exist on relation " . implode('.', $customColumnRelation) . " of model " . $this->getModel()); + if (!array_key_exists($customColumnRelationFieldName, $relationColumns)) { + throw new AbstractorException('Column '.$customColumnRelationFieldName.' does not exist on relation '.implode('.', $customColumnRelation).' of model '.$this->getModel()); } $columns[$customColumn] = $relationColumns[$customColumnRelationFieldName]; } else { - if (! array_key_exists($customColumn, $tableColumns)) { - throw new AbstractorException("Column " . $customColumn . " does not exist on " . $this->getModel()); + if (!array_key_exists($customColumn, $tableColumns)) { + throw new AbstractorException('Column '.$customColumn.' does not exist on '.$this->getModel()); } $columns[$customColumn] = $tableColumns[$customColumn]; @@ -195,8 +194,8 @@ protected function getNestedRelation(Model $modelAbstractor, $relationName) { $relations = $modelAbstractor->getRelations(); - if (! $relations->has($relationName)) { - throw new AbstractorException("Relation " . $relationName . " not configured on " . $modelAbstractor->getModel()); + if (!$relations->has($relationName)) { + throw new AbstractorException('Relation '.$relationName.' not configured on '.$modelAbstractor->getModel()); } $relation = $relations->get($relationName); @@ -217,7 +216,7 @@ public function getRelations() $relations = collect(); - if (! empty($configRelations)) { + if (!empty($configRelations)) { foreach ($configRelations as $relationName => $configRelation) { if (is_int($relationName)) { $relationName = $configRelation; @@ -225,7 +224,7 @@ public function getRelations() $config = []; if ($configRelation !== $relationName) { - if (! is_array($configRelation)) { + if (!is_array($configRelation)) { $config['type'] = $configRelation; } else { $config = $configRelation; @@ -240,7 +239,7 @@ public function getRelations() $secondaryRelations = $relation->getSecondaryRelations(); - if (! $secondaryRelations->isEmpty()) { + if (!$secondaryRelations->isEmpty()) { $relations->put( $relationName, collect(['relation' => $relation, 'secondaryRelations' => $secondaryRelations]) @@ -256,16 +255,18 @@ public function getRelations() /** * @param string|null $arrayKey - * @return array + * * @throws AbstractorException + * + * @return array */ public function getListFields($arrayKey = 'main') { $columns = $this->getColumns('list'); - $fieldsPresentation = $this->getConfigValue('fields_presentation') ? : []; + $fieldsPresentation = $this->getConfigValue('fields_presentation') ?: []; - $fields = array(); + $fields = []; foreach ($columns as $name => $column) { $presentation = null; if (array_key_exists($name, $fieldsPresentation)) { @@ -277,7 +278,7 @@ public function getListFields($arrayKey = 'main') 'presentation' => $presentation, 'form_type' => null, 'validation' => null, - 'functions' => null + 'functions' => null, ]; $fields[$arrayKey][] = $this->fieldFactory @@ -291,16 +292,18 @@ public function getListFields($arrayKey = 'main') /** * @param string|null $arrayKey - * @return array + * * @throws AbstractorException + * + * @return array */ public function getDetailFields($arrayKey = 'main') { $columns = $this->getColumns('detail'); - $fieldsPresentation = $this->getConfigValue('fields_presentation') ? : []; + $fieldsPresentation = $this->getConfigValue('fields_presentation') ?: []; - $fields = array(); + $fields = []; foreach ($columns as $name => $column) { $presentation = null; if (array_key_exists($name, $fieldsPresentation)) { @@ -312,7 +315,7 @@ public function getDetailFields($arrayKey = 'main') 'presentation' => $presentation, 'form_type' => null, 'validation' => null, - 'functions' => null + 'functions' => null, ]; $fields[$arrayKey][] = $this->fieldFactory @@ -325,10 +328,12 @@ public function getDetailFields($arrayKey = 'main') } /** - * @param bool|null $withForeignKeys + * @param bool|null $withForeignKeys * @param string|null $arrayKey - * @return array + * * @throws AbstractorException + * + * @return array */ public function getEditFields($withForeignKeys = false, $arrayKey = 'main') { @@ -336,9 +341,9 @@ public function getEditFields($withForeignKeys = false, $arrayKey = 'main') $this->readConfig('edit'); - $fields = array(); + $fields = []; foreach ($columns as $name => $column) { - if (! in_array($name, $this->getReadOnlyColumns())) { + if (!in_array($name, $this->getReadOnlyColumns())) { $presentation = null; if (array_key_exists($name, $this->fieldsPresentation)) { $presentation = $this->fieldsPresentation[$name]; @@ -349,7 +354,7 @@ public function getEditFields($withForeignKeys = false, $arrayKey = 'main') 'presentation' => $presentation, 'form_type' => null, 'validation' => null, - 'functions' => null + 'functions' => null, ]; $config = $this->setConfig($config, $name); @@ -359,24 +364,24 @@ public function getEditFields($withForeignKeys = false, $arrayKey = 'main') ->setConfig($config) ->get(); - if (! empty($this->instance) && ! empty($this->instance->getAttribute($name))) { + if (!empty($this->instance) && !empty($this->instance->getAttribute($name))) { $field->setValue($this->instance->getAttribute($name)); } $fields[$arrayKey][$name] = $field; - if (! empty($config['form_type']) && $config['form_type'] === 'file') { + if (!empty($config['form_type']) && $config['form_type'] === 'file') { $field = $this->fieldFactory ->setColumn($column) ->setConfig([ - 'name' => $name . '__delete', + 'name' => $name.'__delete', 'presentation' => null, 'form_type' => 'checkbox', 'no_validate' => true, - 'functions' => null + 'functions' => null, ]) ->get(); - $fields[$arrayKey][$name . '__delete'] = $field; + $fields[$arrayKey][$name.'__delete'] = $field; } } } @@ -395,6 +400,7 @@ protected function getReadOnlyColumns() /** * @param string $action + * * @return ElementInterface */ public function getForm($action) @@ -407,13 +413,14 @@ public function getForm($action) /** * @param array $requestForm + * * @return mixed */ public function persist(Request $request) { /** @var \ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager $modelManager */ $modelManager = App::make('ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager'); - if (! empty($this->instance)) { + if (!empty($this->instance)) { $item = $this->instance; } else { $item = $modelManager->getModelInstance($this->getModel()); @@ -425,10 +432,10 @@ public function persist(Request $request) return; } - if (! empty($fields['main'])) { + if (!empty($fields['main'])) { $skip = null; foreach ($fields['main'] as $key => $field) { - /** @var FieldContract $field */ + /* @var FieldContract $field */ if ($skip === $key) { $skip = null; continue; @@ -449,21 +456,21 @@ public function persist(Request $request) } if (get_class($field->getFormField()) === \FormManager\Fields\File::class) { - $handleResult = $this->handleField($request, $item, $fields['main'], 'main', $fieldName,$this->mustDeleteFilesInFilesystem); - if (! empty($handleResult['skip'])) { + $handleResult = $this->handleField($request, $item, $fields['main'], 'main', $fieldName, $this->mustDeleteFilesInFilesystem); + if (!empty($handleResult['skip'])) { $skip = $handleResult['skip']; } - if (! empty($handleResult['requestValue'])) { + if (!empty($handleResult['requestValue'])) { $requestValue = $handleResult['requestValue']; } } - if (! $field->saveIfEmpty() && empty($requestValue)) { + if (!$field->saveIfEmpty() && empty($requestValue)) { continue; } - if (! empty($requestValue) || (empty($requestValue) && ! empty($item->getAttribute($fieldName)))) { + if (!empty($requestValue) || (empty($requestValue) && !empty($item->getAttribute($fieldName)))) { $item->setAttribute( $fieldName, $field->applyFunctions($requestValue) @@ -477,7 +484,7 @@ public function persist(Request $request) $this->setInstance($item); - if (! empty($relations = $this->getRelations())) { + if (!empty($relations = $this->getRelations())) { foreach ($relations as $relationKey => $relation) { if ($relation instanceof Collection) { $input = $request->input($relationKey); @@ -485,7 +492,7 @@ public function persist(Request $request) $relationInstance = $relation->get('relation'); $relationInstance->persist($input, $request); } else { - /** @var $relation Relation */ + /* @var $relation Relation */ $relation->persist($request->input($relationKey), $request); } } @@ -534,7 +541,7 @@ public function getFieldValue($item, $fieldName) } if (empty($relation)) { - return null; + return; } if ($relation instanceof Collection) { @@ -558,7 +565,7 @@ public function getFieldValue($item, $fieldName) } /** - * @return boolean + * @return bool */ public function mustDeleteFilesInFilesystem() { diff --git a/src/Abstractor/Eloquent/ModelFactory.php b/src/Abstractor/Eloquent/ModelFactory.php index eb1f3bf..6d120e7 100644 --- a/src/Abstractor/Eloquent/ModelFactory.php +++ b/src/Abstractor/Eloquent/ModelFactory.php @@ -1,14 +1,15 @@ fieldFactory = $fieldFactory; $this->slugger = new Slugger(); $this->generator = $generator; - $this->anavel = $anavel; + $this->anavel = $anavel; } /** * @param $slug * @param null $id - * @return Model|null + * * @throws \Exception + * + * @return Model|null */ public function getBySlug($slug, $id = null) { @@ -94,7 +98,7 @@ public function getBySlug($slug, $id = null) } if (is_null($model)) { - throw new FactoryException("Model ".$slug." not found on configuration"); + throw new FactoryException('Model '.$slug.' not found on configuration'); } return $model; @@ -105,7 +109,6 @@ public function getByName($name, $id = null) return $this->getBySlug($this->slugger->slugify($name)); } - public function getByClassName($classname, array $config, $id = null) { $model = new Model(array_merge(['model' => $classname], $config), $this->modelManager->getAbstractionLayer($classname), $this->relationFactory, $this->fieldFactory, $this->generator); diff --git a/src/Abstractor/Eloquent/Relation/MiniCrud.php b/src/Abstractor/Eloquent/Relation/MiniCrud.php index 1b9531a..2e01a59 100644 --- a/src/Abstractor/Eloquent/Relation/MiniCrud.php +++ b/src/Abstractor/Eloquent/Relation/MiniCrud.php @@ -1,11 +1,11 @@ results)) { return $this->results = $this->eloquentRelation->getResults(); } + return $this->results; } @@ -119,12 +120,12 @@ public function getEditFieldsBase() //Add field for model deletion $config = [ - 'name' => '__delete', + 'name' => '__delete', 'presentation' => 'Delete', - 'form_type' => 'checkbox', - 'no_validate' => true, - 'validation' => null, - 'functions' => null + 'form_type' => 'checkbox', + 'no_validate' => true, + 'validation' => null, + 'functions' => null, ]; /** @var Field $field */ @@ -146,12 +147,12 @@ public function getEditFieldsBase() } $config = [ - 'name' => $columnName, - 'presentation' => $this->name . ' ' . ucfirst(transcrud($columnName)), - 'form_type' => $formType, - 'no_validate' => true, - 'validation' => null, - 'functions' => null + 'name' => $columnName, + 'presentation' => $this->name.' '.ucfirst(transcrud($columnName)), + 'form_type' => $formType, + 'no_validate' => true, + 'validation' => null, + 'functions' => null, ]; $config = $this->setConfig($config, $columnName); @@ -168,15 +169,15 @@ public function getEditFieldsBase() $field = $this->fieldFactory ->setColumn($column) ->setConfig([ - 'name' => $columnName . '__delete', + 'name' => $columnName.'__delete', 'presentation' => null, - 'form_type' => 'checkbox', - 'no_validate' => true, - 'validation' => null, - 'functions' => null + 'form_type' => 'checkbox', + 'no_validate' => true, + 'validation' => null, + 'functions' => null, ]) ->get(); - $fields[$columnName . '__delete'] = $field; + $fields[$columnName.'__delete'] = $field; } } } @@ -186,6 +187,7 @@ public function getEditFieldsBase() /** * @param array|null $relationArray + * * @return mixed */ public function persist(array $relationArray = null, Request $request) @@ -221,12 +223,12 @@ public function persist(array $relationArray = null, Request $request) if (get_class($field->getFormField()) === \FormManager\Fields\File::class) { $handleResult = $this->handleField($request, $relationModel, $fieldsBase, - $this->name . ".$relationIndex", $fieldName, $this->modelAbstractor->mustDeleteFilesInFilesystem()); - if (! empty($handleResult['skip'])) { + $this->name.".$relationIndex", $fieldName, $this->modelAbstractor->mustDeleteFilesInFilesystem()); + if (!empty($handleResult['skip'])) { $skip = $handleResult['skip']; unset($relationArray[$relationIndex][$skip]); } - if (! empty($handleResult['requestValue'])) { + if (!empty($handleResult['requestValue'])) { $relationArray[$relationIndex][$fieldName] = $handleResult['requestValue']; } } @@ -306,6 +308,7 @@ public function getDisplayType() /** * @param array $fields + * * @return array */ public function addSecondaryRelationFields(array $fields) @@ -318,7 +321,7 @@ public function addSecondaryRelationFields(array $fields) } else { $tempFields[$editGroupName] = $editGroup; } - }; + } } foreach ($fields[$this->name] as $groupKey => $mainFields) { $combinedFields = array_merge($mainFields, $tempFields); diff --git a/src/Abstractor/Eloquent/Traits/HandleFiles.php b/src/Abstractor/Eloquent/Traits/HandleFiles.php index 18eb592..038b419 100644 --- a/src/Abstractor/Eloquent/Traits/HandleFiles.php +++ b/src/Abstractor/Eloquent/Traits/HandleFiles.php @@ -1,46 +1,44 @@ slug . DIRECTORY_SEPARATOR; - $basePath = base_path(DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR .config('anavel-crud.uploads_path')); - $modelPath = $basePath . $modelFolder; + $normalizeGroupName = str_replace('.', '#', $groupName); + $contentFromUploadedField = "uploaded-content.{$normalizeGroupName}#{$fieldName}#"; + $modelFolder = $this->slug.DIRECTORY_SEPARATOR; + $basePath = base_path(DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.config('anavel-crud.uploads_path')); + $modelPath = $basePath.$modelFolder; $skip = null; $requestValue = null; - if (! empty($fields["{$fieldName}__delete"])) { + if (!empty($fields["{$fieldName}__delete"])) { //We never want to save this field, it doesn't exist in the DB $skip = "{$fieldName}__delete"; //If user wants to delete the existing file - if (! empty($request->input("{$groupName}.{$fieldName}__delete"))) { + if (!empty($request->input("{$groupName}.{$fieldName}__delete"))) { $adapter = new Local($basePath); $filesystem = new Filesystem($adapter); if ($filesystem->has($item->$fieldName) && $mustDeleteFilesInFilesystem) { @@ -52,44 +50,43 @@ protected function handleField(Request $request, $item, array $fields, $groupNam $fieldName, null ); + return [ - 'skip' => $skip + 'skip' => $skip, ]; } } - if($request->has($contentFromUploadedField)) - { + if ($request->has($contentFromUploadedField)) { $requestValue = $request->input($contentFromUploadedField); + } elseif ($request->hasFile($groupName.'.'.$fieldName)) { + $fileName = pathinfo($request->file($groupName.'.'.$fieldName)->getClientOriginalName(), PATHINFO_FILENAME); + $extension = pathinfo($request->file($groupName.'.'.$fieldName)->getClientOriginalName(), PATHINFO_EXTENSION); - }elseif ($request->hasFile($groupName .'.'.$fieldName)) { - $fileName = pathinfo($request->file($groupName .'.'.$fieldName)->getClientOriginalName(), PATHINFO_FILENAME); - $extension = pathinfo($request->file($groupName .'.'.$fieldName)->getClientOriginalName(), PATHINFO_EXTENSION); - - $fileName = uniqid() . '_' . slugify($fileName); - if (! empty($extension)) { - $fileName .= '.' . $extension; + $fileName = uniqid().'_'.slugify($fileName); + if (!empty($extension)) { + $fileName .= '.'.$extension; } - $request->file($groupName .'.'.$fieldName)->move( + $request->file($groupName.'.'.$fieldName)->move( $modelPath, $fileName ); - $requestValue = $modelFolder . $fileName; - } elseif (! empty($request->file($groupName .'.'.$fieldName)) && ! $request->file($groupName .'.'.$fieldName)->isValid()) { - throw new \Exception($request->file($groupName .'.'.$fieldName)->getErrorMessage()); + $requestValue = $modelFolder.$fileName; + } elseif (!empty($request->file($groupName.'.'.$fieldName)) && !$request->file($groupName.'.'.$fieldName)->isValid()) { + throw new \Exception($request->file($groupName.'.'.$fieldName)->getErrorMessage()); } //Avoid losing the existing filename if the user doesn't change it: - if (empty($requestValue) && (! empty($item->$fieldName))) { + if (empty($requestValue) && (!empty($item->$fieldName))) { $requestValue = $item->$fieldName; } return [ 'requestValue' => $requestValue, - 'skip' => $skip + 'skip' => $skip, ]; } } diff --git a/src/View/Composers/FormFieldComposer.php b/src/View/Composers/FormFieldComposer.php index 6659924..1abeec7 100644 --- a/src/View/Composers/FormFieldComposer.php +++ b/src/View/Composers/FormFieldComposer.php @@ -1,6 +1,5 @@ anavel->hasModule('Anavel\Uploads\UploadsModuleProvider'); $view->with([ - 'canTakeFileFromUploads' => $uploadsModuleIsInstalled + 'canTakeFileFromUploads' => $uploadsModuleIsInstalled, ]); } -} \ No newline at end of file +} diff --git a/tests/Abstractor/Eloquent/ModelFactoryTest.php b/tests/Abstractor/Eloquent/ModelFactoryTest.php index cc9a506..55103f2 100644 --- a/tests/Abstractor/Eloquent/ModelFactoryTest.php +++ b/tests/Abstractor/Eloquent/ModelFactoryTest.php @@ -19,7 +19,7 @@ class ModelFactoryTest extends TestBase protected $fieldMock; /** @var Mock */ protected $generatorMock; - /** @var Mock */ + /** @var Mock */ protected $anavelMock; public function setUp()