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");
62 // $imageInfo = getimagesize($q['_remote_upload']);
63 // require_once 'File/MimeType.php';
64 // $y = new File_MimeType();
65 // $ext = $y->toExt(trim((string) $imageInfo['mime'] ));
66 print_r(parse_url($q['_remote_upload']));
67 print_r(basename($q['_remote_upload']));
69 $path = $tmpdir . '/' . time() . '.' . $ext;
70 if(!file_exists($path)){
71 file_put_contents($path, file_get_contents($q['_remote_upload']));
73 $this->createFrom($path);
75 $roo->addEvent("ADD", $this, $this->toEventString());
77 $r = DB_DataObject::factory($this->tableName());
82 $roo->jok($r->URL(-1,'/Images') . '#attachment-'. $r->id);
89 * create an email from file.
90 * these must have been set first.
94 function createFrom($file, $filename=false)
96 // copy the file into the storage area..
97 if (!file_exists($file) || !filesize($file)) {
101 $filename = empty($filename) ? $file : $filename;
103 if (empty($this->mimetype)) {
104 require_once 'File/MimeType.php';
105 $y = new File_MimeType();
106 $this->mimetype = $y->fromFilename($filename);
109 $this->mimetype= strtolower($this->mimetype);
111 if (array_shift(explode('/', $this->mimetype)) == 'image') {
113 $imgs = @getimagesize($file);
115 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
118 list($this->width , $this->height) = $imgs;
122 $this->filesize = filesize($file);
123 $this->created = date('Y-m-d H:i:s');
126 if (empty($this->filename)) {
127 $this->filename = basename($filename);
130 //DB_DataObject::debugLevel(1);
139 $f = $this->getStoreName();
141 if (!file_exists($dest)) {
142 // currently this is 0775 due to problems using shared hosing (FTP)
143 // it makes all the files unaccessable..
144 // you can normally solve this by giving the storedirectory better perms
145 // if needed on a dedicated server..
146 $oldumask = umask(0);
147 mkdir($dest, 0775, true);
158 // $this->createThumbnail(0,50);
164 * Calculate target file name
166 * @return - target file name
168 function getStoreName()
170 $opts = HTML_FlexyFramework::get()->Pman;
171 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
172 return implode( '/', array(
173 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
179 * deletes all the image instances of it...
183 function beforeDelete()
185 $fn = $this->getStoreName();
186 if (file_exists($fn)) {
192 if (file_exists($d)) {
195 while (false !== ($fn = readdir($dh))) {
196 if (substr($fn, 0, strlen($b)) == $b) {
197 unlink($d. '/'. $fn);
204 * check mimetype against type
205 * - eg. img.is(#image#)
210 if (empty($this->mimetype)) {
213 return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
217 * onUpload (singlely attached image to a table)
220 function onUploadWithTbl($tbl, $fld)
222 if ( $tbl->__table == 'Images') {
223 return; // not upload to self...
225 if (empty($_FILES['imageUpload']['tmp_name']) ||
226 empty($_FILES['imageUpload']['name']) ||
227 empty($_FILES['imageUpload']['type'])
232 $image = DB_DataObject::factory('Images');
233 $image->get($tbl->$fld);
234 $image->beforeDelete();
238 $image = DB_DataObject::factory('Images');
239 $image->onid = $tbl->id;
240 $image->ontable = $tbl->__table;
241 $image->filename = $_FILES['imageUpload']['name'];
242 $image->mimetype = $_FILES['imageUpload']['type'];
244 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
248 $tbl->$fld = $image->id;
255 function onUpload($ctrl)
258 if (empty($_FILES['imageUpload']['tmp_name']) ||
259 empty($_FILES['imageUpload']['name']) ||
260 empty($_FILES['imageUpload']['type'])
262 $this->err = "Missing file details";
267 $this->beforeDelete();
269 if ( empty($this->ontable)) {
270 $this->err = "Missing ontable";
274 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
275 // then its an upload
276 $img = DB_DataObject::factory('Images');
277 $img->onid = $this->onid;
278 $img->ontable = $this->ontable;
279 $img->imgtype = $this->imgtype;
282 while ($img->fetch()) {
283 $img->beforeDelete();
291 require_once 'File/MimeType.php';
292 $y = new File_MimeType();
293 $this->mimetype = $_FILES['imageUpload']['type'];
294 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
295 $inf = pathinfo($_FILES['imageUpload']['name']);
296 $this->mimetype = $y->fromExt($inf['extension']);
300 $ext = $y->toExt(trim((string) $this->mimetype ));
302 $this->filename = empty($this->filename) ?
303 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
307 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
317 * return a list of images for an object, optionally with a mime regex.
318 * eg. '%/pdf' or 'image/%'
322 * $i = DB_DataObject::factory('Images');
323 * $i->imgtype = 'LOGO';
324 * $ar = $i->gather($somedataobject, 'image/%');
326 * @param {DB_DataObject} dataobject = the object to gather data on.
327 * @param {String} mimelike LIKE query to use for search
330 function gather($obj, $mime_like='', $opts=array())
332 //DB_DataObject::debugLevel(1);
333 if (empty($obj->id)) {
338 $c->ontable = $obj->tableName();
341 if (!empty($mime_like)) {
342 $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
345 return $c->fetchAll();
350 * set or get the dataobject this image is associated with
351 * @param DB_DataObject $obj An object to associate this image with
352 * (does not store it - you need to call update() to do that)
353 * @return DB_DataObject the dataobject this image is attached to.
355 function object($obj=false)
357 if ($obj === false) {
358 if (empty($this->ontable) || empty($this->onid)) {
361 $ret = DB_DataObject::factory($this->ontable);
362 $ret->get($this->onid);
367 $this->ontable = $obj->tableName();
368 $this->onid = $obj->id; /// assumes our nice standard of using ids..
373 function toRooArray($req = array()) {
374 // echo '<PRE>';print_r($req);exit;
375 $ret= $this->toArray();
379 $ff = HTML_FlexyFramework::get();
382 $ret['public_baseURL'] = isset($ff->Pman_Images['public_baseURL']) ?
383 $ff->Pman_Images['public_baseURL'] : $ff->baseURL;
385 if (!empty($req['query']['imagesize'])) {
386 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
388 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
390 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
392 if (!empty($req['query']['imagesize'])) {
393 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
403 * URL - create a url for the image.
404 * size - use -1 to show full size.
405 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
409 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
412 return 'about:blank';
416 $ff = HTML_FlexyFramework::get();
417 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
418 if (preg_match('#^http[s]*://#', $provider)) {
423 $provider = preg_replace('#/Thumb$#', '', $provider);
425 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
428 //$size = max(100, (int) $size);
429 //$size = min(1024, (int) $size);
432 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
435 * size could be 123x345
439 function toHTML($size, $provider = '/Images/Thumb')
444 $sz = explode('x', $size);
447 if (!$this->id || empty($this->width)) {
449 $this->width = empty($sz[1]) ? $sx : $sz[1];
453 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
460 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
466 * to Fileconvert object..
471 function toFileConvert()
473 require_once 'File/Convert.php';
474 $fc = new File_Convert($this->getStoreName(), $this->mimetype);
481 require_once 'File/MimeType.php';
483 $y = new File_MimeType();
484 return $y->toExt($this->mimetype);
496 function setFromRoo($ar, $roo)
498 // not sure why we do this..
504 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
505 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
508 if ($this->find(true)) {
509 $roo->old = clone($this);
514 if (!empty($ar['_copy_from'])) {
516 if (!$this->checkPerm( 'A' , $roo->authUser)) {
517 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
520 $copy = DB_DataObject::factory('Images');
521 $copy->get($ar['_copy_from']);
522 $this->setFrom($copy->toArray());
524 $this->createFrom($copy->getStoreName());
526 $roo->addEvent("ADD", $this, $this->toEventString());
528 $r = DB_DataObject::factory($this->tableName());
533 $roo->jok($r->toArray());
540 // FIXME - we should be checking perms here...
542 // this should be doign update
545 if (!$this->checkPerm($this->id ? 'A' : 'E', $roo->authUser)) {
546 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
551 if (!isset($_FILES['imageUpload'])) {
552 return; // standard update...
555 if ( !$this->onUpload($this)) {
556 $roo->jerr("File upload failed : ". $this->err);
559 $roo->addEvent("ADD", $this, $this->toEventString());
561 $r = DB_DataObject::factory($this->tableName());
566 $roo->jok($r->toArray());
570 function toEventString()
573 //$p = DB_DataObject::factory($this->ontable);
575 // return "ERROR unknown table? {$this->ontable}";
579 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
580 //$p->toEventString();