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'] ));
67 $path = $tmpdir . '/' . time() . '#attachment.' . $ext;
68 if(!file_exists($path)){
69 file_put_contents($path, file_get_contents($q['_remote_upload']));
71 $this->createFrom($path);
73 $roo->addEvent("ADD", $this, $this->toEventString());
75 $r = DB_DataObject::factory($this->tableName());
80 $roo->jok($r->URL(-1,'/Images') . '#attachment-'. $r->id);
87 * create an email from file.
88 * these must have been set first.
92 function createFrom($file, $filename=false)
94 // copy the file into the storage area..
95 if (!file_exists($file) || !filesize($file)) {
99 $filename = empty($filename) ? $file : $filename;
101 if (empty($this->mimetype)) {
102 require_once 'File/MimeType.php';
103 $y = new File_MimeType();
104 $this->mimetype = $y->fromFilename($filename);
107 $this->mimetype= strtolower($this->mimetype);
109 if (array_shift(explode('/', $this->mimetype)) == 'image') {
111 $imgs = @getimagesize($file);
113 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
116 list($this->width , $this->height) = $imgs;
120 $this->filesize = filesize($file);
121 $this->created = date('Y-m-d H:i:s');
124 if (empty($this->filename)) {
125 $this->filename = basename($filename);
128 //DB_DataObject::debugLevel(1);
137 $f = $this->getStoreName();
139 if (!file_exists($dest)) {
140 // currently this is 0775 due to problems using shared hosing (FTP)
141 // it makes all the files unaccessable..
142 // you can normally solve this by giving the storedirectory better perms
143 // if needed on a dedicated server..
144 $oldumask = umask(0);
145 mkdir($dest, 0775, true);
156 // $this->createThumbnail(0,50);
162 * Calculate target file name
164 * @return - target file name
166 function getStoreName()
168 $opts = HTML_FlexyFramework::get()->Pman;
169 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
170 return implode( '/', array(
171 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
177 * deletes all the image instances of it...
181 function beforeDelete()
183 $fn = $this->getStoreName();
184 if (file_exists($fn)) {
190 if (file_exists($d)) {
193 while (false !== ($fn = readdir($dh))) {
194 if (substr($fn, 0, strlen($b)) == $b) {
195 unlink($d. '/'. $fn);
202 * check mimetype against type
203 * - eg. img.is(#image#)
208 if (empty($this->mimetype)) {
211 return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
215 * onUpload (singlely attached image to a table)
218 function onUploadWithTbl($tbl, $fld)
220 if ( $tbl->__table == 'Images') {
221 return; // not upload to self...
223 if (empty($_FILES['imageUpload']['tmp_name']) ||
224 empty($_FILES['imageUpload']['name']) ||
225 empty($_FILES['imageUpload']['type'])
230 $image = DB_DataObject::factory('Images');
231 $image->get($tbl->$fld);
232 $image->beforeDelete();
236 $image = DB_DataObject::factory('Images');
237 $image->onid = $tbl->id;
238 $image->ontable = $tbl->__table;
239 $image->filename = $_FILES['imageUpload']['name'];
240 $image->mimetype = $_FILES['imageUpload']['type'];
242 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
246 $tbl->$fld = $image->id;
253 function onUpload($ctrl)
256 if (empty($_FILES['imageUpload']['tmp_name']) ||
257 empty($_FILES['imageUpload']['name']) ||
258 empty($_FILES['imageUpload']['type'])
260 $this->err = "Missing file details";
265 $this->beforeDelete();
267 if ( empty($this->ontable)) {
268 $this->err = "Missing ontable";
272 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
273 // then its an upload
274 $img = DB_DataObject::factory('Images');
275 $img->onid = $this->onid;
276 $img->ontable = $this->ontable;
277 $img->imgtype = $this->imgtype;
280 while ($img->fetch()) {
281 $img->beforeDelete();
289 require_once 'File/MimeType.php';
290 $y = new File_MimeType();
291 $this->mimetype = $_FILES['imageUpload']['type'];
292 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
293 $inf = pathinfo($_FILES['imageUpload']['name']);
294 $this->mimetype = $y->fromExt($inf['extension']);
298 $ext = $y->toExt(trim((string) $this->mimetype ));
300 $this->filename = empty($this->filename) ?
301 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
305 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
315 * return a list of images for an object, optionally with a mime regex.
316 * eg. '%/pdf' or 'image/%'
320 * $i = DB_DataObject::factory('Images');
321 * $i->imgtype = 'LOGO';
322 * $ar = $i->gather($somedataobject, 'image/%');
324 * @param {DB_DataObject} dataobject = the object to gather data on.
325 * @param {String} mimelike LIKE query to use for search
328 function gather($obj, $mime_like='', $opts=array())
330 //DB_DataObject::debugLevel(1);
331 if (empty($obj->id)) {
336 $c->ontable = $obj->tableName();
339 if (!empty($mime_like)) {
340 $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
343 return $c->fetchAll();
348 * set or get the dataobject this image is associated with
349 * @param DB_DataObject $obj An object to associate this image with
350 * (does not store it - you need to call update() to do that)
351 * @return DB_DataObject the dataobject this image is attached to.
353 function object($obj=false)
355 if ($obj === false) {
356 if (empty($this->ontable) || empty($this->onid)) {
359 $ret = DB_DataObject::factory($this->ontable);
360 $ret->get($this->onid);
365 $this->ontable = $obj->tableName();
366 $this->onid = $obj->id; /// assumes our nice standard of using ids..
371 function toRooArray($req = array()) {
372 // echo '<PRE>';print_r($req);exit;
373 $ret= $this->toArray();
377 $ff = HTML_FlexyFramework::get();
380 $ret['public_baseURL'] = isset($ff->Pman_Images['public_baseURL']) ?
381 $ff->Pman_Images['public_baseURL'] : $ff->baseURL;
383 if (!empty($req['query']['imagesize'])) {
384 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
386 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
388 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
390 if (!empty($req['query']['imagesize'])) {
391 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
401 * URL - create a url for the image.
402 * size - use -1 to show full size.
403 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
407 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
410 return 'about:blank';
414 $ff = HTML_FlexyFramework::get();
415 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
416 if (preg_match('#^http[s]*://#', $provider)) {
421 $provider = preg_replace('#/Thumb$#', '', $provider);
423 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
426 //$size = max(100, (int) $size);
427 //$size = min(1024, (int) $size);
430 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
433 * size could be 123x345
437 function toHTML($size, $provider = '/Images/Thumb')
442 $sz = explode('x', $size);
445 if (!$this->id || empty($this->width)) {
447 $this->width = empty($sz[1]) ? $sx : $sz[1];
451 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
458 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
464 * to Fileconvert object..
469 function toFileConvert()
471 require_once 'File/Convert.php';
472 $fc = new File_Convert($this->getStoreName(), $this->mimetype);
479 require_once 'File/MimeType.php';
481 $y = new File_MimeType();
482 return $y->toExt($this->mimetype);
494 function setFromRoo($ar, $roo)
496 // not sure why we do this..
502 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
503 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
506 if ($this->find(true)) {
507 $roo->old = clone($this);
512 if (!empty($ar['_copy_from'])) {
514 if (!$this->checkPerm( 'A' , $roo->authUser)) {
515 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
518 $copy = DB_DataObject::factory('Images');
519 $copy->get($ar['_copy_from']);
520 $this->setFrom($copy->toArray());
522 $this->createFrom($copy->getStoreName());
524 $roo->addEvent("ADD", $this, $this->toEventString());
526 $r = DB_DataObject::factory($this->tableName());
531 $roo->jok($r->toArray());
538 // FIXME - we should be checking perms here...
540 // this should be doign update
543 if (!$this->checkPerm($this->id ? 'A' : 'E', $roo->authUser)) {
544 $roo->jerr("IMAGE UPLOAD PERMISSION DENIED");
549 if (!isset($_FILES['imageUpload'])) {
550 return; // standard update...
553 if ( !$this->onUpload($this)) {
554 $roo->jerr("File upload failed : ". $this->err);
557 $roo->addEvent("ADD", $this, $this->toEventString());
559 $r = DB_DataObject::factory($this->tableName());
564 $roo->jok($r->toArray());
568 function toEventString()
571 //$p = DB_DataObject::factory($this->ontable);
573 // return "ERROR unknown table? {$this->ontable}";
577 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
578 //$p->toEventString();