Versions Compared

Key

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

...

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
$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.