<?php
namespace App\Admin\Sections\Core;
use AdminDisplay;
use SleepingOwl\Admin\Section as SleepingOwlSection;
use AdminForm;
use AdminFormElement;
use Illuminate\Database\Eloquent\Model;
use App\Core\Translit;
use App\Models\Core\System\Fields\Table;
use App\Models\Core\System\Fields\Values;
/**
* Description of Section
*
* @author php-jun
*/
class CoreSectionTest extends SleepingOwlSection {
protected $_defaultFireEdit = false;
protected static $_priority = [
'settings' => 1,
'menu' => 2,
'static' => 3,
'news' => 4,
'catalog' => 5,
'sliders' => 6,
'forms' => 7,
];
//protected $_exceptTables = ['static_texts', 'static_pages', 'fundamental_settings'];
protected static $_fieldTypes = [
'text' => 'text', 'textarea' => 'textarea', 'checkbox' => 'checkbox', 'select' => 'select', 'multiselect' => 'multiselect', 'wysiwyg' => 'wysiwyg', 'radio' => 'radio', 'image' => 'image',
'hidden' => 'hidden', 'images' => 'images', 'file' => 'file'
];
protected function setMeta($moduleName = '', $type = '', $route = '') {
$meta = AdminDisplay::tab(AdminForm::elements([
AdminFormElement::text('meta.alias', 'Alias'),
AdminFormElement::text('meta.meta_title', 'Meta title'),
AdminFormElement::hidden('meta.module')->setDefaultValue($moduleName),
AdminFormElement::hidden('meta.type')->setDefaultValue($type),
AdminFormElement::text('meta.meta_description', 'Meta description'),
AdminFormElement::text('meta.meta_keywords', 'Meta Keywords'),
AdminFormElement::hidden('meta.route')->setDefaultValue($route)
]))->setLabel('Meta');
return $meta;
}
protected function getSystemFields($module, $type, $id = null, $populate = false, $except = []) {
$fields = Table::where('module', $module)->where('type', $type)->orderBy('ordering', 'asc')->get();
if ($populate && $id) {
$values = Values::where('module', $module)->where('type', $type)->where('data_id', $id)->get()->toArray();
$values = \App\Helpers\Arr::setKeys($values, 'field_id');
}
$elements = [];
AdminDisplay::tab(AdminForm::elements());
foreach ($fields as $field) {
if (in_array($field->alias, $except)) {
continue;
}
if ($field->field_type === 'images') {
$element = AdminFormElement::{$field->field_type}("images.path", $field->title)->setView(view('sleepingowl.form.element.images'))->setUploadFileName(function(\Illuminate\Http\UploadedFile $file) {
$pathDirectory = public_path() . '/' . \Config::get('sleeping_owl.imagesUploadDirectory');
$filename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$translit = Translit::translit($filename);
$extension = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
$fullPath = $pathDirectory . '/' . $translit . '.' . $extension;
if (file_exists($fullPath)) {
$translit = Translit::translit($filename) . time();
}
return $translit . '.' . $extension;
});
} elseif ($field->field_type === 'file') {
$element = AdminFormElement::{$field->field_type}("main[" . $field->alias . "]", $field->title)->setUploadFileName(function(\Illuminate\Http\UploadedFile $file) {
$pathDirectory = public_path() . '/' . \Config::get('sleeping_owl.filesUploadDirectory');
$filename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$translit = Translit::translit($filename);
$extension = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
$fullPath = $pathDirectory . '/' . $translit . '.' . $extension;
if (file_exists($fullPath)) {
$translit = Translit::translit($filename) . time();
}
return $translit . '.' . $extension;
})->setValueSkipped(true);
} else {
$element = AdminFormElement::{$field->field_type}("main[" . $field->alias . "]", $field->title)->setValueSkipped(true);
}
if ($populate && isset($values[$field['id']])) {
$element = $element->setDefaultValue($values[$field['id']]['value']);
}
// if (mb_strlen($field) > 0) {
//// $element->setDefaultValue(json_decode($field->default_values, true));
// }
array_push($elements, $element);
}
return $elements;
}
/**
*
* @param Model $model
* @param string $moduleName
* @param string $type
*/
protected function validMeta(Model $model, $moduleName = null, $type = null, $translitAlias = null) {
if (!$moduleName) {
$moduleName = $model::MODULE_NAME;
}
if (!$type) {
$type = $model::META_TYPE;
}
if (!mb_strlen($model->meta->module)) {
$model->meta->module = $moduleName;
}
if (!mb_strlen($model->meta->type)) {
$model->meta->type = $type;
}
// if (!mb_strlen($model->meta->alias)) {
if (isset($model->{$this->fieldTranslit}) && mb_strlen($model->{$this->fieldTranslit}) > 0) {
$model->meta->alias = Translit::translit($model->{$this->fieldTranslit});
} else {
$model->meta->alias = Translit::translit($translitAlias);
}
// }
if (!mb_strlen($model->module)) {
$model->module = $moduleName;
}
if (!mb_strlen($model->type)) {
$model->type = $type;
}
}
/**
*
* @param Model $model
* @param string $moduleName
* @param string $type
*/
protected function validImagesMeta(Model $model, $moduleName = null, $type = null) {
if (!isset($model->images)) {
return;
}
if (!$model->images->path) {
$model->images->delete();
unset($model->images);
return false;
}
if (!$moduleName) {
$moduleName = $model::MODULE_NAME;
}
if (!$type) {
$type = $model::META_TYPE;
}
if (!mb_strlen($model->images->module)) {
$model->images->module = $moduleName;
}
if (!mb_strlen($model->images->type)) {
$model->images->type = $type;
}
}
/**
*
* @param Model $model
* @param string $moduleName
* @param string $type
*/
protected function validFilesMeta(Model $model, $moduleName = null, $type = null) {
if (!isset($model->files)) {
return;
}
if (!$model->files->path) {
$model->files->delete();
unset($model->files);
return false;
}
if (!$moduleName) {
$moduleName = $model::MODULE_NAME;
}
if (!$type) {
$type = $model::META_TYPE;
}
if (!mb_strlen($model->files->module)) {
$model->files->module = $moduleName;
}
if (!mb_strlen($model->files->type)) {
$model->files->type = $type;
}
}
public function fireEdit($id) {
//return parent::fireEdit($id);
if ($this->_defaultFireEdit) {
setcookie('site_view_url', null, -1);
return parent::fireEdit($id);
} else {
$data = $this->model->get($id);
if ($data) {
$meta = $data['meta'];
$meta['id'] = $meta['data_id'];
$url = parse_url(route($meta['route'], $meta), PHP_URL_PATH);
//$_COOKIE['site_view_url'] = $url;
if (mb_strlen($url)) {
setcookie('site_view_url', $url);
\Meta::addJS('admin-view-content', resources_url('js/admin/core/view-content.js'), ['admin-default']);
} else {
setcookie('site_view_url', null, -1);
}
}
return parent::fireEdit($id);
}
}
}