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 $filesize; // int(11) not_null
19 public $created; // datetime(19) not_null binary
20 public $created_by; // int(11) not_null
22 public $width; // int(11) not_null
23 public $height; // int(11) not_null
26 public $imgtype; // string(32) not_null
27 public $parent_image_id; // int(11) not_null
28 public $linkurl; // string(254) not_null
29 public $descript; // blob(65535) not_null blob
30 public $title; // string(128) not_null
31 public $displayorder; // int(11) not_null
32 public $language; // string(6) not_null
35 /* the code above is auto generated do not remove the tag below */
38 * create an email from file.
39 * these must have been set first.
43 function createFrom($file, $filename=false)
45 // copy the file into the storage area..
46 if (!file_exists($file) || !filesize($file)) {
50 $filename = empty($filename) ? $file : $filename;
52 if (empty($this->mimetype)) {
53 require_once 'File/MimeType.php';
54 $y = new File_MimeType();
55 $this->mimetype = $y->fromFilename($filename);
58 $this->mimetype= strtolower($this->mimetype);
60 if (array_shift(explode('/', $this->mimetype)) == 'image') {
62 $imgs = @getimagesize($file);
64 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
67 list($this->width , $this->height) = $imgs;
71 $this->filesize = filesize($file);
72 $this->created = date('Y-m-d H:i:s');
75 if (empty($this->filename)) {
76 $this->filename = basename($filename);
79 //DB_DataObject::debugLevel(1);
88 $f = $this->getStoreName();
90 if (!file_exists($dest)) {
93 mkdir($dest, 0770, true);
104 // $this->createThumbnail(0,50);
110 * Calculate target file name
112 * @return - target file name
114 function getStoreName()
116 $opts = HTML_FlexyFramework::get()->Pman;
117 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
118 return implode( '/', array(
119 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
126 * deletes all the image instances of it...
130 function beforeDelete()
132 $fn = $this->getStoreName();
133 if (file_exists($fn)) {
139 if (file_exists($d)) {
142 while (false !== ($fn = readdir($dh))) {
143 if (substr($fn, 0, strlen($b)) == $b) {
144 unlink($d. '/'. $fn);
151 * check mimetype against type
152 * - eg. img.is(#image#)
157 if (empty($this->mimetype)) {
160 return 0 === strcasecmp($type, array_shift(explode('/',$this->mimetype)));
164 * onUpload (singlely attached image to a table)
167 function onUploadWithTbl($tbl, $fld)
169 if ( $tbl->__table == 'Images') {
170 return; // not upload to self...
172 if (empty($_FILES['imageUpload']['tmp_name']) ||
173 empty($_FILES['imageUpload']['name']) ||
174 empty($_FILES['imageUpload']['type'])
179 $image = DB_DataObject::factory('Images');
180 $image->get($tbl->$fld);
181 $image->beforeDelete();
185 $image = DB_DataObject::factory('Images');
186 $image->onid = $tbl->id;
187 $image->ontable = $tbl->__table;
188 $image->filename = $_FILES['imageUpload']['name'];
189 $image->mimetype = $_FILES['imageUpload']['type'];
191 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
195 $tbl->$fld = $image->id;
201 function onUpload($ctrl)
204 if (empty($_FILES['imageUpload']['tmp_name']) ||
205 empty($_FILES['imageUpload']['name']) ||
206 empty($_FILES['imageUpload']['type'])
208 $this->err = "Missing file details";
213 $this->beforeDelete();
215 if ( empty($this->ontable)) {
216 $this->err = "Missing ontable";
220 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
221 // then its an upload
222 $img = DB_DataObject::factory('Images');
223 $img->onid = $this->onid;
224 $img->ontable = $this->ontable;
225 $img->imgtype = $this->imgtype;
228 while ($img->fetch()) {
229 $img->beforeDelete();
237 require_once 'File/MimeType.php';
238 $y = new File_MimeType();
239 $this->mimetype = $_FILES['imageUpload']['type'];
240 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
241 $inf = pathinfo($_FILES['imageUpload']['name']);
242 $this->mimetype = $y->fromExt($inf['extension']);
246 $ext = $y->toExt(trim((string) $this->mimetype ));
248 $this->filename = empty($this->filename) ?
249 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
253 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
261 * return a list of images for an object, optionally with a mime regex.
262 * eg. '%/pdf' or 'image/%'
264 function gather($obj, $mime_like='')
266 //DB_DataObject::debugLevel(1);
267 if (empty($obj->id)) {
272 $c->ontable = $obj->tableName();
275 if (!empty($mime_like)) {
276 $c->whereAdd("Images.mimetype LIKE '". $c->escape($mime_like) ."'");
279 return $c->fetchAll();
284 * set or get the dataobject this image is associated with
285 * @param DB_DataObject $obj An object to associate this image with
286 * (does not store it - you need to call update() to do that)
287 * @return DB_DataObject the dataobject this image is attached to.
289 function object($obj=false)
291 if ($obj === false) {
292 $ret = DB_DataObject::factory($this->ontable);
293 $ret->get($this->onid);
296 $this->ontable = $obj->tableName();
297 $this->onid = $obj->id; /// assumes our nice standard of using ids..
302 function toRooArray($req = array()) {
303 // echo '<PRE>';print_r($req);exit;
304 $ret= $this->toArray();
307 if (!empty($req['query']['imagesize'])) {
308 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
310 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
312 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
314 if (!empty($req['query']['imagesize'])) {
315 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
325 * URL - create a url for the image.
326 * size - use -1 to show full size.
327 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
331 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
334 return 'about:blank';
338 $ff = HTML_FlexyFramework::get();
339 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
341 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
344 //$size = max(100, (int) $size);
345 //$size = min(1024, (int) $size);
348 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
351 * size could be 123x345
355 function toHTML($size, $provider = '/Images/Thumb')
360 $sz = explode('x', $size);
363 if (!$this->id || empty($this->width)) {
365 $this->width = empty($sz[1]) ? $sx : $sz[1];
369 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
377 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
385 function setFromRoo($ar, $roo)
387 // not sure why we do this..
389 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
390 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
393 if ($this->find(true)) {
394 $roo->old = clone($this);
399 if (!empty($ar['_copy_from'])) {
400 $copy = DB_DataObject::factory('Images');
401 $copy->get($ar['_copy_from']);
402 $this->setFrom($copy->toArray());
404 $this->createFrom($copy->getStoreName());
406 $roo->addEvent("ADD", $this, $this->toEventString());
408 $r = DB_DataObject::factory($this->tableName());
413 $roo->jok($r->toArray());
420 // FIXME - we should be checking perms here...
421 //if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser)) {
422 // $this->jerr("PERMISSION DENIED");
424 // this should be doign update
427 if (!isset($_FILES['imageUpload'])) {
428 return; // standard update...
431 if ( !$this->onUpload($this)) {
432 $this->jerr("File upload failed");
434 $roo->addEvent("ADD", $this, $this->toEventString());
436 $r = DB_DataObject::factory($this->tableName());
441 $roo->jok($r->toArray());
444 function toEventString()
447 //$p = DB_DataObject::factory($this->ontable);
449 // return "ERROR unknown table? {$this->ontable}";
453 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
454 //$p->toEventString();