<?php

namespace ModulesGarden\HostingQuotaNotifications\App\UI\Validators;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\Validators\BaseValidator;
use \ModulesGarden\HostingQuotaNotifications\App\Models\Groups;
use \ModulesGarden\HostingQuotaNotifications\App\Helper;

/**
 * GroupNameExist form data validator
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class GroupNameExist extends BaseValidator
{
    protected function validate($data, $additionalData = null)
    {
        $result = Groups::where('name', '=', $data)->value('name');
        $groupid = Helper\Request::getValue('groupid');
        if($groupid)
        {
            $result = Groups::where('name', '=', $data)->where('id', '!=', $groupid)->value('name');
        }
        if(!$result)
        {
            return true;
        }
        $this->addValidationError('nameAlreadyExist');
        
        return false;
    }
}
