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
45 if (method_exists($o, 'hasPerm')) {
46 // edit permissions on related object needed...
47 return $o->hasPerm( $perm == 'S' ? 'S' : 'E' , $au);
51 return true; //// ??? not really that safe...
61 * create an email from file.
62 * these must have been set first.
66 function createFrom($file, $filename=false)
68 // copy the file into the storage area..
69 if (!file_exists($file) || !filesize($file)) {
73 $filename = empty($filename) ? $file : $filename;
75 if (empty($this->mimetype)) {
76 require_once 'File/MimeType.php';
77 $y = new File_MimeType();
78 $this->mimetype = $y->fromFilename($filename);
81 $this->mimetype= strtolower($this->mimetype);
83 if (array_shift(explode('/', $this->mimetype)) == 'image') {
85 $imgs = @getimagesize($file);
87 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
90 list($this->width , $this->height) = $imgs;
94 $this->filesize = filesize($file);
95 $this->created = date('Y-m-d H:i:s');
98 if (empty($this->filename)) {
99 $this->filename = basename($filename);
102 //DB_DataObject::debugLevel(1);
111 $f = $this->getStoreName();
113 if (!file_exists($dest)) {
115 $oldumask = umask(0);
116 mkdir($dest, 0770, true);
127 // $this->createThumbnail(0,50);
133 * Calculate target file name
135 * @return - target file name
137 function getStoreName()
139 $opts = HTML_FlexyFramework::get()->Pman;
140 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
141 return implode( '/', array(
142 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
149 * deletes all the image instances of it...
153 function beforeDelete()
155 $fn = $this->getStoreName();
156 if (file_exists($fn)) {
162 if (file_exists($d)) {
165 while (false !== ($fn = readdir($dh))) {
166 if (substr($fn, 0, strlen($b)) == $b) {
167 unlink($d. '/'. $fn);
174 * check mimetype against type
175 * - eg. img.is(#image#)
180 if (empty($this->mimetype)) {
183 return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
187 * onUpload (singlely attached image to a table)
190 function onUploadWithTbl($tbl, $fld)
192 if ( $tbl->__table == 'Images') {
193 return; // not upload to self...
195 if (empty($_FILES['imageUpload']['tmp_name']) ||
196 empty($_FILES['imageUpload']['name']) ||
197 empty($_FILES['imageUpload']['type'])
202 $image = DB_DataObject::factory('Images');
203 $image->get($tbl->$fld);
204 $image->beforeDelete();
208 $image = DB_DataObject::factory('Images');
209 $image->onid = $tbl->id;
210 $image->ontable = $tbl->__table;
211 $image->filename = $_FILES['imageUpload']['name'];
212 $image->mimetype = $_FILES['imageUpload']['type'];
214 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
218 $tbl->$fld = $image->id;
224 function onUpload($ctrl)
227 if (empty($_FILES['imageUpload']['tmp_name']) ||
228 empty($_FILES['imageUpload']['name']) ||
229 empty($_FILES['imageUpload']['type'])
231 $this->err = "Missing file details";
236 $this->beforeDelete();
238 if ( empty($this->ontable)) {
239 $this->err = "Missing ontable";
243 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
244 // then its an upload
245 $img = DB_DataObject::factory('Images');
246 $img->onid = $this->onid;
247 $img->ontable = $this->ontable;
248 $img->imgtype = $this->imgtype;
251 while ($img->fetch()) {
252 $img->beforeDelete();
260 require_once 'File/MimeType.php';
261 $y = new File_MimeType();
262 $this->mimetype = $_FILES['imageUpload']['type'];
263 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
264 $inf = pathinfo($_FILES['imageUpload']['name']);
265 $this->mimetype = $y->fromExt($inf['extension']);
269 $ext = $y->toExt(trim((string) $this->mimetype ));
271 $this->filename = empty($this->filename) ?
272 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
276 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
284 * return a list of images for an object, optionally with a mime regex.
285 * eg. '%/pdf' or 'image/%'
289 * $i = DB_DataObject::factory('Images');
290 * $i->imgtype = 'LOGO';
291 * $ar = $i->gather($somedataobject, 'image/%');
293 * @param {DB_DataObject} dataobject = the object to gather data on.
294 * @param {String} mimelike LIKE query to use for search
297 function gather($obj, $mime_like='', $opts=array())
299 //DB_DataObject::debugLevel(1);
300 if (empty($obj->id)) {
305 $c->ontable = $obj->tableName();
308 if (!empty($mime_like)) {
309 $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
312 return $c->fetchAll();
317 * set or get the dataobject this image is associated with
318 * @param DB_DataObject $obj An object to associate this image with
319 * (does not store it - you need to call update() to do that)
320 * @return DB_DataObject the dataobject this image is attached to.
322 function object($obj=false)
324 if ($obj === false) {
325 $ret = DB_DataObject::factory($this->ontable);
326 $ret->get($this->onid);
329 $this->ontable = $obj->tableName();
330 $this->onid = $obj->id; /// assumes our nice standard of using ids..
335 function toRooArray($req = array()) {
336 // echo '<PRE>';print_r($req);exit;
337 $ret= $this->toArray();
340 if (!empty($req['query']['imagesize'])) {
341 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
343 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
345 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
347 if (!empty($req['query']['imagesize'])) {
348 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
358 * URL - create a url for the image.
359 * size - use -1 to show full size.
360 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
364 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
367 return 'about:blank';
371 $ff = HTML_FlexyFramework::get();
372 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
374 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
377 //$size = max(100, (int) $size);
378 //$size = min(1024, (int) $size);
381 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
384 * size could be 123x345
388 function toHTML($size, $provider = '/Images/Thumb')
393 $sz = explode('x', $size);
396 if (!$this->id || empty($this->width)) {
398 $this->width = empty($sz[1]) ? $sx : $sz[1];
402 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
410 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
422 function setFromRoo($ar, $roo)
424 // not sure why we do this..
430 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
431 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
434 if ($this->find(true)) {
435 $roo->old = clone($this);
440 if (!empty($ar['_copy_from'])) {
441 $copy = DB_DataObject::factory('Images');
442 $copy->get($ar['_copy_from']);
443 $this->setFrom($copy->toArray());
445 $this->createFrom($copy->getStoreName());
447 $roo->addEvent("ADD", $this, $this->toEventString());
449 $r = DB_DataObject::factory($this->tableName());
454 $roo->jok($r->toArray());
461 // FIXME - we should be checking perms here...
463 // this should be doign update
466 $this->checkPerm($this->id ? 'A' ? 'E', $this->authUser)) {
467 $this->jerr("IMAGE UPLOAD PERMISSION DENIED");
470 if (!isset($_FILES['imageUpload'])) {
471 return; // standard update...
474 if ( !$this->onUpload($this)) {
475 $roo->jerr("File upload failed : ". $this->err);
477 $roo->addEvent("ADD", $this, $this->toEventString());
479 $r = DB_DataObject::factory($this->tableName());
484 $roo->jok($r->toArray());
488 function toEventString()
491 //$p = DB_DataObject::factory($this->ontable);
493 // return "ERROR unknown table? {$this->ontable}";
497 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
498 //$p->toEventString();