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');
73 if (empty($this->filename)) {
74 $this->filename = basename($file);
77 //DB_DataObject::debugLevel(1);
86 $f = $this->getStoreName();
88 if (!file_exists($dest)) {
91 mkdir($dest, 0770, true);
102 // $this->createThumbnail(0,50);
108 * Calculate target file name
110 * @return - target file name
112 function getStoreName()
114 $opts = PEAR::getStaticProperty('Pman', 'options');
115 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
116 return implode( '/', array(
117 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
124 * deletes all the image instances of it...
128 function beforeDelete()
130 $fn = $this->getStoreName();
131 if (file_exists($fn)) {
137 if (file_exists($d)) {
140 while (false !== ($fn = readdir($dh))) {
141 if (substr($fn, 0, strlen($b)) == $b) {
142 unlink($d. '/'. $fn);
151 * onUpload (singlely attached image to a table)
154 function onUploadWithTbl($tbl, $fld)
156 if ( $tbl->__table == 'Images') {
157 return; // not upload to self...
159 if (empty($_FILES['imageUpload']['tmp_name']) ||
160 empty($_FILES['imageUpload']['name']) ||
161 empty($_FILES['imageUpload']['type'])
166 $image = DB_DataObject::factory('Images');
167 $image->get($tbl->$fld);
168 $image->beforeDelete();
172 $image = DB_DataObject::factory('Images');
173 $image->onid = $tbl->id;
174 $image->ontable = $tbl->__table;
175 $image->filename = $_FILES['imageUpload']['name'];
176 $image->mimetype = $_FILES['imageUpload']['type'];
178 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
182 $tbl->$fld = $image->id;
188 function onUpload($ctrl)
191 if (empty($_FILES['imageUpload']['tmp_name']) ||
192 empty($_FILES['imageUpload']['name']) ||
193 empty($_FILES['imageUpload']['type'])
195 $this->err = "Missing file details";
200 $this->beforeDelete();
202 if ( empty($this->ontable)) {
203 $this->err = "Missing ontable";
207 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
208 // then its an upload
209 $img = DB_DataObject::factory('Images');
210 $img->onid = $this->onid;
211 $img->ontable = $this->ontable;
212 $img->imgtype = $this->imgtype;
215 while ($img->fetch()) {
216 $img->beforeDelete();
224 require_once 'File/MimeType.php';
225 $y = new File_MimeType();
226 $this->mimetype = $_FILES['imageUpload']['type'];
227 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
228 $inf = pathinfo($_FILES['imageUpload']['name']);
229 $this->mimetype = $y->fromExt($inf['extension']);
233 $ext = $y->toExt(trim((string) $this->mimetype ));
235 $this->filename = empty($this->filename) ?
236 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
240 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
248 * return a list of images for an object, optionally with a mime regex.
249 * eg. '%/pdf' or 'image/%'
251 function gather($obj, $mime_like='')
253 //DB_DataObject::debugLevel(1);
254 if (empty($obj->id)) {
258 $c->ontable = $obj->tableName();
260 if (!empty($mime_like)) {
261 $c->whereAdd("mimetype LIKE '". $c->escape($mime_like) ."'");
264 return $c->fetchAll();
268 function toRooArray($req = array()) {
269 // echo '<PRE>';print_r($req);exit;
270 $ret= $this->toArray();
273 if (!empty($req['query']['imagesize'])) {
274 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
276 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
278 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
280 if (!empty($req['query']['imagesize'])) {
281 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
291 * URL - create a url for the image.
292 * size - use -1 to show full size.
293 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
297 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
300 return 'about:blank';
304 $ff = HTML_FlexyFramework::get();
305 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
307 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
310 //$size = max(100, (int) $size);
311 //$size = min(1024, (int) $size);
314 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
317 * size could be 123x345
321 function toHTML($size, $provider = '/Images/Thumb')
326 $sz = explode('x', $size);
331 $this->width = empty($sz[1]) ? $sx : $sz[1];
335 $ratio = $this->height/ ($this->width *1.0);
343 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
351 function setFromRoo($ar, $roo)
353 // not sure why we do this..
355 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
356 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
359 if ($this->find(true)) {
360 $roo->old = clone($this);
365 if (!empty($ar['_copy_from'])) {
366 $copy = DB_DataObject::factory('Images');
367 $copy->get($ar['_copy_from']);
368 $this->setFrom($copy->toArray());
370 $this->createFrom($copy->getStoreName());
372 $roo->addEvent("ADD", $this, $this->toEventString());
374 $r = DB_DataObject::factory($this->tableName());
379 $roo->jok($r->toArray());
386 // FIXME - we should be checking perms here...
387 //if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser)) {
388 // $this->jerr("PERMISSION DENIED");
390 // this should be doign update
393 if (!isset($_FILES['imageUpload'])) {
394 return; // standard update...
397 if ( !$this->onUpload($this)) {
398 $this->jerr("File upload failed");
400 $roo->addEvent("ADD", $this, $this->toEventString());
402 $r = DB_DataObject::factory($this->tableName());
407 $roo->jok($r->toArray());
410 function toEventString()
413 //$p = DB_DataObject::factory($this->ontable);
415 // return "ERROR unknown table? {$this->ontable}";
419 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
420 //$p->toEventString();