DataObjects/Core_event_audit.php
[Pman.Core] / DataObjects / Images.php
index 6621376..2845095 100644 (file)
@@ -15,22 +15,18 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     public $ontable;                         // string(32)  not_null multiple_key
     public $onid;                            // int(11)  not_null
     public $mimetype;                        // string(64)  not_null
-    public $filesize;                        // int(11)  not_null
-    public $created;                         // datetime(19)  not_null binary
-    public $created_by;                         // int(11)  not_null
-
     public $width;                           // int(11)  not_null
     public $height;                          // int(11)  not_null
-    
-
-    public $imgtype;                         // string(32)  not_null
+    public $filesize;                        // int(11)  not_null
+    public $displayorder;                    // int(11)  not_null
+    public $language;                        // string(6)  not_null
     public $parent_image_id;                 // int(11)  not_null
+    public $created;                         // datetime(19)  not_null binary
+    public $imgtype;                         // string(32)  not_null
     public $linkurl;                         // string(254)  not_null
     public $descript;                        // blob(65535)  not_null blob
     public $title;                           // string(128)  not_null
-    public $displayorder;                    // int(11)  not_null
-    public $language;                        // string(6)  not_null
-    
+
     
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
@@ -57,7 +53,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);
             
@@ -113,7 +109,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
      */
     function getStoreName() 
     {
-        $opts = PEAR::getStaticProperty('Pman', 'options');
+        $opts = HTML_FlexyFramework::get()->Pman;
         $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
         return implode( '/', array(
             $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
@@ -147,7 +143,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)
@@ -249,32 +256,52 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     /**
      * return a list of images for an object, optionally with a mime regex.
      * eg. '%/pdf' or 'image/%'
+     *
+     * usage:
+     *
+     * $i = DB_DataObject::factory('Images');
+     * $i->imgtype = 'LOGO';
+     * $ar = $i->gather($somedataobject, 'image/%');
+     * 
+     * @param {DB_DataObject} dataobject  = the object to gather data on.
+     * @param {String} mimelike  LIKE query to use for search
+     
      */
-    function gather($obj, $mime_like='')
+    function gather($obj, $mime_like='', $opts=array())
     {
         //DB_DataObject::debugLevel(1);
         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) ."'");
+            $c->whereAdd("Images.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 +366,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];
         }