1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
9	header("location: index.php");
10	exit;
11}
12
13/**
14 * @return array
15 */
16function module_cart_info()
17{
18	return [
19		'name' => tra('Cart'),
20		'description' => tra('Displays the content of the cart, allows quantities to be modified and proceeds to payment.'),
21		'prefs' => ['payment_feature'],
22		'params' => [
23			'ajax' => [
24				'name' => tra('Use AJAX'),
25				'description' => tra('Use AJAX services for managing the cart') . ' (y/n)',
26				'filter' => 'alpha',
27				'default' => 'n',
28			],
29			'showItems' => [
30				'name' => tra('Show Items'),
31				'description' => tra('Shows the items in the cart as they are added') . ' (y/n)',
32				'filter' => 'alpha',
33				'default' => 'y',
34			],
35			'showCount' => [
36				'name' => tra('Show Item Count'),
37				'description' => tra('Shows the number of items in the cart') . ' (y/n)',
38				'filter' => 'alpha',
39				'default' => 'n',
40			],
41			'checkoutURL' => [
42				'name' => tra('Checkout URL'),
43				'description' => tra('Where to go to when the "Check-out" button is clicked but before the payment invoice is generated') . ' ' . tr('(Default empty: Goes to tiki-payment.php)'),
44				'filter' => 'url',
45				'default' => '',
46			],
47			'postPaymentURL' => [
48				'name' => tra('Post-Payment URL'),
49				'description' => tra('Where to go to once the payment has been generated, will append "?invoice=xx" parameter on the URL for use in pretty trackers etc.') . ' ' . tr('(Default empty: Goes to tiki-payment.php)'),
50				'filter' => 'url',
51				'default' => '',
52			],
53			'showWeight' => [
54				'name' => tra('Show Total Weight'),
55				'description' => tra('Shows the weight of the items in the cart') . ' (y/n)',
56				'filter' => 'alpha',
57				'default' => 'n',
58			],
59			'weightUnit' => [
60				'name' => tra('Weight Unit'),
61				'description' => tra('Shown after the weight'),
62				'filter' => 'alpha',
63				'default' => 'g',
64			],
65			'showItemButtons' => [
66				'name' => tra('Show Item Buttons'),
67				'description' => tra('Shows add, remove and delete buttons on items') . ' (y/n)',
68				'filter' => 'alpha',
69				'default' => 'n',
70			],
71		],
72	];
73}
74
75/**
76 * @param $mod_reference
77 * @param $module_params
78 */
79function module_cart($mod_reference, &$module_params)
80{
81	global $jitRequest;
82
83	$smarty = TikiLib::lib('smarty');
84	$access = TikiLib::lib('access');
85	$cartlib = TikiLib::lib('cart');
86
87	$info = module_cart_info();
88	$defaults = [];
89	foreach ($info['params'] as $key => $param) {
90		$defaults[$key] = $param['default'];
91	}
92
93	if (! empty($module_params['ajax']) && $module_params['ajax'] === 'y') {
94		$smarty->assign('json_data', ' data-params=\'' . json_encode(array_filter($module_params)) . '\'');
95	} else {
96		$smarty->assign('json_data', '');
97	}
98
99	$module_params = array_merge($defaults, $module_params);
100
101	if ($jitRequest->update->text() && $cart = $jitRequest->cart->asArray()) {
102		foreach ($cart as $code => $quantity) {
103			$cartlib->update_quantity($code, $quantity);
104		}
105
106		if ($module_params['ajax'] !== 'y') {
107			$access->redirect($_SERVER['REQUEST_URI'], tra('The quantities in your cart were updated.'));
108		}
109	}
110
111	if (isset($_POST['checkout'])) {
112		if ($module_params['checkoutURL']) {
113			$access->redirect($module_params['checkoutURL']);
114		} else {
115			$invoice = $cartlib->request_payment();
116
117			if ($invoice) {
118				if ($module_params['postPaymentURL']) {
119					$delimiter = (strpos($module_params['postPaymentURL'], '?') === false) ? '?' : '&';
120					$access->redirect($module_params['postPaymentURL'] . $delimiter . 'invoice=' . (int)$invoice, tr('The order was recorded and is now awaiting payment. Reference number is %0.', $invoice));
121				} else {
122					$access->redirect('tiki-payment.php?invoice=' . (int)$invoice, tr('The order was recorded and is now awaiting payment. Reference number is %0.', $invoice));
123				}
124			}
125		}
126	}
127
128	if ($cartlib->has_gift_certificate()) {
129		if (! empty($_POST['gift_certificate_redeem_code'])) {
130			$added = $cartlib->add_gift_certificate($_POST['gift_certificate_redeem_code']);
131			if ($added) {
132				$access->redirect($_SERVER['REQUEST_URI'], tra('Gift card added'));
133			} else {
134				$access->redirect($_SERVER['REQUEST_URI'], tra('Gift card not found'));
135			}
136		}
137
138		if (isset($_POST['remove_gift_certificate'])) {
139			$cartlib->add_gift_certificate();
140			$access->redirect($_SERVER['REQUEST_URI'], tra('Gift card removed'));
141		}
142
143		$cartlib->get_gift_certificate();
144
145		$smarty->assign('has_gift_certificate', true);
146		$smarty->assign('gift_certificate_redeem_code', $cartlib->gift_certificate_code);
147		$smarty->assign('gift_certificate_amount', $cartlib->gift_certificate_amount);
148		$smarty->assign('gift_certificate_mode_symbol_before', $cartlib->gift_certificate_mode_symbol_before);
149		$smarty->assign('gift_certificate_mode_symbol_after', $cartlib->gift_certificate_mode_symbol_after);
150	}
151
152	$smarty->assign('cart_total', $cartlib->get_total());
153	$smarty->assign('cart_content', $cartlib->get_content());
154	$smarty->assign('cart_weight', $cartlib->get_total_weight());
155	$smarty->assign('cart_count', $cartlib->get_count());
156}
157