Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Sprig::_upload_file() callback to work with the new Upload fiel…

…d class
  • Loading branch information...
commit f4ed646361ff1cbf5b07c3d6c30f51270ec89594 1 parent 3014c5e
@shadowhand shadowhand authored
Showing with 70 additions and 0 deletions.
  1. +70 −0 classes/sprig.php
View
70 classes/sprig.php
@@ -252,6 +252,11 @@ protected function __construct()
{
$field->rules['max_length'] = array($field->max_length);
}
+
+ if ($field instanceof Sprig_Field_Upload)
+ {
+ $field->callbacks[] = array($this, '_upload_file');
+ }
}
if ($field instanceof Sprig_Field_BelongsTo OR ! $field instanceof Sprig_Field_ForeignKey)
@@ -1331,6 +1336,71 @@ public function _unique_field(Validate $array, $field)
}
/**
+ * Callback for validating file uploads.
+ *
+ * @param object Validate array
+ * @param string field name
+ * @return void
+ */
+ public function _upload_file(Validate $array, $name)
+ {
+ if ($array->errors())
+ {
+ // Attempting an upload when errors exist would be useless
+ return;
+ }
+
+ $field = $this->_fields[$name];
+
+ if (is_array($array[$name]))
+ {
+ // Handle a file upload via a form
+
+ if (Upload::not_empty($array[$name]))
+ {
+ if ($field->types AND ! Upload::type($array[$name], $field->types))
+ {
+ $array->error($name, 'Upload::type', $field->types);
+ }
+ elseif ($field->size AND ! Upload::size($array[$name], $field->size))
+ {
+ $array->error($name, 'Upload::size', $field->size);
+ }
+ else
+ {
+ $filename = NULL;
+
+ if ($field->field_name)
+ {
+ $filename = $field->field_name;
+ $filename = $this->$filename.'.'.strtolower(pathinfo($array[$name]['name'], PATHINFO_EXTENSION));
+ }
+
+ if ($file = Upload::save($array[$name], $filename, $field->directory))
+ {
+ $array[$name] = $this->$name = $file;
+ }
+ else
+ {
+ $array->error($name, 'Upload::save');
+ }
+ }
+ }
+ else
+ {
+ if ( ! $field->empty)
+ {
+ // File cannot be empty
+ $array->error($name, 'Upload::not_empty');
+ }
+
+ // Clear the empty upload value
+ $array[$name] = $this->$name = $this->_original[$name];
+ }
+ }
+ }
+
+ /**
* Initialize the fields. This method will only be called once
* by Sprig::init(). All models must define this method!
*
Please sign in to comment.
Something went wrong with that request. Please try again.