<?php
/******************************************************************************/
//                                                                            //
//                             Smarty plugin                                  //
//                            @package Smarty                                 //
//							 @subpackage plugins                              //
//                        https://life-style.uz/                              //
//                   produced by Life Style, life-style.uz                    //
//                                                                            //
/******************************************************************************/

function smarty_function_fetch_banners($params, &$smarty) {
	
	$assign   = fetch_getParam('assign', $params, 'banners');
	$fields   = fetch_getParam('fields', $params, 'bannerId, title, alias, text, summary, url, fileId, orderBy');
	$status   = fetch_getParam('status', $params, 'V');
    $sectionId  = fetch_getParam('section', $params);
	$limit    = (int) fetch_getParam('limit', $params,  10);
    $orderBy  = fetch_getParam('orderBy', $params, 'orderBy ASC');

    $banners = null;

    if ($group = connect('banners_group')->usingJoin('banner_tracking', 'groupId')->select('*')->where('sectionId', $sectionId)->where('status', $status)->get('array')) {
        $banners = connect()->table('banners')->select($fields)->where('status', $status)->where('groupId', $group['groupId'])->orderBy($orderBy)->limit($limit)->getAll('array');
        foreach ($banners as $bannerId => $banner) {
            $banners[$bannerId]['images'] = prepareBanner($banner['bannerId']);
        }
    }
//    print_r($banners);
	$smarty->assign($assign, $banners);

}