<?php
/*
 *
 * @package		ARI Framework
 * @author		ARI Soft
 * @copyright	Copyright (c) 2011 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 */

(defined('_JEXEC') && defined('ARI_FRAMEWORK_LOADED')) or die;

class SettingsnameValidator extends AriValidator
{
	function validate($value, $params)
	{
		$id = AriUtils::getParam($params, 'Id');
		$group = AriUtils::getParam($params, '_GroupName');
		if (empty($group))
			return false;

		$model = AriModel::getInstance('Providersettings', 'AriSmartContentModel');
		$isValid = $model->isUniqueName($value, $id, $group);

		return $isValid;
	}

	function registerScript($prefix, $validationGroups = array())
	{
		$this->registerAssets();

		$ctrlId = $prefix . $this->_fieldToValidate;
		$config = array(
			'prefix' => $prefix,
			'validationGroups' => $validationGroups,
			'errorMessage' => $this->getErrorMessage()
		);
		$jsConfig = json_encode($config);

		$doc = JFactory::getDocument();
		$doc->addScriptDeclaration(
			'YAHOO.ARISoft.validators.validatorManager.addValidator(' .
			'	new YAHOO.ARISoft.SmartContent.validators.isSettingsNameUnique("' . $ctrlId . '",' . $jsConfig . '))');
		
	}
	
	function registerAssets()
	{
		$doc = JFactory::getDocument();
		
		$uri = JURI::root(true) . '/administrator/components/com_arismartcontent/validators/assets/';
		$doc->addScript($uri . 'settingsname.js');
	}
}