Ignore:
Timestamp:
Jun 4, 2014 5:53:33 PM (11 years ago)
Author:
dungnv
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pro-bachkim-filespace/sourcecode/application/modules/ajax/models/directory_model.php

    r1 r11  
    99{
    1010
    11         function __construct()
     11        public function __construct()
    1212        {
    1313                // Call the Model constructor
    1414                parent::__construct();
    1515        }
     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        }
     130       
     131        public function getAllDirChild ($dirID) {
     132                $dirList = $this->getAllDirectory();
     133                $aryChild = array();
     134               
     135                foreach ($dirList as $key => $dir) {
     136                        if ($dir['parentID'] == $dirID) {
     137                                $aryChild[$dir['id']] = $dir;
     138                               
     139                                $aryChild[$dir['id']]['child'] = $this->getAllDirChild($dir['id']);
     140                                if (count($aryChild[$dir['id']]['child']) == 0) unset($aryChild[$dir['id']]['child']);
     141                        }
     142                }
     143               
     144                return $aryChild;
     145        }
     146       
     147        private function writeDB ($file, $aryDataSet, $flag = '') {
     148                $flag = ($flag == 'append') ? 'a' : 'w';
     149               
     150                if (file_exists($file) && count($aryDataSet) > 0) {
     151                        $h = fopen ($file, $flag);
     152                        foreach ($aryDataSet as $key => $line) {
     153                                $line = implode(',',$aryTmp);
     154                                fwrite($line."\n", $h);
     155                        }
     156                        fclose ($h);
     157                       
     158                        return true;
     159                }else {
     160                        return false;
     161                }
     162        }
     163       
     164        public function getMaxID($isDir) {
     165                $list = $isDir = true ? $this->getAllDirectory() : $this->getAllFile();
     166                $maxID = 0;
     167                foreach ($list as $key => $e) {
     168                        if ($e['id'] > $maxID) $maxID = $e['id'];
     169                }
     170               
     171                return $maxID;
     172        }
     173       
    16174}
Note: See TracChangeset for help on using the changeset viewer.