/app/code/core/Mage/Adminhtml/Model/System/Config/Backend/File.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror · PHP · 186 lines · 85 code · 20 blank · 81 comment · 12 complexity · a177b362499a61b4d9f25814ac513b03 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * System config file field backend model
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Model_System_Config_Backend_File extends Mage_Core_Model_Config_Data
  34. {
  35. /**
  36. * Save uploaded file before saving config value
  37. *
  38. * @return Mage_Adminhtml_Model_System_Config_Backend_File
  39. */
  40. protected function _beforeSave()
  41. {
  42. $value = $this->getValue();
  43. if (is_array($value) && !empty($value['delete'])) {
  44. $this->setValue('');
  45. }
  46. if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value']){
  47. $uploadDir = $this->_getUploadDir();
  48. try {
  49. $file = array();
  50. $tmpName = $_FILES['groups']['tmp_name'];
  51. $file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value'];
  52. $name = $_FILES['groups']['name'];
  53. $file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value'];
  54. $uploader = new Mage_Core_Model_File_Uploader($file);
  55. $uploader->setAllowedExtensions($this->_getAllowedExtensions());
  56. $uploader->setAllowRenameFiles(true);
  57. $result = $uploader->save($uploadDir);
  58. } catch (Exception $e) {
  59. Mage::throwException($e->getMessage());
  60. return $this;
  61. }
  62. $filename = $result['file'];
  63. if ($filename) {
  64. if ($this->_addWhetherScopeInfo()) {
  65. $filename = $this->_prependScopeInfo($filename);
  66. }
  67. $this->setValue($filename);
  68. }
  69. }
  70. return $this;
  71. }
  72. /**
  73. * Makes a decision about whether to add info about the scope.
  74. *
  75. * @return boolean
  76. */
  77. protected function _addWhetherScopeInfo()
  78. {
  79. $fieldConfig = $this->getFieldConfig();
  80. $el = $fieldConfig->descend('upload_dir');
  81. return (!empty($el['scope_info']));
  82. }
  83. /**
  84. * Return path to directory for upload file
  85. *
  86. * @return string
  87. * @throw Mage_Core_Exception
  88. */
  89. protected function _getUploadDir()
  90. {
  91. $fieldConfig = $this->getFieldConfig();
  92. /* @var $fieldConfig Varien_Simplexml_Element */
  93. if (empty($fieldConfig->upload_dir)) {
  94. Mage::throwException(Mage::helper('catalog')->__('The base directory to upload file is not specified.'));
  95. }
  96. $uploadDir = (string)$fieldConfig->upload_dir;
  97. $el = $fieldConfig->descend('upload_dir');
  98. /**
  99. * Add scope info
  100. */
  101. if (!empty($el['scope_info'])) {
  102. $uploadDir = $this->_appendScopeInfo($uploadDir);
  103. }
  104. /**
  105. * Take root from config
  106. */
  107. if (!empty($el['config'])) {
  108. $uploadRoot = $this->_getUploadRoot((string)$el['config']);
  109. $uploadDir = $uploadRoot . '/' . $uploadDir;
  110. }
  111. return $uploadDir;
  112. }
  113. /**
  114. * Return the root part of directory path for uploading
  115. *
  116. * @var string
  117. * @return string
  118. */
  119. protected function _getUploadRoot($token)
  120. {
  121. $uploadRoot = (string)Mage::getConfig()->getNode($token, $this->getScope(), $this->getScopeId());
  122. $uploadRoot = Mage::getConfig()->substDistroServerVars($uploadRoot);
  123. return $uploadRoot;
  124. }
  125. /**
  126. * Prepend path with scope info
  127. *
  128. * E.g. 'stores/2/path' , 'websites/3/path', 'default/path'
  129. *
  130. * @param string $path
  131. * @return string
  132. */
  133. protected function _prependScopeInfo($path)
  134. {
  135. $scopeInfo = $this->getScope();
  136. if ('default' != $this->getScope()) {
  137. $scopeInfo .= '/' . $this->getScopeId();
  138. }
  139. return $scopeInfo . '/' . $path;
  140. }
  141. /**
  142. * Add scope info to path
  143. *
  144. * E.g. 'path/stores/2' , 'path/websites/3', 'path/default'
  145. *
  146. * @param string $path
  147. * @return string
  148. */
  149. protected function _appendScopeInfo($path)
  150. {
  151. $path .= '/' . $this->getScope();
  152. if ('default' != $this->getScope()) {
  153. $path .= '/' . $this->getScopeId();
  154. }
  155. return $path;
  156. }
  157. /**
  158. * Getter for allowed extensions of uploaded files
  159. *
  160. * @return array
  161. */
  162. protected function _getAllowedExtensions()
  163. {
  164. return array();
  165. }
  166. }