DataObjects/core.sql
[Pman.Core] / DataObjects / Images.php
index d328709..56285fc 100644 (file)
@@ -54,9 +54,39 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         
     }
     
-    
-    
-    
+    function beforeInsert($q, $roo) 
+    {
+        if (isset($q['_remote_upload'])) {
+            require_once 'System.php';
+            $tmpdir  = System::mktemp("-d remote_upload");
+            
+            $path = $tmpdir . '/' . basename($q['_remote_upload']);
+            if(!file_exists($path)){
+               file_put_contents($path, file_get_contents($q['_remote_upload'])); 
+            }
+            
+            $imageInfo = getimagesize($path);
+            require_once 'File/MimeType.php';
+            $y = new File_MimeType();
+            $ext = $y->toExt(trim((string) $imageInfo['mime'] ));
+            
+            if (!preg_match("/\." . $ext."$/", $path, $matches)) {
+                rename($path,$path.$ext);
+            }
+            
+            $this->createFrom($path);
+            
+            $roo->addEvent("ADD", $this, $this->toEventString());
+        
+            $r = DB_DataObject::factory($this->tableName());
+            $r->id = $this->id;
+            $roo->loadMap($r);
+            $r->limit(1);
+            $r->find(true);
+            $roo->jok($r->URL(-1,'/Images') . '#attachment-'.  $r->id);
+        }
+        
+    }
     
     
     /**
@@ -148,29 +178,6 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         ));
           
     }
-    
-    function onInsert($request,$roo)
-    {
-        
-        
-        if(!empty($this->imgtype)){
-            $c = DB_DataObject::factory('Companies');
-
-            if(!$c->get($this->onid)){
-                return;
-            }
-
-            if(!empty($c->logo_id)){
-                $xx = DB_DataObject::factory('Images');
-                $xx->get($c->logo_id);
-                $xx->delete();
-            }
-
-            $c->logo_id = $this->id;
-            $c->update();
-        }
-        
-    }
      
     /**
      * deletes all the image instances of it...
@@ -472,6 +479,17 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         return $fc;
         
     }
+    
+    function fileExt()
+    {
+        require_once 'File/MimeType.php';
+        
+        $y = new File_MimeType();
+        return  $y->toExt($this->mimetype);
+        
+        
+    }
+    
     /**
      *
      *
@@ -532,6 +550,8 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
             $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
         }
         
+        
+        
         if (!isset($_FILES['imageUpload'])) {
             return; // standard update...
         }