Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

To manage resources use resource library. It is designed to

...

Code Block
languagephp
themeDJango
<?php
if ( !defined ( 'DIR_CORE' )) {
  	header ( 'Location: static_pages/' );
}

$layout = new ALayoutManager();
$block_data = array(
    'block_txt_id' => 'slideshow',
    'controller' => 'blocks/slideshow',
    'templates' => array(
        array(
            'parent_block_txt_id' => 'content_top',
            'template' => 'blocks/slideshow.tpl',
        ),
	),
);
$layout->saveBlock( $block_data );


- create uninstall.php

Code Block
languagephp
themeDJango
<?php
if ( !defined ( 'DIR_CORE' )) {
  	header ( 'Location: static_pages/' );
}

// delete block
$layout = new ALayoutManager();
$layout->deleteBlock('slideshow');


- create image folder. Here we will have o icon.png – extension icon 
slides/slide1.jpg, slides/slide2.jpg, slides/slide3.jpg – default slides

- create main.php. Here we will define extension resources

Code Block
languagephp
themeDJango
<?php
if ( !defined ( 'DIR_CORE' )) {
  	header ( 'Location: static_pages/' );
}

$controllers = array(
    'storefront' => array(
        'blocks/slideshow',
	),
    'admin' => array(
        'pages/blocks/slideshow',
	),
);

$models = array(
    'storefront' => array(
        'slideshow/slideshow',
	),
    'admin' => array(
        'slideshow/slideshow',
	),
);

$languages = array(
    'storefront' => array(),
    'admin' => array(
        'slideshow/slideshow',
	),
);

$templates = array(
    'storefront' => array(
        'blocks/slideshow.tpl',
	),
    'admin' => array(
        'pages/blocks/slideshow_list.tpl',
        'pages/blocks/slideshow_form.tpl',
	),
);

 

Our extension will have

  • 2 controllers – one for admin to manage slides, one for storefront to prepare data for slideshow
  • One language file
  • A couple of templates
  • Two models. Model for storefront has only methods to retrieve data. Model for admin has methods to add/edit/delete items

...

Code Block
languagephp
themeDJango
if (! defined ( 'DIR_CORE' )) {
  	header ( 'Location: static_pages/' );
}
class ModelSlideshowSlideshow extends Model {

    protected $model_tool_image;
    protected $slide;
    protected $rm;

    public function __construct($registry) {
        parent::__construct($registry);
        $this->model_tool_image = $this->registry->get('model_tool_image');
  		$this->slide = array(
        		'x' => $this->registry->get('config')->get('slideshow_x'),
        		'y' => $this->registry->get('config')->get('slideshow_y'),
  		);
        $this->rm = new AResource('image');
  	}

    public function getSlides() {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "slides");
  		$result = $query->rows;
  		foreach ( $result as $key => $img ) {
              	$result[$key]['slide_img'] = $this->_getSlideUrl($img['slide_img']);
        }

        	return $result;
	}

    public function getSlide($slide_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "slides WHERE slide_id = '".(int)$slide_id."' ");
  		$query->row['preview'] = $this->_getSlideUrl($query->row['slide_img']);
        	return $query->row;
	}

  	private function _getSlideUrl( $img ) {

        	if(!is_file(DIR_RESOURCE . $img)){
              	if(!is_file(DIR_EXT . 'slideshow/image/' . $img)){
                    	$result = $this->model_tool_image->resize('no_image.jpg', $this->slide['x'], $this->slide['y']);
              	}else{
                    	$result = HTTP_EXT.'slideshow/image/' . $img;
              	}
        	}else{

            $id = $this->rm->getIdFromHexPath( str_replace($this->rm->getTypeDir(), '', $img) );
            $result = $this->rm->getResourceThumb($id, $this->slide['x'], $this->slide['y']);
            if ( !$result )
              		$result = $this->model_tool_image->resize('no_image.jpg', $this->slide['x'], $this->slide['y']);
        	}

        	return $result;
  	}

}

This model has two methods to fetch data
also note _getSlideUrl method. Since we have default images we should check if image passed in function is exist in resource directory. If so we try to get resource id from its path and create a thumbnail with width/height defined in extension settings

- Now add model for admin - extensions\slideshow\admin\model\slideshow\slideshow.php

Code Block
languagephp
themeDJango
<?php
if (! defined ( 'DIR_CORE' )) {
  	header ( 'Location: static_pages/' );
}
class ModelSlideshowSlideshow extends Model {

    protected $model_tool_image;
    protected $slide;
    protected $rm;

    public function __construct($registry) {
        parent::__construct($registry);
        $this->model_tool_image = $this->registry->get('model_tool_image');
  		$this->slide = array(
        		'x' => $this->registry->get('config')->get('slideshow_x'),
        		'y' => $this->registry->get('config')->get('slideshow_y'),
  		);
        $this->rm = new AResource('image');
  	}

