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)) {
113 while (false !== ($fn = readdir($dh))) {
114 if (substr($fn, 0, strlen($b)) == $b) {
115 unlink($d. '/'. $fn);
124 * onUpload (singlely attached image to a table)
127 function onUploadWithTbl($tbl, $fld)
129 if ( $tbl->__table == 'Images') {
130 return; // not upload to self...
132 if (empty($_FILES['imageUpload']['tmp_name']) ||
133 empty($_FILES['imageUpload']['name']) ||
134 empty($_FILES['imageUpload']['type'])
139 $image = DB_DataObject::factory('Images');
140 $image->get($tbl->$fld);
141 $image->beforeDelete();
145 $image = DB_DataObject::factory('Images');
146 $image->onid = $tbl->id;
147 $image->ontable = $tbl->__table;
148 $image->filename = $_FILES['imageUpload']['name'];
149 $image->mimetype = $_FILES['imageUpload']['type'];
151 if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) {
155 $tbl->$fld = $image->id;
161 function onUpload($ctrl)
164 if (empty($_FILES['imageUpload']['tmp_name']) ||
165 empty($_FILES['imageUpload']['name']) ||
166 empty($_FILES['imageUpload']['type'])
168 $this->err = "Missing file details";
173 $this->beforeDelete();
175 if ( empty($this->ontable)) {
176 $this->err = "Missing ontable";
180 if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) {
181 // then its an upload
182 $img = DB_DataObject::factory('Images');
183 $img->onid = $this->onid;
184 $img->ontable = $this->ontable;
185 $img->imgtype = $this->imgtype;
188 while ($img->fetch()) {
189 $img->beforeDelete();
197 require_once 'File/MimeType.php';
198 $y = new File_MimeType();
199 $this->mimetype = $_FILES['imageUpload']['type'];
200 if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps..
201 $inf = pathinfo($_FILES['imageUpload']['name']);
202 $this->mimetype = $y->fromExt($inf['extension']);
206 $ext = $y->toExt(trim((string) $this->mimetype ));
208 $this->filename = empty($this->filename) ?
209 $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext);
213 if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) {
221 * return a list of images for an object, optionally with a mime regex.
222 * eg. '%/pdf' or 'image/%'
224 function gather($obj, $mime_like='')
226 DB_DataObject::debugLevel(1);
227 if (empty($obj->id)) {
231 $c->ontable = $obj->tableName();
233 if (!empty($mime_like)) {
234 $c->whereAdd("mimetype LIKE '". $c->escape($mime_like) ."'");
237 return $c->fetchAll();
241 function toRooArray($req = array()) {
242 if (empty($req['query']['imagesize'])) {
243 return $this->toArray();
245 $ret= $this->toArray();
246 $ret['url'] = $this->URL($req['query']['imagesize']);
252 function URL($size, $provider = '/Images/Thumb')
255 return 'about:blank';
259 return $ff->baseURL . $provider . "/{$this->id}/{$this->filename}";
261 $size = max(100, (int) $size);
262 $size = min(1024, (int) $size);
264 $ff = HTML_FlexyFramework::get();
265 return $ff->baseURL . $provider . "/$size/{$this->id}/{$this->filename}";
268 * size could be 123x345
272 function toHTML($size, $provider = '/Images/Thumb')
277 $sz = explode('x', $size);
282 $this->width = empty($sz[1]) ? $sx : $sz[1];
285 $ratio = $this->height/ ($this->width *1.0);
291 return '<img src="' . $this->URL($size, $provider) . '" width="'. $sx . '" height="'. $sy . '">';
299 function setFromRoo($ar, $roo)
301 // not sure why we do this..
303 // if imgtype starts with '-' ? then we set the 'old' (probably to delete later)
304 if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) {
307 if ($this->find(true)) {
308 $roo->old = clone($this);
317 // FIXME - we should be checking perms here...
318 //if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser)) {
319 // $this->jerr("PERMISSION DENIED");
321 // this should be doign update
324 if (!isset($_FILES['imageUpload'])) {
325 return; // standard update...
328 if ( !$this->onUpload($this)) {
329 $this->jerr("File upload failed");
331 $roo->addEvent("ADD", $this, $this->toEventString());
333 $r = DB_DataObject::factory($this->tableName());
338 $roo->jok($r->toArray());
341 function toEventString()
344 //$p = DB_DataObject::factory($this->ontable);
346 // return "ERROR unknown table? {$this->ontable}";
350 return $this->filename .' - on ' . $this->ontable . ':' . $this->onid;
351 //$p->toEventString();