* @license LGPL, see license.txt for details * @link http://www.php-tools.net */ class patForms_Rule_CheckFile extends patForms_Rule { /** * name of the rule * * @abstract * @access private */ var $ruleName = 'CheckFile'; /** * define error codes and messages for the rule * * @access private * @var array $validatorErrorCodes * @todo translate error messages */ var $validatorErrorCodes = array( 'C' => array( 1 => 'File or directory already exist', 2 => 'File or directory does not exit, yet.', 3 => 'Empty file name given' ), 'de' => array( 1 => 'Datei oder Verzeichnis existiert bereits.', 2 => 'Datei oder Verzeichnis gibt es nicht.', 3 => 'Dateiname ist leer' ) ); /** * working directory * * @var string * @access private */ var $_wd = ''; /** * format * * @var string * @access private */ var $_format = '%s'; /** * whether an empty string is alllowed * * @var boolean * @access private */ var $_allowEmpty = false; /** * whether the file must exist or must not exist * * @var boolean * @access private */ var $_mustExist = true; /** * set regular expression * * @access public * @param boolean $exist */ function setParam( $exist = true, $empty = false ) { $this->_allowEmpty = $empty; $this->_mustExist = $exist; } /** * set working directory * * @access public * @param string $dir */ function setDirectory( $dir ) { $this->_wd = $dir; } /** * set format string * * use the format string in case the value needs additional sufix etc * * @access public * @param string $format * @see sprintf() */ function setFormat( $format ) { $this->_format = $format; } /** * method called by patForms or any patForms_Element to validate the * element or the form. * * @access public * @param object patForms form object */ function applyRule( &$element, $type = PATFORMS_RULE_BEFORE_VALIDATION ) { $value = sprintf( $this->_format, $element->getValue() ); if( !$this->_allowEmpty && empty( $value ) ) { $this->addValidationError( 3 ); return false; } $exist = file_exists( $this->_wd . DIRECTORY_SEPARATOR . $value ); if( $exist && !$this->_mustExist ) { $this->addValidationError( 1 ); return false; } if( !$exist && $this->_mustExist ) { $this->addValidationError( 2 ); return false; } return true; } } ?>