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

use Mnv\Core\DB;

function smarty_function_fetch_donations($params, &$smarty) {
	global $fetchedArticles;

	$assign        = fetch_getParam('assign', $params, 'articles');
	$fields        = fetch_getParam('fields', $params, 'articleId, sectionId, orderBy, publishedOn, title, alias, url, summary, content, isFeatured, orderBy, typeContent, counter, addedBy, addedOn');
	$status        = fetch_getParam('status', $params, 'V');
	$limit         = (int) fetch_getParam('limit', $params);
	$perPage       = (int) fetch_getParam('perPage', $params, $limit);
	$isFeatured    = fetch_getParam('isFeatured', $params, NULL);
	
	$query         = fetch_getParam('query', $params, NULL);
	$match         = fetch_getParam('match', $params, 'any');
	$order         = fetch_getParam('order', $params, NULL);
	$orderBy       = fetch_getParam('orderBy', $params, $order);
	$section       = fetch_getParam('section', $params);
    $articleId     = fetch_getParam('articleId', $params);
    $userId        = fetch_getParam('userId', $params);
	$page          = (int) fetch_getParam('page', $params);
	$skip          = fetch_getParam('skip', $params, NULL);
	$noSubsections = (boolean) fetch_getParam('noSubsections', $params, false);

	$sectionInfo  = fetch_getParam('sectionInfo', $params, false);
	$parentsInfo  = fetch_getParam('parentsInfo', $params, false);
	
	$assignPagination = fetch_getParam('assignPagination', $params);
	$path             = fetch_getParam('path', $params);
	$seFriendly       = (boolean) fetch_getParam('seFriendly', $params);
	$pnFirst          = (int) fetch_getParam('pnFirst', $params, 0);
	$pnBefore         = (int) fetch_getParam('pnBefore', $params, 4);
	$pnAfter          = (int) fetch_getParam('pnAfter', $params, 4);
	$pnLast           = (int) fetch_getParam('pnLast', $params, 0);
	
	if (empty($assign)) {
		$smarty->_trigger_fatal_error('fetch_articles: "assign" must not be empty');
		return;
	}
	
	if (empty($path)) $path = array();

    /** calculate article popularity article fields */
    fetch_articles_calculatePopularity($orderBy);

	/** conditions */
    filterDonation($userId, $status, $isFeatured, $articleId, $orderBy, $query);

    if (!is_null($query) && is_array($path)) $path[] = "query=".urlencode($query);

    /** query conditions */
    fetch_articles_addQueryConditions($query, $match, $fields, $orderBy);

    /** add section conditions */
    fetch_articles_addSectionConditions($section, $noSubsections);
    /** skipping articles */
    fetch_articles_addSkipConditions($skip);
    /** checking article fields */

    fetch_articles_filterFields($fields);

	if ($page < 1) $page = 1;
	if ($perPage < 1) $perPage = 10;

	if (!is_null($query) && empty($query)) {
		$articles = array();
	} else {
        $articles = connect('articles')->select($fields)->pagination($perPage, $page)->getAll('array');
        $articles = prepareArticles($articles, $sectionInfo, $parentsInfo, true, 'gallery');
        foreach ($articles as $articleId => $article) {
            $fieldProperties = getProperties($article['articleId']);
            if (!empty($fieldProperties)) {
                foreach ($fieldProperties as $fieldId => $field) {
                    $articles[$articleId][$fieldId] = $field;
                }
            }

        }
	}

//    print_r(DB::init()->connect()->getQuery());
//	print_r($articles);
	$smarty->assign($assign, $articles);

	/* saving fetched articles for SKIP ability */
	if(empty($fetchedArticles[$assign])) {
        $fetchedArticles[$assign] = array();
    }
	if(!empty($articles)) {
        foreach ($articles as $article) {
            $fetchedArticles[$assign][] = $article['articleId'];
        }
    }
	
	if (!empty($assignPagination)) {
        /** calculate article popularity article fields */
        fetch_articles_calculatePopularity($orderBy);
        /** conditions */
        filterDonation($userId, $status, $isFeatured, $articleId, $orderBy, $query);

        if (!is_null($query) && is_array($path)) $path[] = "query=".urlencode($query);

        /** query conditions */
        fetch_articles_addQueryConditions($query, $match, $fields, $orderBy);
        /** add section conditions */
        fetch_articles_addSectionConditions($section, $noSubsections);
        /** skipping articles */
        fetch_articles_addSkipConditions($skip);
        /** checking article fields */

        fetch_articles_filterFields($fields);

        /** page numbering */
        $total = connect('articles')->countDistinct('articleId')->getValue();
		if (is_array($path)) {
			$path = SITE_URL.'/'.basename($_SERVER['PHP_SELF']).'?'.(!empty($path) ? implode('&', $path).'&' : '');
			$seFriendly = false;
		}
		$pagination = getPageNums($total, $page, $perPage, $pnFirst, $pnBefore, $pnAfter, $pnLast, $path, $seFriendly);
		$smarty->assign($assignPagination, $pagination);
	}
	
	return null;
}

function filterDonation($userId, $status, $isFeatured, $articleId, $orderBy, $query)
{
    global $gmNow;

    connect()->where('publishedOn','<=', $gmNow);
    connect()->where('typeContent', 'donations');
    if (!empty($userId)) connect()->where('addedBy', $userId);
    if (!empty($status)) connect()->where('status', $status);
    if (!is_null($isFeatured)) connect()->where('isFeatured',$isFeatured ? 1 : 0);
    if (!empty($articleId)) connect()->where('articleId', '!=', $articleId);
    if (is_null($orderBy)) {
        connect()->orderBy(is_null($query) ? 'publishedOn DESC' : '');
    } else {
        connect()->orderBy($orderBy);
    }
}

?>