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


function smarty_function_fetch_user_comments($params, &$smarty){

	$assign    = fetch_getParam('assign', $params, 'comments');
	$fields    = fetch_getParam('fields', $params, '*');
	$status    = fetch_getParam('status', $params, 'approved');
	$orderBy   = fetch_getParam('orderBy', $params, 'addedOn ASC');
	$limit     = (int) fetch_getParam('limit', $params, 300);
	$articleId = (int) fetch_getParam('articleId', $params);
	$userId  = (int) fetch_getParam('userId', $params, 0);

	if(empty($assign)){
		$smarty->_trigger_fatal_error('fetch_comments: "assign" must not be empty');
		return;
	}
	
	/* conditions */
	if(!empty($status)) connect()->where('status',  $status);
//	if(!empty($articleId)) connect()->where('articleId', '=', $articleId);
	connect()->where('userId', '=', $userId);

    if ($comments =  connect()->table('comments')->select($fields)->orderBy($orderBy)->limit($limit)->getAll('array')) {
        $comments = buildTreeFromArray($comments, 'answerId', 'commentId', 'answer');
        foreach ($comments as $comment) {
            if (!empty($comment['answer'])) {
                $comment['count'] = count($comment['answer']);
            }
        }

//        print_r($comments);
    } else {
        $comments = null;
    }
	$smarty->assign($assign, $comments);

	return null;
}

?>