Skip to content

Commit

Permalink
closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
josegonzalez committed Dec 29, 2009
1 parent 0b62a52 commit 37c5ba6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
5 changes: 4 additions & 1 deletion README.markdown
Expand Up @@ -12,7 +12,7 @@ You can also use it in No-DB mode, which means that no data for the upload is st

* If you require thumbnails for image generation, download the latest copy of phpThumb and extract it into your vendors directory. Should end up like: /vendors/phpThumb/{files}. (http://phpthumb.sourceforge.net)

# Usage
## Usage
In a model that needs uploading, replace the class declaration with something similar to the following:

<?php
Expand Down Expand Up @@ -53,3 +53,6 @@ You'll want to include any other fields in your Model as well :)
Make sure your directory (app/webroot/uploads/image/ in this case, image changes to the name of your model) is at least CHMOD 775, also check your php.ini MAX_FILE_SIZE is enough to support the filesizes you are uploading (default in the behavior is 2MB, can be configured)

The behavior code will save the uploaded file's name in the 'filename' field in database, it will not overwrite existing files, instead it will create a new filename based on the original plus a counter. For thumbnails, when the file is uploaded, it will automatically create 3 thumbnail sizes (this can be configured) and prepend the name to the thumbfiles as directories(i.e. app/webroot/uploads/image/image_001.jpg will produced app/webroot/uploads/thumb/small/image/image_001.jpg, app/webroot/uploads/thumb/medium/image/image_001.jpg, app/webroot/uploads/thumb/large/image/image_001.jpg)

### Deleting an uploaded file while keeping the record
Flag the file for deletion by setting `data[Model][filename][remove]` to something non-empty, e.g. `TRUE`. The uploaded file including possible thumbnails will then be deleted together with adherent database fields upon save. Note that the record will be preserved, only the file meta-data columns will be reset.
59 changes: 35 additions & 24 deletions models/behaviors/meio_upload.php
Expand Up @@ -32,11 +32,8 @@ class MeioUploadBehavior extends ModelBehavior {
'allowedExt' => array('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.ico'),
'default' => false, // Not sure what this does
'zoomCrop' => false, // Whether to use ZoomCrop or not with PHPThumb
'thumbnails' => true,
'thumbsizes' => array(
'small' => array('width' => 100, 'height' => 100, 'thumbnailQuality' => 75),
'medium' => array('width' => 220, 'height' => 220, 'thumbnailQuality' => 75),
'large' => array('width' => 800, 'height' => 600, 'thumbnailQuality' => 75)
// Place any custom thumbsize in model config instead,
),
'thumbnailQuality' => 75, // Global Thumbnail Quality
'maxDimension' => null, // Can be null, h, or w
Expand Down Expand Up @@ -236,6 +233,9 @@ function setup(&$model, $settings = array()) {
$options['dir'] = rtrim($this->_replaceTokens($model, $options['dir'], $field, $tokens), DS);
$options['uploadName'] = rtrim($this->_replaceTokens($model, $options['uploadName'], $field, $tokens), DS);

// Create the folders for the uploads
$this->_createFolders($options['dir'], array_keys($options['thumbsizes']));

// Replace tokens in the fields names
if ($options['useTable']) {
foreach ($options['fields'] as $fieldToken => $fieldName) {
Expand Down Expand Up @@ -299,7 +299,7 @@ function beforeSave(&$model) {
function afterSave(&$model) {
foreach ($this->__filesToRemove as $file) {
if ($file['name']) {
$this->_deleteFiles($file['name'], $file['dir']);
$this->_deleteFiles($model, $file['field'], $file['name'], $file['dir']);
}
}
// Reset the filesToRemove array
Expand Down Expand Up @@ -348,7 +348,7 @@ function beforeDelete(&$model) {
foreach ($this->__fields[$model->alias] as $field => $options) {
$file = $model->data[$model->alias][$field];
if ($file && $file != $options['default']) {
$this->_deleteFiles($file, $options['dir']);
$this->_deleteFiles($model, $field, $file, $options['dir']);
}
}
}
Expand Down Expand Up @@ -634,6 +634,15 @@ function _uploadFile(&$model, $data = null) {
$data =& $model->data;
}
foreach ($this->__fields[$model->alias] as $fieldName => $options) {
// Take care of removal flagged field
// However, this seems to be kind of code duplicating, see line ~711
if (!empty($data[$model->alias][$fieldName]['remove'])) {
$this->_markForDeletion($model->alias, $model->primaryKey, $fieldName, $data, $options['default']);
$data = $this->_unsetDataFields($model->alias, $fieldName, $data, $options);
$result = array('return' => true, 'data' => $data);
continue;
}
// If no file was selected we do not need to proceed
if (empty($data[$model->alias][$fieldName]['name'])) {
unset($data[$model->alias][$fieldName]);
$result = array('return' => true, 'data' => $data);
Expand Down Expand Up @@ -663,7 +672,7 @@ function _uploadFile(&$model, $data = null) {
}

// If the file is an image, try to make the thumbnails
if ($options['thumbnails'] && count($options['allowedExt']) > 0 && in_array($data[$model->alias][$fieldName]['type'], $this->_imageTypes)) {
if ((count($options['thumbsizes']) > 0) && count($options['allowedExt']) > 0 && in_array($data[$model->alias][$fieldName]['type'], $this->_imageTypes)) {
$this->_createThumbnails($model, $data, $fieldName, $saveAs, $ext, $options);
}

Expand Down Expand Up @@ -718,7 +727,7 @@ function _uploadFile(&$model, $data = null) {
}

// If the file is an image, try to make the thumbnails
if ($options['thumbnails'] && count($options['allowedExt']) > 0 && in_array($data[$model->alias][$fieldName]['type'], $this->_imageTypes)) {
if ((count($options['thumbsizes']) > 0) && count($options['allowedExt']) > 0 && in_array($data[$model->alias][$fieldName]['type'], $this->_imageTypes)) {
$this->_createThumbnails($model, $data, $fieldName, $saveAs, $ext, $options);
}

Expand Down Expand Up @@ -960,12 +969,9 @@ function _splitFilenameAndExt($filename) {
**/
function _getThumbnailName($saveAs, $dir, $key, $fieldToSaveAs, $sub = null) {
if ($key == 'normal') {
// Create the directory if it doesn't exist
$this->_createThumbnailFolders($saveAs, $key);
return $saveAs;
// Otherwise, set the thumb filename to thumb.$key.$filename.$ext
}
$this->_createThumbnailFolders($dir, $key);
// Otherwise, set the thumb filename to thumb.$key.$filename.$ext
$result = $dir . DS . 'thumb' . DS . $key . DS . $fieldToSaveAs;
if (isset($sub)) {
return $result . '.' . $sub;
Expand Down Expand Up @@ -1027,6 +1033,7 @@ function _setupValidation(&$model, $fieldName, $options) {
$model->validate[$fieldName] = $this->_arrayMerge($options['validations'], $model->validate[$fieldName]);
}


/**
* Creates thumbnail folders if they do not already exist
*
Expand All @@ -1035,16 +1042,20 @@ function _setupValidation(&$model, $fieldName, $options) {
* @return void
* @author Jose Diaz-Gonzalez
**/
function _createThumbnailFolders($dir, $key) {
function _createFolders($dir, $thumbsizes) {
$dir (substr($dir, 0, 1) != "/") ? WWW_ROOT . $dir : $dir;
$folder = new Folder();
if (!$folder->cd(WWW_ROOT . $dir)) {
$folder->mkdir(WWW_ROOT . $dir);

if (!$folder->cd($dir)) {
$folder->mkdir($dir);
}
if (!$folder->cd(WWW_ROOT . $dir. DS . 'thumb')) {
$folder->mkdir(WWW_ROOT . $dir . DS . 'thumb');
if (!$folder->cd($dir. DS . 'thumb')) {
$folder->mkdir($dir . DS . 'thumb');
}
if (!$folder->cd(WWW_ROOT . $dir . DS .'thumb' . DS . $key)) {
$folder->mkdir(WWW_ROOT . $dir . DS . 'thumb' . DS . $key);
foreach ($thumbsizes as $thumbsize) {
if ($thumbsize != 'normal' && !$folder->cd($dir . DS .'thumb' . DS . $thumbsize)) {
$folder->mkdir($dir . DS . 'thumb' . DS . $thumbsize);
}
}
}

Expand Down Expand Up @@ -1079,6 +1090,7 @@ function _setFileToRemove(&$model, $fieldName) {
$filename = $model->field($fieldName);
if (!empty($filename) && $filename != $this->__fields[$model->alias][$fieldName]['default']) {
$this->__filesToRemove[] = array(
'field' => $fieldName,
'dir' => $this->__fields[$model->alias][$fieldName]['dir'],
'name' => $filename
);
Expand Down Expand Up @@ -1119,15 +1131,14 @@ function _markForDeletion($modelName, $modelPrimaryKey, $fieldName, $data, $defa
* @return boolean
* @author Vinicius Mendes
*/
function _deleteFiles($filename, $dir) {
function _deleteFiles(&$model, $field, $filename, $dir) {
$saveAs = $dir . DS . $filename;
if (is_file($saveAs) && !unlink($saveAs)) {
return false;
}
$folder = &new Folder($dir);
$files = $folder->find('thumb\.[a-zA-Z0-9]+\.' . $filename);
foreach ($files as $f) {
unlink($dir . DS . $f);
foreach ($this->__fields[$model->alias][$field]['thumbsizes'] as $size => &$config) {
$file = &$new File($dir . DS . $size . DS . $fileName);
$file->delete();
}
return true;
}
Expand Down

0 comments on commit 37c5ba6

Please sign in to comment.