00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Adminhtml_Model_System_Config_Backend_Image extends Mage_Core_Model_Config_Data
00036 {
00037
00038
00039
00040
00041
00042
00043 protected function _beforeSave()
00044 {
00045 $value = $this->getValue();
00046 if (is_array($value) && !empty($value['delete'])) {
00047 $this->setValue('');
00048 }
00049
00050 if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value']){
00051
00052 $fieldConfig = $this->getFieldConfig();
00053
00054
00055 if (empty($fieldConfig->upload_dir)) {
00056 Mage::throwException(Mage::helper('catalog')->__('Base directory to upload image file is not specified'));
00057 }
00058
00059 $uploadDir = (string)$fieldConfig->upload_dir;
00060
00061 $el = $fieldConfig->descend('upload_dir');
00062
00063
00064
00065
00066 if (!empty($el['scope_info'])) {
00067 $uploadDir = $this->_appendScopeInfo($uploadDir);
00068 }
00069
00070
00071
00072
00073 if (!empty($el['config'])) {
00074 $uploadRoot = (string)Mage::getConfig()->getNode((string)$el['config'], $this->getScope(), $this->getScopeId());
00075 $uploadRoot = Mage::getConfig()->substDistroServerVars($uploadRoot);
00076 $uploadDir = $uploadRoot . '/' . $uploadDir;
00077 }
00078
00079 try {
00080 $file = array();
00081 $file['tmp_name'] = $_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value'];
00082 $file['name'] = $_FILES['groups']['name'][$this->getGroupId()]['fields'][$this->getField()]['value'];
00083 $uploader = new Varien_File_Uploader($file);
00084 $uploader->setAllowedExtensions($this->_getAllowedExtensions());
00085 $uploader->setAllowRenameFiles(true);
00086 $uploader->save($uploadDir);
00087 } catch (Exception $e) {
00088 Mage::throwException($e->getMessage());
00089 return $this;
00090 }
00091
00092 if ($filename = $uploader->getUploadedFileName()) {
00093
00094
00095
00096
00097 if (!empty($el['scope_info'])) {
00098 $filename = $this->_prependScopeInfo($filename);
00099 }
00100
00101 $this->setValue($filename);
00102 }
00103 }
00104
00105 return $this;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 protected function _prependScopeInfo($path)
00117 {
00118 $scopeInfo = $this->getScope();
00119 if ('default' != $this->getScope()) {
00120 $scopeInfo .= '/' . $this->getScopeId();
00121 }
00122 return $scopeInfo . '/' . $path;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 protected function _appendScopeInfo($path)
00134 {
00135 $path .= '/' . $this->getScope();
00136 if ('default' != $this->getScope()) {
00137 $path .= '/' . $this->getScopeId();
00138 }
00139 return $path;
00140 }
00141
00142 protected function _getAllowedExtensions()
00143 {
00144 return array('jpg', 'jpeg', 'gif', 'png');
00145 }
00146 }