forked from kongondo/Padloper2Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_func.php
291 lines (249 loc) · 8.43 KB
/
_func.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
namespace ProcessWire;
/**
* /site/templates/_func.php
*
* Example of shared functions used by template files
*
* This file is currently included by _init.php
*
* FUN FACT: This file is identical to the one in the NON-multi-language
* version of this site profile (site-default). In fact, it's rare that
* one has to think about languages when developing a multi-language
* site in ProcessWire.
*
*/
/**
* Given a group of pages, render a simple <ul> navigation
*
* This is here to demonstrate an example of a simple shared function.
* Usage is completely optional.
*
* @param PageArray $items
* @return string
*
*/
function renderNav(PageArray $items) {
// $out is where we store the markup we are creating in this function
$out = '';
// cycle through all the items
foreach ($items as $item) {
// render markup for each navigation item as an <li>
if ($item->id == wire('page')->id) {
// if current item is the same as the page being viewed, add a "current" class to it
$out .= "<li class='current'>";
} else {
// otherwise just a regular list item
$out .= "<li>";
}
// markup for the link
$out .= "<a href='$item->url'>$item->title</a> ";
// if the item has summary text, include that too
if ($item->summary) $out .= "<div class='summary'>$item->summary</div>";
// close the list item
$out .= "</li>";
}
// if output was generated above, wrap it in a <ul>
if ($out) $out = "<ul class='nav'>$out</ul>\n";
// return the markup we generated above
return $out;
}
/**
* Given a group of pages, render a <ul> navigation tree
*
* This is here to demonstrate an example of a more intermediate level
* shared function and usage is completely optional. This is very similar to
* the renderNav() function above except that it can output more than one
* level of navigation (recursively) and can include other fields in the output.
*
* @param array|PageArray $items
* @param int $maxDepth How many levels of navigation below current should it go?
* @param string $fieldNames Any extra field names to display (separate multiple fields with a space)
* @param string $class CSS class name for containing <ul>
* @return string
*
*/
function renderNavTree($items, $maxDepth = 0, $fieldNames = '', $class = 'nav') {
// if we were given a single Page rather than a group of them, we'll pretend they
// gave us a group of them (a group/array of 1)
if ($items instanceof Page) $items = array($items);
// $out is where we store the markup we are creating in this function
$out = '';
// cycle through all the items
foreach ($items as $item) {
// markup for the list item...
// if current item is the same as the page being viewed, add a "current" class to it
$out .= $item->id == wire('page')->id ? "<li class='current'>" : "<li>";
// markup for the link
$out .= "<a href='$item->url'>$item->title</a>";
// if there are extra field names specified, render markup for each one in a <div>
// having a class name the same as the field name
if ($fieldNames) foreach (explode(' ', $fieldNames) as $fieldName) {
$value = $item->get($fieldName);
if ($value) $out .= " <div class='$fieldName'>$value</div>";
}
// if the item has children and we're allowed to output tree navigation (maxDepth)
// then call this same function again for the item's children
if ($item->hasChildren() && $maxDepth) {
if ($class == 'nav') $class = 'nav nav-tree';
$out .= renderNavTree($item->children, $maxDepth - 1, $fieldNames, $class);
}
// close the list item
$out .= "</li>";
}
// if output was generated above, wrap it in a <ul>
if ($out) $out = "<ul class='$class'>$out</ul>\n";
// return the markup we generated above
return $out;
}
function renderProducts($selector = "") {
$padloper = wire('padloper');
$selector = "template=product,limit=50," . $selector;
$products = $padloper->find($selector);
/** @var TemplateFile $t */
$t = getPartialTemplate('products-grid-html.php');
$t->set('products', $products);
// -----------
$out = $t->render();
// --------
return $out;
}
function renderSingleProduct($product) {
/** @var TemplateFile $t */
$t = getPartialTemplate('single-product-html.php');
$padloper = wire('padloper');
$cart = $padloper->cart;
// ------------------
$numberOfTitles = $cart->getNumberOfTitles();
$totalAmount = $cart->getTotalAmount();
$totalQuantity = $cart->getQuantity();
$t->set('product', $product);
$t->set('numberOfTitles', $numberOfTitles);
$t->set('totalAmount', $totalAmount);
$t->set('totalQuantity', $totalQuantity);
// -------------
$out = $t->render();
// --------
return $out;
}
function renderRelatedProducts($product, $selector = "") {
$padloper = wire('padloper');
// ------------
// find related products
// @todo: for now, we only 'relate' using categories
$productCategories = $product->padloper_categories;
$productCategoriesIDs = $productCategories->implode("|", 'id');
// -----------
$selector = "template=product,limit=50,categories={$productCategoriesIDs},id!={$product->id}," . $selector;
$relatedProducts = $padloper->find($selector);
/** @var TemplateFile $t */
$t = getPartialTemplate('related-products-html.php');
$t->set('relatedProducts', $relatedProducts);
// -----------
$out = $t->render();
// --------
return $out;
}
function renderCategories($selector = "") {
$padloper = wire('padloper');
// @note - WE WANT CATEGORIES WITH AT LEAST ONE PUBLISHED PRODUCT
// we use owner selector for this!
// @see: https://processwire.com/blog/posts/processwire-3.0.95-core-updates/
$selector = "template=category, categories.owner.status!=unpublished,limit=50," . $selector;
$categories = $padloper->find($selector);
/** @var TemplateFile $t */
$t = getPartialTemplate('categories-grid-html.php');
$t->set('categories', $categories);
// -----------
$out = $t->render();
// --------
return $out;
}
function renderFooter() {
/** @var TemplateFile $t */
$t = getPartialTemplate('footer-html.php');
// -----------
$out = $t->render();
// --------
return $out;
}
function renderCarousel() {
/** @var TemplateFile $t */
$t = getPartialTemplate('carousel-html.php');
// -----------
$out = $t->render();
// --------
return $out;
}
function renderSideCart() {
/** @var TemplateFile $t */
$t = getPartialTemplate('side-cart-html.php');
// -----------
$out = $t->render();
// --------
return $out;
}
function renderTopNavigation() {
/** @var TemplateFile $t */
$t = getPartialTemplate('top-navigation-html.php');
// -----------
$out = $t->render();
// --------
return $out;
}
function renderCheckoutForm($formErrors = [], $previousValues = null) {
/** @var TemplateFile $t */
$t = getPartialTemplate('checkout-form-html.php');
$padloper = wire('padloper');
$cartItems = $padloper->getCart();
$t->set('cartItems', $cartItems);
$t->set('formErrors', $formErrors);
// @note: if not empty will be WireInputData from $input->post of values submitted with the form
$t->set('previousValues', $previousValues);
// -----------
$out = $t->render();
// --------
return $out;
}
function renderCheckoutFormOrderSummary($cartItems) {
/** @var TemplateFile $t */
$t = getPartialTemplate('checkout-form-order-summary-html.php');
$t->set('cartItems', $cartItems);
// -----------
$out = $t->render();
// --------
return $out;
}
function renderCheckoutFormCustomerDetails($formErrors, $previousValues) {
/** @var TemplateFile $t */
$t = getPartialTemplate('checkout-form-customer-details-html.php');
$padloper = wire('padloper');
// @TODO: SET LOGGED IN USER AS CUSTOMER?
$shippingCountries = $padloper->getShippingCountries();
$t->set('shippingCountries', $shippingCountries);
$t->set('formErrors', $formErrors);
// @note: if not empty will be WireInputData from $input->post of values submitted with the form
$t->set('previousValues', $previousValues);
// -----------
$out = $t->render();
// --------
return $out;
}
// -----------
function getPartialTemplate($file) {
$config = wire('config');
$templatePath = $config->paths->templates . "partials/" . $file;
return new TemplateFile($templatePath);
}
function getCartItemThumbURL($cartItem, $cartItemTitle, $isSmall = false) {
$out = "";
if (!empty($cartItem->pad_thumb_url)) {
// $isSmallClass = empty($isSmall) ? '' : ' h-20 w-20 object-cover rounded';
$isSmallClass = empty($isSmall) ? '' : ' h-20 w-20 object-cover';
$out .= "<div class='col-span-1 self-center'>
<img src='{$cartItem->pad_thumb_url}' alt='{$cartItemTitle}' class='rounded w-full{$isSmallClass}'>
</div>";
}
// ------
return $out;
}