width , $this->height) = $imgs; } $this->filesize = filesize($file); $this->created = date('Y-m-d H:i:s'); if (empty($this->mimetype)) { require_once 'File/MimeType.php'; $y = new File_MimeType(); $this->mimetype = $y->fromFilename($file); } if (empty($this->filename)) { $this->filename = basename($file); } //DB_DataObject::debugLevel(1); if (!$this->id) { $this->insert(); } else { $this->update(); } $f = $this->getStoreName(); $dest = dirname($f); if (!file_exists($dest)) { $oldumask = umask(0); mkdir($dest, 0770, true); umask($oldumask); } copy($file,$f); // fill in details.. /* thumbnails */ // $this->createThumbnail(0,50); return true; } /** * Calculate target file name * * @return - target file name */ function getStoreName() { $opts = PEAR::getStaticProperty('Pman', 'options'); $fn = preg_replace('/[^a-z0-9\.]+/i', '_', $this->filename); return implode( '/', array( $opts['storedir'], '_images_', date('Y/m', strtotime($this->created)), $this->id . '-'. $fn )); } /** * deletes all the image instances of it... * * */ function beforeDelete() { $fn = $this->getStoreName(); if (file_exists($fn)) { unlink($fn); } // delete thumbs.. $b = basename($fn); $d = dirname($fn); if (file_exists($d)) { $dh = opendir($d); while (false !== ($fn = readdir($dh))) { if (substr($fn, 0, strlen($b)) == $b) { unlink($d. '/'. $fn); } } } } /** * onUpload (singlely attached image to a table) */ function onUploadWithTbl($tbl, $fld) { if ( $tbl->__table == 'Images') { return; // not upload to self... } if (empty($_FILES['imageUpload']['tmp_name']) || empty($_FILES['imageUpload']['name']) || empty($_FILES['imageUpload']['type']) ) { return false; } if ($tbl->$fld) { $image = DB_DataObject::factory('Images'); $image->get($tbl->$fld); $image->beforeDelete(); $image->delete(); } $image = DB_DataObject::factory('Images'); $image->onid = $tbl->id; $image->ontable = $tbl->__table; $image->filename = $_FILES['imageUpload']['name']; $image->mimetype = $_FILES['imageUpload']['type']; if (!$image->createFrom($_FILES['imageUpload']['tmp_name'])) { return false; } $old = clone($tbl); $tbl->$fld = $image->id; $tbl->update($old); } // direct via roo... function onUpload($ctrl) { if (empty($_FILES['imageUpload']['tmp_name']) || empty($_FILES['imageUpload']['name']) || empty($_FILES['imageUpload']['type']) ) { $this->err = "Missing file details"; return false; } if ($this->id) { $this->beforeDelete(); } if ( empty($this->ontable)) { $this->err = "Missing ontable"; return false; } if (!empty($this->imgtype) && $this->imgtype[0] == '-' && !empty($this->onid)) { // then its an upload $img = DB_DataObject::factory('Images'); $img->onid = $this->onid; $img->ontable = $this->ontable; $img->imgtype = $this->imgtype; $img->find(); while ($img->fetch()) { $img->beforeDelete(); $img->delete(); } } require_once 'File/MimeType.php'; $y = new File_MimeType(); $this->mimetype = $_FILES['imageUpload']['type']; if (in_array($this->mimetype, array('text/application', 'application/octet-stream'))) { // weird tyeps.. $inf = pathinfo($_FILES['imageUpload']['name']); $this->mimetype = $y->fromExt($inf['extension']); } $ext = $y->toExt(trim((string) $this->mimetype )); $this->filename = empty($this->filename) ? $_FILES['imageUpload']['name'] : ($this->filename .'.'. $ext); if (!$this->createFrom($_FILES['imageUpload']['tmp_name'])) { return false; } return true; } /** * return a list of images for an object, optionally with a mime regex. * eg. '%/pdf' or 'image/%' */ function gather($obj, $mime_like='') { //DB_DataObject::debugLevel(1); if (empty($obj->id)) { return array(); } $c = clone($this); $c->ontable = $obj->tableName(); $c->onid = $obj->id; if (!empty($mime_like)) { $c->whereAdd("mimetype LIKE '". $c->escape($mime_like) ."'"); } return $c->fetchAll(); } function toRooArray($req = array()) { // echo '
';print_r($req);exit; $ret= $this->toArray(); if (!empty($req['query']['imagesize'])) { $baseURL = isset($req['query']['imageBaseURL']) ? $req['query']['imageBaseURL'] : false; $ret['url'] = $this->URL(-1, '/Images/Download',$baseURL); $ret['url_view'] = $this->URL(-1, '/Images',$baseURL); if (!empty($req['query']['imagesize'])) { $ret['url_thumb'] = $this->URL($req['query']['imagesize'], '/Images/Thumb',$baseURL); } } return $ret; } /** * URL - create a url for the image. * size - use -1 to show full size. * provier = baseURL + /Images/Thumb ... use '/Images/' for full * * */ function URL($size , $provider = '/Images/Thumb', $baseURL=false) { if (!$this->id) { return 'about:blank'; } $ff = HTML_FlexyFramework::get(); $baseURL = $baseURL ? $baseURL : $ff->baseURL ; if ($size < 0) { return $baseURL . $provider . "/{$this->id}/{$this->filename}"; } //-- max? //$size = max(100, (int) $size); //$size = min(1024, (int) $size); return $baseURL . $provider . "/$size/{$this->id}/{$this->filename}"; } /** * size could be 123x345 * * */ function toHTML($size, $provider = '/Images/Thumb') { $sz = explode('x', $size); $sx = $sz[0]; //var_dump($sz); if (!$this->id) { $this->height = $sx; $this->width = empty($sz[1]) ? $sx : $sz[1]; $sy = $this->width ; } if (empty($sz[1])) { $ratio = $this->height/ ($this->width *1.0); $sy = $ration * $sx; } else { $sy = $sz[1]; } // create it? return ''; } function setFromRoo($ar, $roo) { // not sure why we do this.. // if imgtype starts with '-' ? then we set the 'old' (probably to delete later) if (!empty($ar['imgtype']) && !empty($ar['ontable']) && !empty($ar['onid']) && ($ar['imgtype'][0] == '-')) { $this->setFrom($ar); $this->limit(1); if ($this->find(true)) { $roo->old = clone($this); } } if (!empty($ar['_copy_from'])) { $copy = DB_DataObject::factory('Images'); $copy->get($ar['_copy_from']); $this->setFrom($copy->toArray()); $this->setFrom($ar); $this->createFrom($copy->getStoreName()); $roo->addEvent("ADD", $this, $this->toEventString()); $r = DB_DataObject::factory($this->tableName()); $r->id = $this->id; $roo->loadMap($r); $r->limit(1); $r->find(true); $roo->jok($r->toArray()); } // FIXME - we should be checking perms here... //if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser)) { // $this->jerr("PERMISSION DENIED"); // } // this should be doign update $this->setFrom($ar); if (!isset($_FILES['imageUpload'])) { return; // standard update... } if ( !$this->onUpload($this)) { $this->jerr("File upload failed"); } $roo->addEvent("ADD", $this, $this->toEventString()); $r = DB_DataObject::factory($this->tableName()); $r->id = $this->id; $roo->loadMap($r); $r->limit(1); $r->find(true); $roo->jok($r->toArray()); } function toEventString() { //$p = DB_DataObject::factory($this->ontable); //if (!is_$p) { // return "ERROR unknown table? {$this->ontable}"; // } //$p->get($p->onid); return $this->filename .' - on ' . $this->ontable . ':' . $this->onid; //$p->toEventString(); } }