Projekt

Allgemein

Profil

GX-Bug #41669 » PropertiesView.inc.php

/system/classes/properties/ - Till Tepelmann, 27.04.2015 12:44

 
1
<?php
2
/* --------------------------------------------------------------
3
   PropertiesView.inc.php 2014-09-22 gambio
4
   Gambio GmbH
5
   http://www.gambio.de
6
   Copyright (c) 2014 Gambio GmbH
7
   Released under the GNU General Public License (Version 2)
8
   [http://www.gnu.org/licenses/gpl-2.0.html]
9
   --------------------------------------------------------------
10
*/
11

    
12
#TODO: create language files
13
require_once(DIR_FS_CATALOG . 'gm/inc/gm_prepare_number.inc.php');
14

    
15
  function steves_json_encode($a=false)
16
  {
17
    if (is_null($a)) return 'null';
18
    if ($a === false) return 'false';
19
    if ($a === true) return 'true';
20
    if (is_scalar($a))
21
    {
22
      if (is_float($a))
23
      {
24
        // Always use "." for floats.
25
        return floatval(str_replace(",", ".", strval($a)));
26
      }
27

    
28
      if (is_string($a))
29
      {
30
        static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
31
        return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
32
      }
33
      else
34
        return $a;
35
    }
36
    $isList = true;
37
    for ($i = 0, reset($a); $i < count($a); $i++, next($a))
38
    {
39
      if (key($a) !== $i)
40
      {
41
        $isList = false;
42
        break;
43
      }
44
    }
45

    
46
	$coo_json = new GMJSON(false);
47

    
48
    $result = array();
49
    if ($isList)
50
    {
51
      foreach ($a as $v) $result[] = $coo_json->encode($v);
52
      return '[' . join(',', $result) . ']';
53
    }
54
    else
55
    {
56
      foreach ($a as $k => $v) $result[] = $coo_json->encode($k).':'.$coo_json->encode($v);
57
      return '{' . join(',', $result) . '}';
58
    }
59
  }
