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)
41 // copy the file into the storage area..
42 if (!file_exists($file) || !filesize($file)) {
47 $imgs = @getimagesize($file);
49 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
52 list($this->width , $this->height) = $imgs;
55 $this->filesize = filesize($file);
56 $this->created = date('Y-m-d H:i:s');
58 if (empty($this->mimetype)) {
59 require_once 'File/MimeType.php';
60 $y = new File_MimeType();
61 $this->mimetype = $y->fromFilename($file);
65 if (empty($this->filename)) {
66 $this->filename = basename($file);
69 //DB_DataObject::debugLevel(1);
78 $f = $this->getStoreName();
80 if (!file_exists($dest)) {
83 mkdir($dest, 0770, true);
94 // $this->createThumbnail(0,50);
100 * Calculate target file name
102 * @return - target file name
104 function getStoreName()
106 $opts = PEAR::getStaticProperty('Pman', 'options');
107 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
108 return implode( '/', array(
109 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
116 * deletes all the image instances of it...
120 function beforeDelete()
122 $fn = $this->getStoreName();
123 if (file_exists($fn)) {
129 if (file_exists($d)) {
132 while (false !== ($fn = readdir($dh))) {
133 if (substr($fn, 0, strlen($b)) == $b) {
134 unlink($d. '/'. $fn);
143 * onUpload (singlely attached image to a table)
146 function onUploadWithTbl($tbl, $fld)
148 if ( $tbl->__table == 'Images') {
149 return; // not upload to self...
151 if (empty($_FILES['imageUpload']['tmp_name']) ||
152 empty($_FILES['imageUpload']['name']) ||
153 empty($_FILES['imageUpload']['type'])
158 $image = DB_DataObject::factory('Images');
159 $image->get($tbl->$fld);
160 $image->beforeDelete();
164 $image = DB_DataObject::factory('Images');
165 $image->onid = $tbl->id;
166 $image->ontable = $tbl->__table;
167 $image->filename = $_FILES['imageUpload']['name'];
168 $image->mimetype = $_FILES['imageUpload']['type'];
170 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
174 $tbl->$fld = $image->id;
180 function onUpload($ctrl)
183 if (empty($_FILES['imageUpload']['tmp_name']) ||
184 empty($_FILES['imageUpload']['name']) ||
185 empty($_FILES['imageUpload']['type'])
187 $this->err = "Missing file details";
192 $this->beforeDelete();
194 if ( empty($this->ontable)) {
195 $this->err = "Missing ontable";
199 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
200 // then its an upload
201 $img = DB_DataObject::factory('Images');
202 $img->onid = $this->onid;
203 $img->ontable = $this->ontable;
204 $img->imgtype = $this->imgtype;
207 while ($img->fetch()) {
208 $img->beforeDelete();
216 require_once 'File/MimeType.php';
217 $y = new File_MimeType();
218 $this->mimetype = $_FILES['imageUpload']['type'];
219 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
220 $inf = pathinfo($_FILES['imageUpload']['name']);
221 $this->mimetype = $y->fromExt($inf['extension']);
225 $ext = $y->toExt(trim((string) $this->mimetype ));
227 $this->filename = empty($this->filename) ?
228 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
232 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
240 * return a list of images for an object, optionally with a mime regex.
241 * eg. '%/pdf' or 'image/%'
243 function gather($obj, $mime_like='')
245 //DB_DataObject::debugLevel(1);
246 if (empty($obj->id)) {
250 $c->ontable = $obj->tableName();
252 if (!empty($mime_like)) {
253 $c->whereAdd("mimetype LIKE '". $c->escape($mime_like) ."'");
256 return $c->fetchAll();
260 function toRooArray($req = array()) {
261 // echo '<PRE>';print_r($req);exit;
262 $ret= $this->toArray();
265 if (!empty($req['query']['imagesize'])) {
266 $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false;
268 $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL);
270 $ret['url_view'] = $this->URL(-1, '/Images',$baseURL);
272 if (!empty($req['query']['imagesize'])) {
273 $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL);
283 * URL - create a url for the image.
284 * size - use -1 to show full size.
285 * provier = baseURL + /Images/Thumb ... use '/Images/' for full
289 function URL($size , $provider = '/Images/Thumb', $baseURL=false)
292 return 'about:blank';
296 $ff = HTML_FlexyFramework::get();
297 $baseURL = $baseURL ? $baseURL : $ff->baseURL ;
299 return $baseURL . $provider . "/{$this->id}/{$this->filename}";
302 //$size = max(100, (int) $size);
303 //$size = min(1024, (int) $size);
306 return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
309 * size could be 123x345
313 function toHTML($size, $provider = '/Images/Thumb')
318 $sz = explode('x', $size);
323 $this->width = empty($sz[1]) ? $sx : $sz[1];
327 $ratio = $this->height/ ($this->width *1.0);
335 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
343 function setFromRoo($ar, $roo)
345 // not sure why we do this..
347 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
348 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
351 if ($this->find(true)) {
352 $roo->old = clone($this);
357 if (!empty($ar['_copy_from'])) {
358 $copy = DB_DataObject::factory('Images');
359 $copy->get($ar['_copy_from']);
360 $this->setFrom($copy->toArray());
362 $this->createFrom($copy->getStoreName());
364 $roo->addEvent("ADD", $this, $this->toEventString());
366 $r = DB_DataObject::factory($this->tableName());
371 $roo->jok($r->toArray());
378 // FIXME - we should be checking perms here...
379 //if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser)) {
380 // $this->jerr("PERMISSION DENIED");
382 // this should be doign update
385 if (!isset($_FILES['imageUpload'])) {
386 return; // standard update...
389 if ( !$this->onUpload($this)) {
390 $this->jerr("File upload failed");
392 $roo->addEvent("ADD", $this, $this->toEventString());
394 $r = DB_DataObject::factory($this->tableName());
399 $roo->jok($r->toArray());
402 function toEventString()
405 //$p = DB_DataObject::factory($this->ontable);
407 // return "ERROR unknown table? {$this->ontable}";
411 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
412 //$p->toEventString();