/**
 * Smarty {widget_product_gallery} function plugin
 *
 * Type:     function
 * Name:     microgalery
 * Date:     January 11 2013
 * Update:     March 20 2014
 * Purpose:  
 * Output:
 * @author   Gerits Aurelien
 * @author   Sire Sam (sire-sam.be)
 * @link http://www.magix-cms.com
 * @version  1.0
 * @param array
 * @param Smarty
 * @return string
 *
 */
function smarty_function_widget_product_gallery($params, $template)
{
    $ModelSystem = new magixglobal_model_system();
    $ModelConstructor = new magixglobal_model_constructor();
    $ModelCatalog = new frontend_model_catalog();
    // Set and load data
    $current = $ModelSystem->setCurrentId();
    $conf = array('context' => 'product-gallery');
    $data = $ModelCatalog->getData($conf, $current);
    $html = null;
    if ($data != null) {
        $pattern['default'] = patternMicroGallery();
        $pattern['custom'] = null;
        if ($params['pattern']) {
            $pattern['custom'] = is_array($params['pattern']) ? $params['pattern'] : patternMicroGallery($params['pattern']);
        }
        $pattern['global'] = $ModelConstructor->mergeHtmlPattern($pattern['default'], $pattern['custom']);
        magixcjquery_debug_magixfire::magixFireTable('pattern', $pattern);
        magixcjquery_debug_magixfire::magixFireTable('pattern global', $pattern['global']);
        $i = 0;
        $items['html'] = null;
        foreach ($data as $row) {
            $i++;
            // *** Construit donées de l'item en array avec clée nominative unifiée ('name' => 'monname,'descr' => '<p>ma descr</p>,...)
            $item['data'] = $ModelCatalog->setItemData($row, $current);
            // Configuration de la structure HTML de l'item
            $pattern['item'] = $ModelConstructor->setItemPattern($pattern['global'], $i);
            if ($pattern['item']['img']['classLink'] != '') {
                $pattern['item']['img']['classLink'] = ' class="' . $pattern['item']['img']['classLink'] . '"';
            }
            // remise à zero du compteur si élément est le dernier de la ligne
            if ($pattern['item']['is_last'] == 1) {
                $i = 0;
            }
            $items['html'] .= $pattern['item']['item']['before'];
            $items['html'] .= '<a href="' . $item['data']['imgSrc']['medium'] . '" rel="productGallery" title="Agrandir"' . $pattern['item']['img']['classLink'] . '>';
            $items['html'] .= '<img src="' . $item['data']['imgSrc']['small'] . '" alt="Galery" />';
            $items['html'] .= '</a>';
            $items['html'] .= $pattern['item']['item']['after'];
        }
    }
    // *** ouput
    if ($items['html'] != null) {
        $html = isset($params['title']) ? $params['title'] : '';
        $html .= $pattern['global']['container']['before'];
        $html .= isset($params['htmlPrepend']) ? $params['htmlPrepend'] : null;
        $html .= $items['html'];
        $html .= isset($params['htmlAppend']) ? $params['htmlAppend'] : null;
        $html .= $pattern['global']['container']['after'];
    }
    return $html;
}