Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated Browser and Repeater Fields #400

Merged
merged 11 commits into from
Nov 19, 2019
139 changes: 139 additions & 0 deletions src/Repositories/Behaviors/HandleBrowsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace A17\Twill\Repositories\Behaviors;

trait HandleBrowsers
{
/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @return void
*/
public function afterSaveHandleBrowsers($object, $fields)
{
if (property_exists($this, 'browsers')) {
foreach ($this->browsers as $module) {
if (is_string($module)) {
$this->updateBrowser($object, $fields, $module, 'position', $module);
} elseif (is_array($module)) {
$browserName = !empty($module['browserName']) ? $module['browserName'] : key($module);
$relation = !empty($module['relation']) ? $module['relation'] : key($module);
$positionAttribute = !empty($module['positionAttribute']) ? $module['positionAttribute'] : 'position';
$this->updateBrowser($object, $fields, $relation, $positionAttribute, $browserName);
}
}
}
}

/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @return array
*/
public function getFormFieldsHandleBrowsers($object, $fields)
{
if (property_exists($this, 'browsers')) {
foreach ($this->browsers as $module) {
if (is_string($module)) {
$fields['browsers'][$module] = $this->getFormFieldsForBrowser($object, $module, null, 'title', null);
} elseif (is_array($module)) {
$relation = !empty($module['relation']) ? $module['relation'] : key($module);
$routePrefix = isset($module['routePrefix']) ? $module['routePrefix'] : null;
$titleKey = !empty($module['titleKey']) ? $module['titleKey'] : 'title';
$moduleName = isset($module['moduleName']) ? $module['moduleName'] : null;
$browserName = !empty($module['browserName']) ? $module['browserName'] : key($module);
$fields['browsers'][$browserName] = $this->getFormFieldsForBrowser($object, $relation, $routePrefix, $titleKey, $moduleName);
}
}
}

return $fields;
}

/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @param string $relationship
* @param string $positionAttribute
* @return void
*/
public function updateBrowser($object, $fields, $relationship, $positionAttribute = 'position', $browserName = null)
{
$browserName = $browserName ?? $relationship;
$fieldsHasElements = isset($fields['browsers'][$browserName]) && !empty($fields['browsers'][$browserName]);
$relatedElements = $fieldsHasElements ? $fields['browsers'][$browserName] : [];
$relatedElementsWithPosition = [];
$position = 1;
foreach ($relatedElements as $relatedElement) {
$relatedElementsWithPosition[$relatedElement['id']] = [$positionAttribute => $position++];
}

$object->$relationship()->sync($relatedElementsWithPosition);
}

/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @param string $relationship
* @param string $positionAttribute
* @return void
*/
public function updateOrderedBelongsTomany($object, $fields, $relationship, $positionAttribute = 'position') {
$this->updateBrowser($object, $fields, $relationship, $positionAttribute);
}

/**
* @param mixed $object
* @param array $fields
* @param string $browserName
* @return void
*/
public function updateRelatedBrowser($object, $fields, $browserName)
{
$object->saveRelated($fields['browsers'][$browserName] ?? [], $browserName);
}

/**
* @param \A17\Twill\Models\Model $object
* @param string $relation
* @param string|null $routePrefix
* @param string $titleKey
* @param string|null $moduleName
* @return array
*/
public function getFormFieldsForBrowser($object, $relation, $routePrefix = null, $titleKey = 'title', $moduleName = null)
{
return $object->$relation->map(function ($relatedElement) use ($titleKey, $routePrefix, $relation, $moduleName) {
return [
'id' => $relatedElement->id,
'name' => $relatedElement->titleInBrowser ?? $relatedElement->$titleKey,
'edit' => moduleRoute($moduleName ?? $relation, $routePrefix ?? '', 'edit', $relatedElement->id),
'endpointType' => $relatedElement->getMorphClass(),
] + (classHasTrait($relatedElement, HasMedias::class) ? [
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
] : []);
})->toArray();
}

/**
* @param \A17\Twill\Models\Model $object
* @param string $relation
* @return array
*/
public function getFormFieldsForRelatedBrowser($object, $relation)
{
return $object->getRelated($relation)->map(function ($relatedElement) {
return ($relatedElement != null) ? [
'id' => $relatedElement->id,
'name' => $relatedElement->titleInBrowser ?? $relatedElement->title,
'endpointType' => $relatedElement->getMorphClass(),
] + (($relatedElement->adminEditUrl ?? null) ? [] : [
'edit' => $relatedElement->adminEditUrl,
]) + (classHasTrait($relatedElement, HasMedias::class) ? [
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
] : []) : [];
})->reject(function ($item) {
return empty($item);
})->values()->toArray();
}
}
49 changes: 49 additions & 0 deletions src/Repositories/Behaviors/HandleRepeaters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,59 @@

