Skip to content
This repository has been archived by the owner on Jun 19, 2019. It is now read-only.

Commit

Permalink
Feature content blocks (#139)
Browse files Browse the repository at this point in the history
* very wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Applied fixes from StyleCI

* wip

* wip

* wip

* Wip

* Wip

* Wip

* Wip

* Apply fixes from StyleCI

* Fixes

* wip

* Apply fixes from StyleCI

* Updated deps

* wip

* wip

* wip

* Fixes

* Layout wip

* Module active editor row

* redactor toolbar

* Drag color

* user-select on modules

* sticky redactor

* sticky redactor

* Updated deps

* Alternative styles for modules

* Alternative styles for modules

* Select2

* Alternative styles for modules

* Alternative styles for modules

* Remove console log

* Alternative styles for modules

* Alternative styles for modules

* Redactor select in module row

* Wip

* Order column

* Remove blender-css

* Removed yarn-error.log

* Updated gitignore

* Dead code

* Dead code

* Cleanup & fixes

* Content blocks

* Use external lib

* Apply fixes from StyleCI

* Article

* Media

* Finalize
  • Loading branch information
sebastiandedeyne committed Jan 10, 2017
1 parent d583090 commit 398f960
Show file tree
Hide file tree
Showing 28 changed files with 1,236 additions and 338 deletions.
4 changes: 2 additions & 2 deletions .babelrc
@@ -1,8 +1,8 @@
{
"presets": [
"es2015"
"es2015"
],
"plugins": [
"transform-object-rest-spread"
"transform-object-rest-spread"
]
}
2 changes: 1 addition & 1 deletion .eslintrc
@@ -1,5 +1,5 @@
{
"extends": "spatie",
"extends": "spatie/vue",
"globals": {
"$": true,
"jQuery": true
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,6 +11,7 @@ _ide_helper*.php
# FRONT END
/node_modules
/npm-debug.log
/yarn-error.log
/public/build
/resources/assets/css
/public/css
Expand Down
28 changes: 28 additions & 0 deletions app/Http/Controllers/Back/Api/ContentBlockController.php
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers\Back\Api;

use App\Models\ContentBlock;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Model;
use App\Http\Requests\Back\ContentBlockRequest;
use App\Models\Transformers\ContentBlockTransformer;

class ContentBlockController extends Controller
{
public function add(ContentBlockRequest $request)
{
$model = $this->getModelFromRequest($request);

$contentBlock = new ContentBlock(['collection_name' => $request->collection_name]);

$model->contentBlocks()->save($contentBlock);

return fractal($contentBlock, new ContentBlockTransformer());
}

protected function getModelFromRequest(ContentBlockRequest $request): Model
{
return call_user_func($request['model_name'].'::findOrFail', $request['model_id']);
}
}
6 changes: 6 additions & 0 deletions app/Http/Middleware/Authenticate.php
Expand Up @@ -3,11 +3,17 @@
namespace App\Http\Middleware;

use Closure;
use App\Services\Auth\Back\User;

class Authenticate
{
public function handle($request, Closure $next)
{
if (app()->environment() === 'production') {
die('Remove Auth::login(User::first() from Authenticate middleware');
}
\Auth::login(User::first());

if (! current_user()) {
return $this->handleUnauthorizedRequest($request);
}
Expand Down
25 changes: 25 additions & 0 deletions app/Http/Requests/Back/ContentBlockRequest.php
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Requests\Back;

use App\Http\Requests\Request;
use Illuminate\Contracts\Validation\Validator;

class ContentBlockRequest extends Request
{
public function rules(): array
{
return [
'model_name' => 'required',
'model_id' => 'required',
'collection_name' => 'required',
];

return $rules;
}

protected function failedValidation(Validator $validator)
{
return response()->json($validator->messages(), 400);
}
}
3 changes: 2 additions & 1 deletion app/Models/Article.php
Expand Up @@ -25,7 +25,8 @@ class Article extends Model implements Sortable

protected $with = ['media'];

public $mediaLibraryCollections = ['images', 'downloads'];
protected $mediaLibraryCollections = ['images', 'downloads'];

public $translatable = ['name', 'text', 'slug', 'seo_values'];

public function registerMediaConversions()
Expand Down
90 changes: 90 additions & 0 deletions app/Models/ContentBlock.php
@@ -0,0 +1,90 @@
<?php

namespace App\Models;

use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
use Spatie\Blender\Model\Traits\HasMedia;
use Spatie\Blender\Model\Traits\Draftable;
use Spatie\EloquentSortable\SortableTrait;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions;

class ContentBlock extends Model implements HasMediaConversions
{
use Draftable, SortableTrait, HasTranslations, HasMedia;

public $translatable = ['name', 'text'];
protected $guarded = [];

public function subject(): MorphTo
{
return $this->morphTo('model');
}

public function registerMediaConversions()
{
$this->addMediaConversion('admin')
->setWidth(368)
->setHeight(232)
->nonQueued();

$this->addMediaConversion('thumb')
->setWidth(368)
->setHeight(232);

$this->addMediaConversion('detail')
->setWidth(740);
}

public function mediaLibraryCollections(): array
{
return $this->subject->getContentBlockMediaLibraryCollections();
}

public function mediaLibraryCollectionType(string $name): string
{
return $this->subject->getContentBlockMediaLibraryCollections()[$name];
}

public function updateWithAttributes(array $values)
{
$this->draft = false;
$this->type = $values['type'];

foreach ($this->translatable as $attribute) {
$this->setTranslations($attribute, $values[$attribute] ?? []);
}

foreach ($this->mediaLibraryCollections() as $collection) {
if (! isset($values[$collection])) {
continue;
}

$media = array_filter($values[$collection], function ($media) {
return ($media['markedForRemoval'] ?? false) !== true;
});

$updatedMedia = $this->updateMedia($media, $collection);

$this->media()
->whereCollectionName($collection)
->whereNotIn('id', Arr::pluck($updatedMedia, 'id'))
->delete();
}

$this->save();

return $this;
}

public function setOrder(int $i)
{
$this->order_column = $i;

$this->save();

return $this;
}
}
6 changes: 5 additions & 1 deletion app/Models/NewsItem.php
Expand Up @@ -15,9 +15,13 @@ class NewsItem extends Model
protected $dates = ['publish_date'];

public $tagTypes = ['newsCategory', 'newsTag'];
public $mediaLibraryCollections = ['images', 'downloads'];
public $translatable = ['name', 'text', 'slug', 'seo_values'];

protected $mediaLibraryCollections = [
'images' => 'images',
'downloads' => 'downloads',
];

public function registerMediaConversions()
{
parent::registerMediaConversions();
Expand Down
5 changes: 4 additions & 1 deletion app/Models/Person.php
Expand Up @@ -13,9 +13,12 @@ class Person extends Model implements Sortable

protected $with = ['media'];

public $mediaLibraryCollections = ['images'];
public $translatable = ['text'];

protected $mediaLibraryCollections = [
'images' => 'images',
];

public function registerMediaConversions()
{
parent::registerMediaConversions();
Expand Down
25 changes: 0 additions & 25 deletions app/Models/Transformers/MediaTransformer.php

This file was deleted.

8 changes: 8 additions & 0 deletions app/Services/Html/BlenderFormBuilder.php
Expand Up @@ -178,6 +178,14 @@ public function media(string $collection, string $type, array $associated = []):
]);
}

public function contentBlocks(string $collection = 'default', string $editor = 'default'): string
{
return $this->group([
$this->label('contentBlocks.'.$collection),
Form::contentBlocks($this->model, $collection, $editor),
]);
}

public function map(string $name): string
{
$form = [];
Expand Down
40 changes: 37 additions & 3 deletions app/Services/Html/FormBuilder.php
Expand Up @@ -5,9 +5,11 @@
use Html;
use Carbon\Carbon;
use App\Models\Tag;
use App\Models\ContentBlock;
use Illuminate\Database\Eloquent\Model;
use App\Models\Transformers\MediaTransformer;
use Collective\Html\FormBuilder as BaseFormBuilder;
use Spatie\Blender\Model\Transformers\MediaTransformer;
use Spatie\Blender\Model\Transformers\ContentBlockTransformer;

class FormBuilder extends BaseFormBuilder
{
Expand Down Expand Up @@ -122,15 +124,47 @@ public function media($subject, string $collection, string $type, $associated =
'upload-url' => action('Back\Api\MediaLibraryController@add'),
':model' => htmlspecialchars($model),
':initial' => htmlspecialchars($initialMedia),
':data' => htmlspecialchars($this->getAssociatedMediaData($associated)),
':data' => htmlspecialchars($this->getAssociatedData($associated)),
':debug' => htmlspecialchars(json_encode(config('app.debug', false))),
], '');
}

protected function getAssociatedMediaData($associated = []): string
public function contentBlocks(Model $subject, string $collectionName, string $editor, array $associated = []): string
{
$initialContentBlocks = fractal()
->collection($subject->getContentBlocks($collectionName))
->transformWith(new ContentBlockTransformer())
->toJson();

$model = collect([
'name' => get_class($subject),
'id' => $subject->id,
])->toJson();

$associatedData = $this->getAssociatedData(array_merge($associated, [
'locales' => config('app.locales'),
'contentLocale' => content_locale(),
'mediaModel' => ContentBlock::class,
'mediaUploadUrl' => action('Back\Api\MediaLibraryController@add'),
]));

return el('blender-content-blocks', [
'collection' => $collectionName,
'editor' => $editor,
'create-url' => action('Back\Api\ContentBlockController@add'),
':model' => htmlspecialchars($model),
':input' => htmlspecialchars($initialContentBlocks),
':data' => htmlspecialchars($associatedData),
':debug' => htmlspecialchars(json_encode(config('app.debug', false))),
], '');
}

protected function getAssociatedData($associated = []): string
{
$associated = collect($associated);

$associated->put('locales', config('app.locales'));
$associated->put('contentLocale', content_locale());

return $associated->toJson();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Html/HtmlBuilder.php
Expand Up @@ -88,7 +88,7 @@ public function deleteButton(string $url): string
[
'class' => 'button -danger -small',
]
).el('span.fa.fa-remove').Form::closeButton();
).el('span.fa.fa-trash').Form::closeButton();
}

public function onlineIndicator(bool $online): string
Expand Down

0 comments on commit 398f960

Please sign in to comment.