Ejemplo n.º 1
0
 /**
  * Creates an object to represent an image on the filesystem
  * 
  * @throws fValidationException  When no image was specified, when the image does not exist or when the path specified is not an image
  * 
  * @param  string  $file_path    The path to the image
  * @param  boolean $skip_checks  If file checks should be skipped, which improves performance, but may cause undefined behavior - only skip these if they are duplicated elsewhere
  * @return fImage
  */
 public function __construct($file_path, $skip_checks = FALSE)
 {
     self::determineProcessor();
     parent::__construct($file_path, $skip_checks);
     if (!self::isImageCompatible($file_path)) {
         $valid_image_types = array('GIF', 'JPG', 'PNG');
         if (self::$processor == 'imagemagick') {
             $valid_image_types[] = 'TIF';
         }
         throw new fValidationException('The image specified, %1$s, is not a valid %2$s file', $file_path, fGrammar::joinArray($valid_image_types, 'or'));
     }
 }
Ejemplo n.º 2
0
 /**
  * Makes sure a record with the same primary keys is not already in the database
  *
  * @param  fSchema        $schema       The schema object for the object
  * @param  fActiveRecord  $object       The instance of the class to check
  * @param  array          &$values      An associative array of all values going into the row (needs all for multi-field unique constraint checking)
  * @param  array          &$old_values  The old values for the record
  * @return array  A single element associative array with the key being the primary keys joined by ,s and the value being the error message
  */
 private static function checkPrimaryKeys($schema, $object, &$values, &$old_values)
 {
     $class = get_class($object);
     $table = fORM::tablize($class);
     $db = fORMDatabase::retrieve($class, 'read');
     $pk_columns = $schema->getKeys($table, 'primary');
     $columns = array();
     $found_value = FALSE;
     foreach ($pk_columns as $pk_column) {
         $columns[] = fORM::getColumnName($class, $pk_column);
         if ($values[$pk_column]) {
             $found_value = TRUE;
         }
     }
     if (!$found_value) {
         return;
     }
     $different = FALSE;
     foreach ($pk_columns as $pk_column) {
         if (!fActiveRecord::hasOld($old_values, $pk_column)) {
             continue;
         }
         $old_value = fActiveRecord::retrieveOld($old_values, $pk_column);
         $value = $values[$pk_column];
         if (self::isCaseInsensitive($class, $pk_column) && self::stringlike($value) && self::stringlike($old_value)) {
             if (fUTF8::lower($value) != fUTF8::lower($old_value)) {
                 $different = TRUE;
             }
         } elseif ($old_value != $value) {
             $different = TRUE;
         }
     }
     if (!$different) {
         return;
     }
     try {
         $params = array("SELECT %r FROM %r WHERE ", $pk_columns, $table);
         $column_info = $schema->getColumnInfo($table);
         $conditions = array();
         foreach ($pk_columns as $pk_column) {
             $value = $values[$pk_column];
             // This makes sure the query performs the way an insert will
             if ($value === NULL && $column_info[$pk_column]['not_null'] && $column_info[$pk_column]['default'] !== NULL) {
                 $value = $column_info[$pk_column]['default'];
             }
             if (self::isCaseInsensitive($class, $pk_column) && self::stringlike($value)) {
                 $condition = fORMDatabase::makeCondition($schema, $table, $pk_column, '=', $value);
                 $conditions[] = str_replace('%r', 'LOWER(%r)', $condition);
                 $params[] = $pk_column;
                 $params[] = fUTF8::lower($value);
             } else {
                 $conditions[] = fORMDatabase::makeCondition($schema, $table, $pk_column, '=', $value);
                 $params[] = $pk_column;
                 $params[] = $value;
             }
         }
         $params[0] .= join(' AND ', $conditions);
         $result = call_user_func_array($db->translatedQuery, $params);
         $result->tossIfNoRows();
         return array(join(',', $pk_columns) => self::compose('Another %1$s with the same %2$s already exists', fORM::getRecordName($class), fGrammar::joinArray($columns, 'and')));
     } catch (fNoRowsException $e) {
     }
 }
Ejemplo n.º 3
0
 /**
  * Makes sure a record with the same primary keys is not already in the database
  *
  * @param  fActiveRecord  $object       The instance of the class to check
  * @param  array          &$values      An associative array of all values going into the row (needs all for multi-field unique constraint checking)
  * @param  array          &$old_values  The old values for the record
  * @return string  An error message
  */
 private static function checkPrimaryKeys($object, &$values, &$old_values)
 {
     $class = get_class($object);
     $table = fORM::tablize($class);
     $primary_keys = fORMSchema::retrieve()->getKeys($table, 'primary');
     $columns = array();
     $found_value = FALSE;
     foreach ($primary_keys as $primary_key) {
         $columns[] = fORM::getColumnName($class, $primary_key);
         if ($values[$primary_key]) {
             $found_value = TRUE;
         }
     }
     if (!$found_value) {
         return;
     }
     $different = FALSE;
     foreach ($primary_keys as $primary_key) {
         if (!fActiveRecord::hasOld($old_values, $primary_key)) {
             continue;
         }
         $old_value = fActiveRecord::retrieveOld($old_values, $primary_key);
         $value = $values[$primary_key];
         if (self::isCaseInsensitive($class, $primary_key) && self::stringlike($value) && self::stringlike($old_value)) {
             if (fUTF8::lower($value) != fUTF8::lower($old_value)) {
                 $different = TRUE;
             }
         } elseif ($old_value != $value) {
             $different = TRUE;
         }
     }
     if (!$different) {
         return;
     }
     try {
         $sql = "SELECT " . join(', ', $primary_keys) . " FROM " . $table . " WHERE ";
         $conditions = array();
         foreach ($primary_keys as $primary_key) {
             if (self::isCaseInsensitive($class, $primary_key) && self::stringlike($values[$primary_key])) {
                 $conditions[] = 'LOWER(' . $primary_key . ')' . fORMDatabase::escapeBySchema($table, $primary_key, fUTF8::lower($values[$primary_key]), '=');
             } else {
                 $conditions[] = $primary_key . fORMDatabase::escapeBySchema($table, $primary_key, $values[$primary_key], '=');
             }
         }
         $sql .= join(' AND ', $conditions);
         $result = fORMDatabase::retrieve()->translatedQuery($sql);
         $result->tossIfNoRows();
         return self::compose('Another %1$s with the same %2$s already exists', fORM::getRecordName($class), fGrammar::joinArray($columns, 'and'));
     } catch (fNoRowsException $e) {
     }
 }