Blame view

libs/catalogs.class.php 162 KB
42868d70   andryeyev   Создал GIT
1
  <?php
1b81d16b   andryeyev   + откат до пред. ...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  class Catalogs{
   var $db = null;
   var $tpl = null;
   var $error = null;
   var $cache = null;
   var $photo_id = null;
  
  
   function Catalogs(&$db,&$tpl,&$error,&$cache){
    $this->db = &$db;
    $this->tpl = &$tpl;
    $this->error = &$error;
    $this->cache = &$cache;
   }
  
  
   function trim(&$data){
    foreach($data as $key=>$value){
     if(is_array($value))$this->trim($value);
     else $data[$key] = stripslashes(trim($value));
    }
   }
   
42868d70   andryeyev   Создал GIT
25
  
1b81d16b   andryeyev   + откат до пред. ...
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
   function valid($data,$upload = null){
  
    if(isset($data['name'])){
     if( !preg_match("/.{1,100}/i",$data['name']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Íàçâàíèÿ.";
    }
    
    if(isset($data['about_min'])){
     if( !preg_match("/.+/is",$data['about_min']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Êðàòêîå îïèñàíèå.";
    }
   /*
    if(isset($data['text'])){
     if( !preg_match("/.+/is",$data['text']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ïîëíîå îïèñàíèå.";
    } */
  /*
    if(isset($data['cine'])){
     if( !preg_match("/[0-9]+/",$data['cine']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Öåíà, òîëüêî öèôðû.";
    } */
    
    if(isset($upload['pic']['name']) && $upload['pic']['tmp_name']){
     $type = substr(strrchr($upload['pic']['name'],"."),1);
     if( !preg_match("/^(jpeg|jpg|gif|png|bmp)$/i",$type) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Èçîáðàæåíèÿ, òîëüêî jpg,png,gif.";
    }
    
    if(isset($upload['file']['name'])){
     if( !preg_match("/\.csv$/i",$upload['file']['name']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ CSV ôàéë, òîëüêî csv.";
    }
    
  
    if(isset($data['order_name'])){
     if( !preg_match("/.{1,100}/i",$data['order_name']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Èìÿ (Ìàêñèìóì 100 ñèìâîëîâ).";
    }
    
    if(isset($data['order_adress'])){
     if( !preg_match("/.{1,200}/i",$data['order_adress']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Àäðåñ (Ìàêñèìóì 200 ñèìâîëîâ).";
    }
    
    if(isset($data['order_phone2'])&& strlen($data['order_phone2'])>0){
     if( !preg_match("/.{1,20}/i",$data['order_phone2']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Òåëåôîí (Ìàêñèìóì 70 ñèìâîëîâ).";
    }
  
    if(isset($data['order_phone'])){
  if( trim($data['order_phone']) == "" ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ìîáèëüíûé òåëåôîí (ôîðìàò: (0XX) XXX-XXXX).";
  if( strlen(trim($data['order_phone']))<5 ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ìîáèëüíûé òåëåôîí (ôîðìàò: (0XX) XXX-XXXX).";
    }
    
    if(isset($data['delivery']) && $data['delivery']==0){
          $this->error[] = "Îøèáêà ââîäà ïîëÿ Äîñòàâêà.";
    }  
    
    if(isset($data['user_name'])){
     if( !preg_match("/.{1,100}/i",$data['user_name']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Èìÿ (Ìàêñèìóì 100 ñèìâîëîâ).";
    }
   
    if(isset($data['order_city'])){
     if( !preg_match("/.{1,100}/i",$data['order_city']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Ãîðîä (Ìàêñèìóì 100 ñèìâîëîâ).";
    }
    
    if(isset($data['user_email'])){
     if( !preg_match("/.{1,80}/i",$data['user_email']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Âàø e-mail (Ìàêñèìóì 80 ñèìâîëîâ).";
    }
    
    if(isset($data['user_login'])){
     if( !preg_match("/.{1,100}/i",$data['user_login']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Âàø ëîãèí (Ìàêñèìóì 100 ñèìâîëîâ).";
     elseif( $this->isLoginUser($data['user_login']) ) $this->error[] = "Îøèáêà òàêîé Ëîãèí óæå åñòü!";
    }
    
    if(isset($data['you_login'])){
     if( !$this->isLoginUser($data['you_login']) ) $this->error[] = "Òàêîãî ËÎÃÈÍÀ íåò â áàçå äàííûõ ñàéòà!";
    }
    
    if(isset($data['user_psw'])){
     if( !preg_match("/.{1,100}/i",$data['user_psw']) ) $this->error[] = "Îøèáêà ââîäà ïîëÿ Âàø ïàðîëü (Ìàêñèìóì 100 ñèìâîëîâ).";
    }
  
   return ( count($this->error) ) ? true : false;
   }
   
   function deleteRubric($id,$field="id"){
    $sql = "select pic,id,parent_id from catalogs_rubrics where $field=?";
    $res = $this->db->getAll($sql,array($id),DB_FETCHMODE_ASSOC);
    foreach($res as $row){
    @unlink("{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/rubrics/{$row['pic']}");
    $sql = "delete from catalogs_rubrics where id=?";
    $this->db->query($sql,array($row['id']));
    $this->deleteFiltersRubric_id($row['id']);
    $this->deleteFiltersRubric_id2($row['id']);
    $this->deleteYearsRubric_id($row['id']);
    $this->deleteProductsRubric($row['id']);
    $this->deleteRubric($row['id'],"parent_id");
    }
   }
   
   function deleteProductPics($id,$pics = array('pic_big','pic','pic2')){
    $sql = "select pic_big,pic,pic2 from catalogs_products where id=?";
    $row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
   // if(in_array('pic_big',$pics))@unlink("{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/products/big/{$row['pic_big']}");
    if(in_array('pic',$pics))@unlink("{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/products/{$row['pic']}");
    if(in_array('pic2',$pics))@unlink("{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/products/2/{$row['pic2']}");
   }
   
   function deleteProduct($id){
    $this->deleteProductPics($id);
    $sql = "delete from catalogs_products where id=?";
    $this->db->query($sql,array($id));
    $this->delete_keysProductsFilters($id);
    $this->delete_keysProductsFilters2($id);
    $this->delete_keysProductsAkcii($id);
    $this->delete_keysProductsYears($id);
    $this->delete_keysProductsCities($id);
    $this->deleteProductModifications($id);
   }
   
   function deleteProductsRubric($id){
    $res = $this->db->getAll("select id from catalogs_products where rubric_id=?",array($id),DB_FETCHMODE_ASSOC);
    foreach($res as $row){
     $this->deleteProduct($row['id']);
     $this->deleteGalleryProduct($row['id']);
    }
   }
  
  
   function PhotosCheck($id_s,$check_s = array()){
    $sql = "UPDATE gallery SET active = ? WHERE id =?";
  
    foreach($id_s as $id){
    $this->db->query($sql,array(0,$id));
    }
   if(count($check_s)){
    foreach($check_s as $id){
    $this->db->query($sql,array(1,$id));
    }
   }
   }
   
   function viewRubrics($id = 0,$parent_id = 0){ // print $parent_id."<br />";
    global $rubrics;
    //if(!$this->cache->cacheLife("cache_sql_rubrics")){
    
    //$sql = "select a.*,(select b.id from catalogs_rubrics b where b.parent_id=a.id order by b.sort limit 1) as r_pid from catalogs_rubrics a where a.parent_id=? group by a.id order by a.sort asc";
      $sql = "
42868d70   andryeyev   Создал GIT
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
          select 
              a.name, 
              a.name_ukr, 
              a.id,
              a.parent_id,
              a.level,
              a.sort,
              a.sort_cost, 
              a.list_name,
              a.list_name_ukr,
              a.translit,
              a.pic,
              a.top,  
          (
              select b.translit from catalogs_rubrics b where b.parent_id=a.id order by b.sort limit 1
          ) as r_pid2, (
              select b.id from catalogs_rubrics b where b.parent_id=a.id order by b.sort limit 1
          ) as r_pid 
              from catalogs_rubrics a 
              where a.parent_id=? 
          group by a.id 
          order by a.sort asc
      ";
1b81d16b   andryeyev   + откат до пред. ...
189
190
191
192
      
  $res = $this->db->query($sql,array($id));
    while ($res->fetchInto($row,DB_FETCHMODE_ASSOC)){
     $rubrics[] = $row;   //print $row['id']."<br />";
42868d70   andryeyev   Создал GIT
193
  
1b81d16b   andryeyev   + откат до пред. ...
194
195
196
     if(in_array($row['id'],$this->getParentIdRubric($parent_id))){$this->viewRubrics($row['id']);}
    // if($row['level']==0){$this->viewRubrics($row['id']);}
    }
42868d70   andryeyev   Создал GIT
197
          // $this->cache->cacheSave($rubrics,130);
1b81d16b   andryeyev   + откат до пред. ...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
          //}else $count = $this->cache->sql_result;  
     $this->tpl->assign("rubrics",$rubrics);
   }
   
   function getParentIdRubric($id){
   //$ids = array();
   global $ids;
    if(!count($ids))$ids = array();
    while($id>0){
     $row = $this->db->getRow('select id,parent_id from catalogs_rubrics where id=?',array($id),DB_FETCHMODE_ASSOC);
     //if(isset($row['id']))
     $ids[] = $row['id'];
     $id = $row['parent_id'];
    }   //print_r($ids);
   // sort($ids);
    return $ids;
   }
   
42868d70   andryeyev   Создал GIT
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
      function viewRubricsAll ($id = 0)
      {
          global $rubrics;
          $sql = "
              SELECT 
                  a.name, 
                  a.name_ukr, 
                  a.id,
                  a.parent_id,
                  a.level,
                  a.sort,
                  a.sort_cost, 
                  a.list_name,
                  a.list_name_ukr,
                  a.translit,
                  a.pic,
                  a.top
              FROM catalogs_rubrics a
              WHERE parent_id=? 
              ORDER BY sort asc
          ";
1b81d16b   andryeyev   + откат до пред. ...
237
238
          $res = $this->db->query($sql,array($id));
          while ($res->fetchInto($row,DB_FETCHMODE_ASSOC))
42868d70   andryeyev   Создал GIT
239
240
          {
              $rubrics[] = $row;
1b81d16b   andryeyev   + откат до пред. ...
241
              $this->viewRubricsAll($row['id']);
42868d70   andryeyev   Создал GIT
242
          }
1b81d16b   andryeyev   + откат до пред. ...
243
244
  
          $this->tpl->assign("rubrics",$rubrics);
42868d70   andryeyev   Создал GIT
245
246
      }
  
1b81d16b   andryeyev   + откат до пред. ...
247
248
  
      function viewRubrics_begin ($item = 'parents',$lang = 'ru')
42868d70   andryeyev   Создал GIT
249
      {
1b81d16b   andryeyev   + откат до пред. ...
250
251
          //if(!$this->cache->cacheLife("cache_sql_rubrics_$item"."_".$lang)){
          
42868d70   andryeyev   Создал GIT
252
253
          $sql = "
              SELECT 
1b81d16b   andryeyev   + откат до пред. ...
254
                  a.name".(($lang=='ukr')?'_ukr':'')." as name, 
42868d70   andryeyev   Создал GIT
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
                  a.id,
                  a.parent_id,
                  a.level,
                  a.sort,
                  a.sort_cost, 
                  a.list_name,
                  a.list_name_ukr, 
                  a.translit,
                  a.pic,
                  a.top,
                  (
                      SELECT b.id 
                      FROM catalogs_rubrics b 
                      WHERE b.parent_id = a.id 
                      ORDER BY b.sort 
                      LIMIT 1
                  ) as r_pid 
              FROM catalogs_rubrics a 
              WHERE a.parent_id=? 
              ORDER BY a.sort asc
          ";
          
1b81d16b   andryeyev   + откат до пред. ...
277
278
          $row = $this->db->getAll($sql, array (0), DB_FETCHMODE_ASSOC);
  
42868d70   andryeyev   Создал GIT
279
280
281
282
          foreach ($row as $key => $value)
          {
              if ($item == 'parents')
              {
1b81d16b   andryeyev   + откат до пред. ...
283
                  $row[$key]['parents'] = $this->db->getAll("
42868d70   andryeyev   Создал GIT
284
                      SELECT 
1b81d16b   andryeyev   + откат до пред. ...
285
                          a.name".(($lang=='ukr')?'_ukr':'')." as name, 
42868d70   andryeyev   Создал GIT
286
287
288
289
290
291
292
293
294
295
296
297
298
                          a.id,
                          a.parent_id,
                          a.level,
                          a.sort,
                          a.sort_cost, 
                          a.list_name,
                          a.list_name_ukr,
                          a.translit,
                          a.pic,
                          a.top
                      FROM catalogs_rubrics a
                      WHERE parent_id=? 
                      ORDER BY sort asc
1b81d16b   andryeyev   + откат до пред. ...
299
                  ", array($value['id']), DB_FETCHMODE_ASSOC);
42868d70   andryeyev   Создал GIT
300
301
302
              }
              else if ($item == 'brends')
              {
1b81d16b   andryeyev   + откат до пред. ...
303
                  $row[$key]['brends'] = $this->db->getAll("
42868d70   andryeyev   Создал GIT
304
305
306
307
308
309
310
311
                      SELECT * 
                      FROM catalogs_brends 
                      WHERE rubric_id in (
                          SELECT id 
                          FROM catalogs_rubrics 
                          WHERE parent_id=? or id=?
                      ) 
                      GROUP BY name
1b81d16b   andryeyev   + откат до пред. ...
312
                  ",array ($value['id'], $value['id']), DB_FETCHMODE_ASSOC);
42868d70   andryeyev   Создал GIT
313
314
              }
          }
1b81d16b   andryeyev   + откат до пред. ...
315
316
  
          $this->tpl->assign("catalogs_rubrics_begin", $row);
42868d70   andryeyev   Создал GIT
317
318
319
      }
  
      // ==== CACHE viewRubrics begin ====
1b81d16b   andryeyev   + откат до пред. ...
320
   
42868d70   andryeyev   Создал GIT
321
322
      function getFilenameCacheVRB ($item = 'parents', $lang = 'ru')
      {
1b81d16b   andryeyev   + откат до пред. ...
323
          return $_SERVER['DOCUMENT_ROOT'].'/cache/menu/menu-'.$item.'-'.$lang.'.php';
42868d70   andryeyev   Создал GIT
324
      }
1b81d16b   andryeyev   + откат до пред. ...
325
      
42868d70   andryeyev   Создал GIT
326
327
328
329
      function isFreshCacheVRB ($item = 'parents', $lang = 'ru')
      {
          $filename = $this->getFilenameCacheVRB ($item, $lang);
          $time = 60 * 60 * 24;
1b81d16b   andryeyev   + откат до пред. ...
330
331
  
          if (is_file ($filename) && (filemtime ($filename) + $time) > time())
42868d70   andryeyev   Создал GIT
332
333
334
335
336
337
          {
              return true;
          }
          
          return false;
      }
1b81d16b   andryeyev   + откат до пред. ...
338
      
42868d70   andryeyev   Создал GIT
339
      function saveCacheVRB ($item = 'parents', $lang = 'ru')
1b81d16b   andryeyev   + откат до пред. ...
340
341
342
      {  
          $filename = $this->getFilenameCacheVRB ($item, $lang); 
        
42868d70   andryeyev   Создал GIT
343
          ob_start ();
1b81d16b   andryeyev   + откат до пред. ...
344
345
346
          var_export ($this->tpl->get_template_vars('catalogs_rubrics_begin'));
          // $row - êàñòûëü çàòî÷êà ïîä "catalogs_rubrics_begin", $row   
          file_put_contents ($filename, '<?php $row = '.ob_get_clean (). ';'); 
42868d70   andryeyev   Создал GIT
347
      }
1b81d16b   andryeyev   + откат до пред. ...
348
   
42868d70   andryeyev   Создал GIT
349
      function loadCacheVRB ($item = 'parents', $lang = 'ru')
1b81d16b   andryeyev   + откат до пред. ...
350
      {   
42868d70   andryeyev   Создал GIT
351
352
353
          include_once $this->getFilenameCacheVRB ($item, $lang);
          
          if (! isset ($this->zlofound))
1b81d16b   andryeyev   + откат до пред. ...
354
355
          { 
              $this->tpl->assign("catalogs_rubrics_begin", $row);
42868d70   andryeyev   Создал GIT
356
357
358
              $this->zlofound = true;
              unset ($row);
          }
1b81d16b   andryeyev   + откат до пред. ...
359
360
361
362
363
      } 
      
      // ==== ====
      
      function viewRubrics_parent($parent_id = 0)
42868d70   andryeyev   Создал GIT
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
      {
          $sql = " 
              SELECT 
                  a.name, 
                  a.name_ukr, 
                  a.id,
                  a.parent_id,
                  a.level,
                  a.sort,
                  a.sort_cost, 
                  a.list_name,
                  a.list_name_ukr,
                  a.translit,
                  a.pic,
                  a.top
              FROM catalogs_rubrics a
              WHERE parent_id=? 
              ORDER BY sort asc
          ";
          
1b81d16b   andryeyev   + откат до пред. ...
384
385
          $row = $this->db->getAll($sql,array($parent_id),DB_FETCHMODE_ASSOC);
          $this->tpl->assign("catalogs_rubrics_parent",$row);
42868d70   andryeyev   Создал GIT
386
      }
1b81d16b   andryeyev   + откат до пред. ...
387
   
42868d70   andryeyev   Создал GIT
388
389
      function viewRubrics_parent2 ($rub_id, $lang = 'ru')
      {
1b81d16b   andryeyev   + откат до пред. ...
390
391
          $parent_id = $this->getRubricParent($rub_id);
  
42868d70   andryeyev   Создал GIT
392
393
394
395
396
397
398
          if ($parent_id == 0)
          {
              $parent_id = $rub_id;
          }
          
          $sql = "
              SELECT 
1b81d16b   andryeyev   + откат до пред. ...
399
                  a.name".(($lang=='ukr')?'_ukr':'')." as name, 
42868d70   andryeyev   Создал GIT
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
                  a.id,
                  a.parent_id,
                  a.level,
                  a.sort,
                  a.sort_cost, 
                  a.list_name,
                  a.list_name_ukr,
                  a.translit,
                  a.pic,
                  a.top
              FROM catalogs_rubrics a
              WHERE parent_id=? 
              ORDER BY sort asc
          ";
          
1b81d16b   andryeyev   + откат до пред. ...
415
416
          $row = $this->db->getAll($sql,array($parent_id),DB_FETCHMODE_ASSOC);
          $this->tpl->assign("catalogs_rubrics_parent2",$row);
42868d70   andryeyev   Создал GIT
417
      }
1b81d16b   andryeyev   + откат до пред. ...
418
419
420
421
   
   function viewRubricOne($id,&$row,$lang = 'ru'){
    if($lang=='ukr')
     $sql = "select *,name_ukr as name,name_h1_ukr as name_h1,top_text_ukr as top_text,seo_text_ukr as seo_text,
42868d70   andryeyev   Создал GIT
422
423
  	 meta_title_ukr as meta_title,meta_description_ukr as meta_description,meta_keywords_ukr as meta_keywords,meta_about_ukr as meta_about,
  		filter2_ukr as filter2, filter_ukr as filter, list_name_ukr as list_name from catalogs_rubrics where id=?";
1b81d16b   andryeyev   + откат до пред. ...
424
425
426
427
428
    else 
   $sql = "select * from catalogs_rubrics where id=?";
    $row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('rubric',$row);
   }
42868d70   andryeyev   Создал GIT
429
   
1b81d16b   andryeyev   + откат до пред. ...
430
431
432
433
   function getRubricName($id,$lang = 'ru'){
    $sql = "select name".($lang=='ukr'?'_ukr':'')." from catalogs_rubrics where id=?";
    return $this->db->getOne($sql,array($id));
   }
42868d70   andryeyev   Создал GIT
434
   
1b81d16b   andryeyev   + откат до пред. ...
435
436
437
438
   function getRubricParent($id){
    $sql = "select parent_id from catalogs_rubrics where id=?";
    return $this->db->getOne($sql,array($id));
   }
42868d70   andryeyev   Создал GIT
439
   
1b81d16b   andryeyev   + откат до пред. ...
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
   function activeRubricTop($id,$top){
  	 $table_name = "catalogs_rubrics";
  	 $fields_values = array('top'=>$top);
  	 $this->db->autoExecute($table_name,$fields_values,DB_AUTOQUERY_UPDATE,"id=$id");
   }
  
   function SaveRubric($data,$upload){
   $table_name = "catalogs_rubrics";
  
  if(trim($data['translit']) == '') $data['translit'] = translit($data['name']);
  
   $DB_AUTOQUERY = ($data['update_id']>0) ? DB_AUTOQUERY_UPDATE : DB_AUTOQUERY_INSERT;
   $parent_id = (isset($data['parent_id']) ? $data['parent_id'] : 0);
   $fields_values = array('parent_id' => $parent_id,"level"=>($this->getLevelRubric($parent_id)+1),'sort' => $data['sort'],'name' => $data['name'],'name_ukr' => $data['name_ukr'],'list_name' => $data['list_name'],'list_name_ukr' => $data['list_name_ukr'],'name_h1' => $data['name_h1'],'name_h1_ukr' => $data['name_h1_ukr'],'seo_text' => $data['seo_text'],'seo_text_ukr' => $data['seo_text_ukr'],'top_text' => $data['top_text'],'top_text_ukr' => $data['top_text_ukr'],'filter' => $data['filter'],'filter_ukr' => $data['filter_ukr'],'filter2' => $data['filter2'],'filter2_ukr' => $data['filter2_ukr'],'filter_products' => $data['filter_products'],'sort_cost' => $data['sort_cost'],'meta_title' => $data['meta_title'],'meta_title_ukr' => $data['meta_title_ukr'],'meta_description' => $data['meta_description'],'meta_description_ukr' => $data['meta_description_ukr'],'meta_keywords' => $data['meta_keywords'],'meta_keywords_ukr' => $data['meta_keywords_ukr'],'meta_about' => $data['meta_about'],'meta_about_ukr' => $data['meta_about_ukr'],'translit' => $data['translit']);
  
  if(isset($data['literal']))$fields_values['literal'] = 1;
  else $fields_values['literal'] = 0;
  
  if(isset($data['if_filter_mod']))$fields_values['if_filter_mod'] = 1;
  else $fields_values['if_filter_mod'] = 0;
  
  if(isset($data['is_usd']))$fields_values['is_usd'] = 1;
  else $fields_values['is_usd'] = 0;
  
  if(isset($data['is_xml']))$fields_values['is_xml'] = 1;
  else $fields_values['is_xml'] = 0;
  
  if(isset($data['is_xml2']))$fields_values['is_xml2'] = 1;
  else $fields_values['is_xml2'] = 0;
  
  if(isset($data['is_width']))$fields_values['is_width'] = 1;
  else $fields_values['is_width'] = 0;
   /*
    if($upload['pic']['tmp_name']){
     $fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"180",'height'=>"180",'upload_path'=>"./uploaded/pic/catalogs/rubrics/"));
    } */
    
    if($upload['pic']['tmp_name']){
     if($data['update_id']>0)$this->deletePicRubric($data['update_id']);
     $uploadfile = "./uploaded/pic/catalogs/rubrics/";
     $type = substr(strrchr($upload['pic']['name'],"."),1);
     $uploadname = mktime() . "." . $type;
     if (!move_uploaded_file($_FILES['pic']['tmp_name'], $uploadfile . $uploadname))print"Îøèáêà ïðè çàêà÷êå ôàéëà!";
     $fields_values['pic'] = $uploadname;
    }
    //print_r($fields_values);exit;
    $this->db->autoExecute($table_name,$fields_values,$DB_AUTOQUERY,"id={$data['update_id']}");
   }
   
   function deletePicRubric($id){
    $sql = "select pic from catalogs_rubrics where id=?";
    $rub = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
    @unlink("./uploaded/pic/catalogs/rubrics/{$rub['pic']}");
   }
   
   function getLevelRubric($id){
    $level = $this->db->getOne("select level from catalogs_rubrics where id=?",array($id));
    return ($level==null)? -1 : $level;
   }
   
   function saveUser($data){
    $table_name = "catalogs_users";
    $DB_AUTOQUERY = ($data['update_id']>0) ? DB_AUTOQUERY_UPDATE : DB_AUTOQUERY_INSERT;
    $fields_values = array('name' => $data['user_name'],'email' => $data['user_email'],'login' => $data['user_login'],'psw' => $data['user_psw'],'about' => $data['user_about'],'mktime'=>mktime());
    $this->db->autoExecute($table_name,$fields_values,$DB_AUTOQUERY,"id={$data['update_id']}");
   }
   
   function updateUser($data){
    $table_name = "catalogs_users";
    $DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;
    $fields_values = array('name' => $data['user_name'],'email' => $data['user_email'],'login' => $data['user_login'],'psw' => $data['user_psw'],'about' => $data['user_about'],'discount'=>$data['user_discount']);
    $this->db->autoExecute($table_name,$fields_values,$DB_AUTOQUERY,"id={$data['update_id']}");
   }
   
   function updateUserID($data,$id){
    $table_name = "catalogs_users";
    $DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;
    $fields_values = array('name' => $data['user_name'],'email' => $data['user_email'],'psw' => $data['user_psw'],'about' => $data['user_about'],'phone' => $data['user_phone'],'phonemob' => $data['user_phonemob'],'adress' => $data['user_adress']);
    $this->db->autoExecute($table_name,$fields_values,$DB_AUTOQUERY,"id={$id}");
   }
   
   function deleteUser($id){
    $this->db->query("delete from catalogs_users where id=?",array($id));
    $res_order_id = $this->db->getRow("select id from catalogs_orders where user_id=?",array($id),DB_FETCHMODE_ASSOC);
    if(count($res_order_id)){
    foreach($res_order_id as $_id){
     $this->db->query("delete from catalogs_orders where id=?",array($_id));
     $this->db->query("delete from catalogs_orders_products where order_id=?",array($_id));
    }}
   }
   
   function isLoginUser($login){
    return $this->db->getOne("select id from catalogs_users where login=?",array($login));
   }
   
   function sendLoginInfo($login){
    $user = $this->db->getRow("select * from catalogs_users where login=?",array($login),DB_FETCHMODE_ASSOC);
    $mail = new PHPMailer();
    $mail->CharSet = "windows-1251";
    $mail->From = "shop@eltrade.com.ua";
    $mail->FromName = "shop@eltrade.com.ua";
    $mail->AddAddress($user['email'], $user['name']);
    $mail->Subject = "Àêêàóíò ýêñòðèì";
    $msg = "";
     $msg .= "Çäðàâñòâóéòå: ".$user['name'];
     $msg .= "<br />";
     $msg .= "Âàø ëîãèí: ".$user['login'];
     $msg .= "<br>";
     $msg .= "Âàø ïàðîëü: ".$user['psw'];
     $msg .= "<br>";
     $msg .= "Ñ óâàæåíèåì, shop@eltrade.com.ua";
  
    $mail->MsgHTML($msg);
  
     if(!$mail->Send()){echo "There has been a mail error sending to <br>";return false;}
     $mail->ClearAddresses();
     $mail->ClearAttachments();
   }
   
   function isUser($login,$psw){
    $row = $this->db->getRow("select * from catalogs_users where login=? AND psw=?",array($login,$psw),DB_FETCHMODE_ASSOC);
    //print $login .' - '.$psw;
    $this->tpl->assign('user_service',$row);
    return $row['id'];
   }
   
   function viewUser($id){
    $sql = "select * from catalogs_users where id=?";
    $row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('user',$row);
   }
   
   function getUserDiscount($id){
    return $this->db->getOne("select discount from catalogs_users where id=?",array($id));
   }
   
   function viewUsers(){
    $sql = "SELECT catalogs_users. *, COUNT(catalogs_orders.user_id) as count_orders
  FROM catalogs_users LEFT JOIN catalogs_orders ON catalogs_orders.user_id=catalogs_users.id
  GROUP BY catalogs_users.id
  ORDER BY catalogs_users.mktime DESC";
42868d70   andryeyev   Создал GIT
581
  
1b81d16b   andryeyev   + откат до пред. ...
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
    $pagerOptions = Array(
      'mode' => 'Sliding',
      'delta' => 6,
      'perPage' => 10,
      'linkClass'=>"link_class",
      'curPageLinkClassName'=>"link_class_active",
      'spacesBeforeSeparator' => 1,
      'spacesAfterSeparator' => 1
    );
    $res = Pager_Wrapper_DB($this->db, $sql, $pagerOptions, false, DB_FETCHMODE_ASSOC, array());
  
    $this->tpl->assign('usersData', $res);
   }
          
  // ======================
  // ==== MODIFICATION ====
  // ======================
          
  function viewModifications ($product_id)
  {
      $sql = "select * from catalogs_modifications where product_id=? order by id desc";
      $row = $this->db->getAll ($sql, array (
          $product_id 
      ), DB_FETCHMODE_ASSOC);
      if (trim ($_REQUEST['test2']) == '1')
          print_r ($row);
      $this->tpl->assign ('modifications', $row);
  }
   
  function viewModificationsProduct ($product_id)
  {
      $sql = "select a1.* from catalogs_modifications a1 where a1.product_id=? and a1.active>0 order by a1.id asc";
      // $sql = "select m.* from catalogs_keys_products_cities k left join catalogs_modifications m on m.code=k.mod_code where k.product_id=? and k.count>0 order by k.mod_code desc";
      $row = $this->db->getAll ($sql, array (
          $product_id 
      ), DB_FETCHMODE_ASSOC);
      
      for ($i = 0; $i < count ($row); $i ++)
7a508b62   andryeyev   + попытка исправи...
620
      {
1b81d16b   andryeyev   + откат до пред. ...
621
622
          $row2 = $this->db->getRow ("SELECT * FROM catalogs_keys_products_cities WHERE product_id='" . $product_id . "' AND mod_code='" . @$row[$i]['code'] . "' LIMIT 1;", array (), DB_FETCHMODE_ASSOC);
          // if($row2['city_id']==25){ unset($row[$i]); }
7a508b62   andryeyev   + попытка исправи...
623
      }
1b81d16b   andryeyev   + откат до пред. ...
624
625
626
      $new_row = array ();
      // if($_GET['test']=='18') print_r($row);
      foreach ($row as $key => $value)
7a508b62   andryeyev   + попытка исправи...
627
      {
1b81d16b   andryeyev   + откат до пред. ...
628
          // $new_row[] = $value;
7a508b62   andryeyev   + попытка исправи...
629
      }
1b81d16b   andryeyev   + откат до пред. ...
630
631
632
633
      // if($_GET['test']=='18') print_r($new_row);
      $this->tpl->assign ('modifications', $row);
      return $row;
  }
42868d70   andryeyev   Создал GIT
634
  
1b81d16b   andryeyev   + откат до пред. ...
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
  function viewModificationOne ($id)
  {
      $sql = "select * from catalogs_modifications where id=?";
      $row = $this->db->getRow ($sql, array (
          $id 
      ), DB_FETCHMODE_ASSOC);
      $this->tpl->assign ('modification', $row);
      return $row;
  }
  
  function getModificationProduct_id ($code)
  {
      return $this->db->getOne ("select product_id from catalogs_modifications where code=?", array (
          $code 
      ));
  }
42868d70   andryeyev   Создал GIT
651
  
1b81d16b   andryeyev   + откат до пред. ...
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
  function getModification ($product_id, $code)
  {
      return $this->db->getOne ("
          SELECT id 
          FROM catalogs_modifications 
          WHERE product_id=? 
              AND code=?", array (
          $product_id,
          $code
      ));  
  }
   
  function SaveModification ($data, $upload)
  {
      $table_name = "catalogs_modifications";
      $DB_AUTOQUERY = ($data ['update_id'] > 0) ? DB_AUTOQUERY_UPDATE : DB_AUTOQUERY_INSERT;
      $fields_values = array (
          'product_id' => $data ['product_id'],
          'code' => $data ['code'],
          'size' => $data ['size'],
          'color' => $data ['color'],
          'active' => $data ['active'] 
      );
      
      if (isset ($data ['color2']))
7a508b62   andryeyev   + попытка исправи...
677
      {
1b81d16b   andryeyev   + откат до пред. ...
678
          $fields_values ['color2'] = $data ['color2'];
7a508b62   andryeyev   + попытка исправи...
679
      }
1b81d16b   andryeyev   + откат до пред. ...
680
681
      
      if (isset ($data ['rost']))
e8b0e34e   andryeyev   + fix дублей моди...
682
      {
1b81d16b   andryeyev   + откат до пред. ...
683
          $fields_values ['rost'] = $data ['rost'];
e8b0e34e   andryeyev   + fix дублей моди...
684
      }
1b81d16b   andryeyev   + откат до пред. ...
685
686
      
      if (isset ($data ['delete_pic']))
e8b0e34e   andryeyev   + fix дублей моди...
687
      {
1b81d16b   andryeyev   + откат до пред. ...
688
689
690
691
          if ($data ['update_id'] > 0)
          {
              $this->deleteModificationPics ($data ['update_id']);
          }
42868d70   andryeyev   Создал GIT
692
  
1b81d16b   andryeyev   + откат до пред. ...
693
694
695
696
697
          $fields_values ['pic'] = null;
          $fields_values ['pic_big'] = null;
      }
      
      if ($upload ['pic'] ['tmp_name'])
7a508b62   andryeyev   + попытка исправи...
698
      {
1b81d16b   andryeyev   + откат до пред. ...
699
          if ($data ['update_id'] > 0)
7a508b62   andryeyev   + попытка исправи...
700
          {
1b81d16b   andryeyev   + откат до пред. ...
701
702
703
704
705
706
707
708
              $delete_pics = (isset ($upload ['pic'] ['big']) && $upload ['pic'] ['big'] == true) ? array (
                  'pic' 
              ) : array (
                  'pic',
                  'pic_big' 
              );
              
              $this->deleteModificationPics ($data ['update_id'], $delete_pics);
7a508b62   andryeyev   + попытка исправи...
709
          }
1b81d16b   andryeyev   + откат до пред. ...
710
711
712
713
714
715
          
          $fields_values ['pic'] = upload_ImageResize ($upload ['pic'], array (
              'width' => "144",
              'height' => "144",
              'upload_path' => "./uploaded/pic/catalogs/modifications/" 
          ));
57a8be43   andryeyev   Исправил cron, им...
716
  
1b81d16b   andryeyev   + откат до пред. ...
717
718
719
720
          $fields_values ['pic_big'] = (isset ($upload ['pic'] ['big']) && $upload ['pic'] ['big'] == true) ? $upload ['pic'] ['name'] : upload_ImageResize ($upload ['pic'], array (
              'width' => "1000",
              'height' => "1000",
              'upload_path' => "./uploaded/pic/catalogs/modifications/big/" 
7a508b62   andryeyev   + попытка исправи...
721
722
          ));
      }
1b81d16b   andryeyev   + откат до пред. ...
723
724
725
726
          
      $this->db->autoExecute ($table_name, $fields_values, $DB_AUTOQUERY, "id='{$data['update_id']}'");
      return $id;
  }
57a8be43   andryeyev   Исправил cron, им...
727
  
1b81d16b   andryeyev   + откат до пред. ...
728
729
730
731
732
733
734
735
736
737
738
  function deleteModification ($id)
  {
      $this->deleteModificationPics ($id);
      $this->db->query ("delete from catalogs_modifications where id=?", array (
          $id 
      ));
  }
   
  function deleteProductModifications ($product_id, $nopic = false)
  {
      IF ($nopic == false)
57a8be43   andryeyev   Исправил cron, им...
739
      {
1b81d16b   andryeyev   + откат до пред. ...
740
741
742
743
744
745
          $sql = "
              select * 
              from catalogs_modifications 
              where product_id=?";
          $res = $this->db->getAll ($sql, array (
              $product_id 
7a508b62   andryeyev   + попытка исправи...
746
          ), DB_FETCHMODE_ASSOC);
7a508b62   andryeyev   + попытка исправи...
747
          
1b81d16b   andryeyev   + откат до пред. ...
748
          foreach ($res as $row)
7a508b62   andryeyev   + попытка исправи...
749
          {
1b81d16b   andryeyev   + откат до пред. ...
750
              $this->deleteModificationPics ($row ['id']);
7a508b62   andryeyev   + попытка исправи...
751
          }
7a508b62   andryeyev   + попытка исправи...
752
      }
1b81d16b   andryeyev   + откат до пред. ...
753
754
  
      $this->db->query ("
7a508b62   andryeyev   + попытка исправи...
755
756
          delete from catalogs_modifications 
          where product_id=?", array (
57a8be43   andryeyev   Исправил cron, им...
757
              $product_id 
1b81d16b   andryeyev   + откат до пред. ...
758
759
760
761
762
763
      ));
  }
  
  function deleteModificationPics ($id, $pics = array('pic_big','pic'))
  {
      $sql = "select pic,pic_big from catalogs_modifications where id=?";
7a508b62   andryeyev   + попытка исправи...
764
      
1b81d16b   andryeyev   + откат до пред. ...
765
766
767
768
769
      $row = $this->db->getRow ($sql, array (
          $id 
      ), DB_FETCHMODE_ASSOC);
  
      if (in_array ('pic', $pics))
57a8be43   andryeyev   Исправил cron, им...
770
      {
1b81d16b   andryeyev   + откат до пред. ...
771
          @unlink ("{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/modifications/{$row['pic']}");
57a8be43   andryeyev   Исправил cron, им...
772
      }
1b81d16b   andryeyev   + откат до пред. ...
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
  
      // if(in_array('pic_big',$pics))@unlink("{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/modifications/big/{$row['pic_big']}");
  }
  
  // =================
  // ==== PRODUCT ====
  // =================
  
   function ProductClick($id){
    $this->db->query("update catalogs_products set click=click+1 where id='$id'");
   }
  
  function SaveProduct ($data, $upload)
  {
      $table_name = "catalogs_products";
      
      if (trim (@$data['translit']) == "")
          $data['translit'] = translit ($data['name']);
      
      $DB_AUTOQUERY = ($data['update_id'] > 0) ? DB_AUTOQUERY_UPDATE : DB_AUTOQUERY_INSERT;
      $id = ($data['update_id'] > 0) ? $data['update_id'] : $this->db->nextId ('mySequence_' . $table_name);
      $fields_values = array (
          'id' => $id,
          'rubric_id' => $data['rubric_id'],
          'rubric_parent_id' => $this->getRubricParent ($data['rubric_id']),
          'brend_id' => $data['brend_id'],
          'name' => $data['name'],
          'about' => $data['about'],
          'text' => $data['text'],
          'update_text' => (int)$data['update_text'],    
          'strnumber' => $data['strnumber'],
          'cine' => $data['cine'],
          'cine_last' => $data['cine_last'],
          'mktime' => mktime (),
          'meta_title' => $data['meta_title'],
          'meta_description' => $data['meta_description'],
          'meta_keywords' => $data['meta_keywords'],
          'meta_about' => $data['meta_about'],
          'active' => $data['active'],
          'translit' => $data['translit'] 
      );
      if (isset ($data['is_cost2']))
          $fields_values['is_cost2'] = 1;
      else
          $fields_values['is_cost2'] = 0;
      if (isset ($data['cine2']))
          $fields_values['cine2'] = $data['cine2'];
      if (isset ($data['params']))
          $this->saveProductParams ($id, $data['params']);
      if (isset ($data['doc']))
          $fields_values['doc'] = $data['doc'];
      if (isset ($data['update_text']))
          $fields_values['update_text'] = $data['update_text'];
      if (isset ($data['video_code']))
          $fields_values['video_code'] = $data['video_code'];
      if (isset ($data['akcii_id']))
          $fields_values['akcii_id'] = $data['akcii_id'];
      
      if (isset ($data['translit']))
          $fields_values['translit'] = $data['translit'];
      
      if ($upload['pic']['tmp_name'])
      {
          $delete_pics = (isset ($upload['pic']['big']) && $upload['pic']['big'] == true) ? array (
              'pic',
              'pic2' 
          ) : array (
              'pic',
              'pic2',
              'pic_big' 
c52bd933   andryeyev   - удаление модифи...
843
          );
1b81d16b   andryeyev   + откат до пред. ...
844
845
          $this->deleteProductPics ($id, $delete_pics);
          $fields_values['pic'] = upload_ImageResize ($upload['pic'], array (
c52bd933   andryeyev   - удаление модифи...
846
847
848
849
850
851
852
853
854
855
856
857
858
859
                  'width' => "144",
                  'height' => "144",
                  'upload_path' => "./uploaded/pic/catalogs/products/" 
              ));
              $fields_values['pic2'] = upload_ImageResize ($upload['pic'], array (
                  'width' => "244",
                  'height' => "244",
                  'upload_path' => "./uploaded/pic/catalogs/products/2/" 
              ));
              $fields_values['pic_big'] = (isset ($upload['pic']['big']) && $upload['pic']['big'] == true) ? $upload['pic']['name'] : upload_ImageResize ($upload['pic'], array (
                  'width' => "1000",
                  'height' => "1000",
                  'upload_path' => "./uploaded/pic/catalogs/products/big/" 
              ));
7a508b62   andryeyev   + попытка исправи...
860
      }
1b81d16b   andryeyev   + откат до пред. ...
861
862
863
864
865
866
867
868
          
      $this->db->autoExecute ($table_name, $fields_values, $DB_AUTOQUERY, "id='$id'");
      $this->save_keysProductsFilters ($id, $data);
      $this->save_keysProductsFilters2 ($id, $data);
      $this->save_keysProductsYears ($id, $data);
      // $this->save_keysProductsCities($id,$data);
      $this->save_keysProductsAkcii ($id, $data);
      $this->save_keysProductsSex ($id, $data);
7a508b62   andryeyev   + попытка исправи...
869
      
1b81d16b   andryeyev   + откат до пред. ...
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
      return $id;
  }
   
   function save_keysProductsSex($id,$data){
    $this->delete_keysProductsSex($id);
    if(!isset($data['sex']))return false;
    $table_name = "catalogs_keys_products_sex";
    foreach($data['sex'] as $sex){
     $this->db->autoExecute($table_name,array('product_id'=>$id,'sex'=>$sex,'rubric_id'=>$data['rubric_id']),DB_AUTOQUERY_INSERT);
    }
   }
  
   function delete_keysProductsSex($id){
    $this->db->query("delete from catalogs_keys_products_sex where product_id=?",array($id));
   } 
   
   function save_keysProductsFilters2($id,$data){
    $this->delete_keysProductsFilters2($id);
    if(!isset($data['filter2']))return false;
    $table_name = "catalogs_keys_products_filters2";
    foreach($data['filter2'] as $filter_id){
     $this->db->autoExecute($table_name,array('product_id'=>$id,'filter_id'=>$filter_id,'rubric_id'=>$data['rubric_id']),DB_AUTOQUERY_INSERT);
    }
   }
  
   function delete_keysProductsFilters2($id){
    $this->db->query("delete from catalogs_keys_products_filters2 where product_id=?",array($id));
   } 
   
   function save_keysProductsFilters($id,$data){
    $this->delete_keysProductsFilters($id);
    if(!isset($data['filter']))return false;
    $table_name = "catalogs_keys_products_filters";
    foreach($data['filter'] as $filter_id){
     $this->db->autoExecute($table_name,array('product_id'=>$id,'filter_id'=>$filter_id,'rubric_id'=>$data['rubric_id']),DB_AUTOQUERY_INSERT);
    }
   }
   
   function delete_keysProductsFilters($id){
    $this->db->query("delete from catalogs_keys_products_filters where product_id=?",array($id));
   }
   
   function save_keysProductsYears($id,$data){
    $this->delete_keysProductsYears($id);
    if(!isset($data['years']))return false;
    $table_name = "catalogs_keys_products_years";
    foreach($data['years'] as $filter_id){
     $this->db->autoExecute($table_name,array('product_id'=>$id,'year_id'=>$filter_id,'rubric_id'=>$data['rubric_id']),DB_AUTOQUERY_INSERT);
    }
   }
   
   function delete_keysProductsYears($id){
    $this->db->query("delete from catalogs_keys_products_years where product_id=?",array($id));
   }
   
   function save_keysProductsCities($id,$data){
    $this->delete_keysProductsCities($id);
    if(!isset($data['city']))return false;
    $table_name = "catalogs_keys_products_cities";
    foreach($data['city'] as $city_id){
     $this->db->autoExecute($table_name,array('product_id'=>$id,'city_id'=>$city_id),DB_AUTOQUERY_INSERT);
    }
   }
   
   function delete_keysProductsAkcii($id){
    $this->db->query("delete from catalogs_keys_products_akcii where product_id=?",array($id));
   }
  
   function save_keysProductsAkcii($id,$data){
    $this->delete_keysProductsAkcii($id);
    if(!isset($data['akcii']))return false;
    $table_name = "catalogs_keys_products_akcii";
    foreach($data['akcii'] as $rubric_id){
     $this->db->autoExecute($table_name,array('product_id'=>$id,'rubric_id'=>$rubric_id),DB_AUTOQUERY_INSERT);
    }
   }
   
   function delete_keysProductsCities($id){
    $this->db->query("delete from catalogs_keys_products_cities where product_id=?",array($id));
   }
  
   
   function saveProductSizeCine($id,$data){
    $this->db->query("delete from catalogs_products_cine where product_id=?",array($id));
    foreach($data['size'] as $key=>$size){
     $this->db->autoExecute("catalogs_products_cine",array('product_id'=>$id,'size'=>$size,'cine'=>$data['cine'][$key],'metka'=>$data['size_metka'][$key],'n'=>$key),DB_AUTOQUERY_INSERT);
    }
   }
   
  
   function productsIsHits($products,$hits,$tops,$akcii){
    $sql = "update catalogs_products set active=?,top=?,akcii_id=? where id=?";
    foreach($products as $id){
     if(@in_array($id,$hits))$hit = true;
     else $hit = false;
     if(@in_array($id,$tops))$top = true;
     else $top = false;
     if(@in_array($id,$akcii))$akcii = true;
     else $akcii = false;
     $this->db->query($sql,array($hit,$top,$akcii, $id));
    }
   }
   
   function productsIsHits2($data,$hits){
    $sql = "update catalogs_products set hit=?,hit_sort=? where id=?";
    foreach($data['product_id'] as $key=>$id){
     if(@in_array($id,$hits))$hit = true;
     else $hit = false;
     $this->db->query($sql,array($hit,$data['hit_sort'][$key],$id));
    }
   }
   
   function viewProductsRubrics($rubrics,$limit=20){
    $data = array();
    foreach($rubrics as $key=>$rubric_id){
     $data[$key]['rubric']['name'] = $this->getRubricName($rubric_id);
     $data[$key]['rubric']['id'] = $rubric_id;
     
     $count = $this->db->getOne("select count(*) from catalogs_products WHERE count_modifications>0 and rubric_id=?",array($rubric_id));
     $limit = 5; $round = ($count>$limit) ? rand(0,($count-$limit)) : $limit;
     $sql = "select * from catalogs_products where count_modifications>0 and rubric_id=? order by id " . ((rand(1,1000)%2) ? "asc" : "desc") . " limit $round,$limit";
     $data[$key]['products'] = $this->db->getAll($sql,array($rubric_id),DB_FETCHMODE_ASSOC);
    }
   /* print"<pre>";
    print_r($data);
    print"</pre>";   */
    $this->tpl->assign('products',$data);
   }
   
   function seralizeFilter($str){
    $array = explode(';',iconv('UTF-8','CP1251',$str));
    $filter = array();
    foreach($array as $key=>$value){
     $filter[substr($value,0,1)][] = "'".substr(str_replace(".", ",", $value),1, strpos($value,"_")-1 )."'";
    }
    //print_r($filter);
    return $filter;
   }
   
   function seralizeSex($str){
  	$sex = array();
  	$sex_num = array('male'=>1,'female'=>2,'child'=>3,'unisex'=>4);
  	$sex_row = explode(';',$str);
  	foreach($sex_row as $row){	
  		$sex[] = $sex_num[$row];
  	}
  	return $sex;
   }
   
  
   function viewProducts($params,$rubricsP = false,$count_modf = true,$lang='ru'){
           
           
           //exit;
           //print $params['filter_products'];
           
    if(isset($params['filter']) && count($params['filter'])>0)$filter = $this->seralizeFilter($params['filter']);
    $search = array();
    if(isset($_GET['perPage']) && $_GET['perPage']>0 && $_GET['perPage']<=100)$perPage = $_GET['perPage'];else $perPage = 20;
    if(isset($params['search_str']) && strlen($params['search_str'])>0){ 
     $array_string = explode(" ",trim($params['search_str']));
  			foreach($array_string as $row){
  			$search[] = "(catalogs_products.name LIKE  '%".$row."%' OR catalogs_products.text LIKE  '%".$row." %' OR catalogs_rubrics.name LIKE  '%".$row."%' OR catalogs_brends.name LIKE  '%".$row."%' OR catalogs_modifications.code='".$row."')";
  			}
     $search_string = array();
    //  $search_string[] = "catalogs_products.name like '%".mysql_real_escape_string($params['search_str'])."%'";
     // $search_string[] = "catalogs_products.text like '%".mysql_real_escape_string($params['search_str'])."%'";
   /*  foreach($array_string as $str){
      $search_string[] = "catalogs_products.name like '$str'";
      $search_string[] = "catalogs_products.name like'% $str %'";
      $search_string[] = "catalogs_products.name like'%$str %'";
      $search_string[] = "catalogs_products.name like'% $str%'";
  
      $search_string[] = "catalogs_products.text like '$str'";
      $search_string[] = "catalogs_products.text like'% $str %'";
      $search_string[] = "catalogs_products.text like'%$str %'";
      $search_string[] = "catalogs_products.text like'% $str%'";
     } */
     //$search[] = "((" . implode(" OR ",$search_string) . ") OR ". sprintf("catalogs_modifications.code='%s'", mysql_real_escape_string($params['search_str'])).")";
    }
        // print_r($params);
    if(isset($params['brendID']) && $params['brendID']>0)$search[] = sprintf("catalogs_products.brend_id='%d'",$params['brendID']);
    if(isset($params['akciiID']) && $params['akciiID']>0)$search[] = sprintf("catalogs_products.akcii_id>'%d'",0);
    if(isset($params['new']) && $params['new']>0)$search[] = sprintf("catalogs_products.active='%d'",$params['new']);
    if(isset($params['top']) && $params['top']>0)$search[] = sprintf("catalogs_products.top='%d'",$params['top']);
    if(isset($params['brend']) && strlen($params['brend'])>0)$search[] = "catalogs_products.brend_id in(".implode(",",$this->getBrendIDs_name($params['brend'])).")";
    if(isset($params['sex']) && strlen($params['sex'])>0){
    $sex = $this->seralizeSex($params['sex']);
    //print_r($sex);
  
   // $search[] = sprintf("catalogs_products.sex='%d'",$sex[$params['sex']]);
  
    $search[] = "s.sex IN (" . implode(",",$sex) . ")";
    }
    //if(isset($params['akcii']) && $params['akcii']>0)$search[] = sprintf("catalogs_products.hit='%d'",$params['akcii']);
    if($count_modf==true)$search[] = "catalogs_products.count_modifications>0";
    if(isset($params['rubID']) && $params['rubID']>0 && !isset($params['filter_products']) && !isset($params['what'])){
     $search[] = ($rubricsP==true)?sprintf("(catalogs_products.rubric_id='%d')",$params['rubID']):sprintf("
     (catalogs_products.rubric_parent_id='%d' || 
     catalogs_products.rubric_id='%d' || 
     catalogs_products.rubric_id=(SELECT id FROM catalogs_rubrics WHERE translit='%d') ||
     catalogs_products.rubric_parent_id=(SELECT id FROM catalogs_rubrics WHERE translit='%d'))",$params['rubID'],$params['rubID'],$params['rubID'],$params['rubID']);
    }
    
   /* if(isset($params['filterID'])){
     //$search[] = sprintf("catalogs_keys_products_filters.filter_id='%d'", $params['filterID']);
     $search[] = "catalogs_keys_products_filters.filter_id in (".implode(",",$params['filterID']).")";
    }*/
  
    if(isset($filter['f']) && count($filter['f'])>0){
     $search[] = "catalogs_keys_products_filters.filter_id in (".implode(",",$filter['f']).")";
    }
    elseif(isset($params['filter_products']) && strlen($params['filter_products'])>0 && !isset($params['what'])){
     $search[] = "catalogs_keys_products_filters.filter_id in (".$params['filter_products'].")";
    }
    if(isset($filter['g']) && count($filter['g'])>0){
     $search[] = "catalogs_keys_products_filters2.filter_id in (".implode(",",$filter['g']).")";
    }
    if(isset($filter['s']) && count($filter['s'])>0){
     $search[] = "catalogs_modifications.size in (".implode(",",$filter['s']).")";
    }
    if(isset($filter['r']) && count($filter['r'])>0){ 
     $search[] = "m2.rost in (".implode(",",$filter['r']).")";
    }    
  /*  if(isset($params['yearID']) && $params['yearID']>0){
     //$search[] = sprintf("catalogs_keys_products_years.year_id='%d'", $params['yearID']);
     $search[] = "catalogs_keys_products_years.year_id in (".implode(",",$params['yearID']).")";
    }*/
    if(isset($filter['y']) && $filter['y']>0){
     $search[] = "catalogs_keys_products_years.year_id in (".implode(",",$filter['y']).")";
    }
    $sql = "select catalogs_rubrics.translit as rubric_translit,catalogs_products.*,catalogs_products.name".($lang=='ukr'?'':'')." as name,catalogs_brends.name as brend_name,catalogs_rubrics.list_name".($lang=='ukr'?'_ukr':'')." as list_name from catalogs_products LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id LEFT JOIN catalogs_rubrics ON catalogs_rubrics.id=catalogs_products.rubric_id ";
    if(isset($params['akcii']) && $params['akcii']>0)$sql .= "RIGHT JOIN catalogs_keys_products_akcii ON catalogs_keys_products_akcii.product_id=catalogs_products.id ";
    if(isset($filter['f']) && count($filter['f'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
    elseif(isset($params['filter_products']) && strlen($params['filter_products'])>0 && !isset($params['what']))$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
  
    if(isset($filter['g']) && count($filter['g'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters2 ON catalogs_keys_products_filters2.product_id=catalogs_products.id ";
  
    //if(isset($params['filterID']) && $params['filterID']>0)$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
  //  if(isset($params['yearID']) && $params['yearID']>0)$sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=catalogs_products.id ";
    if(isset($filter['y']) && $filter['y']>0)$sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=catalogs_products.id ";
    if(isset($params['search_str']) && strlen($params['search_str'])>0){$sql .= "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=catalogs_products.id ";}
    if(isset($filter['s']) && count($filter['s'])>0){$sql .= "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=catalogs_products.id and catalogs_modifications.active=1 ";}
    if(isset($filter['r']) && count($filter['r'])>0){$sql .= "LEFT JOIN catalogs_modifications m2 ON m2.product_id=catalogs_products.id and m2.active=1 ";}
    if(isset($params['sex']) && strlen($params['sex'])>0){$sql .= "LEFT JOIN catalogs_keys_products_sex s ON s.product_id=catalogs_products.id ";}
  
    
    $sql .= "WHERE 1=1 ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
   // if(isset($params['akcii']) && $params['akcii']>0){$sql .= " GROUP BY catalogs_keys_products_akcii.product_id ";}
    //if(isset($params['search_str']) && strlen($params['search_str'])>0){}
    $sql .= " GROUP BY catalogs_products.id ";
    $sql .= "order by ";
    if(isset($params['sort'])){
     switch($params['sort']){
      case 'cine' : $sql .= " catalogs_products.cine " . (($_GET['order']=='asc')?"asc":"desc"); break;
      case 'click' : $sql .= " catalogs_products.click " . (($_GET['order']=='asc')?"asc":"desc"); break;
  	case 'rating' : $sql .= " catalogs_products.rating " . (($_GET['order']=='asc')?"asc":"desc"); break;
      case 'hit' : $sql .= " catalogs_products.hit_sort asc"; break;
      default : $sql .= " catalogs_products.mktime desc";
     }
    }else $sql .= (!empty($params['top']))?" catalogs_rubrics.top desc":" catalogs_products.rating desc";
  
    $pagerOptions = Array(
      'mode' => 'Sliding',
      'delta' => 5,
      'perPage' => $perPage,
      'linkClass'=>"link_class",
      'curPageLinkClassName'=>"link_class_active",
      'spacesBeforeSeparator' => 1,
      'spacesAfterSeparator' => 1
    );
          // echo $sql;
          // $sql="";
          $res = Pager_Wrapper_DB ($this->db, $sql, $pagerOptions, false, DB_FETCHMODE_ASSOC, array ());
          if (! empty ($res))
7a508b62   andryeyev   + попытка исправи...
1146
          {
1b81d16b   andryeyev   + откат до пред. ...
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
              // if($_GET['test']=='1')
              for ($i = 0; $i < count ($res['data']); $i ++)
              {
                  $res['data'][$i]['mod'] = $this->db->getRow ('select * from catalogs_modifications where product_id=? and active=1 Order By id Asc', array (
                      $res['data'][$i]['id'] 
                  ), DB_FETCHMODE_ASSOC);
                  $res['data'][$i]['ph1'] = $this->getFilterPh1 ($res['data'][$i]['id'], $lang);
                  
                  $res['data'][$i]['rating2'] = $res['data'][$i]['rating'];
                  if (@$res['data'][$i]['rating'])
                      $res['data'][$i]['rating'] = round ((@$res['data'][$i]['rating'] / @$res['data'][$i]['vote_num']), 0);
                  else
                      $res['data'][$i]['rating'] = 0;
                  $res['data'][$i]['rating'] = @$res['data'][$i]['rating'] * 17;
              }
7a508b62   andryeyev   + попытка исправи...
1162
          }
1b81d16b   andryeyev   + откат до пред. ...
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
        
          $this->tpl->assign ('productsData', $res);
          return $res;
   }
   
   function getFilterPh1($product_id,$lang = 'ru'){
  	$sql = "select ph1".($lang=='ukr'?'_ukr':'')." as ph1 from catalogs_keys_products_filters k LEFT JOIN catalogs_filters f ON f.id=k.filter_id where k.product_id=? AND f.ph1".($lang=='ukr'?'_ukr':'')."<>''";
  	return $this->db->getOne($sql,array($product_id));
   }
   
   function getCineCurs($type_curs,$cine){
    $curs = array();
    
       $uah = $this->db->getOne("select curs from catalogs_curs where type=?",array('uah'));
       $uah_bank = $this->db->getOne("select curs from catalogs_curs where type=?",array('uah_bank'));
       $usd = $this->db->getOne("select curs from catalogs_curs where type=?",array('usd'));
       
    switch($type_curs){
     case 'uah' :
      $curs['uah'] = round($cine,2);
      $curs['uah_bank'] = round(($cine/$usd)*$uah_bank,2);
      $curs['usd'] = round($cine/$usd,2);
     break;
     case 'uah_bank' :
      $curs['uah'] = round(($cine/$uah_bank)*$uah,2);
      $curs['uah_bank'] = round($cine,2);
      $curs['usd'] = round($cine/$uah_bank,2);
     break;
     case 'usd' :
      $curs['uah'] = round($cine*$uah,2);
      $curs['uah_bank'] = round($cine*$uah_bank,2);
      $curs['usd'] = round($cine,2);
     break;
    }
    return $curs;
   }
   
   function viewProductsAkcii($rubric_id,$limit = 8){
    // $sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    // LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    // LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id
    // WHERE catalogs_products.top=? and catalogs_products.count_modifications>0 ";
    // if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    // $sql .= "order by RAND() LIMIT $limit";
  
   $search = array();
   if(isset($_GET['filter']) && count($_GET['filter'])>0)$filter = $this->seralizeFilter($_GET['filter']);
    if(isset($filter['f']) && count($filter['f'])>0){
     $search[] = "catalogs_keys_products_filters.filter_id in (".implode(",",$filter['f']).")";
    }
    if(isset($filter['g']) && count($filter['g'])>0){
     $search[] = "catalogs_keys_products_filters2.filter_id in (".implode(",",$filter['g']).")";
    }
    if(isset($filter['s']) && count($filter['s'])>0){
     $search[] = "catalogs_modifications.size in (".implode(",",$filter['s']).")";
    }
    if(isset($filter['y']) && $filter['y']>0){
     $search[] = "catalogs_keys_products_years.year_id in (".implode(",",$filter['y']).")";
    }
  
  
    if(isset($_GET['brend']) && strlen($_GET['brend'])>0)$search[] = "catalogs_products.brend_id in(".implode(",",$this->getBrendIDs_name($_GET['brend'])).")";
  /*  if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
  	$sex = array('male'=>1,'female'=>2,'child'=>3,'unisex'=>4);
  	$search[] = sprintf("catalogs_products.sex='%d'",$sex[$_GET['sex']]);
    } */
     if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
     $sex = $this->seralizeSex($_GET['sex']);
     $search[] = "catalogs_products.sex IN (" . implode(",",$sex) . ")";
    }  
    
   /*$sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_keys_products_akcii 
   left join catalogs_products on catalogs_products.id=catalogs_keys_products_akcii.product_id 
   LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
   LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id ";*/
   $sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
   LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
   LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id "; 
    if(isset($filter['f']) && count($filter['f'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
    if(isset($filter['g']) && count($filter['g'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters2 ON catalogs_keys_products_filters2.product_id=catalogs_products.id ";
    if(isset($filter['y']) && $filter['y']>0)$sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=catalogs_products.id ";
    if(isset($filter['s']) && count($filter['s'])>0){$sql .= "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=catalogs_products.id and catalogs_modifications.active=1 ";}
   //$sql .= "where catalogs_keys_products_akcii.rubric_id=? ";
   $sql .= "where catalogs_products.count_modifications>0 and catalogs_products.akcii_id>0 ";
   if($rubric_id>0)$sql .="and catalogs_products.rubric_id='$rubric_id' ";
   if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    $sql .= "order by RAND()";
   if($rubric_id==0){$sql .= " limit 8";}  
    $res = $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);
    //print_r($res);
  for($i=0;$i<count($res);$i++){
          $res[$i]['rating2'] = $res[$i]['rating'];
          if( @$res[$i]['rating'] ) $res[$i]['rating'] = round( (@$res[$i]['rating'] / @$res[$i]['vote_num']), 0 );
          else $res[$i]['rating'] = 0;
          $res[$i]['rating'] = @$res[$i]['rating'] * 17;
  }  
    $this->tpl->assign('products_akcii',$res);
   }
   
   function getProductsAkcii($limit = 6){
   $sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
   LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
   LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id "; 
   //$sql .= "where catalogs_keys_products_akcii.rubric_id=? ";
   $sql .= "where catalogs_products.count_modifications>0 and catalogs_products.akcii_id>0 ";
   //if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    $sql .= "order by RAND() limit $limit";
    return $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);	
   }
  
   function viewProductsHitsAll2(){
    if(isset($_GET['perPage']) && $_GET['perPage']>0 && $_GET['perPage']<=100)$perPage = $_GET['perPage'];else $perPage = 20;
    $sql = "select * from catalogs_products where hit=?";
    $pagerOptions = Array(
      'mode' => 'Sliding',
      'delta' => 6,
      'perPage' => $perPage,
      'linkClass'=>"link_class",
      'curPageLinkClassName'=>"link_class_active",
      'spacesBeforeSeparator' => 1,
      'spacesAfterSeparator' => 1
    );
    $res = Pager_Wrapper_DB($this->db, $sql, $pagerOptions, false, DB_FETCHMODE_ASSOC, array(true));
  
    $this->tpl->assign('productsData', $res);
   }
  
   function viewProductsActiveAll($params,$limit = 4,$curs = null){
    $search = array();
    
    $sql = "
    SELECT * FROM (
    select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id
    WHERE catalogs_products.active=? and catalogs_products.count_modifications>0 ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    $sql .= "ORDER BY RAND()) as T ";
    $sql .= "Group By rubric_id ORDER BY RAND() LIMIT $limit";
    $res = $this->db->getAll($sql,array(true),DB_FETCHMODE_ASSOC);
    //print_r($res);
    $this->tpl->assign('products_active',$res);
   }
   
   function viewProductsActive($params,$limit = 4,$curs = null){
    $search = array();
    if(isset($params['rubID']) && $params['rubID']>0 && !isset($params['filter_products']))$search[] = sprintf("catalogs_products.rubric_id=%d",$params['rubID']);
    if(isset($params['filter']) && count($params['filter'])>0)$filter = $this->seralizeFilter($params['filter']);
    
    if(isset($filter['f']) && count($filter['f'])>0){
     $search[] = "catalogs_keys_products_filters.filter_id in (".implode(",",$filter['f']).")";
    }
    if(isset($filter['g']) && count($filter['g'])>0){
     $search[] = "catalogs_keys_products_filters2.filter_id in (".implode(",",$filter['g']).")";
    }
    if(isset($filter['s']) && count($filter['s'])>0){
     $search[] = "catalogs_modifications.size in (".implode(",",$filter['s']).")";
    }
    if(isset($filter['y']) && $filter['y']>0){
     $search[] = "catalogs_keys_products_years.year_id in (".implode(",",$filter['y']).")";
    }
    if(isset($params['filter_products']) && strlen($params['filter_products'])>0 && !isset($filter['f'])){
     $search[] = "catalogs_keys_products_filters.filter_id in (".$params['filter_products'].")";
    } 
  
    if(isset($params['brend']) && strlen($params['brend'])>0)$search[] = "catalogs_products.brend_id in(".implode(",",$this->getBrendIDs_name($params['brend'])).")";
  
  /*  if(isset($params['sex']) && strlen($params['sex'])>0){
  	$sex = array('male'=>1,'female'=>2,'child'=>3,'unisex'=>4);
  	$search[] = sprintf("catalogs_products.sex='%d'",$sex[$params['sex']]);
    }  */
     if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
     $sex = $this->seralizeSex($_GET['sex']);
     $search[] = "s.sex IN (" . implode(",",$sex) . ")";
    }  
    
    $sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id ";
    if(isset($filter['f']) && count($filter['f'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
    if(isset($filter['g']) && count($filter['g'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters2 ON catalogs_keys_products_filters2.product_id=catalogs_products.id ";
    if(isset($filter['y']) && $filter['y']>0)$sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=catalogs_products.id ";
    if(isset($filter['s']) && count($filter['s'])>0){$sql .= "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=catalogs_products.id and catalogs_modifications.active=1 ";}
    if(isset($params['filter_products']) && strlen($params['filter_products'])>0 && !isset($filter['f']))$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
     if(isset($params['sex']) && strlen($params['sex'])>0){$sql .= "LEFT JOIN catalogs_keys_products_sex s ON s.product_id=catalogs_products.id ";}
       
    $sql .= "WHERE catalogs_products.active=? and catalogs_products.count_modifications>0 ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    $sql .= "order by RAND() LIMIT $limit";
    $res = $this->db->getAll($sql,array(true),DB_FETCHMODE_ASSOC);
    //print_r($res);
    $this->tpl->assign('products_active',$res);
   } 
   
   function viewProductsHits($params,$limit = 4,$curs = null){
    $search = array();
    if(isset($params['rubID']) && $params['rubID']>0)$search[] = sprintf("catalogs_products.rubric_id=%d",$params['rubID']);
    
    $sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id
    WHERE catalogs_products.hit=? and catalogs_products.count_modifications>0 ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    $sql .= "order by ";
    if(isset($params['sort'])){
     switch($params['sort']){
      case 'cine' : $sql .= " catalogs_products.cine " . (($_GET['order']=='asc')?"asc":"desc"); break;
      case 'click' : $sql .= " catalogs_products.click " . (($_GET['order']=='asc')?"asc":"desc"); break;
  	case 'rating' : $sql .= " catalogs_products.rating " . (($_GET['order']=='asc')?"asc":"desc"); break;
      case 'hit' : $sql .= " catalogs_products.hit_sort asc"; break;
      default : $sql .= " RAND()";
     }
    }else $sql .= " RAND()";
    $sql .=" LIMIT $limit";
    $res = $this->db->getAll($sql,array(true),DB_FETCHMODE_ASSOC);
    //print_r($res);
    $this->tpl->assign('products_hits',$res);
   }
   
   function viewProductsTops($params,$limit = 4,$curs = null){
    $search = array();
    if(isset($params['rubID']) && $params['rubID']>0 && !isset($params['filter_products']))$search[] = sprintf("catalogs_products.rubric_id=%d",$params['rubID']);
    if(isset($params['filter']) && count($params['filter'])>0)$filter = $this->seralizeFilter($params['filter']);
    
    if(isset($filter['f']) && count($filter['f'])>0){
     $search[] = "catalogs_keys_products_filters.filter_id in (".implode(",",$filter['f']).")";
    }
    if(isset($filter['g']) && count($filter['g'])>0){
     $search[] = "catalogs_keys_products_filters2.filter_id in (".implode(",",$filter['g']).")";
    }
    if(isset($filter['s']) && count($filter['s'])>0){
     $search[] = "catalogs_modifications.size in (".implode(",",$filter['s']).")";
    }
    if(isset($filter['y']) && $filter['y']>0){
     $search[] = "catalogs_keys_products_years.year_id in (".implode(",",$filter['y']).")";
    }
    if(isset($params['filter_products']) && strlen($params['filter_products'])>0 && !isset($filter['f'])){
     $search[] = "catalogs_keys_products_filters.filter_id in (".$params['filter_products'].")";
    }
  
    if(isset($params['brend']) && strlen($params['brend'])>0)$search[] = "catalogs_products.brend_id in(".implode(",",$this->getBrendIDs_name($params['brend'])).")";
    
    /*if(isset($params['sex']) && strlen($params['sex'])>0){
  	$sex = array('male'=>1,'female'=>2,'child'=>3,'unisex'=>4);
  	$search[] = sprintf("catalogs_products.sex='%d'",$sex[$params['sex']]);
    }*/
     if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
     $sex = $this->seralizeSex($_GET['sex']);
     $search[] = "catalogs_products.sex IN (" . implode(",",$sex) . ")";
    }  
    
    $sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id ";
    if(isset($filter['f']) && count($filter['f'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
    if(isset($filter['g']) && count($filter['g'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters2 ON catalogs_keys_products_filters2.product_id=catalogs_products.id ";
    if(isset($filter['y']) && $filter['y']>0)$sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=catalogs_products.id ";
    if(isset($filter['s']) && count($filter['s'])>0){$sql .= "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=catalogs_products.id and catalogs_modifications.active=1 ";}
    if(isset($params['filter_products']) && strlen($params['filter_products'])>0 && !isset($filter['f']))$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
      
    $sql .= "WHERE catalogs_products.top=? and catalogs_products.count_modifications>0 ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
   // $sql .= "order by RAND()";
    $sql .= "order by ";
    if(isset($params['sort'])){
     switch($params['sort']){
      case 'cine' : $sql .= " catalogs_products.cine " . (($_GET['order']=='asc')?"asc":"desc"); break;
      case 'click' : $sql .= " catalogs_products.click " . (($_GET['order']=='asc')?"asc":"desc"); break;
  	case 'rating' : $sql .= " catalogs_products.rating " . (($_GET['order']=='asc')?"asc":"desc"); break;
      case 'hit' : $sql .= " catalogs_products.hit_sort asc"; break;
      default : $sql .= " r.top desc";
     }
    }else $sql .= " r.top desc";
    $sql .=" LIMIT $limit";
    //print $sql;
    $res = $this->db->getAll($sql,array(true),DB_FETCHMODE_ASSOC);
    //print_r($res);
    $this->tpl->assign('products_top',$res);
   }
  
   function viewProductsTopOne(){
     $count = $this->db->getOne("select count(*) from catalogs_products WHERE catalogs_products.header=?",array(true));
     $limit = 1; $round = rand(0,($count-$limit));
     $res = $this->db->getRow("select * from catalogs_products WHERE catalogs_products.header=? order by id " . ((rand(1,1000)%2) ? "asc" : "desc") . " LIMIT $round,$limit",array(true),DB_FETCHMODE_ASSOC);
     $this->tpl->assign('products_top_one',$res);
   }
  
   function viewProductsLast($limit = 1){
    $search = array();
    $sql = "select catalogs_products.*,catalogs_brends.name as brend_name from catalogs_products LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id WHERE catalogs_products.active=? ";
    $sql .= "order by catalogs_products.mktime desc LIMIT $limit";
    $res = $this->db->getAll($sql,array(true),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('products_last',$res);
   }
   
   function viewProductsHitsAll($limit = 4){
    $sql = "select catalogs_products.*,catalogs_brends.name as brend_name from catalogs_products LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id WHERE hit=? ";
    $sql .= "order by mktime desc LIMIT $limit";
    $products_hits = $this->db->getAll($sql,array(true),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('products_hits_all',$products_hits);
   }
   
   function activeNull($rubric_id){
          $sql = "update catalogs_products set active=? where rubric_id=?";
          $this->db->query($sql,array(0,$rubric_id));
   }
   
   function viewProductOne($id,&$row){
  //  $sql = "select p.*,b.name as brend_name,b.text as brend_text,b.pic_s as brend_pic_s from catalogs_products p left join catalogs_brends b2 ON b2.id=p.brend_id left join catalogs_brends b ON b.name=b2.name and b.active=1 where p.id=?";
    $sql = "select p.*,b.name as brend_name,b.text as brend_text,b.pic_s as brend_pic_s from catalogs_products p left join catalogs_brends b ON b.id=p.brend_id where p.id=?";
  
    $row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
    $row['ph1'] = $this->getFilterPh1($row['id']);
    
          $row['rating2'] = $row['rating'];
          if( @$row['rating'] ) $row['rating'] = round( (@$row['rating'] / @$row['vote_num']), 0 );
          else $row['rating'] = 0;
          $row['rating'] = @$row['rating'] * 17;  
    //print_r($row);
    $this->tpl->assign('product',$row);
   }
   
   function deleteCompareProduct(&$compare,$id){
    foreach($compare as $key=>$_id){
     if($_id == $id)unset($compare[$key]);
    }
   }
   
   function viewCompareProducts($compare){
    $products = array();
    $i = 0;
    foreach($compare as $id){
     $sql = "select p.* from catalogs_products p where p.id=?";
     $products[$i] = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
     $products[$i]['params'] = $this->getProductParams($id);
     $i++;
    }  //print_r($products);
    $this->tpl->assign('products',$products);
   }
   
   function viewProductNembers($str_n){
    $arr_id = explode(",",$str_n);
    $products = array();
    foreach($arr_id as $id){
     $count = $this->db->getOne("select count(*) from catalogs_keys_products_filters where filter_id=?",array(trim($id)));
     $limit = 1; $round = rand(0,($count-$limit));                                                                                                                                                                                                                                                                                                                                           //" LIMIT 0,$limit"
     $sql = "select catalogs_rubrics.translit as rubric_translit,catalogs_rubrics.name as rubric, catalogs_products.*,catalogs_keys_products_filters.filter_id as prd from catalogs_keys_products_filters Left Join catalogs_products ON catalogs_products.id=catalogs_keys_products_filters.product_id Left Join catalogs_rubrics ON catalogs_rubrics.id=catalogs_products.rubric_id where catalogs_keys_products_filters.filter_id=? and catalogs_products.count_modifications>0 order by catalogs_keys_products_filters.product_id " . ((rand(1,1000)%2) ? "asc" : "desc") ; //. " LIMIT $round,$limit";
     $row = $this->db->getRow($sql ,array(trim($id)),DB_FETCHMODE_ASSOC);
     if(count($row)>0)$products[] = $row;
    }
  
  //  $products = $this->db->getAll("select catalogs_rubrics.translit as translit2,catalogs_products.*,catalogs_keys_products_filters.filter_id as prd from catalogs_keys_products_filters Left Join catalogs_products ON catalogs_products.id=catalogs_keys_products_filters.product_id Left Join catalogs_rubrics ON catalogs_rubrics.id=catalogs_products.rubric_id where catalogs_keys_products_filters.filter_id in(".implode(',',$arr_id).") and catalogs_products.count_modifications>0 Group By catalogs_keys_products_filters.filter_id order by catalogs_keys_products_filters.product_id " . ((rand(1,1000)%2) ? "asc" : "desc") ,array(),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('products_parent',$products);
   }
   
   function getProductSizeCine($id,&$products){
    $res = $this->db->getAll("select * from catalogs_products_cine where product_id=?",array($id),DB_FETCHMODE_ASSOC);
    $i = 0;
    foreach($res as $row){
     $products['size_' . $row['n']] = $row['size'];
     $products['cine_' . $row['n']] = $row['cine'];
     $products['metka_' . $row['n']] = $row['metka'];
     $products['id_cine_' . $row['n']] = $row['id'];
    $i++;
    }  //print_r($products);
   }
   /*
   function getProductCine($id,$curs = null){
    $sql = "select cine from catalogs_products where id=?";
    $row = $this->db->getOne($sql,array($id));
    return $row;
   }*/
   
   function getProductCine($id){
    $sql = "select IF( catalogs_products.is_cost2 <1, catalogs_products.cine, catalogs_products.cine2 ) AS cine from catalogs_modifications left join catalogs_products on catalogs_products.id=catalogs_modifications.product_id where catalogs_modifications.id=?";
    return $this->db->getOne($sql,array($id));
   }
   
   function viewProductCine($id){
    $row = $this->db->getAll("select * from catalogs_products_cine where product_id=? and cine>0 Order By n ASC",array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('product_cines',$row);
   }
   
   function viewCatalogsCurs(){
    $uah = $this->db->getOne("select curs from catalogs_curs where type=?",array('uah'));
    $this->tpl->assign('curs_uah',$uah);
    $uah_bank = $this->db->getOne("select curs from catalogs_curs where type=?",array('uah_bank'));
    $this->tpl->assign('curs_uah_bank',$uah_bank);
    $usd = $this->db->getOne("select curs from catalogs_curs where type=?",array('usd'));
    $this->tpl->assign('curs_usd',$usd);
   }
   
   function saveCurs($data){
   $table_name = "catalogs_curs";
   $DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;
   foreach($data as $key=>$value){
    $this->db->autoExecute($table_name,array('curs'=>$value),$DB_AUTOQUERY,"type='$key'");
   }
   }
   
   function viewVarCurs($curs){
    $this->tpl->assign('curs',$curs);
   }
   
   function viewBasketProducts($session_basket,$curs = null,$discount = 0){
    $products = array(); $i = 0; $cineAll = 0;
  //  $sql = "select catalogs_products.* from catalogs_products where catalogs_products.id=?";
  //  $sql = "select catalogs_modifications.id,IF(catalogs_products.is_cost2<1,catalogs_products.cine,catalogs_products.cine2) as cine,catalogs_products.name,catalogs_modifications.code,catalogs_modifications.size,catalogs_modifications.color,catalogs_modifications.pic,catalogs_modifications.pic_big from catalogs_modifications Left Join catalogs_products On catalogs_products.id=catalogs_modifications.product_id where catalogs_modifications.id=?";
    $sql = "select catalogs_modifications.id,catalogs_modifications.sale,IF(catalogs_modifications.cine>0,catalogs_modifications.cine,catalogs_products.cine) as cine,catalogs_products.name,catalogs_modifications.code,catalogs_modifications.size,catalogs_modifications.color,catalogs_modifications.pic,catalogs_modifications.pic_big from catalogs_modifications Left Join catalogs_products On catalogs_products.id=catalogs_modifications.product_id where catalogs_modifications.id=?";
  
    foreach($session_basket as $id){
     $products_count = 1;
     if(count($products)){
     foreach($products as $key=>$value){
      if($value['id']==$id){$products_count=$products[$key]['products_count']+$products_count;$products[$key]['products_count']=$products_count;$products[$key]['products_cine_all']=$products[$key]['cine']*$products_count;$cineAll = $cineAll+$products[$key]['cine'];}
     }}
     if($products_count==1){
      $row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
      $products[$i] = $row;
  	$products[$i]['cine'] = round($products[$i]['cine'] - ($products[$i]['cine']*$products[$i]['sale']/100));
  
      $products[$i]['products_count']=$products_count;
      $products[$i]['products_cine_all']=$products[$i]['cine']*$products_count;
      $cineAll = $cineAll+$products[$i]['products_cine_all'];
      $i++;
     }
  
    }
    /*print"<pre>";
    print_r($products);
    print"</pre>";*/
    $this->tpl->assign('cineAll',$cineAll);
    $this->tpl->assign('cineAll_discount',$cineAll - (($cineAll*$discount)/100));
    $this->tpl->assign('basket_products',$products);   //print_r($products);
   }
   
   
   function BasketUpload(&$session_basket,$proructs_count){
   
   // ========== ZLODEYEV ============
   
      foreach($proructs_count as $key=>$value){ 
    
          // ==== îáíîâëÿåì ====
7a508b62   andryeyev   + попытка исправи...
1607
      
1b81d16b   andryeyev   + откат до пред. ...
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
          // Âûáèðàåì èíôî î òîâàðå
          $sql4 = "SELECT * FROM catalogs_modifications as m
                  LEFT JOIN catalogs_products as p
                  ON p.id=m.product_id 
                  WHERE m.id='".$key."' ";
          $result4 = mysql_query($sql4) or die(mysql_error());
          if (mysql_affected_rows()!=0) {        
              $item=mysql_fetch_assoc($result4);
          }    
          
          $f=0;
          
          if (isset($_SESSION['shopcart'])) {
          
              foreach ($_SESSION['shopcart'] as $i => $cart) {
  
                  
                  if ($cart['code']!='' && $cart['code']==$key) { 
                      $f=1;
                      $_SESSION['shopcart'][$i]['count']=$value;
                      $_SESSION['shopcart'][$i]['total']=$value*$_SESSION['shopcart'][$i]['price'];
                      if ($_SESSION['shopcart'][$i]['total']==0) {
                          unset($_SESSION['shopcart'][$i]);
                      }
                      break;
                  }
              }
          } 
7a508b62   andryeyev   + попытка исправи...
1636
      }
637e6fff   andryeyev   + выгрузка пользо...
1637
   
1b81d16b   andryeyev   + откат до пред. ...
1638
    // ========== ZLODEYEV ============
637e6fff   andryeyev   + выгрузка пользо...
1639
   
637e6fff   andryeyev   + выгрузка пользо...
1640
   
1b81d16b   andryeyev   + откат до пред. ...
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
    $session_basket = array();
    foreach($proructs_count as $key=>$value){
     for($i=0;$i<$value;$i++){
      $session_basket[] = $key;
     }
    }
   }
   
   function sendOrder($data){
    $mail = new PHPMailer();
    $mail->CharSet = "windows-1251";
    $mail->From = (strlen($data['order_email'])>0) ? $data['order_email'] : "shopextremstyle2@gmail.com";
    $mail->FromName = $data['order_name'];
    $mail->AddAddress("shop@eltrade.com.ua","shop");
    $mail->Subject = "Îôîðìèòü çàêàç ñ ÝêñòðèìÑòèëü";
    $title = Mail::str_rus("Îôîðìèòü çàêàç");
    $msg = "";
    foreach($data['product_id'] as $key=>$value){
     $msg .= "ID: ".$value." ";
     $msg .= "Íàçâàíèå: <b>".$data['product_name'][$key]."</b> ";
     $msg .= "Öåíà çà îäèí: ".$data['product_cine'][$key]." ";
     $msg .= "Êîëè÷åñòâî: ".$data['product_count'][$key]." ";
     $msg .= "Çà âåñü òîâàð: ".$data['product_cine_all'][$key]." ";
     $msg .= "<br>";
    }
     $msg .= "Îáùàÿ ñóìà: ".$data['cineAll'];
     $msg .= "<br>";
   /*  $msg .= "Ñêèäêà: ".$data['discount']." %";
     $msg .= "<br>";    */
     $msg .= "Èòîãî ê îïëàòå: ".$data['cineAll_discount'];
     $msg .= "<br><br><br>";
     $msg .= "Êîíòàêòíûå äàííûå:";
     $msg .= "<br>";
     $msg .= "Èìÿ: ".$data['order_name'];
     $msg .= "<br>";
     $msg .= "Àäðåñ: ".$data['order_adress'];
     $msg .= "<br>";
     $msg .= "E-mail: ".$data['order_email'];
     $msg .= "<br>";
     $msg .= "Êîíòàêòíûé òåëåôîí: ".$data['order_phone'];
  //   $msg .= "<br>";
  //   $msg .= "Ìîáèëüíûé òåëåôîí: ".$data['order_phonemob'];
     $msg .= "<br>";
     $msg .= "Êîììåíòàðèé: ".$data['order_comment'];
  //   $order_email = (strlen($data['order_email'])>0) ? $data['order_email'] : "vapila@vapila.kiev.ua";
  //   Mail::send("borisenko.pavel@gmail.com",$data['order_email'],$title,Mail::str_rus($msg));
    $mail->MsgHTML($msg);
    if(!$mail->Send()){echo "There has been a mail error sending to <br>";return false;}
    $mail->ClearAddresses();
    $mail->ClearAttachments();
    
    $this->sendOrderXML($data);
   }
   
   
   function sendOrderXML($data){
  
  
  
  $data['order_phone'] = str_replace("-","",$data['order_phone']);
  $data['order_phone'] = str_replace(")","",$data['order_phone']);
  $data['order_phone'] = str_replace("(","",$data['order_phone']);
  $data['order_phone'] = str_replace(" ","",$data['order_phone']);
  $ffffffff = substr($data['order_phone'],0,3);
  if($ffffffff!="380")
  $data['order_phone'] = "+38".$data['order_phone'];
  
  
  
    $mail = new PHPMailer();
    $mail->CharSet = "windows-1251";
    $mail->From = (strlen($data['order_email'])>0) ? $data['order_email'] : "shopextremstyle2@gmail.com";
    $mail->FromName = $data['order_name'];
    $mail->AddAddress("extremshop@portal.myoffice.biz.ua","extremshop");
    $mail->Subject = "xml çàêàç ñ ÝêñòðèìÑòèëü";
    $order_email = (strlen($data['order_email'])>0) ? $data['order_email'] : "shopextremstyle2@gmail.com";
    $xml = '<?xml version="1.0" encoding="windows-1251" ?>';
    $xml .= '<Order orderNum="'.mktime().'">';
    foreach($data['product_id'] as $key=>$value){
     $xml .= "<OrderItem ID='".$data['product_code'][$key]."'>";
     $xml .= "<ProductName>".$data['product_name2'][$key]."</ProductName>";
     $xml .= "<Size>".$data['product_size'][$key]."</Size>";
     $xml .= "<Color>".$data['product_color'][$key]."</Color>";
     $xml .= "<Price>".$data['product_price'][$key]."</Price>";
     $xml .= "<Count>".$data['product_count'][$key]."</Count>";
     $xml .= "</OrderItem>";
    }
     $xml .= '<CustomerInfo>';
     $xml .= '<Name>'.$data['order_name'].'</Name>';
     $xml .= '<Mobile>'.$data['order_phone'].'</Mobile>';
     $xml .= '<Email>'.$data['order_email'].'</Email>';
     $xml .= '</CustomerInfo>';
     $xml .= '<DeliveryInfo>';
     $xml .= '<City>Óêðàèíà</City>';
     $xml .= '<Address>'.$data['order_adress'].'</Address>';
     $xml .= '<DeliveryType>Äîñòàâêà</DeliveryType>';
     $xml .= '</DeliveryInfo>';
     $xml .= '<Comment>'.$data['order_comment'].'</Comment>';
     $xml .= '</Order>';
    $tmpfname = tempnam("tmp", "FOO");
    $handle = fopen($tmpfname, "w");
     fwrite($handle, $xml);
     $mail->AddAttachment($tmpfname, 'order.xml');
    if(!$mail->Send()){echo "There has been a mail error sending to <br>";return false;}
     fclose($handle);
     unlink($tmpfname);
    $mail->ClearAddresses();
    $mail->ClearAttachments();
   // Mail::send("borisenko.pavel@gmail.com",$order_email,"Order",Mail::str_rus($xml));
     // Mail::send("andrey.belyy@gmail.com",$order_email,"Order",Mail::str_rus($xml));
   }
   
   function saveOrder($data,$userID = 0){
   
    $table_name = "catalogs_orders";
    
    if(isset($data['update_id']) && $data['update_id']>0){
        $DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;
        $id = $data['update_id']; 
        $where = sprintf("id='%d'",$id);
    } else {
      $DB_AUTOQUERY = DB_AUTOQUERY_INSERT;
      $id = $this->db->nextId('mySequence_'.$table_name);
      $where = null;
    }
  
  
  
  
      $fields_values = array(
          'id'=>$id,
          'user_id'=>$userID,
          'name' => $data['order_name'],
          'email' => $data['order_email'],
          'adress' => $data['order_adress'],
          //'phone' => phone_mobil($data['order_phone']),
          'phone' => $data['order_phone'],
          'comment' => $data['order_comment'],
          'delivery' => $data['delivery'],
          'total'=>$_SESSION['cart']['total'], 
          'status' => '1',
          'mktime'=>mktime()
      );
      $DB_AUTOQUERY;
   
      $this->db->autoExecute($table_name,$fields_values,$DB_AUTOQUERY,$where);
  
    if(isset($data['product_id'])){ $i=-1;
     foreach($data['product_id'] as $key=>$value){ $i++;
      $this->db->autoExecute("catalogs_orders_products",array(
      'order_id'=>$id,
      'product_id'=>$value,
      'product_cine_id'=>$_SESSION['cart'][$i]['price'],
      'count'=>$data['product_count'][$key])
      ,DB_AUTOQUERY_INSERT);
     }
    }
    
    
   }
   
   function deleteOrder($id){
    $this->db->query("delete from catalogs_orders where id=?",array($id));
    $this->db->query("delete from catalogs_orders_products where order_id=?",array($id));
   }
   
   function updateOrder($data){
    $table_name = "catalogs_orders";
    $fields_values = array('name' => $data['order_name'],'email' => $data['order_email'],'adress' => $data['order_adress'],'phone' => $data['order_phone'],'comment' => $data['order_comment'],'mktime'=>mktime());
    if(isset($data['order_active']))$fields_values['active'] = 1;
    else $fields_values['active'] = 0;
    $this->db->autoExecute($table_name,$fields_values,DB_AUTOQUERY_UPDATE,sprintf("id='%d'",$data['update_id']));
   }
   
   function updateOrderProducts($data){
     foreach($data['product_id'] as $key=>$value){
      if($data['product_count'][$value]<1){$this->db->query("delete from catalogs_orders_products where order_id='{$data['order_id']}' AND product_id='$value'");}
      else{$this->db->autoExecute("catalogs_orders_products",array('count'=>$data['product_count'][$value]),DB_AUTOQUERY_UPDATE,"order_id='{$data['order_id']}' AND product_id='$value'");}
     }
   }
   
   function viewOrder($id){
    $row = $this->db->getRow("select * from catalogs_orders where id=?",array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('order', $row);
   }
   
   function viewOrderProducts($order_id){
    $res = $this->db->getAll("select p.*,op.*,pr.name,pr.pic,pr.pic_big from catalogs_orders_products op Left join catalogs_products_cine p On p.id=op.product_id Left join catalogs_products pr On pr.id=p.product_id where order_id=?",array($order_id),DB_FETCHMODE_ASSOC);
     $sum = 0;
     foreach($res as $key=>$row){
      $sum = $sum + ($row['cine']*$row['count']);
      $res[$key]['cine_all'] = ($row['cine']*$row['count']);
     }
    $discount = $this->db->getOne('select catalogs_users.discount from catalogs_orders Left join catalogs_users on catalogs_users.id=user_id where catalogs_orders.id=?',array($order_id));
    $this->tpl->assign('cine', array('sum'=>$sum,'discount'=>$discount,'sum_discount'=>($sum - (($sum*$discount)/100)) ));
    $this->tpl->assign('basket_products', $res);
   }
   
   function viewOrders($params = array()){
    $search = array();
    if(isset($params['userID']) && $params['userID']>0)$search[] = sprintf("catalogs_orders.user_id='%d'",$params['userID']);
    $sql = "select catalogs_orders.*,catalogs_users.login as user_login,catalogs_users.discount from catalogs_orders Left join catalogs_users on catalogs_users.id=user_id WHERE 1=1 ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    $sql .= " order by mktime desc";
    $pagerOptions = Array(
      'mode' => 'Sliding',
      'delta' => 16,
      'perPage' => 50,
      'linkClass'=>"link_class",
      'curPageLinkClassName'=>"link_class_active",
      'spacesBeforeSeparator' => 1,
      'spacesAfterSeparator' => 1
    );
    $res = Pager_Wrapper_DB($this->db, $sql, $pagerOptions, false, DB_FETCHMODE_ASSOC, array());
    foreach($res['data'] as $key=>$value){
     $res['data'][$key]['sum'] = $this->getOrderProductsSum($value['id']);
     $res['data'][$key]['sum_discount'] = $res['data'][$key]['sum'] - (($res['data'][$key]['sum']*$value['discount'])/100);
    }
    $this->tpl->assign('ordersData', $res);
   }
   
   function getOrderProductsSum($order_id){
     $res = $this->db->getAll("select op.*,p.cine from catalogs_orders_products op Left join catalogs_products_cine p On p.id=op.product_id where op.order_id=?",array($order_id),DB_FETCHMODE_ASSOC);
    // print_r($res);
     $sum = 0;
     foreach($res as $row){
      $sum = $sum + ($row['cine']*$row['count']);
     }
     return $sum;
   }
   
   function SaveBrend($data,$upload){
    $table_name = "catalogs_brends";
    $DB_AUTOQUERY = ($data['update_id']>0) ? DB_AUTOQUERY_UPDATE : DB_AUTOQUERY_INSERT;
    $fields_values = array('rubric_id'=>$data['rubric_id'],'name'=>$data['brend'],'email'=>$data['email'],'text'=>$data['text_setka'],'youtube'=>$data['youtube'],'seo'=>$data['seo_text'],'sort'=>$data['sort'],'meta_title'=>$data['meta_title'],'meta_description'=>$data['meta_description'],'meta_keywords'=>$data['meta_keywords'],'active'=>$data['active'] ,'about'=>$data['about'],'link'=>$data['link'],'teh'=>$data['teh']);
  
    if(isset($data['delete_video']) && $data['delete_video']==1){
     $this->deleteBrendPic($data['update_id']);
     $fields_values['pic'] = null;
    }
    if($upload['pic']['tmp_name']){
     if($data['update_id']>0)$this->deleteBrendPic($data['update_id'],array('pic'));
     $name_pic = mktime() . "." . substr(strrchr($_FILES['pic']['name'],"."),1);
     $file_name = "{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/brends/$name_pic";
     move_uploaded_file($_FILES["pic"]["tmp_name"], $file_name);
     chmod($file_name,"0777");
     $fields_values['pic'] = $name_pic;
    }
  
    if(isset($data['delete_pic_s']) && $data['delete_pic_s']==1){
     $this->deleteBrendPic($data['update_id'],array('pic_s'));
     $fields_values['pic_s'] = null;
    }
    if($upload['pic_s']['tmp_name']){
     if($data['update_id']>0)$this->deleteBrendPic($data['update_id'],array('pic_s'));
     $name_pic = mktime() . "." . substr(strrchr($_FILES['pic_s']['name'],"."),1);
     $file_name = "{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/brends/s/$name_pic";
     move_uploaded_file($_FILES["pic_s"]["tmp_name"], $file_name);
     chmod($file_name,"0777");
     $fields_values['pic_s'] = $name_pic;
    }
  
  
    $this->db->autoExecute($table_name,$fields_values,$DB_AUTOQUERY,"id={$data['update_id']}");
   }
   
   function deleteBrendPic($id,$pics = array('pic','pic_s')){
    $sql = "select " . implode(',',$pics) . " from catalogs_brends where id=?";
    $row = $this->db->getRow($sql,array($id),DB_FETCHMODE_ASSOC);
    if(isset($row['pic'])){
     $file_name = "{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/brends/{$row['pic']}";
     @unlink($file_name);
    }
    if(isset($row['pic_s'])){
     $file_name = "{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/brends/s/{$row['pic_s']}";
     @unlink($file_name);
    }
   }
  
   function viewBrends($type = false){
    $sql = "select b.*,r.name as rubric from catalogs_brends b left join catalogs_rubrics r On r.id=b.rubric_id ";
    if($type!=false)$sql .= "where b.type='$type'";
    $sql .= " Order by b.name Asc";
    $row = $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('brends',$row);
   }
   
   function viewBrendsGroup($params = array()){
    $search = array();
    if(isset($params['filter']) && count($params['filter'])>0)$filter = $this->seralizeFilter($params['filter']);
  
    if(isset($params['filter_products']) && strlen($params['filter_products'])>0 && !isset($filter['f'])){
     $search[] = "catalogs_keys_products_filters.filter_id in (".$params['filter_products'].")";
    }
    
    if(isset($filter['y']) && $filter['y']>0){
     $search[] = "catalogs_keys_products_years.year_id in (".implode(",",$filter['y']).")";
    }
    
    if(isset($filter['f']) && count($filter['f'])>0){
     $search[] = "catalogs_keys_products_filters.filter_id in (".implode(",",$filter['f']).")";
    }
    
    if(isset($filter['g']) && count($filter['g'])>0){
     $search[] = "catalogs_keys_products_filters2.filter_id in (".implode(",",$filter['g']).")";
    }  
    
    if(isset($filter['s']) && count($filter['s'])>0){
     $search[] = "catalogs_modifications.size in (".implode(",",$filter['s']).")";
    }
    if(isset($filter['r']) && count($filter['r'])>0){
     $search[] = "m2.rost in (".implode(",",$filter['r']).")";
    }  
    if(isset($_GET['new']) && $_GET['new']>0)$search[] = sprintf("catalogs_products.active='%d'",$_GET['new']);
    if(isset($_GET['top']) && $_GET['top']>0)$search[] = sprintf("catalogs_products.top='%d'",$_GET['top']); 
  if(isset($_GET['akciiID']) && $_GET['akciiID']>0)$search[] = sprintf("catalogs_products.akcii_id>'%d'",0);    
    /*if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
  	$sex = array('male'=>1,'female'=>2,'child'=>3,'unisex'=>4);
  	$search[] = sprintf("catalogs_products.sex='%d'",$sex[$_GET['sex']]);
    }*/
  
     if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
     $sex = $this->seralizeSex($_GET['sex']);
     $search[] = "s.sex IN (" . implode(",",$sex) . ")";
    }   
  /* 
    $sql = "select * from catalogs_products ";
    $sql .= "LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id ";
    if(isset($params['filter_products']) && strlen($params['filter_products'])>0 && !isset($filter['f'])){
  	//$sql .= "LEFT JOIN catalogs_products ON catalogs_products.brend_id=catalogs_brends.id ";
  	$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
    }
    if(isset($filter['y']) && $filter['y']>0)$sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=catalogs_products.id ";
    if(isset($filter['f']) && count($filter['f'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";  
    if(isset($filter['g']) && count($filter['g'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters2 ON catalogs_keys_products_filters2.product_id=catalogs_products.id ";  
  
    if(isset($filter['s']) && count($filter['s'])>0){$sql .= "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=catalogs_products.id and catalogs_modifications.active=1 ";}
    if(isset($filter['r']) && count($filter['r'])>0){$sql .= "LEFT JOIN catalogs_modifications m2 ON m2.product_id=catalogs_products.id and m2.active=1 ";}
    if(isset($_GET['sex']) && strlen($_GET['sex'])>0){$sql .= "LEFT JOIN catalogs_keys_products_sex s ON s.product_id=catalogs_products.id ";}
    $sql .= "where catalogs_products.count_modifications>0 and catalogs_brends.id>0 ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    if(isset($params['rubID']) && !isset($params['filter_products'])){
     $rubric_id = $params['rubID'];//$this->getRubricParent($params['rubID']);
     $rubric_id = ($rubric_id==0)?$params['rubID']:$rubric_id;
     $sql .= "and catalogs_products.rubric_id='".$rubric_id."'";
    }
    $sql .= " Group by catalogs_brends.name Order by catalogs_brends.name Asc";
   // print $sql;
    $row = $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('brends',$row);
   }
  */
  
    // ==== catalogs_products ====
    
      $sql = array ();
      $sql[] = "select * from catalogs_products ";
      $sql[] = "LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id ";
  
      if (isset ($params['filter_products']) && strlen ($params['filter_products']) > 0 && ! isset ($filter['f']))
7a508b62   andryeyev   + попытка исправи...
2001
      {
1b81d16b   andryeyev   + откат до пред. ...
2002
2003
            //$sql .= "LEFT JOIN catalogs_products ON catalogs_products.brend_id=catalogs_brends.id ";
            $sql[] = "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
7a508b62   andryeyev   + попытка исправи...
2004
      }
1b81d16b   andryeyev   + откат до пред. ...
2005
2006
  
      if (isset ($filter['y']) && $filter['y'] > 0)
7a508b62   andryeyev   + попытка исправи...
2007
      {
1b81d16b   andryeyev   + откат до пред. ...
2008
          $sql[] = "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=catalogs_products.id ";
7a508b62   andryeyev   + попытка исправи...
2009
      }
1b81d16b   andryeyev   + откат до пред. ...
2010
2011
  
      if (isset ($filter['f']) && count ($filter['f']) > 0)
7a508b62   andryeyev   + попытка исправи...
2012
      {
1b81d16b   andryeyev   + откат до пред. ...
2013
          $sql[] = "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=catalogs_products.id ";
7a508b62   andryeyev   + попытка исправи...
2014
      }
1b81d16b   andryeyev   + откат до пред. ...
2015
2016
  
      if (isset ($filter['g']) && count ($filter['g']) > 0)
7a508b62   andryeyev   + попытка исправи...
2017
      {
1b81d16b   andryeyev   + откат до пред. ...
2018
          $sql[] = "LEFT JOIN catalogs_keys_products_filters2 ON catalogs_keys_products_filters2.product_id=catalogs_products.id ";
7a508b62   andryeyev   + попытка исправи...
2019
      }
1b81d16b   andryeyev   + откат до пред. ...
2020
2021
  
      if (isset ($filter['s']) && count ($filter['s']) > 0)
7a508b62   andryeyev   + попытка исправи...
2022
      {
1b81d16b   andryeyev   + откат до пред. ...
2023
          $sql[] = "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=catalogs_products.id and catalogs_modifications.active=1 ";
7a508b62   andryeyev   + попытка исправи...
2024
      }
1b81d16b   andryeyev   + откат до пред. ...
2025
2026
  
      if (isset ($filter['r']) && count ($filter['r']) > 0)
7a508b62   andryeyev   + попытка исправи...
2027
      {
1b81d16b   andryeyev   + откат до пред. ...
2028
          $sql[] = "LEFT JOIN catalogs_modifications m2 ON m2.product_id=catalogs_products.id and m2.active=1 ";
7a508b62   andryeyev   + попытка исправи...
2029
      }
1b81d16b   andryeyev   + откат до пред. ...
2030
2031
  
      if (isset ($_GET['sex']) && strlen ($_GET['sex']) > 0)
7a508b62   andryeyev   + попытка исправи...
2032
      {
1b81d16b   andryeyev   + откат до пред. ...
2033
          $sql[] = "LEFT JOIN catalogs_keys_products_sex s ON s.product_id=catalogs_products.id ";
7a508b62   andryeyev   + попытка исправи...
2034
      }
1b81d16b   andryeyev   + откат до пред. ...
2035
2036
2037
2038
  
      $sql[] = "where catalogs_products.count_modifications > 0 and catalogs_brends.id > 0 ";
  
      if (count ($search))
7a508b62   andryeyev   + попытка исправи...
2039
      {
1b81d16b   andryeyev   + откат до пред. ...
2040
          $sql[] = "AND " . implode(" AND ", $search) . " ";
7a508b62   andryeyev   + попытка исправи...
2041
      }
1b81d16b   andryeyev   + откат до пред. ...
2042
2043
  
      if (isset ($params['rubID']) && ! isset ($params['filter_products']))
7a508b62   andryeyev   + попытка исправи...
2044
      {
1b81d16b   andryeyev   + откат до пред. ...
2045
2046
2047
          $rubric_id = $params['rubID'];//$this->getRubricParent($params['rubID']);
          $rubric_id = ($rubric_id == 0) ? $params['rubID'] : $rubric_id;
          $sql[] = "and catalogs_products.rubric_id='".$rubric_id."'";
7a508b62   andryeyev   + попытка исправи...
2048
      }
1b81d16b   andryeyev   + откат до пред. ...
2049
2050
2051
2052
  
      $sql[] = " Group by catalogs_brends.name Order by catalogs_brends.name Asc";
  
      if (count ($sql) > 4)
7a508b62   andryeyev   + попытка исправи...
2053
      {
1b81d16b   andryeyev   + откат до пред. ...
2054
          $row = $this->db->getAll(implode (' ', $sql), array(), DB_FETCHMODE_ASSOC);
7a508b62   andryeyev   + попытка исправи...
2055
      }
1b81d16b   andryeyev   + откат до пред. ...
2056
      else 
7a508b62   andryeyev   + попытка исправи...
2057
      {
1b81d16b   andryeyev   + откат до пред. ...
2058
2059
          // çíà÷èò ñòðàíèöà
          $row = array ();
7a508b62   andryeyev   + попытка исправи...
2060
      }
1b81d16b   andryeyev   + откат до пред. ...
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
          $this->tpl->assign('brends', $row);
  
  }
    
  // =====
    
   function viewBrendsActive(){
    $sql = "select * from catalogs_brends where active=1 ";
    $sql .= " Order by name Asc";
    $row = $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('brends_a',$row);
   }
   
   function SaveCity($data){
    $table_name = "catalogs_cities";
    $DB_AUTOQUERY = (isset($data['update_id']) && $data['update_id']>0) ? DB_AUTOQUERY_UPDATE : DB_AUTOQUERY_INSERT;
    $fields_values = array('name'=>$data['name']);
    $this->db->autoExecute($table_name,$fields_values,$DB_AUTOQUERY,"id={$data['update_id']}");
    return mysql_insert_id();
   }
   
   function is_city($name){
    return $this->db->getOne('select id from catalogs_cities where name=?',array($name));
   }
   
   function viewCityMod($code){
    $sql = "select c.name from catalogs_keys_products_cities k left join catalogs_cities c on c.id=k.city_id where k.mod_code=? and  k.count>0";
    $row = $this->db->getCol($sql,"name",array($code));
    if(!count($row))return "";
    if(count($row) == 1 && trim($row[0])==''){  return ""; }
  
    foreach($row as $key=>$value){if($value=="Èíòåðíåò ìàãàçèí" or $value=="Êèåâ(Îëèìïèéñêèé)" or $value=="Ëüâîâ" or trim($value)=="")unset($row[$key]);}
  $row2 = array();
  foreach($row as $key=>$value) if(trim($value)!='') $row2[] = $value;
  $row = $row2;
  
    $l = array();
    for($i=0;$i<count($row);$i++){
            $key = 0;
           for($i2=0;$i2<count($l);$i2++) 
            if($l[$i2]==$row[$i]) $key=1;
    if($key==0) $l[]=$row[$i];
    }
    if(trim($_REQUEST['test2'])!='1') 
           { return implode(', ',$l);}
    return implode(', ',$row);
   }
   
   function viewCitiesProductID($id){
    $sql = "select city_id from catalogs_keys_products_cities where product_id=?";
    $row = $this->db->getCol($sql,"city_id",array($id));
    $this->tpl->assign('product_cities',$row);
   }
   
   function viewCities($type = false){
    $sql = "select * from catalogs_cities ";
    $sql .= " Order by name Asc";
    $row = $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('cities',$row);
   }
   
   function viewCitiesProduct($id){
    $sql = "select catalogs_cities.* from catalogs_keys_products_cities left join catalogs_cities on catalogs_cities.id=catalogs_keys_products_cities.city_id Where catalogs_keys_products_cities.product_id=?";
    $row = $this->db->getAll($sql,array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('cities',$row);
   }
   
   function viewCity($id){
    $row = $this->db->getRow("select * from catalogs_cities where id=?",array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('city', $row);
   }
   
   function deleteCity($id){
    $this->db->query("delete from catalogs_cities where id=?",array($id));
    $this->delete_keysCitiesProduct($id);
   }
   
   function delete_keysCitiesProduct($id){
    $sql = "delete from catalogs_keys_products_cities where city_id=?";
    $this->db->query($sql,array($id));
   }
   
   function SaveYears($data){
    $table_name = "catalogs_years";
    foreach($data['filter'] as $key=>$name){
     if(strlen($name)>0){
       if(isset($data['update_id'][$key]) && $data['update_id'][$key]>0){$DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;$where = "id='{$data['update_id'][$key]}'";}
       else{$DB_AUTOQUERY = DB_AUTOQUERY_INSERT;$where = "";}
      $this->db->autoExecute($table_name,array('name'=>$name,'rubric_id'=>$data['rubric_id']),$DB_AUTOQUERY,$where);
     }
    }
   }
   
   function viewYearsProduct($id){
    $sql = "select y.* from catalogs_keys_products_years k LEFT JOIN catalogs_years y ON y.id=k.year_id where k.product_id=?";
    $row = $this->db->getAll($sql,array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('product_years',$row);
   } 
   
   function viewYearsProductID($id){
    $sql = "select year_id from catalogs_keys_products_years where product_id=?";
    $row = $this->db->getCol($sql,"year_id",array($id));
    $this->tpl->assign('product_years',$row);
   }
  
  function viewYears ($rubric_id, $filters = null)
  {
      $search = array ();
  
      if (isset ($_GET['filter']) && count ($_GET['filter']) > 0)
          $filter = $this->seralizeFilter ($_GET['filter']);
  
      if (isset ($filter['f']) && count ($filter['f']) > 0)
7a508b62   andryeyev   + попытка исправи...
2174
      {
1b81d16b   andryeyev   + откат до пред. ...
2175
          $search[] = "catalogs_keys_products_filters.filter_id in (" . implode (",", $filter['f']) . ")";
7a508b62   andryeyev   + попытка исправи...
2176
      }
1b81d16b   andryeyev   + откат до пред. ...
2177
2178
  
      if (isset ($filter['s']) && count ($filter['s']) > 0)
7a508b62   andryeyev   + попытка исправи...
2179
      {
1b81d16b   andryeyev   + откат до пред. ...
2180
          $search[] = "catalogs_modifications.size in (" . implode (",", $filter['s']) . ")";
7a508b62   andryeyev   + попытка исправи...
2181
      }
1b81d16b   andryeyev   + откат до пред. ...
2182
2183
  
      if (isset ($filter['r']) && count ($filter['r']) > 0)
7a508b62   andryeyev   + попытка исправи...
2184
      {
1b81d16b   andryeyev   + откат до пред. ...
2185
          $search[] = "m2.rost in (" . implode (",", $filter['r']) . ")";
7a508b62   andryeyev   + попытка исправи...
2186
2187
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2188
2189
2190
2191
2192
2193
2194
2195
      if (isset ($_GET['new']) && $_GET['new'] > 0)
          $search[] = sprintf ("p.active='%d'", $_GET['new']);
  
      if (isset ($_GET['top']) && $_GET['top'] > 0)
          $search[] = sprintf ("p.top='%d'", $_GET['top']);
  
      if (isset ($_GET['akciiID']) && $_GET['akciiID'] > 0)
          $search[] = sprintf ("p.akcii_id>'%d'", 0);
7a508b62   andryeyev   + попытка исправи...
2196
      
1b81d16b   andryeyev   + откат до пред. ...
2197
2198
      if (isset ($_GET['brend']) && strlen ($_GET['brend']) > 0)
          $search[] = "p.brend_id in(" . implode (",", $this->getBrendIDs_name ($_GET['brend'])) . ")";
7a508b62   andryeyev   + попытка исправи...
2199
      
1b81d16b   andryeyev   + откат до пред. ...
2200
2201
2202
2203
2204
2205
2206
      /**
       * if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
       * $sex = array('male'=>1,'female'=>2,'child'=>3,'unisex'=>4);
       * $search[] = sprintf("p.sex='%d'",$sex[$_GET['sex']]);
       * }
       */
      if (isset ($_GET['sex']) && strlen ($_GET['sex']) > 0)
7a508b62   andryeyev   + попытка исправи...
2207
      {
1b81d16b   andryeyev   + откат до пред. ...
2208
2209
          $sex = $this->seralizeSex ($_GET['sex']);
          $search[] = "s.sex IN (" . implode (",", $sex) . ")";
7a508b62   andryeyev   + попытка исправи...
2210
2211
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2212
      if (strlen ($filters) > 0 && ! isset ($filter['f']))
57a8be43   andryeyev   Исправил cron, им...
2213
      {
1b81d16b   andryeyev   + откат до пред. ...
2214
          $search[] = "catalogs_keys_products_filters.filter_id in (" . $filters . ")";
57a8be43   andryeyev   Исправил cron, им...
2215
2216
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2217
      $sql = "select y.* from catalogs_products p, catalogs_keys_products_years k LEFT JOIN catalogs_years y ON y.id=k.year_id ";
7a508b62   andryeyev   + попытка исправи...
2218
      
1b81d16b   andryeyev   + откат до пред. ...
2219
2220
2221
2222
      if (isset ($filter['f']) && count ($filter['f']) > 0)
          $sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=k.product_id ";
      
      if (isset ($filter['s']) && count ($filter['s']) > 0)
7a508b62   andryeyev   + попытка исправи...
2223
      {
1b81d16b   andryeyev   + откат до пред. ...
2224
          $sql .= "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=k.product_id and catalogs_modifications.active=1 ";
57a8be43   andryeyev   Исправил cron, им...
2225
      }
7a508b62   andryeyev   + попытка исправи...
2226
      
1b81d16b   andryeyev   + откат до пред. ...
2227
      if (isset ($filter['r']) && count ($filter['r']) > 0)
7a508b62   andryeyev   + попытка исправи...
2228
      {
1b81d16b   andryeyev   + откат до пред. ...
2229
          $sql .= "LEFT JOIN catalogs_modifications m2 ON m2.product_id=k.product_id and m2.active=1 ";
7a508b62   andryeyev   + попытка исправи...
2230
2231
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2232
      if (strlen ($filters) > 0 && ! isset ($filter['f']))
7a508b62   andryeyev   + попытка исправи...
2233
      {
1b81d16b   andryeyev   + откат до пред. ...
2234
          $sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=k.product_id ";
7a508b62   andryeyev   + попытка исправи...
2235
2236
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2237
      if (isset ($_GET['sex']) && strlen ($_GET['sex']) > 0)
7a508b62   andryeyev   + попытка исправи...
2238
      {
1b81d16b   andryeyev   + откат до пред. ...
2239
          $sql .= "LEFT JOIN catalogs_keys_products_sex s ON s.product_id=k.product_id ";
7a508b62   andryeyev   + попытка исправи...
2240
2241
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2242
2243
2244
2245
      $sql .= "where p.id=k.product_id and p.count_modifications>0 ";
  
      if ($filters == null)
          $sql .= " and p.rubric_id='" . $rubric_id . "' ";
7a508b62   andryeyev   + попытка исправи...
2246
      
1b81d16b   andryeyev   + откат до пред. ...
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
      if (count ($search))
          $sql .= "AND " . implode (" AND ", $search) . " ";
      
      $sql .= " GROUP BY k.year_id order by y.name desc";
      
      // print $sql;
      $row = $this->db->getAll ($sql, array (), DB_FETCHMODE_ASSOC);
      $this->tpl->assign ('years', $row);
  }
   
   function viewYearsAll($rubric_id){
    $sql = "SELECT * FROM  catalogs_years WHERE rubric_id=? ORDER BY id asc ";
    $row = $this->db->getAll($sql,array($rubric_id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('years', $row); 
   }
   
   function viewSexProduct($id){
    $sql = "select * from catalogs_keys_products_sex where product_id=?";
    $row = $this->db->getAll($sql,array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('product_sex',$row);
   } 
   
   function viewSexProductID($id){
    $sql = "select sex from catalogs_keys_products_sex where product_id=?";
    $row = $this->db->getCol($sql,"sex",array($id));
    $this->tpl->assign('product_sex',$row);
   }
   
  function viewSex ($rubric_id, $filters = null, $lang = 'rus')
  {
      $search = array ();
      
      if (isset ($_GET['filter']) && count ($_GET['filter']) > 0)
          $filter = $this->seralizeFilter ($_GET['filter']);
7a508b62   andryeyev   + попытка исправи...
2281
      
1b81d16b   andryeyev   + откат до пред. ...
2282
      if (isset ($filter['f']) && count ($filter['f']) > 0)
7a508b62   andryeyev   + попытка исправи...
2283
      {
1b81d16b   andryeyev   + откат до пред. ...
2284
          $search[] = "catalogs_keys_products_filters.filter_id in (" . implode (",", $filter['f']) . ")";
7a508b62   andryeyev   + попытка исправи...
2285
      }
1b81d16b   andryeyev   + откат до пред. ...
2286
      if (isset ($filter['y']) && $filter['y'] > 0)
7a508b62   andryeyev   + попытка исправи...
2287
      {
1b81d16b   andryeyev   + откат до пред. ...
2288
          $search[] = "catalogs_keys_products_years.year_id in (" . implode (",", $filter['y']) . ")";
7a508b62   andryeyev   + попытка исправи...
2289
      }
1b81d16b   andryeyev   + откат до пред. ...
2290
      if (isset ($filter['s']) && count ($filter['s']) > 0)
7a508b62   andryeyev   + попытка исправи...
2291
      {
1b81d16b   andryeyev   + откат до пред. ...
2292
          $search[] = "catalogs_modifications.size in (" . implode (",", $filter['s']) . ")";
7a508b62   andryeyev   + попытка исправи...
2293
      }
1b81d16b   andryeyev   + откат до пред. ...
2294
      if (isset ($filter['r']) && count ($filter['r']) > 0)
7a508b62   andryeyev   + попытка исправи...
2295
      {
1b81d16b   andryeyev   + откат до пред. ...
2296
          $search[] = "m2.rost in (" . implode (",", $filter['r']) . ")";
7a508b62   andryeyev   + попытка исправи...
2297
      }
1b81d16b   andryeyev   + откат до пред. ...
2298
2299
2300
2301
2302
2303
      if (isset ($_GET['new']) && $_GET['new'] > 0)
          $search[] = sprintf ("p.active='%d'", $_GET['new']);
      if (isset ($_GET['top']) && $_GET['top'] > 0)
          $search[] = sprintf ("p.top='%d'", $_GET['top']);
      if (isset ($_GET['akciiID']) && $_GET['akciiID'] > 0)
          $search[] = sprintf ("p.akcii_id>'%d'", 0);
7a508b62   andryeyev   + попытка исправи...
2304
      
1b81d16b   andryeyev   + откат до пред. ...
2305
      if (strlen ($filters) > 0 && ! isset ($filter['f']))
7a508b62   andryeyev   + попытка исправи...
2306
      {
1b81d16b   andryeyev   + откат до пред. ...
2307
          $search[] = "catalogs_keys_products_filters.filter_id in (" . $filters . ")";
7a508b62   andryeyev   + попытка исправи...
2308
2309
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2310
2311
      if (isset ($_GET['brend']) && strlen ($_GET['brend']) > 0)
          $search[] = "p.brend_id in(" . implode (",", $this->getBrendIDs_name ($_GET['brend'])) . ")";
7a508b62   andryeyev   + попытка исправи...
2312
      
1b81d16b   andryeyev   + откат до пред. ...
2313
      $sql = "select s.sex from catalogs_products p LEFT JOIN catalogs_keys_products_sex s ON s.product_id=p.id ";
7a508b62   andryeyev   + попытка исправи...
2314
      
1b81d16b   andryeyev   + откат до пред. ...
2315
2316
2317
2318
2319
      if (isset ($filter['f']) && count ($filter['f']) > 0)
          $sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=p.id ";
      if (isset ($filter['y']) && $filter['y'] > 0)
          $sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=p.id ";
      if (isset ($filter['s']) && count ($filter['s']) > 0)
7a508b62   andryeyev   + попытка исправи...
2320
      {
1b81d16b   andryeyev   + откат до пред. ...
2321
          $sql .= "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=p.id and catalogs_modifications.active=1 ";
7a508b62   andryeyev   + попытка исправи...
2322
      }
1b81d16b   andryeyev   + откат до пред. ...
2323
      if (isset ($filter['r']) && count ($filter['r']) > 0)
7a508b62   andryeyev   + попытка исправи...
2324
      {
1b81d16b   andryeyev   + откат до пред. ...
2325
          $sql .= "LEFT JOIN catalogs_modifications m2 ON m2.product_id=p.id and m2.active=1 ";
7a508b62   andryeyev   + попытка исправи...
2326
      }
1b81d16b   andryeyev   + откат до пред. ...
2327
      if (strlen ($filters) > 0 && ! isset ($filter['f']))
7a508b62   andryeyev   + попытка исправи...
2328
      {
1b81d16b   andryeyev   + откат до пред. ...
2329
          $sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=p.id ";
7a508b62   andryeyev   + попытка исправи...
2330
2331
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2332
2333
2334
2335
      $sql .= "where p.count_modifications>0 
              and s.sex < 4  
              and p.sex > 0 
          ";
7a508b62   andryeyev   + попытка исправи...
2336
      
1b81d16b   andryeyev   + откат до пред. ...
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
      if ($filters == null)
          $sql .= " and p.rubric_id='" . $rubric_id . "' ";
      if (count ($search))
          $sql .= "AND " . implode (" AND ", $search) . " ";
      
      $sql .= " GROUP BY s.sex";
      // print $sql;
      $res = $this->db->getAll ($sql, array (), DB_FETCHMODE_ASSOC);
      // print_r($res);
      $sex = array ();
      if ($lang == 'ukr')
          $arr = array (
              1 => array (
                  'name' => "×îëîâ³÷èé",
                  'link' => 'male' 
              ),
              2 => array (
                  'name' => "Ƴíî÷èé",
                  'link' => 'female' 
              ),
              3 => array (
                  'name' => "Äèòÿ÷èé",
                  'link' => 'child' 
              ) 
          )
          // 4=>array('name'=>"Óí³ñåêñ",'link'=>'unisex')
          ;
      else
          $arr = array (
              1 => array (
                  'name' => "Ìóæñêîé",
                  'link' => 'male' 
              ),
              2 => array (
                  'name' => "Æåíñêèé",
                  'link' => 'female' 
              ),
              3 => array (
                  'name' => "Äåòñêèé",
                  'link' => 'child' 
              ) 
          )
          // 4=>array('name'=>"Óíèñåêñ",'link'=>'unisex')
          ;
          
      foreach ($res as $row)
7a508b62   andryeyev   + попытка исправи...
2383
      {
1b81d16b   andryeyev   + откат до пред. ...
2384
          $sex[] = $arr[$row['sex']];
7a508b62   andryeyev   + попытка исправи...
2385
2386
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2387
2388
      if (isset ($_GET['s']))
          print_r ($sex);
7a508b62   andryeyev   + попытка исправи...
2389
      
1b81d16b   andryeyev   + откат до пред. ...
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
      $this->tpl->assign ('sex', $sex);
  } 
   
   function getYearsName_IDs($ids){
    //$sql = "select name from catalogs_years where id in (".implode(",",$ids).") order by name";
    //$row = $this->db->getCol($sql,"name",array());
    //return implode(", ",$row);
    
    $name = array();
    foreach($ids as $id){
     $name[] = $this->db->getOne("select name from catalogs_years where id=$id",array());
    }
    return implode(", ",$name);
   }
   
   function deleteYearsRubric_id($id){
    $this->db->query("delete from catalogs_years where rubric_id=?",array($id));
    $this->delete_keysYearsRubric($id);
   }
   
   function delete_keysYearsRubric($id){
    $sql = "delete from catalogs_keys_products_years where rubric_id=?";
    $this->db->query($sql,array($id));
   }
   
   function deleteYear($id){
    $this->db->query("delete from catalogs_years where id=?",array($id));
    $this->delete_keysYearsProduct($id);
   }
   
   function delete_keysYearsProduct($id){
    $sql = "delete from catalogs_keys_products_years where year_id=?";
    $this->db->query($sql,array($id));
   }
   
   function SaveFilters($data){
    $table_name = "catalogs_filters";
    foreach($data['filter'] as $key=>$name){
     if(strlen($name)>0){
       if(isset($data['update_id'][$key]) && $data['update_id'][$key]>0){$DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;$where = "id='{$data['update_id'][$key]}'";}
       else{$DB_AUTOQUERY = DB_AUTOQUERY_INSERT;$where = "";}
       $active = (isset($data['active'][$key])) ? 1 : 0;
      $fields = array('name'=>$name,"name_ukr"=>$data['filter_ukr'][$key],'prefix'=>encodestring($name),'rubric_id'=>$data['rubric_id'],"ph1"=>$data['ph1'][$key],"ph1_ukr"=>$data['ph1_ukr'][$key],"sort"=>$data['sort'][$key],"active"=>$active);
  	if(isset($data['cat'][$key])){$fields['cat'] = $data['cat'][$key];}
  	$this->db->autoExecute($table_name,$fields,$DB_AUTOQUERY,$where);
     }
    }
   }
   
   function viewFilterProduct($id){
    $sql = "select f.* from catalogs_keys_products_filters k LEFT JOIN catalogs_filters f ON f.id=k.filter_id where f.active='0' and k.product_id=?";
    $row = $this->db->getAll($sql,array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('product_filters',$row);
    return $row;
   }  
  
   function viewFilterProductID($id){
    $sql = "select filter_id from catalogs_keys_products_filters where product_id=?";
    $row = $this->db->getCol($sql,"filter_id",array($id));
    $this->tpl->assign('product_filters',$row);
   }
   
   function deleteFilter($id){
    $this->db->query("delete from catalogs_filters where id=?",array($id));
    $this->delete_keysFilterProduct($id);
   }
   
   function deleteFiltersRubric_id($rubric_id){
    $this->db->query("delete from catalogs_filters where rubric_id=?",array($rubric_id));
    $this->delete_keysFilterRubric($rubric_id);
   }
   
   function delete_keysFilterProduct($id){
    $sql = "delete from catalogs_keys_products_filters where filter_id=?";
    $this->db->query($sql,array($id));
   }
   
   function delete_keysFilterRubric($id){
    $sql = "delete from catalogs_keys_products_filters where rubric_id=?";
    $this->db->query($sql,array($id));
   }
   
   function viewFilters($rubric_id,$all = false,$lang = 'ru'){
    $search = array();
    if(isset($_GET['filter']) && count($_GET['filter'])>0)$filter = $this->seralizeFilter($_GET['filter']);
    if(isset($filter['g']) && count($filter['g'])>0){
     $search[] = "catalogs_keys_products_filters2.filter_id in (".implode(",",$filter['g']).")";
    }  
    if(isset($filter['y']) && $filter['y']>0){
     $search[] = "catalogs_keys_products_years.year_id in (".implode(",",$filter['y']).")";
    }
  
    if(isset($filter['s']) && count($filter['s'])>0){
     $search[] = "catalogs_modifications.size in (".implode(",",$filter['s']).")";
    }
  
    if(isset($filter['r']) && count($filter['r'])>0){
     $search[] = "m2.rost in (".implode(",",$filter['r']).")";
    }
  
    if(isset($_GET['new']) && $_GET['new']>0)$search[] = sprintf("p.active='%d'",$_GET['new']);
    if(isset($_GET['top']) && $_GET['top']>0)$search[] = sprintf("p.top='%d'",$_GET['top']); 
  if(isset($_GET['akciiID']) && $_GET['akciiID']>0)$search[] = sprintf("p.akcii_id>'%d'",0);  
    
    if(isset($_GET['brend']) && strlen($_GET['brend'])>0)$search[] = "p.brend_id in(".implode(",",$this->getBrendIDs_name($_GET['brend'])).")";
   /* if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
  	$sex = array('male'=>1,'female'=>2,'child'=>3,'unisex'=>4);
  	$search[] = sprintf("p.sex='%d'",$sex[$_GET['sex']]);
    } */
    if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
     $sex = $this->seralizeSex($_GET['sex']);
     $search[] = "s.sex IN (" . implode(",",$sex) . ")";
    }  
    $sql = "select f.*,f.name".($lang=='ukr'?'_ukr':'')." as name,f.h1".($lang=='ukr'?'_ukr':'')." as h1 from catalogs_products p, catalogs_keys_products_filters k LEFT JOIN catalogs_filters f ON f.id=k.filter_id ";
    if(isset($filter['g']) && count($filter['g'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters2 ON catalogs_keys_products_filters2.product_id=k.product_id "; 
    if(isset($filter['y']) && $filter['y']>0)$sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=k.product_id "; 
    if(isset($filter['s']) && count($filter['s'])>0){$sql .= "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=k.product_id and catalogs_modifications.active=1 ";}
    if(isset($filter['r']) && count($filter['r'])>0){$sql .= "LEFT JOIN catalogs_modifications m2 ON m2.product_id=k.product_id and m2.active=1 ";}
    if(isset($_GET['sex']) && strlen($_GET['sex'])>0){$sql .= "LEFT JOIN catalogs_keys_products_sex s ON s.product_id=k.product_id ";}
    $sql .= "where p.id=k.product_id and p.count_modifications>0 ";
    //$sql = "select * from catalogs_filters where 1=1 ";
    if($all == false)$sql .= "and f.active='0' ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    $sql .= "and f.rubric_id=? GROUP BY k.filter_id order by f.sort asc";
   // print $sql;
    $row = $this->db->getAll($sql,array($rubric_id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('filters', $row);
   }
   
   function viewFiltersAll($rubric_id){
    $row = $this->db->getAll("select * from catalogs_filters where rubric_id=? order by sort",array($rubric_id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('filters', $row);
   } 
   
   function getFiltersName_IDs($ids,$lang){
    //$sql = "select name from catalogs_filters where id in (".implode(",",$ids).")";
    //$row = $this->db->getCol($sql,"name",array());
    $name = array();
    foreach($ids as $id){
     $row = $this->db->getRow("select h1".($lang=='ukr'?'_ukr':'')." as h1 from catalogs_filters where id=$id",array(),DB_FETCHMODE_ASSOC);  
     $name[] = ((strlen($row['h1'])>0) ? $row['h1'] : $row['name']);
    }
  
    return implode(", ",$name);
   }
   
   function getFilters2Name_IDs($ids){ 
    //$sql = "select name from catalogs_filters where id in (".implode(",",$ids).")";
    //$row = $this->db->getCol($sql,"name",array());
    $name = array();
    foreach($ids as $id){
     $row = $this->db->getRow("select h1,name from catalogs_filters2 where id=$id",array(),DB_FETCHMODE_ASSOC);  
     $name[] = ((strlen($row['h1'])>0) ? $row['h1'] : $row['name']);
    }
  //print_r($name);
    return implode(", ",$name);
   } 
   
   function SaveFilters2($data){
    $table_name = "catalogs_filters2";
    foreach($data['filter2'] as $key=>$name){
     if(strlen($name)>0){ 
       if(isset($data['update_id'][$key]) && $data['update_id'][$key]>0){$DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;$where = "id='{$data['update_id'][$key]}'";}
       else{$DB_AUTOQUERY = DB_AUTOQUERY_INSERT;$where = "";}
       $active = (isset($data['active'][$key])) ? 1 : 0;
      $this->db->autoExecute($table_name,array('name'=>$name,"name_ukr"=>$data['filter_ukr'][$key],'prefix'=>encodestring($name),'rubric_id'=>$data['rubric_id'],"sort"=>$data['sort'][$key],"active"=>$active),$DB_AUTOQUERY,$where);
     }
    }
   }
  
   function viewFilterProductID2($id){
    $sql = "select filter_id from catalogs_keys_products_filters2 where product_id=?";
    $row = $this->db->getCol($sql,"filter_id",array($id));
    $this->tpl->assign('product_filters2',$row);
   } 
   
   function deleteFilter2($id){
    $this->db->query("delete from catalogs_filters2 where id=?",array($id));
    $this->delete_keysFilterProduct2($id);
   }
   
   function delete_keysFilterProduct2($id){
    $sql = "delete from catalogs_keys_products_filters2 where filter_id=?";
    $this->db->query($sql,array($id));
   } 
  
   function deleteFiltersRubric_id2($rubric_id){
    $this->db->query("delete from catalogs_filters2 where rubric_id=?",array($rubric_id));
    $this->delete_keysFilterRubric2($rubric_id);
   } 
   
   function delete_keysFilterRubric2($id){
    $sql = "delete from catalogs_keys_products_filters2 where rubric_id=?";
    $this->db->query($sql,array($id));
   }
  
   function viewFilters2($rubric_id,$lang='ru'){
    $search = array();
    if(isset($_GET['filter']) && count($_GET['filter'])>0)$filter = $this->seralizeFilter($_GET['filter']);
    if(isset($filter['f']) && count($filter['f'])>0){
     $search[] = "catalogs_keys_products_filters.filter_id in (".implode(",",$filter['f']).")";
    }  
    if(isset($filter['y']) && $filter['y']>0){
     $search[] = "catalogs_keys_products_years.year_id in (".implode(",",$filter['y']).")";
    }
  
    if(isset($filter['s']) && count($filter['s'])>0){
     $search[] = "catalogs_modifications.size in (".implode(",",$filter['s']).")";
    }
  
    if(isset($filter['r']) && count($filter['r'])>0){
     $search[] = "m2.rost in (".implode(",",$filter['r']).")";
    } 
  
    if(isset($_GET['new']) && $_GET['new']>0)$search[] = sprintf("p.active='%d'",$_GET['new']);
    if(isset($_GET['top']) && $_GET['top']>0)$search[] = sprintf("p.top='%d'",$_GET['top']); 
  if(isset($_GET['akciiID']) && $_GET['akciiID']>0)$search[] = sprintf("p.akcii_id>'%d'",0);    
    
    if(isset($_GET['brend']) && strlen($_GET['brend'])>0)$search[] = "p.brend_id in(".implode(",",$this->getBrendIDs_name($_GET['brend'])).")";
   /* if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
  	$sex = array('male'=>1,'female'=>2,'child'=>3,'unisex'=>4);
  	$search[] = sprintf("p.sex='%d'",$sex[$_GET['sex']]);
    } */
    if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
     $sex = $this->seralizeSex($_GET['sex']);
     $search[] = "s.sex IN (" . implode(",",$sex) . ")";
    }  
    $sql = "select f.*,f.name".($lang=='ukr'?'_ukr':'')." as name from catalogs_products p, catalogs_keys_products_filters2 k LEFT JOIN catalogs_filters2 f ON f.id=k.filter_id ";
    if(isset($filter['f']) && count($filter['f'])>0)$sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=k.product_id ";  
    if(isset($filter['y']) && $filter['y']>0)$sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=k.product_id "; 
    if(isset($filter['s']) && count($filter['s'])>0){$sql .= "LEFT JOIN catalogs_modifications ON catalogs_modifications.product_id=k.product_id and catalogs_modifications.active=1 ";}
    if(isset($filter['r']) && count($filter['r'])>0){$sql .= "LEFT JOIN catalogs_modifications m2 ON m2.product_id=k.product_id and m2.active=1 ";}
    if(isset($_GET['sex']) && strlen($_GET['sex'])>0){$sql .= "LEFT JOIN catalogs_keys_products_sex s ON s.product_id=k.product_id ";}
    $sql .= "where p.id=k.product_id and p.count_modifications>0 ";
    //$sql = "select * from catalogs_filters where 1=1 ";
    if($all == false)$sql .= "and f.active='0' ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    $sql .= "and f.rubric_id=? GROUP BY k.filter_id order by f.sort asc";
    //print $sql;
    $row = $this->db->getAll($sql,array($rubric_id),DB_FETCHMODE_ASSOC);
   	 
   // $row = $this->db->getAll("select *,f.name".($lang=='ukr'?'_ukr':'')." as name from catalogs_filters2 f where f.active='0' and f.rubric_id=? order by sort",array($rubric_id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('filters2', $row);
   }
   
   function getFiltersMod_IDs($ids){
    $name = array();
    foreach($ids as $id){
     $name[] = $this->db->getOne("select size from catalogs_modifications where size=$id",array());
    }
    return implode(", ",$name);
   } 
  
   function viewFiltersMod ($rubric_id)
  {
      $search = array ();
7a508b62   andryeyev   + попытка исправи...
2646
      
1b81d16b   andryeyev   + откат до пред. ...
2647
2648
      if (isset ($_GET['filter']) && count ($_GET['filter']) > 0)
          $filter = $this->seralizeFilter ($_GET['filter']);
7a508b62   andryeyev   + попытка исправи...
2649
      
1b81d16b   andryeyev   + откат до пред. ...
2650
      if (isset ($filter['f']) && count ($filter['f']) > 0)
7a508b62   andryeyev   + попытка исправи...
2651
      {
1b81d16b   andryeyev   + откат до пред. ...
2652
          $search[] = "catalogs_keys_products_filters.filter_id in (" . implode (",", $filter['f']) . ")";
7a508b62   andryeyev   + попытка исправи...
2653
      }
1b81d16b   andryeyev   + откат до пред. ...
2654
2655
  
      if (isset ($filter['y']) && $filter['y'] > 0)
7a508b62   andryeyev   + попытка исправи...
2656
      {
1b81d16b   andryeyev   + откат до пред. ...
2657
          $search[] = "catalogs_keys_products_years.year_id in (" . implode (",", $filter['y']) . ")";
7a508b62   andryeyev   + попытка исправи...
2658
      }
1b81d16b   andryeyev   + откат до пред. ...
2659
2660
  
      if (isset ($filter['r']) && count ($filter['r']) > 0)
7a508b62   andryeyev   + попытка исправи...
2661
      {
1b81d16b   andryeyev   + откат до пред. ...
2662
          $search[] = "m2.rost in (" . implode (",", $filter['r']) . ")";
7a508b62   andryeyev   + попытка исправи...
2663
2664
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2665
2666
2667
2668
2669
2670
2671
2672
2673
      if (isset ($_GET['brend']) && strlen ($_GET['brend']) > 0)
          $search[] = "p.brend_id in(" . implode (",", $this->getBrendIDs_name ($_GET['brend'])) . ")";
          /*
       * if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
       * $sex = array('male'=>1,'female'=>2,'child'=>3,'unisex'=>4);
       * $search[] = sprintf("p.sex='%d'",$sex[$_GET['sex']]);
       * }
       */
      if (isset ($_GET['sex']) && strlen ($_GET['sex']) > 0)
7a508b62   andryeyev   + попытка исправи...
2674
      {
1b81d16b   andryeyev   + откат до пред. ...
2675
2676
          $sex = $this->seralizeSex ($_GET['sex']);
          $search[] = "p.sex IN (" . implode (",", $sex) . ")";
7a508b62   andryeyev   + попытка исправи...
2677
2678
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2679
2680
      if (isset ($_GET['new']) && $_GET['new'] > 0)
          $search[] = sprintf ("p.active='%d'", $_GET['new']);
7a508b62   andryeyev   + попытка исправи...
2681
      
1b81d16b   andryeyev   + откат до пред. ...
2682
2683
2684
2685
2686
      if (isset ($_GET['top']) && $_GET['top'] > 0)
          $search[] = sprintf ("p.top='%d'", $_GET['top']);
      
      if (isset ($_GET['akciiID']) && $_GET['akciiID'] > 0)
          $search[] = sprintf ("p.akcii_id>'%d'", 0);
7a508b62   andryeyev   + попытка исправи...
2687
      
1b81d16b   andryeyev   + откат до пред. ...
2688
2689
2690
2691
2692
2693
2694
2695
      $sql = "select m.* from catalogs_modifications m,catalogs_products p ";
      if (isset ($filter['f']) && count ($filter['f']) > 0)
          $sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=p.id ";
      
      if (isset ($filter['y']) && $filter['y'] > 0)
          $sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=p.id ";
      
      if (isset ($filter['r']) && count ($filter['r']) > 0)
7a508b62   andryeyev   + попытка исправи...
2696
      {
1b81d16b   andryeyev   + откат до пред. ...
2697
          $sql .= "LEFT JOIN catalogs_modifications m2 ON m2.product_id=p.id and m2.active=1 ";
7a508b62   andryeyev   + попытка исправи...
2698
2699
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
      $sql .= "where m.size<>'' ";
      
      if (count ($search))
          $sql .= "AND " . implode (" AND ", $search) . " ";
      
      $sql .= "
              and m.product_id=p.id 
              and m.active=1 
              and p.rubric_id=? 
              and p.count_modifications>0 
              and m.size > 0
              GROUP BY m.size";
      $row = $this->db->getAll ($sql, array (
          $rubric_id 
      ), DB_FETCHMODE_ASSOC);
      
      if ($_GET['t'] == 1)
7a508b62   andryeyev   + попытка исправи...
2717
      {
1b81d16b   andryeyev   + откат до пред. ...
2718
2719
2720
          print "<pre>";
          print_r ($row);
          print "</pre>";
7a508b62   andryeyev   + попытка исправи...
2721
      }
1b81d16b   andryeyev   + откат до пред. ...
2722
2723
2724
2725
2726
2727
2728
   
      $this->tpl->assign ('size', $row);
  }
   
  function viewFiltersRost ($rubric_id)
  {
      $search = array ();
7a508b62   andryeyev   + попытка исправи...
2729
      
1b81d16b   andryeyev   + откат до пред. ...
2730
2731
2732
2733
      if (isset ($_GET['filter']) && count ($_GET['filter']) > 0)
          $filter = $this->seralizeFilter ($_GET['filter']);
      
      if (isset ($filter['f']) && count ($filter['f']) > 0)
7a508b62   andryeyev   + попытка исправи...
2734
      {
1b81d16b   andryeyev   + откат до пред. ...
2735
          $search[] = "catalogs_keys_products_filters.filter_id in (" . implode (",", $filter['f']) . ")";
7a508b62   andryeyev   + попытка исправи...
2736
2737
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2738
      if (isset ($filter['y']) && $filter['y'] > 0)
7a508b62   andryeyev   + попытка исправи...
2739
      {
1b81d16b   andryeyev   + откат до пред. ...
2740
          $search[] = "catalogs_keys_products_years.year_id in (" . implode (",", $filter['y']) . ")";
7a508b62   andryeyev   + попытка исправи...
2741
2742
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2743
2744
2745
2746
2747
2748
2749
2750
2751
      if (isset ($_GET['brend']) && strlen ($_GET['brend']) > 0)
          $search[] = "p.brend_id in(" . implode (",", $this->getBrendIDs_name ($_GET['brend'])) . ")";
          /*
       * if(isset($_GET['sex']) && strlen($_GET['sex'])>0){
       * $sex = array('male'=>1,'female'=>2,'child'=>3,'unisex'=>4);
       * $search[] = sprintf("p.sex='%d'",$sex[$_GET['sex']]);
       * }
       */
      if (isset ($_GET['sex']) && strlen ($_GET['sex']) > 0)
7a508b62   andryeyev   + попытка исправи...
2752
      {
1b81d16b   andryeyev   + откат до пред. ...
2753
2754
          $sex = $this->seralizeSex ($_GET['sex']);
          $search[] = "p.sex IN (" . implode (",", $sex) . ")";
7a508b62   andryeyev   + попытка исправи...
2755
2756
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
      $sql = "select m.* from catalogs_modifications m, catalogs_products p ";
      
      if (isset ($filter['f']) && count ($filter['f']) > 0)
          $sql .= "LEFT JOIN catalogs_keys_products_filters ON catalogs_keys_products_filters.product_id=p.id ";
      
      if (isset ($filter['y']) && $filter['y'] > 0)
          $sql .= "LEFT JOIN catalogs_keys_products_years ON catalogs_keys_products_years.product_id=p.id ";
      
      $sql .= "where m.rost<>'' ";
      
      if (count ($search))
7a508b62   andryeyev   + попытка исправи...
2768
      {
1b81d16b   andryeyev   + откат до пред. ...
2769
          $sql .= "AND " . implode (" AND ", $search) . " ";
7a508b62   andryeyev   + попытка исправи...
2770
2771
      }
      
1b81d16b   andryeyev   + откат до пред. ...
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
      $sql .= "
              and m.product_id=p.id 
              and m.active=1 
              and p.rubric_id=? 
              and p.count_modifications>0 
              GROUP BY m.rost";
      $row = $this->db->getAll ($sql, array (
          $rubric_id 
      ), DB_FETCHMODE_ASSOC);
      
      if ($_GET['t'] == 1)
      {
          print "<pre>";
          print_r ($row);
          print "</pre>";
      }
      
      $this->tpl->assign ('rost', $row);
  }
  
   function viewRostProduct($id){
    $sql = "select m.* from catalogs_modifications m where m.product_id=? GROUP BY m.rost";
    $row = $this->db->getAll($sql,array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('product_rost',$row);
   }  
  
   function viewFiltersInProducts($filter_products,$lang='ru'){
  	$search = array(); 
    if(isset($_GET['new']) && $_GET['new']>0)$search[] = sprintf("p.active='%d'",$_GET['new']);
    if(isset($_GET['top']) && $_GET['top']>0)$search[] = sprintf("p.top='%d'",$_GET['top']); 
    if(isset($_GET['akciiID']) && $_GET['akciiID']>0)$search[] = sprintf("p.akcii_id>'%d'",0);  
    
  	 $sql  ="select f.*,f.name".($lang=='ukr'?'_ukr':'')." as name from catalogs_products p, catalogs_keys_products_filters k LEFT JOIN catalogs_filters f ON f.id=k.filter_id where p.id=k.product_id AND f.id IN (".$filter_products.") and f.active='0' ";
    if(count($search))$sql .= "AND " . implode(" AND ",$search) . " ";
    $sql .= 'GROUP BY k.filter_id order by f.sort';
    //print $sql;
    $row = $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('filters', $row);
   } 
   
   function viewBrend($id){
    $row = $this->db->getRow("select * from catalogs_brends where id=?",array($id),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('brend', $row);
   }
   
   function viewBrendName($name){
    $name = iconv("utf-8", "windows-1251", $name);
    $sql = "select * from catalogs_brends where name=? limit 1";
    $row = $this->db->getRow($sql,array($name),DB_FETCHMODE_ASSOC);
    $this->tpl->assign('brend_seo', $row);
    return $row;
   }
   
   function getBrendIDs_name($name){
    $n = array();
    $name_arr = explode(";",$name);
    foreach($name_arr as $row){
  	$n[] = "'".$row."'";
    }
    //$name = iconv("utf-8", "windows-1251", $name);
    $sql = "select id from catalogs_brends where name in (" . implode(",",$n) . ")";
    //print $sql;
    return $this->db->getCol($sql,"id",array());
   }
   
   function viewBrendRubrics($name,$lang = 'ru'){
    $name = iconv("utf-8", "windows-1251", $name);
    $sql = "select r.name".(($lang=='ukr')?'_ukr':'')." as name,r.parent_id,r.translit,count(p.id) as c from catalogs_brends b
    Left Join catalogs_rubrics r On r.id=b.rubric_id
    Left Join catalogs_products p On p.brend_id=b.id AND p.count_modifications>0
    where b.name=? and r.name<>'' Group by p.brend_id";
    $res = $this->db->getAll($sql,array($name),DB_FETCHMODE_ASSOC);
    $rubrics = array();
    foreach($res as $row){
          if($row['c']>0){
                  $rubrics[$this->getRubricName($row['parent_id'],$lang)][] = $row;
          }
    }
    // print"<pre>";
    // print_r($rubrics);
    // print"</pre>";
    $this->tpl->assign('brend_rubrics',$rubrics);
   }
   
   function deleteBrend($id){
    $this->db->query("delete from catalogs_brends where id=?",array($id));
   }
   
   function viewAkciiProductID($id){
    $sql = "select rubric_id from catalogs_keys_products_akcii where product_id=?";
    $row = $this->db->getCol($sql,"rubric_id",array($id));
    $this->tpl->assign('product_akcii',$row);
   }
   
   function import_saveRubric($name,$parent_id){
    $id = $this->db->getOne("select id from catalogs_rubrics where name=? and parent_id=?",array($name,$parent_id));
    if( !$id ){
     $this->db->autoExecute("catalogs_rubrics",array('name'=>$name,'translit'=>translit($name),'parent_id' => $parent_id,"level"=>($this->getLevelRubric($parent_id)+1)),DB_AUTOQUERY_INSERT);
     return mysql_insert_id();
    }
    return $id;
   }
   
   function import_saveBrend($name,$rubric_id){
    //getRubricParent();
    $id = $this->db->getOne("select id from catalogs_brends where name=? and rubric_id=?",array($name,$rubric_id));
    if( !$id ){
     $this->db->autoExecute("catalogs_brends",array('rubric_id'=>$rubric_id,'name'=>$name),DB_AUTOQUERY_INSERT);
     return mysql_insert_id();
    }
    return $id;
   }
   
   function import_saveFilters2($rubric_id,$str){
    if(!$str)return array();
    $row_str = explode(",",trim($str));
    $arr_id = array();
    foreach($row_str as $str){
     $name = trim($str);
     $name_r = explode('=',$name);$name = $name_r[0];  $active = (isset($name_r[1])) ? 1 : 0;
     $id = $this->db->getOne("select id from catalogs_filters2 where rubric_id=? and name=?",array($rubric_id,$name));
     if( !$id ){
      $this->db->autoExecute("catalogs_filters2",array('rubric_id'=>$rubric_id,'name'=>$name,'prefix'=>encodestring($name),'active'=>$active),DB_AUTOQUERY_INSERT);
      $id = mysql_insert_id();
     }else{ $this->db->autoExecute("catalogs_filters2",array('rubric_id'=>$rubric_id,'name'=>$name,'prefix'=>encodestring($name),'active'=>$active),DB_AUTOQUERY_UPDATE,"id=$id");}
     $arr_id[] = $id;
    }
    return $arr_id;
   } 
   
   function import_saveFilters($rubric_id,$str){
    if(!$str)return array();
    $row_str = explode(",",trim($str));
    $arr_id = array();
    foreach($row_str as $str){
     $name = trim($str);
     $name_r = explode('=',$name);$name = $name_r[0];  $active = (isset($name_r[1])) ? 1 : 0;
     $id = $this->db->getOne("select id from catalogs_filters where rubric_id=? and name=?",array($rubric_id,$name));
     if( !$id ){
      $this->db->autoExecute("catalogs_filters",array('rubric_id'=>$rubric_id,'name'=>$name,'prefix'=>encodestring($name),'active'=>$active),DB_AUTOQUERY_INSERT);
      $id = mysql_insert_id();
     }else{ $this->db->autoExecute("catalogs_filters",array('rubric_id'=>$rubric_id,'name'=>$name,'prefix'=>encodestring($name),'active'=>$active),DB_AUTOQUERY_UPDATE,"id=$id");}
     $arr_id[] = $id;
    }
    return $arr_id;
   }
   
   function import_saveYear($rubric_id,$name){
    if(!$name)return false;
    $id = $this->db->getOne("select id from catalogs_years where rubric_id=? and name=?",array($rubric_id,$name));
    if( !$id ){
     $this->db->autoExecute("catalogs_years",array('rubric_id'=>$rubric_id,'name'=>$name),DB_AUTOQUERY_INSERT);
     $id = mysql_insert_id();
    }
    return ($id)?array($id):array();
   }
   
   function import_updateProductCine($product_id,$row,$line='',$mktime = 0){  // print"<pre>";print_r($row);print"</pre>";
  
    IF($product_id>0){
    IF($row[3]>0){
     if($row[2]<1){$cine=$row[1];$cine_last=0;}
     else {$cine=$row[2];$cine_last=(($row[1]>0)?$row[1]:$row[2]);}
     $this->db->autoExecute("catalogs_products",array('cine'=>$cine,'cine_last'=>$cine_last),DB_AUTOQUERY_UPDATE,"id='$product_id'");
    }
  
  
    $city = trim($row[4]);
    if(!$city)$city = "Èíòåðíåò ìàãàçèí";
    //echo "OK(1) - <br/>";
    if(strlen($city)>0){    //print $city;
     $city_id = $this->is_city($city);
     //echo "OK(2) - <br/>";
     if(!$city_id){$city_id = $this->SaveCity(array('name'=>$city));}
       $this->db->query("delete from catalogs_keys_products_cities where product_id=? and mod_code=? and mktime<?", array($product_id,$row[0],$mktime));
  
       if($row[3]==0){
  
       }
       else{
     //  $this->db->query("delete from catalogs_keys_products_cities where product_id=? and mod_code=? and mktime<?", array($product_id,$row[0],$mktime));
     //echo "OK(3) - <br/>";
       if($this->getKeysModificationCity($row[0], $city_id)){$DB_AUTOQUERY = DB_AUTOQUERY_UPDATE;$where = "mod_code='".$row[0]."' and city_id='".$city_id."'";}
       else{$DB_AUTOQUERY = DB_AUTOQUERY_INSERT;$where = null;}
  
      // if($this->getKeysModificationCityMktime_count($row[0], $city_id, $mktime)>0){$row[3]=1;}
       //print $city_id .'--'.$where."<br>";
       $fields = array('count'=>$row[3],'product_id'=>$product_id,'mod_code'=>$row[0],'city_id'=>$city_id,'mktime'=>$mktime);
       $s = $this->db->autoExecute("catalogs_keys_products_cities",$fields,$DB_AUTOQUERY,$where);
       if (PEAR::isError($s)) die($s->getMessage());
    }}
    //echo "OK(4) - <br/>";
    $active = $this->db->getOne("select count(*) from catalogs_keys_products_cities where mod_code=? and count>0",array($row[0]));
    //$active = true;
    if($this->db->getOne("select count(*) from catalogs_modifications where code=?",array($row[0]))>0){
     $this->db->autoExecute("catalogs_modifications",array('active'=>$active,'is_update'=>1,'prefix'=>$row[4]),DB_AUTOQUERY_UPDATE,"code='".$row[0]."'");
     //echo "OK(5) - <br/>";
    }else{
            //echo "OK(6) - <br/>";
     $this->saveCsvMod($line);
    }
    }
   }
   
   function saveCsvMod($line){
           echo "OK1 - <br/>";
    $tmpfname = $_SERVER['DOCUMENT_ROOT'] . "/tmp/noDB_mod.csv";
    $handle = @fopen($tmpfname, "a");
  //  $row = explode(";",$line);
  //  File_FGetCSV::fputcsv($handle,$row,';');
           echo "OK2 - <br/>";
          echo $line;
           echo "OK3 - <br/>";
    fwrite($handle,$line);
    fclose($handle);
   }
   
   function getKeysModificationCity($code, $city_id){
    return $this->db->getOne("select count(*) from catalogs_keys_products_cities where mod_code=? and city_id=?", array($code, $city_id));
   }
   
   function getKeysModificationCity_count($code, $city_id){
    return $this->db->getOne("select count from catalogs_keys_products_cities where mod_code=? and city_id=?", array($code, $city_id));
   }
   
   function getKeysModificationCityMktime_count($code, $city_id, $mktime){
    $r = $this->db->getOne("select count from catalogs_keys_products_cities where mod_code=? and city_id=? and mktime=?", array($code, $city_id, $mktime));
   return $r;
   }
   
   function getProductID($name){
    $id = $this->db->getOne("select id from catalogs_products where name=?",array($name));
    return $id;
   }
   
  function getProductID_R ($name, $rubric_parent_id)
  {  
      $id = $this->db->getOne ("select id from catalogs_products where name=? and rubric_id=?", array (
          $name,
          $rubric_parent_id 
      ));
   
      return $id;
  }
   
   function getModificationActive($code){
    return $this->db->getOne("select active from catalogs_modifications where code=?",array($code));
   }
   
  function updateProduct_countModification ($product_id, $count)
  {
      $this->db->autoExecute ("catalogs_products", array (
              'count_modifications' => $count 
      ), DB_AUTOQUERY_UPDATE, "id='$product_id'");
  }
   
   function updateProductModificationCountPrefix($product_id,$prefix = "+"){
  // print $product_id;
   $sql = "UPDATE catalogs_products SET count_modifications = count_modifications $prefix 1 WHERE id =?";
    $this->db->query($sql,array($product_id));
   }
   
   function exportCSV(){
    $tmpfname = "tmp/price.csv";
    $handle = fopen($tmpfname, "w");
    // fwrite($handle, $xml);
    $res = $this->db->getAll('select p.id,p.rubric_id,r2.name,r.name,b.name as brend,p.name,p.about,p.text,null,null,p.sex,null,p.cine,p.cine_last,p.akcii_id,p.strnumber,p.active,null,p.video_code,p.pic_big,null from catalogs_products p left join catalogs_rubrics r on r.id=p.rubric_id left join catalogs_rubrics r2 on r2.id=p.rubric_parent_id left join catalogs_brends b on b.id=p.brend_id',array());
    foreach($res as $key=>$row){
     $list = $row;
     $mod_res = $this->db->getAll('select code,size,color,pic_big,active from catalogs_modifications where product_id=? Order by id',array($row[0]));
     foreach($mod_res as $mod_row){$list[] = implode('=',$mod_row);}
  
     $f_row = $this->db->getCol("select f.name from catalogs_keys_products_filters k Left Join catalogs_filters f ON f.id=k.filter_id where k.product_id=?",'name',array($list[0]));
     $list[8]=implode(',',$f_row);
     
     $f_row = $this->db->getCol("select f.name from catalogs_keys_products_filters2 k Left Join catalogs_filters2 f ON f.id=k.filter_id where k.product_id=?",'name',array($list[0]));
     $list[9]=implode(',',$f_row);   
  
     switch($list[10]){
      case 1 : $list[10] = 'ìóæñêîé'; break;
      case 2 : $list[10] = 'æåíñêèé'; break;
      case 3 : $list[10] = 'äåòñêèé'; break;
      case 4 : $list[10] = 'óíèñåêñ'; break;
      default : $list[10] = 'ìóæñêîé'; break;
     }
     
     $y_row = $this->db->getCol("select y.name from catalogs_keys_products_years k Left Join catalogs_years y ON y.id=k.year_id where k.product_id=?",'name',array($list[0]));
     $list[11]=implode(',',$y_row);
  
     $p_row = $this->db->getCol("select pic_big from catalogs_gallery where product_id=?",'pic_big',array($list[0]));
     $list[20]=implode(',',$p_row);   
  
     //print_r($list);exit;
     $p_res = $this->db->getAll("select name,size,metka from catalogs_products_params where product_id=? order by id asc",array($list[0]));
     $param = array();
     foreach($p_res as $row_p){
      $param[] = implode('*',$row_p);
     }
     $list[17] = implode('=',$param);
  
     unset($list[0]);unset($list[1]);
     
     File_FGetCSV::fputcsv($handle,$list,';');
  
  
  
    }
  
       fclose($handle);
  
     $params = array(
        'file'                => $tmpfname,
        'contenttype'         => 'application/xls',
        'contentdisposition'  => array(HTTP_DOWNLOAD_ATTACHMENT, 'price.csv'),
        'cache'               => false
        );
        $error = HTTP_Download::staticSend($params, false); //îòäà¸ì ôàéë
        if ($error === true) {
             //åñëè ôàéë çàãðóæåí óñïåøíî, îáíîâëÿåì äàòó ïîñëåäíåé çàêà÷êè
        }
       
       unlink($tmpfname);
   }
   
    function exportCSVaktual(){
    $tmpfname = "tmp/price.csv";
    $handle = fopen($tmpfname, "w");
    // fwrite($handle, $xml);
    $res = $this->db->getAll('select p.id,p.rubric_id,r2.name,r.name,b.name as brend,p.name,p.about,p.text,null,null,p.sex,null,p.cine,p.cine_last,p.akcii_id,p.strnumber,p.active,null,p.video_code,p.pic_big,null from catalogs_products p left join catalogs_rubrics r on r.id=p.rubric_id left join catalogs_rubrics r2 on r2.id=p.rubric_parent_id left join catalogs_brends b on b.id=p.brend_id WHERE p.count_modifications>0',array());
    foreach($res as $key=>$row){
     $list = $row;
     $mod_res = $this->db->getAll('select code,size,rost,color,pic_big,active from catalogs_modifications where product_id=? Order by id',array($row[0]));
     foreach($mod_res as $mod_row){if(strlen($mod_row[2])>0)$mod_row[1] = $mod_row[1].'*'.$mod_row[2]; unset($mod_row[2]);$list[] = implode('=',$mod_row);}
  
     $f_row = $this->db->getCol("select f.name from catalogs_keys_products_filters k Left Join catalogs_filters f ON f.id=k.filter_id where k.product_id=?",'name',array($list[0]));
     $list[8]=implode(',',$f_row);
     
     $f_row = $this->db->getCol("select f.name from catalogs_keys_products_filters2 k Left Join catalogs_filters2 f ON f.id=k.filter_id where k.product_id=?",'name',array($list[0]));
     $list[9]=implode(',',$f_row);   
  
     switch($list[10]){
      case 1 : $list[10] = 'ìóæñêîé'; break;
      case 2 : $list[10] = 'æåíñêèé'; break;
      case 3 : $list[10] = 'äåòñêèé'; break;
      case 4 : $list[10] = 'óíèñåêñ'; break;
      default : $list[10] = 'ìóæñêîé'; break;
     }
     
     $y_row = $this->db->getCol("select y.name from catalogs_keys_products_years k Left Join catalogs_years y ON y.id=k.year_id where k.product_id=?",'name',array($list[0]));
     $list[11]=implode(',',$y_row);
  
     $p_row = $this->db->getCol("select pic_big from catalogs_gallery where product_id=?",'pic_big',array($list[0]));
     $list[20]=implode(',',$p_row);   
  
     //print_r($list);exit;
     $p_res = $this->db->getAll("select name,size,metka from catalogs_products_params where product_id=? order by id asc",array($list[0]));
     $param = array();
     foreach($p_res as $row_p){
      $param[] = implode('*',$row_p);
     }
     $list[17] = implode('=',$param);
  
     unset($list[0]);unset($list[1]);
     
     File_FGetCSV::fputcsv($handle,$list,';');
  
  
  
    }
  
       fclose($handle);
  
     $params = array(
        'file'                => $tmpfname,
        'contenttype'         => 'application/xls',
        'contentdisposition'  => array(HTTP_DOWNLOAD_ATTACHMENT, 'price.csv'),
        'cache'               => false
        );
        $error = HTTP_Download::staticSend($params, false); //îòäà¸ì ôàéë
        if ($error === true) {
             //åñëè ôàéë çàãðóæåí óñïåøíî, îáíîâëÿåì äàòó ïîñëåäíåé çàêà÷êè
        }
       
       unlink($tmpfname);
   }
   
   function exportModCSV(){
    $tmpfname = "tmp/price_mod.csv";
    $handle = fopen($tmpfname, "w");
    $res = $this->db->getAll('select m.code,p.cine,p.cine_last,m.active,null,m.prefix,m.is_update From catalogs_modifications m Left join catalogs_products p on p.id=m.product_id Order by m.id');
    foreach($res as $key=>$row){
     File_FGetCSV::fputcsv($handle,$row,';');
    }
    fclose($handle);
     $params = array(
        'file'                => $tmpfname,
        'contenttype'         => 'application/xls',
        'contentdisposition'  => array(HTTP_DOWNLOAD_ATTACHMENT, 'file_1.csv'),
        'cache'               => false
        );
        $error = HTTP_Download::staticSend($params, false); //îòäà¸ì ôàéë
        if ($error === true) {
             //åñëè ôàéë çàãðóæåí óñïåøíî, îáíîâëÿåì äàòó ïîñëåäíåé çàêà÷êè
        }
  
       unlink($tmpfname);
       $this->db->autoExecute("catalogs_modifications",array('is_update'=>0),DB_AUTOQUERY_UPDATE);
   }
   
   function exportUsersCSV(){
    $tmpfname = "tmp/users.csv";
    $handle = fopen($tmpfname, "w");
  
  	$sql = "SELECT u.id,u.username,u.tel,u.city,u.email,g.name,u.registered,summary,u.birthday,c.card
              FROM zlo_users AS u
              LEFT JOIN card c ON c.user_id = u.id
  			LEFT JOIN zlo_users_group AS g ON u.group = g.id
              LEFT JOIN (SELECT user_id AS order_user_id, sum( `total` ) AS `summary` FROM catalogs_orders group by user_id ) o
              ON u.id = o.order_user_id 
              ";
    $res = $this->db->getAll($sql);
    foreach($res as $key=>$row){
     File_FGetCSV::fputcsv($handle,$row,';');
    }
    fclose($handle);
     $params = array(
        'file'                => $tmpfname,
        'contenttype'         => 'application/xls',
        'contentdisposition'  => array(HTTP_DOWNLOAD_ATTACHMENT, 'users.csv'),
        'cache'               => false
        );
        $error = HTTP_Download::staticSend($params, false); //îòäà¸ì ôàéë
        if ($error === true) {
             //åñëè ôàéë çàãðóæåí óñïåøíî, îáíîâëÿåì äàòó ïîñëåäíåé çàêà÷êè
        }
  
       unlink($tmpfname);
   } 
   
   function exportUsersBirdthCSV(){
    $tmpfname = "tmp/usersBirdth.csv";
    $handle = fopen($tmpfname, "w");
  
  	$sql = "SELECT u.id,u.username,u.tel,u.city,u.email,g.name,u.registered,u.birthday,c.card
              FROM zlo_users AS u
              LEFT JOIN card c ON c.user_id = u.id
  			LEFT JOIN zlo_users_group AS g ON u.group = g.id
  			WHERE DAYOFMONTH(u.birthday) >= '19' AND DAYOFMONTH(u.birthday) <= '28' and MONTH(u.birthday) = '10'
              ";
    $res = $this->db->getAll($sql);
    foreach($res as $key=>$row){
     File_FGetCSV::fputcsv($handle,$row,';');
    }
    fclose($handle);
     $params = array(
        'file'                => $tmpfname,
        'contenttype'         => 'application/xls',
        'contentdisposition'  => array(HTTP_DOWNLOAD_ATTACHMENT, 'users.csv'),
        'cache'               => false
        );
        $error = HTTP_Download::staticSend($params, false); //îòäà¸ì ôàéë
        if ($error === true) {
             //åñëè ôàéë çàãðóæåí óñïåøíî, îáíîâëÿåì äàòó ïîñëåäíåé çàêà÷êè
        }
  
       unlink($tmpfname);
   } 
   
   function exportSeoF1CSV(){
    $tmpfname = "tmp/seo_f1.csv";
    $handle = fopen($tmpfname, "w");
    $res = $this->db->getAll("select id,name,translit,name_h1 from catalogs_rubrics");
    foreach($res as $key=>$row){
  	//$res_b = $this->db->getAll("select b.name from catalogs_products p left join catalogs_brends b on b.id=p.brend_id where p.rubric_id=? group by p.brend_id",array($row[0]));
  	//foreach($res_b as $row_b){
  	$list = array();
  	$f_res = $this->db->getAll("select id,prefix,name from catalogs_filters where rubric_id=?",array($row[0]));
  	foreach($f_res as $f_row){
  		$list[0] = $row[0];
  		$list[1] = (strlen($row[3])>0) ? $row[3] : $row[1];
  		$list[2] = $f_row[2];
  		$list[3] = "http://extremstyle.ua/".$row[2]."-catalogs/filter;filter=f".$f_row[0]."_".$f_row[1]."/";
  		//$list[4] = $row_b[0];
  		File_FGetCSV::fputcsv($handle,$list,';');
  	}
  	//}
    }
    
    
    $res = $this->db->getAll("select id,name,translit,name_h1 from catalogs_rubrics");
    foreach($res as $key=>$row){
  	//$res_b = $this->db->getAll("select b.name from catalogs_products p left join catalogs_brends b on b.id=p.brend_id where p.rubric_id=? group by p.brend_id",array($row[0]));
  	//foreach($res_b as $row_b){
  	$list = array();
  	$f_res = $this->db->getAll("select b.name from catalogs_products p left join catalogs_brends b on b.id=p.brend_id where p.rubric_id=? group by p.brend_id",array($row[0]));
  	foreach($f_res as $f_row){
  		if(!strlen($f_row[0]))continue;
  		$list[0] = $row[0];
  		$list[1] = (strlen($row[3])>0) ? $row[3] : $row[1];
  		$list[2] = $f_row[0];
  		$list[3] = "http://extremstyle.ua/".$row[2]."-catalogs/filter;brend=".$f_row[0]."/";
  		//$list[4] = $row_b[0];
  		File_FGetCSV::fputcsv($handle,$list,';');
  	}
  	//}
    }  
    
    
  
    fclose($handle);
     $params = array(
        'file'                => $tmpfname,
        'contenttype'         => 'application/xls',
        'contentdisposition'  => array(HTTP_DOWNLOAD_ATTACHMENT, 'seo_f1.csv'),
        'cache'               => false
        );
        $error = HTTP_Download::staticSend($params, false); //îòäà¸ì ôàéë
        if ($error === true) {
             //åñëè ôàéë çàãðóæåí óñïåøíî, îáíîâëÿåì äàòó ïîñëåäíåé çàêà÷êè
        }
  
       unlink($tmpfname);  
   }
   
   function exportSeoCSV(){
    $tmpfname = "tmp/seo.csv";
    $handle = fopen($tmpfname, "w");
    $res = $this->db->getAll('select p.id,p.rubric_id,r.name_h1,null,r.list_name,b.name,p.name,null,p.cine, r.translit,p.translit,r.name From catalogs_products p Left join catalogs_rubrics r on r.id=p.rubric_id Left join catalogs_brends b on b.id=p.brend_id');
    foreach($res as $key=>$row){
     $row[3] = $this->getFilterPh1($row[0]);
     if(!strlen($row[2]))$row[2] = $row[11];
     $row[7] = "http://extremstyle.ua/".$row[9]."-catalogs/".$row[10]."-".$row[0]."/";
     unset($row[9]);unset($row[10]);unset($row[11]);
     File_FGetCSV::fputcsv($handle,$row,';');
    }
    fclose($handle);
     $params = array(
        'file'                => $tmpfname,
        'contenttype'         => 'application/xls',
        'contentdisposition'  => array(HTTP_DOWNLOAD_ATTACHMENT, 'seo.csv'),
        'cache'               => false
        );
        $error = HTTP_Download::staticSend($params, false); //îòäà¸ì ôàéë
        if ($error === true) {
             //åñëè ôàéë çàãðóæåí óñïåøíî, îáíîâëÿåì äàòó ïîñëåäíåé çàêà÷êè
        }
  
       unlink($tmpfname);
   }
  
  
   function exportProductsSeoCSV(){
    $tmpfname = "tmp/products_seo.csv";
    $handle = fopen($tmpfname, "w");
    $res = $this->db->getAll('select r.id,r.name,p.id,p.name,p.cine,r.translit,p.translit,r.list_name From catalogs_products p Left join catalogs_rubrics r on r.id=p.rubric_id Left join catalogs_brends b on b.id=p.brend_id where b.name=?',array('Dakine'));
    foreach($res as $key=>$row){
  	$vowels = array("'05","'06","'07","'08","'09","'10", "'11", "'12", "'13", "'14", "'15", "'16","2005","2006","2007","2008","2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016",);
  	$row[3] = $this->getFilterPh1($row[2]) .' '. str_replace($vowels, "", $row[3]);  
     $row[8] = "http://extremstyle.ua/".$row[5]."-catalogs/".$row[6]."-".$row[2]."/";
  		unset($row[6]);unset($row[7]);
     File_FGetCSV::fputcsv($handle,$row,';');
    }
    fclose($handle);
     $params = array(
        'file'                => $tmpfname,
        'contenttype'         => 'application/xls',
        'contentdisposition'  => array(HTTP_DOWNLOAD_ATTACHMENT, 'products_seo.csv'),
        'cache'               => false
        );
        $error = HTTP_Download::staticSend($params, false); //îòäà¸ì ôàéë
        if ($error === true) {
             //åñëè ôàéë çàãðóæåí óñïåøíî, îáíîâëÿåì äàòó ïîñëåäíåé çàêà÷êè
        }
  
       unlink($tmpfname);
   }
  
   function exportProducts2SeoCSV(){
    $tmpfname = $_SERVER['DOCUMENT_ROOT'] . "/cron/products_seo.csv";
    $handle = fopen($tmpfname, "w");
    $sql = 'select r.id,r.name,p.id,p.name,p.cine,r.translit,p.translit,r.list_name From catalogs_products p Left join catalogs_rubrics r on r.id=p.rubric_id Left join catalogs_brends b on b.id=p.brend_id LEFT JOIN catalogs_keys_products_years ky ON ky.product_id=p.id LEFT JOIN catalogs_years y ON y.id=ky.year_id where p.count_modifications>0 and b.name=? and y.name in ("2013","2014","2015","2014-2015")';
    $res = $this->db->getAll($sql,array('Dakine'));
    foreach($res as $key=>$row){
  	$vowels = array("'05","'06","'07","'08","'09","'10", "'11", "'12", "'13", "'14", "'15", "'16","2005","2006","2007","2008","2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016",);
  	$row[3] = $this->getFilterPh1($row[2]) .' '. str_replace($vowels, "", $row[3]);  
     $row[8] = "http://extremstyle.ua/".$row[5]."-catalogs/".$row[6]."-".$row[2]."/";
  		unset($row[5]);unset($row[6]);unset($row[7]);
     File_FGetCSV::fputcsv($handle,$row,';');
    }
    fclose($handle);
  
   }
  
   function exportProductsFeed(){
    $tmpfname = "tmp/products_feed.csv";
    $handle = fopen($tmpfname, "w");
    $sql = 'select r.id,r.name,p.id,p.name,p.cine_last,r.translit,p.translit,r.list_name,p.pic,b.name,p.about,p.cine From catalogs_products p Left join catalogs_rubrics r on r.id=p.rubric_id Left join catalogs_brends b on b.id=p.brend_id where p.count_modifications>0 and p.cine>0';
    $res = $this->db->getAll($sql,array());
    $list = array('ID','ID2','Item title','Destination URL','Image URL','Item subtitle','Item description','Item category','Price','Sale price','Contextual keywords','Item address');
    File_FGetCSV::fputcsv($handle,$list,',');
    foreach($res as $key=>$row){
  	$url = "http://extremstyle.ua/".$row[5]."-catalogs/".$row[6]."-".$row[2]."/";
  	$image = "http://extremstyle.ua/uploaded/pic/catalogs/products/".$row[8];
  	$subtitle = $row[1].' '.$row[9];
  	$list = array(
  	$row[2],$row[2].'S',substr($row[3],0,12),$url,$image,substr($subtitle,0,12),"Ãàðàíòèÿ êà÷åñòâà",$row[1],round($row[11]).' UAH',round($row[4]).' UAH',$row[1].' '.$row[9],
  	); 
     //print_r($list);
  		unset($row[5]);unset($row[6]);unset($row[7]);
     File_FGetCSV::fputcsv($handle,$list,',');
    }
    fclose($handle);
     $params = array(
        'file'                => $tmpfname,
        'contenttype'         => 'application/xls',
        'contentdisposition'  => array(HTTP_DOWNLOAD_ATTACHMENT, 'products_feed.csv'),
        'cache'               => false
        );
        $error = HTTP_Download::staticSend($params, false); //îòäà¸ì ôàéë
        if ($error === true) {
             //åñëè ôàéë çàãðóæåí óñïåøíî, îáíîâëÿåì äàòó ïîñëåäíåé çàêà÷êè
        }
  
       unlink($tmpfname);
   } 
   
   function exportOrdersCSV(){
    $tmpfname = "tmp/orders.csv";
    $handle = fopen($tmpfname, "w");
    $res = $this->db->getAll('select o.id,o.name,o.email,o.adress,o.phone,o.total From catalogs_orders o LEFT JOIN zlo_users u ON u.id=o.user_id WHERE u.`group`=2 order by o.id desc');
    foreach($res as $key=>$row){
     $products = $this->db->getAll("select p.* from catalogs_orders_products op 
     LEFT JOIN catalogs_modifications m ON m.code=op.code 
     LEFT JOIN catalogs_products p ON p.id=m.product_id
     where op.order_id=? GROUP BY p.id",array($row[0]),DB_FETCHMODE_ASSOC);
     $arr_products = array();
     foreach($products as $p){
     $arr_products[] = $p['name'];
     }
     $row[6] = implode(', ',$arr_products);
     File_FGetCSV::fputcsv($handle,$row,';');
    }
    fclose($handle);
     $params = array(
        'file'                => $tmpfname,
        'contenttype'         => 'application/xls',
        'contentdisposition'  => array(HTTP_DOWNLOAD_ATTACHMENT, 'orders.csv'),
        'cache'               => false
        );
        $error = HTTP_Download::staticSend($params, false); //îòäà¸ì ôàéë
        if ($error === true) {
             //åñëè ôàéë çàãðóæåí óñïåøíî, îáíîâëÿåì äàòó ïîñëåäíåé çàêà÷êè
        }
  
       unlink($tmpfname);
   }
   
   function deleteProductParams($product_id){
    $sql = "delete from catalogs_products_params where product_id=?";
    $this->db->query($sql,array($product_id));
   }
   
   function saveProductParams($product_id,$pruduct_params){
    $this->deleteProductParams($product_id);
    $array_params = explode("=",$pruduct_params);
    foreach($array_params as $row){
     $arr = explode('*',$row);
     $this->db->autoExecute("catalogs_products_params",array('product_id'=>$product_id,'name'=>$arr[0],'size'=>$arr[1],'metka'=>$arr[2]),DB_AUTOQUERY_INSERT);
    }
   }
   
   function getProductParams($product_id){
    $sql = "select * from catalogs_products_params where product_id=? order by id";
    $row = $this->db->getAll($sql,array($product_id),DB_FETCHMODE_ASSOC);
    return $row;
   }
  
   function viewProductParams($product_id){
    $this->tpl->assign('params',$this->getProductParams($product_id));
   }
   
  /**
   * íóæíî äëÿ ïðîñìîòðà ïîâòîðåíèå ñòðîê â ôàéëå èìïîðòà
   */
  function saveTmpProduct ($row, $product_id)
  {
      if ($product_id > 0)
7a508b62   andryeyev   + попытка исправи...
3459
      {
1b81d16b   andryeyev   + откат до пред. ...
3460
3461
3462
3463
          $this->db->autoExecute ("catalogs_tmp_products", array (
              'name' => $row [3],
              'product_id' => $product_id 
          ), DB_AUTOQUERY_INSERT);
7a508b62   andryeyev   + попытка исправи...
3464
      }
1b81d16b   andryeyev   + откат до пред. ...
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
  }
  
  function clearTmpProduct ()
  {
      $sql = "TRUNCATE TABLE catalogs_tmp_products";
      $this->db->query ($sql, array ());
  }
  
  function viewTmpProducts ()
  {
      $sql = "
          SELECT count( name ) AS cn, name
          FROM catalogs_tmp_products
          GROUP BY name
          ORDER BY name";
      $row = $this->db->getAll ($sql, array (), DB_FETCHMODE_ASSOC);
      $this->tpl->assign ('list', $row);
      $this->tpl->assign ('list_total', $this->db->getOne ("
          select count(*) 
          from catalogs_tmp_products
      "));
  }
   
   function deleteGalleryProduct($id){
          $sql = "select * from catalogs_gallery where product_id=?";
          $res = $this->db->getAll($sql,array($id),DB_FETCHMODE_ASSOC);
          foreach($res as $row){
                  @unlink("{$_SERVER['DOCUMENT_ROOT']}/uploaded/pic/catalogs/products/gallery/{$row['pic']}");
                  $this->db->query("delete from catalogs_gallery where id=?",array($row['id']));
          }
   } 
   
   function saveGalleryPics($product_id,$pruduct_gallery_pics){
          $this->deleteGalleryProduct($product_id);
          if(strlen($pruduct_gallery_pics)>0){
          $arr = explode(",",$pruduct_gallery_pics);
          foreach($arr as $pic){
                  $pic = trim($pic);
                  $id = $this->db->nextId('mySequence_catalogs_gallery');
                  $fields_values = array('id'=>$id,'name' => $pic,'product_id' => $product_id);
                  $upload['pic'] = array('tmp_name'=>"./uploaded/pic/catalogs/products/big/$pic",'name'=>$pic);
                  $fields_values['pic'] = upload_ImageResize($upload['pic'],array('width'=>"100",'height'=>"100",'upload_path'=>"./uploaded/pic/catalogs/products/gallery/"));        
                  $fields_values['pic_big'] = $pic;
                  
                  $this->db->autoExecute("catalogs_gallery",$fields_values,DB_AUTOQUERY_INSERT);
          }
          }
   }
  
   function importCSV ($upload)
   {
       error_reporting (0);
   
       if ($upload['file']['name'] == "file_1.csv")
       {
           print "Îøèáêà! Ôàéë öåí!";
           exit ();
       }
   
       // $arr_line = file($upload['file']['tmp_name']);
       $f = fopen ($upload['file']['tmp_name'], "r") or die ("Îøèáêà!");
   
       // print"<pre>";
   
       $this->clearTmpProduct();
   
       for ($i = 0; $row_line = File_FGetCSV::fgetcsv ($f, 15000, ";"); $i ++)
       {
           // foreach($arr_line as $line){
           // $row_line = fgetcsv(";",$line);
           $rubric_id = $this->import_saveRubric ($row_line [0], 0);
           $rubric_parent_id = $this->import_saveRubric ($row_line [1], $rubric_id);
           $brend_id = $this->import_saveBrend ($row_line [2], $rubric_parent_id);
           $pruduct_name = trim ($row_line [3]);
           $pruduct_about = trim ($row_line [4]);
           $pruduct_text = trim ($row_line [5]);
           $sex = explode (',', $row_line [8]);
           $pruduct_sex = array ();
   
           foreach ($sex as $s)
           {
               switch (trim ($s))
               {
                   case 'ìóæñêîé' :
                       $pruduct_sex [] = 1;
                       break;
                   case 'æåíñêèé' :
                       $pruduct_sex [] = 2;
                       break;
                   case 'äåòñêèé' :
                       $pruduct_sex [] = 3;
                       break;
                   case 'óíèñåêñ' :
                       $pruduct_sex [] = 4;
                       break;
                   default :
                       $pruduct_sex [] = array ();
                       break;
               }
           }
    
           $pruduct_date = trim ($row_line [9]);
           $pruduct_cine = trim ($row_line [10]);
           $pruduct_cine_last = trim ($row_line [11]);
   
           if ($pruduct_cine < 1)
           {
               $pruduct_cine = $pruduct_cine_last;
               $pruduct_cine_last = 0;
           }
   
           $pruduct_akcii_id = trim ($row_line [12]);
           $pruduct_strnumber = trim ($row_line [13]);
           $pruduct_active = trim ($row_line [14]);
           $pruduct_params = trim ($row_line [15]);
           $pruduct_pic_tmp = trim ($row_line [17]);
           $pruduct_gallery_pics = trim ($row_line [18]);
           $pruduct_video = trim ($row_line [16]);
           $pruduct_update_id = $this->getProductID_R ($pruduct_name, $rubric_parent_id);
   
           if ($pruduct_update_id > 0)
           {
               $row_product = $this->db->getRow ('
                  select update_text, about, text
                  from catalogs_products
                  where id=?', array (
                       $pruduct_update_id
                   ),
                   DB_FETCHMODE_ASSOC
               );
  
               // íå îáíîâëÿòü ïðè èìïîðòå?
               // 0 - îáíîâëÿòü
               // 1 - íå îáíîâëÿòü
               if ($row_product['update_text'] == 1)
               {  
                   // about - ukr
                   $pruduct_about = $row_product['about'];
                   
                   // text - ru
                   $pruduct_text = $row_product['text'];
               } 
           }
   
           $upload_pruduct = (strlen ($pruduct_pic_tmp) > 0 && is_file ("./uploaded/pic/catalogs/products/big/$pruduct_pic_tmp")) ? array (
               'pic' => array (
                   'tmp_name' => "./uploaded/pic/catalogs/products/big/$pruduct_pic_tmp",
                   'name' => $pruduct_pic_tmp,
                   'big' => true
               )
           ) : null;
   
           $product_id = $this->saveProduct (array (
               'rubric_id' => $rubric_parent_id,
               'rubric_parent_id' => $rubric_parent_id,
               'brend_id' => $brend_id,
               'name' => $pruduct_name,
               'about' => $pruduct_about,
               'update_text' => isset ($row_product['update_text']) ? $row_product['update_text'] : 0,
               'text' => $pruduct_text,
               'video_code' => $pruduct_video,
               'sex' => $pruduct_sex,
               'date' => $pruduct_date,
               'cine' => $pruduct_cine,
               'cine_last' => $pruduct_cine_last,
               'akcii_id' => $pruduct_akcii_id,
               'strnumber' => $pruduct_strnumber,
               'active' => $pruduct_active,
               'meta_title' => $pruduct_name,
               'meta_description' => $pruduct_name,
               'meta_keywords' => $pruduct_name,
               'meta_about' => $pruduct_name,
               'filter' => $this->import_saveFilters ($rubric_parent_id, $row_line [6]),
               'filter2' => $this->import_saveFilters2 ($rubric_parent_id, $row_line [7]),
               'years' => $this->import_saveYear ($rubric_parent_id, $pruduct_date),
               'akcii' => array (),
               'update_id' => $pruduct_update_id
           ), $upload_pruduct);
   
           // print $pruduct_update_id;exit;
           $this->saveProductParams ($product_id, $pruduct_params);
           $this->saveGalleryPics ($product_id, $pruduct_gallery_pics);
           // íå óäàëÿåì !
           //$this->deleteProductModifications ($product_id, true);
           $m_i = 0;
   
           for ($i = 19; $i < count ($row_line); $i ++)
           {
               $arr_mod = trim ($row_line [$i]);
   
               if (strlen ($arr_mod) > 0)
               {
                   $row_mod = explode ("=", $arr_mod);
   
                   $upload_mod = (strlen ($row_mod [3]) > 0 && is_file ("./uploaded/pic/catalogs/modifications/big/" . $row_mod [3])) ? array (
                       'pic' => array (
                           'tmp_name' => "./uploaded/pic/catalogs/modifications/big/" . $row_mod [3],
                           'name' => $row_mod [3],
                           'big' => true
                       )
                   ) : null;
   
                   $size_mod = explode ("*", $row_mod [1]);
                   $rost = (isset ($size_mod [1])) ? $size_mod [1] : '';
                   
                   // íàõîäèì modifications_id
                   $modifications_id = $this->getModification($product_id, $row_mod[0]);  
  
                   // ñîõðàíÿåì
                   $this->SaveModification (array (
                       'product_id' => $product_id,
                       'code' => $row_mod [0],
                       'size' => $size_mod [0],
                       'rost' => $rost,
                       'color' => $row_mod [2],
                       'active' => $row_mod [4],
                       'update_id' => (int)$modifications_id
                   ), $upload_mod); 
   
                   if ($row_mod[4] > 0)
                   {
                       $m_i ++;
                   }
               }
           }
  
           $this->updateProduct_countModification ($product_id, $m_i);
   
           // $this->import_saveFilters($product_id,$rubric_parent_id,$row_line[6]);
           // $strnumber = $this->getProductID($row_line[3]); $this->save_keysProductsFilters($id,$data);
           // print_r($row_line);
   
           $this->saveTmpProduct ($row_line, $product_id);
           if (($i % 30) == 0)
           {
               sleep (1);
           }
       }
       // print"</pre>";
   }
   
   function importCSV_cine ($upload)
   {
       if ($upload['file']['name'] != "file_1.csv")
       {
           print "Îøèáêà! Òîëüêî file_1.csv";
           exit;
       }
   
       $arr_line = file ($upload['file']['tmp_name']);
   
       //print"<pre>";
       $i = 0;
       $mktime = mktime();
       foreach ($arr_line as $line)
       {
           $row_line = explode (";", $line);
           $this->import_updateProductCine($this->getModificationProduct_id ($row_line[0]), $row_line, $line, $mktime);
   
           if (($i % 30) == 0)
           {
               sleep (1);
           }
   
           $i++;
       }
  }
  
  function loadCSV_cine($upload)
  {
      if ($upload['file']['name'] != "file_1.csv")
7a508b62   andryeyev   + попытка исправи...
3736
      {
1b81d16b   andryeyev   + откат до пред. ...
3737
3738
          print "Îøèáêà! Òîëüêî file_1.csv";
          exit;
7a508b62   andryeyev   + попытка исправи...
3739
3740
      }
      
1b81d16b   andryeyev   + откат до пред. ...
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
      $file = $_SERVER['DOCUMENT_ROOT'] . "/cron/file_1.csv";
  
      //echo $file;
      //exit;
  
      move_uploaded_file ($upload['file']['tmp_name'], $file);
  }
    
  function is_loadCSV_cine()
  {
      $is = is_file($_SERVER['DOCUMENT_ROOT'] . '/cron/file_1.csv');
      $this->tpl->assign('is', $is);
  }
   
  function CronImportCSV_cine($upload)
  {
      $this->importCSV_cine ($upload);
  }
   
  function updateCountModificationProducts ()
  {
      $res = $this->db->getAll("
         select id
         from catalogs_products
      ", array (), DB_FETCHMODE_ASSOC);
   
      $i = 0;
   
      foreach ($res as $row)
7a508b62   andryeyev   + попытка исправи...
3770
      {
1b81d16b   andryeyev   + откат до пред. ...
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
          $count = $this->db->getOne ("
             select count(*)
             from catalogs_modifications
             where product_id=? and active>0", array (
                 $row['id']
             )
          );
   
          $this->db->autoExecute ("catalogs_products", array (
             'count_modifications' => $count
          ), DB_AUTOQUERY_UPDATE, "id='" . $row['id'] . "'");
   
          $i ++;
7a508b62   andryeyev   + попытка исправи...
3784
      }
1b81d16b   andryeyev   + откат до пред. ...
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
  }
   
   function config_update($id,$value){
    $this->db->autoExecute("catalogs_config",array('value'=>$value),DB_AUTOQUERY_UPDATE,"id='".$id."'");
   }
   
   function view_config_value($id){
    $r = $this->db->getOne("select value from catalogs_config where id=?",$id);
    $this->tpl->assign("config",$r);
    return $r;
   }
   
   function addViewProduct($product_id){
          $i = 0;
          foreach($_SESSION['view_product'] as $id){
                  if($product_id==$id)$i++;
          }
          if($i==0)$_SESSION['view_product'][] = $product_id;
   }
   
   function viewUserProducts($limit = "all"){ //unset($_SESSION['view_product']);
    if(isset($_SESSION['view_product']) && count($_SESSION['view_product'])>0){        
    $sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id
    WHERE catalogs_products.id IN (".implode(",",$_SESSION['view_product']).") ";
    if($limit != "all")$sql .= "LIMIT ".$limit;
    $res = $this->db->getAll($sql,array(),DB_FETCHMODE_ASSOC);
    $this->tpl->assign("products_view",$res);
    }
   }
   
   function viewSimilarProducts($product_id,$brend,$rubric_id,$mods){
  	IF($brend == 'Dakine'){
  		$rub_arr = array(14071,14069,14070,14072);
  		unset($rub_arr[array_search('rubric_id', $rub_arr)]);
  		
  		if(count($mods)>1){
  			$r = array();
  			foreach($mods as $key=>$mod){
  	$r[$key]['color'] = 	$mod['color'];		
  	$row = $this->db->getRow("select * from catalogs_products where id=?",array($product_id),DB_FETCHMODE_ASSOC);
  	$sql = "select catalogs_products.*,m.pic,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id
    LEFT JOIN catalogs_modifications m ON m.product_id=catalogs_products.id
    WHERE catalogs_products.rubric_id IN (".implode(',',$rub_arr).") and catalogs_products.count_modifications>0 and catalogs_products.cine<? and catalogs_brends.name=? and m.color=? and m.active=1 Order by catalogs_products.cine DESC LIMIT 5";
    $r[$key]["products_similar_prev"] = $this->db->getAll($sql,array($row['cine'],$brend,$mod['color']),DB_FETCHMODE_ASSOC);
    
  	
  	$sql = "select catalogs_products.*,m.pic,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id
    LEFT JOIN catalogs_modifications m ON m.product_id=catalogs_products.id
    WHERE catalogs_products.rubric_id IN (".implode(',',$rub_arr).") and catalogs_products.count_modifications>0 and catalogs_products.cine>? and catalogs_brends.name=? and m.color=? and m.active=1 Order by catalogs_products.cine DESC LIMIT 5";
    $r[$key]["products_similar_next"] = $this->db->getAll($sql,array($row['cine'],$brend,$mod['color']),DB_FETCHMODE_ASSOC);
    				
  				
  			}
  //print_r($r);
  $this->tpl->assign("products_similar",$r);			
  		}
  		else{
  		
  	$row = $this->db->getRow("select * from catalogs_products where id=?",array($product_id),DB_FETCHMODE_ASSOC);
  	$sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id
    WHERE catalogs_products.rubric_id IN (".implode(',',$rub_arr).") and catalogs_products.count_modifications>0 and catalogs_products.cine<? and catalogs_brends.name=? Order by catalogs_products.cine DESC LIMIT 5";
    $res = $this->db->getAll($sql,array($row['cine'],$brend),DB_FETCHMODE_ASSOC);
    $this->tpl->assign("products_similar_prev",$res);
  	
  	$sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id
    WHERE catalogs_products.rubric_id IN (".implode(',',$rub_arr).") and catalogs_products.count_modifications>0 and catalogs_products.cine>? and catalogs_brends.name=? Order by catalogs_products.cine DESC LIMIT 5";
    $res = $this->db->getAll($sql,array($row['cine'],$brend),DB_FETCHMODE_ASSOC);
    $this->tpl->assign("products_similar_next",$res);		
  		}
  	} else{
  	$row = $this->db->getRow("select * from catalogs_products where id=?",array($product_id),DB_FETCHMODE_ASSOC);
  	$sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id
    WHERE catalogs_products.rubric_id=? and catalogs_products.count_modifications>0 and catalogs_products.cine<? Order by catalogs_products.cine DESC LIMIT 5";
    $res = $this->db->getAll($sql,array($row['rubric_id'],$row['cine']),DB_FETCHMODE_ASSOC);
    $this->tpl->assign("products_similar_prev",$res);
  	
  	$sql = "select catalogs_products.*,catalogs_brends.name as brend_name,r.translit as rubric_translit from catalogs_products 
    LEFT JOIN catalogs_brends ON catalogs_brends.id=catalogs_products.brend_id
    LEFT JOIN catalogs_rubrics r ON r.id=catalogs_products.rubric_id
    WHERE catalogs_products.rubric_id=? and catalogs_products.count_modifications>0 and catalogs_products.cine>? Order by catalogs_products.cine DESC LIMIT 5";
    $res = $this->db->getAll($sql,array($row['rubric_id'],$row['cine']),DB_FETCHMODE_ASSOC);
    $this->tpl->assign("products_similar_next",$res);
  	}  
   }
  
   function displayCatalogsCurs(){
    $this->tpl->assign("tpl",'catalogs_curs.tpl');
   }
   
   function displayCatalogs(){
    $this->tpl->assign("tpl",'catalogs.tpl');
   }
   
   function displayProducts(){
    $this->tpl->assign("tpl",'catalogs_product-list.tpl');
   }
   
   function displayProductsTops(){
    $this->tpl->assign("tpl",'catalogs_products_tops.tpl');
   }
   
   function displayProductsHits(){
    $this->tpl->assign("tpl",'catalogs_products_hits.tpl');
   }
   
   function displayProductsHitsAll(){
    $this->tpl->assign("tpl",'catalogs_products_hits_all.tpl');
   }
  
   function displayBasket($count_products,$cine_products,$cine_products_discount,$discount){
    $this->tpl->assign("count_products",$count_products);
    $this->tpl->assign("cine_products",$cine_products);
    $this->tpl->assign("cine_products_discount",$cine_products_discount);
    $this->tpl->assign("discount",$discount);
    $this->tpl->display('basket.tpl');
   }
   
   function displayProduct(){
    $this->tpl->assign("tpl",'catalogs_product.tpl');
   }
   
   function displayProductBasket(){
    $this->tpl->assign("tpl",'catalogs_basket.tpl');
   }
   
   function displayBrends(){
    $this->tpl->assign("tpl",'catalogs_brends.tpl');
   }
   
   function displayBrend(){
    $this->tpl->assign("tpl",'catalogs_brend.tpl');
   }
   
   function displayCities(){
    $this->tpl->assign("tpl",'catalogs_cities.tpl');
   }
  
   function displayProductBasketSend(){
    $this->tpl->assign("tpl",'catalogs_basket_send.tpl');
   }
   
   function displayReg(){
    $this->tpl->assign("tpl",'catalogs_reg.tpl');
   }
   
   function displayEditUser(){
    $this->tpl->assign("tpl",'catalogs_user_edit.tpl');
   }
   
   function displayNoPsw(){
    $this->tpl->assign("tpl",'catalogs_nopsw.tpl');
   }
   
   function displayOrders(){
    $this->tpl->assign("tpl",'catalogs_orders.tpl');
   }
   
   function displayOrder(){
    $this->tpl->assign("tpl",'catalogs_order.tpl');
   }
   
   function displayUsers(){
    $this->tpl->assign("tpl",'catalogs_users.tpl');
   }
   
   function displayUser(){
    $this->tpl->assign("tpl",'catalogs_user.tpl');
   }
   
   function displayFilters(){
    $this->tpl->assign("tpl",'catalogs_filters.tpl');
   }
   
   function displayFilters2(){
    $this->tpl->assign("tpl",'catalogs_filters2.tpl');
   } 
   
   function displayYears(){
    $this->tpl->assign("tpl",'catalogs_years.tpl');
   }
   
   function displayWinProducts(){
    $this->tpl->display('catalogs_win_products.tpl');
   }
   
   function displayModifications(){
    $this->tpl->assign("tpl",'catalogs_modifications.tpl');
   }
   
   function displayImport(){
    $this->tpl->assign("tpl",'catalogs_import.tpl');
   }
   
   function displayImportCine(){
    $this->tpl->assign("tpl",'catalogs_import_cine.tpl');
   }
   
   function displayAkcii(){
    $this->tpl->assign("tpl",'catalogs_akcii.tpl');
   }
   
   function displayCompare(){
    $this->tpl->assign("tpl",'catalogs_compare.tpl');
   }
   
   function displayTmpProducts(){
    $this->tpl->assign("tpl",'catalogs_tmp_products.tpl');
   }
   
7a508b62   andryeyev   + попытка исправи...
4006
  }