06.27 php saved
jmikola
Tags add more
 
Note
jmikola did not leave a note
  1. <?php
  2.  
  3. /**
  4. * Common validation error messages.
  5. */
  6. define('MSG_INVALID_EMAIL', 'Email address is invalid');
  7. define('MSG_INVALID_PHONE', 'Phone number is invalid');
  8. define('MSG_INVALID_FAX',   'Fax number is invalid');
  9.  
  10. /**
  11. * Application model for Cake.
  12. *
  13. * This is a placeholder class.
  14. * Create the same file in app/app_model.php
  15. * Add your application-wide methods to the class, your models will inherit them.
  16. *
  17. * @package        cake
  18. * @subpackage    cake.cake
  19. */
  20. class AppModel extends Model {
  21.        
  22.     /**
  23.      * Validation replacement for Model::isUnique.  Checks uniqueness of one or
  24.      * multiple fields at once.  If the $ignore_on parameter specifies a field
  25.      * name, a potential conflict will be ignored if this model shares a value
  26.      * with that field.  This is most useful during an update, where we only
  27.      * need to validate against other records with different id's.
  28.      *
  29.      * @param mixed $data Field value (ignored)
  30.      * @param mixed $fieldNames Field name if a string, or an array of names (must not include Model name in dotted notation)
  31.      * @param boolean $or If false, all fields must match for isUnique to return false
  32.      * @param string $ignore_on_same Field name to ignore conflicts with
  33.      * @return boolean True if field value is unique; otherwise, false
  34.      */
  35.     function checkUnique($data, $fieldNames, $or = true, $ignore_on_same = 'id') {
  36.         // No need to perform any joins
  37.         $this->recursive = -1;
  38.                
  39.         // Pack fieldNames into an array, if a single field was specified
  40.         if (is_string($fieldNames)) {
  41.             $fieldNames = array($fieldNames);
  42.         }
  43.         // If fieldNames isn't an array by this point, the parameter was invalid
  44.         else if (!is_array($fieldNames)) {
  45.             return false;
  46.         }
  47.        
  48.         $fields = array();
  49.        
  50.         foreach ($fieldNames as $fieldName) {
  51.             // Ensure all fields to be checked exist in the Model and $data array
  52.             if (!is_string($fieldName) || !$this->hasField($fieldName) ||
  53.                 !isset($this->data[$this->alias][$fieldName])) {
  54.                 return false;
  55.             }
  56.            
  57.             $fieldValue = $this->data[$this->alias][$fieldName];
  58.            
  59.             // Prefex $fieldName with model name for the full field name
  60.             $fieldName = $this->alias . '.' . $fieldName;
  61.            
  62.             // Construct $fields parameter for Model::isUnique
  63.             $fields[$fieldName] = $fieldValue;
  64.         }
  65.        
  66.         // Groups field conditions in an OR clause if necessary
  67.         // Note: AND parent is necessary since cake does not properly surround
  68.         // the OR constraints with parentheses
  69.         if ($or) {
  70.             $fields = array('and' => array('or' => $fields));
  71.         }
  72.        
  73.         // Append $ignore_on_same as an AND comparison if possible
  74.         if ($ignore_on_same && !empty($this->data[$this->alias][$ignore_on_same])) {
  75.             // Construct full field name and append "!=" SQL operator
  76.             $key = $this->alias . '.' . $ignore_on_same . ' <>';
  77.             $val = $this->data[$this->alias][$ignore_on_same];
  78.            
  79.             // Use array_merge to prepend this clause, which is necessary in
  80.             // case all previous fields were grouped under an OR comparison
  81.             $fields = array_merge(array($key => $val), $fields);
  82.         }
  83.        
  84.         return ($this->find('count', array('conditions' => $fields)) == 0);
  85.     }
  86.    
  87. }
  88. ?>
Parsed in 0.118 seconds, using GeSHi 1.0.7.14

Modify this Paste