/app/code/core/Mage/Core/Model/File/Storage/Directory/Database.php

https://bitbucket.org/acidel/buykoala · PHP · 255 lines · 121 code · 32 blank · 102 comment · 17 complexity · de6631d621a8d131d0e31fdad0b87b66 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_Core
  23. * @copyright Copyright (c) 2011 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. * Directory database storage model class
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Model_File_Storage_Directory_Database extends Mage_Core_Model_File_Storage_Database_Abstract
  34. {
  35. /**
  36. * Prefix of model events names
  37. *
  38. * @var string
  39. */
  40. protected $_eventPrefix = 'core_file_storage_directory_database';
  41. /**
  42. * Collect errors during sync process
  43. *
  44. * @var array
  45. */
  46. protected $_errors = array();
  47. /**
  48. * Class construct
  49. *
  50. * @param string $databaseConnection
  51. */
  52. public function __construct($connectionName = null)
  53. {
  54. $this->_init('core/file_storage_directory_database');
  55. parent::__construct($connectionName);
  56. }
  57. /**
  58. * Load object data by path
  59. *
  60. * @param string $path
  61. * @return Mage_Core_Model_File_Storage_Directory_Database
  62. */
  63. public function loadByPath($path)
  64. {
  65. /**
  66. * Clear model data
  67. * addData() is used because it's needed to clear only db storaged data
  68. */
  69. $this->addData(
  70. array(
  71. 'directory_id' => null,
  72. 'name' => null,
  73. 'path' => null,
  74. 'upload_time' => null,
  75. 'parent_id' => null
  76. )
  77. );
  78. $this->_getResource()->loadByPath($this, $path);
  79. return $this;
  80. }
  81. /**
  82. * Check if there was errors during sync process
  83. *
  84. * @return bool
  85. */
  86. public function hasErrors()
  87. {
  88. return !empty($this->_errors);
  89. }
  90. /**
  91. * Retrieve directory parent id
  92. *
  93. * @return int
  94. */
  95. public function getParentId()
  96. {
  97. if (!$this->getData('parent_id')) {
  98. $parentId = $this->_getResource()->getParentId($this->getPath());
  99. if (empty($parentId)) {
  100. $parentId = null;
  101. }
  102. $this->setData('parent_id', $parentId);
  103. }
  104. return $parentId;
  105. }
  106. /**
  107. * Create directories recursively
  108. *
  109. * @param string $path
  110. * @return Mage_Core_Model_File_Storage_Directory_Database
  111. */
  112. public function createRecursive($path)
  113. {
  114. $directory = Mage::getModel('core/file_storage_directory_database')->loadByPath($path);
  115. if (!$directory->getId()) {
  116. $dirName = basename($path);
  117. $dirPath = dirname($path);
  118. if ($dirPath != '.') {
  119. $parentDir = $this->createRecursive($dirPath);
  120. $parentId = $parentDir->getId();
  121. } else {
  122. $dirPath = '';
  123. $parentId = null;
  124. }
  125. $directory->setName($dirName);
  126. $directory->setPath($dirPath);
  127. $directory->setParentId($parentId);
  128. $directory->save();
  129. }
  130. return $directory;
  131. }
  132. /**
  133. * Export directories from storage
  134. *
  135. * @param int $offset
  136. * @param int $count
  137. * @return bool
  138. */
  139. public function exportDirectories($offset = 0, $count = 100)
  140. {
  141. $offset = ((int) $offset >= 0) ? (int) $offset : 0;
  142. $count = ((int) $count >= 1) ? (int) $count : 1;
  143. $result = $this->_getResource()->exportDirectories($offset, $count);
  144. if (empty($result)) {
  145. return false;
  146. }
  147. return $result;
  148. }
  149. /**
  150. * Import directories to storage
  151. *
  152. * @param array $dirs
  153. * @return Mage_Core_Model_File_Storage_Directory_Database
  154. */
  155. public function importDirectories($dirs)
  156. {
  157. if (!is_array($dirs)) {
  158. return $this;
  159. }
  160. $dateSingleton = Mage::getSingleton('core/date');
  161. foreach ($dirs as $dir) {
  162. if (!is_array($dir) || !isset($dir['name']) || !strlen($dir['name'])) {
  163. continue;
  164. }
  165. try {
  166. $directory = Mage::getModel(
  167. 'core/file_storage_directory_database',
  168. array('connection' => $this->getConnectionName())
  169. );
  170. $directory->setPath($dir['path']);
  171. $parentId = $directory->getParentId();
  172. if ($parentId || $dir['path'] == '') {
  173. $directory->setName($dir['name']);
  174. $directory->setUploadTime($dateSingleton->date());
  175. $directory->save();
  176. } else {
  177. Mage::throwException(Mage::helper('core')->__('Parent directory does not exist: %s', $dir['path']));
  178. }
  179. } catch (Exception $e) {
  180. Mage::logException($e);
  181. }
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Clean directories at storage
  187. *
  188. * @return Mage_Core_Model_File_Storage_Directory_Database
  189. */
  190. public function clearDirectories()
  191. {
  192. $this->_getResource()->clearDirectories();
  193. return $this;
  194. }
  195. /**
  196. * Return subdirectories
  197. *
  198. * @param string $directory
  199. * @return mixed
  200. */
  201. public function getSubdirectories($directory)
  202. {
  203. $directory = Mage::helper('core/file_storage_database')->getMediaRelativePath($directory);
  204. return $this->_getResource()->getSubdirectories($directory);
  205. }
  206. /**
  207. * Delete directory from database
  208. *
  209. * @param string $path
  210. * @return Mage_Core_Model_File_Storage_Directory_Database
  211. */
  212. public function deleteDirectory($dirPath)
  213. {
  214. $dirPath = Mage::helper('core/file_storage_database')->getMediaRelativePath($dirPath);
  215. $name = basename($dirPath);
  216. $path = dirname($dirPath);
  217. if ('.' == $path) {
  218. $path = '';
  219. }
  220. $this->_getResource()->deleteDirectory($name, $path);
  221. return $this;
  222. }
  223. }