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);
153 * onUpload (singlely attached image to a table)
156 function onUploadWithTbl($tbl, $fld)
158 if ( $tbl->__table == 'Images') {
159 return; // not upload to self...
161 if (empty($_FILES['imageUpload']['tmp_name']) ||
162 empty($_FILES['imageUpload']['name']) ||
163 empty($_FILES['imageUpload']['type'])
168 $image = DB_DataObject::factory('Images');
169 $image->get($tbl->$fld);
170 $image->beforeDelete();
174 $image = DB_DataObject::factory('Images');
175 $image->onid = $tbl->id;
176 $image->ontable = $tbl->__table;
177 $image->filename = $_FILES['imageUpload']['name'];
178 $image->mimetype = $_FILES['imageUpload']['type'];
180 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
184 $tbl->$fld = $image->id;
190 function onUpload($ctrl)
193 if (empty($_FILES['imageUpload']['tmp_name']) ||
194 empty($_FILES['imageUpload']['name']) ||
195 empty($_FILES['imageUpload']['type'])
197 $this->err = "Missing file details";
202 $this->beforeDelete();
204 if ( empty($this->ontable)) {
205 $this->err = "Missing ontable";
209 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
210 // then its an upload
211 $img = DB_DataObject::factory('Images');
212 $img->onid = $this->onid;
213 $img->ontable = $this->ontable;
214 $img->imgtype = $this->imgtype;
217 while ($img->fetch()) {
218 $img->beforeDelete();
226 require_once 'File/MimeType.php';
227 $y = new File_MimeType();
228 $this->mimetype = $_FILES['imageUpload']['type'];
229 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
230 $inf = pathinfo($_FILES['imageUpload']['name']);
231 $this->mimetype = $y->fromExt($inf['extension']);
235 $ext = $y->toExt(trim((string) $this->mimetype ));
237 $this->filename = empty($this->filename) ?
238 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
242 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
250 * return a list of images for an object, optionally with a mime regex.
251 * eg. '%/pdf' or 'image/%'
253 function gather($obj, $mime_like='')
255 //DB_DataObject::debugLevel(1);
256 if (empty($obj->id)) {
261 $c->ontable = $obj->tableName();
264 if (!empty($mime_like)) {
265 $c->whereAdd("mimetype LIKE '". $c->escape($mime_like) ."'");
268 return $c->fetchAll();
271 * creation - associate this image with a dataobject
272 * - currently assumes id is the key column
273 * @param DB_DataObject $obj a dataobject
275 function associate($obj)
277 $this->ontable = $obj->tableName();
278 $this->onid = $obj->id; /// assumes our nice standard of using ids..
283 * set or get the dataobject this image is associated with
284 * @param DB_DataObject $obj An object to associate this image with
285 * (does not store it - you need to call update() to do that)
286 * @return DB_DataObject the dataobject this image is attached to.
288 function object($obj=false)
290 if ($obj === false) {
291 $ret = DB_DataObject::factory($this->ontable);
292 $ret->get($this->onid);
295 $this->ontable = $obj->tableName();
296 $this->onid = $obj->id; /// assumes our nice standard of using ids..
301 function toRooArray($req = array()) {
302 // echo '<PRE>';print_r($req);exit;
303 $ret= $this->toArray();
306 if (!empty($req['query']['imagesize'])) {
307 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
309 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
311 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
313 if (!empty($req['query']['imagesize'])) {
314 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
324 * URL - create a url for the image.
325 * size - use -1 to show full size.
326 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
330 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
333 return 'about:blank';
337 $ff = HTML_FlexyFramework::get();
338 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
340 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
343 //$size = max(100, (int) $size);
344 //$size = min(1024, (int) $size);
347 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
350 * size could be 123x345
354 function toHTML($size, $provider = '/Images/Thumb')
359 $sz = explode('x', $size);
362 if (!$this->id || empty($this->width)) {
364 $this->width = empty($sz[1]) ? $sx : $sz[1];
368 $ratio = empty($this->width) ? 1 : $this->height/ ($this->width *1.0);
376 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
384 function setFromRoo($ar, $roo)
386 // not sure why we do this..
388 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
389 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
392 if ($this->find(true)) {
393 $roo->old = clone($this);
398 if (!empty($ar['_copy_from'])) {
399 $copy = DB_DataObject::factory('Images');
400 $copy->get($ar['_copy_from']);
401 $this->setFrom($copy->toArray());
403 $this->createFrom($copy->getStoreName());
405 $roo->addEvent("ADD", $this, $this->toEventString());
407 $r = DB_DataObject::factory($this->tableName());
412 $roo->jok($r->toArray());
419 // FIXME - we should be checking perms here...
420 //if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser)) {
421 // $this->jerr("PERMISSION DENIED");
423 // this should be doign update
426 if (!isset($_FILES['imageUpload'])) {
427 return; // standard update...
430 if ( !$this->onUpload($this)) {
431 $this->jerr("File upload failed");
433 $roo->addEvent("ADD", $this, $this->toEventString());
435 $r = DB_DataObject::factory($this->tableName());
440 $roo->jok($r->toArray());
443 function toEventString()
446 //$p = DB_DataObject::factory($this->ontable);
448 // return "ERROR unknown table? {$this->ontable}";
452 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
453 //$p->toEventString();