use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;

trait HandleRepeaters
{
/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @return void
*/
public function afterSaveHandleRepeaters($object, $fields)
{
if (property_exists($this, 'repeaters')) {
foreach ($this->repeaters as $module) {
if (is_string($module)) {
$model = Str::studly(Str::singular($module));
$repeaterName = Str::singular($module);
$this->updateRepeater($object, $fields, $module, $model, $repeaterName);
} elseif (is_array($module)) {
$relation = !empty($module['relation']) ? $module['relation'] : key($module);
$model = isset($module['model']) ? $module['model'] : Str::studly(Str::singular(key($module)));
$repeaterName = !empty($module['repeaterName']) ? $module['repeaterName'] : Str::singular(key($module));
$this->updateRepeater($object, $fields, $relation, $model, $repeaterName);
}
}
}
}

/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @return array
*/
public function getFormFieldsHandleRepeaters($object, $fields)
{
if (property_exists($this, 'repeaters')) {
foreach ($this->repeaters as $module) {
if (is_string($module)) {
$model = Str::studly(Str::singular($module));
$repeaterName = Str::singular($module);
$fields = $this->getFormFieldsForRepeater($object, $fields, $module, $model, $repeaterName);
} elseif (is_array($module)) {
$model = isset($module['model']) ? $module['model'] : Str::studly(Str::singular(key($module)));
$relation = !empty($module['relation']) ? $module['relation'] : key($module);
$repeaterName = !empty($module['repeaterName']) ? $module['repeaterName'] : Str::singular(key($module));
$fields = $this->getFormFieldsForRepeater($object, $fields, $relation, $model, $repeaterName);
}
}
}

return $fields;
}

public function updateRepeaterMany($object, $fields, $relation, $keepExisting = true, $model = null)
{
$relationFields = $fields['repeaters'][$relation] ?? [];
Expand Down
30 changes: 30 additions & 0 deletions src/Repositories/Behaviors/HandleRevisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@

trait HandleRevisions
{
public function hydrateHandleRevisions($object, $fields)
{
if (property_exists($this, 'browsers')) {
foreach ($this->browsers as $module) {
if (is_string($module)) {
$this->hydrateBrowser($object, $fields, $module);
} elseif (is_array($module)) {
$relation = !empty($module['relation']) ? $module['relation'] : key($module);
$positionAttribute = !empty($module['positionAttribute']) ? $module['positionAttribute'] : 'position';
$model = isset($module['model']) ? $module['model'] : null;
$this->hydrateBrowser($object, $fields, $relation, $positionAttribute, $model);
}
}
}

if (property_exists($this, 'repeaters')) {
foreach ($this->repeaters as $module) {
if (is_string($module)) {
$model = Str::studly(Str::singular($module));
$this->hydrateRepeater($object, $fields, $module, $model);
} elseif (is_array($module)) {
$relation = !empty($module['relation']) ? $module['relation'] : key($module);
$model = isset($module['model']) ? $module['model'] : Str::studly(Str::singular(key($module)));
$this->hydrateRepeater($object, $fields, $relation, $model);
}
}
}

return $object;
}

public function beforeSaveHandleRevisions($object, $fields)
{
Expand Down
92 changes: 3 additions & 89 deletions src/Repositories/ModuleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use A17\Twill\Models\Behaviors\HasMedias;
use A17\Twill\Models\Behaviors\Sortable;
use A17\Twill\Repositories\Behaviors\HandleDates;
use A17\Twill\Repositories\Behaviors\HandleBrowsers;
use A17\Twill\Repositories\Behaviors\HandleFieldsGroups;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand All @@ -17,8 +18,8 @@

abstract class ModuleRepository
{
use HandleDates, HandleFieldsGroups;

use HandleDates, HandleBrowsers, HandleFieldsGroups;
/**
* @var \A17\Twill\Models\Model
*/
Expand Down Expand Up @@ -660,50 +661,6 @@ public function order($query, array $orders = [])
return $query;
}

/**
* @param \A17\Twill\Models\Model $object
* @param string $relation
* @param string|null $routePrefix
* @param string $titleKey
* @param string|null $moduleName
* @return array
*/
public function getFormFieldsForBrowser($object, $relation, $routePrefix = null, $titleKey = 'title', $moduleName = null)
{
return $object->$relation->map(function ($relatedElement) use ($titleKey, $routePrefix, $relation, $moduleName) {
return [
'id' => $relatedElement->id,
'name' => $relatedElement->titleInBrowser ?? $relatedElement->$titleKey,
'edit' => moduleRoute($moduleName ?? $relation, $routePrefix ?? '', 'edit', $relatedElement->id),
'endpointType' => $relatedElement->getMorphClass(),
] + (classHasTrait($relatedElement, HasMedias::class) ? [
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
] : []);
})->toArray();
}

/**
* @param \A17\Twill\Models\Model $object
* @param string $relation
* @return array
*/
public function getFormFieldsForRelatedBrowser($object, $relation)
{
return $object->getRelated($relation)->map(function ($relatedElement) {
return ($relatedElement != null) ? [
'id' => $relatedElement->id,
'name' => $relatedElement->titleInBrowser ?? $relatedElement->title,
'endpointType' => $relatedElement->getMorphClass(),
] + (($relatedElement->adminEditUrl ?? null) ? [] : [
'edit' => $relatedElement->adminEditUrl,
]) + (classHasTrait($relatedElement, HasMedias::class) ? [
'thumbnail' => $relatedElement->defaultCmsImage(['w' => 100, 'h' => 100]),
] : []) : [];
})->reject(function ($item) {
return empty($item);
})->values()->toArray();
}

/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
Expand All @@ -729,49 +686,6 @@ public function updateOneToMany($object, $fields, $relationship, $formField, $at
}
}

/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @param string $relationship
* @param string $positionAttribute
* @return void
*/
public function updateOrderedBelongsTomany($object, $fields, $relationship, $positionAttribute = 'position')
{
$fieldsHasElements = isset($fields['browsers'][$relationship]) && !empty($fields['browsers'][$relationship]);
$relatedElements = $fieldsHasElements ? $fields['browsers'][$relationship] : [];
$relatedElementsWithPosition = [];
$position = 1;
foreach ($relatedElements as $relatedElement) {
$relatedElementsWithPosition[$relatedElement['id']] = [$positionAttribute => $position++];
}

$object->$relationship()->sync($relatedElementsWithPosition);
}

/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
* @param string $relationship
* @param string $positionAttribute
* @return void
*/
public function updateBrowser($object, $fields, $relationship, $positionAttribute = 'position')
{
$this->updateOrderedBelongsTomany($object, $fields, $relationship, $positionAttribute);
}

/**
* @param mixed $object
* @param array $fields
* @param string $browserName
* @return void
*/
public function updateRelatedBrowser($object, $fields, $browserName)
{
$object->saveRelated($fields['browsers'][$browserName] ?? [], $browserName);
}

/**
* @param \A17\Twill\Models\Model $object
* @param array $fields
Expand Down