php class CD_VEHICLES function CD_VEHICLES init function function Init

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?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;
}
}
?>