diff --git a/.travis.yml b/.travis.yml index 34a0767..e46d3d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ language: php php: - 5.5 - 5.6 - - 7.0 - hhvm before_script: diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..df2303f --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ + + +Copyright (c) 2016 A navalla suíza + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/config/anavel-crud.php b/config/anavel-crud.php index 1641a4e..cca22ff 100644 --- a/config/anavel-crud.php +++ b/config/anavel-crud.php @@ -43,7 +43,7 @@ 'date' => 'd/m/Y', 'time' => 'H:i', 'datetime' => 'd/m/Y H:i:s', - 'number' => '%.2f' + 'number' => '%.2f', ], /* @@ -143,7 +143,6 @@ ] */ ], - 'modelsGroups' => - [/*'Blog' => ['Blog Posts', 'Blog Tags']*/] + 'modelsGroups' => [/*'Blog' => ['Blog Posts', 'Blog Tags']*/], ]; diff --git a/lang/ca/messages.php b/lang/ca/messages.php index ea3fe2a..b259f26 100644 --- a/lang/ca/messages.php +++ b/lang/ca/messages.php @@ -22,5 +22,5 @@ 'alert_success_model_destroy_title' => 'Tot bé!', 'alert_success_model_destroy_text' => "L'element fou eliminat correctament", 'modal_delete_title' => 'Estàs segur?', - 'modal_delete_text' => 'Aquesta acció esborrarà permanentment aquest contingut' + 'modal_delete_text' => 'Aquesta acció esborrarà permanentment aquest contingut', ]; diff --git a/lang/en/messages.php b/lang/en/messages.php index 3b3aaea..256f73e 100644 --- a/lang/en/messages.php +++ b/lang/en/messages.php @@ -22,5 +22,5 @@ 'alert_success_model_destroy_title' => 'Everything OK!', 'alert_success_model_destroy_text' => 'The item was deleted successfully', 'modal_delete_title' => 'Are you sure?', - 'modal_delete_text' => 'This will permanently delete this content' + 'modal_delete_text' => 'This will permanently delete this content', ]; diff --git a/lang/es/messages.php b/lang/es/messages.php index e346c12..6b846bb 100644 --- a/lang/es/messages.php +++ b/lang/es/messages.php @@ -22,5 +22,5 @@ 'alert_success_model_destroy_title' => '¡Todo bien!', 'alert_success_model_destroy_text' => 'El elemento ha sido borrado correctamente', 'modal_delete_title' => 'Estás seguro/a?', - 'modal_delete_text' => 'Esto borrará permanentemente este contenido' + 'modal_delete_text' => 'Esto borrará permanentemente este contenido', ]; diff --git a/lang/gl/messages.php b/lang/gl/messages.php index 3588213..00d7768 100644 --- a/lang/gl/messages.php +++ b/lang/gl/messages.php @@ -22,5 +22,5 @@ 'alert_success_model_destroy_title' => 'Todo ben!', 'alert_success_model_destroy_text' => 'Borrouse perfectamente o que querías', 'modal_delete_title' => 'Estás seguro/a?', - 'modal_delete_text' => 'Así borrarás este contido para sempre' + 'modal_delete_text' => 'Así borrarás este contido para sempre', ]; diff --git a/public/js/app.js b/public/js/app.js index 4c67025..a75da2d 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -5,6 +5,6 @@ $(function () { //bootstrap WYSIHTML5 - text editor $(".bootstrap-wysihtml5").wysihtml5(); - $('select').selectize(); + $('select[multiple]').selectize(); $("input[type=date]").pikaday({ format: 'YYYY-MM-DD'}); }); \ No newline at end of file diff --git a/src/Abstractor/ConfigurationReader.php b/src/Abstractor/ConfigurationReader.php index 9cb6ce5..39d3001 100644 --- a/src/Abstractor/ConfigurationReader.php +++ b/src/Abstractor/ConfigurationReader.php @@ -1,12 +1,13 @@ config) || empty($this->config)) { - return null; + if (!property_exists($this, 'config') || !is_array($this->config) || empty($this->config)) { + return; } $params = func_get_args(); @@ -17,9 +18,10 @@ public function getConfigValue() if (is_array($params) && count($params) > 0) { foreach ($params as $configKey) { - if (! array_key_exists($configKey, $nestedConfig)) { - $nestedConfig = array(); - return null; + if (!array_key_exists($configKey, $nestedConfig)) { + $nestedConfig = []; + + return; } $nestedConfig = $nestedConfig[$configKey]; @@ -29,7 +31,5 @@ public function getConfigValue() if (array_key_exists($lastParam, $nestedConfig)) { return $nestedConfig[$lastParam]; } - - return null; } } diff --git a/src/Abstractor/Eloquent/Field.php b/src/Abstractor/Eloquent/Field.php index 3556427..9f810cc 100644 --- a/src/Abstractor/Eloquent/Field.php +++ b/src/Abstractor/Eloquent/Field.php @@ -1,4 +1,5 @@ formField = $formField; $this->name = $name; $this->presentation = $presentation; - $this->validationRules = array(); - $this->functions = array(); + $this->validationRules = []; + $this->functions = []; $this->options = []; $this->hideValue = false; $this->saveIfEmpty = true; @@ -44,7 +45,7 @@ public function __clone() { $formField = clone $this->formField; $this->formField = $formField; - $field = new Field($this->dbal, $formField, $this->name, $this->presentation); + $field = new self($this->dbal, $formField, $this->name, $this->presentation); return $field; } @@ -106,8 +107,8 @@ public function getValidationRulesArray() public function setFunctions($functions) { - if (! is_array($functions)) { - $functions = array($functions); + if (!is_array($functions)) { + $functions = [$functions]; } $this->functions = $functions; @@ -116,8 +117,8 @@ public function setFunctions($functions) public function applyFunctions($value) { foreach ($this->functions as $function) { - if (! function_exists($function)) { - throw new \Exception("Function ".$function." does not exist"); + if (!function_exists($function)) { + throw new \Exception('Function '.$function.' does not exist'); } $value = call_user_func($function, $value); @@ -128,13 +129,14 @@ public function applyFunctions($value) /** * @param string $value + * * @return void */ public function setValue($value) { $this->value = $value; - if (! $this->hideValue()) { + if (!$this->hideValue()) { $this->formField->val($this->value); } } @@ -149,6 +151,7 @@ public function getValue() /** * @param array $options + * * @return void */ public function setOptions(array $options) @@ -168,7 +171,7 @@ public function getOptions() public function getFormField() { - if (! $this->hideValue()) { + if (!$this->hideValue()) { if (Request::old($this->name)) { $this->formField->val(Request::old($this->name)); } @@ -177,36 +180,27 @@ public function getFormField() return $this->formField; } - /** - * - */ public function hideValue($value = null) { - if (! is_null($value)) { + if (!is_null($value)) { $this->hideValue = $value; } return $this->hideValue; } - /** - * - */ public function saveIfEmpty($value = null) { - if (! is_null($value)) { + if (!is_null($value)) { $this->saveIfEmpty = $value; } return $this->saveIfEmpty; } - /** - * - */ public function noValidate($value = null) { - if (! is_null($value)) { + if (!is_null($value)) { $this->noValidate = $value; } @@ -215,6 +209,7 @@ public function noValidate($value = null) /** * @param array $attributes + * * @return void */ public function setFormElementAttributes(array $attributes) diff --git a/src/Abstractor/Eloquent/FieldFactory.php b/src/Abstractor/Eloquent/FieldFactory.php index 03dbcff..31041a2 100644 --- a/src/Abstractor/Eloquent/FieldFactory.php +++ b/src/Abstractor/Eloquent/FieldFactory.php @@ -1,8 +1,9 @@ 'number', DbalType::SMALLINT => 'number', DbalType::BIGINT => 'number', @@ -44,17 +45,14 @@ class FieldFactory implements FieldAbstractorFactoryContract 'hidden', 'select', 'file', - 'money' - ); + 'money', + ]; public function __construct(FormManagerFactory $factory) { $this->factory = $factory; } - /** - * - */ public function setColumn(Column $column) { $this->column = $column; @@ -62,9 +60,6 @@ public function setColumn(Column $column) return $this; } - /** - * - */ public function setConfig(array $config) { $this->config = $config; @@ -72,16 +67,13 @@ public function setConfig(array $config) return $this; } - /** - * - */ public function get() { $formElement = $this->getFormElement(); $field = new Field($this->column, $formElement, $this->config['name'], $this->config['presentation']); - if (! empty($this->config['validation'])) { + if (!empty($this->config['validation'])) { if ($this->config['validation'] === 'no_validate') { $this->config['no_validate'] = true; } else { @@ -89,11 +81,11 @@ public function get() } } - if (! empty($this->config['functions'])) { + if (!empty($this->config['functions'])) { $field->setFunctions($this->config['functions']); } - if (! empty($this->config['no_validate']) && $this->config['no_validate'] === true) { + if (!empty($this->config['no_validate']) && $this->config['no_validate'] === true) { $field->noValidate(true); } @@ -107,7 +99,7 @@ public function get() if (in_array('required', $rules)) { $field->setFormElementAttributes( [ - 'required' => true + 'required' => true, ] ); } @@ -118,15 +110,15 @@ public function get() protected function getFormElement() { - if (! empty($this->config['form_type'])) { - if (! in_array($this->config['form_type'], $this->databaseTypeToFormType)) { - throw new FactoryException("Unknown form type " . $this->config['form_type']); + if (!empty($this->config['form_type'])) { + if (!in_array($this->config['form_type'], $this->databaseTypeToFormType)) { + throw new FactoryException('Unknown form type '.$this->config['form_type']); } $formElementType = $this->config['form_type']; } else { - if (! array_key_exists($this->column->getType()->getName(), $this->databaseTypeToFormType)) { - throw new FactoryException("No form type found for database type " . $this->column->getType()->getName()); + if (!array_key_exists($this->column->getType()->getName(), $this->databaseTypeToFormType)) { + throw new FactoryException('No form type found for database type '.$this->column->getType()->getName()); } $formElementType = $this->databaseTypeToFormType[$this->column->getType()->getName()]; @@ -136,10 +128,9 @@ protected function getFormElement() $formElementType = 'select'; } - $formElement = $this->factory->get($formElementType, []); - if (! empty($this->config['attr']) && is_array($this->config['attr'])) { + if (!empty($this->config['attr']) && is_array($this->config['attr'])) { $formElement->attr($this->config['attr']); } @@ -150,7 +141,7 @@ protected function getFormElement() } if ($formElementType === 'textarea') { - $formElement->class('form-control ' . config('anavel-crud.text_editor')); + $formElement->class('form-control '.config('anavel-crud.text_editor')); } if ($formElementType === 'checkbox') { @@ -158,7 +149,7 @@ protected function getFormElement() } if (isset($this->config['defaults'])) { - if (! is_array($this->config['defaults'])) { + if (!is_array($this->config['defaults'])) { $formElement->val(transcrud($this->config['defaults'])); } else { $defaults = []; diff --git a/src/Abstractor/Eloquent/Model.php b/src/Abstractor/Eloquent/Model.php index e8f12c0..a8edc9f 100644 --- a/src/Abstractor/Eloquent/Model.php +++ b/src/Abstractor/Eloquent/Model.php @@ -1,24 +1,23 @@ model = $config['model']; @@ -57,6 +62,7 @@ public function __construct( $this->relationFactory = $relationFactory; $this->fieldFactory = $fieldFactory; $this->generator = $generator; + $this->mustDeleteFilesInFilesystem = $mustDeleteFilesInFilesystem; } public function setSlug($slug) @@ -123,7 +129,6 @@ public function getColumns($action, $withForeignKeys = false) } $tableColumns = $filteredColumns; - $foreignKeysName = []; if ($withForeignKeys === false) { $foreignKeys = $this->dbal->getTableForeignKeys(); @@ -136,32 +141,32 @@ public function getColumns($action, $withForeignKeys = false) } $customDisplayedColumns = $this->getConfigValue($action, 'display'); - $customHiddenColumns = $this->getConfigValue($action, 'hide') ? : []; - - $relations = $this->getRelations(); + $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); - if (! $relations->has($customColumnRelation[0])) { - throw new AbstractorException("Relation " . $customColumnRelation[0] . " not configured on " . $this->getModel()); - } + $customColumnRelationFieldName = array_pop($customColumnRelation); - $relation = $relations->get($customColumnRelation[0]); + $modelAbstractor = $this; + foreach ($customColumnRelation as $relationName) { + $nestedRelation = $this->getNestedRelation($modelAbstractor, $relationName); + $modelAbstractor = $nestedRelation->getModelAbstractor(); + } - $relationColumns = $relation->getModelAbstractor()->getColumns($action); + $relationColumns = $nestedRelation->getModelAbstractor()->getColumns($action); - if (! array_key_exists($customColumnRelation[1], $relationColumns)) { - throw new AbstractorException("Column " . $customColumnRelation[1] . " does not exist on relation ".$customColumnRelation[0]. " 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[$customColumnRelation[1]]; + $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]; @@ -184,6 +189,23 @@ public function getColumns($action, $withForeignKeys = false) return $columns; } + protected function getNestedRelation(Model $modelAbstractor, $relationName) + { + $relations = $modelAbstractor->getRelations(); + + if (!$relations->has($relationName)) { + throw new AbstractorException('Relation '.$relationName.' not configured on '.$modelAbstractor->getModel()); + } + + $relation = $relations->get($relationName); + + if ($relation instanceof Relation) { + return $relation; + } else { + return $relation['relation']; + } + } + /** * @return \Illuminate\Support\Collection */ @@ -193,7 +215,7 @@ public function getRelations() $relations = collect(); - if (! empty($configRelations)) { + if (!empty($configRelations)) { foreach ($configRelations as $relationName => $configRelation) { if (is_int($relationName)) { $relationName = $configRelation; @@ -201,7 +223,7 @@ public function getRelations() $config = []; if ($configRelation !== $relationName) { - if (! is_array($configRelation)) { + if (!is_array($configRelation)) { $config['type'] = $configRelation; } else { $config = $configRelation; @@ -215,8 +237,7 @@ public function getRelations() $secondaryRelations = $relation->getSecondaryRelations(); - - if (! $secondaryRelations->isEmpty()) { + if (!$secondaryRelations->isEmpty()) { $relations->put( $relationName, collect(['relation' => $relation, 'secondaryRelations' => $secondaryRelations]) @@ -232,16 +253,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)) { @@ -253,7 +276,7 @@ public function getListFields($arrayKey = 'main') 'presentation' => $presentation, 'form_type' => null, 'validation' => null, - 'functions' => null + 'functions' => null, ]; $fields[$arrayKey][] = $this->fieldFactory @@ -267,16 +290,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)) { @@ -288,7 +313,7 @@ public function getDetailFields($arrayKey = 'main') 'presentation' => $presentation, 'form_type' => null, 'validation' => null, - 'functions' => null + 'functions' => null, ]; $fields[$arrayKey][] = $this->fieldFactory @@ -301,10 +326,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') { @@ -312,9 +339,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]; @@ -325,7 +352,7 @@ public function getEditFields($withForeignKeys = false, $arrayKey = 'main') 'presentation' => $presentation, 'form_type' => null, 'validation' => null, - 'functions' => null + 'functions' => null, ]; $config = $this->setConfig($config, $name); @@ -335,24 +362,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; } } } @@ -362,7 +389,7 @@ public function getEditFields($withForeignKeys = false, $arrayKey = 'main') protected function getReadOnlyColumns() { - $columns = [LaravelModel::CREATED_AT, LaravelModel::UPDATED_AT]; + $columns = [LaravelModel::CREATED_AT, LaravelModel::UPDATED_AT, 'deleted_at']; $columns[] = $this->dbal->getModel()->getKeyName(); @@ -371,6 +398,7 @@ protected function getReadOnlyColumns() /** * @param string $action + * * @return ElementInterface */ public function getForm($action) @@ -383,29 +411,29 @@ 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()); } - $fields = $this->getEditFields(true); $foreignFields = array_diff_key($fields['main'], $this->getEditFields(false)['main']); if (empty($fields['main']) && $this->getRelations()->isEmpty()) { 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; @@ -430,17 +458,16 @@ public function persist(Request $request) } if (get_class($field->getFormField()) === \FormManager\Fields\File::class) { - $handleResult = $this->handleField($request, $item, $fields['main'], 'main', $fieldName); - 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; } @@ -460,13 +487,15 @@ 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); - $relation->get('relation')->persist($input, $request); + /** @var $relationInstance Relation */ + $relationInstance = $relation->get('relation'); + $relationInstance->persist($input, $request); } else { + /* @var $relation Relation */ $relation->persist($request->input($relationKey), $request); } } @@ -490,23 +519,49 @@ public function getFieldValue($item, $fieldName) if (strpos($fieldName, '.')) { $customColumnRelation = explode('.', $fieldName); - $relation = $item->{$customColumnRelation[0]}; + $customColumnRelationFieldName = array_pop($customColumnRelation); + + $entity = $item; + $relation = null; + $relationName = ''; + foreach ($customColumnRelation as $relationName) { + $relation = $entity->{$relationName}; + if ($relation instanceof Collection) { + $entity = $relation->first(); + } else { + $entity = $relation; + } + } + + if (is_null($entity)) { + return; + } + $lastRelationName = $relationName; + + array_pop($customColumnRelation); + + $prevModelAbtractor = $this; + foreach ($customColumnRelation as $relationName) { + $nestedRelation = $this->getNestedRelation($prevModelAbtractor, $relationName); + $prevModelAbtractor = $nestedRelation->getModelAbstractor(); + } + if (empty($relation)) { - return null; + return; } - if ($relation instanceof \Illuminate\Support\Collection) { - $relations = $this->getRelations(); + if ($relation instanceof Collection) { + $relations = $prevModelAbtractor->getRelations(); - $relationAbstractor = $relations->get($customColumnRelation[0]); + $relationAbstractor = $relations->get($lastRelationName); if ($relationAbstractor instanceof \Anavel\Crud\Abstractor\Eloquent\Relation\Translation) { - $value = $item->getAttribute($customColumnRelation[1]); + $value = $entity->getAttribute($customColumnRelationFieldName); } else { - $value = $relation->implode($customColumnRelation[1], ', '); + $value = $relation->implode($customColumnRelationFieldName, ', '); } } else { - $value = $relation->getAttribute($customColumnRelation[1]); + $value = $relation->getAttribute($customColumnRelationFieldName); } } else { $value = $item->getAttribute($fieldName); @@ -514,4 +569,12 @@ public function getFieldValue($item, $fieldName) return $value; } + + /** + * @return bool + */ + public function mustDeleteFilesInFilesystem() + { + return $this->mustDeleteFilesInFilesystem; + } } diff --git a/src/Abstractor/Eloquent/ModelFactory.php b/src/Abstractor/Eloquent/ModelFactory.php index ba465b0..536798c 100644 --- a/src/Abstractor/Eloquent/ModelFactory.php +++ b/src/Abstractor/Eloquent/ModelFactory.php @@ -1,13 +1,15 @@ allConfiguredModels = $allConfiguredModels; $this->modelManager = $modelManager; @@ -36,13 +54,16 @@ public function __construct( $this->fieldFactory = $fieldFactory; $this->slugger = new Slugger(); $this->generator = $generator; + $this->anavel = $anavel; } /** * @param $slug * @param null $id - * @return Model|null + * * @throws \Exception + * + * @return Model|null */ public function getBySlug($slug, $id = null) { @@ -58,7 +79,9 @@ public function getBySlug($slug, $id = null) $modelNamespace = $config; } - $model = new Model($config, $this->modelManager->getAbstractionLayer($modelNamespace), $this->relationFactory, $this->fieldFactory, $this->generator); + $model = new Model($config, $this->modelManager->getAbstractionLayer($modelNamespace), + $this->relationFactory, $this->fieldFactory, $this->generator, + !$this->anavel->hasModule('Anavel\Uploads\UploadsModuleProvider')); $model->setSlug($modelSlug) ->setName($modelName); @@ -75,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; @@ -86,13 +109,11 @@ 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); $model->setSlug($this->slugger->slugify((new ReflectionClass($classname))->getShortName())); - if (is_null($id)) { $model->setInstance($this->modelManager->getModelInstance($classname)); } else { diff --git a/src/Abstractor/Eloquent/Relation/MiniCrud.php b/src/Abstractor/Eloquent/Relation/MiniCrud.php index a2320c5..d22d711 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; } @@ -53,11 +54,9 @@ public function getEditFields($arrayKey = null) $fieldsBase = $this->getEditFieldsBase(); - /** @var Collection $results */ $results = $this->getResults(); - $results->put('emptyResult', ''); if (!empty($fieldsBase)) { foreach ($results as $key => $result) { @@ -80,7 +79,6 @@ public function getEditFields($arrayKey = null) $tempFields[$columnName] = $field; } - $relationModel = $this->eloquentRelation->getRelated()->newInstance(); if (!empty($result)) { $relationModel = $result; @@ -97,7 +95,7 @@ public function getEditFields($arrayKey = null) } else { $tempFields[$editGroupName] = $editGroup; } - }; + } } } @@ -115,16 +113,16 @@ public function getEditFieldsBase() $this->readConfig('edit'); if (!empty($columns)) { - $readOnly = [Model::CREATED_AT, Model::UPDATED_AT]; + $readOnly = [Model::CREATED_AT, Model::UPDATED_AT, 'deleted_at']; //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 */ @@ -134,7 +132,6 @@ public function getEditFieldsBase() ->get(); $fields['__delete'] = $field; - foreach ($columns as $columnName => $column) { if (in_array($columnName, $readOnly, true)) { continue; @@ -146,12 +143,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 +165,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 +183,7 @@ public function getEditFieldsBase() /** * @param array|null $relationArray + * * @return mixed */ public function persist(array $relationArray = null, Request $request) @@ -209,7 +207,6 @@ public function persist(array $relationArray = null, Request $request) $this->modelAbstractor->setInstance($relationModel); $secondaryRelations = $this->getSecondaryRelations(); - $this->setKeys($relationModel); $shouldBeSkipped = true; @@ -220,12 +217,13 @@ public function persist(array $relationArray = null, Request $request) $fieldName = $field->getName(); if (get_class($field->getFormField()) === \FormManager\Fields\File::class) { - $handleResult = $this->handleField($request, $relationModel, $fieldsBase, $this->name . ".$relationIndex", $fieldName); - if (! empty($handleResult['skip'])) { + $handleResult = $this->handleField($request, $relationModel, $fieldsBase, + $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']; } } @@ -240,7 +238,6 @@ public function persist(array $relationArray = null, Request $request) } } - foreach ($relation as $fieldKey => $fieldValue) { if ($secondaryRelations->has($fieldKey)) { $delayedRelations->put($fieldKey, $fieldValue); @@ -305,6 +302,7 @@ public function getDisplayType() /** * @param array $fields + * * @return array */ public function addSecondaryRelationFields(array $fields) @@ -317,7 +315,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/Relation/MiniCrudPolymorphic.php b/src/Abstractor/Eloquent/Relation/MiniCrudPolymorphic.php index 7c029f2..0a1b704 100644 --- a/src/Abstractor/Eloquent/Relation/MiniCrudPolymorphic.php +++ b/src/Abstractor/Eloquent/Relation/MiniCrudPolymorphic.php @@ -1,14 +1,14 @@ eloquentRelation->getParent()->getKeyName())) { return true; } + return false; } diff --git a/src/Abstractor/Eloquent/Relation/MiniCrudSingle.php b/src/Abstractor/Eloquent/Relation/MiniCrudSingle.php index a614000..7ce945d 100644 --- a/src/Abstractor/Eloquent/Relation/MiniCrudSingle.php +++ b/src/Abstractor/Eloquent/Relation/MiniCrudSingle.php @@ -1,20 +1,19 @@ eloquentRelation->getPlainForeignKey(), $this->eloquentRelation->getPlainMorphType(), - $this->eloquentRelation->getParent()->getKeyName() + $this->eloquentRelation->getParent()->getKeyName(), ]; - $this->readConfig('edit'); - if (! empty($columns)) { + if (!empty($columns)) { //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 */ @@ -74,11 +72,11 @@ public function getEditFields($arrayKey = null) $config = [ 'name' => $columnName, - 'presentation' => $this->name . ' ' . ucfirst(transcrud($columnName)), + 'presentation' => $this->name.' '.ucfirst(transcrud($columnName)), 'form_type' => $formType, 'no_validate' => true, 'validation' => null, - 'functions' => null + 'functions' => null, ]; $config = $this->setConfig($config, $columnName); @@ -89,7 +87,7 @@ public function getEditFields($arrayKey = null) ->setConfig($config) ->get(); - if (! empty($result->id)) { + if (!empty($result->id)) { $field->setValue($result->getAttribute($columnName)); } @@ -99,19 +97,19 @@ public function getEditFields($arrayKey = null) $fields = $this->addSecondaryRelationFields($fields); - return $fields; } /** * @param array|null $relationArray + * * @return mixed */ public function persist(array $relationArray = null, Request $request) { - if (! empty($relationArray)) { + if (!empty($relationArray)) { $currentRelation = $this->eloquentRelation->getResults(); - if (! empty($currentRelation)) { + if (!empty($currentRelation)) { $relationModel = $currentRelation; } else { $relationModel = $this->eloquentRelation->getRelated()->newInstance(); @@ -133,7 +131,7 @@ public function persist(array $relationArray = null, Request $request) $relationModel->setAttribute($fieldKey, $fieldValue); } - if (! $shouldBeSkipped) { + if (!$shouldBeSkipped) { $relationModel->save(); } } diff --git a/src/Abstractor/Eloquent/Relation/Relation.php b/src/Abstractor/Eloquent/Relation/Relation.php index 0702f94..9d56291 100644 --- a/src/Abstractor/Eloquent/Relation/Relation.php +++ b/src/Abstractor/Eloquent/Relation/Relation.php @@ -1,15 +1,16 @@ config = $config; $this->setup(); - $this->modelAbstractor = \App::make('Anavel\Crud\Contracts\Abstractor\ModelFactory')->getByClassName(get_class($this->eloquentRelation->getRelated()), $this->config); + $relatedModelClassName = get_class($this->eloquentRelation->getRelated()); + $relatedmodelRelationsConfig = []; + + foreach (config('anavel-crud.models') as $modelConfig) { + if (is_array($modelConfig) && array_key_exists('model', $modelConfig) && $relatedModelClassName == $modelConfig['model']) { + if (array_key_exists('relations', $modelConfig)) { + $relatedmodelRelationsConfig['relations'] = $modelConfig['relations']; + } + } + } + + $config = array_merge($this->config, $relatedmodelRelationsConfig); + + $this->modelAbstractor = \App::make('Anavel\Crud\Contracts\Abstractor\ModelFactory')->getByClassName(get_class($this->eloquentRelation->getRelated()), $config); } public function addSecondaryRelationFields(array $fields) @@ -68,8 +82,9 @@ public function addSecondaryRelationFields(array $fields) /** @var RelationAbstractorContract $relation */ foreach ($relation->getEditFields($relationKey) as $editGroupName => $editGroup) { $fields[$this->name][$editGroupName] = $editGroup; - }; + } } + return $fields; } @@ -79,14 +94,13 @@ public function getName() } /** - * return null|string + * return null|string. */ public function getDisplay() { - if (! empty($this->config['display'])) { + if (!empty($this->config['display'])) { return $this->config['display']; } - return null; } public function getPresentation() @@ -122,6 +136,7 @@ public function getSecondaryRelations() /** * @param Model $relatedModel + * * @return Relation */ public function setRelatedModel(Model $relatedModel) diff --git a/src/Abstractor/Eloquent/Relation/Select.php b/src/Abstractor/Eloquent/Relation/Select.php index 8a5e79d..e4d9330 100644 --- a/src/Abstractor/Eloquent/Relation/Select.php +++ b/src/Abstractor/Eloquent/Relation/Select.php @@ -1,8 +1,8 @@ getAttribute($display); } + return $displayString; } + return $result->getAttribute($this->config['display']); } @@ -103,11 +109,11 @@ protected function setDisplay($result) protected function getConfig() { return [ - 'name' => $this->eloquentRelation->getForeignKey(), + 'name' => $this->eloquentRelation->getForeignKey(), 'presentation' => $this->getPresentation(), - 'form_type' => 'select', - 'validation' => null, - 'functions' => null + 'form_type' => 'select', + 'validation' => null, + 'functions' => null, ]; } @@ -115,14 +121,16 @@ protected function setFieldValue($field) { $results = $this->eloquentRelation->getResults(); - if (! empty($results)) { + if (!empty($results)) { $field->setValue($results->getKey()); } + return $field; } /** * @param \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal + * * @return Column */ protected function getColumn($dbal) diff --git a/src/Abstractor/Eloquent/Relation/SelectMultiple.php b/src/Abstractor/Eloquent/Relation/SelectMultiple.php index da21d05..95ab262 100644 --- a/src/Abstractor/Eloquent/Relation/SelectMultiple.php +++ b/src/Abstractor/Eloquent/Relation/SelectMultiple.php @@ -1,10 +1,9 @@ modelManager->getRepository(get_class($this->eloquentRelation->getRelated())); @@ -37,7 +37,7 @@ public function persist(array $relationArray = null, Request $request) $alreadyAssociated = $this->relatedModel->$relationName; $search = []; - if (! empty($relationArray[$relatedKeyName])) { + if (!empty($relationArray[$relatedKeyName])) { $search = $relationArray[$relatedKeyName]; } $results = $repo->pushCriteria( @@ -46,14 +46,13 @@ public function persist(array $relationArray = null, Request $request) $missing = $alreadyAssociated->diff($results); - $keyName = $this->eloquentRelation->getPlainForeignKey(); foreach ($results as $result) { $result->$keyName = $this->relatedModel->getKey(); $result->save(); } - if (! $missing->isEmpty()) { + if (!$missing->isEmpty()) { foreach ($missing as $result) { $result->$keyName = null; $result->save(); @@ -62,8 +61,6 @@ public function persist(array $relationArray = null, Request $request) } } - - protected function addToArray($arrayKey, $field) { $select = []; @@ -71,6 +68,7 @@ protected function addToArray($arrayKey, $field) $arrayKey = $this->name; } $select[$arrayKey][] = $field; + return $select; } @@ -80,25 +78,23 @@ protected function addToArray($arrayKey, $field) protected function getConfig() { return [ - 'name' => $this->eloquentRelation->getRelated()->getKeyName(), + 'name' => $this->eloquentRelation->getRelated()->getKeyName(), 'presentation' => $this->getPresentation(), - 'form_type' => 'select', - 'attr' => [ - 'multiple' => true + 'form_type' => 'select', + 'attr' => [ + 'multiple' => true, ], 'no_validate' => true, - 'validation' => null, - 'functions' => null + 'validation' => null, + 'functions' => null, ]; } - - protected function setFieldValue($field) { $results = $this->eloquentRelation->getResults(); - if (! $results->isEmpty()) { + if (!$results->isEmpty()) { $values = []; foreach ($results as $result) { @@ -107,12 +103,13 @@ protected function setFieldValue($field) $field->setValue($values); } + return $field; } - /** * @param \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal + * * @return Column */ protected function getColumn($dbal) diff --git a/src/Abstractor/Eloquent/Relation/SelectMultipleManyToMany.php b/src/Abstractor/Eloquent/Relation/SelectMultipleManyToMany.php index 05d8a44..2b00fab 100644 --- a/src/Abstractor/Eloquent/Relation/SelectMultipleManyToMany.php +++ b/src/Abstractor/Eloquent/Relation/SelectMultipleManyToMany.php @@ -1,10 +1,8 @@ eloquentRelation->getRelated()->getKeyName()]; if (in_array('', $array)) { $array = []; @@ -39,6 +38,7 @@ public function persist(array $relationArray = null, Request $request) /** * @param \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal + * * @return Column */ protected function getColumn($dbal) diff --git a/src/Abstractor/Eloquent/Relation/Traits/CheckRelationCompatibility.php b/src/Abstractor/Eloquent/Relation/Traits/CheckRelationCompatibility.php index d33ea7d..6c6d484 100644 --- a/src/Abstractor/Eloquent/Relation/Traits/CheckRelationCompatibility.php +++ b/src/Abstractor/Eloquent/Relation/Traits/CheckRelationCompatibility.php @@ -1,4 +1,5 @@ eloquentRelation), $this->compatibleEloquentRelations)) { - throw new RelationException(get_class($this->eloquentRelation)." eloquent relation is not compatible with ".$this->getType()." type"); + if (!in_array(get_class($this->eloquentRelation), $this->compatibleEloquentRelations)) { + throw new RelationException(get_class($this->eloquentRelation).' eloquent relation is not compatible with '.$this->getType().' type'); } } } diff --git a/src/Abstractor/Eloquent/Relation/Traits/CheckRelationConfig.php b/src/Abstractor/Eloquent/Relation/Traits/CheckRelationConfig.php index 7cf9482..5d8ba4c 100644 --- a/src/Abstractor/Eloquent/Relation/Traits/CheckRelationConfig.php +++ b/src/Abstractor/Eloquent/Relation/Traits/CheckRelationConfig.php @@ -1,4 +1,5 @@ langs as $key => $lang) { $tempFields = []; foreach ($columns as $columnName => $column) { @@ -66,11 +66,11 @@ public function getEditFields($arrayKey = null) $config = [ 'name' => $columnName, - 'presentation' => ucfirst(transcrud($columnName)) . ' [' . $lang . ']', + 'presentation' => ucfirst(transcrud($columnName)).' ['.$lang.']', 'form_type' => $formType, 'no_validate' => true, 'validation' => null, - 'functions' => null + 'functions' => null, ]; $config = $this->setConfig($config, $columnName); @@ -102,11 +102,12 @@ public function getEditFields($arrayKey = null) /** * @param array|null $relationArray + * * @return mixed */ public function persist(array $relationArray = null, Request $request) { - if (! empty($relationArray)) { + if (!empty($relationArray)) { $currentTranslations = $this->eloquentRelation->getResults(); $currentTranslations = $currentTranslations->keyBy('locale'); diff --git a/src/Abstractor/Eloquent/RelationFactory.php b/src/Abstractor/Eloquent/RelationFactory.php index 8308627..633c655 100644 --- a/src/Abstractor/Eloquent/RelationFactory.php +++ b/src/Abstractor/Eloquent/RelationFactory.php @@ -1,11 +1,12 @@ self::SELECT, 'Illuminate\Database\Eloquent\Relations\BelongsToMany' => self::SELECT_MULTIPLE_MANY_TO_MANY, 'Illuminate\Database\Eloquent\Relations\HasMany' => self::SELECT_MULTIPLE, @@ -26,10 +27,10 @@ class RelationFactory implements RelationAbstractorFactoryContract 'Illuminate\Database\Eloquent\Relations\HasOne' => self::SELECT, 'Illuminate\Database\Eloquent\Relations\HasOneOrMany' => self::SELECT_MULTIPLE, 'Illuminate\Database\Eloquent\Relations\MorphMany' => self::MINI_CRUD_POLYMORPHIC, - 'Illuminate\Database\Eloquent\Relations\MorphOne' => self::MINI_CRUD_SINGLE - ); + 'Illuminate\Database\Eloquent\Relations\MorphOne' => self::MINI_CRUD_SINGLE, + ]; - protected $typesMap = array( + protected $typesMap = [ self::SELECT => 'Anavel\Crud\Abstractor\Eloquent\Relation\Select', self::SELECT_MULTIPLE => 'Anavel\Crud\Abstractor\Eloquent\Relation\SelectMultiple', self::SELECT_MULTIPLE_MANY_TO_MANY => 'Anavel\Crud\Abstractor\Eloquent\Relation\SelectMultipleManyToMany', @@ -37,8 +38,8 @@ class RelationFactory implements RelationAbstractorFactoryContract self::MINI_CRUD => 'Anavel\Crud\Abstractor\Eloquent\Relation\MiniCrud', self::MINI_CRUD_SINGLE => 'Anavel\Crud\Abstractor\Eloquent\Relation\MiniCrudSingle', self::MINI_CRUD_POLYMORPHIC => 'Anavel\Crud\Abstractor\Eloquent\Relation\MiniCrudPolymorphic', - self::TRANSLATION => 'Anavel\Crud\Abstractor\Eloquent\Relation\Translation' - ); + self::TRANSLATION => 'Anavel\Crud\Abstractor\Eloquent\Relation\Translation', + ]; protected $modelManager; protected $fieldFactory; @@ -53,7 +54,7 @@ public function __construct(ModelManager $modelManager, FieldFactoryContract $fi { $this->modelManager = $modelManager; $this->fieldFactory = $fieldFactory; - $this->config = array(); + $this->config = []; } public function setModel($model) @@ -72,20 +73,19 @@ public function setConfig(array $config) public function get($name) { - if (! empty($this->config) && ! empty($this->config['name'])) { + if (!empty($this->config) && !empty($this->config['name'])) { $name = $this->config['name']; } - if (! method_exists($this->model, $name)) { - throw new FactoryException("Relation " . $name . " does not exist on " . get_class($this->model)); + if (!method_exists($this->model, $name)) { + throw new FactoryException('Relation '.$name.' does not exist on '.get_class($this->model)); } $relationInstance = $this->model->$name(); $relationEloquentType = get_class($relationInstance); - if (empty($this->config['type'])) { - if (! array_key_exists($relationEloquentType, $this->eloquentTypeToRelationType)) { - throw new FactoryException($relationEloquentType . " relation not supported"); + if (!array_key_exists($relationEloquentType, $this->eloquentTypeToRelationType)) { + throw new FactoryException($relationEloquentType.' relation not supported'); } $type = $this->eloquentTypeToRelationType[$relationEloquentType]; @@ -93,8 +93,8 @@ public function get($name) $type = $this->config['type']; } - if (! array_key_exists($type, $this->typesMap)) { - throw new FactoryException("Unexpected relation type: " . $type); + if (!array_key_exists($type, $this->typesMap)) { + throw new FactoryException('Unexpected relation type: '.$type); } $this->config['name'] = $name; diff --git a/src/Abstractor/Eloquent/Traits/HandleFiles.php b/src/Abstractor/Eloquent/Traits/HandleFiles.php index 2a127d4..cd31a0a 100644 --- a/src/Abstractor/Eloquent/Traits/HandleFiles.php +++ b/src/Abstractor/Eloquent/Traits/HandleFiles.php @@ -1,72 +1,88 @@ 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)) { + if ($filesystem->has($item->$fieldName) && $mustDeleteFilesInFilesystem) { $filesystem->delete($item->$fieldName); } - $item->setAttribute( $fieldName, null ); + return [ - 'skip' => $skip + 'skip' => $skip, ]; } } - if ($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; - } + 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); + $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/Abstractor/Eloquent/Traits/ModelFields.php b/src/Abstractor/Eloquent/Traits/ModelFields.php index 7b1ef1e..7c84e20 100644 --- a/src/Abstractor/Eloquent/Traits/ModelFields.php +++ b/src/Abstractor/Eloquent/Traits/ModelFields.php @@ -1,10 +1,7 @@ fieldsPresentation = $this->getConfigValue('fields_presentation') ? : []; - $this->formTypes = $this->getConfigValue($action, 'form_types') ? : []; - $this->validationRules = $this->getConfigValue($action, 'validation') ? : []; - $this->functions = $this->getConfigValue($action, 'functions') ? : []; - $this->defaults = $this->getConfigValue($action, 'defaults') ? : []; + $this->fieldsPresentation = $this->getConfigValue('fields_presentation') ?: []; + $this->formTypes = $this->getConfigValue($action, 'form_types') ?: []; + $this->validationRules = $this->getConfigValue($action, 'validation') ?: []; + $this->functions = $this->getConfigValue($action, 'functions') ?: []; + $this->defaults = $this->getConfigValue($action, 'defaults') ?: []; } /** - * @param array $config + * @param array $config * @param string $columnName + * * @return array */ public function setConfig(array $config, $columnName) diff --git a/src/Abstractor/Exceptions/AbstractorException.php b/src/Abstractor/Exceptions/AbstractorException.php index 4d93f21..b4765ea 100644 --- a/src/Abstractor/Exceptions/AbstractorException.php +++ b/src/Abstractor/Exceptions/AbstractorException.php @@ -1,4 +1,5 @@ app->bind( 'Anavel\Crud\Contracts\Abstractor\FieldFactory', function () { - return new FieldAbstractorFactory(new FormFactory); + return new FieldAbstractorFactory(new FormFactory()); } ); @@ -71,7 +71,8 @@ function () { $this->app['ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager'], $this->app['Anavel\Crud\Contracts\Abstractor\RelationFactory'], $this->app['Anavel\Crud\Contracts\Abstractor\FieldFactory'], - $this->app['Anavel\Crud\Contracts\Form\Generator'] + $this->app['Anavel\Crud\Contracts\Form\Generator'], + $this->app['Anavel\Foundation\Contracts\Anavel'] ); } ); @@ -79,7 +80,7 @@ function () { $this->app->bind( 'Anavel\Crud\Contracts\Form\Generator', function () { - return new FormGenerator(new FormFactory); + return new FormGenerator(new FormFactory()); } ); @@ -135,7 +136,7 @@ public function isActive() } /** - * Registers types that Doctrine doesn's support by default + * Registers types that Doctrine doesn's support by default. */ protected function registerDoctrineTypeMappings() { diff --git a/src/Http/Controllers/HomeController.php b/src/Http/Controllers/HomeController.php index 6da1df6..877ffc5 100644 --- a/src/Http/Controllers/HomeController.php +++ b/src/Http/Controllers/HomeController.php @@ -1,11 +1,12 @@ $model) { $modelSlug = Slugger::slugify($modelName); $modelAbstractor = $modelFactory->getByName($modelSlug); $config = $modelAbstractor->getConfig(); - if (! array_key_exists('authorize', $config) || ($config['authorize'] === true && Gate::allows('adminIndex', $modelAbstractor->getInstance()) || $config['authorize'] === false)) { + if (!array_key_exists('authorize', $config) || ($config['authorize'] === true && Gate::allows('adminIndex', $modelAbstractor->getInstance()) || $config['authorize'] === false)) { return new RedirectResponse(route('anavel-crud.model.index', $modelSlug)); } } diff --git a/src/Http/Controllers/ModelController.php b/src/Http/Controllers/ModelController.php index 1da4422..76bf964 100644 --- a/src/Http/Controllers/ModelController.php +++ b/src/Http/Controllers/ModelController.php @@ -1,16 +1,17 @@ getConfig()) && (! empty($config['controller']))) { + if (!$this instanceof CustomController) { //Avoid infinite recursion + if (array_key_exists('controller', $config = $modelAbstractor->getConfig()) && (!empty($config['controller']))) { /** @var CustomController $controller */ $controller = App::make($config['controller']); $controller->setAbstractor($modelAbstractor); + return $controller; } } - return null; } /** * Display a listing of the resource. * * @param Request $request - * @param string $model + * @param string $model + * * @return Response */ public function index(Request $request, $model) @@ -62,14 +65,14 @@ public function index(Request $request, $model) $this->authorizeMethod($modelAbstractor, 'adminIndex'); - if (! empty($customController = $this->customController($modelAbstractor))) { + if (!empty($customController = $this->customController($modelAbstractor))) { return $customController->index($request, $model); } $repository = $this->modelManager->getRepository($modelAbstractor->getModel()); if ($request->has('search')) { - $searchByColumns = array(); + $searchByColumns = []; foreach ($modelAbstractor->getListFields()['main'] as $field) { $searchByColumns[] = $field->getName(); @@ -86,14 +89,15 @@ public function index(Request $request, $model) return view('anavel-crud::pages.index', [ 'abstractor' => $modelAbstractor, - 'items' => $items + 'items' => $items, ]); } /** * Show the form for creating a new resource. * - * @param string $model + * @param string $model + * * @return Response */ public function create($model) @@ -102,7 +106,7 @@ public function create($model) $this->authorizeMethod($modelAbstractor, 'adminCreate'); - if (! empty($customController = $this->customController($modelAbstractor))) { + if (!empty($customController = $this->customController($modelAbstractor))) { return $customController->create($model); } @@ -110,8 +114,8 @@ public function create($model) return view('anavel-crud::pages.create', [ 'abstractor' => $modelAbstractor, - 'form' => $form, - 'relations' => $modelAbstractor->getRelations() + 'form' => $form, + 'relations' => $modelAbstractor->getRelations(), ]); } @@ -119,7 +123,8 @@ public function create($model) * Store a newly created resource in storage. * * @param Request $request - * @param string $model + * @param string $model + * * @return Response */ public function store(Request $request, $model) @@ -128,7 +133,7 @@ public function store(Request $request, $model) $this->authorizeMethod($modelAbstractor, 'adminCreate'); - if (! empty($customController = $this->customController($modelAbstractor))) { + if (!empty($customController = $this->customController($modelAbstractor))) { return $customController->store($request, $model); } @@ -143,7 +148,7 @@ public function store(Request $request, $model) 'type' => 'success', 'icon' => 'fa-check', 'title' => trans('anavel-crud::messages.alert_success_model_store_title'), - 'text' => trans('anavel-crud::messages.alert_success_model_store_text') + 'text' => trans('anavel-crud::messages.alert_success_model_store_text'), ]); return redirect()->route('anavel-crud.model.index', $model); @@ -152,8 +157,9 @@ public function store(Request $request, $model) /** * Display the specified resource. * - * @param string $model - * @param int $id + * @param string $model + * @param int $id + * * @return Response */ public function show($model, $id) @@ -162,7 +168,7 @@ public function show($model, $id) $this->authorizeMethod($modelAbstractor, 'adminShow'); - if (! empty($customController = $this->customController($modelAbstractor))) { + if (!empty($customController = $this->customController($modelAbstractor))) { return $customController->show($model, $id); } @@ -171,15 +177,16 @@ public function show($model, $id) return view('anavel-crud::pages.show', [ 'abstractor' => $modelAbstractor, - 'item' => $item + 'item' => $item, ]); } /** * Show the form for editing the specified resource. * - * @param string $model - * @param int $id + * @param string $model + * @param int $id + * * @return Response */ public function edit($model, $id) @@ -189,7 +196,7 @@ public function edit($model, $id) $this->authorizeMethod($modelAbstractor, 'adminUpdate'); - if (! empty($customController = $this->customController($modelAbstractor))) { + if (!empty($customController = $this->customController($modelAbstractor))) { return $customController->edit($model, $id); } @@ -199,9 +206,9 @@ public function edit($model, $id) return view('anavel-crud::pages.edit', [ 'abstractor' => $modelAbstractor, - 'form' => $form, - 'item' => $item, - 'relations' => $modelAbstractor->getRelations() + 'form' => $form, + 'item' => $item, + 'relations' => $modelAbstractor->getRelations(), ]); } @@ -209,8 +216,9 @@ public function edit($model, $id) * Update the specified resource in storage. * * @param Request $request - * @param string $model - * @param int $id + * @param string $model + * @param int $id + * * @return Response */ public function update(Request $request, $model, $id) @@ -219,7 +227,7 @@ public function update(Request $request, $model, $id) $this->authorizeMethod($modelAbstractor, 'adminUpdate'); - if (! empty($customController = $this->customController($modelAbstractor))) { + if (!empty($customController = $this->customController($modelAbstractor))) { return $customController->update($request, $model, $id); } @@ -234,7 +242,7 @@ public function update(Request $request, $model, $id) 'type' => 'success', 'icon' => 'fa-check', 'title' => trans('anavel-crud::messages.alert_success_model_update_title'), - 'text' => trans('anavel-crud::messages.alert_success_model_update_text') + 'text' => trans('anavel-crud::messages.alert_success_model_update_text'), ]); return redirect()->route('anavel-crud.model.edit', [$model, $id]); @@ -244,8 +252,9 @@ public function update(Request $request, $model, $id) * Remove the specified resource from storage. * * @param Request $request - * @param string $model - * @param int $id + * @param string $model + * @param int $id + * * @return Response */ public function destroy(Request $request, $model, $id) @@ -254,7 +263,7 @@ public function destroy(Request $request, $model, $id) $this->authorizeMethod($modelAbstractor, 'adminDestroy'); - if (! empty($customController = $this->customController($modelAbstractor))) { + if (!empty($customController = $this->customController($modelAbstractor))) { return $customController->destroy($request, $model, $id); } @@ -267,7 +276,7 @@ public function destroy(Request $request, $model, $id) 'type' => 'success', 'icon' => 'fa-check', 'title' => trans('anavel-crud::messages.alert_success_model_destroy_title'), - 'text' => trans('anavel-crud::messages.alert_success_model_destroy_text') + 'text' => trans('anavel-crud::messages.alert_success_model_destroy_text'), ]); return redirect()->route('anavel-crud.model.index', $model); diff --git a/src/Http/Form/Generator.php b/src/Http/Form/Generator.php index 10b96ed..f2acab0 100644 --- a/src/Http/Form/Generator.php +++ b/src/Http/Form/Generator.php @@ -1,20 +1,14 @@ 'number', DbalType::STRING => 'text', DbalType::TEXT => 'textarea', @@ -43,13 +37,13 @@ class Generator implements GeneratorContract 'email', 'password', 'hidden', - 'select' - ); + 'select', + ]; public function __construct(FactoryInterface $factory) { $this->factory = $factory; - $this->fields = array(); + $this->fields = []; } public function setModelFields(array $fields) @@ -90,11 +84,10 @@ public function getForm($action) 'action' => $action, 'enctype' => 'multipart/form-data', 'method' => 'post', - 'class' => 'form-horizontal' + 'class' => 'form-horizontal', ]); - - $formFields = array(); + $formFields = []; foreach ($this->fields as $fieldGroupName => $fieldGroup) { $tempFields = $this->addFormFields($fieldGroup, $fieldGroupName); $formFields[key($tempFields)] = $this->factory->get('group', [$tempFields[key($tempFields)]]); @@ -102,13 +95,12 @@ public function getForm($action) $form->add($formFields); - return $form; } public function getValidationRules() { - $rules = array(); + $rules = []; foreach ($this->fields as $fieldGroupName => $fieldGroup) { foreach ($fieldGroup as $fieldKey => $field) { @@ -126,12 +118,13 @@ public function getValidationRules() /** * @param array $fields * @param $key + * * @return array */ protected function addFormFields(array $fields, $key) { - $formFields = array(); - $tempFields = array(); + $formFields = []; + $tempFields = []; foreach ($fields as $fieldKey => $field) { if (is_array($field)) { @@ -142,13 +135,14 @@ protected function addFormFields(array $fields, $key) } } $formFields[$key] = $tempFields; + return $formFields; } protected function addValidationRules(array $fields, $key) { - $rules = array(); - $tempFields = array(); + $rules = []; + $tempFields = []; foreach ($fields as $fieldKey => $field) { if (is_array($field)) { $group = $this->addValidationRules($field, $fieldKey); @@ -158,6 +152,7 @@ protected function addValidationRules(array $fields, $key) } } $rules[$key] = $tempFields; + return $rules; } } diff --git a/src/Http/routes.php b/src/Http/routes.php index 31a1e61..ad796d9 100644 --- a/src/Http/routes.php +++ b/src/Http/routes.php @@ -2,49 +2,49 @@ Route::group( [ - 'prefix' => 'crud', - 'namespace' => 'Anavel\Crud\Http\Controllers' + 'prefix' => 'crud', + 'namespace' => 'Anavel\Crud\Http\Controllers', ], function () { Route::get('/', [ 'as' => 'anavel-crud.home', - 'uses' => 'HomeController@index' + 'uses' => 'HomeController@index', ]); // Model CRUD routes Route::get('{model}', [ 'as' => 'anavel-crud.model.index', - 'uses' => 'ModelController@index' + 'uses' => 'ModelController@index', ]); Route::get('{model}/create', [ 'as' => 'anavel-crud.model.create', - 'uses' => 'ModelController@create' + 'uses' => 'ModelController@create', ]); Route::post('{model}', [ 'as' => 'anavel-crud.model.store', - 'uses' => 'ModelController@store' + 'uses' => 'ModelController@store', ]); Route::get('{model}/{id}', [ 'as' => 'anavel-crud.model.show', - 'uses' => 'ModelController@show' + 'uses' => 'ModelController@show', ]); Route::get('{model}/{id}/edit', [ 'as' => 'anavel-crud.model.edit', - 'uses' => 'ModelController@edit' + 'uses' => 'ModelController@edit', ]); Route::put('{model}/{id}', [ 'as' => 'anavel-crud.model.update', - 'uses' => 'ModelController@update' + 'uses' => 'ModelController@update', ]); Route::delete('{model}/{id}', [ 'as' => 'anavel-crud.model.destroy', - 'uses' => 'ModelController@destroy' + 'uses' => 'ModelController@destroy', ]); // Batch actions diff --git a/src/Providers/ViewComposersServiceProvider.php b/src/Providers/ViewComposersServiceProvider.php index 3737143..728a756 100644 --- a/src/Providers/ViewComposersServiceProvider.php +++ b/src/Providers/ViewComposersServiceProvider.php @@ -1,4 +1,5 @@ app->view->composer('anavel-crud::molecules.sidebar.default', 'Anavel\Crud\View\Composers\SidebarComposer'); + $this->app->view->composer('anavel-crud::atoms.forms.field', 'Anavel\Crud\View\Composers\FormFieldComposer'); } } diff --git a/src/Repository/Criteria/InArrayCriteria.php b/src/Repository/Criteria/InArrayCriteria.php index 0bb37f3..377f3f6 100644 --- a/src/Repository/Criteria/InArrayCriteria.php +++ b/src/Repository/Criteria/InArrayCriteria.php @@ -1,4 +1,5 @@ columns); - - if (strpos($firstColumn, '.')) { - $query = $this->setRelationFieldCondition($model, $firstColumn, false); - } else { - $query = $model->where($firstColumn, 'LIKE', '%'.$this->queryString.'%'); - } + $query = $model->where(function ($query) use ($repository) { + $firstColumn = array_shift($this->columns); - foreach ($this->columns as $column) { - if (strpos($column, '.')) { - $query = $this->setRelationFieldCondition($query, $column); + if (strpos($firstColumn, '.')) { + $query = $this->setRelationFieldCondition($query, $firstColumn, false); } else { - $query->orWhere($column, 'LIKE', '%'.$this->queryString.'%'); + $query = $query->where($firstColumn, 'LIKE', '%'.$this->queryString.'%'); } - } + + foreach ($this->columns as $column) { + if (strpos($column, '.')) { + $query = $this->setRelationFieldCondition($query, $column); + } else { + $query->orWhere($column, 'LIKE', '%'.$this->queryString.'%'); + } + } + }); return $query; } @@ -40,16 +43,27 @@ private function setRelationFieldCondition($query, $column, $or = true) { $columnRelation = explode('.', $column); + $firstRelation = array_shift($columnRelation); + if ($or) { - $query->orWhereHas($columnRelation[0], function ($subquery) use ($columnRelation) { - $subquery->where($columnRelation[1], 'LIKE', '%'.$this->queryString.'%'); - }); + $query->orWhereHas($firstRelation, $this->getRelationClosure($columnRelation)); } else { - $query->whereHas($columnRelation[0], function ($subquery) use ($columnRelation) { - $subquery->where($columnRelation[1], 'LIKE', '%'.$this->queryString.'%'); - }); + $query->whereHas($firstRelation, $this->getRelationClosure($columnRelation)); } return $query; } + + private function getRelationClosure(array $relations) + { + return function ($query) use ($relations) { + $relation = array_shift($relations); + + if (count($relations) > 0) { + $query->whereHas($relation, $this->getRelationClosure($relations)); + } else { + $query->where($relation, 'LIKE', '%'.$this->queryString.'%'); + } + }; + } } diff --git a/src/View/Composers/FormFieldComposer.php b/src/View/Composers/FormFieldComposer.php new file mode 100644 index 0000000..1abeec7 --- /dev/null +++ b/src/View/Composers/FormFieldComposer.php @@ -0,0 +1,27 @@ +anavel = $anavel; + } + + public function compose($view) + { + $uploadsModuleIsInstalled = $this->anavel->hasModule('Anavel\Uploads\UploadsModuleProvider'); + + $view->with([ + 'canTakeFileFromUploads' => $uploadsModuleIsInstalled, + ]); + } +} diff --git a/src/View/Composers/SidebarComposer.php b/src/View/Composers/SidebarComposer.php index 59da7a5..7333a27 100644 --- a/src/View/Composers/SidebarComposer.php +++ b/src/View/Composers/SidebarComposer.php @@ -1,9 +1,10 @@ getSidebarItems(); $view->with([ - 'menuItems' => $menuItems, - 'headerName' => config('anavel-crud.name') + 'menuItems' => $menuItems, + 'headerName' => config('anavel-crud.name'), ]); } @@ -52,7 +53,7 @@ private function getSidebarItems() } } // Remove empty groups (resulting, most probably, of different permissions) - if(count($menuItems[$group]['items']) < 1) { + if (count($menuItems[$group]['items']) < 1) { unset($menuItems[$group]); } } @@ -64,7 +65,7 @@ private function getSidebarItems() } catch (\Exception $e) { continue; } - $menuItems[$modelName]['isActive'] = $item['isActive']; + $menuItems[$modelName]['isActive'] = $item['isActive']; $menuItems[$modelName]['name'] = $item['name']; $menuItems[$modelName]['items'][] = $item; } @@ -73,13 +74,16 @@ private function getSidebarItems() usort($menuItems, function ($itemA, $itemB) { return strcmp($itemA['name'], $itemB['name']); }); + return $menuItems; } /** * @param $modelName - * @return array + * * @throws \Exception + * + * @return array */ private function getModelItem($modelName) { @@ -96,9 +100,9 @@ private function getModelItem($modelName) } $item = [ - 'route' => route('anavel-crud.model.index', $modelAbstractor->getSlug()), - 'name' => $modelAbstractor->getName(), - 'isActive' => $isActive + 'route' => route('anavel-crud.model.index', $modelAbstractor->getSlug()), + 'name' => $modelAbstractor->getName(), + 'isActive' => $isActive, ]; return $item; diff --git a/src/View/Presenters/Paginator.php b/src/View/Presenters/Paginator.php index 1749a90..4c81ca6 100644 --- a/src/View/Presenters/Paginator.php +++ b/src/View/Presenters/Paginator.php @@ -1,4 +1,5 @@ sut->type(); - $this->assertInstanceOf('Doctrine\DBAL\Types\Type', $type); } diff --git a/tests/Abstractor/Eloquent/MiniCrudPolymorphicTest.php b/tests/Abstractor/Eloquent/MiniCrudPolymorphicTest.php index 927543d..a1639e8 100644 --- a/tests/Abstractor/Eloquent/MiniCrudPolymorphicTest.php +++ b/tests/Abstractor/Eloquent/MiniCrudPolymorphicTest.php @@ -1,31 +1,29 @@ wrongConfig = require __DIR__ . '/../../wrong-config.php'; + $this->wrongConfig = require __DIR__.'/../../wrong-config.php'; $this->relationMock = $this->mock('Illuminate\Database\Eloquent\Relations\Relation'); $this->fieldFactoryMock = $this->mock('Anavel\Crud\Contracts\Abstractor\FieldFactory'); $this->modelManagerMock = Mockery::mock('ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager'); $this->requestMock = $this->mock('Illuminate\Http\Request'); - $this->modelManagerMock->shouldReceive('getAbstractionLayer')->andReturn($this->dbalMock = $this->mock('ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer')); + $this->modelManagerMock->shouldReceive('getAbstractionLayer')->andReturn($this->dbalMock = $this->mock('ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer')); $this->getClassMock = PHPMockery::mock('Anavel\Crud\Abstractor\Eloquent\Relation\Traits', 'get_class'); @@ -54,7 +52,7 @@ public function setUp() public function buildRelation() { - $config = require __DIR__ . '/../../config.php'; + $config = require __DIR__.'/../../config.php'; $this->sut = new MiniCrudPolymorphic( $config['Users']['relations']['group'], $this->modelManagerMock, @@ -79,7 +77,6 @@ public function test_throws_exception_when_class_not_compatible() $this->buildRelation(); } - public function test_get_edit_fields_returns_array() { $this->modelAbstractorMock->shouldReceive('getColumns')->times(1)->andReturn(['columname' => $columnMock = $this->mock('Doctrine\DBAL\Schema\Column'), '__delete' => $columnMock = $this->mock('Doctrine\DBAL\Schema\Column')]); @@ -98,7 +95,6 @@ public function test_get_edit_fields_returns_array() $postMock->shouldReceive('getAttribute')->andReturn('idValue'); - $fieldMock->shouldReceive('setOptions'); $fieldMock->shouldReceive('setValue')->times(1); @@ -106,8 +102,6 @@ public function test_get_edit_fields_returns_array() $this->modelAbstractorMock->shouldReceive('getRelations')->times(2)->andReturn([$this->secondaryRelationMock = $this->mock('Anavel\Crud\Abstractor\Eloquent\Relation\Select')]); $this->secondaryRelationMock->shouldReceive('getEditFields')->andReturn([]); - - $this->getClassMock->andReturn('Illuminate\Database\Eloquent\Relations\MorphMany'); $this->buildRelation(); $fields = $this->sut->getEditFields(); @@ -136,7 +130,7 @@ public function test_persist_with_no_old_results() 'field' => 1, 'otherField' => 3, 'someOtherField' => 3, - ] + ], ]; $this->relationMock->shouldReceive('getForeignKey', 'getPlainMorphType', 'getMorphClass'); @@ -182,7 +176,7 @@ public function test_persist_with_old_results() 'id' => 1, 'otherField' => 3, 'someOtherField' => 3, - ] + ], ]; $this->relationMock->shouldReceive('getForeignKey', 'getPlainMorphType', 'getMorphClass'); @@ -194,7 +188,6 @@ public function test_persist_with_old_results() $this->modelAbstractorMock->shouldReceive('setInstance')->with($modelMock); - $modelMock->shouldReceive('getKey')->andReturn(1); $modelMock->shouldReceive('setAttribute')->times(10); $modelMock->shouldReceive('save')->times(2); diff --git a/tests/Abstractor/Eloquent/MiniCrudSingleTest.php b/tests/Abstractor/Eloquent/MiniCrudSingleTest.php index 71940ab..c3a7e54 100644 --- a/tests/Abstractor/Eloquent/MiniCrudSingleTest.php +++ b/tests/Abstractor/Eloquent/MiniCrudSingleTest.php @@ -1,4 +1,5 @@ wrongConfig = require __DIR__ . '/../../wrong-config.php'; + $this->wrongConfig = require __DIR__.'/../../wrong-config.php'; $this->relationMock = $this->mock('Illuminate\Database\Eloquent\Relations\Relation'); $this->fieldFactoryMock = $this->mock('Anavel\Crud\Contracts\Abstractor\FieldFactory'); @@ -50,7 +50,7 @@ public function setUp() //We can not do the construct in the setup because get_class needs to be mocked differently each time public function buildRelation() { - $config = require __DIR__ . '/../../config.php'; + $config = require __DIR__.'/../../config.php'; $this->sut = new MiniCrudSingle( $config['Users']['relations']['group'], $this->modelManagerMock, @@ -76,7 +76,6 @@ public function test_throws_exception_when_class_not_compatible() $this->buildRelation(); } - public function test_get_edit_fields_returns_array() { $this->relationMock->shouldReceive('getRelated', 'getPlainForeignKey', 'getPlainMorphType', 'getParent', @@ -86,7 +85,7 @@ public function test_get_edit_fields_returns_array() $this->modelAbstractorMock->shouldReceive('getColumns')->times(1)->andReturn( [ 'columname' => $columnMock = $this->mock('Doctrine\DBAL\Schema\Column'), - '__delete' => $columnMock = $this->mock('Doctrine\DBAL\Schema\Column') + '__delete' => $columnMock = $this->mock('Doctrine\DBAL\Schema\Column'), ] ); @@ -94,7 +93,6 @@ public function test_get_edit_fields_returns_array() $postMock->shouldReceive('getAttributeValue')->andReturn('1'); $postMock->shouldReceive('getAttribute')->andReturn('chompy'); - $this->fieldFactoryMock->shouldReceive('setColumn', 'setConfig')->andReturn($this->fieldFactoryMock); $this->fieldFactoryMock->shouldReceive('get')->andReturn($fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\Field')); $fieldMock->shouldReceive('setOptions'); @@ -104,7 +102,6 @@ public function test_get_edit_fields_returns_array() $this->modelAbstractorMock->shouldReceive('getRelations')->times(1)->andReturn([$secondaryRelationMock = $this->mock('Anavel\Crud\Abstractor\Eloquent\Relation\Select')]); $secondaryRelationMock->shouldReceive('getEditFields')->andReturn([]); - $this->getClassMock->andReturn('Illuminate\Database\Eloquent\Relations\MorphOne'); $this->buildRelation(); $fields = $this->sut->getEditFields(); diff --git a/tests/Abstractor/Eloquent/MiniCrudTest.php b/tests/Abstractor/Eloquent/MiniCrudTest.php index 5202bfd..228fd25 100644 --- a/tests/Abstractor/Eloquent/MiniCrudTest.php +++ b/tests/Abstractor/Eloquent/MiniCrudTest.php @@ -1,4 +1,5 @@ wrongConfig = require __DIR__ . '/../../wrong-config.php'; + $this->wrongConfig = require __DIR__.'/../../wrong-config.php'; $this->relationMock = $this->mock('Illuminate\Database\Eloquent\Relations\Relation'); $this->fieldFactoryMock = $this->mock('Anavel\Crud\Contracts\Abstractor\FieldFactory'); $this->modelManagerMock = Mockery::mock('ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager'); - $this->modelManagerMock->shouldReceive('getAbstractionLayer')->andReturn($this->dbalMock = $this->mock('ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer')); + $this->modelManagerMock->shouldReceive('getAbstractionLayer')->andReturn($this->dbalMock = $this->mock('ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer')); $this->requestMock = $this->mock('Illuminate\Http\Request'); $this->getClassMock = PHPMockery::mock('Anavel\Crud\Abstractor\Eloquent\Relation\Traits', @@ -52,7 +52,7 @@ public function setUp() //We can not do the construct in the setup because get_class needs to be mocked differently each time public function buildRelation() { - $config = require __DIR__ . '/../../config.php'; + $config = require __DIR__.'/../../config.php'; $this->sut = new MiniCrud( $config['Users']['relations']['group'], $this->modelManagerMock, @@ -77,7 +77,6 @@ public function test_throws_exception_when_class_not_compatible() $this->buildRelation(); } - public function test_get_edit_fields_returns_array() { $this->modelAbstractorMock->shouldReceive('getColumns')->times(1)->andReturn(['columname' => $columnMock = $this->mock('Doctrine\DBAL\Schema\Column'), '__delete' => $columnMock = $this->mock('Doctrine\DBAL\Schema\Column')]); @@ -90,7 +89,6 @@ public function test_get_edit_fields_returns_array() $this->fieldFactoryMock->shouldReceive('setColumn', 'setConfig')->andReturn($this->fieldFactoryMock); $this->fieldFactoryMock->shouldReceive('get')->andReturn($fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\Field')); - $this->relationMock->shouldReceive('newInstance')->andReturn($postMock); $this->modelAbstractorMock->shouldReceive('setInstance')->with($postMock); @@ -99,7 +97,6 @@ public function test_get_edit_fields_returns_array() $secondaryRelationMock->shouldReceive('getEditFields')->andReturn([]); - $postMock->shouldReceive('getAttribute')->andReturn('idValue'); $fieldMock->shouldReceive('setOptions'); @@ -125,18 +122,17 @@ public function test_get_edit_fields_returns_array() $this->assertCount(2, $fields['group']['idValue']); // Mocked field, plus delete checkbox $this->assertArrayHasKey('columname', $fields['group']['idValue']); $this->assertInstanceOf('Anavel\Crud\Contracts\Abstractor\Field', $fields['group']['idValue']['columname']); - } public function test_persist_with_no_old_results() { $inputArray = [ '0' => [ - 'field' => 1, - 'otherField' => 3, + 'field' => 1, + 'otherField' => 3, 'someOtherField' => 3, - 'relationName' => [] - ] + 'relationName' => [], + ], ]; $this->relationMock->shouldReceive('getForeignKey'); @@ -156,7 +152,6 @@ public function test_persist_with_no_old_results() $this->fieldFactoryMock->shouldReceive('get')->andReturn($fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\Field')); //////// - $fieldMock->shouldReceive('getName'); $fieldMock->shouldReceive('getFormField'); $modelMock->shouldReceive('getKey')->andReturn(1); @@ -175,15 +170,15 @@ public function test_persist_with_old_results() $inputArray = [ '0' => [ - 'id' => 1, - 'otherField' => 3, + 'id' => 1, + 'otherField' => 3, 'someOtherField' => 3, ], '1' => [ - 'id' => 1, - 'otherField' => 3, + 'id' => 1, + 'otherField' => 3, 'someOtherField' => 3, - ] + ], ]; $this->relationMock->shouldReceive('getForeignKey'); @@ -193,8 +188,6 @@ public function test_persist_with_old_results() $this->relationMock->shouldReceive('getKeyName')->andReturn('id'); $this->modelAbstractorMock->shouldReceive('getRelations')->andReturn(collect()); - - $this->relationMock->shouldReceive('getResults')->andReturn($this->relationMock, collect([1 => $modelMock = $this->mock('Anavel\Crud\Tests\Models\Post')])); // This, basically, re-tests getEditFields... It shouldn't be re-tested, but I can't figure out how to partially mock that method $this->relationMock->shouldReceive('getRelated', 'getPlainForeignKey', 'getParent', diff --git a/tests/Abstractor/Eloquent/ModelFactoryTest.php b/tests/Abstractor/Eloquent/ModelFactoryTest.php index 38c7b2d..55103f2 100644 --- a/tests/Abstractor/Eloquent/ModelFactoryTest.php +++ b/tests/Abstractor/Eloquent/ModelFactoryTest.php @@ -1,14 +1,14 @@ modelManagerMock = $this->mock('ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager'); $this->relationMock = $this->mock('Anavel\Crud\Contracts\Abstractor\RelationFactory'); $this->fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\FieldFactory'); $this->generatorMock = $this->mock('Anavel\Crud\Contracts\Form\Generator'); - - $this->sut = new ModelFactory($config, $this->modelManagerMock, $this->relationMock, $this->fieldMock, $this->generatorMock); + $this->anavelMock = $this->mock('Anavel\Foundation\Contracts\Anavel'); + $this->anavelMock->shouldReceive('hasModule')->andReturn(false); + $this->sut = new ModelFactory($config, $this->modelManagerMock, $this->relationMock, $this->fieldMock, $this->generatorMock, $this->anavelMock); } public function test_implements_model__factory_interface() @@ -41,7 +44,7 @@ public function test_implements_model__factory_interface() public function test_throws_exception_when_model_not_found() { - $this->setExpectedException('Exception', "Model chompy not found on configuration" ); + $this->setExpectedException('Exception', 'Model chompy not found on configuration'); $model = $this->sut->getBySlug('chompy'); } diff --git a/tests/Abstractor/Eloquent/ModelTest.php b/tests/Abstractor/Eloquent/ModelTest.php index b78c348..b112863 100644 --- a/tests/Abstractor/Eloquent/ModelTest.php +++ b/tests/Abstractor/Eloquent/ModelTest.php @@ -1,16 +1,16 @@ dbalMock = $this->mock('ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer'); $this->relationFactoryMock = $this->mock('Anavel\Crud\Contracts\Abstractor\RelationFactory'); @@ -38,10 +38,9 @@ public function setUp() $this->columnMock = $this->mock('Doctrine\DBAL\Schema\Column'); $this->generatorMock = $this->mock('Anavel\Crud\Contracts\Form\Generator'); - $this->getClassMock = PHPMockery::mock('Anavel\Crud\Abstractor\Eloquent', 'get_class'); - $this->sut = \Mockery::mock(Model::class, [$config['Users'], $this->dbalMock, $this->relationFactoryMock, $this->fieldFactoryMock,$this->generatorMock])->makePartial(); + $this->sut = \Mockery::mock(Model::class, [$config['Users'], $this->dbalMock, $this->relationFactoryMock, $this->fieldFactoryMock, $this->generatorMock])->makePartial(); } public function test_implements_model_interface() @@ -57,7 +56,7 @@ public function test_returns_list_fields_as_array() 'id' => $this->columnMock, 'username' => $this->columnMock, 'fullname' => $this->columnMock, - 'active' => $this->columnMock + 'active' => $this->columnMock, ]); $this->dbalMock->shouldReceive('getTableForeignKeys') ->andReturn([]); @@ -90,7 +89,7 @@ public function test_returns_list_fields_as_array_with_key() 'id' => $this->columnMock, 'username' => $this->columnMock, 'fullname' => $this->columnMock, - 'active' => $this->columnMock + 'active' => $this->columnMock, ]); $this->dbalMock->shouldReceive('getTableForeignKeys') ->andReturn([]); @@ -125,7 +124,7 @@ public function test_returns_detail_fields_as_array() 'password' => $this->columnMock, 'fullname' => $this->columnMock, 'info' => $this->columnMock, - 'active' => $this->columnMock + 'active' => $this->columnMock, ]); $this->dbalMock->shouldReceive('getTableForeignKeys') ->andReturn([]); @@ -162,7 +161,7 @@ public function test_returns_detail_fields_as_array_with_key() 'password' => $this->columnMock, 'fullname' => $this->columnMock, 'info' => $this->columnMock, - 'active' => $this->columnMock + 'active' => $this->columnMock, ]); $this->dbalMock->shouldReceive('getTableForeignKeys') ->andReturn([]); @@ -233,7 +232,7 @@ public function test_returns_edit_fields_as_array() 'presentation' => null, 'form_type' => null, 'validation' => null, - 'functions' => null + 'functions' => null, ]) ->andReturn($this->fieldFactoryMock); @@ -245,7 +244,7 @@ public function test_returns_edit_fields_as_array() 'form_type' => 'email', 'validation' => 'required|email', 'functions' => null, - 'defaults' => 'Chompy' + 'defaults' => 'Chompy', ]) ->andReturn($this->fieldFactoryMock); @@ -275,11 +274,11 @@ public function test_returns_edit_fields_as_array() // Wit this one we check that file fields generate an extra __delete one $fielMock5->shouldReceive('setConfig') ->with([ - 'name' => 'image__delete', - 'presentation' => null, - 'form_type' => 'checkbox', + 'name' => 'image__delete', + 'presentation' => null, + 'form_type' => 'checkbox', 'no_validate' => true, - 'functions' => null, + 'functions' => null, ]) ->andReturn($this->fieldFactoryMock); @@ -294,7 +293,6 @@ public function test_returns_edit_fields_as_array() $this->assertArrayHasKey('username', $fields['main']); $this->assertInstanceOf('Anavel\Crud\Contracts\Abstractor\Field', $fields['main']['username']); - } public function test_returns_edit_fields_as_array_with_key() @@ -353,7 +351,6 @@ public function test_returns_relations_as_collection() foreach ($relations as $relation) { $this->assertInstanceOf('Anavel\Crud\Contracts\Abstractor\Relation', $relation); } - } public function test_returns_relations_as_multidimensional_collection() @@ -376,7 +373,6 @@ public function test_returns_relations_as_multidimensional_collection() $this->assertInstanceOf('Anavel\Crud\Contracts\Abstractor\Relation', $secondaryRelation); } } - } public function test_get_form() @@ -420,7 +416,6 @@ public function test_get_form() $this->assertInstanceOf('FormManager\ElementInterface', $form); } - public function test_get_form_with_instance() { $this->generatorMock->shouldReceive('setModelFields', 'setRelatedModelFields') @@ -462,7 +457,6 @@ public function test_get_form_with_instance() $this->sut->setInstance($modelMock); - $form = $this->sut->getForm('crudoado.model.store', 'users'); $this->assertInstanceOf('FormManager\ElementInterface', $form); @@ -496,7 +490,7 @@ public function test_persist() $fieldMock, $fieldMock, $fieldMock, - ] + ], ]); $this->sut->shouldReceive('getEditFields')->times(1)->with(false)->andReturn([ 'main' => [ @@ -520,8 +514,6 @@ public function test_persist() $this->getClassMock->andReturn('nomatch'); - - $result = $this->sut->persist($requestMock); $this->assertInstanceOf('Anavel\Crud\Tests\Models\User', $result); diff --git a/tests/Abstractor/Eloquent/RelationFactoryTest.php b/tests/Abstractor/Eloquent/RelationFactoryTest.php index 9c00f67..b95793f 100644 --- a/tests/Abstractor/Eloquent/RelationFactoryTest.php +++ b/tests/Abstractor/Eloquent/RelationFactoryTest.php @@ -1,15 +1,15 @@ sut->setConfig($config['Users']['relations']['translations']); $this->setExpectedException('Anavel\Crud\Abstractor\Exceptions\FactoryException', 'Unexpected relation type: fake'); diff --git a/tests/Abstractor/Eloquent/SelectMultipleManyToManyTest.php b/tests/Abstractor/Eloquent/SelectMultipleManyToManyTest.php index 23cf13e..e4f7ce3 100644 --- a/tests/Abstractor/Eloquent/SelectMultipleManyToManyTest.php +++ b/tests/Abstractor/Eloquent/SelectMultipleManyToManyTest.php @@ -1,30 +1,28 @@ wrongConfig = require __DIR__ . '/../../wrong-config.php'; - + $config = require __DIR__.'/../../config.php'; + $this->wrongConfig = require __DIR__.'/../../wrong-config.php'; $this->relationMock = $this->mock('Illuminate\Database\Eloquent\Relations\Relation'); $this->fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\FieldFactory'); @@ -96,7 +93,7 @@ public function test_get_edit_fields_returns_array_of_fields_with_proper_key() public function test_persist() { $inputArray = ['id' => [1, 3, 4]]; - $config = require __DIR__ . '/../../config.php'; + $config = require __DIR__.'/../../config.php'; $relationMock = $this->mock('Illuminate\Database\Eloquent\Relations\Relation'); $relationMock->shouldReceive('getRelated')->andReturn($relationMock); @@ -127,8 +124,6 @@ public function test_throws_exception_if_display_is_not_set_in_config() ); } - - public function test_throws_exception_if_name_is_not_set_in_config() { $this->setExpectedException('Anavel\Crud\Abstractor\Exceptions\RelationException', 'Relation name should be set'); diff --git a/tests/Abstractor/Eloquent/SelectMultipleTest.php b/tests/Abstractor/Eloquent/SelectMultipleTest.php index 466894c..73468c0 100644 --- a/tests/Abstractor/Eloquent/SelectMultipleTest.php +++ b/tests/Abstractor/Eloquent/SelectMultipleTest.php @@ -1,29 +1,27 @@ wrongConfig = require __DIR__ . '/../../wrong-config.php'; - + $config = require __DIR__.'/../../config.php'; + $this->wrongConfig = require __DIR__.'/../../wrong-config.php'; $this->relationMock = $this->mock('Illuminate\Database\Eloquent\Relations\Relation'); $this->fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\FieldFactory'); @@ -119,8 +116,6 @@ public function test_throws_exception_if_display_is_not_set_in_config() ); } - - public function test_throws_exception_if_name_is_not_set_in_config() { $this->setExpectedException('Anavel\Crud\Abstractor\Exceptions\RelationException', 'Relation name should be set'); diff --git a/tests/Abstractor/Eloquent/SelectTest.php b/tests/Abstractor/Eloquent/SelectTest.php index dc84891..ef3d246 100644 --- a/tests/Abstractor/Eloquent/SelectTest.php +++ b/tests/Abstractor/Eloquent/SelectTest.php @@ -1,4 +1,5 @@ wrongConfig = require __DIR__ . '/../../wrong-config.php'; + $this->wrongConfig = require __DIR__.'/../../wrong-config.php'; $this->relationMock = $this->mock('Illuminate\Database\Eloquent\Relations\Relation'); $this->fieldMock = $this->mock('Anavel\Crud\Contracts\Abstractor\FieldFactory'); @@ -64,7 +64,6 @@ public function test_get_edit_fields_return_array_with_one_field() $field->shouldReceive('setOptions'); - $fields = $this->sut->getEditFields(); $this->assertInternalType('array', $fields, 'getEditFields should return an array'); @@ -84,7 +83,6 @@ public function test_get_edit_fields_return_array_with_one_field_with_key() $field->shouldReceive('setOptions'); - $fields = $this->sut->getEditFields('chompy'); $this->assertInternalType('array', $fields, 'getEditFields should return an array'); diff --git a/tests/Abstractor/Eloquent/TranslationTest.php b/tests/Abstractor/Eloquent/TranslationTest.php index be684d0..a1ab240 100644 --- a/tests/Abstractor/Eloquent/TranslationTest.php +++ b/tests/Abstractor/Eloquent/TranslationTest.php @@ -1,4 +1,5 @@ relationMock = $this->mock('Illuminate\Database\Eloquent\Relations\Relation'); $this->fieldFactoryMock = $this->mock('Anavel\Crud\Contracts\Abstractor\FieldFactory'); @@ -86,7 +85,7 @@ public function test_get_edit_fields_returns_array_of_fields_with_proper_key() 'id' => $columnMock, 'user_id' => $columnMock, 'locale' => $columnMock, - 'bio' => $columnMock + 'bio' => $columnMock, ]); $fields = $this->sut->getEditFields(); @@ -99,7 +98,7 @@ public function test_get_edit_fields_returns_array_of_fields_with_proper_key() $this->assertArrayHasKey('en', $fields['translations']); $this->assertInternalType('array', $fields['translations']['gl']); - foreach($fields['translations']['gl'] as $element) { + foreach ($fields['translations']['gl'] as $element) { $this->assertInstanceOf('Anavel\Crud\Contracts\Abstractor\Field', $element); } } @@ -108,13 +107,13 @@ public function test_persist_with_no_results() { $inputArray = [ 'es' => [ - 'locale' => 'es', + 'locale' => 'es', 'otherField' => 'something', ], 'en' => [ - 'locale' => 'en', + 'locale' => 'en', 'otherField' => '', - ] + ], ]; $this->relationMock->shouldReceive('getResults')->andReturn(collect()); @@ -124,7 +123,6 @@ public function test_persist_with_no_results() $modelMock->shouldReceive('setAttribute')->times(3); // Should pass the language with only locale $modelMock->shouldReceive('save')->once(); - $this->sut->persist($inputArray, $this->requestMock); } @@ -132,13 +130,13 @@ public function test_persist_with_old_results() { $inputArray = [ 'es' => [ - 'locale' => 'es', + 'locale' => 'es', 'otherField' => 'something', ], 'en' => [ - 'locale' => 'en', + 'locale' => 'en', 'otherField' => '', - ] + ], ]; $modelMock = $this->mock(UserTranslations::class); @@ -152,7 +150,6 @@ public function test_persist_with_old_results() $modelMock->shouldReceive('delete')->once(); // Should delete the already existing language, since it's empty $modelMock->shouldReceive('save')->once(); - $this->sut->persist($inputArray, $this->requestMock); } } diff --git a/tests/Http/Form/GeneratorTest.php b/tests/Http/Form/GeneratorTest.php index fedfa36..b101682 100644 --- a/tests/Http/Form/GeneratorTest.php +++ b/tests/Http/Form/GeneratorTest.php @@ -1,9 +1,10 @@ morphTo(); } -} \ No newline at end of file +} diff --git a/tests/Models/Post.php b/tests/Models/Post.php index 12be00b..ac187aa 100644 --- a/tests/Models/Post.php +++ b/tests/Models/Post.php @@ -1,9 +1,7 @@ belongsTo(User::class, 'user_id'); } -} \ No newline at end of file +} diff --git a/tests/Models/Role.php b/tests/Models/Role.php index d0dea55..7918c85 100644 --- a/tests/Models/Role.php +++ b/tests/Models/Role.php @@ -1,9 +1,7 @@ belongsToMany(User::class, 'roles_users'); } -} \ No newline at end of file +} diff --git a/tests/Models/User.php b/tests/Models/User.php index da22068..adbba9f 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -1,9 +1,7 @@ belongsToMany(Role::class, 'roles_users'); } -} \ No newline at end of file +} diff --git a/tests/Models/UserGroup.php b/tests/Models/UserGroup.php index 1ceeddc..8490d46 100644 --- a/tests/Models/UserGroup.php +++ b/tests/Models/UserGroup.php @@ -1,9 +1,7 @@ hasMany(User::class, 'user_group_id'); } -} \ No newline at end of file +} diff --git a/tests/Models/UserTranslations.php b/tests/Models/UserTranslations.php index 27bba0d..932607e 100644 --- a/tests/Models/UserTranslations.php +++ b/tests/Models/UserTranslations.php @@ -1,12 +1,9 @@ set('database.default', 'sqlite'); $app['config']->set('database.connections.sqlite', [ - 'driver' => 'sqlite', - 'database' => ':memory:', - 'prefix' => '' + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', ]); + $app['aliases'] = ['Route' => \Illuminate\Support\Facades\Route::class]; + $app['config']->set('anavel.translation_languages', ['gl', 'en', 'es']); + $app['config']->set('anavel-crud.models', []); \App::bind('ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager', function ($app) { return \Mockery::mock('ANavallaSuiza\Laravel\Database\Manager\Eloquent\ModelManager'); @@ -63,7 +66,6 @@ protected function getPackageProviders($app) return []; } - public function mock($className) { return Mockery::mock($className); @@ -78,7 +80,7 @@ public function test_running_migration() { $migrations = \DB::select('SELECT * FROM migrations'); - $fi = new \FilesystemIterator(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR .self::MIGRATIONS_PATH, \FilesystemIterator::SKIP_DOTS); + $fi = new \FilesystemIterator(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.self::MIGRATIONS_PATH, \FilesystemIterator::SKIP_DOTS); $this->assertCount(iterator_count($fi), $migrations); } diff --git a/tests/config.php b/tests/config.php index b2a5ac9..c65810f 100644 --- a/tests/config.php +++ b/tests/config.php @@ -9,45 +9,45 @@ 'soft_deletes' => true, 'fields_presentation' => [ 'active' => 'Is Active?', - 'role_id' => 'Role' + 'role_id' => 'Role', ], 'relations_presentation' => [ 'translations' => 'Translations', - 'posts' => 'Published posts' + 'posts' => 'Published posts', ], 'relations' => [ 'translations' => [ 'type' => 'translation', - 'name' => 'translations' + 'name' => 'translations', ], 'group' => [ 'type' => 'select', 'name' => 'group', - 'display' => 'title' + 'display' => 'title', ], 'posts' => [ 'type' => 'select-multiple', 'name' => 'posts', - 'display' => 'title' + 'display' => 'title', ], 'photos' => [ 'type' => 'select-multiple', 'name' => 'photos', - 'display' => 'title' + 'display' => 'title', ], 'roles' => [ 'type' => 'select-multiple-many-to-many', 'name' => 'roles', - 'display' => 'name' - ] + 'display' => 'name', + ], ], 'list' => [ 'display' => ['id', 'username', 'fullname', 'active'], - 'hide' => [] + 'hide' => [], ], 'detail' => [ 'display' => ['id', 'username', 'password', 'fullname', 'info', 'active'], - 'hide' => [] + 'hide' => [], ], 'edit' => [ 'display' => ['id', 'username', 'password', 'image'], @@ -56,22 +56,22 @@ 'username' => 'Chompy', 'info' => [ 'key' => 'Value', - 'otherKey' => 'Other value' - ] + 'otherKey' => 'Other value', + ], ], 'form_types' => [ 'username' => 'email', 'password' => 'password', - 'image' => 'file' + 'image' => 'file', ], 'validation' => [ 'username' => 'required|email', - 'password' => 'required|min:8' + 'password' => 'required|min:8', ], 'functions' => [ 'fullname' => 'slugify', - 'password' => 'bcrypt' - ] - ] - ] + 'password' => 'bcrypt', + ], + ], + ], ]; diff --git a/tests/migrations/2015_11_12_155600_create_tables.php b/tests/migrations/2015_11_12_155600_create_tables.php index 57ef8d8..900623e 100644 --- a/tests/migrations/2015_11_12_155600_create_tables.php +++ b/tests/migrations/2015_11_12_155600_create_tables.php @@ -1,4 +1,5 @@ 'App\Post', 'Users' => [ - 'model' => 'App\User', - 'icon' => 'fa-database', - 'soft_deletes' => true, + 'model' => 'App\User', + 'icon' => 'fa-database', + 'soft_deletes' => true, 'fields_presentation' => [ - 'active' => 'Is Active?', - 'role_id' => 'Role' + 'active' => 'Is Active?', + 'role_id' => 'Role', ], 'relations_presentation' => [ 'translations' => 'Translations', - 'posts' => 'Published posts' + 'posts' => 'Published posts', ], 'relations' => [ 'translations' => [ @@ -33,32 +33,32 @@ ], 'roles' => [ 'type' => 'select-multiple-many-to-many', - 'name' => 'roles' - ] + 'name' => 'roles', + ], ], 'list' => [ 'display' => ['id', 'username', 'fullname', 'active'], - 'hide' => [] + 'hide' => [], ], 'detail' => [ 'display' => ['id', 'username', 'password', 'fullname', 'info', 'active'], - 'hide' => [] + 'hide' => [], ], 'edit' => [ - 'display' => ['id', 'username', 'password'], - 'hide' => [], + 'display' => ['id', 'username', 'password'], + 'hide' => [], 'form_types' => [ 'username' => 'email', - 'password' => 'password' + 'password' => 'password', ], 'validation' => [ 'username' => 'required|email', - 'password' => 'required|min:8' + 'password' => 'required|min:8', ], 'functions' => [ 'fullname' => 'slugify', - 'password' => 'bcrypt' - ] - ] - ] + 'password' => 'bcrypt', + ], + ], + ], ]; diff --git a/views/atoms/forms/field.blade.php b/views/atoms/forms/field.blade.php index 2e5b938..5f3b52d 100644 --- a/views/atoms/forms/field.blade.php +++ b/views/atoms/forms/field.blade.php @@ -24,22 +24,41 @@ @endif @else -
+ +
@if($field->attr('type') != 'hidden') +
+ class="control-label">{{ $field->label->html() }}{{ $field->attr('required') ? ' *' : '' }} +
@endif + @if($field instanceof FormManager\Fields\File) +
+ {!! $field->input !!} +
+
-
- {!! $field->input !!} -
+ + @if ($canTakeFileFromUploads) + + {{_('Get from uploads')}} + - @if($field instanceof FormManager\Fields\File && ! empty($field->val())) + @endif + @if (!empty($field->val())) + + Ver + + @endif + +
+ @else +
+ {!! $field->input !!} +
- + @endif - @endif
-@endif \ No newline at end of file +@endif + diff --git a/views/molecules/forms/edit-main.blade.php b/views/molecules/forms/edit-main.blade.php index 5b866b8..57b6ba9 100644 --- a/views/molecules/forms/edit-main.blade.php +++ b/views/molecules/forms/edit-main.blade.php @@ -13,7 +13,7 @@ @empty @endforelse -
+
@unless(empty($form['main'])) @foreach($form['main'] as $field) @@ -59,4 +59,82 @@ @endif @empty @endforelse -
\ No newline at end of file +
+ + + +@section('footer-scripts') + @parent + + +@stop