HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Kms/ActionLog/ActionLogSection.php
<?php

namespace App\Komma\Kms\ActionLog;

//The new object oriented attributes
use App\Helpers\KommaHelpers;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\DatePicker;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\Select;
use App\Komma\Kms\Core\Attributes\Title;
use App\Komma\Kms\Core\Sections\NoLanguageTabsDirector;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\Sections\SectionServiceInterface;
use App\Komma\Kms\Core\Sections\SectionTabGroups;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Kms\Core\Sections\SectionTabsBuilder;
use App\Komma\Routes\RouteService;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Users\Kms\UserService;
use App\Komma\Users\Models\Role;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\MessageBag;

class ActionLogSection extends Section
{
    protected $title = 'Action log';

    protected $subtitle = 'Who did what';

    protected $slug = 'actionlog';

    protected $hasRoutes = false;

    public $showSave = [];

    public $showDelete = [];

    public $showCreate = [];

    /**
     * @var ActionLogService
     */
    protected $sectionService;

    /** @var UserService */
    private $userService;

    /**
     * PageSection constructor.
     * @param ActionLogService $actionLogService
     * @param RouteService $routeService
     * @param SectionServiceInterface $siteService
     */
//    function __construct(Kms $kms, PageRepository $repository)
    public function __construct(ActionLogService $actionLogService, RouteService $routeService, SiteServiceInterface $siteService)
    {
        $this->title = ucfirst(__('kms/actionlog.section.title'));
        $this->subtitle = __('kms/actionlog.section.subtitle');
        $this->submitButtonLabel = __('kms/actionlog.retrieve');

        $this->userService = \App::make(UserService::class);

        $sectionTabDirector = new NoLanguageTabsDirector(new SectionTabsBuilder());
        parent::__construct($actionLogService, $routeService, $siteService, $sectionTabDirector);
    }

    /**
     * Generates the attributes for this section. They all must extend the App\Komma\Kms\Core\Attributes\Attribute class
     * This is the place where you need to setup your sections appearance. Just make sure you build an array of attributes
     * and put each attribute in a AbstractSectionTabItem with a SectionTabGroups constant to link them to a tab.
     *
     * @see PageRepository::saveModel()
     * @return Collection A collection of SectionTabItems
     */
    protected function generateAttributes(): Collection
    {
//        \Log::info("PageSection::Generating attributes");

        //*****************************************************************************************\\
        //*** Define attribute validation sets                                                  ***\\
        //*****************************************************************************************\\

        //*****************************************************************************************\\
        //*** Generate the attributes                                                           ***\\
        //*****************************************************************************************\\
        $attributes = [];

        //Build the general attributes and put them in the attributes array
        $attributes[] = (new Title(__('kms/actionlog.section.title')))
                        ->setMinimumUserRole(Role::Admin);

        $attributes[] = (new DatePicker(__('kms/actionlog.from')))
                        ->setMinimumUserRole(Role::Admin)
                        ->mapValueFrom(Attribute::ValueFromItself, 'from');

        $attributes[] = (new DatePicker(__('kms/actionlog.trough')))
                        ->setMinimumUserRole(Role::Admin)
                        ->mapValueFrom(Attribute::ValueFromItself, 'trough');

        $userSelectOptionModels = $this->userService->getOptionsForSelect();
        $userSelectOptionModels->prepend(
            (new SelectOption())
                ->setContent(__('kms/actionlog.all_users'))
                ->setHtmlContent(__('kms/actionlog.all_users'))
                ->setValue(0)
        );

        $attributes[] = (new Select())
            ->setItems($userSelectOptionModels->toArray())
            ->setLabelText(__('kms/actionlog.user_filter'))
            ->setMinimumUserRole(Role::Admin)
            ->mapValueFrom(Attribute::ValueFromItself, 'user_filter');

        //****************************************************************************************************************************************\\
        //*** Put the all attributes in a SectionTabItem so we can track for which tab they are. And then put SectionTabItems in a Collection  ***\\
        //****************************************************************************************************************************************\\
        $tabItems = new Collection();
        foreach ($attributes as $attribute) {
            $tabItems->push(new SectionTabItem($attribute, SectionTabGroups::General));
        }

        return $tabItems;
    }

    /**
     * Returns a rendered view
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function render()
    {
        return parent::render();
    }

    /**
     * Gets the action logs between two dates
     * Optionally filtered for a specific user.
     */
    public function getActionLogs()
    {
        $fromDate = null;
        $troughDate = null;
        $userFilter = 0;

        collect($this->sectionTabItems)->each(function (SectionTabItem $sectionTabItem) use (&$fromDate, &$troughDate, &$userFilter) {
            $attribute = $sectionTabItem->getAttribute();
            $value = $attribute->getValue();

            switch ($attribute->getsValueFrom()) {
                case Attribute::ValueFromItself:
                    switch ($attribute->getsValueFromReference()) {
                        case 'from':
                            $fromDate = $value;
                            break;
                        case 'trough':
                            $troughDate = $value;
                            break;
                        case 'user_filter':
                            $userFilter = (int) $value;
                    }
                    break;
            }
        });

        return $this->sectionService->getLogs($fromDate, $troughDate, $userFilter)->get();
    }

    /**
     * Makes the view and returns it
     *
     * @return View
     */
    protected function makeView(): View
    {
        $successes = (\Session::has('successes')) ? \Session::get('successes') : new MessageBag();

        return \View::make('kms/section.actionlog', [
            'kms' => $this->siteService,
            'section' => $this,
            'retrieveRoute' => [
                'route' => route('actionlog.retrieve'),
                'method' => 'POST',
            ],
            'successes' => $successes,
            'maxUploadSize' => KommaHelpers::fileUploadMaxSize(),
        ]);
    }
}