DataObjects/Images.php
[Pman.Core] / DataObjects / Images.php
index 07d7adb..78ebae7 100644 (file)
@@ -96,29 +96,6 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
             $roo->jok($r->URL(-1,'/Images') . '#attachment-'.  $r->id);
         }
         
-        if(isset($q['_auto_save'])){
-            require_once 'System.php';
-            
-            $tmpdir  = System::mktemp("-d auto_save");
-            
-            $path = $tmpdir . '/' . time();
-            
-            if(!file_exists($path)){
-               file_put_contents($path, $q['_source']); 
-            }
-            
-            $this->ontable = $q['ontable'];
-            $this->onid = $q['onid'];
-            
-            if (!$this->createFrom($path)) {
-                $roo->jerr("error on auto save making image");
-            }
-            
-            $roo->addEvent("AUTOSAVE", $this, $this->toEventString());
-            
-            $this->jok("OK");
-        }
-        
     }
     
      
@@ -386,6 +363,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         if (!empty($mime_like)) {
             $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
         }
+        $c->orderBy('created DESC');
 
         return $c->fetchAll();
     }
@@ -482,8 +460,12 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         $fc = $this->toFileConvert();
 //        print_r($size);
 //        exit;
-        $fc->convert($this->mimetype, $size);
+        $mt = $this->mimetype;
+        if (!preg_match('#^image/#i',$mt)) {
+            $mt = 'image/jpeg';
+        }
         
+        $fc->convert($mt, $size);
         
         return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
     }
@@ -660,4 +642,137 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         //$p->toEventString();
     }
     
+    function onUploadFromData($filename, $data, $roo)
+    {
+        if (empty($filename) || empty($data)) {
+            $this->err = "Missing file details";
+            return false;
+        }
+        
+        if ($this->id) {
+            $this->beforeDelete();
+        }
+        
+        if (empty($this->ontable)) {
+            $this->err = "Missing  ontable";
+            return false;
+        }
+        
+        if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
+            $img  = DB_DataObject::factory('Images');
+            $img->onid = $this->onid;
+            $img->ontable = $this->ontable;
+            $img->imgtype = $this->imgtype;
+            
+            $img->find();
+            while ($img->fetch()) {
+                $img->beforeDelete();
+                $img->delete();
+            }
+        }
+        
+        preg_match('/^data:([.]*);/', $data, $matches);
+        
+        print_r($matches);exit;
+        
+        require_once 'File/MimeType.php';
+        $y = new File_MimeType();
+        $this->mimetype = $_FILES['imageUpload']['type'];
+        if (in_array($this->mimetype, array(
+                        'text/application',
+                        'application/octet-stream',
+                        'image/x-png',  // WTF does this?
+                        'image/pjpeg',  // WTF does this?
+                        'application/x-apple-msg-attachment', /// apple doing it's magic...
+                        'application/vnd.ms-excel',   /// sometimes windows reports csv as excel???
+                        'application/csv-tab-delimited-table', // windows again!!?
+                ))) { // weird tyeps..
+            $inf = pathinfo($_FILES['imageUpload']['name']);
+            $this->mimetype  = $y->fromExt($inf['extension']);
+        }
+        
+        
+        $ext = $y->toExt(trim((string) $this->mimetype ));
+        
+        $this->filename = empty($this->filename) ? 
+            $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext); 
+        
+        
+        
+        if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
+            return false;
+        }
+        return true;
+         
+    }
+    
+    function createFromData($file, $filename=false)
+    {
+        // copy the file into the storage area..
+        if (!file_exists($file) || !filesize($file)) {
+            return false;
+        }
+        
+        $filename = empty($filename) ? $file : $filename;
+        
+        if (empty($this->mimetype)) {
+            require_once 'File/MimeType.php';
+            $y = new File_MimeType();
+            $this->mimetype = $y->fromFilename($filename);
+        }
+        
+        $this->mimetype= strtolower($this->mimetype);
+        
+        if (array_shift(explode('/', $this->mimetype)) == 'image') { 
+        
+            $imgs = @getimagesize($file);
+            
+            if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
+                // it's a file!!!!
+            } else {
+                list($this->width , $this->height)  = $imgs;
+            }
+        }
+        
+        $this->filesize = filesize($file);
+        $this->created = date('Y-m-d H:i:s');
+         
+        
+        if (empty($this->filename)) {
+            $this->filename = basename($filename);
+        }
+        
+        //DB_DataObject::debugLevel(1);
+        if (!$this->id) {
+            $this->insert();
+        } else {
+            $this->update();
+        }
+        
+        
+        
+        $f = $this->getStoreName();
+        $dest = dirname($f);
+        if (!file_exists($dest)) {
+            // currently this is 0775 due to problems using shared hosing (FTP)
+            // it makes all the files unaccessable..
+            // you can normally solve this by giving the storedirectory better perms
+            // if needed on a dedicated server..
+            $oldumask = umask(0);
+            mkdir($dest, 0775, true);
+            umask($oldumask);  
+        }
+        
+        copy($file,$f);
+        
+        // fill in details..
+        
+        /* thumbnails */
+        
+     
+       // $this->createThumbnail(0,50);
+        return true;
+        
+    }
+    
  }