<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);

/**
 *
 * @package  VehicleManager
 * @copyright 2012 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru); Rob de Cleen(rob@decleen.com);
 * Homepage: http://www.ordasoft.com
  * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @version: 3.0 Free
 *
 **/

$path = JPATH_BASE. '/' .'components'. '/' .'com_vehiclemanager'. '/' .'functions.php';
if (!file_exists($path)){
  echo "To display the featured books You have to install VehicleManager first<br />"; exit;
} else{
  require_once($path);
}
$database = JFactory::getDBO();
$doc = JFactory::getDocument();
$url = JURI::base();
$mosConfig_live_site = $GLOBALS['mosConfig_live_site'] = substr_replace($url, '', -1, 1);
$doc->addStyleSheet(JURI::base(true) . '/' . 'components' . '/' . 'com_vehiclemanager' .
    '/' . 'includes' . '/' . 'vehiclemanager.css');

$doc->addStyleSheet(JURI::base(true) . '/' . 'components' . '/' . 'com_vehiclemanager' .
    '/' . 'includes' . '/' . 'animate.css');

$doc->addStyleSheet('//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css');



if(checkJavaScriptIncludedVEH("jQuerVEH-1.9.0.js") === false ) {
  $doc->addScript(JURI::root(true) . '/components/com_vehiclemanager/lightbox/js/jQuerVEH-1.9.0.js');

    $doc->addScript(JURI::root(true) . '/components/com_vehiclemanager/includes/jquery-ui.js');
}

if(checkJavaScriptIncludedVEH("jQuerVEH.viewportchecker.min.js") === false ) {
  $doc->addScript(JURI::root(true) . '/components/com_vehiclemanager/includes/jQuerVEH.viewportchecker.min.js');
}
?>

<noscript>Javascript is required to use Vehicle Manager <a href="http://ordasoft.com/Vehicle-Manager/vehiclemanager-basic-and-pro-feature-comparison.html">Vehicle manager software Joomla extension for Auto Dealers, Auto Dealership Companies and other Enterprises selling Vehicles</a>, <a href="http://ordasoft.com/Vehicle-Manager/vehiclemanager-basic-and-pro-feature-comparison.html">Vehicle Manager create own vehicle web portal for sell, rent,  buy vehicles</a></noscript>

<?php
$menu = new JSite;
$menu->getMenu();
require_once ( JPATH_BASE . '/' .'components'. '/' .'com_vehiclemanager'. '/' .'functions.php' );
$mosConfig_absolute_path = $GLOBALS['mosConfig_absolute_path'] = JPATH_SITE;
global $vehiclemanager_configuration;

$database = JFactory::getDBO();
      // load language
      $languagelocale = "";
       global $langContent;
      $query = "SELECT l.title, l.lang_code, l.sef ";
      $query .= "FROM #__vehiclemanager_const_languages as cl ";
      $query .= "LEFT JOIN #__vehiclemanager_languages AS l ON cl.fk_languagesid=l.id ";
      $query .= "LEFT JOIN #__vehiclemanager_const AS c ON cl.fk_constid=c.id ";
      $query .= "GROUP BY  l.title";
      $database->setQuery($query);
      $languages = $database->loadObjectList();
      $lang = JFactory::getLanguage();
      foreach ($lang->getLocale() as $locale) {
        foreach ($languages as $language) {
          if (strtolower($locale) == strtolower($language->title)
              || strtolower($locale) == strtolower($language->lang_code) ) {
            $mosConfig_lang = $locale;
            $languagelocale = $language->lang_code;
            break;
          }
        }
      }
      if ($languagelocale == '') {
        $languagelocale = "en-GB";
        $mosConfig_lang = "en-GB";
      }
      // Set content language
     
      // $langContent = substr($languagelocale, 0, 2);
      $query = "SELECT c.const, cl.value_const ";
      $query .= "FROM #__vehiclemanager_const_languages as cl ";
      $query .= "LEFT JOIN #__vehiclemanager_languages AS l ON cl.fk_languagesid=l.id ";
      $query .= "LEFT JOIN #__vehiclemanager_const AS c ON cl.fk_constid=c.id ";
      $query .= "WHERE l.lang_code = '$languagelocale'";
      $database->setQuery($query);
      $langConst = $database->loadObjectList();
      foreach ($langConst as $item) {
        if(!defined($item->const) ) 
         define($item->const, $item->value_const); // $database->quote()
      }

      $query = "SELECT l.lang_code "
          . "FROM #__languages as l " ;
      $database->setQuery($query);
      $languages = $database->loadObjectList();



     foreach ($lang->getLocale() as $locale) {

          foreach ($languages as $language) {


              if (strtolower($locale) == strtolower($language->lang_code)
                  || strtolower(str_replace(array('_','-'), '', $locale)) 
                  == strtolower((str_replace(array('_','-'), '', $language->lang_code)))
                  
                  ) {

                  $langContent = $language->lang_code;


                  break;
              }
          }
      }

//Common parameters
$show_image = $params->get('image');
$image_height = $params->get('image_height');
$item_width = $params->get('item_width');
$container_image_width = $params->get('container_image_width');
$height_auto = $params->get('height_auto', false);

$show_hits = $params->get('hits');
$price = $params->get('price', 0);
$status = $params->get('status', 0);
$listing_status_on = $params->get('listing_status', 0);
$location = $params->get('location', 0 );
$features = $params->get('features', 0);
$description = $params->get('description', 0);
$view_listing = $params->get('view_listing', 0);
$categories = $params->get('categories', 0);
//Individual parameters
$count = intval($params->get('count',1));
$id = JRequest::getInt('id');
//Display type
$displaytype = $params->get('displaytype', 0);
//Advanced parameters
$class_suffix = $params->get('moduleclass_sfx', 1);
$Itemid_from_params = $params->get('ItemId');
$g_words = $params->get('words','');
$sortnewby  = $params->get ('sortnewby', 0);
$image_source_type = $params->get('image_source_type');
//realestate

if (!function_exists('searchPicture_vehiclemanager')){
function searchPicture_vehiclemanager ($image_source_type,$imageURL){
			
	global $vehiclemanager_configuration;
	
            switch ($image_source_type) {
                case "0": $img_height = $vehiclemanager_configuration['fotomain']['high'];
                    $img_width = $vehiclemanager_configuration['fotomain']['width'];
                    break;
                case "1": $img_height = $vehiclemanager_configuration['foto']['high'];
                    $img_width = $vehiclemanager_configuration['foto']['width'];
                    break;
                case "2": $img_height = $vehiclemanager_configuration['fotogallery']['high'];
                    $img_width = $vehiclemanager_configuration['fotogallery']['width'];
                    break;					
                default:$img_height = $vehiclemanager_configuration['fotomain']['high'];
                    $img_width = $vehiclemanager_configuration['fotomain']['width'];
                    break;
            }
			       
            $watermark = ($vehiclemanager_configuration['watermark']['show'] == 1) ? true : false;  
            $imageURL1 = vm_picture_thumbnail($imageURL, $img_height, $img_width, $watermark);			
												
            $imageURL = "/components/com_vehiclemanager/photos/" . $imageURL1;
            return $imageURL;
            
    }  
}

switch($sortnewby) {
case 0:
    $sql_orderby_query = "id"; 
    $sql_where_query =  "";  // Last Added
    break;
case 1:
case 2:
    $sql_orderby_query  = "hits";     // Top (most popular)
    $sql_where_query = "";
    break;
case 3:
    $sql_orderby_query  = "rand()";     // Random (most popular)
    $sql_where_query = "";
    break;
case 4:
    $sql_orderby_query  = "rand()";     // Random (most popular)
    $sql_where_query = "";
    $similar = '';
    break;
}


if (isset($langContent)){
    $lang = $langContent;
    // $query = "SELECT lang_code FROM #__languages WHERE sef = '$lang'";
    // $database->setQuery($query);
    // $lang = $database->loadResult();
    $lang = " and (v.language like 'all' or v.language like '' or v.language like '*' or v.language is null or v.language like '$lang')
            AND (c.language like 'all' or c.language like '' or c.language like '*' or c.language is null or c.language like '$lang') ";
}else{
    $lang = "";
}

$database->SetQuery("SELECT id 
                    FROM #__menu 
                    WHERE link 
                    LIKE'%option=com_vehiclemanager%' 
                    AND params 
                    LIKE '%back_button%'");
$Itemid_from_db = $database->loadResult();
if ($Itemid_from_params!=''){
    $Itemid = $Itemid_from_params;
} else{
    $Itemid = $Itemid_from_db;
}
$sql_published = "published = 1";

$s = vmLittleThings::getWhereUsergroupsCondition("c");

$cat_sel = "";
$vehicle_ids = "";


if(isset($similar) ){
if($id == '') return;

    $query = "SELECT v.vtitle, v.listing_status, v.id, v.image_link, v.hits,  c.id AS catid, c.title AS cattitle, v.price, v.published, v.priceunit,
    v.vlocation,v.city,v.country, v.maker, v.vmodel, v.year, v.mileage,v.description, v.listing_type".

            ' FROM #__vehiclemanager_vehicles AS v, #__vehiclemanager_main_categories AS c , #__vehiclemanager_categories AS hc ' .
            ' WHERE v.id <> ' . (int) $id .$lang. " and c.section='com_vehiclemanager' " .
            " AND c.published='1' " .
            " AND hc.iditem=v.id " .
            " AND hc.idcat = c.id " .
            " AND v.published='1' " .
            " AND v.approved='1' ";
        $query_flag = true;

        if ($params->get('optMarque') != 0) {
            $query .= ' and v.maker =(select maker from #__vehiclemanager_vehicles t where t.id = ' . (int) $id . ')';
        }

        if ($params->get('optCategorie') != 0) {
            $query .= ' and hc.idcat in (select idcat from #__vehiclemanager_categories t2 where t2.iditem = ' . (int) $id . ')';
        }

        if ($params->get('optCarburant') != 0) {
            $query .= ' and v.fuel_type =(select fuel_type from #__vehiclemanager_vehicles t3 where t3.id = ' . (int) $id . ')';
        }



}
else
{
  $query = "SELECT v.vtitle,v.listing_status, v.id, v.image_link, v.hits,  c.id AS catid, c.title AS cattitle, v.price, v.published, v.priceunit,
    v.vlocation,v.city,v.country, v.maker, v.vmodel, v.year, v.mileage,v.description, v.listing_type
            \n FROM #__vehiclemanager_vehicles AS v
            \n LEFT JOIN #__vehiclemanager_categories AS hc ON hc.iditem=v.id
            \n LEFT JOIN #__vehiclemanager_main_categories AS c ON c.id=hc.idcat
            \n WHERE (".$s.") $lang ".$sql_where_query."";
    $query_flag = true;
    if ((isset($count) AND $count > 0) AND $cat_sel == "" AND $sql_published != "" AND $vehicle_ids == ""){
        $query .= " AND c.".$sql_published." AND v.".$sql_published; 
    }   
}



$query .= $cat_sel.$vehicle_ids." AND v.approved=1 GROUP BY v.id ORDER BY ".$sql_orderby_query." DESC LIMIT 0 , ".$count.";";  

if ($query_flag){
    $database->setQuery($query);
    $vehicles= $database->loadObjectList();
}


if($vehicles!=="" && $vehicles!==Null && count($vehicles)>0){
  require JModuleHelper::getLayoutPath('mod_vehiclemanager_featured_free_j3', $params->get('layout'));
}
?>
<!-- <div style="text-align: center;"><a href="http://ordasoft.com" style="font-size: 10px;">Powered by OrdaSoft!</a></div> -->