    public function getSlides() {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "slides");
  		$result = $query->rows;
  		foreach ( $result as $key => $img ) {
              	$result[$key]['slide_img'] = $this->_getSlideUrl($img['slide_img']);
        }

        	return $result;
	}

    public function addSlide($data) {
        $this->db->query(
            "INSERT INTO " . DB_PREFIX . "slides
            SET slide_img = '".$this->db->escape( $data['slide_img'] )."',
                slide_url = '".$this->db->escape( $data['slide_url'] )."'  ");
	}

    public function editSlide($slide_id, $data) {
        $this->db->query(
            "UPDATE " . DB_PREFIX . "slides
            SET slide_img = '".$this->db->escape( $data['slide_img'] )."',
                slide_url = '".$this->db->escape( $data['slide_url'] )."'
            WHERE slide_id = '".(int)$slide_id."' ");
	}

    public function getSlide($slide_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "slides WHERE slide_id = '".(int)$slide_id."' ");
  		$query->row['preview'] = $this->_getSlideUrl($query->row['slide_img']);
        	return $query->row;
	}

    public function deleteSlide($slide_id) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "slides WHERE slide_id = '".(int)$slide_id."' ");
	}

  	private function _getSlideUrl( $img ) {

        	if(!is_file(DIR_RESOURCE . $img)){
              	if(!is_file(DIR_EXT . 'slideshow/image/' . $img)){
        	        	$result = $this->model_tool_image->resize('no_image.jpg', $this->slide['x'], $this->slide['y']);
              	}else{
                    	$result = HTTP_EXT.'slideshow/image/' . $img;
              	}
        	}else{

            $id = $this->rm->getIdFromHexPath( str_replace($this->rm->getTypeDir(), '', $img) );
            $result = $this->rm->getResourceThumb($id, $this->slide['x'], $this->slide['y']);
            if ( !$result )
              		$result = $this->model_tool_image->resize('no_image.jpg', $this->slide['x'], $this->slide['y']);
        	}

        	return $result;
  	}

}

...

Code Block
languagephp
themeDJango
if ( !defined ( 'DIR_CORE' )) {
  	header ( 'Location: static_pages/' );
}

class ControllerPagesBlocksSlideshow extends AController {

  	private $error = array();
  	public $data = array();

  	public function main() {

          //init controller data
        $this->extensions->hk_InitData($this);

	  $this->loadLanguage('extension/extensions');
	  $this->loadLanguage('slideshow/slideshow');
	  $this->loadModel('slideshow/slideshow');
        $this->document->setTitle( $this->language->get('heading_title') );

        	$this->view->assign('error_warning', $this->error['warning']);
        	$this->view->assign('success', $this->session->data['success']);
        	if (isset($this->session->data['success'])) {
              	unset($this->session->data['success']);
        	}

	  $this->document->initBreadcrumb( array (
   	       	'href'  	=> $this->html->getSecureURL('index/home'),
   	       	'text'  	=> $this->language->get('text_home'),
  	        	'separator' => FALSE
        	 ));
        $this->document->addBreadcrumb(array(
            'href' => $this->html->getSecureURL('extension/extensions/'.$this->session->data['extension_filter']),
            'text' => $this->language->get('heading_title'),
            'separator' => ' :: '
        ));
        	$this->document->addBreadcrumb(array(
            'href' => $this->html->getSecureURL('extension/extensions/edit', '&extension=slideshow'),
   	     'text' => $this->language->get('slideshow_name'),
            'separator' => ' :: '
        ));
        	$this->document->addBreadcrumb( array (
   	       	'href'  	=> $this->html->getSecureURL('blocks/slideshow'),
   	       	'text'  	=> $this->language->get('slides_title'),
  	        	'separator' => ' :: '
        	 ));

        	$this->data['images'] = $this->model_slideshow_slideshow->getSlides();

        	$this->data['delete'] = $this->html->getSecureURL('blocks/slideshow/delete', '&slide_id=%ID%' );
        	$this->data['update'] = $this->html->getSecureURL('blocks/slideshow/update', '&slide_id=%ID%' );
        	$this->data['insert'] = $this->html->getSecureURL('blocks/slideshow/insert' );

        	$this->data['button_remove'] = $this->html->buildButton(array(
              	'text' => $this->language->get('button_remove'),
              	'style' => 'button2',
        	));
        	$this->data['button_edit'] = $this->html->buildButton(array(
              	'text' => $this->language->get('button_edit'),
              	'style' => 'button2',
        	));
        	$this->data['button_add_slide'] = $this->html->buildButton(array(
              	'text' => $this->language->get('button_add_slide'),
              	'style' => 'button1',
        	));

        $this->view->batchAssign(  $this->language->getASet() );
        	$this->view->batchAssign( $this->data );

        	$this->processTemplate('pages/blocks/slideshow_list.tpl' );

          //update controller data
        $this->extensions->hk_UpdateData($this);
  	}

