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...
57 function beforeInsert($q, $roo)
59 if (isset($q['_remote_upload'])) {
60 require_once 'System.php';
61 $tmpdir = System::mktemp("-d remote_upload");
63 $path = $tmpdir . '/' . basename($q['_remote_upload']);
64 if(!file_exists($path)){
65 file_put_contents($path, file_get_contents($q['_remote_upload']));
68 $imageInfo = getimagesize($path);
69 require_once 'File/MimeType.php';
70 $y = new File_MimeType();
71 $ext = $y->toExt(trim((string) $imageInfo['mime'] ));
73 if (!preg_match("/\." . $ext."$/", $path, $matches)) {
74 rename($path,$path.$ext);
77 $this->createFrom($path);
79 $roo->addEvent("ADD", $this, $this->toEventString());
81 $r = DB_DataObject::factory($this->tableName());
86 $roo->jok($r->URL(-1,'/Images') . '#attachment-'. $r->id);
93 * create an email from file.
94 * these must have been set first.
98 function createFrom($file, $filename=false)
100 // copy the file into the storage area..
101 if (!file_exists($file) || !filesize($file)) {
105 $filename = empty($filename) ? $file : $filename;
107 if (empty($this->mimetype)) {
108 require_once 'File/MimeType.php';
109 $y = new File_MimeType();
110 $this->mimetype = $y->fromFilename($filename);
113 $this->mimetype= strtolower($this->mimetype);
115 if (array_shift(explode('/', $this->mimetype)) == 'image') {
117 $imgs = @getimagesize($file);
119 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
122 list($this->width , $this->height) = $imgs;
126 $this->filesize = filesize($file);
127 $this->created = date('Y-m-d H:i:s');
130 if (empty($this->filename)) {
131 $this->filename = basename($filename);
134 //DB_DataObject::debugLevel(1);
143 $f = $this->getStoreName();
145 if (!file_exists($dest)) {
146 // currently this is 0775 due to problems using shared hosing (FTP)
147 // it makes all the files unaccessable..
148 // you can normally solve this by giving the storedirectory better perms
149 // if needed on a dedicated server..
150 $oldumask = umask(0);
151 mkdir($dest, 0775, true);
162 // $this->createThumbnail(0,50);
168 * Calculate target file name
170 * @return - target file name
172 function getStoreName()
174 $opts = HTML_FlexyFramework::get()->Pman;
175 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
176 return implode( '/', array(
177 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
183 * deletes all the image instances of it...
187 function beforeDelete()
189 $fn = $this->getStoreName();
190 if (file_exists($fn)) {
196 if (file_exists($d)) {
199 while (false !== ($fn = readdir($dh))) {
200 if (substr($fn, 0, strlen($b)) == $b) {
201 unlink($d. '/'. $fn);
208 * check mimetype against type
209 * - eg. img.is(#image#)
214 if (empty($this->mimetype)) {
217 return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
221 * onUpload (singlely attached image to a table)
224 function onUploadWithTbl($tbl, $fld)
226 if ( $tbl->__table == 'Images') {
227 return; // not upload to self...
229 if (empty($_FILES['imageUpload']['tmp_name']) ||
230 empty($_FILES['imageUpload']['name']) ||
231 empty($_FILES['imageUpload']['type'])
236 $image = DB_DataObject::factory('Images');
237 $image->get($tbl->$fld);
238 $image->beforeDelete();
242 $image = DB_DataObject::factory('Images');
243 $image->onid = $tbl->id;
244 $image->ontable = $tbl->__table;
245 $image->filename = $_FILES['imageUpload']['name'];
246 $image->mimetype = $_FILES['imageUpload']['type'];
248 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
252 $tbl->$fld = $image->id;
259 function onUpload($ctrl)
262 if (empty($_FILES['imageUpload']['tmp_name']) ||
263 empty($_FILES['imageUpload']['name']) ||
264 empty($_FILES['imageUpload']['type'])
266 $this->err = "Missing file details";
271 $this->beforeDelete();
273 if ( empty($this->ontable)) {
274 $this->err = "Missing ontable";
278 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
279 // then its an upload
280 $img = DB_DataObject::factory('Images');
281 $img->onid = $this->onid;
282 $img->ontable = $this->ontable;
283 $img->imgtype = $this->imgtype;
286 while ($img->fetch()) {
287 $img->beforeDelete();
295 require_once 'File/MimeType.php';
296 $y = new File_MimeType();
297 $this->mimetype = $_FILES['imageUpload']['type'];
298 if (in_array($this->mimetype, array(
300 'application/octet-stream',
301 'application/vnd.ms-excel', /// sometimes windows reports csv as excel???
302 ))) { // weird tyeps..
303 $inf = pathinfo($_FILES['imageUpload']['name']);
304 $this->mimetype = $y->fromExt($inf['extension']);
308 $ext = $y->toExt(trim((string) $this->mimetype ));
310 $this->filename = empty($this->filename) ?
311 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
315 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
325 * return a list of images for an object, optionally with a mime regex.
326 * eg. '%/pdf' or 'image/%'
330 * $i = DB_DataObject::factory('Images');
331 * $i->imgtype = 'LOGO';
332 * $ar = $i->gather($somedataobject, 'image/%');
334 * @param {DB_DataObject} dataobject = the object to gather data on.
335 * @param {String} mimelike LIKE query to use for search
338 function gather($obj, $mime_like='', $opts=array())
340 //DB_DataObject::debugLevel(1);
341 if (empty($obj->id)) {
346 $c->ontable = $obj->tableName();
349 if (!empty($mime_like)) {
350 $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
353 return $c->fetchAll();
358 * set or get the dataobject this image is associated with
359 * @param DB_DataObject $obj An object to associate this image with
360 * (does not store it - you need to call update() to do that)
361 * @return DB_DataObject the dataobject this image is attached to.
363 function object($obj=false)
365 if ($obj === false) {
366 if (empty($this->ontable) || empty($this->onid)) {
369 $ret = DB_DataObject::factory($this->ontable);
370 $ret->get($this->onid);
375 $this->ontable = $obj->tableName();
376 $this->onid = $obj->id; /// assumes our nice standard of using ids..
381 function toRooArray($req = array()) {
382 // echo '<PRE>';print_r($req);exit;
383 $ret= $this->toArray();
387 $ff = HTML_FlexyFramework::get();
390 $ret['public_baseURL'] = isset($ff->Pman_Images['public_baseURL']) ?
391 $ff->Pman_Images['public_baseURL'] : $ff->baseURL;
393 if (!empty($req['query']['imagesize'])) {
394 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
396 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
398 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
400 if (!empty($req['query']['imagesize'])) {
401 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
411 * URL - create a url for the image.
412 * size - use -1 to show full size.
413 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
417 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
420 return 'about:blank';
424 $ff = HTML_FlexyFramework::get();
425 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
426 if (preg_match('#^http[s]*://#', $provider)) {
431 $provider = preg_replace('#/Thumb$#', '', $provider);
433 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
436 //$size = max(100, (int) $size);
437 //$size = min(1024, (int) $size);
440 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
443 * size could be 123x345
447 function toHTML($size, $provider = '/Images/Thumb')
452 $sz = explode('x', $size);
455 if (!$this->id || empty($this->width)) {
457 $this->width = empty($sz[1]) ? $sx : $sz[1];
461 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
468 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
474 * to Fileconvert object..
479 function toFileConvert()
481 require_once 'File/Convert.php';
482 $fc = new File_Convert($this->getStoreName(), $this->mimetype);
489 require_once 'File/MimeType.php';
491 $y = new File_MimeType();
492 return $y->toExt($this->mimetype);
504 function setFromRoo($ar, $roo)
506 // not sure why we do this..
512 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
513 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
516 if ($this->find(true)) {
517 $roo->old = clone($this);
522 if (!empty($ar['_copy_from'])) {
524 if (!$this->checkPerm( 'A' , $roo->authUser)) {
525 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
528 $copy = DB_DataObject::factory('Images');
529 $copy->get($ar['_copy_from']);
530 $this->setFrom($copy->toArray());
532 $this->createFrom($copy->getStoreName());
534 $roo->addEvent("ADD", $this, $this->toEventString());
536 $r = DB_DataObject::factory($this->tableName());
541 $roo->jok($r->toArray());
548 // FIXME - we should be checking perms here...
550 // this should be doign update
553 if (!$this->checkPerm($this->id ? 'A' : 'E', $roo->authUser)) {
554 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
559 if (!isset($_FILES['imageUpload'])) {
560 return; // standard update...
563 if ( !$this->onUpload($this)) {
564 $roo->jerr("File upload failed : ". $this->err);
567 $roo->addEvent("ADD", $this, $this->toEventString());
569 $r = DB_DataObject::factory($this->tableName());
574 $roo->jok($r->toArray());
578 function toEventString()
581 //$p = DB_DataObject::factory($this->ontable);
583 // return "ERROR unknown table? {$this->ontable}";
587 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
588 //$p->toEventString();