60

    
61

    
62
class PropertiesView 
63
{
64
	var $v_coo_properties_control = false;
65
	
66
	var $m_env_get_array = array();
67
	var $m_env_post_array = array();
68
	
69
	var $m_content_array = array();
70
	var $m_content_template = '';
71
	
72
	protected $coo_lang_manager;
73
	
74
	function PropertiesView($p_get_array=false, $p_post_array=false) 
75
	{
76
		$this->v_coo_properties_control = MainFactory::create_object('PropertiesControl');
77
		$this->coo_lang_manager = MainFactory::create_object('LanguageTextManager', array('properties_dropdown', $_SESSION['languages_id']));
78
		
79
		if($p_get_array) $this->m_env_get_array = $p_get_array;
80
		if($p_post_array) $this->m_env_post_array = $p_post_array;
81
	}
82
	
83
	
84
	function proceed()
85
	{
86
		$t_output = '';
87
		return $t_output;
88
	}
89
	
90
	function get_order_details_by_combis_id($p_properties_combis_id, $p_order_details_type)
91
	{
92
		$t_language_id = $_SESSION['languages_id'];
93
		$t_properties_data_array = $this->v_coo_properties_control->get_properties_combis_details($p_properties_combis_id, $t_language_id);
94
								
95
		$t_html_output = $this->build_order_details($t_properties_data_array, $p_order_details_type);
96
		return $t_html_output;
97
	}
98
	
99
	function build_order_details($p_properties_data_array, $p_order_details_type)
100
	{
101
		$t_content_data_array = array(
102
									'PROPERTIES_DATA' => $p_properties_data_array
103
								);
104
		
105
		switch($p_order_details_type)
106
		{
107
			case 'mail_html':
108
				$t_content_template = 'order_details_mail.html';
109
				break;
110
			
111
			case 'mail_text':
112
				$t_content_template = 'order_details_mail.txt';
113
				break;
114
			
115
			case 'cart':
116
			default:
117
				$t_content_template = 'order_details_cart.html';
118
				break;
119
		}
120
		
121
		$t_html_output = $this->build_html($t_content_data_array, $t_content_template);
122
		return $t_html_output;
123
	}
124
	
125
	# -1 not available
126
	#  0 out of stock
127
	#  1 available
128
	function get_combis_status_code($p_products_id, $p_value_ids_array, $p_need_qty=1)
129
	{
130
		$t_return_code = 0; # return value
131
        $c_need_qty= gm_prepare_number($p_need_qty);
132
		$t_products_properties_combis_id = $this->v_coo_properties_control->get_available_combis_ids_by_values($p_products_id, $p_value_ids_array, false);
133
		if(is_array($t_products_properties_combis_id) == false || count($t_products_properties_combis_id) != 1) 
134
		{
135
			# not found
136
			$t_return_code = -1; 
137
		}
138
		else
139
		{
140
			# found -> check quantity
141
			$t_return_code = $this->get_combis_status_code_by_combis_id($p_products_id, $t_products_properties_combis_id[0], $c_need_qty);
142
		}
143
		return $t_return_code;
144
	}
145

    
146
	#  0 out of stock
147
	#  1 available
148
	function get_combis_status_code_by_combis_id($p_products_id, $p_combis_id, $p_need_qty=1)
149
	{
150
		$t_return_code = 0; # return value
151
        
152
        // get quantity check
153
        $coo_products = MainFactory::create_object('GMDataObject', array('products', array('products_id' => $p_products_id) ));
154
        $use_properties_combis_quantity = $coo_products->get_data_value('use_properties_combis_quantity');
155
        
156
		if(($use_properties_combis_quantity == 0 && STOCK_CHECK == 'true' && ATTRIBUTE_STOCK_CHECK == 'true') || $use_properties_combis_quantity == 2)
157
		{
158
            $t_quantity = $this->v_coo_properties_control->get_properties_combis_quantity($p_combis_id);
159
        }
160
		else if(($use_properties_combis_quantity == 0 && STOCK_CHECK == 'true' && ATTRIBUTE_STOCK_CHECK == 'false') || $use_properties_combis_quantity == 1)
161
		{
162
            $t_quantity = $coo_products->get_data_value('products_quantity');
163
        }
164
		else
165
		{
166
			$t_quantity = 99999;
167
		}
168
		
169
		if($t_quantity < $p_need_qty && STOCK_ALLOW_CHECKOUT == 'true')
170
		{
171
			# out of stock but allowed
172
			$t_return_code = 2;
173
		}
174
		else if($t_quantity < $p_need_qty)
175
		{
176
			# out of stock
177
			$t_return_code = 0;
178
		}
179
		else
180
		{
181
			# combi found and available
182
			$t_return_code = 1;
183
		}
184
		return $t_return_code;
185
	}
186

    
187
	function get_combis_status_code_text($p_status_code)
188
	{	
189
		switch($p_status_code)
190
		{
191
			case -1: 	 	 
192
				$t_output_text = $this->coo_lang_manager->get_text('COMBI_NOT_EXIST');
193
				break; 
194
			case 0:
195
				$t_output_text = $this->coo_lang_manager->get_text('COMBI_NOT_AVAILABLE');
196
				break;
197
			case 1:
198
				$t_output_text = $this->coo_lang_manager->get_text('available');
199
				break;
200
			case 2:
201
				$t_output_text = $this->coo_lang_manager->get_text('COMBI_NOT_AVAILABLE_BUT_ALLOWED');
202
				break;
203
		}
204
		return $t_output_text;
205
	}
206

    
207
	function get_combis_status_json($p_products_id, $p_value_ids_array, $p_need_qty=1)
208
	{
209
		$t_output_json = false;
210

    
211
		# get CODE and TEXT
212
		$t_status_code = $this->get_combis_status_code($p_products_id, $p_value_ids_array, $p_need_qty);
213
		$t_status_text = $this->get_combis_status_code_text($t_status_code);
214
                
215
		$t_status_array = array(
216
							'STATUS_CODE' => $t_status_code,
217
							'STATUS_TEXT' => $t_status_text
218
							);
219
							
220
		$t_output_json = steves_json_encode($t_status_array);
221
		return $t_output_json;
222
	}
223
	
224
	function get_combis_status_by_combis_id_json($p_combis_id, $p_need_qty=1)
225
	{
226
		$t_output_json = false;
227

    
228
        $coo_products_properties_combis = MainFactory::create_object('GMDataObject', array('products_properties_combis', array('products_properties_combis_id' => $p_combis_id) ));
229
        
230
		# get CODE and TEXT
231
		$t_status_code = $this->get_combis_status_code_by_combis_id($coo_products_properties_combis->get_data_value('products_id'), $p_combis_id, $p_need_qty);
232
		$t_status_text = $this->get_combis_status_code_text($t_status_code);
233
      
234
		$t_status_array = array(
235
							'STATUS_CODE' => $t_status_code,
236
							'STATUS_TEXT' => $t_status_text
237
							);
238

    
239
		$t_output_json = steves_json_encode($t_status_array);
240
		return $t_output_json;
241
	}
242
        
243
        function get_combis_exists($p_products_id, $p_value_ids_array, $p_need_qty=1)
244
	{
245
		$t_return_code = 0; # return value
246
		
247
		$t_products_properties_combis_id = $this->v_coo_properties_control->get_combis_id_by_value_ids_array($p_products_id, $p_value_ids_array);
248
		if($t_products_properties_combis_id == 0) 
249
		{
250
			# not found
251
			$t_return_code = 0; 
252
		}
253
		else {
254
			# found -> return $t_products_properties_combis_id
255
			$t_return_code = $t_products_properties_combis_id;
256
		}
257
		return $t_return_code;
258
	}
259

    
260
	function get_selection_form($p_products_id, $p_language_id, $p_selected_ids=false, $p_selected_combi=false, $p_quantity=false)
261
	{
262
		$c_products_id = (int)$p_products_id;
263
		$c_language_id = (int)$p_language_id;
264
		$c_quantity = (int)$p_quantity;
265
		if($c_quantity == 0)
266
		{
267
			$c_quantity = 1;
268
		}
269

    
270
		$t_html_output = '';
271
		
272
		if(gm_get_env_info('TEMPLATE_VERSION') < FIRST_GX2_TEMPLATE_VERSION)
273
		{
274
			return $t_html_output;
275
		}
276
		
277
		$t_selection_form_type = 'dropdowns';
278

    
279
		switch($t_selection_form_type)
280
		{
281
			case 'dropdowns':
282
				// GET PRODUCT
283
				$coo_product_object = MainFactory::create_object("GMDataObject", array("products", array("products_id" => $c_products_id)));
284
				$t_properties_dropdown_mode = $coo_product_object->get_data_value('properties_dropdown_mode');
285
				$t_properties_price_show = $coo_product_object->get_data_value('properties_show_price');
286
				
287
				// GET ALL PROPERTIES DATA
288
				$t_properties_array = $this->v_coo_properties_control->get_products_properties_data($c_products_id, $c_language_id);
289

    
290
				if(is_string($p_selected_ids) && trim($p_selected_ids) != '')
291
				{
292
					$p_selected_ids = $this->v_coo_properties_control->split_properties_values_string($p_selected_ids);
293
				}
294
				$t_error = '';
295
				$t_image = '';
296
				$t_selected_combi = false;
297
				$t_selected_values = array();
298
				if($p_selected_combi != false)
299
				{
300
					// GET SELECTED COMBI
301
					$t_selected_combi = $p_selected_combi;
302
					if(trim($t_selected_combi['combi_image']) != '')
303
					{
304
						$t_image = '<img src="images/product_images/properties_combis_images/' . $t_selected_combi['combi_image'] . '" alt="" />';
305
					}					
306
				}
307
				
308
				if($t_selected_combi != false)
309
				{
310
					$t_valid_quantity = $this->v_coo_properties_control->quantity_check($coo_product_object, $t_selected_combi, $c_quantity);
311
					$t_error = $t_valid_quantity['message'];
312
					
313
					// GET VALUES FROM SELECTED COMBI
314
					foreach($t_selected_combi['COMBIS_VALUES'] as $t_value)
315
					{
316
						$t_selected_values[$t_value['properties_id']] = $t_value['properties_values_id'];
317
					}
318
				}
319
				else if(is_array($p_selected_ids) && count($p_selected_ids) > 0)
320
				{
321
					$t_selected_combi = $this->v_coo_properties_control->get_selected_combi($c_products_id, $c_language_id, $p_selected_ids);
322
					$t_selected_values = $p_selected_ids;
323
				}
324
				
325
				if($t_properties_dropdown_mode == '' && $t_selected_combi == false && is_array($p_selected_ids) && count($p_selected_ids) == count($t_properties_array))
326
				{
327
					$t_error = $this->coo_lang_manager->get_text('COMBI_NOT_EXIST');
328
				}
329
				
330
				$t_single_propertie = 0;
331
				if(count($t_selected_values) == 1)
332
				{
333
					$t_single_propertie = key($t_selected_values);
334
				}
335
				
336
				$t_available_properties_values = $this->v_coo_properties_control->get_available_properties_values_by_values($c_products_id, $t_selected_values);
337
				
338
				$t_visible_properties = array();
339
				if($t_selected_combi != false || $t_properties_dropdown_mode != 'dropdown_mode_2')
340
				{
341
					foreach($t_properties_array as $t_propertie)
342
					{
343
						$t_visible_properties[] = $t_propertie['properties_id'];
344
					}
345
				}
346
				else
347
				{
348
					if(count($t_selected_values) > 0)
349
					{
350
						$t_append_next = false;
351
						foreach($t_properties_array as $t_propertie)
352
						{
353
							if(count(array_intersect($t_selected_values, array_keys($t_propertie['values_array']))) > 0)
354
							{
355
								$t_visible_properties[] = $t_propertie['properties_id'];
356
							}
357
							else
358
							{
359
								$t_append_next = true;
360
							}
361
							if($t_append_next == true)
362
							{
363
								$t_visible_properties[] = $t_propertie['properties_id'];
364
								break;
365
							}
366
						}
367
					}
368
				}
369
				
370
				$t_index = 0;
371
				foreach($t_properties_array as $t_propertie)
372
				{
373
					$t_properties_id = $t_propertie['properties_id'];
374
					$t_visible = false;
375
					if(in_array($t_properties_id, $t_visible_properties) == true || $t_index == 0)
376
					{
377
						$t_visible = true;
378
					}
379
					foreach($t_properties_array[$t_properties_id]['values_array'] as $t_value)
380
					{
381
						$t_value_id = $t_value['properties_values_id'];
382
						$t_disabled = true;
383
						$t_selected = false;
384
						if(in_array($t_value_id, $t_available_properties_values) == true)
385
						{
386
							$t_disabled = false;
387
						}
388
						if(in_array($t_value_id, $t_selected_values) == true && $t_visible == true)
389
						{
390
							$t_selected = true;
391
						}
392
						$t_properties_array[$t_properties_id]['values_array'][$t_value_id]['selected'] = $t_selected;
393
						$t_properties_array[$t_properties_id]['values_array'][$t_value_id]['disabled'] = $t_disabled;
394
					}
395
					$t_properties_array[$t_properties_id]['visible'] = $t_visible;
396
					$t_index++;
397
				}			
398
				
399
				$t_content_data_array = array(
400
											'products_id'				=> $c_products_id,
401
											'properties_dropdown_mode'	=> $t_properties_dropdown_mode,
402
											'properties_price_show'		=> $t_properties_price_show,
403
											'properties_currency'		=> $_SESSION['currency'],
404
											'PROPERTIES_DATA'			=> $t_properties_array,
405
											'PROPERTIES_ERROR'			=> $t_error,
406
											'PROPERTIES_IMAGE'			=> $t_image
407
										);
408
				$t_content_template = 'selection_forms/dropdowns.html';
409
				break;
410
			
411
			default:
412
				break;
413
		}
414

    
415
		$t_html_output = $this->build_html($t_content_data_array, $t_content_template);
416
		return $t_html_output;
417
	}
418
		
419
	
420
	
421
	
422
	# standard function
423
	function build_html($p_content_data_array, $p_template_file, $p_add_languages=true)
424
	{
425
		# language array for assigning in smarty template
426
		if($p_add_languages)
427
		{
428
			$t_languages_array = $this->v_coo_properties_control->get_shop_languages_data();
429
			$p_content_data_array = array_merge(
430
										array('LANGUAGES' => $t_languages_array), 
431
										$p_content_data_array);
432
		}
433
		$coo_smarty = new Smarty();
434
		
435
		# assign content
436
		$coo_smarty->assign('content_data', $p_content_data_array);
437
		
438
		# compile settings
439
		$coo_smarty->compile_dir = DIR_FS_CATALOG.'templates_c/';
440
		$coo_smarty->caching = false;
441
		
442
		# get html content
443
		$t_full_template_path = DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/module/properties/'.$p_template_file;
444
		//$coo_smarty->clear_compiled_tpl($t_full_template_path);
445
		//$coo_smarty->clear_compiled_tpl();
446
		
447
		$t_html_output = $coo_smarty->fetch($t_full_template_path);
448
		return $t_html_output;
449
	}
450
	
451
	# ???
452
	function get_output() 
453
	{
454
		$t_content_array = $this->m_content_array;
455
		$t_template_file = $this->m_content_template;
456
		
457
		$t_html_output = $this->build_html($t_content_array, $t_template_file); 
458
		return $t_html_output;
459
	}
460
}
    (1-1/1)