├── .editorconfig ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── BlenderModelServiceProvider.php ├── Controller.php ├── Model.php ├── Scopes ├── NonDraftMediaScope.php ├── NonDraftScope.php ├── OnlineScope.php └── SortableScope.php ├── Traits ├── Draftable.php ├── HasContentBlocks.php ├── HasMedia.php ├── HasSeoValues.php └── HasSlug.php ├── Transformers ├── ContentBlockTransformer.php └── MediaTransformer.php └── Updaters ├── UpdateDates.php ├── UpdateMedia.php ├── UpdateOnlineToggle.php ├── UpdateSeoValues.php ├── UpdateTags.php └── UpdateTranslations.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | linting: true 4 | 5 | disabled: 6 | - single_class_element_per_statement 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `blender-model` will be documented in this file 4 | 5 | ## 5.1.7 6 | - Fixed: Added properties to `ContentBlockTransformer` 7 | 8 | ## 5.1.6 9 | - Use a regex in `NonDraftMediaScope` for sqlite compat 10 | 11 | ## 5.1.5 12 | - Also apply the `SortableScope` in back requests 13 | 14 | ## 5.1.3 - 2017-02-20 15 | - Fixed old collection call for L5.4 16 | 17 | ## 5.1.1 - 2017-02-20 18 | - Removed `fragment` usages 19 | 20 | ## 5.0.2 - 2017-02-16 21 | - Fixed `updateDates` 22 | 23 | ## 5.0.1 - 2017-02-03 24 | - make compatible with `spatie/laravel-medialibrary` v5 25 | 26 | ## 5.0.0 - 2017-02-03 27 | - make compatible with `spatie/laravel-medialibrary` v5 28 | 29 | ## 4.2.14 - 2017-02-16 30 | - Fixed `updateDates` 31 | 32 | ## 4.2.13 - 2017-02-03 33 | - make compatible with `spatie/laravel-medialibrary` v5 34 | 35 | ## 4.2.12 - 2016-01-24 36 | - allow drafts in `Controller::find` 37 | 38 | ## 4.2.9 - 2016-01-24 39 | - improve support for Laravel 5.4 40 | 41 | ## 4.2.8 - 2016-01-13 42 | - rename `temp` to `draft` in media custom properties 43 | 44 | ## 4.2.7 - 2016-01-12 45 | - fix typehint of `hasContentBlocks` 46 | 47 | ## 4.2.6 - 2016-01-12 48 | - fix typehint of `hasContentBlocks` 49 | 50 | ## 4.2.5 - 2016-01-12 51 | - add `hasContentBlocks` 52 | 53 | ## 4.2.4 - 2016-01-12 54 | - Use SortableScope on all front models 55 | 56 | ## 4.2.3 - 2016-01-11 57 | - Content block tweaks 58 | 59 | ## 4.2.2 - 2016-01-10 60 | - Media collection regression fixes 61 | 62 | ## 4.2.0 - 2016-01-10 63 | - Add content blocks 64 | - Transformers now live here too 65 | 66 | ## 4.1.0 - 2016-01-10 67 | - Add sortable scope 68 | 69 | ## 4.0.4 - 2016-01-05 70 | - Fix for scope to filter draft media 71 | 72 | ## 4.0.3 - 2016-01-05 73 | - Use scope to filter draft media 74 | 75 | ## 4.0.2 - 2016-01-05 76 | - Do not display temp media on front site 77 | 78 | ## 4.0.1 - 2016-01-04 79 | - Fix non-draft scope 80 | 81 | ## 4.0.0 - 2016-01-04 82 | - Added `NonDraftScope` and `OnlineScope` 83 | 84 | ## 3.1.1 - 2016-11-29 85 | - The `mediaLibraryCollections` property should now be an associative array with the collection type 86 | 87 | ## 3.0.1 - 2016-11-28 88 | - Fix tag updater 89 | 90 | ## 3.0.0 - 2016-11-28 91 | - Make compatible with laravel-tags 92 | 93 | ## 2.1.3 - 2016-11-23 94 | - Flush cache after `changeOrder` 95 | 96 | ## 2.1.2 - 2016-11-17 97 | - PR #8 98 | 99 | ## 2.1.1 - 2016-11-08 100 | - fix for saving custom properties on media 101 | 102 | ## 2.1.0 - 2016-11-08 103 | - Updated Vuex to 2.0.0-rc 104 | 105 | ## 2.0.1 - 2016-11-07 106 | - remove unused `hasTags` 107 | 108 | ## 2.0.0 - 2016-11-07 109 | - rename all usages of `url` to `slug` 110 | - add support for `spatie/laravel-tags` 111 | 112 | ## 1.0.11 - 2017-01-03 113 | - Fix `updateTranslations` request check 114 | 115 | ## 1.0.10 - 2016-11-23 116 | - Flush cache afters `changeOrder` 117 | 118 | ## 1.0.9 - 2016-10-26 119 | - Fix destroy method 120 | 121 | ## 1.0.8 - 2016-10-25 122 | - Pass all create method parameters to make method 123 | 124 | ## 1.0.7 - 2016-10-25 125 | - Made `DateUpdater` much more generic 126 | 127 | ## 1.0.6 - 2016-10-22 128 | - Allow eloquent-sortable v3 129 | 130 | ## 1.0.5 - 2016-10-20 131 | - Added `updateFields` to `Controller` 132 | 133 | ## 1.0.4 - 2016-10-20 134 | - Fixed `Sortable` check in `Controller` 135 | 136 | ## 1.0.3 - 2016-10-20 137 | - Controller module name resolve fix 138 | - Removed `Repository` 139 | 140 | ## 1.0.2 - 2016-10-20 141 | - Controller module name resolve fix 142 | 143 | ## 1.0.1 - 2016-10-18 144 | - Repository namespace fix 145 | 146 | ## 1.0.0 - 2016-10-18 147 | 148 | - First release 149 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Spatie bvba 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **THIS PACKAGE IS ABANDONED** 2 | 3 | # Base model for entities in our Blender Laravel template 4 | 5 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/blender-model.svg?style=flat-square)](https://packagist.org/packages/spatie/blender-model) 6 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 7 | [![SensioLabsInsight](https://img.shields.io/sensiolabs/i/9b403fd2-e879-4bdb-8368-3f776c27d52a.svg?style=flat-square)](https://insight.sensiolabs.com/projects/9b403fd2-e879-4bdb-8368-3f776c27d52a) 8 | [![Quality Score](https://img.shields.io/scrutinizer/g/spatie/blender-model.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/blender-model) 9 | 10 | See https://github.com/spatie-custom/blender 11 | 12 | Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). 13 | 14 | ## Postcardware 15 | 16 | You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using. 17 | 18 | Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium. 19 | 20 | The best postcards will get published on the open source page on our website. 21 | 22 | ## Installation 23 | 24 | This package is custom built for [Spatie](https://spatie.be) projects and is therefore not registered on packagist. In order to install it via composer you must specify this extra repository in `composer.json`: 25 | 26 | ```json 27 | "repositories": [ { "type": "composer", "url": "https://satis.spatie.be/" } ] 28 | ``` 29 | 30 | You can install the package via composer: 31 | 32 | ``` bash 33 | composer require spatie/blender-model 34 | ``` 35 | 36 | ## Changelog 37 | 38 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 39 | 40 | ## Testing 41 | 42 | ``` bash 43 | $ composer test 44 | ``` 45 | 46 | ## Contributing 47 | 48 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 49 | 50 | ## Security 51 | 52 | If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker. 53 | 54 | ## Credits 55 | 56 | - [Freek Van der Herten](https://github.com/freekmurze) 57 | - [Sebastian De Deyne](https://github.com/sebastiandedeyne) 58 | - [All Contributors](../../contributors) 59 | 60 | ## About Spatie 61 | Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). 62 | 63 | ## License 64 | 65 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 66 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/blender-model", 3 | "description": "Base model for entities in our Blender Laravel template", 4 | "keywords": [ 5 | "spatie", 6 | "blender", 7 | "blender-model" 8 | ], 9 | "homepage": "https://github.com/spatie/blender-model", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Freek Van der Herten", 14 | "email": "freek@spatie.be", 15 | "homepage": "https://spatie.be", 16 | "role": "Developer" 17 | }, 18 | { 19 | "name": "Sebastian De Deyne", 20 | "email": "sebastian@spatie.be", 21 | "homepage": "https://spatie.be", 22 | "role": "Developer" 23 | } 24 | ], 25 | "require": { 26 | "php": "^7.0", 27 | "illuminate/database": "^5.3", 28 | "illuminate/support": "^5.3", 29 | "league/fractal": "^0.14.0", 30 | "nesbot/carbon": "^1.21", 31 | "spatie/eloquent-sortable": "^2.1|^3.0", 32 | "spatie/fractalistic": "^1.0", 33 | "spatie/laravel-medialibrary": "^4.9|^5.0", 34 | "spatie/laravel-model-cleanup": "^1.1", 35 | "spatie/laravel-translatable": "^1.1", 36 | "spatie/regex": "^1.1" 37 | }, 38 | "autoload": { 39 | "psr-4": { 40 | "Spatie\\Blender\\Model\\": "src" 41 | } 42 | }, 43 | "config": { 44 | "sort-packages": true 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/BlenderModelServiceProvider.php: -------------------------------------------------------------------------------- 1 | modelClass = $this->determineModelClass(); 30 | $this->moduleName = $this->determineModuleName(); 31 | } 32 | 33 | public function index() 34 | { 35 | $models = $this->all(); 36 | 37 | return view("back.{$this->moduleName}.index")->with('models', $models); 38 | } 39 | 40 | public function create(...$arguments) 41 | { 42 | $model = $this->make(...$arguments); 43 | 44 | return redirect()->to($this->action('edit', $model->id)); 45 | } 46 | 47 | public function show($id) 48 | { 49 | return redirect()->to($this->action('edit', $id)); 50 | } 51 | 52 | public function edit(int $id) 53 | { 54 | $model = $this->find($id); 55 | 56 | return view("back.{$this->moduleName}.edit") 57 | ->with('model', $model) 58 | ->with('module', $this->moduleName); 59 | } 60 | 61 | public function update(int $id) 62 | { 63 | $formRequest = $this->determineUpdateRequestClass(); 64 | 65 | $request = app()->make($formRequest); 66 | 67 | $model = $this->find($id); 68 | 69 | $this->updateFromRequest($model, $request); 70 | 71 | Cache::flush(); 72 | 73 | $eventDescription = $this->updatedEventDescriptionFor($model); 74 | activity()->on($model)->log($eventDescription); 75 | flash()->success(strip_tags($eventDescription)); 76 | 77 | return redirect()->to( 78 | $this->redirectToIndex ? $this->action('index') : $this->action('edit', $model->id) 79 | ); 80 | } 81 | 82 | public function destroy($id) 83 | { 84 | $model = $this->find($id); 85 | 86 | $eventDescription = $this->deletedEventDescriptionFor($model); 87 | activity()->log($eventDescription); 88 | flash()->success(strip_tags($eventDescription)); 89 | 90 | $model->delete(); 91 | 92 | Cache::flush(); 93 | 94 | return redirect()->to($this->action('index')); 95 | } 96 | 97 | public function changeOrder() 98 | { 99 | call_user_func([$this->modelClass, 'setNewOrder'], request('ids')); 100 | 101 | Cache::flush(); 102 | } 103 | 104 | protected function find(int $id): Eloquent 105 | { 106 | return call_user_func([$this->modelClass, 'where'], 'id', $id) 107 | ->withoutGlobalScope(NonDraftScope::class) 108 | ->firstOrFail(); 109 | } 110 | 111 | protected function all(): Collection 112 | { 113 | return call_user_func([$this->modelClass, 'all']); 114 | } 115 | 116 | protected function determineModelClass(): string 117 | { 118 | return (new ReflectionClass($this)) 119 | ->getMethod('make') 120 | ->getReturnType(); 121 | } 122 | 123 | protected function determineModuleName(): string 124 | { 125 | return lcfirst(str_replace_last('Controller', '', class_basename($this))); 126 | } 127 | 128 | protected function determineUpdateRequestClass(): string 129 | { 130 | return (new ReflectionClass($this)) 131 | ->getMethod('updateFromRequest') 132 | ->getParameters()[1] 133 | ->getClass() 134 | ->getName(); 135 | } 136 | 137 | protected function updateModel(Eloquent $model, FormRequest $request) 138 | { 139 | $this->updateTranslations($model, $request); 140 | $this->updateMedia($model, $request); 141 | $this->updateOnlineToggle($model, $request); 142 | $this->updateDates($model, $request); 143 | $this->updateSeoValues($model, $request); 144 | 145 | $model->save(); 146 | } 147 | 148 | protected function updateFields(Eloquent $model, FormRequest $request, array $fields) 149 | { 150 | collect($fields)->each(function ($field) use ($model, $request) { 151 | $model->$field = $request->get($field, false); 152 | }); 153 | } 154 | 155 | protected function updatedEventDescriptionFor(Eloquent $model): string 156 | { 157 | $modelName = ucfirst(__("back.models.{$this->moduleName}")); 158 | 159 | $linkToModel = '"'.$model->name.'"'; 160 | 161 | if ($model->wasDraft) { 162 | return $modelName.' '.$linkToModel.' '.__('werd aangemaakt'); 163 | } 164 | 165 | return $modelName.' '.$linkToModel.' '.__('werd gewijzigd'); 166 | } 167 | 168 | protected function deletedEventDescriptionFor(Eloquent $model): string 169 | { 170 | $modelName = ucfirst(__("back.models.{$this->moduleName}")); 171 | 172 | return $modelName.' "'.$model->name.'" '.__('werd verwijderd'); 173 | } 174 | 175 | protected function action(string $action, $parameters = []): string 176 | { 177 | return action('\\'.static::class.'@'.$action, $parameters); 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /src/Model.php: -------------------------------------------------------------------------------- 1 | isFront()) { 32 | static::addGlobalScope(new OnlineScope()); 33 | } 34 | } 35 | 36 | public function registerMediaConversions() 37 | { 38 | $this->addMediaConversion('admin') 39 | ->width(368) 40 | ->height(232) 41 | ->nonQueued(); 42 | 43 | $this->addMediaConversion('redactor') 44 | ->width(368) 45 | ->height(232) 46 | ->nonQueued(); 47 | } 48 | 49 | public static function cleanUp(Builder $query): Builder 50 | { 51 | return $query 52 | ->draft() 53 | ->where('created_at', '<', Carbon::now()->subWeek()); 54 | } 55 | 56 | public function defaultSeoValues(): array 57 | { 58 | return [ 59 | 'title' => $this->name, 60 | 'meta_description' => (string) string($this->text)->tease(155), 61 | 'meta_og:title' => $this->name, 62 | 'meta_og:type' => 'website', 63 | 'meta_og:description' => (string) string($this->text)->tease(155), 64 | 'meta_og:image' => $this->hasMedia('images') ? 65 | url($this->getFirstMediaUrl('images')) : 66 | url('/images/og-image.png'), 67 | ]; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Scopes/NonDraftMediaScope.php: -------------------------------------------------------------------------------- 1 | isFront()) { 14 | $builder->where('custom_properties', 'not regexp', '[^"]*"draft"[^:]*:[^"]*true'); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Scopes/NonDraftScope.php: -------------------------------------------------------------------------------- 1 | where('draft', false); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Scopes/OnlineScope.php: -------------------------------------------------------------------------------- 1 | where('online', true); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Scopes/SortableScope.php: -------------------------------------------------------------------------------- 1 | orderBy('order_column'); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Traits/Draftable.php: -------------------------------------------------------------------------------- 1 | wasDraft = $model->isDraft(); 17 | $model->draft = false; 18 | }); 19 | } 20 | 21 | /** 22 | * Determine if this model is a draft. 23 | * 24 | * @return bool 25 | */ 26 | public function isDraft() 27 | { 28 | return $this->draft; 29 | } 30 | 31 | /** 32 | * Get the draft scope. 33 | * 34 | * @param $query 35 | * @param bool $draft set to true to get only the drafts and to false for all non drafts 36 | * 37 | * @return mixed 38 | */ 39 | public function scopeDraft(Builder $query, $draft = true) 40 | { 41 | return $query 42 | ->withoutGlobalScope(NonDraftScope::class) 43 | ->where('draft', $draft); 44 | } 45 | 46 | /** 47 | * Get draft that are older than the given numberOfHours. 48 | * 49 | * @param $query 50 | * @param int $numberOfHours 51 | * 52 | * @return mixed 53 | */ 54 | public function scopeDraftsOlderThanHours($query, $numberOfHours) 55 | { 56 | return $query 57 | ->draft() 58 | ->where('created_at', '<=', Carbon::now()->subHours($numberOfHours)->toDateString()); 59 | } 60 | 61 | /** 62 | * Get the online scope. 63 | * 64 | * @param $query 65 | * 66 | * @return mixed 67 | */ 68 | public function scopeOnline($query) 69 | { 70 | return $query 71 | ->where('online', true); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Traits/HasContentBlocks.php: -------------------------------------------------------------------------------- 1 | morphMany(ContentBlock::class, 'model') 16 | ->whereDraft(false) 17 | ->orderBy('order_column'); 18 | } 19 | 20 | public function getContentBlockCollections(): array 21 | { 22 | return $this->contentBlockCollections ?? ['default']; 23 | } 24 | 25 | public function hasContentBlocks($collection = 'default'): int 26 | { 27 | return $this->getContentBlocks($collection)->count(); 28 | } 29 | 30 | public function getContentBlockMediaLibraryCollections(): array 31 | { 32 | return $this->contentBlockMediaLibraryCollections ?? ['image']; 33 | } 34 | 35 | public function getContentBlocks($collection = 'default'): Collection 36 | { 37 | return $this->contentBlocks->where('collection_name', $collection); 38 | } 39 | 40 | public function syncContentBlocks(Request $request) 41 | { 42 | foreach ($this->getContentBlockCollections() as $collection) { 43 | if (! $request->has("content_blocks_{$collection}")) { 44 | continue; 45 | } 46 | 47 | $this->syncContentBlockCollection( 48 | json_decode($request->get("content_blocks_{$collection}"), true), 49 | $collection 50 | ); 51 | } 52 | } 53 | 54 | protected function syncContentBlockCollection(array $data, string $collection) 55 | { 56 | $contentBlocks = collect($data)->map(function (array $attributes, int $i): ContentBlock { 57 | return ContentBlock::findOrFail($attributes['id']) 58 | ->updateWithAttributes($attributes) 59 | ->setOrder($i); 60 | }); 61 | 62 | $this->contentBlocks() 63 | ->where('collection_name', $collection) 64 | ->whereNotIn('id', $contentBlocks->pluck('id')) 65 | ->delete(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Traits/HasMedia.php: -------------------------------------------------------------------------------- 1 | mediaLibraryCollections ?? []; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Traits/HasSeoValues.php: -------------------------------------------------------------------------------- 1 | defaultSeoValues()) 18 | ->merge(collect($this->seo_values ?? [])->filter()); 19 | } 20 | 21 | return $this->seo()->get($key); 22 | } 23 | 24 | public function renderSeoTags() 25 | { 26 | $html = $this->seo()->filter(function ($value, $key) { 27 | return starts_with($key, 'meta_'); 28 | })->map(function ($value, $key) : string { 29 | $key = substr($key, 5); 30 | $attribute = starts_with($key, 'og:') ? 'property' : 'name'; 31 | 32 | return ""; 33 | })->implode("\n"); 34 | 35 | return new HtmlString($html); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Traits/HasSlug.php: -------------------------------------------------------------------------------- 1 | attributes['slug'] = $model->generateSlug(); 11 | }); 12 | } 13 | 14 | protected function generateSlug(): string 15 | { 16 | $slugs = []; 17 | 18 | foreach ($this->getTranslatedLocales('name') as $locale) { 19 | $slugs[$locale] = str_slug($this->getTranslation('name', $locale)); 20 | } 21 | 22 | return json_encode($slugs); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Transformers/ContentBlockTransformer.php: -------------------------------------------------------------------------------- 1 | $contentBlock->id, 14 | 'type' => $contentBlock->type, 15 | 'orderColumn' => $contentBlock->order_column, 16 | ]; 17 | 18 | return array_merge( 19 | $attributes, 20 | $contentBlock->properties, 21 | $this->getMediaAttributes($contentBlock), 22 | $this->getTranslatedAttributes($contentBlock) 23 | ); 24 | } 25 | 26 | protected function getMediaAttributes(ContentBlock $contentBlock): array 27 | { 28 | return array_reduce($contentBlock->mediaLibraryCollections(), function ($mediaAttributes, $collectionName) use ($contentBlock) { 29 | $mediaAttributes[$collectionName] = fractal($contentBlock->getMedia($collectionName), new MediaTransformer())->toArray(); 30 | 31 | return $mediaAttributes; 32 | }, []); 33 | } 34 | 35 | protected function getTranslatedAttributes(ContentBlock $contentBlock): array 36 | { 37 | return array_reduce($contentBlock->translatable, function ($translatables, $attribute) use ($contentBlock) { 38 | foreach (config('app.locales') as $locale) { 39 | $translatables[$attribute][$locale] = $contentBlock->getTranslation($attribute, $locale); 40 | } 41 | 42 | return $translatables; 43 | }, []); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Transformers/MediaTransformer.php: -------------------------------------------------------------------------------- 1 | $media->id, 14 | 'name' => $media->name, 15 | 'collection' => $media->collection_name, 16 | 'fileName' => $media->file_name, 17 | 'customProperties' => $media->custom_properties, 18 | 'orderColumn' => $media->order_column, 19 | 'thumbUrl' => strtolower($media->extension) === 'svg' ? 20 | $media->getUrl() : 21 | $media->getUrl('admin'), 22 | 'originalUrl' => $media->getUrl(), 23 | ]; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Updaters/UpdateDates.php: -------------------------------------------------------------------------------- 1 | getDates() as $dateAttribute) { 14 | if ($request->has($dateAttribute)) { 15 | $model->$dateAttribute = Carbon::createFromFormat('d/m/Y', $request->get($dateAttribute)); 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Updaters/UpdateMedia.php: -------------------------------------------------------------------------------- 1 | mediaLibraryCollections() as $collection) { 14 | if (! $request->has($collection)) { 15 | continue; 16 | } 17 | 18 | $model->updateMedia( 19 | $this->convertKeysToSnakeCase(json_decode($request->get($collection), true)), 20 | $collection 21 | )->each(function (Media $media) { 22 | $media->setCustomProperty('draft', false); 23 | $media->save(); 24 | }); 25 | } 26 | } 27 | 28 | protected function convertKeysToSnakeCase(array $array): array 29 | { 30 | return collect($array)->map(function ($mediaProperties) { 31 | return collect($mediaProperties)->keyBy(function ($value, $key) { 32 | return snake_case($key); 33 | }); 34 | })->toArray(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Updaters/UpdateOnlineToggle.php: -------------------------------------------------------------------------------- 1 | online = $request->get('online') ?? false; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Updaters/UpdateSeoValues.php: -------------------------------------------------------------------------------- 1 | all()) 16 | ->filter(function ($value, $fieldName) { 17 | // Filter out everything that starts with 'translated__seo_' 18 | return Regex::match('/^translated_([a-z][a-z])_seo_/', $fieldName)->hasMatch(); 19 | }) 20 | ->map(function ($value, $fieldName) { 21 | 22 | // Replace 'translated__seo_' with '_' 23 | $localeAndAttribute = Regex::replace('/translated_([a-z][a-z])_seo_/', function (MatchResult $matchResult) { 24 | return $matchResult->group(1).'_'; 25 | }, $fieldName)->result(); 26 | 27 | $localeAndAttribute = explode('_', $localeAndAttribute, 2); 28 | 29 | return [ 30 | 'locale' => $localeAndAttribute[0], 31 | 'attribute' => $localeAndAttribute[1], 32 | 'value' => $value, 33 | ]; 34 | }) 35 | ->groupBy('locale') 36 | ->map(function (Collection $valuesInLocale) { 37 | return $valuesInLocale->mapWithKeys(function ($values) { 38 | return [$values['attribute'] => $values['value']]; 39 | }); 40 | }) 41 | ->each(function ($values, $locale) use ($model) { 42 | $model->setTranslation('seo_values', $locale, $values); 43 | }); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Updaters/UpdateTags.php: -------------------------------------------------------------------------------- 1 | tags()->detach(); 14 | 15 | foreach ($model->tagTypes as $type) { 16 | collect($request->get("{$type}_tags"))->each(function ($name) use ($model, $type) { 17 | $tag = Tag::findOrCreate($name, $type); 18 | 19 | $model->tags()->attach($tag); 20 | }); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Updaters/UpdateTranslations.php: -------------------------------------------------------------------------------- 1 | getTranslatableAttributes() as $fieldName) { 14 | $translatedFieldName = translate_field_name($fieldName, $locale); 15 | 16 | if (! $request->exists($translatedFieldName)) { 17 | continue; 18 | } 19 | 20 | $model->setTranslation($fieldName, $locale, $request->get($translatedFieldName)); 21 | } 22 | } 23 | } 24 | } 25 | --------------------------------------------------------------------------------