Image Uploads and Errors on the Offer Form

Details of modifications which users can incorporate within Enuuk Auction Platform free of charge
Post Reply
RWAP
Site Admin
Posts: 748
Joined: Fri Jan 08, 2010 2:23 am
Location: Stoke-on-Trent
Contact:

Image Uploads and Errors on the Offer Form

Post by RWAP » Sat Jul 10, 2010 1:07 pm

One of the most frustrating issues for anyone listing items on an Enuuk platform based website can be that when creating a new offer, if you make errors in the offer form, you have to remember to choose the pictures to go with your item again.

We have made some changes to the way in which the offer form is processed in order to overcome this - the images are uploaded and stored on the server even if there are errors in the form.

To implement this:
In class\action\Offer.php

Find:

Code: Select all

    public function add()
    {
        if($this->isLoggedUserASeller()){
            $par = $this->context->parameters;
            if($par['action']=='add' ){
                $offerId = $this->processOfferForm();
Change this to:

Code: Select all

    public function add()
    {
        if($this->isLoggedUserASeller()){
            $par = $this->context->parameters;
            if ($par['action']=='correct') $par['action']='modify';
            if($par['action']=='add' || $par['action']=='modify'){
                $offerId = $this->processOfferForm();
Find:

Code: Select all

    private function processOfferForm()
    {
        $return = false;
        $par = $this->context->parameters;
Add below this:

Code: Select all

        $imageUploaded=false;
        if ($par['action']=='correct') $par['action']='modify';
Find:

Code: Select all

            $item->youtubeVideo = $par['youtubeVideo'];

            // default image for item | uploading the user file if a file is selected for upload
            if(isset($_FILES['defaultImage']['name'][0])){
                if($par['action'] == 'add'){
                    $defaultImage = Image::uploadFile($_FILES['defaultImage']['tmp_name'], $_FILES['defaultImage']['name'], $this->context->siteOptions['imageMaxUploadSize']);
                }else{
                    $defaultImage = Image::uploadFile($_FILES['defaultImage']['tmp_name'], $_FILES['defaultImage']['name'],$this->context->siteOptions['imageMaxUploadSize'], true, $offer->id);
                }
                if(!($defaultImage instanceof Image)){
                    $image = Image::getInstanceFromDB($this->db, Image::DEFAULT_IMAGE_ID);
                    $item->image = $image;
                }
            }else{
                //verify and update default image
                if($par['action'] == 'modify' && !isset($par['removeDefault'])){
                    $defaultImage = Image::getInstanceFromDB($this->db, $par['defaultImageOld']);
                }else{
                    $defaultImage = Image::getInstanceFromDB($this->db, Image::DEFAULT_IMAGE_ID);
                }
                $item->image = $defaultImage;
            }

            $images = array();
            $oldImage = array();
            $oldImg = array(); // these images are removed from database only
            //old images
            $totalImages = count($_FILES['image']['name']);

            for($i=0; $i<$totalImages; $i++){
                if(trim($_FILES['image']['name'][$i])!='' && trim($_FILES['image']['tmp_name'][$i])!=''){
                    $tmpName = $_FILES['image']['tmp_name'][$i];
                    $name = $_FILES['image']['name'][$i];
                    if($par['action'] == 'add'){
                        $img = Image::uploadFile($tmpName,$name,$this->context->siteOptions['imageMaxUploadSize']);
                    }else{
                        $img = Image::uploadFile($tmpName,$name,$this->context->siteOptions['imageMaxUploadSize'],true,$offer->id);

                    }

                    if($img instanceof Image){
                        $images[] = $img;
                        $tmpImg = Image::getInstanceFromDB($this->db,$par['imageOld'][$i]);
                        if($img->url == $tmpImg->url){
                            $oldImg[] = $tmpImg;
                        }else{
                            $oldImage[] = $tmpImg;
                        }

                    }else{
                        //retrieving old image
                        if($par['action'] == 'modify' && !in_array($par['imageOld'][$i],$par['imageToRemove'])){
                            $img = Image::getInstanceFromDB($this->db,$par['imageOld'][$i]);
                            $images[] = $img;
                        }
                    }
                }else{
                    if($par['action'] == 'modify' && !in_array($par['imageOld'][$i],array($par['imageToRemove']))){
                        $img = Image::getInstanceFromDB($this->db,$par['imageOld'][$i]);
                        if($img instanceof Image){
                            $images[] = $img;
                        }
                    }
                }
                unset($img);
            }

            //uploading user file if  selected
Change this to:

Code: Select all

            $item->youtubeVideo = $par['youtubeVideo'];

			$imageUploaded=true;  // Only try to upload images once
            $offer=$this->uploadImages ($offer);

            //uploading user file if  selected
Find:

Code: Select all

                    //renaming offer images
                    if($par['action'] == 'add'){
                        //renaming default image
                        if($item->image->id != Image::DEFAULT_IMAGE_ID){
                            $oldPath = $item->image->url;
                            $newName = Image::renameFile($oldPath,$offer->id);
                            $item->image->url = $newName;
                            $item->image->saveToDB($this->db);
                            unset($oldPath);
                            unset($newName);
                        }

                        //renaming image gallery
                        if($images){
                            foreach($images as $image){
                                $oldPath = $image->url;
                                $newName = Image::renameFile($oldPath,$offer->id);
                                $image->url = $newName;
                                $image->saveToDB($this->db);
                            }
                        }
                    }
                    if($par['action'] == 'modify'){
                        if(count($oldImage)){
                            foreach($oldImage as $k=>$img){
                                if($img instanceof Image){
                                $img->deletefromDB($this->db);
                                }
                            }
                        }
                        if(count($oldImg)){
                            foreach($oldImg as $k=>$img){
                                if($img instanceof Image){
                                $img->deletefromDBOnly($this->db);
                                }
                            }
                        }

                        //removing the images selected for removal
                        if(isset($par['imageToRemove'])){
                            foreach($par['imageToRemove'] as $imgID){
                                $img = Image::getInstanceFromDB($this->db,$imgID);
                                if($img instanceof Image){
                                    $img->deleteFromDB($this->db);
                                }
                            }
                        }
                        //remove the default image if marked for removal
                        if(isset($par['removeDefault'])){
                            $img = Image::getInstanceFromDB($this->db,$par['removeDefault']);
                            if($img instanceof Image ){
                                $img->deleteFromDB($this->db);
                            }                            
                        }
                    }

                }else{ //if can't save offer, delete everything
                    if(file_exists($offer->file)){
                        @unlink($offer->file);
                    }
                    if($par['action'] !='modify'){
                        foreach($images as $image){
                            $image->deleteFromDB($this->db);
                        }
                        
                        if($defaultImage instanceof Image){
                            $defaultImage->deleteFromDB($this->db);
                        }

                        $item->deleteFromDB($this->db);
                    }
                }
Change this to:

Code: Select all

		            //renaming offer images
		            if($par['action'] == 'add' || preg_match('/\/_/',$item->image->url)){
		                //renaming default image
		                if($item->image->id != Image::DEFAULT_IMAGE_ID){
		                    $newName = Image::renameFile($item->image->url,$offer->id);
		                    $item->image->url = $newName;
		                    $item->image->saveToDB($this->db);
		                    unset($oldPath);
		                    unset($newName);
		                }
		            }
		            $images=$item->images;
		            //renaming image gallery
		            if($images){
		                foreach($images as $image){
		                    $oldPath = $image->url;
		                    if($par['action'] == 'add' || preg_match('/\/_/',$oldPath)){
		                        $newName = Image::renameFile($oldPath,$offer->id);
			                    $image->url = $newName;
			                    $image->saveToDB($this->db);
		                        unset ($newName);
		                    }
		                }
		            }

                }else{ //if can't save offer, delete everything
                    if(file_exists($offer->file)){
                        @unlink($offer->file);
                    }
                }
Find:

Code: Select all

        }catch(Exception $e){
            unset($par['id']);
            if($par['action'] == 'add'){
                if($item instanceof Item){
                $item->deleteFromDB($this->db);
                }
                if(count($images)){
                    foreach($images as $image){
                        $image->deleteFromDB($this->db);
                    }
                }
                if($defaultImage instanceof Image){
                    $defaultImage->deleteFromDB($this->db);
                }

            }
Change this to:

Code: Select all

        }catch(Exception $e){
            if ($offer instanceof Offer)
            {
	        	if (!$imageUploaded) $offer=$this->uploadImages($offer);
                $par['image'] = $offer->item->image;
                $par['images']= $offer->item->images;
                $par['action']='correct';
                $par['id']=$offer->id;
            }
Find:

Code: Select all

                $par['customFields'] = $customFields;
                $this->context->addData($par);
            }
        }
        return $return;
    }
Below this add:

Code: Select all

    public function uploadImages($offer)
    {
    	$par = $this->context->parameters;
        if ($par['action']=='correct') $par['action']=='modify';
        if ($offer instanceof Offer && $_FILES)
        {
            $defaultImage = $offer->item->image;
            $item=$offer->item;
		    // default image for item | uploading the user file if a file is selected for upload
		    if(isset($_FILES['defaultImage']['name'][0])){
		        if($par['action'] == 'add'){
		        $defaultImage = Image::uploadFile($_FILES['defaultImage']['tmp_name'],$_FILES['defaultImage']['name'],$this->context->siteOptions,false,null,true);
		        }else{
		        $defaultImage = Image::uploadFile($_FILES['defaultImage']['tmp_name'],$_FILES['defaultImage']['name'],$this->context->siteOptions,true,$offer->id,true);
		        }
		        if(!($defaultImage instanceof Image)){
		            $image = Image::getInstanceFromDB($this->db, Image::DEFAULT_IMAGE_ID);
		            $item->image = $image;
		        }
		    }else{
		        //verify and update default image
		        if($par['action'] == 'modify' && !isset($par['removeDefault'])){
		            $defaultImage = Image::getInstanceFromDB($this->db, $par['defaultImageOld']);
		        }else{
		        	$defaultImage = Image::getInstanceFromDB($this->db, Image::DEFAULT_IMAGE_ID);
		        }
		    }

		    $images = array();
		    $oldImage = array();
		    $oldImg = array(); // these images are removed from database only
		    //old images
		    $maxImages=$this->context->siteOptions['maxImagesInGallery'];
		    for($i=0; $i<$maxImages; $i++){
		        if(trim($_FILES['image_'.$i]['name'][0])!='' && trim($_FILES['image_'.$i]['tmp_name'][0])!=''){
		            $tmpName = $_FILES['image_'.$i]['tmp_name'][0];
		            $name = $_FILES['image_'.$i]['name'][0];
		            if($par['action'] == 'add'){
		                $img = Image::uploadFile($tmpName,$name,$this->context->siteOptions);
		            }else{
		                $img = Image::uploadFile($tmpName,$name,$this->context->siteOptions,true,$offer->id);
		            }

		            if($img instanceof Image){
		                $images[] = $img;
		                $tmpImg = Image::getInstanceFromDB($this->db,$par['imageOld'][$i]);
		                if($img->url == $tmpImg->url){
		                    $oldImg[] = $tmpImg;
		                }else{
		                    $oldImage[] = $tmpImg;
		                }

		            }else{
		                //retrieving old image
		                if($par['action'] == 'modify' && !in_array($par['imageOld'][$i],$par['imageToRemove'])){
		                    $img = Image::getInstanceFromDB($this->db,$par['imageOld'][$i]);
		                    $images[] = $img;
		                }
		            }
		        }else{
		            if($par['action'] == 'modify' && !in_array($par['imageOld'][$i],array($par['imageToRemove']))){
		                $img = Image::getInstanceFromDB($this->db,$par['imageOld'][$i]);
		                if($img instanceof Image){
		                    $images[] = $img;
		                }
		            }
		        }
		        unset($img);
		    }

            // saving objects
            if($defaultImage instanceof Image){
                if($defaultImage->saveToDB($this->db)){
                        $item->image = $defaultImage;
                }
            }

            if($par['action'] == 'modify'){
                $item->emptyImages();
            }

            foreach($images as $image){
                if($image instanceof Image){
                    if($image->saveToDB($this->db)){
                        $item->addImage($image);
                    }
                }
            }

            //renaming offer images
            if($par['action'] == 'add' || preg_match('/\/_/',$item->image->url)){
                //renaming default image
                if($item->image->id != Image::DEFAULT_IMAGE_ID){
                    $newName = Image::renameFile($item->image->url,$offer->id);
                    $item->image->url = $newName;
                    $item->image->saveToDB($this->db);
                    unset($oldPath);
                    unset($newName);
                }
            }
            $images=$item->images;
            //renaming image gallery
            if($images){
                foreach($images as $image){
                    $oldPath = $image->url;
                    if($par['action'] == 'add' || preg_match('/\/_/',$oldPath)){
                        $newName = Image::renameFile($oldPath,$offer->id);
	                    $image->url = $newName;
	                    $image->saveToDB($this->db);
                        unset ($newName);
                    }
                }
            }

            if($par['action'] == 'modify'){
                if(count($oldImage)){
                    foreach($oldImage as $k=>$img){
                        if($img instanceof Image){
                        $img->deletefromDB($this->db);
                        }
                    }
                }
                if(count($oldImg)){
                    foreach($oldImg as $k=>$img){
                        if($img instanceof Image){
                        $img->deletefromDBOnly($this->db);
                        }
                    }
                }

                //removing the images selected for removal
                if(isset($par['imageToRemove'])){
                    foreach($par['imageToRemove'] as $imgID){
                        $img = Image::getInstanceFromDB($this->db,$imgID);
                        if($img instanceof Image){
                            $img->deleteFromDB($this->db);
                        }
                    }
                }
                //remove the default image if marked for removal
                if(isset($par['removeDefault'])){
                    $img = Image::getInstanceFromDB($this->db,$par['removeDefault']);
                    if($img instanceof Image ){
                        $img->deleteFromDB($this->db);
                    }
                }
            }
	        $item->user=$this->context->loggedUser;
            $item->saveToDB($this->db);
	        if (!$offer->id)
	        {
                $offer->startPrice=($par['startPrice']?$par['startPrice']:0); // Just temporary until get full figure
                $offer->currentPrice=($par['currentPrice']?$par['currentPrice']:0); // Just temporary until get full figure
                $offer->shipConditions=($par['shipConditions']?$par['shipConditions']:'Seller');
                $offer->shipCosts=($par['shipCosts']?$par['shipCosts']:0);
                $offer->shipInternational=!!$par['shipInternational'];
                $offer->inWebstore=!!$par['inWebstore'];
                $offer->active=0;
                $offer->datePending=0;
                $offer->adult=0;
                $offer->payMethods  = ($par['payMethods']?$par['payMethods']:array(1));
            	$offer->categories  = array(($par['categoryId']?$par['categoryId']:1)); //At the moment, just 1 category
                $offer->item=$item;
                $offer->savetoDB($this->db);
                //$offer->categories=array();
                //$offer->payMethods=array();
	        }
            return $offer;
        }
    }


RWAP
Site Admin
Posts: 748
Joined: Fri Jan 08, 2010 2:23 am
Location: Stoke-on-Trent
Contact:

Re: Image Uploads and Errors on the Offer Form

Post by RWAP » Sat Jul 10, 2010 1:55 pm

Then in themes\default\offerForm.php

Find:

Code: Select all

                <div class="c17 multiLabel">
                    <?
                        if($action=='modify'){
                    ?>
Change this to:

Code: Select all

                <div class="c17">
                    <?
                        if($action=='modify' || $action=='correct'){
                    ?>
Find:

Code: Select all

                        for($i=1;$i<=$maxImagesInGallery;$i++){
                            if($action == 'modify'){
                                    $image = $images[$i-1];
                            }
                            ?>
Change this to:

Code: Select all

                        for($i=1;$i<=$maxImagesInGallery;$i++){
                            if($action == 'modify' || $action=='correct'){
                                    $image = $images[$i-1];
                            }
                            ?>
Find:

Code: Select all

                            <? if($action == 'modify' && $image){
                            ?>
                                <img src="<?=$relativePath?><?=$image->url?>" alt=""  width="150"/>
                            <?
Change this to:

Code: Select all

                            <? if(($action == 'modify' || $action=='correct') && $image){
                            ?>
                                <img src="<?=$relativePath?><?=$image->url?>" alt=""  width="150"/>
                            <?
Find:

Code: Select all

                <div class="c17 multiLabel">
                    <?
                        if($action=='modify' && $fileOld !=''){
                    ?>
Change this to:

Code: Select all

                <div class="c17 multiLabel">
                    <?
                        if(($action=='modify' || $action=='correct') && $fileOld !=''){
                    ?>
Find:

Code: Select all

                            <?
                                if($action == 'modify'){
                                    if($video || $video!=''){

                            ?>
Change this to:

Code: Select all

                            <?
                                if($action == 'modify' || $action=='correct'){
                                    if($video || $video!=''){

                            ?>

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest