Monday, July 27, 2009

problem solved

after weeks of trying to figure out how to get the image in a field from p4a displayed, i finally figured it out. in p4a there are two folders, an uploads folder which contains a tmp folder. when the image is uploaded, it is supposed to put the image in the tmp folder then in the uploads folder.

my problem was that the images were only going in the tmp folder and not in the uploads folder. so after numerous nights, i finally stayed up and was determined to make it work before i went to bed and that i did. it was alot of work but it payed off. i now know why it happened and how to fix it.

after reading the forum and different posts several times, with the toolbar, it does the moving of the image on saveRow(). I had created my own field and therefore wasnt using the save button on the toolbar. so i had to intercept the field when i was doing the save to the db and move the file myself.

the code is posted below:

$filename = $this->newProductImage->getNewValue();
$uploaded_tmp_filename = P4A_UPLOADS_DIR.$this->newProductImage->getNewValue(1);
$uploaded_filename = P4A_UPLOADS_DIR."/".$this->newProductImage->getNewValue(0);

$sequence = $this->imageSequence->getNewValue();
$product_id = $this->dbSourceProduct->fields->oid->getNewValue();

if( file_exists( $uploaded_tmp_filename ) ) {

$sql = "
insert into Image
values ('', '$filename', $sequence, $product_id);
";//exit;
if( P4A_DB::singleton()->query( $sql ) ){
if(!copy( $uploaded_tmp_filename, $uploaded_filename )){
$this->showMessage( "Error uploading file", "error" );
} else {
$this->showMessage("Image was successfully added", "applied");
//$this->doRefreshOriginalPage();
}
}else {
$this->showMessage("Image was not added", "error");
}
} else {
$this->showMessage("File not uploaded", "error");
}

No comments: