|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 require_once 'Zend/Validate/Abstract.php'; 00026 00036 abstract class Zend_Validate_Db_Abstract extends Zend_Validate_Abstract 00037 { 00041 const ERROR_NO_RECORD_FOUND = 'noRecordFound'; 00042 const ERROR_RECORD_FOUND = 'recordFound'; 00043 00047 protected $_messageTemplates = array( 00048 self::ERROR_NO_RECORD_FOUND => 'No record matching %value% was found', 00049 self::ERROR_RECORD_FOUND => 'A record matching %value% was found', 00050 ); 00051 00055 protected $_schema = null; 00056 00060 protected $_table = ''; 00061 00065 protected $_field = ''; 00066 00070 protected $_exclude = null; 00071 00077 protected $_adapter = null; 00078 00095 public function __construct($options) 00096 { 00097 if ($options instanceof Zend_Config) { 00098 $options = $options->toArray(); 00099 } else if (func_num_args() > 1) { 00100 $options = func_get_args(); 00101 $temp['table'] = array_shift($options); 00102 $temp['field'] = array_shift($options); 00103 if (!empty($options)) { 00104 $temp['exclude'] = array_shift($options); 00105 } 00106 00107 if (!empty($options)) { 00108 $temp['adapter'] = array_shift($options); 00109 } 00110 00111 $options = $temp; 00112 } 00113 00114 if (!array_key_exists('table', $options) && !array_key_exists('schema', $options)) { 00115 require_once 'Zend/Validate/Exception.php'; 00116 throw new Zend_Validate_Exception('Table or Schema option missing!'); 00117 } 00118 00119 if (!array_key_exists('field', $options)) { 00120 require_once 'Zend/Validate/Exception.php'; 00121 throw new Zend_Validate_Exception('Field option missing!'); 00122 } 00123 00124 if (array_key_exists('adapter', $options)) { 00125 $this->setAdapter($options['adapter']); 00126 } 00127 00128 if (array_key_exists('exclude', $options)) { 00129 $this->setExclude($options['exclude']); 00130 } 00131 00132 $this->setField($options['field']); 00133 if (array_key_exists('table', $options)) { 00134 $this->setTable($options['table']); 00135 } 00136 00137 if (array_key_exists('schema', $options)) { 00138 $this->setSchema($options['schema']); 00139 } 00140 } 00141 00147 public function getAdapter() 00148 { 00149 return $this->_adapter; 00150 } 00151 00158 public function setAdapter($adapter) 00159 { 00160 if (!($adapter instanceof Zend_Db_Adapter_Abstract)) { 00161 require_once 'Zend/Validate/Exception.php'; 00162 throw new Zend_Validate_Exception('Adapter option must be a database adapter!'); 00163 } 00164 00165 $this->_adapter = $adapter; 00166 return $this; 00167 } 00168 00174 public function getExclude() 00175 { 00176 return $this->_exclude; 00177 } 00178 00185 public function setExclude($exclude) 00186 { 00187 $this->_exclude = $exclude; 00188 return $this; 00189 } 00190 00196 public function getField() 00197 { 00198 return $this->_field; 00199 } 00200 00207 public function setField($field) 00208 { 00209 $this->_field = (string) $field; 00210 return $this; 00211 } 00212 00218 public function getTable() 00219 { 00220 return $this->_table; 00221 } 00222 00229 public function setTable($table) 00230 { 00231 $this->_table = (string) $table; 00232 return $this; 00233 } 00234 00240 public function getSchema() 00241 { 00242 return $this->_schema; 00243 } 00244 00251 public function setSchema($schema) 00252 { 00253 $this->_schema = $schema; 00254 return $this; 00255 } 00256 00263 protected function _query($value) 00264 { 00268 if ($this->_adapter === null) { 00269 $this->_adapter = Zend_Db_Table_Abstract::getDefaultAdapter(); 00270 if (null === $this->_adapter) { 00271 require_once 'Zend/Validate/Exception.php'; 00272 throw new Zend_Validate_Exception('No database adapter present'); 00273 } 00274 } 00275 00279 $select = new Zend_Db_Select($this->_adapter); 00280 $select->from($this->_table, array($this->_field), $this->_schema) 00281 ->where($this->_adapter->quoteIdentifier($this->_field, true).' = ?', $value); 00282 if ($this->_exclude !== null) { 00283 if (is_array($this->_exclude)) { 00284 $select->where($this->_adapter->quoteIdentifier($this->_exclude['field'], true).' != ?', $this->_exclude['value']); 00285 } else { 00286 $select->where($this->_exclude); 00287 } 00288 } 00289 $select->limit(1); 00290 00294 $result = $this->_adapter->fetchRow($select, array(), Zend_Db::FETCH_ASSOC); 00295 00296 return $result; 00297 } 00298 }