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_Resource_File_Storage_Directory_Database extends Mage_Core_Model_Resource_File_Storage_Abstract
36: {
37: 38: 39:
40: protected function _construct()
41: {
42: $this->_init('core/directory_storage', 'directory_id');
43: }
44:
45: 46: 47: 48: 49:
50: public function createDatabaseScheme()
51: {
52: $adapter = $this->_getWriteAdapter();
53: $table = $this->getMainTable();
54: if ($adapter->isTableExists($table)) {
55: return $this;
56: }
57:
58: $ddlTable = $adapter->newTable($table)
59: ->addColumn('directory_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
60: 'identity' => true,
61: 'unsigned' => true,
62: 'nullable' => false,
63: 'primary' => true
64: ), 'Directory Id')
65: ->addColumn('name', Varien_Db_Ddl_Table::TYPE_TEXT, 100, array(
66: 'nullable' => false
67: ), 'Directory Name')
68: ->addColumn('path', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array(
69: 'default' => null), 'Path to the Directory')
70: ->addColumn('upload_time', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
71: 'nullable' => false,
72: 'default' => Varien_Db_Ddl_Table::TIMESTAMP_INIT
73: ), 'Upload Timestamp')
74: ->addColumn('parent_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
75: 'nullable' => true,
76: 'default' => null,
77: 'unsigned' => true
78: ), 'Parent Directory Id')
79: ->addIndex($adapter->getIndexName($table, array('name', 'parent_id'),
80: Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE),
81: array('name', 'parent_id'), array('type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE))
82: ->addIndex($adapter->getIndexName($table, array('parent_id')), array('parent_id'))
83: ->addForeignKey($adapter->getForeignKeyName($table, 'parent_id', $table, 'directory_id'),
84: 'parent_id', $table, 'directory_id',
85: Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
86: ->setComment('Directory Storage');
87:
88: $adapter->createTable($ddlTable);
89: return $this;
90: }
91:
92: 93: 94: 95: 96: 97: 98:
99: public function loadByPath(Mage_Core_Model_File_Storage_Directory_Database $object, $path)
100: {
101: $adapter = $this->_getReadAdapter();
102:
103: $name = basename($path);
104: $path = dirname($path);
105: if ($path == '.') {
106: $path = '';
107: }
108:
109: $select = $adapter->select()
110: ->from(array('e' => $this->getMainTable()))
111: ->where('name = ?', $name)
112: ->where($adapter->prepareSqlCondition('path', array('seq' => $path)));
113:
114: $data = $adapter->fetchRow($select);
115: if ($data) {
116: $object->setData($data);
117: $this->_afterLoad($object);
118: }
119:
120: return $this;
121: }
122:
123: 124: 125: 126: 127: 128:
129: public function getParentId($path)
130: {
131: $adapter = $this->_getReadAdapter();
132:
133: $name = basename($path);
134: $path = dirname($path);
135: if ($path == '.') {
136: $path = '';
137: }
138:
139: $select = $adapter->select()
140: ->from(
141: array('e' => $this->getMainTable()),
142: array('directory_id')
143: )
144: ->where('name = ?', $name)
145: ->where($adapter->prepareSqlCondition('path', array('seq' => $path)));
146:
147: return $adapter->fetchOne($select);
148: }
149:
150: 151: 152: 153: 154:
155: public function clearDirectories()
156: {
157: $adapter = $this->_getWriteAdapter();
158: $adapter->delete($this->getMainTable());
159:
160: return $this;
161: }
162:
163: 164: 165: 166: 167: 168: 169:
170: public function exportDirectories($offset, $count = 100)
171: {
172: $adapter = $this->_getReadAdapter();
173:
174: $select = $adapter->select()
175: ->from(
176: array('e' => $this->getMainTable()),
177: array('name', 'path')
178: )
179: ->order('directory_id')
180: ->limit($count, $offset);
181:
182: return $adapter->fetchAll($select);
183: }
184:
185: 186: 187: 188: 189: 190:
191: public function getSubdirectories($directory)
192: {
193: $directory = trim($directory, '/');
194: $adapter = $this->_getReadAdapter();
195:
196: $select = $adapter->select()
197: ->from(
198: array('e' => $this->getMainTable()),
199: array('name', 'path')
200: )
201: ->where($adapter->prepareSqlCondition('path', array('seq' => $directory)))
202: ->order('directory_id');
203:
204: return $adapter->fetchAll($select);
205: }
206:
207: 208: 209: 210: 211: 212:
213: public function deleteDirectory($name, $path)
214: {
215: $adapter = $this->_getWriteAdapter();
216:
217: $where = array('name = ?' => $name);
218: $where[] = new Zend_Db_Expr($adapter->prepareSqlCondition('path', array('seq' => $path)));
219:
220: $adapter->delete($this->getMainTable(), $where);
221: }
222: }
223: