Extending the AJAX refresh to other pages

General Discussion about the commercial Enuuk Auction Platform
bamse
Posts: 220
Joined: Mon Feb 06, 2012 12:05 pm
Contact:

Extending the AJAX refresh to other pages

Post by bamse » Mon Apr 09, 2012 9:00 pm

By default phpauction does an AJAX refresh only for the home page and the offer details page of penny auctions. I am in the process of extending the refresh to other pages. Maybe somebody finds the following notes useful. If you spot something silly, please let me know as well.

Extension to other offer details pages is straightforward as one can use most of what is done for penny auctions.

To extend for offerDetailsStandard for instance, simply replace in themes/default/js/global.js:

Code: Select all

if(typeof(refreshTime)!="undefined"){
        if(/index|index2/.test(template)){
            refreshH = setInterval(refreshHome, refreshTime*1000);
         }else if(/offerDetailsPenny/.test(template) && active){
            refreshDP = setInterval(refreshDetails, refreshTime*1000);
         }
}
with

Code: Select all

if(typeof(refreshTime)!="undefined"){
        if(/index|index2/.test(template)){
            refreshH = setInterval(refreshHome, refreshTime*1000);
        }else if(/offerDetailsPenny/.test(template) && active){
            refreshDP = setInterval(refreshDetails, refreshTime*1000);
        }else if(/offerDetailsStandard/.test(template) && active){
            refreshDP = setInterval(refreshDetails, refreshTime*1000);
        }
}
and add "id=" (currentPrice, bestBidder,...) to tags in themes/default/offerDetailsStandard.php.

Haven't tried, but it should work in the same way for offer details pages of reverse and other auction types.

bamse
Posts: 220
Joined: Mon Feb 06, 2012 12:05 pm
Contact:

Re: Extending the AJAX refresh to other pages

Post by bamse » Mon Apr 09, 2012 9:12 pm

To extend refresh to the favourites page add to global.js (in the same if condition as above):

Code: Select all

}else if(/userFavourites/.test(template)){
            refreshF = setInterval(refreshFavourites, refreshTime*1000);
and add

Code: Select all

function refreshFavourites(){
    $.ajax({
        url: 'user/favourites/?v=JSON',
        cache:false,
        success: refreshFavouritesUpdate,
        dataType: "json"
    });
}
and

Code: Select all

function refreshFavouritesUpdate(JSONdata)
{
    list = JSONdata.favourites;
    for(var i in list){
        id      = list[i].id;
        price   = list[i].currentPriceText;

        DOMPrice = $('#auctionFavourite'+id+'Price');
        if(DOMPrice.length && price!=DOMPrice.text() ){
            DOMPrice.animate({backgroundColor: "#4ED"}, 1000)
                    .text(price)
                    .animate({backgroundColor: "white"}, 1000);
        }
    }
}
In class/Action/User.php->favourite()
replace

Code: Select all

$favourites[$k]['offers'] = $offers;
with

Code: Select all

$favourites[$k]['offers'] = $offers;
$favourites[$k]['currentPriceText'] = String::printMoney($offer->currentPrice,$this->context->siteOptions);
And add the id to themes/default/userFavourites.php as follows:

Code: Select all

<td class="price"><strong id="auctionFavourite<?=$v['id']?>Price"><?=String::printMoney($offer->currentPrice,$siteOptions)?></strong></td>
This should AJAX refresh the current bid/price on the favourite page. Needs to be extended to whatever changeable data are displayed on the favourite page (highest bidder,countdown,...)

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

Re: Extending the AJAX refresh to other pages

Post by RWAP » Mon Apr 09, 2012 9:24 pm

Just be careful with this - it can put a lot of load on your server.

Here, I separated out the code which is called by the Ajax routines, so that the relevant code appears in
class\Action\Json\xxx.php

This means only a short routine needs to be called on Ajax refresh.

The categories page also needs a refresh to be added.

bamse
Posts: 220
Joined: Mon Feb 06, 2012 12:05 pm
Contact:

Re: Extending the AJAX refresh to other pages

Post by bamse » Tue Apr 10, 2012 7:51 am

You mean to move stuff which is now in class/Action/User.php->favourite() (for instance) to class/Action/Json ? Did you also do that with the default refresh code of the homepage?

Is there an advantage of having the AJAX relevant code in a separate folder versus having a separate function for AJAX code such as in class/Action/Offer.php->bidDetails() ?

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

Re: Extending the AJAX refresh to other pages

Post by RWAP » Tue Apr 10, 2012 8:06 am

Yes I did mean moving that - it was done for the code which deals with the homepage refresh and offer details refresh.

There are several advantages which you can then use:

a) The Ajax code calls a smaller PHP file for the information required - so less processing time for the server.
b) You can amend the SQL calls and code, so that ONLY the information actually used by the Ajax call is passed back to the browser.

