forked from mrsamo/progtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout.php
415 lines (366 loc) · 16.1 KB
/
layout.php
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/*
* Родительский класс для всех видов Kohana
* Здесь размещены свойства и методы, общие для всех видов
*/
class View_Layout extends Kohana_Kostache_Layout
{
protected $_layout = 'layout';
public $is_front = FALSE;
// Метка закрытого раздела для неавторизованных пользователей в состоянии FALSE
protected $auth_zone = FALSE;
// Метка закрытого раздела для авторизованных пользователей в состоянии FALSE
protected $no_auth_zone = FALSE;
// Инфоблоки
public $header_phone_block;
public $footer_copyright_block;
public $footer_address_block;
public $footer_phone_block;
public $footer_counters_block;
public $year;
// Баннеры
public $banners_dir = 'images/banners/';
// Преимущества
public $benefits_dir = 'images/benefits/';
// Метки для блока преимущества
public $benefit = FALSE;
// Каталог
public $orig_images_dir = 'catalog_files/images/orig/';
public $cat_images_dir = 'catalog_files/images/cat/';
public $cart_images_dir = 'catalog_files/images/cart/';
// Переменные для хранения пользователя и объекта авторизации
public $auth;
public $user;
// Цепляем все составные части страницы
protected $_partials = array(
'header' => 'partials/header',
'footer' => 'partials/footer',
'top-menu' => 'partials/top-menu',
'breadcrumb' => 'partials/breadcrumb',
'form-os' => 'partials/form-os',
'form-callback' => 'partials/form-callback',
'form-reqbuysell' => 'partials/form-reqbuysell',
'form-reqbuy' => 'partials/form-reqbuy',
'page-content' => 'partials/page-content',
'page-front' => 'partials/page-front',
'page-catalog' => 'partials/page-catalog',
'front-promo' => 'partials/front-promo',
'front-business' => 'partials/front-business',
'front-banners' => 'partials/front-banners',
'one-benefit' => 'partials/one-benefit',
'mortgage-block' => 'partials/mortgage-block',
'form-viewer' => 'partials/form-viewer',
'form-exchange' => 'partials/form-exchange',
);
/*
* Обновляем конструктор
*/
public function __construct($template = NULL, array $partials = NULL)
{
if (Request::current() !== null)
{
// URL страницы
$this->page = Request::current()->param('page');
}
// Register infoblocks
$this->header_phone_block = Sprig::factory('infoblock', Array('id' => 1))->load()->content;
$this->footer_copyright_block = Sprig::factory('infoblock', Array('id' => 2))->load()->content;
$this->footer_address_block = Sprig::factory('infoblock', Array('id' => 3))->load()->content;
$this->footer_phone_block = Sprig::factory('infoblock', Array('id' => 4))->load()->content;
$this->footer_counters_block = Sprig::factory('infoblock', Array('id' => 5))->load()->content;
// Current year
$this->year = date('Y');
// Split phone number for two parts - code and phone
if (preg_match('!^\(([^)]+)\)(.*)$!', $this->header_phone_block, $header_phone_parts))
{
$header_phone_block_tmp = array();
$header_phone_block_tmp['code'] = $header_phone_parts[1];
$header_phone_block_tmp['phone'] = $header_phone_parts[2];
$this->header_phone_block = $header_phone_block_tmp;
}
else
{
$header_phone_block_tmp = array();
$header_phone_block_tmp['code'] = $this->header_phone_block;
$this->header_phone_block = $header_phone_block_tmp;
}
// Выбираем случайно одно преимущество
$benefitO = Sprig::factory('benefits')->load(DB::select('*')->where('status', '=', '1')->order_by(NULL, 'RAND()'), 1);
$this->benefit = $benefitO->as_array();
parent::__construct($template, $partials);
}
/*
* Функция построения массива для вывода верхнего меню разделов
*/
public function menu()
{
$contents_arr = array();
// Получаем корневую категорию и категории первого уровня
$root_content = Sprig::factory('content')->root(1);
$contents_id = $root_content->id;
$root_childrens = $root_content->children->as_array();
// Сортируем массив дочерних категорий
usort($root_childrens, Array('View_Layout', 'sort_func_ord'));
// Если мы не на главной странице - определяем родителя
if (!empty($this->page))
$parent = Sprig::factory('content', Array('url' => $this->page))->load()->parent();
// Формируем новый массив с нужными значениями разделов первого уровня
$index = -1;
for ($i = 0; $i < count($root_childrens); $i++)
{
// Пропускаем страницы со статусом Не отображать
if (empty($root_childrens[$i]->status))
continue;
$contents_arr[++$index]['name'] = $root_childrens[$i]->name;
$contents_arr[$index]['num'] = $index + 1;
// По умолчанию второго уровня меню нет
$contents_arr[$index]['sub_menu']['exists'] = FALSE;
// Если к странице привязан модуль - получаем данные о модуле
if (!empty($root_childrens[$i]->module_id))
{
// Находим нужный модуль
$module = Sprig::factory('module', array('mid' => $root_childrens[$i]->module_id))->load();
if ($module->loaded())
{
$contents_arr[$index]['url'] = $root_childrens[$i]->url;
// Добавляем активность пункта меню, если текущий контроллер совпадает с модулем
if ($module->name == Request::current()->controller())
$contents_arr[$index]['active'] = TRUE;
// Костылим, определяем жестко 4 типа каталога
if ($root_childrens[$i]->module_id == 3)
{
if ($root_childrens[$i]->url == 'catalog/1')
$contents_arr[$index]['icon'] = 'house';
elseif ($root_childrens[$i]->url == 'catalog/2')
$contents_arr[$index]['icon'] = 'city';
elseif ($root_childrens[$i]->url == 'catalog/3')
$contents_arr[$index]['icon'] = 'commerce';
elseif ($root_childrens[$i]->url == 'catalog/4')
$contents_arr[$index]['icon'] = 'business';
$contents_arr[$index]['name'] = preg_replace('!\s!', '<br>', $contents_arr[$index]['name'], 1);
}
}
}
// Если страница - просто текстовый элемент
else
{
$contents_arr[$index]['icon'] = 'services';
// Для главной страницы URL не создаем
if ($root_childrens[$i]->url != 'index')
$contents_arr[$index]['url'] = 'content/' . $root_childrens[$i]->url;
// Добавляем активность пункта меню
if ($this->page == $root_childrens[$i]->url or (isset($parent) and $root_childrens[$i]->id == $parent->id) or (empty($this->page) and $root_childrens[$i]->url == 'index'))
{
$contents_arr[$index]['active'] = TRUE;
}
// Строим меню со вторым уровнем вложенности
$childrens_2 = $root_childrens[$i]->children->as_array();
// Сортируем массив дочерних категорий
usort($childrens_2, Array('View_Layout', 'sort_func_ord'));
// Формируем новый массив с нужными значениями разделов второго уровня
$index2 = -1;
for ($r = 0; $r < count($childrens_2); $r++)
{
// Пропускаем страницы со статусом Не отображать
if (empty($childrens_2[$r]->status))
continue;
$contents_arr[$index]['sub_menu']['menu_items'][++$index2]['name'] = $childrens_2[$r]->name;
$contents_arr[$index]['sub_menu']['exists'] = TRUE;
// Если к странице привязан модуль - получаем данные о модуле
if (!empty($childrens_2[$r]->module_id))
{
// Находим нужный модуль
$module = Sprig::factory('module', array('mid' => $childrens_2[$r]->module_id))->load();
if ($module->loaded())
$contents_arr[$index]['sub_menu']['menu_items'][$index2]['url'] = $module->name;
// Добавляем активность пункта меню, если текущий контроллер совпадает с модулем
if ($module->name == Request::current()->controller())
{
$contents_arr[$index]['active'] = TRUE;
$contents_arr[$index]['sub_menu']['menu_items'][$index2]['active_2'] = TRUE;
}
}
// Если страница - просто текстовый элемент
else
{
// Для главной страницы URL не создаем
if ($childrens_2[$r]->url != 'index')
$contents_arr[$index]['sub_menu']['menu_items'][$index2]['url'] = 'content/' . $childrens_2[$r]->url;
// Добавляем активность пункта меню
if ($this->page == $childrens_2[$r]->url)
{
$contents_arr[$index]['active'] = TRUE;
$contents_arr[$index]['sub_menu']['menu_items'][$index2]['active_2'] = TRUE;
}
}
}
}
}
$contents_arr[0]['first'] = TRUE;
$contents_arr[count($contents_arr) - 1]['last'] = TRUE;
// print_r($contents_arr);
return $contents_arr;
}
/*
* Обновляем конструктор
*/
public function prepare_table($data, $cols)
{
$table = $row = array();
$even = $last = FALSE;
$r = 0;
$first = TRUE;
for ($i = 0; $i < count($data); $i++)
{
$r++;
$row[] = $data[$i];
if ($r == $cols)
{
$table[] = array('data' => $row, 'even' => $even, 'first' => $first, 'last' => $last);
$row = array();
$first = FALSE;
$r = 0;
$even = !$even;
}
}
if (($r > 0) and ($r < $cols))
{
for ($j = $r; $j <= $cols; $j++)
$row[] = array('_empty_data' => TRUE);
$table[] = array('data' => $row, 'even' => $even, 'first' => $first, 'last' => TRUE);
}
$table[count($table) - 1]['last'] = TRUE;
return $table;
}
/*
* Функция удаления тэгов и квотирования
* для всех значений из массива $postData
*/
protected function set_safe(& $post_arr, $def_dash = true)
{
foreach ($post_arr as $key => $value)
{
$post_arr[$key] = strip_tags($value);
if (empty($post_arr[$key]) and !empty($def_dash))
$post_arr[$key] = '-';
}
}
/*
* Функция получения настроек админ.панели
*/
protected function get_variable($param)
{
$variable = Sprig::factory('settings', array('param' => $param))->load();
return ($variable->loaded()) ? $variable->value : false;
}
/*
* Функция фомирования тела письма из шаблона
*/
protected function email_tpl_render($tpl_name, $vars_arr = array())
{
// Создаем объект нужного вида
$tpl_path = 'mail/' . $tpl_name;
// Подключаем класс с видом вручную
require_once('application/classes/view/mail/index.php');
$view = new View_Mail_Noticemail($tpl_path, $vars_arr);
// Регистрируем переменные
foreach ($vars_arr as $key => $value)
$view->$key = $value;
// Создаем служебные переменные
$view->domen = $_SERVER['HTTP_HOST'];
$view->date = date('d.m.Y H:i');
// Генерируем контент
$tpl_content = $view->render();
return $tpl_content;
}
/*
* Функция callback для сортировки дочерних категорий
*/
static function sort_func_ord($a, $b)
{
return ($a->ord > $b->ord) ? +1 : -1;
}
static function sort_func_ord_arr($a, $b)
{
return ($a['ord'] > $b['ord']) ? +1 : -1;
}
/*
* Функция для сортировки дочерних категорий по дате добавления
*/
static function sort_func_time_add_arr($a, $b)
{
return ($a['time_add'] < $b['time_add']) ? +1 : -1;
}
static function sort_func_time_add($a, $b)
{
return ($a->time_add < $b->time_add) ? +1 : -1;
}
/*
* Функция возвращает истину для всех четных вызовов
* Используется в модулях для создания строк-зебр
*/
public function even()
{
static $even;
if (empty($even))
$even = 1;
if ($even == 2)
{
$even = 1;
return true;
}
$even++;
}
public function even2()
{
static $even;
if (empty($even))
$even = 1;
if ($even == 2)
{
$even = 1;
return true;
}
$even++;
}
/*
* Get one banner
*/
public function one_banner()
{
static $banners;
static $index = 0;
if (!empty($banners))
{
if (isset($banners[$index]))
{
$banner = $banners[$index++];
return $banner;
}
else
{
return FALSE;
}
}
else
{
// select all banners
$bannersO = Sprig::factory('banner')->load(DB::select('*')->order_by(NULL, 'RAND()'), NULL);
// Проходимся по всем баннерам
for ($i = 0; $i < count($bannersO); $i++)
{
$banners[$i] = $bannersO[$i]->as_array();
$banners[$i]['is_' . $banners[$i]['type']] = TRUE;
}
if (!empty($banners))
{
$banner = $banners[$index++];
return $banner;
}
else
{
return FALSE;
}
}
}
}