init
[Pman.Core] / DataObjects / Images.php
1 <?php
2 /**
3  * Table Definition for Images
4  */
5 require_once 'DB/DataObject.php';
6
7 class Pman_Core_DataObjects_Images extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'Images';                          // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $filename;                        // string(255)  not_null
15     public $ontable;                         // string(32)  not_null multiple_key
16     public $onid;                            // int(11)  not_null
17     public $mimetype;                        // string(64)  not_null
18     public $width;                           // int(11)  not_null
19     public $height;                          // int(11)  not_null
20     public $filesize;                        // int(11)  not_null
21     public $displayorder;                    // int(11)  not_null
22     public $language;                        // string(6)  not_null
23     public $parent_image_id;                 // int(11)  not_null
24     public $created;                         // datetime(19)  not_null binary
25     public $imgtype;                         // string(32)  not_null
26     public $linkurl;                         // string(254)  not_null
27     public $descript;                        // blob(65535)  not_null blob
28     public $title;                           // string(128)  not_null
29
30     
31     /* the code above is auto generated do not remove the tag below */
32     ###END_AUTOCODE
33     
34     function createFrom($file)
35     {
36         // copy the file into the storage area..
37         if (!file_exists($file) || !filesize($file)) {
38             return false;
39         }
40         
41         
42         $imgs = @getimagesize($file);
43         
44         if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
45             // it's a file!!!!
46         } else {
47             list($this->width , $this->height)  = $imgs;
48         }
49         
50         $this->filesize = filesize($file);
51         $this->created = date('Y-m-d H:i:s');
52         //DB_DataObject::debugLevel(1);
53         if (!$this->id) {
54             $this->insert();
55         } else {
56             $this->update();
57         }
58         
59         
60         
61         $f = $this->getStoreName();
62         $dest = dirname($f);
63         if (!file_exists($dest)) {
64             
65             $oldumask = umask(0);
66             mkdir($dest, 0770, true);
67             umask($oldumask);  
68         }
69         
70         copy($file,$f);
71         
72         // fill in details..
73         
74         /* thumbnails */
75         
76      
77        // $this->createThumbnail(0,50);
78         return true;
79         
80     }
81
82     /**
83      * Calculate target file name
84      *
85      * @return - target file name
86      */
87     function getStoreName() 
88     {
89         $opts = PEAR::getStaticProperty('Pman', 'options');
90         $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
91         return implode( '/', array(
92             $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
93         ));
94           
95     }
96
97      
98     /**
99      * deletes all the image instances of it...
100      * 
101      * 
102      */
103     function beforeDelete()
104     {
105         $fn = $this->getStoreName();
106         if (file_exists($fn)) {
107             unlink($fn);
108         }
109         
110         $b = basename($fn);
111         $d = dirname($fn);
112         $dh = opendir($d);
113         while (false !== ($fn = readdir($dh))) {
114             if (substr($fn, 0, strlen($b)) == $b) {
115                 unlink($d. '/'. $fn);
116             }
117         }
118         
119         
120     }
121     
122   
123     /**
124      * onUpload (singlely attached image to a table)
125      */
126     
127     function onUploadWithTbl($tbl,  $fld)
128     {
129         if ( $tbl->__table == 'Images') {
130             return; // not upload to self...
131         }
132         if (empty($_FILES['imageUpload']['tmp_name']) || 
133             empty($_FILES['imageUpload']['name']) || 
134             empty($_FILES['imageUpload']['type'])
135         ) {
136             return false;
137         }
138         if ($tbl->$fld) {
139             $image = DB_DataObject::factory('Images');
140             $image->get($tbl->$fld);
141             $image->beforeDelete();
142             $image->delete();
143         }
144         
145         $image = DB_DataObject::factory('Images');
146         $image->onid = $tbl->id;
147         $image->ontable = $tbl->__table;
148         $image->filename = $_FILES['imageUpload']['name']; 
149         $image->mimetype = $_FILES['imageUpload']['type'];
150        
151         if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
152             return false;
153         }
154         $old = clone($tbl);
155         $tbl->$fld = $image->id;
156         $tbl->update($old);
157          
158     }
159     
160     // direct via roo...
161     function onUpload($ctrl)
162     {
163         
164         if (empty($_FILES['imageUpload']['tmp_name']) || 
165             empty($_FILES['imageUpload']['name']) || 
166             empty($_FILES['imageUpload']['type'])
167         ) {
168             $this->err = "Missing file details";
169             return false;
170         }
171         
172         if ($this->id) {
173             $this->beforeDelete();
174         }
175         if ( empty($this->ontable)) {
176             $this->err = "Missing  ontable";
177             return false;
178         }
179         
180         if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
181             // then its an upload 
182             $img  = DB_DataObject::factory('Images');
183             $img->onid = $this->onid;
184             $img->ontable = $this->ontable;
185             $img->imgtype = $this->imgtype;
186             
187             $img->find();
188             while ($img->fetch()) {
189                 $img->beforeDelete();
190                 $img->delete();
191             }
192             
193         }
194         
195         
196         
197         require_once 'File/MimeType.php';
198         $y = new File_MimeType();
199         $this->mimetype = $_FILES['imageUpload']['type'];
200         if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
201             $inf = pathinfo($_FILES['imageUpload']['name']);
202             $this->mimetype  = $y->fromExt($inf['extension']);
203         }
204         
205         
206         $ext = $y->toExt(trim((string) $this->mimetype ));
207         
208         $this->filename = empty($this->filename) ? 
209             $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext); 
210         
211         
212         
213         if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
214             return false;
215         }
216         return true;
217          
218     }
219      
220
221     function setFromRoo($ar, $roo)
222     {
223         // not sure why we do this.. 
224         
225         // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
226         if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
227             $this->setFrom($ar);
228             $this->limit(1);
229             if ($this->find(true)) {
230                 $roo->old = clone($this);
231             }
232         }   
233             
234         
235         
236         
237          
238         
239         // FIXME - we should be checking perms here...
240         //if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser))  {
241         //    $this->jerr("PERMISSION DENIED");
242         // }
243         // this should be doign update
244         $this->setFrom($ar);
245         
246         if (!isset($_FILES['imageUpload'])) {
247             return; // standard update...
248         }
249         
250         if ( !$this->onUpload($this)) {
251             $this->jerr("File upload failed");
252         }
253         $roo->addEvent("ADD", $this, $this->toEventString());
254         
255         $r = DB_DataObject::factory($this->tableName());
256         $r->id = $this->id;
257         $roo->loadMap($r);
258         $r->limit(1);
259         $r->find(true);
260         $roo->jok($r->toArray());
261          
262     }
263     function toEventString()
264     {
265         
266         //$p = DB_DataObject::factory($this->ontable);
267         //if (!is_$p) {
268         //    return "ERROR unknown table? {$this->ontable}";
269        // }
270         //$p->get($p->onid);
271         
272         return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
273         //$p->toEventString();
274     }
275  }