Complex classes like ProductGroup often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductGroup, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
79 | class ProductGroup extends Page |
||
|
|||
80 | { |
||
81 | /** |
||
82 | * standard SS variable. |
||
83 | * |
||
84 | * @static Array |
||
85 | */ |
||
86 | private static $db = array( |
||
87 | 'NumberOfProductsPerPage' => 'Int', |
||
88 | 'LevelOfProductsToShow' => 'Int', |
||
89 | 'DefaultSortOrder' => 'Varchar(20)', |
||
90 | 'DefaultFilter' => 'Varchar(20)', |
||
91 | 'DisplayStyle' => 'Varchar(20)', |
||
92 | ); |
||
93 | |||
94 | /** |
||
95 | * standard SS variable. |
||
96 | * |
||
97 | * @static Array |
||
98 | */ |
||
99 | private static $has_one = array( |
||
100 | 'Image' => 'Product_Image', |
||
101 | ); |
||
102 | |||
103 | /** |
||
104 | * standard SS variable. |
||
105 | * |
||
106 | * @static Array |
||
107 | */ |
||
108 | private static $belongs_many_many = array( |
||
109 | 'AlsoShowProducts' => 'Product', |
||
110 | ); |
||
111 | |||
112 | /** |
||
113 | * standard SS variable. |
||
114 | * |
||
115 | * @static Array |
||
116 | */ |
||
117 | private static $defaults = array( |
||
118 | 'DefaultSortOrder' => 'default', |
||
119 | 'DefaultFilter' => 'default', |
||
120 | 'DisplayStyle' => 'default', |
||
121 | 'LevelOfProductsToShow' => 99, |
||
122 | ); |
||
123 | |||
124 | /** |
||
125 | * standard SS variable. |
||
126 | * |
||
127 | * @static Array |
||
128 | */ |
||
129 | private static $indexes = array( |
||
130 | 'LevelOfProductsToShow' => true, |
||
131 | 'DefaultSortOrder' => true, |
||
132 | 'DefaultFilter' => true, |
||
133 | 'DisplayStyle' => true, |
||
134 | ); |
||
135 | |||
136 | private static $summary_fields = array( |
||
137 | 'Image.CMSThumbnail' => 'Image', |
||
138 | 'Title' => 'Category', |
||
139 | 'NumberOfProducts' => 'Direct Product Count' |
||
140 | ); |
||
141 | |||
142 | private static $casting = array( |
||
143 | 'NumberOfProducts' => 'Int' |
||
144 | ); |
||
145 | |||
146 | /** |
||
147 | * standard SS variable. |
||
148 | * |
||
149 | * @static String |
||
150 | */ |
||
151 | private static $default_child = 'Product'; |
||
152 | |||
153 | /** |
||
154 | * standard SS variable. |
||
155 | * |
||
156 | * @static String | Array |
||
157 | */ |
||
158 | private static $icon = 'ecommerce/images/icons/productgroup'; |
||
159 | |||
160 | /** |
||
161 | * Standard SS variable. |
||
162 | */ |
||
163 | private static $singular_name = 'Product Category'; |
||
164 | public function i18n_singular_name() |
||
165 | { |
||
166 | return _t('ProductGroup.SINGULARNAME', 'Product Category'); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Standard SS variable. |
||
171 | */ |
||
172 | private static $plural_name = 'Product Categories'; |
||
173 | public function i18n_plural_name() |
||
174 | { |
||
175 | return _t('ProductGroup.PLURALNAME', 'Product Categories'); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Standard SS variable. |
||
180 | * |
||
181 | * @var string |
||
182 | */ |
||
183 | private static $description = 'A page the shows a bunch of products, based on your selection. By default it shows products linked to it (children)'; |
||
184 | |||
185 | public function canCreate($member = null) |
||
186 | { |
||
187 | if (! $member) { |
||
188 | $member = Member::currentUser(); |
||
189 | } |
||
190 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
191 | if ($extended !== null) { |
||
192 | return $extended; |
||
193 | } |
||
194 | if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) { |
||
195 | return true; |
||
196 | } |
||
197 | |||
198 | return parent::canEdit($member); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Shop Admins can edit. |
||
203 | * |
||
204 | * @param Member $member |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function canEdit($member = null) |
||
209 | { |
||
210 | if (! $member) { |
||
211 | $member = Member::currentUser(); |
||
212 | } |
||
213 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
214 | if ($extended !== null) { |
||
215 | return $extended; |
||
216 | } |
||
217 | if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) { |
||
218 | return true; |
||
219 | } |
||
220 | |||
221 | return parent::canEdit($member); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Standard SS method. |
||
226 | * |
||
227 | * @param Member $member |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function canDelete($member = null) |
||
232 | { |
||
233 | if (is_a(Controller::curr(), Object::getCustomClass('ProductsAndGroupsModelAdmin'))) { |
||
234 | return false; |
||
235 | } |
||
236 | if (! $member) { |
||
237 | $member = Member::currentUser(); |
||
238 | } |
||
239 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
240 | if ($extended !== null) { |
||
241 | return $extended; |
||
242 | } |
||
243 | if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) { |
||
244 | return true; |
||
245 | } |
||
246 | |||
247 | return parent::canEdit($member); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Standard SS method. |
||
252 | * |
||
253 | * @param Member $member |
||
254 | * |
||
255 | * @return bool |
||
256 | */ |
||
257 | public function canPublish($member = null) |
||
258 | { |
||
259 | if (Permission::checkMember($member, Config::inst()->get('EcommerceRole', 'admin_permission_code'))) { |
||
260 | return true; |
||
261 | } |
||
262 | |||
263 | return parent::canEdit($member); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * list of sort / filter / display variables. |
||
268 | * |
||
269 | * @var array |
||
270 | */ |
||
271 | protected $sortFilterDisplayNames = array( |
||
272 | 'SORT' => array( |
||
273 | 'value' => 'default', |
||
274 | 'configName' => 'sort_options', |
||
275 | 'sessionName' => 'session_name_for_sort_preference', |
||
276 | 'getVariable' => 'sort', |
||
277 | 'dbFieldName' => 'DefaultSortOrder', |
||
278 | 'translationCode' => 'SORT_BY', |
||
279 | ), |
||
280 | 'FILTER' => array( |
||
281 | 'value' => 'default', |
||
282 | 'configName' => 'filter_options', |
||
283 | 'sessionName' => 'session_name_for_filter_preference', |
||
284 | 'getVariable' => 'filter', |
||
285 | 'dbFieldName' => 'DefaultFilter', |
||
286 | 'translationCode' => 'FILTER_FOR', |
||
287 | ), |
||
288 | 'DISPLAY' => array( |
||
289 | 'value' => 'default', |
||
290 | 'configName' => 'display_styles', |
||
291 | 'sessionName' => 'session_name_for_display_style_preference', |
||
292 | 'getVariable' => 'display', |
||
293 | 'dbFieldName' => 'DisplayStyle', |
||
294 | 'translationCode' => 'DISPLAY_STYLE', |
||
295 | ), |
||
296 | ); |
||
297 | |||
298 | /** |
||
299 | * @var array |
||
300 | * List of options to show products. |
||
301 | * With it, we provide a bunch of methods to access and edit the options. |
||
302 | * NOTE: we can not have an option that has a zero key ( 0 => "none"), as this does not work |
||
303 | * (as it is equal to not completed yet - not yet entered in the Database). |
||
304 | */ |
||
305 | protected $showProductLevels = array( |
||
306 | 99 => 'All Child Products (default)', |
||
307 | -2 => 'None', |
||
308 | -1 => 'All products', |
||
309 | 1 => 'Direct Child Products', |
||
310 | 2 => 'Direct Child Products + Grand Child Products', |
||
311 | 3 => 'Direct Child Products + Grand Child Products + Great Grand Child Products', |
||
312 | 4 => 'Direct Child Products + Grand Child Products + Great Grand Child Products + Great Great Grand Child Products', |
||
313 | ); |
||
314 | |||
315 | /** |
||
316 | * variable to speed up methods in this class. |
||
317 | * |
||
318 | * @var array |
||
319 | */ |
||
320 | protected $configOptionsCache = array(); |
||
321 | |||
322 | /** |
||
323 | * cache variable for default preference key. |
||
324 | * |
||
325 | * @var array |
||
326 | */ |
||
327 | protected $myUserPreferencesDefaultCache = array(); |
||
328 | |||
329 | /** |
||
330 | * count before limit. |
||
331 | * |
||
332 | * @var int |
||
333 | */ |
||
334 | protected $rawCount = 0; |
||
335 | |||
336 | /** |
||
337 | * count after limit. |
||
338 | * |
||
339 | * @var int |
||
340 | */ |
||
341 | protected $totalCount = 0; |
||
342 | |||
343 | /** |
||
344 | * Can product list (and related) be cached at all? |
||
345 | * Set this to FALSE if the product details can be changed |
||
346 | * for an individual user. |
||
347 | * |
||
348 | * @var bool |
||
349 | */ |
||
350 | protected $allowCaching = true; |
||
351 | |||
352 | /** |
||
353 | * return the options for one type. |
||
354 | * This method solely exists to speed up processing. |
||
355 | * |
||
356 | * @param string $type - options are FILTER | SORT | DISPLAY |
||
357 | * |
||
358 | * @return array |
||
359 | */ |
||
360 | protected function getConfigOptions($type) |
||
361 | { |
||
362 | if (!isset($this->configOptionsCache[$type])) { |
||
363 | $configName = $this->sortFilterDisplayNames[$type]['configName']; |
||
364 | $this->configOptionsCache[$type] = EcommerceConfig::get($this->ClassName, $configName); |
||
365 | } |
||
366 | |||
367 | return $this->configOptionsCache[$type]; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * returns the full sortFilterDisplayNames set, a subset, or one value |
||
372 | * by either type (e.g. FILER) or variable (e.g dbFieldName) |
||
373 | * or both. |
||
374 | * |
||
375 | * @param string $typeOfVariableName FILTER | SORT | DISPLAY or sessionName, getVariable, etc... |
||
376 | * @param string $variable: sessionName, getVariable, etc... |
||
377 | * |
||
378 | * @return array | String |
||
379 | */ |
||
380 | protected function getSortFilterDisplayNames($typeOrVariable = '', $variable = '') |
||
381 | { |
||
382 | //return a string ... |
||
383 | if ($variable) { |
||
384 | return $this->sortFilterDisplayNames[$typeOrVariable][$variable]; |
||
385 | } |
||
386 | //return an array ... |
||
387 | $data = array(); |
||
388 | if (isset($this->sortFilterDisplayNames[$typeOrVariable])) { |
||
389 | $data = $this->sortFilterDisplayNames[$typeOrVariable]; |
||
390 | } elseif ($typeOrVariable) { |
||
391 | foreach ($this->sortFilterDisplayNames as $group) { |
||
392 | $data[] = $group[$typeOrVariable]; |
||
393 | } |
||
394 | } else { |
||
395 | $data = $this->sortFilterDisplayNames; |
||
396 | } |
||
397 | |||
398 | return $data; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * sets a user preference. This is typically used by the controller |
||
403 | * to set filter and sort. |
||
404 | * |
||
405 | * @param string $type SORT | FILTER | DISPLAY |
||
406 | * @param string $value |
||
407 | */ |
||
408 | protected function setCurrentUserPreference($type, $value) |
||
409 | { |
||
410 | $this->sortFilterDisplayNames[$type]['value'] = $value; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Get a user preference. |
||
415 | * This value can be updated by the controller |
||
416 | * For example, the filter can be changed, based on a session value. |
||
417 | * |
||
418 | * @param string $type SORT | FILTER | DISPLAY |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | protected function getCurrentUserPreferences($type) |
||
423 | { |
||
424 | return $this->sortFilterDisplayNames[$type]['value']; |
||
425 | } |
||
426 | |||
427 | /********************* |
||
428 | * SETTINGS: Default Key |
||
429 | *********************/ |
||
430 | |||
431 | /** |
||
432 | * Checks for the most applicable user preferences for this page: |
||
433 | * 1. what is saved in Database for this page. |
||
434 | * 2. what the parent product group has saved in the database |
||
435 | * 3. what the standard default is. |
||
436 | * |
||
437 | * @param string $type - FILTER | SORT | DISPLAY |
||
438 | * |
||
439 | * @return string - returns the key |
||
440 | */ |
||
441 | protected function getMyUserPreferencesDefault($type) |
||
442 | { |
||
443 | if (!isset($this->myUserPreferencesDefaultCache[$type]) || !$this->myUserPreferencesDefaultCache[$type]) { |
||
444 | $options = $this->getConfigOptions($type); |
||
445 | $dbVariableName = $this->sortFilterDisplayNames[$type]['dbFieldName']; |
||
446 | $defaultOption = ''; |
||
447 | if ($defaultOption == 'inherit' && $parent = $this->ParentGroup()) { |
||
448 | $defaultOption = $parent->getMyUserPreferencesDefault($type); |
||
449 | } elseif ($this->$dbVariableName && array_key_exists($this->$dbVariableName, $options)) { |
||
450 | $defaultOption = $this->$dbVariableName; |
||
451 | } |
||
452 | if (!$defaultOption) { |
||
453 | if (isset($options['default'])) { |
||
454 | $defaultOption = 'default'; |
||
455 | } else { |
||
456 | user_error("It is recommended that you have a default (key) option for $type", E_USER_NOTICE); |
||
457 | $keys = array_keys($options); |
||
458 | $defaultOption = $keys[0]; |
||
459 | } |
||
460 | } |
||
461 | $this->myUserPreferencesDefaultCache[$type] = $defaultOption; |
||
462 | } |
||
463 | |||
464 | return $this->myUserPreferencesDefaultCache[$type]; |
||
465 | } |
||
466 | |||
467 | /********************* |
||
468 | * SETTINGS: Dropdowns |
||
469 | *********************/ |
||
470 | /** |
||
471 | * SORT: |
||
472 | * returns an array of Key => Title for sort options. |
||
473 | * |
||
474 | * FILTER: |
||
475 | * Returns options for the dropdown of filter options. |
||
476 | * |
||
477 | * DISPLAY: |
||
478 | * Returns the options for product display styles. |
||
479 | * In the configuration you can set which ones are available. |
||
480 | * If one is available then you must make sure that the corresponding template is available. |
||
481 | * For example, if the display style is |
||
482 | * MyTemplate => "All Details" |
||
483 | * Then you must make sure MyTemplate.ss exists. |
||
484 | * |
||
485 | * @param string $type - FILTER | SORT | DISPLAY |
||
486 | * |
||
487 | * @return array |
||
488 | */ |
||
489 | protected function getUserPreferencesOptionsForDropdown($type) |
||
490 | { |
||
491 | $options = $this->getConfigOptions($type); |
||
492 | $inheritTitle = _t('ProductGroup.INHERIT', 'Inherit'); |
||
493 | $array = array('inherit' => $inheritTitle); |
||
494 | if (is_array($options) && count($options)) { |
||
495 | foreach ($options as $key => $option) { |
||
496 | if (is_array($option)) { |
||
497 | $array[$key] = $option['Title']; |
||
498 | } else { |
||
499 | $array[$key] = $option; |
||
500 | } |
||
501 | } |
||
502 | } |
||
503 | |||
504 | return $array; |
||
505 | } |
||
506 | |||
507 | /********************* |
||
508 | * SETTINGS: SQL |
||
509 | *********************/ |
||
510 | |||
511 | /** |
||
512 | * SORT: |
||
513 | * Returns the sort sql for a particular sorting key. |
||
514 | * If no key is provided then the default key will be returned. |
||
515 | * |
||
516 | * @param string $key |
||
517 | * |
||
518 | * @return array (e.g. Array(MyField => "ASC", "MyOtherField" => "DESC") |
||
519 | * |
||
520 | * FILTER: |
||
521 | * Returns the sql associated with a filter option. |
||
522 | * |
||
523 | * @param string $type - FILTER | SORT | DISPLAY |
||
524 | * @param string $key - the options selected |
||
525 | * |
||
526 | * @return array | String (e.g. array("MyField" => 1, "MyOtherField" => 0)) OR STRING |
||
527 | */ |
||
528 | protected function getUserSettingsOptionSQL($type, $key = '') |
||
529 | { |
||
530 | $options = $this->getConfigOptions($type); |
||
531 | //if we cant find the current one, use the default |
||
532 | if (!$key || (!isset($options[$key]))) { |
||
533 | $key = $this->getMyUserPreferencesDefault($type); |
||
534 | } |
||
535 | if ($key) { |
||
536 | return $options[$key]['SQL']; |
||
537 | } else { |
||
538 | if ($type == 'FILTER') { |
||
539 | return array('Sort' => 'ASC'); |
||
540 | } elseif ($type == 'SORT') { |
||
541 | return array('ShowInSearch' => 1); |
||
542 | } |
||
543 | } |
||
544 | } |
||
545 | |||
546 | /********************* |
||
547 | * SETTINGS: Title |
||
548 | *********************/ |
||
549 | |||
550 | /** |
||
551 | * Returns the Title for a type key. |
||
552 | * If no key is provided then the default key is used. |
||
553 | * |
||
554 | * @param string $type - FILTER | SORT | DISPLAY |
||
555 | * @param string $key |
||
556 | * |
||
557 | * @return string |
||
558 | */ |
||
559 | public function getUserPreferencesTitle($type, $key = '') |
||
560 | { |
||
561 | $options = $this->getConfigOptions($type); |
||
562 | if (!$key || (!isset($options[$key]))) { |
||
563 | $key = $this->getMyUserPreferencesDefault($type); |
||
564 | } |
||
565 | if ($key && isset($options[$key]['Title'])) { |
||
566 | return $options[$key]['Title']; |
||
567 | } else { |
||
568 | return _t('ProductGroup.UNKNOWN', 'UNKNOWN USER SETTING'); |
||
569 | } |
||
570 | } |
||
571 | |||
572 | /********************* |
||
573 | * SETTINGS: products per page |
||
574 | *********************/ |
||
575 | |||
576 | /** |
||
577 | * @return int |
||
578 | **/ |
||
579 | public function ProductsPerPage() |
||
580 | { |
||
581 | return $this->MyNumberOfProductsPerPage(); |
||
582 | } |
||
583 | |||
584 | private $_numberOfProductsPerPage = null; |
||
585 | |||
586 | /** |
||
587 | * @return int |
||
588 | **/ |
||
589 | public function MyNumberOfProductsPerPage() |
||
590 | { |
||
591 | if($this->_numberOfProductsPerPage === null) { |
||
592 | $productsPagePage = 0; |
||
593 | if ($this->NumberOfProductsPerPage) { |
||
594 | $productsPagePage = $this->NumberOfProductsPerPage; |
||
595 | } else { |
||
596 | if ($parent = $this->ParentGroup()) { |
||
597 | $productsPagePage = $parent->MyNumberOfProductsPerPage(); |
||
598 | } else { |
||
599 | $productsPagePage = $this->EcomConfig()->NumberOfProductsPerPage; |
||
600 | } |
||
601 | } |
||
602 | $this->_numberOfProductsPerPage = $productsPagePage; |
||
603 | } |
||
604 | return $this->_numberOfProductsPerPage; |
||
605 | } |
||
606 | |||
607 | /********************* |
||
608 | * SETTINGS: level of products to show |
||
609 | *********************/ |
||
610 | |||
611 | /** |
||
612 | * returns the number of product groups (children) |
||
613 | * to show in the current product list |
||
614 | * based on the user setting for this page. |
||
615 | * |
||
616 | * @return int |
||
617 | */ |
||
618 | public function MyLevelOfProductsToShow() |
||
619 | { |
||
620 | if ($this->LevelOfProductsToShow == 0) { |
||
621 | if ($parent = $this->ParentGroup()) { |
||
622 | $this->LevelOfProductsToShow = $parent->MyLevelOfProductsToShow(); |
||
623 | } |
||
624 | } |
||
625 | //reset to default |
||
626 | if ($this->LevelOfProductsToShow == 0) { |
||
627 | $defaults = Config::inst()->get('ProductGroup', 'defaults'); |
||
628 | |||
629 | return isset($defaults['LevelOfProductsToShow']) ? $defaults['LevelOfProductsToShow'] : 99; |
||
630 | } |
||
631 | |||
632 | return $this->LevelOfProductsToShow; |
||
633 | } |
||
634 | |||
635 | /********************* |
||
636 | * CMS Fields |
||
637 | *********************/ |
||
638 | |||
639 | /** |
||
640 | * standard SS method. |
||
641 | * |
||
642 | * @return FieldList |
||
643 | */ |
||
644 | public function getCMSFields() |
||
645 | { |
||
646 | $fields = parent::getCMSFields(); |
||
647 | //dirty hack to show images! |
||
648 | $fields->addFieldToTab('Root.Images', Product_ProductImageUploadField::create('Image', _t('Product.IMAGE', 'Product Group Image'))); |
||
649 | //number of products |
||
650 | $calculatedNumberOfProductsPerPage = $this->MyNumberOfProductsPerPage(); |
||
651 | $numberOfProductsPerPageExplanation = $calculatedNumberOfProductsPerPage != $this->NumberOfProductsPerPage ? _t('ProductGroup.CURRENTLVALUE', 'Current value: ').$calculatedNumberOfProductsPerPage.' '._t('ProductGroup.INHERITEDFROMPARENTSPAGE', ' (inherited from parent page because the current page is set to zero)') : ''; |
||
652 | $fields->addFieldToTab( |
||
653 | 'Root', |
||
654 | Tab::create( |
||
655 | 'ProductDisplay', |
||
656 | _t('ProductGroup.DISPLAY', 'Display'), |
||
657 | $productsToShowField = DropdownField::create('LevelOfProductsToShow', _t('ProductGroup.PRODUCTSTOSHOW', 'Products to show'), $this->showProductLevels), |
||
658 | HeaderField::create('WhatProductsAreShown', _t('ProductGroup.WHATPRODUCTSSHOWN', _t('ProductGroup.OPTIONSSELECTEDBELOWAPPLYTOCHILDGROUPS', 'Inherited options'))), |
||
659 | $numberOfProductsPerPageField = NumericField::create('NumberOfProductsPerPage', _t('ProductGroup.PRODUCTSPERPAGE', 'Number of products per page')) |
||
660 | ) |
||
661 | ); |
||
662 | $numberOfProductsPerPageField->setRightTitle($numberOfProductsPerPageExplanation); |
||
663 | if ($calculatedNumberOfProductsPerPage && !$this->NumberOfProductsPerPage) { |
||
664 | $this->NumberOfProductsPerPage = null; |
||
665 | $numberOfProductsPerPageField->setAttribute('placeholder', $calculatedNumberOfProductsPerPage); |
||
666 | } |
||
667 | //sort |
||
668 | $sortDropdownList = $this->getUserPreferencesOptionsForDropdown('SORT'); |
||
669 | if (count($sortDropdownList) > 1) { |
||
670 | $sortOrderKey = $this->getMyUserPreferencesDefault('SORT'); |
||
671 | if ($this->DefaultSortOrder == 'inherit') { |
||
672 | $actualValue = ' ('.(isset($sortDropdownList[$sortOrderKey]) ? $sortDropdownList[$sortOrderKey] : _t('ProductGroup.ERROR', 'ERROR')).')'; |
||
673 | $sortDropdownList['inherit'] = _t('ProductGroup.INHERIT', 'Inherit').$actualValue; |
||
674 | } |
||
675 | $fields->addFieldToTab( |
||
676 | 'Root.ProductDisplay', |
||
677 | $defaultSortOrderField = DropdownField::create('DefaultSortOrder', _t('ProductGroup.DEFAULTSORTORDER', 'Default Sort Order'), $sortDropdownList) |
||
678 | ); |
||
679 | $defaultSortOrderField->setRightTitle(_t('ProductGroup.INHERIT_RIGHT_TITLE', "Inherit means that the parent page value is used - and if there is no relevant parent page then the site's default value is used.")); |
||
680 | } |
||
681 | //filter |
||
682 | $filterDropdownList = $this->getUserPreferencesOptionsForDropdown('FILTER'); |
||
683 | if (count($filterDropdownList) > 1) { |
||
684 | $filterKey = $this->getMyUserPreferencesDefault('FILTER'); |
||
685 | if ($this->DefaultFilter == 'inherit') { |
||
686 | $actualValue = ' ('.(isset($filterDropdownList[$filterKey]) ? $filterDropdownList[$filterKey] : _t('ProductGroup.ERROR', 'ERROR')).')'; |
||
687 | $filterDropdownList['inherit'] = _t('ProductGroup.INHERIT', 'Inherit').$actualValue; |
||
688 | } |
||
689 | $fields->addFieldToTab( |
||
690 | 'Root.ProductDisplay', |
||
691 | $defaultFilterField = DropdownField::create('DefaultFilter', _t('ProductGroup.DEFAULTFILTER', 'Default Filter'), $filterDropdownList) |
||
692 | ); |
||
693 | $defaultFilterField->setRightTitle(_t('ProductGroup.INHERIT_RIGHT_TITLE', "Inherit means that the parent page value is used - and if there is no relevant parent page then the site's default value is used.")); |
||
694 | } |
||
695 | //display style |
||
696 | $displayStyleDropdownList = $this->getUserPreferencesOptionsForDropdown('DISPLAY'); |
||
697 | if (count($displayStyleDropdownList) > 2) { |
||
698 | $displayStyleKey = $this->getMyUserPreferencesDefault('DISPLAY'); |
||
699 | if ($this->DisplayStyle == 'inherit') { |
||
700 | $actualValue = ' ('.(isset($displayStyleDropdownList[$displayStyleKey]) ? $displayStyleDropdownList[$displayStyleKey] : _t('ProductGroup.ERROR', 'ERROR')).')'; |
||
701 | $displayStyleDropdownList['inherit'] = _t('ProductGroup.INHERIT', 'Inherit').$actualValue; |
||
702 | } |
||
703 | $fields->addFieldToTab( |
||
704 | 'Root.ProductDisplay', |
||
705 | DropdownField::create('DisplayStyle', _t('ProductGroup.DEFAULTDISPLAYSTYLE', 'Default Display Style'), $displayStyleDropdownList) |
||
706 | ); |
||
707 | } |
||
708 | if ($this->EcomConfig()->ProductsAlsoInOtherGroups) { |
||
709 | if (!$this instanceof ProductGroupSearchPage) { |
||
710 | $fields->addFieldsToTab( |
||
711 | 'Root.OtherProductsShown', |
||
712 | array( |
||
713 | HeaderField::create('ProductGroupsHeader', _t('ProductGroup.OTHERPRODUCTSTOSHOW', 'Other products to show ...')), |
||
714 | $this->getProductGroupsTable(), |
||
715 | ) |
||
716 | ); |
||
717 | } |
||
718 | } |
||
719 | |||
720 | return $fields; |
||
721 | } |
||
722 | |||
723 | /** |
||
724 | * used if you install lumberjack |
||
725 | * @return string |
||
726 | */ |
||
727 | public function getLumberjackTitle() |
||
728 | { |
||
729 | return _t('ProductGroup.BUYABLES', 'Products'); |
||
730 | } |
||
731 | |||
732 | // /** |
||
733 | // * used if you install lumberjack |
||
734 | // * @return string |
||
735 | // */ |
||
736 | // public function getLumberjackGridFieldConfig() |
||
737 | // { |
||
738 | // return GridFieldConfig_RelationEditor::create(); |
||
739 | // } |
||
740 | |||
741 | /** |
||
742 | * Used in getCSMFields. |
||
743 | * |
||
744 | * @return GridField |
||
745 | **/ |
||
746 | protected function getProductGroupsTable() |
||
747 | { |
||
748 | $gridField = GridField::create( |
||
749 | 'AlsoShowProducts', |
||
750 | _t('ProductGroup.OTHER_PRODUCTS_SHOWN_IN_THIS_GROUP', 'Other products shown in this group ...'), |
||
751 | $this->AlsoShowProducts(), |
||
752 | GridFieldBasicPageRelationConfig::create() |
||
753 | ); |
||
754 | //make sure edits are done in the right place ... |
||
755 | return $gridField; |
||
756 | } |
||
757 | |||
758 | /***************************************************** |
||
759 | * |
||
760 | * |
||
761 | * |
||
762 | * PRODUCTS THAT BELONG WITH THIS PRODUCT GROUP |
||
763 | * |
||
764 | * |
||
765 | * |
||
766 | *****************************************************/ |
||
767 | |||
768 | /** |
||
769 | * returns the inital (all) products, based on the all the eligible products |
||
770 | * for the page. |
||
771 | * |
||
772 | * This is THE pivotal method that probably changes for classes that |
||
773 | * extend ProductGroup as here you can determine what products or other buyables are shown. |
||
774 | * |
||
775 | * The return from this method will then be sorted to produce the final product list. |
||
776 | * |
||
777 | * There is no sort for the initial retrieval |
||
778 | * |
||
779 | * This method is public so that you can retrieve a list of products for a product group page. |
||
780 | * |
||
781 | * @param array | string $extraFilter Additional SQL filters to apply to the Product retrieval |
||
782 | * @param string $alternativeFilterKey Alternative standard filter to be used. |
||
783 | * |
||
784 | * @return DataList |
||
785 | **/ |
||
786 | public function currentInitialProducts($extraFilter = null, $alternativeFilterKey = '') |
||
787 | { |
||
788 | |||
789 | //INIT ALLPRODUCTS |
||
790 | unset($this->allProducts); |
||
791 | $className = $this->getBuyableClassName(); |
||
792 | $this->allProducts = $className::get(); |
||
793 | |||
794 | // GROUP FILTER (PRODUCTS FOR THIS GROUP) |
||
795 | $this->allProducts = $this->getGroupFilter(); |
||
796 | |||
797 | // STANDARD FILTER (INCLUDES USER PREFERENCE) |
||
798 | $filterStatement = $this->allowPurchaseWhereStatement(); |
||
799 | if ($filterStatement) { |
||
800 | if (is_array($filterStatement)) { |
||
801 | $this->allProducts = $this->allProducts->filter($filterStatement); |
||
802 | } elseif (is_string($filterStatement)) { |
||
803 | $this->allProducts = $this->allProducts->where($filterStatement); |
||
804 | } |
||
805 | } |
||
806 | $this->allProducts = $this->getStandardFilter($alternativeFilterKey); |
||
807 | |||
808 | // EXTRA FILTER (ON THE FLY FROM CONTROLLER) |
||
809 | if (is_array($extraFilter) && count($extraFilter)) { |
||
810 | $this->allProducts = $this->allProducts->filter($extraFilter); |
||
811 | } elseif (is_string($extraFilter) && strlen($extraFilter) > 2) { |
||
812 | $this->allProducts = $this->allProducts->where($extraFilter); |
||
813 | } |
||
814 | |||
815 | //JOINS |
||
816 | $this->allProducts = $this->getGroupJoin(); |
||
817 | |||
818 | return $this->allProducts; |
||
819 | } |
||
820 | |||
821 | /** |
||
822 | * this method can be used quickly current initial products |
||
823 | * whenever you write: |
||
824 | * ```php |
||
825 | * currentInitialProducts->(null, $key)->map("ID", "ID")->toArray(); |
||
826 | * ``` |
||
827 | * this is the better replacement. |
||
828 | * |
||
829 | * @param string $filterKey |
||
830 | * |
||
831 | * @return array |
||
832 | */ |
||
833 | public function currentInitialProductsAsCachedArray($filterKey) |
||
834 | { |
||
835 | $cacheKey = 'CurrentInitialProductsArray'.$filterKey; |
||
836 | if ($array = $this->retrieveObjectStore($cacheKey)) { |
||
837 | //do nothing |
||
838 | } else { |
||
839 | $array = $this->currentInitialProducts(null, $filterKey)->map('ID', 'ID')->toArray(); |
||
840 | $this->saveObjectStore($array, $cacheKey); |
||
841 | } |
||
842 | |||
843 | return $array; |
||
844 | } |
||
845 | |||
846 | /***************************************************** |
||
847 | * DATALIST: adjusters |
||
848 | * these are the methods you want to override in |
||
849 | * any clases that extend ProductGroup |
||
850 | *****************************************************/ |
||
851 | |||
852 | /** |
||
853 | * Do products occur in more than one group. |
||
854 | * |
||
855 | * @return bool |
||
856 | */ |
||
857 | protected function getProductsAlsoInOtherGroups() |
||
858 | { |
||
859 | return $this->EcomConfig()->ProductsAlsoInOtherGroups; |
||
860 | } |
||
861 | |||
862 | /** |
||
863 | * Returns the class we are working with. |
||
864 | * |
||
865 | * @return string |
||
866 | */ |
||
867 | protected function getBuyableClassName() |
||
868 | { |
||
869 | return EcommerceConfig::get('ProductGroup', 'base_buyable_class'); |
||
870 | } |
||
871 | |||
872 | /** |
||
873 | * @SEE: important notes at the top of this file / class |
||
874 | * |
||
875 | * IMPORTANT: Adjusts allProducts and returns it... |
||
876 | * |
||
877 | * @return DataList |
||
878 | */ |
||
879 | protected function getGroupFilter() |
||
880 | { |
||
881 | $levelToShow = $this->MyLevelOfProductsToShow(); |
||
882 | $cacheKey = 'GroupFilter_'.abs(intval($levelToShow + 999)); |
||
883 | if ($groupFilter = $this->retrieveObjectStore($cacheKey)) { |
||
884 | $this->allProducts = $this->allProducts->where($groupFilter); |
||
885 | } else { |
||
886 | $groupFilter = ''; |
||
887 | $productFilterArray = array(); |
||
888 | //special cases |
||
889 | if ($levelToShow < 0) { |
||
890 | //no produts but if LevelOfProductsToShow = -1 then show all |
||
891 | $groupFilter = ' ('.$levelToShow.' = -1) '; |
||
892 | } elseif ($levelToShow > 0) { |
||
893 | $groupIDs = array($this->ID => $this->ID); |
||
894 | $productFilterTemp = $this->getProductsToBeIncludedFromOtherGroups(); |
||
895 | $productFilterArray[$productFilterTemp] = $productFilterTemp; |
||
896 | $childGroups = $this->ChildGroups($levelToShow); |
||
897 | if ($childGroups && $childGroups->count()) { |
||
898 | foreach ($childGroups as $childGroup) { |
||
899 | $groupIDs[$childGroup->ID] = $childGroup->ID; |
||
900 | $productFilterTemp = $childGroup->getProductsToBeIncludedFromOtherGroups(); |
||
901 | $productFilterArray[$productFilterTemp] = $productFilterTemp; |
||
902 | } |
||
903 | } |
||
904 | $groupFilter = ' ( "ParentID" IN ('.implode(',', $groupIDs).') ) '.implode($productFilterArray).' '; |
||
905 | } else { |
||
906 | //fall-back |
||
907 | $groupFilter = '"ParentID" < 0'; |
||
908 | } |
||
909 | $this->allProducts = $this->allProducts->where($groupFilter); |
||
910 | $this->saveObjectStore($groupFilter, $cacheKey); |
||
911 | } |
||
912 | |||
913 | return $this->allProducts; |
||
914 | } |
||
915 | |||
916 | /** |
||
917 | * If products are show in more than one group |
||
918 | * Then this returns a where phrase for any products that are linked to this |
||
919 | * product group. |
||
920 | * |
||
921 | * @return string |
||
922 | */ |
||
923 | protected function getProductsToBeIncludedFromOtherGroups() |
||
924 | { |
||
925 | //TO DO: this should actually return |
||
926 | //Product.ID = IN ARRAY(bla bla) |
||
927 | $array = array(); |
||
928 | if ($this->getProductsAlsoInOtherGroups()) { |
||
929 | $array = $this->AlsoShowProducts()->map('ID', 'ID')->toArray(); |
||
930 | } |
||
931 | if (count($array)) { |
||
932 | return " OR (\"Product\".\"ID\" IN (".implode(',', $array).')) '; |
||
933 | } |
||
934 | |||
935 | return ''; |
||
936 | } |
||
937 | |||
938 | /** |
||
939 | * @SEE: important notes at the top of this class / file for more information! |
||
940 | * |
||
941 | * IMPORTANT: Adjusts allProducts and returns it... |
||
942 | * |
||
943 | * @param string $alternativeFilterKey - filter key to be used... if none is specified then we use the current one. |
||
944 | * |
||
945 | * @return DataList |
||
946 | */ |
||
947 | protected function getStandardFilter($alternativeFilterKey = '') |
||
948 | { |
||
949 | if ($alternativeFilterKey) { |
||
950 | $filterKey = $alternativeFilterKey; |
||
951 | } else { |
||
952 | $filterKey = $this->getCurrentUserPreferences('FILTER'); |
||
953 | } |
||
954 | $filter = $this->getUserSettingsOptionSQL('FILTER', $filterKey); |
||
955 | if (is_array($filter)) { |
||
956 | $this->allProducts = $this->allProducts->Filter($filter); |
||
957 | } elseif (is_string($filter) && strlen($filter) > 2) { |
||
958 | $this->allProducts = $this->allProducts->Where($filter); |
||
959 | } |
||
960 | |||
961 | return $this->allProducts; |
||
962 | } |
||
963 | |||
964 | /** |
||
965 | * Join statement for the product groups. |
||
966 | * |
||
967 | * IMPORTANT: Adjusts allProducts and returns it... |
||
968 | * |
||
969 | * @return DataList |
||
970 | */ |
||
971 | protected function getGroupJoin() |
||
972 | { |
||
973 | return $this->allProducts; |
||
974 | } |
||
975 | |||
976 | /** |
||
977 | * Quick - dirty hack - filter to |
||
978 | * only show relevant products. |
||
979 | * |
||
980 | * @param bool $asArray |
||
981 | * @param string $table |
||
982 | */ |
||
983 | protected function allowPurchaseWhereStatement($asArray = true, $table = 'Product') |
||
984 | { |
||
985 | if ($this->EcomConfig()->OnlyShowProductsThatCanBePurchased) { |
||
986 | if ($asArray) { |
||
987 | $allowPurchaseWhereStatement = array('AllowPurchase' => 1); |
||
988 | } else { |
||
989 | $allowPurchaseWhereStatement = "\"$table\".\"AllowPurchase\" = 1 "; |
||
990 | } |
||
991 | |||
992 | return $allowPurchaseWhereStatement; |
||
993 | } |
||
994 | } |
||
995 | |||
996 | /***************************************************** |
||
997 | * |
||
998 | * |
||
999 | * |
||
1000 | * |
||
1001 | * FINAL PRODUCTS |
||
1002 | * |
||
1003 | * |
||
1004 | * |
||
1005 | * |
||
1006 | *****************************************************/ |
||
1007 | |||
1008 | /** |
||
1009 | * This is the dataList that contains all the products. |
||
1010 | * |
||
1011 | * @var DataList |
||
1012 | */ |
||
1013 | protected $allProducts = null; |
||
1014 | |||
1015 | /** |
||
1016 | * a list of relevant buyables that can |
||
1017 | * not be purchased and therefore should be excluded. |
||
1018 | * Should be set to NULL to start with so we know if it has been |
||
1019 | * set yet. |
||
1020 | * |
||
1021 | * @var null | Array (like so: array(1,2,4,5,99)) |
||
1022 | */ |
||
1023 | private $canNOTbePurchasedArray = null; |
||
1024 | |||
1025 | /** |
||
1026 | * a list of relevant buyables that can |
||
1027 | * be purchased. We keep this so that |
||
1028 | * that we can save to session, etc... for future use. |
||
1029 | * Should be set to NULL to start with so we know if it has been |
||
1030 | * set yet. |
||
1031 | * |
||
1032 | * @var null | Array (like so: array(1,2,4,5,99)) |
||
1033 | */ |
||
1034 | protected $canBePurchasedArray = null; |
||
1035 | |||
1036 | /** |
||
1037 | * returns the total numer of products |
||
1038 | * (before pagination AND before MAX is applie). |
||
1039 | * |
||
1040 | * @return int |
||
1041 | **/ |
||
1042 | public function RawCount() |
||
1043 | { |
||
1044 | return $this->rawCount ? $this->rawCount : 0; |
||
1045 | } |
||
1046 | |||
1047 | /** |
||
1048 | * returns the total numer of products |
||
1049 | * (before pagination but after MAX is applied). |
||
1050 | * |
||
1051 | * @return int |
||
1052 | **/ |
||
1053 | public function TotalCount() |
||
1054 | { |
||
1055 | return $this->totalCount ? $this->totalCount : 0; |
||
1056 | } |
||
1057 | |||
1058 | /** |
||
1059 | * this is used to save a list of sorted products |
||
1060 | * so that you can find a previous and a next button, etc... |
||
1061 | * |
||
1062 | * @return array |
||
1063 | */ |
||
1064 | public function getProductsThatCanBePurchasedArray() |
||
1065 | { |
||
1066 | return $this->canBePurchasedArray; |
||
1067 | } |
||
1068 | |||
1069 | /** |
||
1070 | * Retrieve a set of products, based on the given parameters. |
||
1071 | * This method is usually called by the various controller methods. |
||
1072 | * The extraFilter helps you to select different products, |
||
1073 | * depending on the method used in the controller. |
||
1074 | * |
||
1075 | * Furthermore, extrafilter can take all sorts of variables. |
||
1076 | * This is basically setup like this so that in ProductGroup extensions you |
||
1077 | * can setup all sorts of filters, while still using the ProductsShowable method. |
||
1078 | * |
||
1079 | * The extra filter can be supplied as array (e.g. array("ID" => 12) or array("ID" => array(12,13,45))) |
||
1080 | * or as string. Arrays are used like this $productDataList->filter($array) and |
||
1081 | * strings are used with the where commands $productDataList->where($string). |
||
1082 | * |
||
1083 | * @param array | string $extraFilter Additional SQL filters to apply to the Product retrieval |
||
1084 | * @param array | string $alternativeSort Additional SQL for sorting |
||
1085 | * @param string $alternativeFilterKey alternative filter key to be used |
||
1086 | * |
||
1087 | * @return DataList | Null |
||
1088 | */ |
||
1089 | public function ProductsShowable($extraFilter = null, $alternativeSort = null, $alternativeFilterKey = '') |
||
1090 | { |
||
1091 | |||
1092 | //get original products without sort |
||
1093 | $this->allProducts = $this->currentInitialProducts($extraFilter, $alternativeFilterKey); |
||
1094 | |||
1095 | //sort products |
||
1096 | $this->allProducts = $this->currentFinalProducts($alternativeSort); |
||
1097 | |||
1098 | return $this->allProducts; |
||
1099 | } |
||
1100 | |||
1101 | /** |
||
1102 | * returns the final products, based on the all the eligile products |
||
1103 | * for the page. |
||
1104 | * |
||
1105 | * In the process we also save a list of included products |
||
1106 | * and we sort them. We also keep a record of the total count. |
||
1107 | * |
||
1108 | * All of the 'current' methods are to support the currentFinalProducts Method. |
||
1109 | * |
||
1110 | * @TODO: cache data for faster access. |
||
1111 | * |
||
1112 | * @param array | string $alternativeSort = Alternative Sort String or array |
||
1113 | * |
||
1114 | * @return DataList |
||
1115 | **/ |
||
1116 | protected function currentFinalProducts($alternativeSort = null) |
||
1117 | { |
||
1118 | if ($this->allProducts) { |
||
1119 | |||
1120 | //limit to maximum number of products for speed's sake |
||
1121 | $this->allProducts = $this->sortCurrentFinalProducts($alternativeSort); |
||
1122 | $this->allProducts = $this->limitCurrentFinalProducts(); |
||
1123 | $this->allProducts = $this->removeExcludedProductsAndSaveIncludedProducts($this->allProducts); |
||
1124 | |||
1125 | return $this->allProducts; |
||
1126 | } |
||
1127 | } |
||
1128 | |||
1129 | /** |
||
1130 | * returns the SORT part of the final selection of products. |
||
1131 | * |
||
1132 | * @return DataList (allProducts) |
||
1133 | */ |
||
1134 | protected function sortCurrentFinalProducts($alternativeSort) |
||
1135 | { |
||
1136 | if ($alternativeSort) { |
||
1137 | if ($this->IsIDarray($alternativeSort)) { |
||
1138 | $sort = $this->createSortStatementFromIDArray($alternativeSort); |
||
1139 | } else { |
||
1140 | $sort = $alternativeSort; |
||
1141 | } |
||
1142 | } else { |
||
1143 | $sort = $this->currentSortSQL(); |
||
1144 | } |
||
1145 | $this->allProducts = $this->allProducts->Sort($sort); |
||
1146 | |||
1147 | return $this->allProducts; |
||
1148 | } |
||
1149 | |||
1150 | /** |
||
1151 | * is the variable provided is an array |
||
1152 | * that can be used as a list of IDs? |
||
1153 | * |
||
1154 | * @param mixed |
||
1155 | * |
||
1156 | * @return bool |
||
1157 | */ |
||
1158 | protected function IsIDarray($variable) |
||
1159 | { |
||
1160 | return $variable && is_array($variable) && count($variable) && intval(current($variable)) == current($variable); |
||
1161 | } |
||
1162 | |||
1163 | /** |
||
1164 | * returns the SORT part of the final selection of products. |
||
1165 | * |
||
1166 | * @return string | Array |
||
1167 | */ |
||
1168 | protected function currentSortSQL() |
||
1169 | { |
||
1170 | $sortKey = $this->getCurrentUserPreferences('SORT'); |
||
1171 | |||
1172 | return $this->getUserSettingsOptionSQL('SORT', $sortKey); |
||
1173 | } |
||
1174 | |||
1175 | /** |
||
1176 | * creates a sort string from a list of ID arrays... |
||
1177 | * |
||
1178 | * @param array $IDarray - list of product IDs |
||
1179 | * |
||
1180 | * @return string |
||
1181 | */ |
||
1182 | protected function createSortStatementFromIDArray($IDarray, $table = 'Product') |
||
1183 | { |
||
1184 | $ifStatement = 'CASE '; |
||
1185 | $sortStatement = ''; |
||
1186 | $stage = $this->getStage(); |
||
1187 | $count = 0; |
||
1188 | foreach ($IDarray as $productID) { |
||
1189 | $ifStatement .= ' WHEN "'.$table.$stage."\".\"ID\" = $productID THEN $count"; |
||
1190 | ++$count; |
||
1191 | } |
||
1192 | $sortStatement = $ifStatement.' END'; |
||
1193 | |||
1194 | return $sortStatement; |
||
1195 | } |
||
1196 | |||
1197 | /** |
||
1198 | * limits the products to a maximum number (for speed's sake). |
||
1199 | * |
||
1200 | * @return DataList (this->allProducts adjusted!) |
||
1201 | */ |
||
1202 | protected function limitCurrentFinalProducts() |
||
1203 | { |
||
1204 | $this->rawCount = $this->allProducts->count(); |
||
1205 | $max = EcommerceConfig::get('ProductGroup', 'maximum_number_of_products_to_list'); |
||
1206 | if ($this->rawCount > $max) { |
||
1207 | $this->allProducts = $this->allProducts->limit($max); |
||
1208 | $this->totalCount = $max; |
||
1209 | } else { |
||
1210 | $this->totalCount = $this->rawCount; |
||
1211 | } |
||
1212 | |||
1213 | return $this->allProducts; |
||
1214 | } |
||
1215 | |||
1216 | /** |
||
1217 | * Excluded products that can not be purchased |
||
1218 | * We all make a record of all the products that are in the current list |
||
1219 | * For efficiency sake, we do both these things at the same time. |
||
1220 | * IMPORTANT: Adjusts allProducts and returns it... |
||
1221 | * |
||
1222 | * @todo: cache data per user .... |
||
1223 | * |
||
1224 | * @return DataList |
||
1225 | */ |
||
1226 | protected function removeExcludedProductsAndSaveIncludedProducts() |
||
1227 | { |
||
1228 | if (is_array($this->canBePurchasedArray) && is_array($this->canNOTbePurchasedArray)) { |
||
1229 | //already done! |
||
1230 | } else { |
||
1231 | $this->canNOTbePurchasedArray = array(); |
||
1232 | $this->canBePurchasedArray = array(); |
||
1233 | if ($this->config()->get('actively_check_for_can_purchase')) { |
||
1234 | foreach ($this->allProducts as $buyable) { |
||
1235 | if ($buyable->canPurchase()) { |
||
1236 | $this->canBePurchasedArray[$buyable->ID] = $buyable->ID; |
||
1237 | } else { |
||
1238 | $this->canNOTbePurchasedArray[$buyable->ID] = $buyable->ID; |
||
1239 | } |
||
1240 | } |
||
1241 | } else { |
||
1242 | if ($this->rawCount > 0) { |
||
1243 | $this->canBePurchasedArray = $this->allProducts->map('ID', 'ID')->toArray(); |
||
1244 | } else { |
||
1245 | $this->canBePurchasedArray = array(); |
||
1246 | } |
||
1247 | } |
||
1248 | if (count($this->canNOTbePurchasedArray)) { |
||
1249 | $this->allProducts = $this->allProducts->Exclude(array('ID' => $this->canNOTbePurchasedArray)); |
||
1250 | } |
||
1251 | } |
||
1252 | |||
1253 | return $this->allProducts; |
||
1254 | } |
||
1255 | |||
1256 | /***************************************************** |
||
1257 | * Children and Parents |
||
1258 | *****************************************************/ |
||
1259 | |||
1260 | /** |
||
1261 | * Returns children ProductGroup pages of this group. |
||
1262 | * |
||
1263 | * @param int $maxRecursiveLevel - maximum depth , e.g. 1 = one level down - so no Child Groups are returned... |
||
1264 | * @param string | Array $filter - additional filter to be added |
||
1265 | * @param int $numberOfRecursions - current level of depth |
||
1266 | * |
||
1267 | * @return ArrayList (ProductGroups) |
||
1268 | */ |
||
1269 | public function ChildGroups($maxRecursiveLevel, $filter = null, $numberOfRecursions = 0) |
||
1270 | { |
||
1271 | $arrayList = ArrayList::create(); |
||
1272 | ++$numberOfRecursions; |
||
1273 | if ($numberOfRecursions < $maxRecursiveLevel) { |
||
1274 | if ($filter && is_string($filter)) { |
||
1275 | $filterWithAND = " AND $filter"; |
||
1276 | $where = "\"ParentID\" = '$this->ID' $filterWithAND"; |
||
1277 | $children = ProductGroup::get()->where($where); |
||
1278 | } elseif (is_array($filter) && count($filter)) { |
||
1279 | $filter = $filter + array('ParentID' => $this->ID); |
||
1280 | $children = ProductGroup::get()->filter($filter); |
||
1281 | } else { |
||
1282 | $children = ProductGroup::get()->filter(array('ParentID' => $this->ID)); |
||
1283 | } |
||
1284 | |||
1285 | if ($children->count()) { |
||
1286 | foreach ($children as $child) { |
||
1287 | $arrayList->push($child); |
||
1288 | $arrayList->merge($child->ChildGroups($maxRecursiveLevel, $filter, $numberOfRecursions)); |
||
1289 | } |
||
1290 | } |
||
1291 | } |
||
1292 | if (!$arrayList instanceof ArrayList) { |
||
1293 | user_error('We expect an array list as output'); |
||
1294 | } |
||
1295 | |||
1296 | return $arrayList; |
||
1297 | } |
||
1298 | |||
1299 | /** |
||
1300 | * Deprecated method. |
||
1301 | */ |
||
1302 | public function ChildGroupsBackup($maxRecursiveLevel, $filter = '') |
||
1303 | { |
||
1304 | Deprecation::notice('3.1', 'No longer in use'); |
||
1305 | if ($maxRecursiveLevel > 24) { |
||
1306 | $maxRecursiveLevel = 24; |
||
1307 | } |
||
1308 | |||
1309 | $stage = $this->getStage(); |
||
1310 | $select = 'P1.ID as ID1 '; |
||
1311 | $from = "ProductGroup$stage as P1 "; |
||
1312 | $join = " INNER JOIN SiteTree$stage AS S1 ON P1.ID = S1.ID"; |
||
1313 | $where = '1 = 1'; |
||
1314 | $ids = array(-1); |
||
1315 | for ($i = 1; $i < $maxRecursiveLevel; ++$i) { |
||
1316 | $j = $i + 1; |
||
1317 | $select .= ", P$j.ID AS ID$j, S$j.ParentID"; |
||
1318 | $join .= " |
||
1319 | LEFT JOIN ProductGroup$stage AS P$j ON P$j.ID = S$i.ParentID |
||
1320 | LEFT JOIN SiteTree$stage AS S$j ON P$j.ID = S$j.ID |
||
1321 | "; |
||
1322 | } |
||
1323 | $rows = DB::Query(' SELECT '.$select.' FROM '.$from.$join.' WHERE '.$where); |
||
1324 | if ($rows) { |
||
1325 | foreach ($rows as $row) { |
||
1326 | for ($i = 1; $i < $maxRecursiveLevel; ++$i) { |
||
1327 | if ($row['ID'.$i]) { |
||
1328 | $ids[$row['ID'.$i]] = $row['ID'.$i]; |
||
1329 | } |
||
1330 | } |
||
1331 | } |
||
1332 | } |
||
1333 | |||
1334 | return ProductGroup::get()->where("\"ProductGroup$stage\".\"ID\" IN (".implode(',', $ids).')'.$filterWithAND); |
||
1335 | } |
||
1336 | |||
1337 | /** |
||
1338 | * returns the parent page, but only if it is an instance of Product Group. |
||
1339 | * |
||
1340 | * @return DataObject | Null (ProductGroup) |
||
1341 | **/ |
||
1342 | public function ParentGroup() |
||
1343 | { |
||
1344 | if ($this->ParentID) { |
||
1345 | return ProductGroup::get()->byID($this->ParentID); |
||
1346 | } |
||
1347 | } |
||
1348 | |||
1349 | /***************************************************** |
||
1350 | * Other Stuff |
||
1351 | *****************************************************/ |
||
1352 | |||
1353 | /** |
||
1354 | * Recursively generate a product menu. |
||
1355 | * |
||
1356 | * @param string $filter |
||
1357 | * |
||
1358 | * @return ArrayList (ProductGroups) |
||
1359 | */ |
||
1360 | public function GroupsMenu($filter = 'ShowInMenus = 1') |
||
1361 | { |
||
1362 | if ($parent = $this->ParentGroup()) { |
||
1363 | return is_a($parent, Object::getCustomClass('ProductGroup')) ? $parent->GroupsMenu() : $this->ChildGroups($filter); |
||
1364 | } else { |
||
1365 | return $this->ChildGroups($filter); |
||
1366 | } |
||
1367 | } |
||
1368 | |||
1369 | /** |
||
1370 | * returns a "BestAvailable" image if the current one is not available |
||
1371 | * In some cases this is appropriate and in some cases this is not. |
||
1372 | * For example, consider the following setup |
||
1373 | * - product A with three variations |
||
1374 | * - Product A has an image, but the variations have no images |
||
1375 | * With this scenario, you want to show ONLY the product image |
||
1376 | * on the product page, but if one of the variations is added to the |
||
1377 | * cart, then you want to show the product image. |
||
1378 | * This can be achieved bu using the BestAvailable image. |
||
1379 | * |
||
1380 | * @return Image | Null |
||
1381 | */ |
||
1382 | public function BestAvailableImage() |
||
1383 | { |
||
1384 | $image = $this->Image(); |
||
1385 | if ($image && $image->exists() && file_exists($image->getFullPath())) { |
||
1386 | return $image; |
||
1387 | } elseif ($parent = $this->ParentGroup()) { |
||
1388 | return $parent->BestAvailableImage(); |
||
1389 | } |
||
1390 | } |
||
1391 | |||
1392 | /***************************************************** |
||
1393 | * Other related products |
||
1394 | *****************************************************/ |
||
1395 | |||
1396 | /** |
||
1397 | * returns a list of Product Groups that have the products for |
||
1398 | * the CURRENT product group listed as part of their AlsoShowProducts list. |
||
1399 | * |
||
1400 | * EXAMPLE: |
||
1401 | * You can use the AlsoShowProducts to list products by Brand. |
||
1402 | * In general, they are listed under type product groups (e.g. socks, sweaters, t-shirts), |
||
1403 | * and you create a list of separate ProductGroups (brands) that do not have ANY products as children, |
||
1404 | * but link to products using the AlsoShowProducts many_many relation. |
||
1405 | * |
||
1406 | * With the method below you can work out a list of brands that apply to the |
||
1407 | * current product group (e.g. socks come in three brands - namely A, B and C) |
||
1408 | * |
||
1409 | * @return DataList |
||
1410 | */ |
||
1411 | public function ProductGroupsFromAlsoShowProducts() |
||
1412 | { |
||
1413 | $parentIDs = array(); |
||
1414 | //we need to add the last array to make sure we have some products... |
||
1415 | $myProductsArray = $this->currentInitialProductsAsCachedArray($this->getMyUserPreferencesDefault('FILTER')); |
||
1416 | $rows = array(); |
||
1417 | if (count($myProductsArray)) { |
||
1418 | $rows = DB::query(' |
||
1419 | SELECT "ProductGroupID" |
||
1420 | FROM "Product_ProductGroups" |
||
1421 | WHERE "ProductID" IN ('.implode(',', $myProductsArray).') |
||
1422 | GROUP BY "ProductGroupID"; |
||
1423 | '); |
||
1424 | } |
||
1425 | foreach ($rows as $row) { |
||
1426 | $parentIDs[$row['ProductGroupID']] = $row['ProductGroupID']; |
||
1427 | } |
||
1428 | //just in case |
||
1429 | unset($parentIDs[$this->ID]); |
||
1430 | if (!count($parentIDs)) { |
||
1431 | $parentIDs = array(0 => 0); |
||
1432 | } |
||
1433 | |||
1434 | return ProductGroup::get()->filter(array('ID' => $parentIDs, 'ShowInSearch' => 1)); |
||
1435 | } |
||
1436 | |||
1437 | /** |
||
1438 | * This is the inverse of ProductGroupsFromAlsoShowProducts |
||
1439 | * That is, it list the product groups that a product is primarily listed under (exact parents only) |
||
1440 | * from a "AlsoShow" product List. |
||
1441 | * |
||
1442 | * @return DataList |
||
1443 | */ |
||
1444 | public function ProductGroupsFromAlsoShowProductsInverse() |
||
1445 | { |
||
1446 | $alsoShowProductsArray = $this->AlsoShowProducts() |
||
1447 | ->filter($this->getUserSettingsOptionSQL('FILTER', $this->getMyUserPreferencesDefault('FILTER'))) |
||
1448 | ->map('ID', 'ID')->toArray(); |
||
1449 | $alsoShowProductsArray[0] = 0; |
||
1450 | $parentIDs = Product::get()->filter(array('ID' => $alsoShowProductsArray))->map('ParentID', 'ParentID')->toArray(); |
||
1451 | //just in case |
||
1452 | unset($parentIDs[$this->ID]); |
||
1453 | if (! count($parentIDs)) { |
||
1454 | $parentIDs = array(0 => 0); |
||
1455 | } |
||
1456 | |||
1457 | return ProductGroup::get()->filter(array('ID' => $parentIDs, 'ShowInMenus' => 1)); |
||
1458 | } |
||
1459 | |||
1460 | /** |
||
1461 | * given the products for this page, |
||
1462 | * retrieve the parent groups excluding the current one. |
||
1463 | * |
||
1464 | * @return DataList |
||
1465 | */ |
||
1466 | public function ProductGroupsParentGroups() |
||
1467 | { |
||
1468 | $arrayOfIDs = $this->currentInitialProductsAsCachedArray($this->getMyUserPreferencesDefault('FILTER')) + array(0 => 0); |
||
1469 | $parentIDs = Product::get()->filter(array('ID' => $arrayOfIDs))->map('ParentID', 'ParentID')->toArray(); |
||
1470 | //just in case |
||
1471 | unset($parentIDs[$this->ID]); |
||
1472 | if (! count($parentIDs)) { |
||
1473 | $parentIDs = array(0 => 0); |
||
1474 | } |
||
1475 | |||
1476 | return ProductGroup::get()->filter(array('ID' => $parentIDs, 'ShowInSearch' => 1)); |
||
1477 | } |
||
1478 | |||
1479 | /** |
||
1480 | * returns stage as "" or "_Live". |
||
1481 | * |
||
1482 | * @return string |
||
1483 | */ |
||
1484 | protected function getStage() |
||
1485 | { |
||
1486 | $stage = ''; |
||
1487 | if (Versioned::current_stage() == 'Live') { |
||
1488 | $stage = '_Live'; |
||
1489 | } |
||
1490 | |||
1491 | return $stage; |
||
1492 | } |
||
1493 | |||
1494 | /***************************************************** |
||
1495 | * STANDARD SS METHODS |
||
1496 | *****************************************************/ |
||
1497 | |||
1498 | /** |
||
1499 | * tells us if the current page is part of e-commerce. |
||
1500 | * |
||
1501 | * @return bool |
||
1502 | */ |
||
1503 | public function IsEcommercePage() |
||
1504 | { |
||
1505 | return true; |
||
1506 | } |
||
1507 | |||
1508 | public function onAfterWrite() |
||
1509 | { |
||
1510 | parent::onAfterWrite(); |
||
1511 | |||
1512 | if ($this->ImageID) { |
||
1513 | if ($normalImage = Image::get()->exclude(array('ClassName' => 'Product_Image'))->byID($this->ImageID)) { |
||
1514 | $normalImage = $normalImage->newClassInstance('Product_Image'); |
||
1515 | $normalImage->write(); |
||
1516 | } |
||
1517 | } |
||
1518 | } |
||
1519 | |||
1520 | function requireDefaultRecords() |
||
1521 | { |
||
1522 | parent::requireDefaultRecords(); |
||
1523 | $urlSegments = ProductGroup::get()->column('URLSegment'); |
||
1524 | foreach($urlSegments as $urlSegment) { |
||
1525 | $counts = array_count_values($urlSegments); |
||
1526 | $hasDuplicates = $counts[$urlSegment] > 1 ? true : false; |
||
1527 | if($hasDuplicates) { |
||
1528 | DB::alteration_message('found duplicates for '.$urlSegment, 'deleted'); |
||
1529 | $checkForDuplicatesURLSegments = ProductGroup::get() |
||
1530 | ->filter(array('URLSegment' => $urlSegment)); |
||
1531 | if($checkForDuplicatesURLSegments->count()){ |
||
1532 | $count = 0; |
||
1533 | foreach($checkForDuplicatesURLSegments as $productGroup) { |
||
1534 | if($count > 0) { |
||
1535 | $oldURLSegment = $productGroup->URLSegment; |
||
1536 | DB::alteration_message(' ... Correcting URLSegment for '.$productGroup->Title.' with ID: '.$productGroup->ID, 'deleted'); |
||
1537 | $productGroup->writeToStage('Stage'); |
||
1538 | $productGroup->publish('Stage', 'Live'); |
||
1539 | $newURLSegment = $productGroup->URLSegment; |
||
1540 | DB::alteration_message(' ... .... from '.$oldURLSegment.' to '.$newURLSegment, 'created'); |
||
1541 | } |
||
1542 | $count++; |
||
1543 | } |
||
1544 | } |
||
1545 | } |
||
1546 | } |
||
1547 | } |
||
1548 | |||
1549 | /***************************************************** |
||
1550 | * CACHING |
||
1551 | *****************************************************/ |
||
1552 | /** |
||
1553 | * |
||
1554 | * @return bool |
||
1555 | */ |
||
1556 | public function AllowCaching() |
||
1557 | { |
||
1558 | return $this->allowCaching; |
||
1559 | } |
||
1560 | |||
1561 | /** |
||
1562 | * keeps a cache of the common caching key element |
||
1563 | * @var string |
||
1564 | */ |
||
1565 | private static $_product_group_cache_key_cache = null; |
||
1566 | |||
1567 | /** |
||
1568 | * |
||
1569 | * @param string $name |
||
1570 | * @param string $filterKey |
||
1571 | * |
||
1572 | * @return string |
||
1573 | */ |
||
1574 | public function cacheKey($cacheKey) |
||
1575 | { |
||
1576 | $cacheKey = $cacheKey.'_'.$this->ID; |
||
1577 | if (self::$_product_group_cache_key_cache === null) { |
||
1578 | self::$_product_group_cache_key_cache = "_PR_" |
||
1579 | .strtotime(Product::get()->max('LastEdited')). "_" |
||
1580 | .Product::get()->count(); |
||
1581 | self::$_product_group_cache_key_cache .= "PG_" |
||
1582 | .strtotime(ProductGroup::get()->max('LastEdited')). "_" |
||
1583 | .ProductGroup::get()->count(); |
||
1584 | if (class_exists('ProductVariation')) { |
||
1585 | self::$_product_group_cache_key_cache .= "PV_" |
||
1586 | .strtotime(ProductVariation::get()->max('LastEdited')). "_" |
||
1587 | .ProductVariation::get()->count(); |
||
1588 | } |
||
1589 | } |
||
1590 | $cacheKey .= self::$_product_group_cache_key_cache; |
||
1591 | |||
1592 | return $cacheKey; |
||
1593 | } |
||
1594 | |||
1595 | /** |
||
1596 | * @var Zend_Cache_Core |
||
1597 | */ |
||
1598 | protected $silverstripeCoreCache = null; |
||
1599 | |||
1600 | /** |
||
1601 | * Set the cache object to use when storing / retrieving partial cache blocks. |
||
1602 | * |
||
1603 | * @param Zend_Cache_Core $silverstripeCoreCache |
||
1604 | */ |
||
1605 | public function setSilverstripeCoreCache($silverstripeCoreCache) |
||
1606 | { |
||
1607 | $this->silverstripeCoreCache = $silverstripeCoreCache; |
||
1608 | } |
||
1609 | |||
1610 | /** |
||
1611 | * Get the cache object to use when storing / retrieving stuff in the Silverstripe Cache |
||
1612 | * |
||
1613 | * @return Zend_Cache_Core |
||
1614 | */ |
||
1615 | protected function getSilverstripeCoreCache() |
||
1616 | { |
||
1617 | return $this->silverstripeCoreCache ? $this->silverstripeCoreCache : SS_Cache::factory('EcomPG'); |
||
1618 | } |
||
1619 | |||
1620 | /** |
||
1621 | * saving an object to the. |
||
1622 | * |
||
1623 | * @param string $cacheKey |
||
1624 | * |
||
1625 | * @return mixed |
||
1626 | */ |
||
1627 | protected function retrieveObjectStore($cacheKey) |
||
1628 | { |
||
1629 | $cacheKey = $this->cacheKey($cacheKey); |
||
1630 | if ($this->AllowCaching()) { |
||
1631 | $cache = $this->getSilverstripeCoreCache(); |
||
1632 | $data = $cache->load($cacheKey); |
||
1633 | if (!$data) { |
||
1634 | return; |
||
1635 | } |
||
1636 | if (! $cache->getOption('automatic_serialization')) { |
||
1637 | $data = @unserialize($data); |
||
1638 | } |
||
1639 | return $data; |
||
1640 | } |
||
1641 | |||
1642 | return; |
||
1643 | } |
||
1644 | |||
1645 | /** |
||
1646 | * returns true when the data is saved... |
||
1647 | * |
||
1648 | * @param mixed $data |
||
1649 | * @param string $cacheKey - key under which the data is saved... |
||
1650 | * |
||
1651 | * @return bool |
||
1652 | */ |
||
1653 | protected function saveObjectStore($data, $cacheKey) |
||
1654 | { |
||
1655 | $cacheKey = $this->cacheKey($cacheKey); |
||
1656 | if ($this->AllowCaching()) { |
||
1657 | $cache = $this->getSilverstripeCoreCache(); |
||
1658 | if (! $cache->getOption('automatic_serialization')) { |
||
1659 | $data = serialize($data); |
||
1660 | } |
||
1661 | $cache->save($data, $cacheKey); |
||
1662 | return true; |
||
1663 | } |
||
1664 | |||
1665 | return false; |
||
1666 | } |
||
1667 | |||
1668 | public function SearchResultsSessionVariable($isForGroups = false) |
||
1669 | { |
||
1670 | $idString = '_'.$this->ID; |
||
1671 | if ($isForGroups) { |
||
1672 | return Config::inst()->get('ProductSearchForm', 'product_session_variable').$idString; |
||
1673 | } else { |
||
1674 | return Config::inst()->get('ProductSearchForm', 'product_group_session_variable').$idString; |
||
1675 | } |
||
1676 | } |
||
1677 | |||
1678 | /** |
||
1679 | * cache for result array. |
||
1680 | * |
||
1681 | * @var array |
||
1682 | */ |
||
1683 | private static $_result_array = array(); |
||
1684 | |||
1685 | /** |
||
1686 | * @return array |
||
1687 | */ |
||
1688 | public function searchResultsArrayFromSession() |
||
1689 | { |
||
1690 | if (! isset(self::$_result_array[$this->ID]) || self::$_result_array[$this->ID] === null) { |
||
1691 | self::$_result_array[$this->ID] = explode(',', Session::get($this->SearchResultsSessionVariable(false))); |
||
1692 | } |
||
1693 | if (! is_array(self::$_result_array[$this->ID]) || ! count(self::$_result_array[$this->ID])) { |
||
1694 | self::$_result_array[$this->ID] = array(0 => 0); |
||
1695 | } |
||
3001 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.