3 * Table Definition for Images
5 require_once 'DB/DataObject.php';
7 class Pman_Core_DataObjects_Images extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
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
31 /* the code above is auto generated do not remove the tag below */
34 function checkPerm($perm, $au)
37 if (method_exists($o, 'hasPerm')) {
38 // edit permissions on related object needed...
39 return $o->hasPerm('E', $au);
43 return true; //// ??? not really that safe...
53 * create an email from file.
54 * these must have been set first.
58 function createFrom($file, $filename=false)
60 // copy the file into the storage area..
61 if (!file_exists($file) || !filesize($file)) {
65 $filename = empty($filename) ? $file : $filename;
67 if (empty($this->mimetype)) {
68 require_once 'File/MimeType.php';
69 $y = new File_MimeType();
70 $this->mimetype = $y->fromFilename($filename);
73 $this->mimetype= strtolower($this->mimetype);
75 if (array_shift(explode('/', $this->mimetype)) == 'image') {
77 $imgs = @getimagesize($file);
79 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
82 list($this->width , $this->height) = $imgs;
86 $this->filesize = filesize($file);
87 $this->created = date('Y-m-d H:i:s');
90 if (empty($this->filename)) {
91 $this->filename = basename($filename);
94 //DB_DataObject::debugLevel(1);
103 $f = $this->getStoreName();
105 if (!file_exists($dest)) {
107 $oldumask = umask(0);
108 mkdir($dest, 0770, true);
119 // $this->createThumbnail(0,50);
125 * Calculate target file name
127 * @return - target file name
129 function getStoreName()
131 $opts = HTML_FlexyFramework::get()->Pman;
132 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
133 return implode( '/', array(
134 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
141 * deletes all the image instances of it...
145 function beforeDelete()
147 $fn = $this->getStoreName();
148 if (file_exists($fn)) {
154 if (file_exists($d)) {
157 while (false !== ($fn = readdir($dh))) {
158 if (substr($fn, 0, strlen($b)) == $b) {
159 unlink($d. '/'. $fn);
166 * check mimetype against type
167 * - eg. img.is(#image#)
172 if (empty($this->mimetype)) {
175 return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
179 * onUpload (singlely attached image to a table)
182 function onUploadWithTbl($tbl, $fld)
184 if ( $tbl->__table == 'Images') {
185 return; // not upload to self...
187 if (empty($_FILES['imageUpload']['tmp_name']) ||
188 empty($_FILES['imageUpload']['name']) ||
189 empty($_FILES['imageUpload']['type'])
194 $image = DB_DataObject::factory('Images');
195 $image->get($tbl->$fld);
196 $image->beforeDelete();
200 $image = DB_DataObject::factory('Images');
201 $image->onid = $tbl->id;
202 $image->ontable = $tbl->__table;
203 $image->filename = $_FILES['imageUpload']['name'];
204 $image->mimetype = $_FILES['imageUpload']['type'];
206 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
210 $tbl->$fld = $image->id;
216 function onUpload($ctrl)
219 if (empty($_FILES['imageUpload']['tmp_name']) ||
220 empty($_FILES['imageUpload']['name']) ||
221 empty($_FILES['imageUpload']['type'])
223 $this->err = "Missing file details";
228 $this->beforeDelete();
230 if ( empty($this->ontable)) {
231 $this->err = "Missing ontable";
235 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
236 // then its an upload
237 $img = DB_DataObject::factory('Images');
238 $img->onid = $this->onid;
239 $img->ontable = $this->ontable;
240 $img->imgtype = $this->imgtype;
243 while ($img->fetch()) {
244 $img->beforeDelete();
252 require_once 'File/MimeType.php';
253 $y = new File_MimeType();
254 $this->mimetype = $_FILES['imageUpload']['type'];
255 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
256 $inf = pathinfo($_FILES['imageUpload']['name']);
257 $this->mimetype = $y->fromExt($inf['extension']);
261 $ext = $y->toExt(trim((string) $this->mimetype ));
263 $this->filename = empty($this->filename) ?
264 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
268 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
276 * return a list of images for an object, optionally with a mime regex.
277 * eg. '%/pdf' or 'image/%'
281 * $i = DB_DataObject::factory('Images');
282 * $i->imgtype = 'LOGO';
283 * $ar = $i->gather($somedataobject, 'image/%');
285 * @param {DB_DataObject} dataobject = the object to gather data on.
286 * @param {String} mimelike LIKE query to use for search
289 function gather($obj, $mime_like='', $opts=array())
291 //DB_DataObject::debugLevel(1);
292 if (empty($obj->id)) {
297 $c->ontable = $obj->tableName();
300 if (!empty($mime_like)) {
301 $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
304 return $c->fetchAll();
309 * set or get the dataobject this image is associated with
310 * @param DB_DataObject $obj An object to associate this image with
311 * (does not store it - you need to call update() to do that)
312 * @return DB_DataObject the dataobject this image is attached to.
314 function object($obj=false)
316 if ($obj === false) {
317 $ret = DB_DataObject::factory($this->ontable);
318 $ret->get($this->onid);
321 $this->ontable = $obj->tableName();
322 $this->onid = $obj->id; /// assumes our nice standard of using ids..
327 function toRooArray($req = array()) {
328 // echo '<PRE>';print_r($req);exit;
329 $ret= $this->toArray();
332 if (!empty($req['query']['imagesize'])) {
333 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
335 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
337 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
339 if (!empty($req['query']['imagesize'])) {
340 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
350 * URL - create a url for the image.
351 * size - use -1 to show full size.
352 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
356 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
359 return 'about:blank';
363 $ff = HTML_FlexyFramework::get();
364 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
366 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
369 //$size = max(100, (int) $size);
370 //$size = min(1024, (int) $size);
373 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
376 * size could be 123x345
380 function toHTML($size, $provider = '/Images/Thumb')
385 $sz = explode('x', $size);
388 if (!$this->id || empty($this->width)) {
390 $this->width = empty($sz[1]) ? $sx : $sz[1];
394 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
402 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
414 function setFromRoo($ar, $roo)
416 // not sure why we do this..
422 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
423 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
426 if ($this->find(true)) {
427 $roo->old = clone($this);
432 if (!empty($ar['_copy_from'])) {
433 $copy = DB_DataObject::factory('Images');
434 $copy->get($ar['_copy_from']);
435 $this->setFrom($copy->toArray());
437 $this->createFrom($copy->getStoreName());
439 $roo->addEvent("ADD", $this, $this->toEventString());
441 $r = DB_DataObject::factory($this->tableName());
446 $roo->jok($r->toArray());
453 // FIXME - we should be checking perms here...
455 // this should be doign update
458 if (method_exists(this, 'checkPerm') &&
459 !$this->checkPerm('E', $this->authUser)) {
460 $this->jerr("IMAGE UPLOAD PERMISSION DENIED");
463 if (!isset($_FILES['imageUpload'])) {
464 return; // standard update...
467 if ( !$this->onUpload($this)) {
468 $roo->jerr("File upload failed : ". $this->err);
470 $roo->addEvent("ADD", $this, $this->toEventString());
472 $r = DB_DataObject::factory($this->tableName());
477 $roo->jok($r->toArray());
481 function toEventString()
484 //$p = DB_DataObject::factory($this->ontable);
486 // return "ERROR unknown table? {$this->ontable}";
490 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
491 //$p->toEventString();