<?php
/**
 *
 * User: Artur Pilch <artur.pi@modulesgarden.com>
 * Date: 2018-08-02
 * Time: 15:57
 */

namespace ModulesGarden\HostingQuotaNotifications\App\UI\Validators;

use ModulesGarden\HostingQuotaNotifications\App\Helper\Request;
use ModulesGarden\HostingQuotaNotifications\App\Models\TicketTemplates;
use ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\Validators\BaseValidator;


class TicketTemplateExist extends BaseValidator
{
    protected function validate($data, $additionalData = null)
    {
        $id = 0;
        $templateId = Request::getValue('templateId');
        if($templateId)
        {
            $id = $templateId;
        }
        $exist = TicketTemplates::where('name', '=', $data)->where('id', '!=', $id)->value('name');
        if(!$exist)
        {
            return true;
        }
        $this->addValidationError('ticketTemplateNameAlreadyExist');

        return false;
    }
}