DataObjects/Images.php
[Pman.Core] / DataObjects / Images.php
index 0f63f5e..6fdda46 100644 (file)
@@ -30,23 +30,20 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
     
-    function checkPerm($perm, $au)
+    function checkPerm($lvl, $au)
     {
         // default permissons are to
         // allow create / edit / if the user has
         
         if (!$au) {
-            
-          
-            
             return false;
         }
         
         $o = $this->object();
         //print_r($o);
-        if (method_exists($o, 'hasPerm')) {
+        if (method_exists($o, 'checkPerm')) {
             // edit permissions on related object needed...
-            return $o->hasPerm( $perm == 'S' ? 'S' : 'E' , $au);
+            return $o->checkPerm( $lvl == 'S' ? 'S' : 'E' , $au);
             
         }
         
@@ -96,32 +93,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();
-            
-            $source = array(
-                'source' => $q['_source']
-            );
-            
-//            if(!file_exists($path)){
-//               file_put_contents($path, $q['_source']); 
-//            }
-//            
-//            $this->setFrom($q);
-//            
-//            if (!$this->createFrom($path)) {
-//                $roo->jerr("error on auto save making image");
-//            }
-//            
-//            $roo->addEvent("AUTOSAVE", $this, $this->toEventString());
-            
-            $this->jok("OK");
-        }
-        
     }
     
      
@@ -294,7 +265,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     /// ctrl not used??
     function onUpload($roo)
     {
-//        echo $_FILES['imageUpload']['type'];exit;
+        //print_r($_FILES); echo $_FILES['imageUpload']['type'];exit;
         if (empty($_FILES['imageUpload']['tmp_name']) || 
             empty($_FILES['imageUpload']['name']) || 
             empty($_FILES['imageUpload']['type'])
@@ -351,7 +322,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
             $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext); 
         
         
-        
+        print_R($this);exit;
         if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
             return false;
         }
@@ -389,6 +360,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();
     }
@@ -485,8 +457,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}";
     }
@@ -663,4 +639,107 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         //$p->toEventString();
     }
     
+    function onUploadFromData($data, $roo)
+    {
+        if (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)) {
+            // then its an upload 
+            $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();
+            }
+            
+        }
+        
+        require_once 'File/MimeType.php';
+        $y = new File_MimeType();
+        
+        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($this->filename);
+            $this->mimetype  = $y->fromExt($inf['extension']);
+        }
+        
+        $ext = $y->toExt(trim((string) $this->mimetype ));
+        
+        if(array_pop(explode('.', $this->filename)) != $ext){
+            $this->filename = $this->filename .'.'. $ext; 
+        }
+        
+        if (!$this->createFromData($data)) {
+            return false;
+        }
+        
+        return true;
+         
+    }
+    
+    function createFromData($data)
+    {   
+        
+        $this->mimetype= strtolower($this->mimetype);
+        
+        if (array_shift(explode('/', $this->mimetype)) == 'image') { 
+        
+            $imgs = @getimagesize($data);
+            
+            if (!empty($imgs) && !empty($imgs[0]) && !empty($imgs[1])) {
+                list($this->width , $this->height)  = $imgs;
+            }
+        }
+        
+        $this->created = date('Y-m-d H:i:s');
+        
+        if (!$this->id) {
+            $this->insert();
+        } else {
+            $this->update();
+        }
+        
+        $f = $this->getStoreName();
+        $dest = dirname($f);
+        if (!file_exists($dest)) {
+            $oldumask = umask(0);
+            mkdir($dest, 0775, true);
+            umask($oldumask);  
+        }
+        
+        file_put_contents($f, file_get_contents("data://" . $data));
+        
+        $o = clone($this);
+        
+        $this->filesize = filesize($f);
+        
+        $this->update($o);
+        
+        return true;
+        
+    }
+    
  }