DataObjects/ProjectDirectory.php
[Pman.Core] / DataObjects / Images.php
index 8628110..37e0579 100644 (file)
@@ -57,7 +57,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         
         $this->mimetype= strtolower($this->mimetype);
         
-        if (array_shift(explode($this->mimetype)) == 'image') { 
+        if (array_shift(explode('/', $this->mimetype)) == 'image') { 
         
             $imgs = @getimagesize($file);
             
@@ -147,7 +147,18 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         }
         
     }
-    
+    /**
+     * check mimetype against type
+     * - eg. img.is(#image#)
+     *
+     */
+    function is($type)
+    {
+        if (empty($this->mimetype)) {
+            return false;
+        }
+        return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
+    }
   
     /**
      * onUpload (singlely attached image to a table)
@@ -256,25 +267,35 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         if (empty($obj->id)) {
             return array();
         }
+        
         $c = clone($this);
         $c->ontable = $obj->tableName();
         $c->onid = $obj->id;
+        $c->autoJoin();
         if (!empty($mime_like)) {
             $c->whereAdd("mimetype LIKE '". $c->escape($mime_like) ."'");
         }
 
         return $c->fetchAll();
     }
+     
+    
     /**
-     * creation - associate this image with a dataobject
-     * - currently assumes id is the key column
-     * @param DB_DataObject $obj a dataobject
-     */
-    function associate($obj)
+    * set or get the dataobject this image is associated with
+    * @param DB_DataObject $obj An object to associate this image with
+    *        (does not store it - you need to call update() to do that)
+    * @return DB_DataObject the dataobject this image is attached to.
+    */
+    function object($obj=false)
     {
+        if ($obj === false) {
+            $ret = DB_DataObject::factory($this->ontable);
+            $ret->get($this->onid);
+            return $ret;
+        }
         $this->ontable = $obj->tableName();
         $this->onid = $obj->id; /// assumes our nice standard of using ids..
-        
+        return $obj;
     }
     
      
@@ -339,14 +360,14 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
         $sz = explode('x', $size);
         $sx = $sz[0];
         //var_dump($sz);
-        if (!$this->id) {
+        if (!$this->id || empty($this->width)) {
             $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;
+            $ratio =  empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
+            $sy = $ratio * $sx;
         } else {
             $sy = $sz[1];
         }