<?php
namespace TourOperatorProBlock\block_tour_popular_in_month;
use TourOperatorPro\Factory as FactoryAlias;
use TourOperatorPro\Mvc\Model\ModelList;
use TourOperatorPro\Utilities\Utility as UtilityAlias;
use TourOperatorProFrontend\app\helpers\DestinationHelper;
use TourOperatorProFrontend\app\helpers\FrontendHelper;

class block_tour_popular_in_month_helper{
    public static function getList_tour($params){
        $db= FactoryAlias::getDBO();
        $filter_by_tour_type=$params->get('filter_by_tour_type',"");
        $filter_by_destination=$params->get('filter_by_destination',"");
        $query=$db->getQuery(true);
        $query->select('DISTINCT tour.*')
            ->from(UtilityAlias::getTableName('tour').' AS tour ')
            ->leftJoin(UtilityAlias::getTableName('tourproperty').' AS tour_property ON tour_property.tour_id=tour.id')
            ->where('tour.published=1')

        ;
        $list=$db->setQuery($query,0, 10)->loadObjectList();
        foreach ($list as &$tour){
            $query->clear()
                ->select('DISTINCT tourtype.*')
                ->from(UtilityAlias::getTableName('tourproperty').' AS tourproperty')
                ->leftJoin(UtilityAlias::getTableName('tourtype').' AS tourtype ON tourtype.id=tourproperty.object_id')
                ->where('tour_id='.(int)$tour->id)
                ->where('type='.$query->quote(FrontendHelper::getTourTypeKey()))
            ;
            $list_tour_type= $db->setQuery($query)->loadObjectList();
            $tour->list_tour_type=$list_tour_type;

            $query->clear()
                ->select('DISTINCT location.*')
                ->from(UtilityAlias::getTableName('tourproperty').' AS tourproperty')
                ->leftJoin(UtilityAlias::getTableName('location').' AS location ON location.id=tourproperty.object_id')
                ->where('tour_id='.(int)$tour->id)
                ->where('type='.$query->quote(FrontendHelper::getTourDestinationKey()))
            ;
            $list_destination= $db->setQuery($query)->loadObjectList();
            $tour->list_destination=$list_destination;

            $query->clear()
                ->select('DISTINCT discount.*')
                ->from(UtilityAlias::getTableName('tourproperty').' AS tourproperty')
                ->leftJoin(UtilityAlias::getTableName('discount').' AS discount ON discount.id=tourproperty.object_id')
                ->where('tour_id='.(int)$tour->id)
                ->where('type='.$query->quote(FrontendHelper::getTourDiscountKey()))
            ;
            $list_discount= $db->setQuery($query)->loadObjectList();
            $tour->discount= reset($list_discount);
        }
       return $list;
    }
}