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 * create an email from file.
35 * these must have been set first.
39 function createFrom($file, $filename=false)
41 // copy the file into the storage area..
42 if (!file_exists($file) || !filesize($file)) {
46 $filename = empty($filename) ? $file : $filename;
48 if (empty($this->mimetype)) {
49 require_once 'File/MimeType.php';
50 $y = new File_MimeType();
51 $this->mimetype = $y->fromFilename($filename);
54 $this->mimetype= strtolower($this->mimetype);
56 if (array_shift(explode('/', $this->mimetype)) == 'image') {
58 $imgs = @getimagesize($file);
60 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
63 list($this->width , $this->height) = $imgs;
67 $this->filesize = filesize($file);
68 $this->created = date('Y-m-d H:i:s');
71 if (empty($this->filename)) {
72 $this->filename = basename($filename);
75 //DB_DataObject::debugLevel(1);
84 $f = $this->getStoreName();
86 if (!file_exists($dest)) {
89 mkdir($dest, 0770, true);
100 // $this->createThumbnail(0,50);
106 * Calculate target file name
108 * @return - target file name
110 function getStoreName()
112 $opts = HTML_FlexyFramework::get()->Pman;
113 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
114 return implode( '/', array(
115 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
122 * deletes all the image instances of it...
126 function beforeDelete()
128 $fn = $this->getStoreName();
129 if (file_exists($fn)) {
135 if (file_exists($d)) {
138 while (false !== ($fn = readdir($dh))) {
139 if (substr($fn, 0, strlen($b)) == $b) {
140 unlink($d. '/'. $fn);
147 * check mimetype against type
148 * - eg. img.is(#image#)
153 if (empty($this->mimetype)) {
156 return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
160 * onUpload (singlely attached image to a table)
163 function onUploadWithTbl($tbl, $fld)
165 if ( $tbl->__table == 'Images') {
166 return; // not upload to self...
168 if (empty($_FILES['imageUpload']['tmp_name']) ||
169 empty($_FILES['imageUpload']['name']) ||
170 empty($_FILES['imageUpload']['type'])
175 $image = DB_DataObject::factory('Images');
176 $image->get($tbl->$fld);
177 $image->beforeDelete();
181 $image = DB_DataObject::factory('Images');
182 $image->onid = $tbl->id;
183 $image->ontable = $tbl->__table;
184 $image->filename = $_FILES['imageUpload']['name'];
185 $image->mimetype = $_FILES['imageUpload']['type'];
187 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
191 $tbl->$fld = $image->id;
197 function onUpload($ctrl)
200 if (empty($_FILES['imageUpload']['tmp_name']) ||
201 empty($_FILES['imageUpload']['name']) ||
202 empty($_FILES['imageUpload']['type'])
204 $this->err = "Missing file details";
209 $this->beforeDelete();
211 if ( empty($this->ontable)) {
212 $this->err = "Missing ontable";
216 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
217 // then its an upload
218 $img = DB_DataObject::factory('Images');
219 $img->onid = $this->onid;
220 $img->ontable = $this->ontable;
221 $img->imgtype = $this->imgtype;
224 while ($img->fetch()) {
225 $img->beforeDelete();
233 require_once 'File/MimeType.php';
234 $y = new File_MimeType();
235 $this->mimetype = $_FILES['imageUpload']['type'];
236 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
237 $inf = pathinfo($_FILES['imageUpload']['name']);
238 $this->mimetype = $y->fromExt($inf['extension']);
242 $ext = $y->toExt(trim((string) $this->mimetype ));
244 $this->filename = empty($this->filename) ?
245 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
249 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
257 * return a list of images for an object, optionally with a mime regex.
258 * eg. '%/pdf' or 'image/%'
262 * $i = DB_DataObject::factory('Images');
263 * $i->imgtype = 'LOGO';
264 * $ar = $i->gather($somedataobject, 'image/%');
266 * @param {DB_DataObject} dataobject = the object to gather data on.
267 * @param {String} mimelike LIKE query to use for search
270 function gather($obj, $mime_like='', $opts=array())
272 //DB_DataObject::debugLevel(1);
273 if (empty($obj->id)) {
278 $c->ontable = $obj->tableName();
281 if (!empty($mime_like)) {
282 $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
285 return $c->fetchAll();
290 * set or get the dataobject this image is associated with
291 * @param DB_DataObject $obj An object to associate this image with
292 * (does not store it - you need to call update() to do that)
293 * @return DB_DataObject the dataobject this image is attached to.
295 function object($obj=false)
297 if ($obj === false) {
298 $ret = DB_DataObject::factory($this->ontable);
299 $ret->get($this->onid);
302 $this->ontable = $obj->tableName();
303 $this->onid = $obj->id; /// assumes our nice standard of using ids..
308 function toRooArray($req = array()) {
309 // echo '<PRE>';print_r($req);exit;
310 $ret= $this->toArray();
313 if (!empty($req['query']['imagesize'])) {
314 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
316 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
318 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
320 if (!empty($req['query']['imagesize'])) {
321 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
331 * URL - create a url for the image.
332 * size - use -1 to show full size.
333 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
337 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
340 return 'about:blank';
344 $ff = HTML_FlexyFramework::get();
345 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
347 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
350 //$size = max(100, (int) $size);
351 //$size = min(1024, (int) $size);
354 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
357 * size could be 123x345
361 function toHTML($size, $provider = '/Images/Thumb')
366 $sz = explode('x', $size);
369 if (!$this->id || empty($this->width)) {
371 $this->width = empty($sz[1]) ? $sx : $sz[1];
375 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
383 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
391 function setFromRoo($ar, $roo)
393 // not sure why we do this..
395 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
396 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
399 if ($this->find(true)) {
400 $roo->old = clone($this);
405 if (!empty($ar['_copy_from'])) {
406 $copy = DB_DataObject::factory('Images');
407 $copy->get($ar['_copy_from']);
408 $this->setFrom($copy->toArray());
410 $this->createFrom($copy->getStoreName());
412 $roo->addEvent("ADD", $this, $this->toEventString());
414 $r = DB_DataObject::factory($this->tableName());
419 $roo->jok($r->toArray());
426 // FIXME - we should be checking perms here...
427 //if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser)) {
428 // $this->jerr("PERMISSION DENIED");
430 // this should be doign update
433 if (!isset($_FILES['imageUpload'])) {
434 return; // standard update...
437 if ( !$this->onUpload($this)) {
438 $roo->jerr("File upload failed : ". $this->err);
440 $roo->addEvent("ADD", $this, $this->toEventString());
442 $r = DB_DataObject::factory($this->tableName());
447 $roo->jok($r->toArray());
450 function toEventString()
453 //$p = DB_DataObject::factory($this->ontable);
455 // return "ERROR unknown table? {$this->ontable}";
459 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
460 //$p->toEventString();