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 function createFrom($file)
36 // copy the file into the storage area..
37 if (!file_exists($file) || !filesize($file)) {
42 $imgs = @getimagesize($file);
44 if (empty($imgs) || empty($imgs[0]) || empty($imgs[1])) {
47 list($this->width , $this->height) = $imgs;
50 $this->filesize = filesize($file);
51 $this->created = date('Y-m-d H:i:s');
52 //DB_DataObject::debugLevel(1);
61 $f = $this->getStoreName();
63 if (!file_exists($dest)) {
66 mkdir($dest, 0770, true);
77 // $this->createThumbnail(0,50);
83 * Calculate target file name
85 * @return - target file name
87 function getStoreName()
89 $opts = PEAR::getStaticProperty('Pman', 'options');
90 $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename);
91 return implode( '/', array(
92 $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn
99 * deletes all the image instances of it...
103 function beforeDelete()
105 $fn = $this->getStoreName();
106 if (file_exists($fn)) {
112 if (file_exists($d)) {
115 while (false !== ($fn = readdir($dh))) {
116 if (substr($fn, 0, strlen($b)) == $b) {
117 unlink($d. '/'. $fn);
126 * onUpload (singlely attached image to a table)
129 function onUploadWithTbl($tbl, $fld)
131 if ( $tbl->__table == 'Images') {
132 return; // not upload to self...
134 if (empty($_FILES['imageUpload']['tmp_name']) ||
135 empty($_FILES['imageUpload']['name']) ||
136 empty($_FILES['imageUpload']['type'])
141 $image = DB_DataObject::factory('Images');
142 $image->get($tbl->$fld);
143 $image->beforeDelete();
147 $image = DB_DataObject::factory('Images');
148 $image->onid = $tbl->id;
149 $image->ontable = $tbl->__table;
150 $image->filename = $_FILES['imageUpload']['name'];
151 $image->mimetype = $_FILES['imageUpload']['type'];
153 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
157 $tbl->$fld = $image->id;
163 function onUpload($ctrl)
166 if (empty($_FILES['imageUpload']['tmp_name']) ||
167 empty($_FILES['imageUpload']['name']) ||
168 empty($_FILES['imageUpload']['type'])
170 $this->err = "Missing file details";
175 $this->beforeDelete();
177 if ( empty($this->ontable)) {
178 $this->err = "Missing ontable";
182 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
183 // then its an upload
184 $img = DB_DataObject::factory('Images');
185 $img->onid = $this->onid;
186 $img->ontable = $this->ontable;
187 $img->imgtype = $this->imgtype;
190 while ($img->fetch()) {
191 $img->beforeDelete();
199 require_once 'File/MimeType.php';
200 $y = new File_MimeType();
201 $this->mimetype = $_FILES['imageUpload']['type'];
202 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
203 $inf = pathinfo($_FILES['imageUpload']['name']);
204 $this->mimetype = $y->fromExt($inf['extension']);
208 $ext = $y->toExt(trim((string) $this->mimetype ));
210 $this->filename = empty($this->filename) ?
211 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
215 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
223 * return a list of images for an object, optionally with a mime regex.
224 * eg. '%/pdf' or 'image/%'
226 function gather($obj, $mime_like='')
228 //DB_DataObject::debugLevel(1);
229 if (empty($obj->id)) {
233 $c->ontable = $obj->tableName();
235 if (!empty($mime_like)) {
236 $c->whereAdd("mimetype LIKE '". $c->escape($mime_like) ."'");
239 return $c->fetchAll();
243 function toRooArray($req = array()) {
244 if (empty($req['query']['imagesize'])) {
245 return $this->toArray();
247 $ret= $this->toArray();
249 $ret['url'] = $this->URL(-1);
250 $ret['url_thumb'] = $this->URL($req['query']['imagesize']);
256 function URL($size, $provider = '/Images/Thumb')
259 return 'about:blank';
263 return $ff->baseURL . $provider . "/{$this->id}/{$this->filename}";
266 //$size = max(100, (int) $size);
267 //$size = min(1024, (int) $size);
269 $ff = HTML_FlexyFramework::get();
270 return $ff->baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
273 * size could be 123x345
277 function toHTML($size, $provider = '/Images/Thumb')
282 $sz = explode('x', $size);
287 $this->width = empty($sz[1]) ? $sx : $sz[1];
291 $ratio = $this->height/ ($this->width *1.0);
299 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
307 function setFromRoo($ar, $roo)
309 // not sure why we do this..
311 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
312 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
315 if ($this->find(true)) {
316 $roo->old = clone($this);
325 // FIXME - we should be checking perms here...
326 //if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser)) {
327 // $this->jerr("PERMISSION DENIED");
329 // this should be doign update
332 if (!isset($_FILES['imageUpload'])) {
333 return; // standard update...
336 if ( !$this->onUpload($this)) {
337 $this->jerr("File upload failed");
339 $roo->addEvent("ADD", $this, $this->toEventString());
341 $r = DB_DataObject::factory($this->tableName());
346 $roo->jok($r->toArray());
349 function toEventString()
352 //$p = DB_DataObject::factory($this->ontable);
354 // return "ERROR unknown table? {$this->ontable}";
358 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
359 //$p->toEventString();