File: D:/HostingSpaces/SBogers10/csb.komma.pro/app/Vat/VatSection.php
<?php
namespace App\Vat;
use Komma\KMS\Core\Attributes\MultiSelect;
use Komma\KMS\Core\Attributes\OnOff;
use Komma\KMS\Core\Attributes\TextArea;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Seperator;
use Komma\KMS\Core\Attributes\TextField;
use Komma\KMS\Core\Attributes\Title;
use Komma\KMS\Core\Sections\Section;
use Komma\KMS\Core\Sections\Tabs\Collections\NoLanguageTabs;
use Komma\KMS\Core\ValidationSet;
use App\Vat\Models\Rate;
use Komma\KMS\Sites\SiteService;
use Komma\KMS\Sites\SiteServiceInterface;
use Komma\KMS\Users\Models\KmsUser;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
final class VatSection extends Section
{
protected $slug = "vatrates";
/**
* @var SiteServiceInterface
*/
private $siteService;
/**
* UserSection constructor.
*/
function __construct()
{
$tabs = new NoLanguageTabs();
parent::__construct($tabs, $this->slug);
$this->siteService = app(SiteServiceInterface::class);
}
/**
* Generates the attributes for this section. They all must extend the App\Kms\Core\Attributes\Attribute class
*
* @param Model $currentModel
* @return Collection A collection of SectionTabItems
*/
protected function generateAttributes(Model $currentModel = null): Collection
{
/** @var KmsUser $user */
$user = Auth::user();
//*****************************************************************************************\\
//*** Define attribute validation sets (Laravel validation rules and message sets) ***\\
//*****************************************************************************************\\
$nameValidationSet = (new ValidationSet())
// ->setRules('required|unique:users,username')
->setRules([
'required',
Rule::unique('vatrates', 'name')->ignore($currentModel ? $currentModel->id : null)
])
->setMessages([
'required' => __('validation.required'),
'unique' => __('validation.unique'),
]);
$percentageValidationSet = (new ValidationSet())
->setRules([
'required',
'integer',
])
->setMessages([
'required' => __('validation.required'),
'integer' => __('shop/vatrates.validation.percentage')
]);
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
//Build the general attributes and put them in the attributes array
$attributes[] = (new Title(__('kms/global.information')));
$attributes[] = (new TextField(__('shop/vatrates.name')))
->setReadOnly(false)
->setPlaceholderText(__('shop/vatrates.name_placeholder'))
->mapValueFrom(Attribute::ValueFromModel, 'name')
->setValidationSet($nameValidationSet);
$attributes[] = (new TextField(__('shop/vatrates.percentage')))
->setReadOnly(false)
->setPlaceholderText(__('shop/vatrates.percentage_placeholder'))
->mapValueFrom(Attribute::ValueFromModel, 'percentage')
->setValidationSet($percentageValidationSet);
$attributes[] = (new TextArea())
->setLabelText(__('shop/vatrates.description'))
->setPlaceholderText(__('shop/vatrates.description_placeholder'))
->mapValueFrom(Attribute::ValueFromModel, 'description');
if($user->can('markRateAsDefault', Rate::class)) {
$attributes[] = (new OnOff())
->setLabelText(__('shop/vatrates.default'))
->setExplanation(__('shop/vatrates.default_description'))
->mapValueFrom(Attribute::ValueFromModel, 'default');
}
if($user->can('modifyRateSites', Rate::class)) {
//Site selection
$attributes[] = (new Seperator());
$siteOptionModels = $this->siteService->getOptionsForSelect();
$attributes[] = (new MultiSelect())
->setItems($siteOptionModels->toArray())
->setLabelText(__('kms/sites.type'))
->mapValueFrom(Attribute::ValueFromItself, 'site_id');
}
//Return all attributes as a collection
return collect(array_merge($attributes));
}
}