AbanteCart Community

AbanteCart Development => API Development => Topic started by: susinthiran on February 28, 2018, 06:01:23 AM

Title: Facing issues while collecting banner images via API
Post by: susinthiran on February 28, 2018, 06:01:23 AM
Hi,

I was trying to get banner images via mobile API and created an API for that.
Based on my understanding in  blocks/banner_block.php  it is calling func_get_arg(0) to get instance_id(It is returning exact instance_id what it has been saved in block_layouts_table for banner_block).

When I try it from my PHP file it is saying Argument 0 is not passed to func_get_arg.I think controller is playing role here.

Is it possible to get same instance_id what we get in banner_blocks.php file into my file?

Thanks in Advance.
Title: Re: Facing issues while collecting banner images via API
Post by: abolabo on February 28, 2018, 08:51:57 AM
what about to get it from hook?

for example:

    public function onControllerBlocksBannerBlock_InitData(){
       $instance_id = $this->baseObject->getInstance();
      //var_dump($instance_id);
    }

is it the same id?
Title: Re: Facing issues while collecting banner images via API
Post by: susinthiran on March 01, 2018, 07:10:56 AM
Hi,

Thank you for reply.

Actually when I tried, I am getting error as call to member function getInstance() on null.
I can't find out the way to rectify it.

I have added my file here.
If i could get instance_id I can get banner_list.
Sorry I can't understand hook concept and how to use it.

class ControllerApiCommonBlocksBannerBlock extends AControllerAPI {

    protected $registry;

    public function onControllerBlocksBannerBlock_InitData()
    {
        $this->registry = Registry::getInstance();
        $instance_id = $this->baseObject->getInstance();
        //var_dump($instance_id);
    }

    public function get() {
        $this->extensions->hk_InitData($this,__FUNCTION__);


        $request = $this->rest->getRequestParams();
        $controller="pages/index/home";
        $this->layout->buildPageData($controller);
        $instance_id=""; //have to get through hooking

        $response=$this->getBlockContent($instance_id);
        $this->extensions->hk_UpdateData($this,__FUNCTION__);

        $this->rest->setResponseData( $response );
        $this->rest->sendResponse( 200 );
    }

    public function getBlockContent($instance_id) {
        $block_info = $this->layout->getBlockDetails($instance_id);
        $custom_block_id = $block_info['custom_block_id'];
        $this->loadModel('extension/banner_manager');
        $results = $this->model_extension_banner_manager->getBanners($custom_block_id);
        $banners = array();
        if($results){
            $rl = new AResource('image');
            foreach($results as $row){
                if($row['banner_type']==1){ // if graphic type
                    /**
                     * @var array
                     */
                    $row['images'] = $rl->getResourceAllObjects('banners',$row['banner_id']);
                    //add click registration wrapper to each URL
                    //NOTE: You can remove below line to use tracking javascript instead. Javascript tracks HTML banner clicks
                    $row['target_url'] = $this->html->getURL('r/extension/banner_manager/click', '&banner_id='.$row['banner_id'], true);

                } else {
                    $row['description'] = html_entity_decode($row['description']);
                }
                $banners[] = $row;
            }
        }
        $output = array(
            'content' => $banners
        );

        return $output;
    }

}


Title: Re: Facing issues while collecting banner images via API
Post by: abolabo on March 01, 2018, 09:32:30 AM
mm..
All banners placed inside banner-block. This block can be placed in any placeholder of layout (see admin->design->layouts ).
There to many dynamical parameters. What if somebody had changed layout? Instance of block will be changed.

I think you should to
1. define is some banner-block enabled in some layout.
2. get it's block_id from blocks_layouts database table.
3. get banner block content from table block_descriptions
4. get all banner IDs from it's content (or banner group)
5. get all images of all banners


Title: Re: Facing issues while collecting banner images via API
Post by: susinthiran on March 02, 2018, 12:10:26 AM
Hi,

Thank you,It is good idea indeed :).

I knew that instance_id is changed each time when layout has been saved.
I may need to write query for that to fetch instance_id by passing block_id.
That's why I chose to go with instance_id as it is fetched from blocks_layouts table on calling buildPageData() function.(That's how it works in web interface.)
However,I fetched banner images via Admin function.(The function which is used to populate list in banner_manager interface.)

can't we get instance_id from another file through hooking?