  	public function insert() {

        //init controller data
        $this->extensions->hk_InitData($this);

	  $this->loadLanguage('slideshow/slideshow');
        $this->loadModel('slideshow/slideshow');
	  $this->document->setTitle($this->language->get('heading_title'));

	  if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
    	    $this->request->post['slide_img'] = html_entity_decode($this->request->post['slide_img'], ENT_COMPAT, 'UTF-8');
            $this->model_slideshow_slideshow->addSlide($this->request->post);
            $this->session->data['success'] = $this->language->get('text_success');
              	$this->redirect( $this->html->getSecureURL('blocks/slideshow') );
	  }
	  $this->_getForm();

        //update controller data
        $this->extensions->hk_UpdateData($this);
  	}

  	public function update() {

        //init controller data
        $this->extensions->hk_InitData($this);

	  $this->loadLanguage('slideshow/slideshow');
        $this->loadModel('slideshow/slideshow');
	  $this->document->setTitle($this->language->get('heading_title'));
	  if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
            $this->request->post['slide_img'] = html_entity_decode($this->request->post['slide_img'], ENT_COMPAT, 'UTF-8');
              	$this->model_slideshow_slideshow->editSlide($this->request->get['slide_id'], $this->request->post);
              	$this->session->data['success'] = $this->language->get('text_success');
              	$this->redirect( $this->html->getSecureURL('blocks/slideshow') );
        	}
	  $this->_getForm();

   	 //update controller data
        $this->extensions->hk_UpdateData($this);
  	}

  	public function delete() {

        //init controller data
        $this->extensions->hk_InitData($this);

	  $this->loadLanguage('slideshow/slideshow');
        $this->loadModel('slideshow/slideshow');
	  $this->model_slideshow_slideshow->deleteSlide($this->request->get['slide_id']);
        $this->session->data['success'] = $this->language->get('text_success');
        	$this->redirect( $this->html->getSecureURL('blocks/slideshow') );

        	//update controller data
        $this->extensions->hk_UpdateData($this);
  	}

  	private function _getForm() {

        	$this->view->assign('error_warning', $this->error['warning']);
        	$this->view->assign('success', $this->session->data['success']);
        	if (isset($this->session->data['success'])) {
              	unset($this->session->data['success']);
        	}
        	$this->view->batchAssign(  $this->language->getASet() );

	  $this->data = array();
        	$this->data['error'] = $this->error;
        	$this->data['cancel'] = $this->html->getSecureURL('blocks/slideshow');

        	$this->data['heading_title'] = $this->language->get('slides_title');

        	$this->document->initBreadcrumb( array (
   	       	'href'  	=> $this->html->getSecureURL('index/home'),
   	       	'text'  	=> $this->language->get('text_home'),
  	        	'separator' => FALSE
        	 ));
        $this->document->addBreadcrumb(array(
            'href' => $this->html->getSecureURL('extension/extensions/'.$this->session->data['extension_filter']),
            'text' => $this->language->get('heading_title'),
            'separator' => ' :: '
        ));
        	$this->document->addBreadcrumb(array(
            'href' => $this->html->getSecureURL('extension/extensions/edit', '&extension=slideshow'),
	        'text' => $this->language->get('slideshow_name'),
            'separator' => ' :: '
        ));
        	$this->document->addBreadcrumb( array (
   	       	'href'  	=> $this->html->getSecureURL('blocks/slideshow'),
   	       	'text'  	=> $this->language->get('slides_title'),
  	        	'separator' => ' :: '
        	 ));


        	if (isset($this->request->get['slide_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  	        	$item_info = $this->model_slideshow_slideshow->getSlide($this->request->get['slide_id']);
	  }

        	$fields = array('slide_img', 'slide_url');
        	foreach ( $fields as $f ) {
              	if (isset ( $this->request->post [$f] )) {
                    	$this->data [$f] = $this->request->post [$f];
              	} elseif (isset($item_info[$f])) {
                    	$this->data[$f] = $item_info[$f];
              	} else {
                    	$this->data[$f] = '';
              	}
        	}

        if (!isset($this->request->get['slide_id'])) {
              	$this->data['action'] = $this->html->getSecureURL('blocks/slideshow/insert' );
              	$this->data['form_title'] = $this->language->get('text_insert') . $this->language->get('text_slide');
        	} else {
              	$this->data['action'] = $this->html->getSecureURL('blocks/slideshow/update', '&slide_id=' . $this->request->get['slide_id'] );
              	$this->data['form_title'] = $this->language->get('text_edit') . $this->language->get('text_slide');
        	}

        $form = new AForm('ST');

        	$this->document->addBreadcrumb( array (
              	'href'  	=> $this->data['action'],
              	'text'  	=> $this->data['form_title'],
              	'separator' => ' :: '
        	 ));

        	$form->setForm(array(
        		'form_name' => 'frm',
              	'update' => '',
  		));

        $this->data['form']['id'] = 'frm';
        $this->data['form']['form_open'] = $form->getFieldHtml(array(
        		'type' => 'form',
        		'name' => 'frm',
        		'action' => $this->data['action'],
  		));
        $this->data['form']['submit'] = $form->getFieldHtml(array(
        		'type' => 'button',
        		'name' => 'submit',
        		'text' => $this->language->get('button_save'),
        		'style' => 'button1',
  		));
        	$this->data['form']['cancel'] = $form->getFieldHtml(array(
        		'type' => 'button',
        		'name' => 'cancel',
        		'text' => $this->language->get('button_cancel'),
        		'style' => 'button2',
  		));

        $this->data['form']['fields']['img'] = $form->getFieldHtml(
            array(
                'type' => 'hidden',
                'name' => 'slide_img',
                'value' => htmlspecialchars($this->data['slide_img'], ENT_COMPAT, 'UTF-8'),
        		)
        );
        $this->data['form']['fields']['url'] = $form->getFieldHtml(
            array(
                'type' => 'input',
                'name' => 'slide_url',
                'value' => $this->data['slide_url'],
                'attr' => 'class="large-field"',
        		)
        );

        $resources_scripts = $this->dispatch(
            'responses/common/resource_library/get_resources_scripts',
            array(
                'object_name' => '',
                'object_id' => '',
                'types' => 'image',
                'mode' => 'url',
            )
        );
        $this->data['resources_scripts'] = $resources_scripts->dispatchGetOutput();
        if ( isset($this->request->get['slide_id']) && is_file( DIR_RESOURCE . $item_info['slide_img']) ) {
            $rm = new AResourceManager();
            $rm->setType('image');
            $id = $rm->getIdFromHexPath( str_replace($rm->getTypeDir(), '', $item_info['slide_img']) );
            $thumb = $rm->getResourceThumb(
                $id,
                $this->config->get('config_image_product_width'),
                $this->config->get('config_image_product_height')
            );
            $this->view->assign( 'thumb', $thumb);
        	}

        $this->view->batchAssign( $this->data );
        $this->processTemplate('pages/blocks/slideshow_form.tpl' );
  	}


}

This controller is work as usual application controller
Main method – fetch all slides and give ability to add/edit/delete slides
Insert – insert new slide
Update – edit slide image/url
Delete – delete slide


Lets look in details how we include resource library scripts

Code Block
languagephp
themeDJango
$resources_scripts = $this->dispatch(
            'responses/common/resource_library/get_resources_scripts',
            array(
                'object_name' => '',
                'object_id' => '',
                'types' => 'image',
                'mode' => 'url',
            )
        );
        $this->data['resources_scripts'] = $resources_scripts->dispatchGetOutput();

$this->dispatch – call controller with arguments and return dispatch object $resources_scripts->dispatchGetOutput(); - return controller output, which is passed to template later

This controller use two templates

  • slideshow_list.tpl – show all slides
  • slideshow_form.tpl – create/update form

Now lets create storefront controller
extensions\slideshow\storefront\controller\blocks\slideshow.php

Code Block
languagephp
themeDJango
<?php
if (! defined ( 'DIR_CORE' )) {
  	header ( 'Location: static_pages/' );
}

class ControllerBlocksSlideshow extends AController {
  	public function main() {

        //init controller data
        $this->extensions->hk_InitData($this);
        $this->loadModel('slideshow/slideshow');

        if ( !$this->registry->has('jcycle') ) {
            $this->document->addScript( $this->view->templateResource('/javascript/jquery/jquery.cycle.js') );
            $this->registry->set('jcycle', true);
        }
        	$this->document->addStyle(
              	array(
                    	 'href' => $this->view->templateResource('/stylesheet/slideshow.css'),
                    	 'rel' => 'stylesheet',
                    	 'media' => 'screen',
              	)
        	);

        	$this->view->assign('images', $this->model_slideshow_slideshow->getSlides());

        $this->processTemplate('blocks/slideshow.tpl');

        //init controller data
        $this->extensions->hk_UpdateData($this);

  	}
}

...

Code Block
languagephp
themeDJango
$this->registry->set('jcycle', true);


Used to avoid loading multiple instances of scripts in order you will have several slideshows on one page

Storefront template include simple slideshow html.

You can take a look at full code by downloading our free template. This extension is a part of template.