For example, the homepage refresh on my system now only calls the following PHP file:

Code: Select all

<?php
/**
 * @copyright Copyright (c) 2011, RWAP Services
 */
/**
* Action for homepage - JSON calls
*
* This action only contains a method, defaultAction, that gets some generic info
*
* @package Action
*/
class Action_Json_Index extends Core_Action
{
    /**
    * Default action
    */
    public function defaultAction()
    {
        $context=$this->context;
        $DB=$this->db;
        $siteOptions = $context->siteOptions;

        // We are updating Home page using JSON data.
        // LAST OFFERS
        $data['lastOffers'] = Offer::getJSONFastListFromDB($DB, 'where offers.active=1 order by startDate desc limit '.$siteOptions['maxLastCreatedItems']);

        // NEXT ENDING OFFERS
        $nextEndOffers = Offer::getJSONFastListFromDB($DB, 'where offers.active=1 order by endDate limit '.$siteOptions['maxNextEndingItems']);
		foreach ($nextEndOffers as $k=>$v){
			$nextEndOffers[$k]['endDateDelta'] = Date::secondsDifference($v['endDate']);
		}
		$data['nextEndOffers'] = $nextEndOffers;

		// FEATURED OFFERS
		$featuredOffers = Offer::getJSONFastListFromDB($DB, 'where featured like "%homePage%" and offers.active=1');
		foreach ($featuredOffers as $k=>$v){
			$featuredOffers[$k]['endDateDelta'] = Date::secondsDifference($v['endDate']);
		}
		$data['featuredOffers'] = $featuredOffers;
	    $context->addData($data);
    }
}
?>

bamse
Posts: 220
Joined: Mon Feb 06, 2012 12:05 pm
Contact:

Re: Extending the AJAX refresh to other pages

Post by bamse » Wed Apr 11, 2012 1:47 pm

Good points, thank you. Will try to optimize my ajax calls in this way.

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

Re: Extending the AJAX refresh to other pages

Post by RWAP » Wed Apr 11, 2012 1:55 pm

Don't forget that if you do follow this route, it then means that you can remove the code dealing with the v=JSON flag, from the main class\Action\Index.php etc.

bamse
Posts: 220
Joined: Mon Feb 06, 2012 12:05 pm
Contact:

Re: Extending the AJAX refresh to other pages

Post by bamse » Thu Apr 12, 2012 8:30 am

I moved the code out into separate methods, but am a bit stuck or confused by which actions/methods are called at which url.

I thought that if I have a file /class/Action/Abc.php which starts:

Code: Select all

class Action_Abc extends Core_Action
{                       
public function defaultAction()
...
public function def()
...
then the url mydomain/abc/ would call "defaultAction() and the url mydomain/abc/def/ would call "def()".

Is this not correct?

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

Re: Extending the AJAX refresh to other pages

Post by RWAP » Thu Apr 12, 2012 9:08 am

Yes, that is correct.

However, what I did was to create class\Action\JSON\abc.php

and then in class\Action\Core\Controller.php

Find:

Code: Select all

        if(!$url || $url == "index.php"){
            $this->_actionToCall = "Action_Index";
            $this->_context->parameters = $request;
        }else{  // url= "action/param1/param2/param3..."
            $chunks = explode("/",$url);
            $this->_actionToCall = "Action_".ucfirst(array_shift($chunks));
            $this->_context->parameters = array_merge($chunks, $request);
        }
Change this to:

Code: Select all

        if(!$url || $url == "index.php"){
            if (strtolower($request['v'])=='json'){
                $this->_actionToCall = "Action_Json_Index";
            } else {
            	$this->_actionToCall = "Action_Index";
            }
            $this->_context->parameters = $request;
        }else{  // url= "action/param1/param2/param3..."
            $chunks = explode("/",$url);
            if (strtolower($request['v'])=='json'){
                $this->_actionToCall = "Action_Json_".ucfirst(array_shift($chunks));
            } else {
            	$this->_actionToCall = "Action_".ucfirst(array_shift($chunks));
            }
            $this->_context->parameters = array_merge($chunks, $request);
        }

bamse
Posts: 220
Joined: Mon Feb 06, 2012 12:05 pm
Contact:

Re: Extending the AJAX refresh to other pages

Post by bamse » Thu Apr 12, 2012 12:09 pm

Oops, I had accidentally capitalized the filename as AbCd.php instead of Abcd.php. Now it works.

As for what you did in Core/Controller.php, is it necessary to redirect v=json calls to the new action if I specify the file directly in global.js? e.g.

Code: Select all

function refreshFavourites(){
    $.ajax({
        url: 'abc/?v=JSON',
        cache:false,
        success: refreshFavouritesUpdate,
        dataType: "json"
    });
}

This would use the method in Abc.php for AJAX refreshes, no?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest