<?php
class CD_VEHICLES
{
function CD_VEHICLES()
{
}
//init function
function Init()
{
//choose needed one
switch($GLOBALS["CD_CLASS_FUNCTION"]->FreeDirs[0])
{
//for models of make
case "make":
//get make selected
$make = $this->GetRowById((int)$GLOBALS["CD_CLASS_FUNCTION"]->FreeDirs[1]);
//if found - show info, if not - show all makes
if(!empty($make[0]['Make']))
$Summary = $this->ShowModelsForMake($make[0]['Make']);
else
$Summary = $this->ShowAllMakes();
break;
//for search
case "search":
//get vehicle selected
$vehicle = $this->GetRowById((int)$GLOBALS["CD_CLASS_FUNCTION"]->FreeDirs[1]);
//if found - perform search, if not - show all makes
if(!empty($vehicle[0]['Make']))
$Summary = $this->performSearch($vehicle[0]);
else
$Summary = $this->ShowAllMakes();
break;
//for all makes
default:
$Summary = $this->ShowAllMakes();
break;
}
return $Summary;
}
//show all makes
function ShowAllMakes()
{
//find all makes
$ProjectData = CD_FUNCTION::_GetSelectQuery(sprintf("select `RowID`, `Make` FROM `%svehicles` GROUP BY `Make` ORDER BY `Make`", $GLOBALS["CD_CFG"]["tablePrefix"]));
//generate template
$Summary = CD_FUNCTION::_GetManager('vehicles/makes', $this, $ProjectData);
return $Summary;
}
//show models for make
function ShowModelsForMake($make)
{
//find all makes
$ProjectData = $this->GetVehiclesForMake($make);
//generate template for make
$Summary = CD_FUNCTION::_GetManager('vehicles/make', $this, array_slice($ProjectData, 0, 1));
//generate template for models
//header
$GLOBALS["CD_CLASS_TEMPLATE"]->SetTemplate('vehicles/models/header');
$Summary .= $GLOBALS["CD_CLASS_TEMPLATE"]->ParseTemplate();
//for each row
$prevModel = "";
for($i = 0, $size = sizeof($ProjectData); $i < $size; $i++)
{
//change all fields if needed
foreach($ProjectData[$i] as $key => $val)
$ProjectData[$i][$key] = empty($val) ? '...' : $val;
//render model + table start + first vehicle
if($prevModel != $ProjectData[$i]['Model'])
{
$GLOBALS["CD_CLASS_TEMPLATE"]->SetTemplate('vehicles/models/row');
$Summary .= $this->_Parse($GLOBALS["CD_CLASS_TEMPLATE"]->ParseTemplate(), $ProjectData[$i]);
$GLOBALS["CD_CLASS_TEMPLATE"]->SetTemplate('vehicles/vehicle/header');
$Summary .= $this->_Parse($GLOBALS["CD_CLASS_TEMPLATE"]->ParseTemplate(), $ProjectData[$i]);
$GLOBALS["CD_CLASS_TEMPLATE"]->SetTemplate('vehicles/vehicle/row');
$Summary .= $this->_Parse($GLOBALS["CD_CLASS_TEMPLATE"]->ParseTemplate(), $ProjectData[$i]);
}
//render other vehicles
else
{
$GLOBALS["CD_CLASS_TEMPLATE"]->SetTemplate('vehicles/vehicle/row');
$Summary .= $this->_Parse($GLOBALS["CD_CLASS_TEMPLATE"]->ParseTemplate(), $ProjectData[$i]);
}
//update prev model
$prevModel = $ProjectData[$i]['Model'];
}
//footer
$GLOBALS["CD_CLASS_TEMPLATE"]->SetTemplate('vehicles/models/footer');
$Summary .= $GLOBALS["CD_CLASS_TEMPLATE"]->ParseTemplate();
return $Summary;
}
//perform search
function performSearch($vehicle)
{
//calculate limits
if((int)$GLOBALS["CD_CLASS_FUNCTION"]->FreeDirs[2] == 0)
{
$start = 0;
$end = 10;
}
elseif((int)$GLOBALS["CD_CLASS_FUNCTION"]->FreeDirs[2] == -1)
{
$start = 0;
$end = 100000;
}
else
{
$start = 10 * ((int) ($GLOBALS["CD_CLASS_FUNCTION"]->FreeDirs[2]) - 1);
$end = 10;
}
//find all accumulators
$ProjectData = $this->GetAccumsForMake($vehicle['CapacityFrom'], $vehicle['CapacityTo'], $vehicle['Polarity'], $vehicle['KlemmaType'], $vehicle['HWD'], $start, $end);
//render paging
$GLOBALS["Pages"] = $this->_Pages($start, 10, sizeof($this->GetAccumsForMake($vehicle['CapacityFrom'], $vehicle['CapacityTo'], $vehicle['Polarity'], $vehicle['KlemmaType'], $vehicle['HWD'], 0, 100000)));
//model data for header
$modelData = $vehicle;
foreach($modelData as $key => $val)
$modelData[$key] = empty($val) ? '...' : $val;
$Summary = "";
//generate template for make
$GLOBALS["CD_CLASS_TEMPLATE"]->SetTemplate('vehicles/model/row');
$Summary .= $this->_ParseSearchResult($this->_Parse($GLOBALS["CD_CLASS_TEMPLATE"]->ParseTemplate(), $modelData), $modelData);
//render header
$GLOBALS["CD_CLASS_TEMPLATE"]->SetTemplate('katalog/result/model/header');
$Summary .= $GLOBALS["CD_CLASS_TEMPLATE"]->ParseTemplate();
//for each row
for($i = 0, $size = sizeof($ProjectData); $i < $size; $i++)
{
//render rows
$GLOBALS["CD_CLASS_TEMPLATE"]->SetTemplate('katalog/result/model/row');
$Summary .= $this->_ParseSearchResult($GLOBALS["CD_CLASS_TEMPLATE"]->ParseTemplate(), $ProjectData[$i]);
}
//render footer
$GLOBALS["CD_CLASS_TEMPLATE"]->SetTemplate('katalog/result/model/footer');
$Summary .= $GLOBALS["CD_CLASS_TEMPLATE"]->ParseTemplate();
return $Summary;
}
//get row by id
function GetRowById($id)
{
return CD_FUNCTION::_GetSelectQuery(sprintf("select * FROM `%svehicles` WHERE `RowID` = '%d' LIMIT 1", $GLOBALS["CD_CFG"]["tablePrefix"], (int)$id));
}
//get cars for make
function GetVehiclesForMake($make)
{
return CD_FUNCTION::_GetSelectQuery(sprintf("select * FROM `%svehicles` WHERE `Make` = '%s' ORDER BY `Model` , `Type` ", $GLOBALS["CD_CFG"]["tablePrefix"], (string)$make));
}
//get cars for model and make
function GetVehiclesForMakeAndModel($make, $model)
{
return CD_FUNCTION::_GetSelectQuery(sprintf("select * FROM `%svehicles` WHERE `Make` = '%s' AND `Model` = '%s' ORDER BY `Type`", $GLOBALS["CD_CFG"]["tablePrefix"], (string)$make, (string)$model));
}
//get accums for make
function GetAccumsForMake($capacityFrom, $capacityTo, $polarity, $klemmaType, $HWD, $start, $end)
{
$SQL = sprintf("select * FROM `%smodels` WHERE `Capacity` >= '%d' AND `Capacity` <= '%d' AND `Polarity` = '%s' AND `KlemmaType` = '%s' AND (`HWD` = '%s' OR `HWD` = '%s' OR `HWD` = '%s' OR `HWD` = '%s') ORDER BY `Price` DESC LIMIT %d, %d", $GLOBALS["CD_CFG"]["tablePrefix"], (int)$capacityFrom, (int)$capacityTo, (string)$polarity, (int)$klemmaType, (string)$HWD, str_replace('x', 'х', (string)$HWD), (string)$HWD." ", str_replace('x', 'х', (string)$HWD)." ", (int)$start, (int)$end);
return CD_FUNCTION::_GetSelectQuery($SQL);
}
//some shity function to parse
function _Parse($text, $m = 0)
{
foreach($m as $k=>$v)
$text = str_replace('%%Vehicles.' . $k . '%%', $v, $text);
return $text;
}
//parse search result
function _ParseSearchResult($text, $m = 0)
{
$Klemmas = array(1 => 'Европейский', 2 => 'Американский', 3 => 'Японский');
$CurrentType = array(1 => 'Постоянный', 2 => 'Переменный');
if(sizeof($m))
{
foreach($m as $k=>$v)
{
if($k == 'BrandID')
{
$text = str_replace('%%Katalog.BrandName%%', $this->brand_img[$v]["Name"], $text);
$text = str_replace('%%Katalog.BrandRowID%%', $v, $text);
}
if($k == 'Image')
{
$brandImg = ($v) ? $v : $this->brand_img[$m["BrandID"]]["Image"];
$brandImg = ($brandImg) ? $brandImg : $GLOBALS["imagePath"].'/1x1.gif';
$text = str_replace('%%Katalog.ModelImage%%', $brandImg, $text);
$v = ($v) ? $v : $GLOBALS["imagePath"].'/1x1.gif';
if($m["LabelRecom"] == 1)
$label =$GLOBALS["imagePath"].'/labelrecom.gif';
else if($m["LabelLider"] == 1)
$label =$GLOBALS["imagePath"].'/labellider.gif';
else
$label =$GLOBALS["imagePath"].'/1x1.gif';
$text = str_replace('%%Katalog.labelImage%%', $label, $text);
}
if($k == 'ImageAdd')
{
$v = ($v) ? $v : $GLOBALS["imagePath"].'/1x1.gif';
$text = str_replace('%%Katalog.ModelImageAdd%%', $v, $text);
}
if($k == 'KlemmaType')
$v = $Klemmas[$v];
if($k == 'CurrentType')
$v = $CurrentType[$v];
if($k == 'Price')
{
if ($v == '' || $v == 0)
$v = 'под заказ';
else
$v .= ' р.';
}
$text = str_replace('%%Katalog.' . $k . '%%', $v, $text);
}
}
return $text;
}
//some pages shit
function _Pages ($cur, $inp, $all)
{
$Summary = 'Страницы: ';//'<table border="0" cellspacing="5" cellpadding="1"><tr>';
$pages = ceil ($all / $inp);
$link = $GLOBALS["catLink"] . 'search/' . ((int)$GLOBALS["CD_CLASS_FUNCTION"]->FreeDirs[1]) . '/';
$open1 = ' | ';//'<td width="15" height="15" bgcolor="#EFEFEF" align="center">';
$open2 = ' | <b>';//'<td width="15" height="15" bgcolor="#DDDDDD" align="center"><b>';
$open3 = ' | ';//'<td width="15" height="15" align="center">';
$close1 = '';//'</td>';
$close2 = '</b>';//'</b></td>';
$close3 = '';//'</td>';
if ($cur <= 5)
$start = 1;
else
$start = $cur - 4;
if ($cur >= $pages - 4)
$end = $pages;
else
$end = $cur + 4;
if ($start > 1)
{
if ($cur == 1)
$Summary .= $open2 . '1' . $close2;
else
$Summary .= $open1.'<a href="' . $link . '1/">1</a>'.$close1;
}
if ($start > 2)
$Summary .= $open3 . ' ... ' . $close3;
for ($i = $start; $i <= $end; $i++)
{
if ($i == $cur)
$Summary .= $open2 . $i . $close2;
else
$Summary .= $open1.'<a href="' . $link . $i . '/">' . $i . '</a>'.$close1;
}
if ($end < $pages-1)
$Summary .= $open3 . ' ... ' . $close3;
if ($end < $pages)
{
if ($pages == $cur)
$Summary .= $open2 . $pages . $close2;
else
$Summary .= $open1.'<a href="' . $link . $pages . '/">' . $pages . '</a>'.$close1;
}
if ($cur == -1)
$Summary .= $open2 . '<nobr>Все на одной странице</nobr>' . $close2;
else
$Summary .= $open1.'<a href="' . $link . '-1/"><nobr>Все на одной странице</nobr></a>'.$close1;
$Summary .= '';//'</tr></table>';
return $Summary;
}
}
?>