summaryrefslogblamecommitdiffstats
path: root/library/Poolctrl/Validator/TitleUnique.php
blob: f051438fb5237844c08232f80302c78e2a2a07b2 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11










                                                                  














                                                                   


                                         


                                                                                    
 





                                                                
                        













                                                                                            


                 
<?php
require_once 'Zend/Validate/Abstract.php';

class Poolctrl_Validate_TitleUnique extends Zend_Validate_Abstract
{
	const NOT_UNIQUE = 'notUnique';

	protected $_messageTemplates = array(
	self::NOT_UNIQUE => "Title is already assigned",
	);

	public function __construct($option)
	{
		if ($option instanceof Zend_Config) {
			$option = $option->toArray();
		}

		if (is_array($option)) {
			if (array_key_exists('title', $option)) {
				$this->oldTitle = $option['title'];
			}
		}
	}

	protected $oldTitle;

	public function isValid($value)
	{
		$this->_setValue($value);
		if(empty($this->oldTitle)) {
			$eventMapper = new Application_Model_EventMapper();
			$sameEvent = $eventMapper->findBy(array("title" => $value));

			if (count($sameEvent) > 0) {
				$this->_error(self::NOT_UNIQUE);
				return false;
			} else {
				return true;
			}
		} else {
			if($this->oldTitle == $value) {
				return true;
			} else {
				$eventMapper = new Application_Model_EventMapper();
				$sameEvent = $eventMapper->findBy(array("title" => $value));

				if (count($sameEvent) > 0) {
					$this->_error(self::NOT_UNIQUE);
					return false;
				} else {
					return true;
				}

			}
		}
	}
}