Last active
June 2, 2016 18:56
-
-
Save dfelton/32e618f5513f23222e568bc5f0634e16 to your computer and use it in GitHub Desktop.
Reusable file upload logic for Magento Models. Good for use in observer classes when image fields have been added to flat table entities such as CMS Page objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Example Usage for cms/page objects. | |
// cms_page_prepare_save event observer logic: | |
// | |
// $model = $observer->getEvent()->getPage(); | |
// $this->processFile($model, 'image_1', 'cms/page', array('jpg', 'jpeg', 'png', 'bmp', 'gif')); | |
/** | |
* Handles processing the upload logic for model files. | |
* | |
* @param Mage_Core_Model_Abstract $model | |
* @param string $field | |
* @param string $dir | |
* @param array $ext | |
*/ | |
protected function processFile($model, $field, $dir, $ext = array()) | |
{ | |
$filename = $_FILES[$field]['name']; | |
$fileSet = false; | |
switch ($_FILES[$field]['error']) { | |
case UPLOAD_ERR_OK: | |
if (false == ($fileSet = $this->saveFile($model, $field, $dir, $ext)) ) { | |
$message = $helper->__('Error uploading "%s"', $filename); | |
Mage::getSingleton('adminhtml/session')->addNotice($message); | |
} | |
break; | |
case UPLOAD_ERR_CANT_WRITE: | |
case UPLOAD_ERR_EXTENSION: | |
case UPLOAD_ERR_NO_TMP_DIR: | |
$message = $helper->__('"%s" not uploaded due to server misconfiguration. Please contact a developer.', $filename); | |
Mage::getSingleton('adminhtml/session')->addNotice($message); | |
break; | |
case UPLOAD_ERR_PARTIAL: | |
$message = $helper->__('"%s" only partially uploaded, please try again', $filename); | |
Mage::getSingleton('adminhtml/session')->addNotice($message); | |
break; | |
case UPLOAD_ERR_FORM_SIZE: | |
case UPLOAD_ERR_INI_SIZE: | |
// TODO: Some additional information may be nice here on what the maximum allowed size actually is. | |
$message = $helper->__('File upload too large: %s', $filename); | |
Mage::getSingleton('adminhtml/session')->addNotice($message); | |
break; | |
case UPLOAD_ERR_NO_FILE: | |
default: | |
break; | |
} | |
if (!$fileSet) { | |
$value = $this->getDeleteFlag($field) | |
? null | |
: $model->getOrigData($field); // necessary since POST data sends array | |
$model->setData($field, $value); | |
} | |
} | |
/** | |
* Handles uploading a file to the server and storing the path to it in | |
* the model class. | |
* | |
* @param Mage_Core_Model_Abstract $model | |
* @param string $field | |
* @param string $dir | |
* @param null|array $ext | |
* @return boolean | |
*/ | |
protected function saveFile($model, $field, $dir, $ext = null) | |
{ | |
$uploader = new Varien_File_Uploader($field); | |
$uploader->setAllowRenameFiles(true); | |
$uploader->setFilesDispersion(true); | |
$uploader->setAllowCreateFolders(true); | |
if ($ext) { | |
$uploader->setAllowedExtensions($ext); | |
} | |
if (false == ($result = $uploader->save($dir))) { | |
return false; | |
} | |
$model->setData($field, $result['file']); | |
return true; | |
} | |
/** | |
* Returns flag indicating if a file for a model should be unset. | |
* | |
* @param Mage_Core_Model_Abstract $model | |
* @param string $key | |
* @return boolean | |
*/ | |
protected function getDeleteFlag($key) | |
{ | |
$param = Mage::app()->getRequest()->getParam($key, false); | |
return $param !== false && isset($param['delete']) && $param['delete'] == '1'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment