[dev] [commit] r954 - phplib phplib/models wwwbase/elfinder-connector

automailer at dexonline.ro automailer at dexonline.ro
Fri Aug 23 16:44:44 EEST 2013


Author: cata
Date: Fri Aug 23 16:44:43 2013
New Revision: 954

Log:
minor changes to r950 and r951.

Modified:
   phplib/OS.php
   phplib/models/Visual.php
   wwwbase/elfinder-connector/elFinderModToDB.class.php

Modified: phplib/OS.php
==============================================================================
--- phplib/OS.php	Fri Aug 23 14:50:47 2013	(r953)
+++ phplib/OS.php	Fri Aug 23 16:44:43 2013	(r954)
@@ -34,6 +34,12 @@
     }
     return $output;
   }
+
+  /** Checks if the directory specified in $path is empty */
+  static function isDirEmpty($path) {
+    $files = scandir($path);
+    return count($files) == 2;
+  }
 }
 
 ?>

Modified: phplib/models/Visual.php
==============================================================================
--- phplib/models/Visual.php	Fri Aug 23 14:50:47 2013	(r953)
+++ phplib/models/Visual.php	Fri Aug 23 16:44:43 2013	(r954)
@@ -5,7 +5,6 @@
   public static $parentDir = 'visual';
   public static $thumbDir = '.thumb';
   public static $thumbSize = 200;
-  public static $cmd, $oldThumbPath;
 
   /** Retrieves the path relative to the visual folder */
   public static function getPath($givenPath) {
@@ -15,78 +14,60 @@
     return $matches[0];
   }
 
-  /** Creates the absolute path of the thumb directory based on the $path parameter */
-  static function getThumbDirPath($path) {
-    preg_match('/[^\/]+$/', $path, $name);
-    $path = str_replace($name[0], '', $path);
-    
-    return util_getRootPath() . 'wwwbase/img/' . $path . self::$thumbDir;
-  }
-
-  /** Creates the absolute path of the thumbnail file based on the $path parameter */
-  static function getThumbPath($path) {
-    preg_match('/[^\/]+$/', $path, $name);
-    $path = str_replace($name[0], '', $path);
-
-    return util_getRootPath() . 'wwwbase/img/' . $path . self::$thumbDir . '/' . $name[0];
+  /** Returns the absolute path of the thumb directory */
+  function getThumbDir() {
+    return util_getRootPath() . 'wwwbase/img/' . dirname($this->path) . '/' . self::$thumbDir;
   }
 
-  /** Checks if the directory specified in $path is empty */
-  static function isDirEmpty($path) {
-    $files = scandir($path);
-    if(count($files) == 2) {
-      return true;
-    } else {
-      return false;
-    }
+  /** Returns the absolute path of the thumbnail file */
+  function getThumbPath() {
+    return $this->getThumbDir() . '/' . basename($this->path);
   }
 
   /** Extended by deleting removed image thumbnails */
   function delete() {
     VisualTag::deleteByImageId($this->id);    
 
-    $thumbPath = self::getThumbPath($this->path);
-    $thumbDirPath = self::getThumbDirPath($this->path);
+    $thumbPath = $this->getThumbPath();
+    $thumbDirPath = $this->getThumbDir();
 
     if(file_exists($thumbPath)) {
       unlink($thumbPath);
     }
 
-    if(file_exists($thumbDirPath) && self::isDirEmpty($thumbDirPath)) {
+    if(file_exists($thumbDirPath) && OS::isDirEmpty($thumbDirPath)) {
       rmdir($thumbDirPath);
     }
     
-    parent::delete();
+    return parent::delete();
   }
 
   /** Extended by creating uploaded image thumbnail */
   function save() {
-    switch(self::$cmd) {
-    case 'upload':
-    case 'copy-paste':
-      $thumbDirPath = self::getThumbDirPath($this->path);
+    // Make a directory into which to generate or copy the thumbnail
+    @mkdir($this->getThumbDir(), 0777);
 
-      if(!file_exists($thumbDirPath)) {
-        mkdir($thumbDirPath, 0777);
-      }
+    // Load the original Visual to determine if this is a move/rename or a copy/upload. It slows things down a little, but it's stateless.
+    $original = ($this->id) ? self::get_by_id($this->id) : null;
 
-      $thumbPath = self::getThumbPath($this->path); 
+    // Generate thumbnails for uploads or copy-pastes
+    if (!$original) {
       $thumb = new Imagick(util_getRootPath() . 'wwwbase/img/' . $this->path);
       $thumb->thumbnailImage(self::$thumbSize, self::$thumbSize, true);
-      $thumb->writeImage( $thumbPath);
-    break;
-
-    case 'rename':
-    case 'cut-paste':
-      $newThumbPath = self::getThumbPath($this->path);
+      $thumb->sharpenImage(1, 1);
+      $thumb->writeImage($this->getThumbPath());
+    }
 
-      if(file_exists(self::$oldThumbPath)) {
-        rename(self::$oldThumbPath, $newThumbPath);
+    // Move thumbnails for renames and cut-pastes
+    if ($original && ($original->path != $this->path)) {
+      $oldThumbPath = $original->getThumbPath();
+      $newThumbPath = $this->getThumbPath();
+      if (file_exists($oldThumbPath)) {
+        rename($oldThumbPath, $newThumbPath);
       }
-    break;
     }
 
-    parent::save();
+    return parent::save();
   }
 }
 ?>

Modified: wwwbase/elfinder-connector/elFinderModToDB.class.php
==============================================================================
--- wwwbase/elfinder-connector/elFinderModToDB.class.php	Fri Aug 23 14:50:47 2013	(r953)
+++ wwwbase/elfinder-connector/elFinderModToDB.class.php	Fri Aug 23 16:44:43 2013	(r954)
@@ -6,54 +6,51 @@
 
 class elFinderModToDB extends Visual {
 
-/** Using the bind option in the connector, the elFinder stores the commands
-  * that the user has made in $cmd and the paths to the files or folders
-  * that have been modified in $result. Thus, the action function within this class
-  * listens to what modifications were made and adds/changes entries in the
-  * Visual table accordingly.
-  */
-public function action($cmd, $result, $args, $elfinder) {
+  /** Using the bind option in the connector, the elFinder stores the commands
+   * that the user has made in $cmd and the paths to the files or folders
+   * that have been modified in $result. Thus, the action function within this class
+   * listens to what modifications were made and adds/changes entries in the
+   * Visual table accordingly.
+   */
+  public function action($cmd, $result, $args, $elfinder) {
 
-  switch($cmd){
+    switch($cmd){
     case 'upload': 
-    if(!empty($result['added'])) {
-      foreach($result['added'] as $file) {
-        $path = Visual::getPath($elfinder->realpath($file['hash']));
-        Visual::$cmd = $cmd;
-
-        $line = Model::factory('Visual')->create();
-        $line->path = $path;
-        $line->userId = session_getUserId();
-        $line->save();
+      if(!empty($result['added'])) {
+        foreach($result['added'] as $file) {
+          $path = Visual::getPath($elfinder->realpath($file['hash']));
+          $line = Model::factory('Visual')->create();
+          $line->path = $path;
+          $line->userId = session_getUserId();
+          $line->save();
+        }
       }
-    }
-    break;
+      break;
 			
     case 'rm':
-    if(!empty($result['removed'])) {
-      foreach($result['removed'] as $file) {
-        $path = Visual::getPath($file['realpath']);
-
-        $line = Visual::get_by_path($path);
-        /** rm command stores its info in $result['removed'] even for folders,
-          * thus, it first checks if there were entries in the table that
-          * matched the value in $file. */
-        if(!empty($line)) {
-          $line->delete();
+      if(!empty($result['removed'])) {
+        foreach($result['removed'] as $file) {
+          $path = Visual::getPath($file['realpath']);
+
+          $line = Visual::get_by_path($path);
+          /** rm command stores its info in $result['removed'] even for folders,
+           * thus, it first checks if there were entries in the table that
+           * matched the value in $file. */
+          if(!empty($line)) {
+            $line->delete();
+          }
         }
       }
-    }
-    break;
+      break;
 
     case 'rename':
       if(!empty($result['removed'])) {
-      /** As the rename array stores both files and folders first it
-        * identifies the type. If a folder is renamed, then all the
-        * paths of the files within it are modified accordingly. */
+        /** As the rename array stores both files and folders first it
+         * identifies the type. If a folder is renamed, then all the
+         * paths of the files within it are modified accordingly. */
         $oldPath = Visual::getPath($result['removed'][0]['realpath']);
         $newPath = Visual::getPath($elfinder->realpath($result['added'][0]['hash']));
         $lines = Model::factory('Visual')->where_like('path', "{$oldPath}/%")->find_many();
-        Visual::$cmd = $cmd;
 
         if(!empty($lines)) {
           /** Directory was renamed **/
@@ -67,7 +64,6 @@
           $line = Visual::get_by_path($oldPath);
 
           if(!empty($line)) {
-            Visual::$oldThumbPath = Visual::getThumbPath($oldPath);
             $line->path = $newPath;
             $line->save();
           }
@@ -75,18 +71,15 @@
       }
       break;
 
-      case 'paste':
+    case 'paste':
       /** Cut - Paste */
       if(!empty($result['removed'])) {
-        Visual::$cmd = 'cut-paste';
-
         foreach($result['removed'] as $i => $file) {
           $oldPath = Visual::getPath($file['realpath']);
           $newPath = Visual::getPath($elfinder->realpath($result['added'][$i]['hash']));
           $line = Visual::get_by_path($oldPath);
 
           if(!empty($line)) {
-            Visual::$oldThumbPath = Visual::getThumbPath($oldPath);
             $line->path = $newPath;
             $line->save();
           }
@@ -94,8 +87,6 @@
 
       /** Copy - Paste */
       } else if(!empty($result['added'])) {
-        Visual::$cmd = 'copy-paste';
-
         foreach($result['added'] as $file) {
           $path = Visual::getPath($elfinder->realpath($file['hash']));
 


More information about the Dev mailing list