class Product extends Persistent {
protected function _registerAtoms(){
parent::_registerAtoms();
$this->_setAtom("name", new Atom_String(128,3));
$this->_setAtom("shortname", new Atom_String(32,3)); // for menues
$this->_setAtom("type", new Atom_Reference("Reference.ProductType"));
$this->_setAtom("shortdesc", new Atom_Text(5120)); // 5kb
$this->_setAtom("fulldesc", new Atom_Text(61440)); // 60kb
$this->_setAtom("fbundle", new Atom_Boolean());
$this->_setAtom("fhidden", new Atom_Boolean());
$this->_setAtom("factive", new Atom_Boolean());
$this->_setAtom("price", new Atom_Numeric());
$this->_setAtom("img_height", new Atom_Integer());
$this->_setAtom("img_width", new Atom_Integer());
$this->_setAtom("img_file", new Atom_String(64,3));
$this->_setAtom("tumb_height", new Atom_Integer());
$this->_setAtom("tumb_width", new Atom_Integer());
$this->_setAtom("tumb_file", new Atom_String(64,3));
}
function getNewIdentifier(){
return Image::getNewIdentifier();
}
function getImagesDir(){
return Image::getImagesDir();
}
function asString(){
return $this->isSelected() ? $this->get("name") : parent::asString();
}
protected function _onMake(){
$this->set("factive",true);
$this->set("type",Glob::getDefaultProductType());
return parent::_onMake();
}
protected function _onDelete(){
if ( !parent::_onDelete() ) return false;
$this->dropImages();
return true;
}
function dropImage(){
if ( ($imagesDir = $this->getImagesDir()) === null ) return false;
$imagePath = $imagesDir."/".$this->get("img_file");
if ( $this->get("img_file") && file_exists($imagePath) )
unlink($imagePath);
$this->set("img_file","");
$this->set("img_width",0);
$this->set("img_height",0);
return true;
}
function dropTumb(){
if ( ($imagesDir = $this->getImagesDir()) === null ) return false;
$tumbPath = $imagesDir."/".$this->get("tumb_file");
if ( $this->get("tumb_file") && file_exists($tumbPath) )
unlink($tumbPath);
$this->set("tumb_file","");
$this->set("tumb_width",0);
$this->set("tumb_height",0);
return true;
}
function dropImages(){
return $this->dropImage() && $this->dropTumb();
}
function setImage(Image $image){
if ( ($imagesDir = $this->getImagesDir()) === null ) return false;
if ( !$this->get("img_file") ){
$fileId = $this->getNewIdentifier();
$this->set("img_file",$fileId.".".$image->getFileExtension());
}
$this->set("img_width",$image->getWidth());
$this->set("img_height",$image->getHeight());
return $image->save($imagesDir."/".$this->get("img_file"));
}
function setTumb(Image $image){
if ( ($imagesDir = $this->getImagesDir()) === null ) return false;
if ( !$this->get("tumb_file") ){
$fileId = $this->getNewIdentifier();
$this->set("tumb_file",$fileId.".".$image->getFileExtension());
}
$this->set("tumb_width",$image->getWidth());
$this->set("tumb_height",$image->getHeight());
return $image->save($imagesDir."/".$this->get("tumb_file"));
}
}