DataObjects/Core_locking.php
[Pman.Core] / DataObjects / Images.php
index 00e26f3..bc1dc1d 100644 (file)
@@ -30,7 +30,12 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
-    
+    /**
+     * create an email from file.
+     * these must have been set first.
+     * ontable / onid.
+     * 
+     */
     function createFrom($file)
     {
         // copy the file into the storage area..
@@ -49,6 +54,18 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         
         $this->filesize = filesize($file);
         $this->created = date('Y-m-d H:i:s');
+        
+        if (empty($this->mimetype)) {
+            require_once 'File/MimeType.php';
+            $y = new File_MimeType();
+            $this->mimetype = $y->fromFilename($file);
+        }
+        
+        
+        if (empty($this->filename)) {
+            $this->filename = basename($file);
+        }
+        
         //DB_DataObject::debugLevel(1);
         if (!$this->id) {
             $this->insert();
@@ -106,17 +123,19 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         if (file_exists($fn)) {
             unlink($fn);
         }
-        
+        // delete thumbs..
         $b = basename($fn);
         $d = dirname($fn);
-        $dh = opendir($d);
-        while (false !== ($fn = readdir($dh))) {
-            if (substr($fn, 0, strlen($b)) == $b) {
-                unlink($d. '/'. $fn);
+        if (file_exists($d)) {
+                
+            $dh = opendir($d);
+            while (false !== ($fn = readdir($dh))) {
+                if (substr($fn, 0, strlen($b)) == $b) {
+                    unlink($d. '/'. $fn);
+                }
             }
         }
         
-        
     }
     
   
@@ -223,13 +242,14 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
      */
     function gather($obj, $mime_like='')
     {
+        //DB_DataObject::debugLevel(1);
         if (empty($obj->id)) {
             return array();
         }
         $c = clone($this);
-        $c->on_table = $obj->tableName();
-        $c->on_id = $obj->id;
-        if (!empty($mime_regex)) {
+        $c->ontable = $obj->tableName();
+        $c->onid = $obj->id;
+        if (!empty($mime_like)) {
             $c->whereAdd("mimetype LIKE '". $c->escape($mime_like) ."'");
         }
 
@@ -238,24 +258,88 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     
      
     function toRooArray($req = array()) {
-        if (empty($req['query']['imagesize'])) {
-            return $this->toArray();
-        }
+      //  echo '<PRE>';print_r($req);exit;
         $ret= $this->toArray();
-        $ret['url'] = $this->URL($req['query']['imagesize']);
-        
+      
+      
+        if (!empty($req['query']['imagesize'])) {
+             $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
+            
+            $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
+            
+            $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);    
+            
+            if (!empty($req['query']['imagesize'])) {
+                $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
+            }
+        }
         
+         
+         
         return $ret;
     }
-     
-    function URL($size)
+    
+    /**
+     * URL - create  a url for the image.
+     * size - use -1 to show full size.
+     * provier = baseURL + /Images/Thumb ... use '/Images/' for full
+     * 
+     * 
+     */
+    function URL($size , $provider = '/Images/Thumb', $baseURL=false)
     {
-        $size = max(100, (int) $size);
-        $size = min(1024, (int) $size);
-        
+        if (!$this->id) {
+            return 'about:blank';
+            
+        }
+
         $ff = HTML_FlexyFramework::get();
-        return $ff->baseURL . "/Images/Thumb/$size/{$this->id}/{$this->filename}";
+        $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
+        if ($size < 0) {
+            return $baseURL . $provider . "/{$this->id}/{$this->filename}";
+        }
+        //-- max?
+        //$size = max(100, (int) $size);
+        //$size = min(1024, (int) $size);
+        
+        
+        return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
+    }
+    /**
+     * size could be 123x345
+     * 
+     * 
+     */
+    function toHTML($size, $provider = '/Images/Thumb') 
+    {
+        
+        
+        
+        $sz = explode('x', $size);
+        $sx = $sz[0];
+        //var_dump($sz);
+        if (!$this->id) {
+            $this->height = $sx;
+            $this->width = empty($sz[1]) ? $sx : $sz[1];
+            $sy = $this->width ;
+        }
+        if (empty($sz[1])) {
+            $ratio =  $this->height/ ($this->width *1.0);
+            $sy = $ration * $sx;
+        } else {
+            $sy = $sz[1];
+        }
+        // create it?
+        
+        
+        return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
+        
+        
     }
+    
+    
+    
+    
     function setFromRoo($ar, $roo)
     {
         // not sure why we do this.. 
@@ -270,7 +354,24 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         }   
             
         
-        
+        if (!empty($ar['_copy_from'])) {
+            $copy = DB_DataObject::factory('Images');
+            $copy->get($ar['_copy_from']);
+            $this->setFrom($copy->toArray());
+            $this->setFrom($ar);
+            $this->createFrom($copy->getStoreName());
+            
+            $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->toArray());
+            
+            
+        }