1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Core_Model_File_Storage_Directory_Database extends Mage_Core_Model_File_Storage_Database_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_eventPrefix = 'core_file_storage_directory_database';
43:
44: 45: 46: 47: 48:
49: protected $_errors = array();
50:
51: 52: 53: 54: 55:
56: public function __construct($connectionName = null)
57: {
58: $this->_init('core/file_storage_directory_database');
59:
60: parent::__construct($connectionName);
61: }
62:
63: 64: 65: 66: 67: 68:
69: public function loadByPath($path)
70: {
71: 72: 73: 74:
75: $this->addData(
76: array(
77: 'directory_id' => null,
78: 'name' => null,
79: 'path' => null,
80: 'upload_time' => null,
81: 'parent_id' => null
82: )
83: );
84:
85: $this->_getResource()->loadByPath($this, $path);
86: return $this;
87: }
88:
89: 90: 91: 92: 93:
94: public function hasErrors()
95: {
96: return !empty($this->_errors);
97: }
98:
99: 100: 101: 102: 103:
104: public function getParentId()
105: {
106: if (!$this->getData('parent_id')) {
107: $parentId = $this->_getResource()->getParentId($this->getPath());
108: if (empty($parentId)) {
109: $parentId = null;
110: }
111:
112: $this->setData('parent_id', $parentId);
113: }
114:
115: return $parentId;
116: }
117:
118: 119: 120: 121: 122: 123:
124: public function createRecursive($path)
125: {
126: $directory = Mage::getModel('core/file_storage_directory_database')->loadByPath($path);
127:
128: if (!$directory->getId()) {
129: $dirName = basename($path);
130: $dirPath = dirname($path);
131:
132: if ($dirPath != '.') {
133: $parentDir = $this->createRecursive($dirPath);
134: $parentId = $parentDir->getId();
135: } else {
136: $dirPath = '';
137: $parentId = null;
138: }
139:
140: $directory->setName($dirName);
141: $directory->setPath($dirPath);
142: $directory->setParentId($parentId);
143: $directory->save();
144: }
145:
146: return $directory;
147: }
148:
149: 150: 151: 152: 153: 154: 155:
156: public function exportDirectories($offset = 0, $count = 100)
157: {
158: $offset = ((int) $offset >= 0) ? (int) $offset : 0;
159: $count = ((int) $count >= 1) ? (int) $count : 1;
160:
161: $result = $this->_getResource()->exportDirectories($offset, $count);
162:
163: if (empty($result)) {
164: return false;
165: }
166:
167: return $result;
168: }
169:
170: 171: 172: 173: 174: 175:
176: public function importDirectories($dirs)
177: {
178: if (!is_array($dirs)) {
179: return $this;
180: }
181:
182: $dateSingleton = Mage::getSingleton('core/date');
183: foreach ($dirs as $dir) {
184: if (!is_array($dir) || !isset($dir['name']) || !strlen($dir['name'])) {
185: continue;
186: }
187:
188: try {
189: $directory = Mage::getModel(
190: 'core/file_storage_directory_database',
191: array('connection' => $this->getConnectionName())
192: );
193: $directory->setPath($dir['path']);
194:
195: $parentId = $directory->getParentId();
196: if ($parentId || $dir['path'] == '') {
197: $directory->setName($dir['name']);
198: $directory->setUploadTime($dateSingleton->date());
199: $directory->save();
200: } else {
201: Mage::throwException(Mage::helper('core')->__('Parent directory does not exist: %s', $dir['path']));
202: }
203: } catch (Exception $e) {
204: Mage::logException($e);
205: }
206: }
207:
208: return $this;
209: }
210:
211: 212: 213: 214: 215:
216: public function clearDirectories()
217: {
218: $this->_getResource()->clearDirectories();
219: return $this;
220: }
221:
222: 223: 224: 225: 226: 227:
228: public function getSubdirectories($directory)
229: {
230: $directory = Mage::helper('core/file_storage_database')->getMediaRelativePath($directory);
231:
232: return $this->_getResource()->getSubdirectories($directory);
233: }
234:
235: 236: 237: 238: 239: 240:
241: public function deleteDirectory($dirPath)
242: {
243: $dirPath = Mage::helper('core/file_storage_database')->getMediaRelativePath($dirPath);
244: $name = basename($dirPath);
245: $path = dirname($dirPath);
246:
247: if ('.' == $path) {
248: $path = '';
249: }
250:
251: $this->_getResource()->deleteDirectory($name, $path);
252:
253: return $this;
254: }
255: }
256: