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
30 /* the code above is auto generated do not remove the tag below */
33 function checkPerm($perm, $au)
35 // default permissons are to
36 // allow create / edit / if the user has
47 if (method_exists($o, 'hasPerm')) {
48 // edit permissions on related object needed...
49 return $o->hasPerm( $perm == 'S' ? 'S' : 'E' , $au);
53 return true; //// ??? not really that safe...
63 * create an email from file.
64 * these must have been set first.
68 function createFrom($file, $filename=false)
70 // copy the file into the storage area..
71 if (!file_exists($file) || !filesize($file)) {
75 $filename = empty($filename) ? $file : $filename;
77 if (empty($this->mimetype)) {
78 require_once 'File/MimeType.php';
79 $y = new File_MimeType();
80 $this->mimetype = $y->fromFilename($filename);
83 $this->mimetype= strtolower($this->mimetype);
85 if (array_shift(explode('/', $this->mimetype)) == 'image') {
87 $imgs = @getimagesize($file);
89 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
92 list($this->width , $this->height) = $imgs;
96 $this->filesize = filesize($file);
97 $this->created = date('Y-m-d H:i:s');
100 if (empty($this->filename)) {
101 $this->filename = basename($filename);
104 //DB_DataObject::debugLevel(1);
113 $f = $this->getStoreName();
115 if (!file_exists($dest)) {
116 // currently this is 0775 due to problems using shared hosing (FTP)
117 // it makes all the files unaccessable..
118 // you can normally solve this by giving the storedirectory better perms
119 // if needed on a dedicated server..
120 $oldumask = umask(0);
121 mkdir($dest, 0775, true);
132 // $this->createThumbnail(0,50);
138 * Calculate target file name
140 * @return - target file name
142 function getStoreName()
144 $opts = HTML_FlexyFramework::get()->Pman;
145 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
146 return implode( '/', array(
147 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
153 * deletes all the image instances of it...
157 function beforeDelete()
159 $fn = $this->getStoreName();
160 if (file_exists($fn)) {
166 if (file_exists($d)) {
169 while (false !== ($fn = readdir($dh))) {
170 if (substr($fn, 0, strlen($b)) == $b) {
171 unlink($d. '/'. $fn);
178 * check mimetype against type
179 * - eg. img.is(#image#)
184 if (empty($this->mimetype)) {
187 return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
191 * onUpload (singlely attached image to a table)
194 function onUploadWithTbl($tbl, $fld)
196 if ( $tbl->__table == 'Images') {
197 return; // not upload to self...
199 if (empty($_FILES['imageUpload']['tmp_name']) ||
200 empty($_FILES['imageUpload']['name']) ||
201 empty($_FILES['imageUpload']['type'])
206 $image = DB_DataObject::factory('Images');
207 $image->get($tbl->$fld);
208 $image->beforeDelete();
212 $image = DB_DataObject::factory('Images');
213 $image->onid = $tbl->id;
214 $image->ontable = $tbl->__table;
215 $image->filename = $_FILES['imageUpload']['name'];
216 $image->mimetype = $_FILES['imageUpload']['type'];
218 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
222 $tbl->$fld = $image->id;
229 function onUpload($ctrl)
232 if (empty($_FILES['imageUpload']['tmp_name']) ||
233 empty($_FILES['imageUpload']['name']) ||
234 empty($_FILES['imageUpload']['type'])
236 $this->err = "Missing file details";
241 $this->beforeDelete();
243 if ( empty($this->ontable)) {
244 $this->err = "Missing ontable";
248 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
249 // then its an upload
250 $img = DB_DataObject::factory('Images');
251 $img->onid = $this->onid;
252 $img->ontable = $this->ontable;
253 $img->imgtype = $this->imgtype;
256 while ($img->fetch()) {
257 $img->beforeDelete();
265 require_once 'File/MimeType.php';
266 $y = new File_MimeType();
267 $this->mimetype = $_FILES['imageUpload']['type'];
268 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
269 $inf = pathinfo($_FILES['imageUpload']['name']);
270 $this->mimetype = $y->fromExt($inf['extension']);
274 $ext = $y->toExt(trim((string) $this->mimetype ));
276 $this->filename = empty($this->filename) ?
277 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
281 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
291 * return a list of images for an object, optionally with a mime regex.
292 * eg. '%/pdf' or 'image/%'
296 * $i = DB_DataObject::factory('Images');
297 * $i->imgtype = 'LOGO';
298 * $ar = $i->gather($somedataobject, 'image/%');
300 * @param {DB_DataObject} dataobject = the object to gather data on.
301 * @param {String} mimelike LIKE query to use for search
304 function gather($obj, $mime_like='', $opts=array())
306 //DB_DataObject::debugLevel(1);
307 if (empty($obj->id)) {
312 $c->ontable = $obj->tableName();
315 if (!empty($mime_like)) {
316 $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
319 return $c->fetchAll();
324 * set or get the dataobject this image is associated with
325 * @param DB_DataObject $obj An object to associate this image with
326 * (does not store it - you need to call update() to do that)
327 * @return DB_DataObject the dataobject this image is attached to.
329 function object($obj=false)
331 if ($obj === false) {
332 if (empty($this->ontable) || empty($this->onid)) {
335 $ret = DB_DataObject::factory($this->ontable);
336 $ret->get($this->onid);
341 $this->ontable = $obj->tableName();
342 $this->onid = $obj->id; /// assumes our nice standard of using ids..
347 function toRooArray($req = array()) {
348 // echo '<PRE>';print_r($req);exit;
349 $ret= $this->toArray();
353 $ff = HTML_FlexyFramework::get();
356 $ret['public_baseURL'] = isset($ff->Pman_Images['public_baseURL']) ?
357 $ff->Pman_Images['public_baseURL'] : $ff->baseURL;
359 if (!empty($req['query']['imagesize'])) {
360 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
362 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
364 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
366 if (!empty($req['query']['imagesize'])) {
367 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
377 * URL - create a url for the image.
378 * size - use -1 to show full size.
379 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
383 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
386 return 'about:blank';
390 $ff = HTML_FlexyFramework::get();
391 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
392 if (preg_match('#^http[s]*://#', $provider)) {
397 $provider = preg_replace('#/Thumb$#', '', $provider);
399 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
402 //$size = max(100, (int) $size);
403 //$size = min(1024, (int) $size);
406 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
409 * size could be 123x345
413 function toHTML($size, $provider = '/Images/Thumb')
418 $sz = explode('x', $size);
421 if (!$this->id || empty($this->width)) {
423 $this->width = empty($sz[1]) ? $sx : $sz[1];
427 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
434 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
440 * to Fileconvert object..
445 function toFileConvert()
447 require_once 'File/Convert.php';
448 $fc = new File_Convert($this->getStoreName(), $this->mimetype);
455 require_once 'File/MimeType.php';
457 $y = new File_MimeType();
458 return $y->toExt($this->mimetype);
470 function setFromRoo($ar, $roo)
472 // not sure why we do this..
478 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
479 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
482 if ($this->find(true)) {
483 $roo->old = clone($this);
488 if (!empty($ar['_copy_from'])) {
490 if (!$this->checkPerm( 'A' , $roo->authUser)) {
491 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
494 $copy = DB_DataObject::factory('Images');
495 $copy->get($ar['_copy_from']);
496 $this->setFrom($copy->toArray());
498 $this->createFrom($copy->getStoreName());
500 $roo->addEvent("ADD", $this, $this->toEventString());
502 $r = DB_DataObject::factory($this->tableName());
507 $roo->jok($r->toArray());
514 // FIXME - we should be checking perms here...
516 // this should be doign update
519 if (!$this->checkPerm($this->id ? 'A' : 'E', $roo->authUser)) {
520 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
523 if (!isset($_FILES['imageUpload'])) {
524 return; // standard update...
527 if ( !$this->onUpload($this)) {
528 $roo->jerr("File upload failed : ". $this->err);
531 $roo->addEvent("ADD", $this, $this->toEventString());
533 $r = DB_DataObject::factory($this->tableName());
538 $roo->jok($r->toArray());
542 function toEventString()
545 //$p = DB_DataObject::factory($this->ontable);
547 // return "ERROR unknown table? {$this->ontable}";
551 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
552 //$p->toEventString();