[289] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Class Directory_model
|
---|
| 4 | *
|
---|
| 5 | * @author dzungnv02
|
---|
| 6 | *
|
---|
| 7 | */
|
---|
| 8 | class Directory_model extends CI_Model
|
---|
| 9 | {
|
---|
| 10 |
|
---|
| 11 | public function __construct()
|
---|
| 12 | {
|
---|
| 13 | // Call the Model constructor
|
---|
| 14 | parent::__construct();
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | public function getAllDirectory () {
|
---|
| 18 | $dbFile = 'directory.db';
|
---|
| 19 | $h = fopen($dbFile, 'r');
|
---|
| 20 | $line = '';
|
---|
| 21 | $aryDir = array();
|
---|
| 22 | $aryField = array ('id', 'name', 'parentID');
|
---|
| 23 |
|
---|
| 24 | while (($line = fgets($h, 4096)) !== false) {
|
---|
| 25 | $aryTmp = explode(',', trim($line));
|
---|
| 26 | $aryDir[] = array_combine($aryField, $aryTmp);
|
---|
| 27 | }
|
---|
| 28 | if (!feof($h)) {
|
---|
| 29 | echo "Error: unexpected fgets() fail\n";
|
---|
| 30 | }
|
---|
| 31 | fclose($h);
|
---|
| 32 |
|
---|
| 33 | return $aryDir;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | public function getAllFile () {
|
---|
| 37 | $dbFile = 'file.db';
|
---|
| 38 | $h = fopen($dbFile, 'r');
|
---|
| 39 | $line = '';
|
---|
| 40 | $aryFile = array();
|
---|
| 41 | $aryField = array ('id', 'name', 'minetype', 'size', 'parentID');
|
---|
| 42 |
|
---|
| 43 | while (($line = fgets($h, 4096)) !== false) {
|
---|
| 44 | $aryTmp = explode(',', trim($line));
|
---|
| 45 | $aryFile[] = array_combine($aryField, $aryTmp);
|
---|
| 46 | }
|
---|
| 47 | if (!feof($h)) {
|
---|
| 48 | echo "Error: unexpected fgets() fail\n";
|
---|
| 49 | }
|
---|
| 50 | fclose($h);
|
---|
| 51 |
|
---|
| 52 | return $aryFile;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public function createDir($aryDir) {
|
---|
| 56 | $dbFile = 'directory.db';
|
---|
| 57 | $aryField = array ('id', 'name', 'parentID');
|
---|
| 58 | $aryTmp = array();
|
---|
| 59 | $h = fopen($dbFile, 'a');
|
---|
| 60 | $line = '';
|
---|
| 61 | foreach ($aryField as $key => $field) {
|
---|
| 62 | $aryTmp[$field] = $aryDir[$field];
|
---|
| 63 | }
|
---|
| 64 | $line = implode(',',$aryTmp);
|
---|
| 65 | fwrite($h, $line."\n");
|
---|
| 66 | fclose($h);
|
---|
| 67 | return true;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public function createFile($aryFile) {
|
---|
| 71 | $dbFile = 'file.db';
|
---|
| 72 | $aryField = array ('id', 'name', 'minetype', 'size', 'parentID');
|
---|
| 73 | $aryTmp = array();
|
---|
| 74 | $h = fopen($dbFile, 'a+');
|
---|
| 75 | $line = '';
|
---|
| 76 | foreach ($aryField as $key => $field) {
|
---|
| 77 | $aryTmp[$field] = $aryFile[$field];
|
---|
| 78 | }
|
---|
| 79 | $line = implode(',',$aryTmp);
|
---|
| 80 | fwrite($line."\n", $h);
|
---|
| 81 | fclose($h);
|
---|
| 82 | return true;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public function deleteDir ($dirID, $delAllChild = FALSE) {
|
---|
| 86 | $dbFile = 'directory.db';
|
---|
| 87 | $dirList = $this->getAllDirectory();
|
---|
| 88 |
|
---|
| 89 | $lineNumber = 0;
|
---|
| 90 |
|
---|
| 91 | foreach ($dirList as $key => $dir) {
|
---|
| 92 | if ($dir[0] == $dirID) {
|
---|
| 93 | $lineNumber = $key;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | //$aryChildDir =
|
---|
| 98 |
|
---|
| 99 | if ($delAllChild) {
|
---|
| 100 |
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | unset($dirList[$lineNumber]);
|
---|
| 104 | return $this->writeDB ($dbFile, $dirList);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | public function deleteFile ($fileID) {
|
---|
| 108 | $dbFile = 'file.db';
|
---|
| 109 | $fileList = $this->getAllFile();
|
---|
| 110 |
|
---|
| 111 | $lineNumber = 0;
|
---|
| 112 |
|
---|
| 113 | foreach ($fileList as $key => $file) {
|
---|
| 114 | if ($dir[0] == $dirID) {
|
---|
| 115 | $lineNumber = $key;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | unset($fileList[$lineNumber]);
|
---|
| 120 | return $this->writeDB ($dbFile, $fileList);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | public function getAllFileChild ($dirID) {
|
---|
| 124 | $fileList = $this->getAllFile();
|
---|
| 125 | $aryChild = array();
|
---|
| 126 |
|
---|
| 127 | $aryDirChild = $this->getAllDirChild($dirID);
|
---|
| 128 |
|
---|
| 129 | foreach ($aryDirChild as $key => $child) {
|
---|
| 130 |
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | public function getAllDirChild ($dirID) {
|
---|
| 136 | $dirList = $this->getAllDirectory();
|
---|
| 137 | $aryChild = array();
|
---|
| 138 |
|
---|
| 139 | foreach ($dirList as $key => $dir) {
|
---|
| 140 | if ($dir['parentID'] == $dirID) {
|
---|
| 141 | $aryChild[$dir['id']] = $dir;
|
---|
| 142 |
|
---|
| 143 | $aryChild[$dir['id']]['child'] = $this->getAllDirChild($dir['id']);
|
---|
| 144 | if (count($aryChild[$dir['id']]['child']) == 0) unset($aryChild[$dir['id']]['child']);
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | return $aryChild;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | private function writeDB ($file, $aryDataSet, $flag = '') {
|
---|
| 152 | $flag = ($flag == 'append') ? 'a' : 'w';
|
---|
| 153 |
|
---|
| 154 | if (file_exists($file) && count($aryDataSet) > 0) {
|
---|
| 155 | $h = fopen ($file, $flag);
|
---|
| 156 | foreach ($aryDataSet as $key => $aryTmp) {
|
---|
| 157 | $line = implode(',',$aryTmp);
|
---|
| 158 | fwrite($h, $line."\n");
|
---|
| 159 | }
|
---|
| 160 | fclose ($h);
|
---|
| 161 |
|
---|
| 162 | return true;
|
---|
| 163 | }else {
|
---|
| 164 | return false;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public function getMaxID($isDir) {
|
---|
| 169 | $list = $isDir = true ? $this->getAllDirectory() : $this->getAllFile();
|
---|
| 170 | $maxID = 0;
|
---|
| 171 | foreach ($list as $key => $e) {
|
---|
| 172 | if ($e['id'] > $maxID) $maxID = $e['id'];
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | return $maxID;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | public function rename ($id, $newName, $type = 'directory') {
|
---|
| 179 |
|
---|
| 180 | if (!$newName || !$id) return FALSE;
|
---|
| 181 | $aryObj = $type == 'directory' ? $this->getAllDirectory() : $this->getAllFile();
|
---|
| 182 | $dbFile = $type == 'directory' ? $dbFile = 'directory.db' : $dbFile = 'file.db';
|
---|
| 183 |
|
---|
| 184 | if (count($aryObj) > 0) {
|
---|
| 185 | foreach ($aryObj as $key => $obj) {
|
---|
| 186 | if ($obj['id'] == $id) break;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | $aryObj[$key]['name'] = $newName;
|
---|
| 190 |
|
---|
| 191 | return $this->writeDB($dbFile, $aryObj);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | return FALSE;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | } |
---|