sleeping owl

← разместить еще код

sleeping

Расшаренный код:

<?php

namespace App\Http\Section;

use File;
use App\Rating;
use App\Tag;
use Illuminate\Database\Eloquent\Model;
use SleepingOwl\Admin\Contracts\Display\DisplayInterface;
use SleepingOwl\Admin\Contracts\Form\FormInterface;
use SleepingOwl\Admin\Contracts\Initializable;
use SleepingOwl\Admin\Section;
use AdminDisplay;
use AdminColumn;
use AdminForm;
use AdminFormElement;


/**
 * Class Training
 *
 * @property \App\Training $model
 *
 * @see http://sleepingowladmin.ru/docs/model_configuration_section
 */
class Training extends Section implements Initializable
{
    protected $size = '10737418240';
    protected $model = '\App\Training';
    /**
     * @see http://sleepingowladmin.ru/docs/model_configuration#ограничение-прав-доступа
     *
     * @var bool
     */
    protected $checkAccess = false;
    /**
     * @var string
     */
    protected $title;
    /**
     * @var string
     */
    protected $alias;

    public function initialize()
    {
        // Добавление пункта меню и счетчика кол-ва записей в разделе
        $this->addToNavigation($priority = 500, function () {
            return \App\Training::count();
        });

        $this->creating(function ($config, \Illuminate\Database\Eloquent\Model $model) {
            //...
        });
    }

    public function deleteImageVideo($local, $imageUrl)
    {
        if (File::exists(public_path() . '/' . $local)) ;
        {

            File::delete(public_path() . '/' . $local);
        }
        if (File::exists(public_path() . '/' . $imageUrl)) ;
        {

            File::delete(public_path() . '/' . $imageUrl);
        }
        return true;
    }

    public function createImageFromVideo($local_url)
    {
        $video_path = public_path() . '/' . $local_url;
        $image_path = 'files/uploads/images/' . str_random(10) . '.jpg';

        $preview_path = public_path() . '/' . $image_path;

        exec(escapeshellcmd('convert ' . escapeshellarg($video_path) . '[100] ' . escapeshellarg($preview_path)));
        return $image_path;
    }

    /**
     * @return DisplayInterface
     */
    public function onDisplay()
    {
        return AdminDisplay::datatables()/*->with('users')*/
        ->setHtmlAttribute('class', 'table-primary')
            ->setColumns(
                AdminColumn::text('id', '#')->setWidth('30px'),
                AdminColumn::link('title', 'Title')->setWidth('200px'),
                // AdminColumn::text('description', 'Description'),
                AdminColumn::custom('Description', function (\Illuminate\Database\Eloquent\Model $model) {
                    return strip_tags(str_limit($model->description, 50));
                }),
                //AdminColumn::text('Lang', 'language'),
                AdminColumn::lists('tags.name', 'Tag'),
                AdminColumn::lists('categories.name', 'Category'),
                AdminColumn::custom('Rate', function (\Illuminate\Database\Eloquent\Model $model) {
                    $rate = (int)Rating::where('training_id', $model->id)->avg('rate');
                    return $rate;
                }),
                AdminColumn::custom('Lang', function (\Illuminate\Database\Eloquent\Model $model) {
                    return $model->lang->name;
                })

            )->paginate(20);
        // todo: remove if unused
    }

    /**
     *
     * @param int $id
     *
     * @return FormInterface
     */
    public function onEdit($id)
    {
        return AdminForm::panel()->addBody([
            AdminFormElement::text('title', 'Title')->required(),
            AdminFormElement::wysiwyg('description', 'Description')->required(),
            //AdminFormElement::select('language', 'Language')->setOptions(['english' => 'english', 'russian' => 'russian'])->required()->setSortable(false),
            AdminFormElement::file('local_url', 'Local Url')->maxSize((int)$this->size),
            AdminFormElement::text('external_url', 'External Url'),
            AdminFormElement::multiselect('tags', 'Tag')->setModelForOptions(Tag::class)->setDisplay('name')->required(),
            AdminFormElement::multiselect('categories', 'Category')->setModelForOptions(\App\Category::class)->setDisplay('name')->required(),
            AdminFormElement::select('language_id', 'Language')->setModelForOptions(\App\Language::class)->setDisplay('name')->exclude(['any' => '1'])->required(),
            AdminFormElement::text('id', 'ID')->setReadonly(1),
            AdminFormElement::text('created_at')->setLabel('Создано')->setReadonly(1),
            AdminFormElement::custom(function (Model $model) {
                if ((string)$model->local_url == 'false' || empty($model->local_url)) {
                    $local = $model->getOriginal('local_url');
                    $imageUrl = $model->getOriginal('imageUrl');

                    $this->deleteImageVideo($local, $imageUrl);
                    $model->imageUrl = '';
                }
                if ($model->local_url == $model->getOriginal('local_url')) {
                    return $model;
                }
                $local = $model->getOriginal('local_url');
                $imageUrl = $model->getOriginal('imageUrl');

                $this->deleteImageVideo($local, $imageUrl);

                if (!empty($model->local_url) && (string)$model->local_url != 'false') {

                    $model->imageUrl = $this->createImageFromVideo($model->local_url);
                }

            })

        ]);
        // todo: remove if unused
    }

    /**
     * @return FormInterface
     */
    public function onCreate()
    {
        return AdminForm::panel()->addBody([
            AdminFormElement::text('title', 'Title')->required(),
            AdminFormElement::select('language_id', 'Language')->setModelForOptions(\App\Language::class)->setDisplay('name')->exclude(['any' => '1'])->required(),
            AdminFormElement::multiselect('tags', 'Tag')->setModelForOptions(Tag::class)->setDisplay('name')->required(),
            AdminFormElement::multiselect('categories', 'Category')->setModelForOptions(\App\Category::class)->setDisplay('name')->required(),
            //AdminFormElement::text('description', 'Description')->required(),
            AdminFormElement::wysiwyg('description', 'Description')->required(),
            AdminFormElement::file('local_url', 'Local Url')->maxSize((int)$this->size),
            AdminFormElement::custom(function (Model $model) {
                if (!empty($model->local_url)) {
                    $model->imageUrl = $this->createImageFromVideo($model->local_url);
                }

            }),
            AdminFormElement::text('external_url', 'External Url')

        ]);
    }

    /**
     * @return void
     */
    public function onDelete($id)
    {
        if (\App\Training::find($id)) {
            $local = \App\Training::find($id)->local_url;
            $imageUrl = \App\Training::find($id)->imageUrl;

        }
        $this->deleteImageVideo($local, $imageUrl);
        if (!\App\Training::find($id)) {
            return false;
        }

        \App\Training::find($id)->users()->detach();
        \App\Training::find($id)->tags()->detach();
        \App\Training::find($id)->categories()->detach();
        \App\Training::find($id)->views()->detach();


    }

    /**
     * @return void
     */
    public function onRestore($id)
    {
        // todo: remove if unused
    }
}