/[drupal]/contributions/modules/fast_gallery/storageengine/default.storage.inc
ViewVC logotype

Diff of /contributions/modules/fast_gallery/storageengine/default.storage.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.6, Tue Nov 10 18:20:18 2009 UTC revision 1.7, Wed Nov 11 21:43:33 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  //$Id:  //$Id:
3  include_once('Istorage.php');  include_once('Istorage.php');
4    
5    /**
6     * This function needs to be implemented!!!
7     * @return Istorage
8     */
9  function fast_gallery_get_storage() {  function fast_gallery_get_storage() {
10    return new DefaultStorage();    return new DefaultStorage();
11  }  }
# Line 19  class DefaultStorage implements Istorage Line 23  class DefaultStorage implements Istorage
23      if ($arTmp[sizeof($arTmp)-1] == '') {      if ($arTmp[sizeof($arTmp)-1] == '') {
24        array_pop($arTmp);        array_pop($arTmp);
25      }      }
26      $path = implode("/", $arTmp);      $folder = implode("/", $arTmp);
   
     //get the images  
     $row = db_query("SELECT uri, fgh.foid FROM {fast_gallery} as fg, {file} as f, {fast_gallery_hierarchy} as fgh  
             WHERE fg.foid = fgh.foid  
             AND fgh.folder = :path  
             AND fg.fid = f.fid", array(':path' => $path), array('fetch' => PDO::FETCH_ASSOC));  
   
27      $return = array();      $return = array();
28    
29        //incase we want to display the subfolder first
30        if (variable_get('fg_folder_first', FALSE)) {
31          $foid = db_query("SELECT foid FROM {fast_gallery_hierarchy} WHERE folder=:folder", array(':folder' => $folder))->fetchField();
32          $return = array_merge($return, $this->getFolders($foid));
33        }
34    
35        //fetch images
36        $row = db_query("SELECT filename, folder, fgf.foid FROM {fast_gallery_file} as fgf, {fast_gallery_hierarchy} as fgh
37                         WHERE fgf.foid = fgh.foid
38                         AND fgh.folder = :folder",
39                         array(':folder' => $folder), array('fetch' => PDO::FETCH_ASSOC));
40    
41      $foid = 0;      $foid = 0;
42      foreach ($row as $item) {      foreach ($row as $item) {
43        $return[] = new FGImage($item['uri']);        $return[] = new FGImage($item['folder'] . '/' . $item['filename']);
44        $foid = $item['foid'];        $foid = $item['foid'];
45      }      }
46    
47      //get the folders      //incase we display the subfolders last
48        if (!variable_get('fg_folder_first', FALSE)) {
49          $return = array_merge($return, $this->getFolders($foid));
50        }
51    
52        return $return;
53      }
54    
55      /**
56       * Get the subfolders of a given folder
57       * @param int $foid
58       * @return array
59       */
60      private function getFolders($foid) {
61        $folders = array();
62      $row = db_query("SELECT folder FROM {fast_gallery_hierarchy} WHERE pid = :foid", array(':foid' => $foid), array('fetch' => PDO::FETCH_ASSOC));      $row = db_query("SELECT folder FROM {fast_gallery_hierarchy} WHERE pid = :foid", array(':foid' => $foid), array('fetch' => PDO::FETCH_ASSOC));
63      foreach ($row as $folder) {      foreach ($row as $folder) {
64        $return[] = new FGImage($folder['folder'], array('dir' => TRUE));        $folders[] = new FGImage($folder['folder'], array('dir' => TRUE));
65      }      }
66      return $return;      return $folders;
67    }    }
68    
69    /**    /**
# Line 53  class DefaultStorage implements Istorage Line 77  class DefaultStorage implements Istorage
77      global $user;      global $user;
78      static $first_run = TRUE;      static $first_run = TRUE;
79      static $runid;      static $runid;
80      $runid = variable_get('fast_gallery_runid', 0);      $runid = variable_get('fast_gallery_runid', 0);
81    
82      //TODO: This makes the APP slow -> Rebuilding the whole      //TODO: This makes the APP slow -> Rebuilding the whole
83      // Gallery every time!      // Gallery every time!
84      if ($first_run) {      if ($first_run) {
85        db_query("TRUNCATE TABLE  `fast_gallery_hierarchy`");        db_query("TRUNCATE TABLE  `fast_gallery_hierarchy`");
       //db_query("TRUNCATE TABLE  `fast_gallery`");  
86        $first_run = FALSE;        $first_run = FALSE;
       dsm($runid, "runid");  
87        variable_set('fast_gallery_runid', ++$runid);        variable_set('fast_gallery_runid', ++$runid);
88      }      }
89    
90      foreach ($arImages as $image) {      foreach ($arImages as $image) {
91        $file = new stdClass();        $foid = $this->storeHierarchy($image);
92        $file->uri = $image->getPath();  
93        $file->uid = $user->uid;        db_merge('fast_gallery_file')
94        $file->filename = $image->getName();        ->key(array('foid' => $foid, 'filename' => $image->getName()))
95        $file->fid = $image->getFid();        ->fields(array('runid' => $runid))
96        file_save($file);        ->execute();
       $file->foid = $this->storeHierarchy($image);  
       $file->runid = $runid;  
   
   
       //only write file if it doesn't exist yet  
       $exists = db_query("SELECT count(fid) as c FROM {fast_gallery} WHERE fid=:fid AND foid=:foid", array(':fid' => $file->fid, ':foid' => $file->foid))->fetchField();  
       if ($exists == 0) {  
         drupal_write_record('fast_gallery', $file);  
       }  
       else {  
         db_update('fast_gallery')  
         ->fields(array('runid' => $runid))  
         ->condition('fid', $file->fid)  
         ->execute();  
       }  
   
97      }      }
98    }    }
99    
100    /**    /**
101     * (non-PHPdoc)     * (non-PHPdoc)
102     * @see sites/all/modules/fast_gallery/storageengine/Istorage#clearDb()     * @see sites/all/modules/fast_gallery/storageengine/Istorage#clearDb()
103     */     */
104    public function clearDb() {    public function clearDb() {
105      $row = db_query("SELECT fid FROM {fast_gallery}", array(), array('fetch' => PDO::FETCH_ASSOC));      db_truncate('fast_gallery_hierarchy')->execute();
106      foreach ($row as $item) {      db_truncate('fast_gallery_file')->execute();
       db_query("DELETE FROM {file} WHERE fid=:fid", array(':fid' => $item['fid']));  
     }  
     db_query("TRUNCATE TABLE {fast_gallery_hierarchy}");  
     db_query("TRUNCATE TABLE {fast_gallery}");  
107    }    }
108    
109    /**    /**
110     * (non-PHPdoc)     * (non-PHPdoc)
111     * @see sites/all/modules/fast_gallery/storageengine/Istorage#removeDeletedFiles()     * @see sites/all/modules/fast_gallery/storageengine/Istorage#removeDeletedFiles()
112     */     */
113    public function removeDeletedFiles() {    public function removeDeletedFiles() {
114        db_query("DELETE FROM {fast_gallery_file} WHERE runid < :runid", array('runid' => variable_get('fast_gallery_runid', 0)));
115    }    }
116    
117    /**    /**
118     * In order to retrieve the hierarchy we need to store this information     * In order to retrieve the hierarchy we need to store this information
119     * in a special table     * in a special table
# Line 125  class DefaultStorage implements Istorage Line 127  class DefaultStorage implements Istorage
127      if (!in_array($image->getFolder(), $folders)) {      if (!in_array($image->getFolder(), $folders)) {
128        $folder = db_query("SELECT folder AS c FROM {fast_gallery_hierarchy}        $folder = db_query("SELECT folder AS c FROM {fast_gallery_hierarchy}
129                WHERE folder = :folder", array(':folder' => $image->getFolder()))->fetchField();                WHERE folder = :folder", array(':folder' => $image->getFolder()))->fetchField();
130    
131        if (!$folder) { //incase we don't have this folder in our db        if (!$folder) { //incase we don't have this folder in our db
132          $dbObject['folder'] = $image->getFolder();          $dbObject['folder'] = $image->getFolder();
133          $dbObject['pid'] = db_query("SELECT foid FROM {fast_gallery_hierarchy}          $dbObject['pid'] = db_query("SELECT foid FROM {fast_gallery_hierarchy}
134                              WHERE folder = :folder", array(':folder' => $image->getParent()))->fetchField();                              WHERE folder = :folder", array(':folder' => $image->getParent()))->fetchField();
135          drupal_write_record('fast_gallery_hierarchy', $dbObject);          drupal_write_record('fast_gallery_hierarchy', $dbObject);
136    
137          //TODO: needs to be checked!!! needs to return the foid, so that we can make          //TODO: needs to be checked!!! needs to return the foid, so that we can make
138          //the correct entry into fast_gallery          //the correct entry into fast_gallery
139          $foid = db_query("SELECT MAX(foid) FROM {fast_gallery_hierarchy}")->fetchField();          $foid = db_query("SELECT MAX(foid) FROM {fast_gallery_hierarchy}")->fetchField();
# Line 142  class DefaultStorage implements Istorage Line 144  class DefaultStorage implements Istorage
144      $ar = array_flip($folders);      $ar = array_flip($folders);
145      return $ar[$image->getFolder()];      return $ar[$image->getFolder()];
146    }    }
147    
148    
149    /**  
    * (non-PHPdoc)  
    * @see sites/all/modules/fast_gallery/storageengine/Istorage#deleteFile($file)  
    */  
   public function deleteFile($file) {  
     db_query("DELETE FROM {fast_gallery} WHERE fid=:fid", array(':fid' => $file->fid));  
   }  
   
150  }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

  ViewVC Help
Powered by ViewVC 1.1.2