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)
36 // default permissons are to
37 // allow create / edit / if the user has
49 if (method_exists($o, 'hasPerm')) {
50 // edit permissions on related object needed...
51 return $o->hasPerm( $perm == 'S' ? 'S' : 'E' , $au);
55 return true; //// ??? not really that safe...
65 * create an email from file.
66 * these must have been set first.
70 function createFrom($file, $filename=false)
72 // copy the file into the storage area..
73 if (!file_exists($file) || !filesize($file)) {
77 $filename = empty($filename) ? $file : $filename;
79 if (empty($this->mimetype)) {
80 require_once 'File/MimeType.php';
81 $y = new File_MimeType();
82 $this->mimetype = $y->fromFilename($filename);
85 $this->mimetype= strtolower($this->mimetype);
87 if (array_shift(explode('/', $this->mimetype)) == 'image') {
89 $imgs = @getimagesize($file);
91 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
94 list($this->width , $this->height) = $imgs;
98 $this->filesize = filesize($file);
99 $this->created = date('Y-m-d H:i:s');
102 if (empty($this->filename)) {
103 $this->filename = basename($filename);
106 //DB_DataObject::debugLevel(1);
115 $f = $this->getStoreName();
117 if (!file_exists($dest)) {
119 $oldumask = umask(0);
120 mkdir($dest, 0770, true);
131 // $this->createThumbnail(0,50);
137 * Calculate target file name
139 * @return - target file name
141 function getStoreName()
143 $opts = HTML_FlexyFramework::get()->Pman;
144 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
145 return implode( '/', array(
146 $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;
228 function onUpload($ctrl)
231 if (empty($_FILES['imageUpload']['tmp_name']) ||
232 empty($_FILES['imageUpload']['name']) ||
233 empty($_FILES['imageUpload']['type'])
235 $this->err = "Missing file details";
240 $this->beforeDelete();
242 if ( empty($this->ontable)) {
243 $this->err = "Missing ontable";
247 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
248 // then its an upload
249 $img = DB_DataObject::factory('Images');
250 $img->onid = $this->onid;
251 $img->ontable = $this->ontable;
252 $img->imgtype = $this->imgtype;
255 while ($img->fetch()) {
256 $img->beforeDelete();
264 require_once 'File/MimeType.php';
265 $y = new File_MimeType();
266 $this->mimetype = $_FILES['imageUpload']['type'];
267 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
268 $inf = pathinfo($_FILES['imageUpload']['name']);
269 $this->mimetype = $y->fromExt($inf['extension']);
273 $ext = $y->toExt(trim((string) $this->mimetype ));
275 $this->filename = empty($this->filename) ?
276 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
280 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
288 * return a list of images for an object, optionally with a mime regex.
289 * eg. '%/pdf' or 'image/%'
293 * $i = DB_DataObject::factory('Images');
294 * $i->imgtype = 'LOGO';
295 * $ar = $i->gather($somedataobject, 'image/%');
297 * @param {DB_DataObject} dataobject = the object to gather data on.
298 * @param {String} mimelike LIKE query to use for search
301 function gather($obj, $mime_like='', $opts=array())
303 //DB_DataObject::debugLevel(1);
304 if (empty($obj->id)) {
309 $c->ontable = $obj->tableName();
312 if (!empty($mime_like)) {
313 $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
316 return $c->fetchAll();
321 * set or get the dataobject this image is associated with
322 * @param DB_DataObject $obj An object to associate this image with
323 * (does not store it - you need to call update() to do that)
324 * @return DB_DataObject the dataobject this image is attached to.
326 function object($obj=false)
328 if ($obj === false) {
329 if (empty($this->ontable) || empty($this->onid)) {
332 $ret = DB_DataObject::factory($this->ontable);
333 $ret->get($this->onid);
338 $this->ontable = $obj->tableName();
339 $this->onid = $obj->id; /// assumes our nice standard of using ids..
344 function toRooArray($req = array()) {
345 // echo '<PRE>';print_r($req);exit;
346 $ret= $this->toArray();
349 if (!empty($req['query']['imagesize'])) {
350 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
352 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
354 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
356 if (!empty($req['query']['imagesize'])) {
357 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
367 * URL - create a url for the image.
368 * size - use -1 to show full size.
369 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
373 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
376 return 'about:blank';
380 $ff = HTML_FlexyFramework::get();
381 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
382 if (preg_match('#^http[s]*://#', $provider)) {
387 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
390 //$size = max(100, (int) $size);
391 //$size = min(1024, (int) $size);
394 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
397 * size could be 123x345
401 function toHTML($size, $provider = '/Images/Thumb')
406 $sz = explode('x', $size);
409 if (!$this->id || empty($this->width)) {
411 $this->width = empty($sz[1]) ? $sx : $sz[1];
415 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
423 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
435 function setFromRoo($ar, $roo)
437 // not sure why we do this..
443 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
444 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
447 if ($this->find(true)) {
448 $roo->old = clone($this);
453 if (!empty($ar['_copy_from'])) {
455 if (!$this->checkPerm( 'A' , $roo->authUser)) {
456 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
459 $copy = DB_DataObject::factory('Images');
460 $copy->get($ar['_copy_from']);
461 $this->setFrom($copy->toArray());
463 $this->createFrom($copy->getStoreName());
465 $roo->addEvent("ADD", $this, $this->toEventString());
467 $r = DB_DataObject::factory($this->tableName());
472 $roo->jok($r->toArray());
479 // FIXME - we should be checking perms here...
481 // this should be doign update
484 if (!$this->checkPerm($this->id ? 'A' : 'E', $roo->authUser)) {
485 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
488 if (!isset($_FILES['imageUpload'])) {
489 return; // standard update...
492 if ( !$this->onUpload($this)) {
493 $roo->jerr("File upload failed : ". $this->err);
496 $roo->addEvent("ADD", $this, $this->toEventString());
498 $r = DB_DataObject::factory($this->tableName());
503 $roo->jok($r->toArray());
507 function toEventString()
510 //$p = DB_DataObject::factory($this->ontable);
512 // return "ERROR unknown table? {$this->ontable}";
516 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
517 //$p->toEventString();