FacetedCategory   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 22
c 7
b 1
f 3
lcom 1
cbo 8
dl 0
loc 137
ccs 0
cts 58
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 11 3
A getController() 0 4 2
A getFacetSpec() 0 5 3
A getFilters() 0 13 4
A FilteredProducts() 0 13 3
A getDisabledFacetsArray() 0 7 2
B Facets() 0 28 5
1
<?php
2
/**
3
 * This extension can be applied to ProductCategory
4
 * to allow categories to have facets as well.
5
 *
6
 * NOTE: You could apply this to either ProductCategory
7
 * or ProductCategory_Controller. I tend to use the model b/c
8
 * that will also cover some other cases like where you
9
 * might list subcategory products on the parent category page.
10
 * In such a case those products would be filtered as well.
11
 *
12
 * @author Mark Guinn <[email protected]>
13
 * @date 10.22.2013
14
 * @package shop_search
15
 * @subpackage helpers
16
 */
17
class FacetedCategory extends SiteTreeExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
{
19
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
        'DisabledFacets' => 'Text', // This will be a comma-delimited list of facets that aren't used for a given category
21
    );
22
23
    /** @var array - facet definition - see ShopSearch and/or docs/en/Facets.md for format */
24
    private static $facets = array();
0 ignored issues
show
Unused Code introduced by
The property $facets is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
25
26
    /** @var bool - if true there will be a tab in the cms to disable some or all defined facets */
27
    private static $show_disabled_facets_tab = true;
0 ignored issues
show
Unused Code introduced by
The property $show_disabled_facets_tab is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
29
    /** @var string - which method should we use to get the child products for FilteredProducts */
30
    private static $products_method = 'ProductsShowable';
0 ignored issues
show
Unused Code introduced by
The property $products_method is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
32
    /** @var bool - automatically create facets for static attributes */
33
    private static $auto_facet_attributes = false;
0 ignored issues
show
Unused Code introduced by
The property $auto_facet_attributes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
34
35
36
    /**
37
     * @param FieldList $fields
38
     */
39
    public function updateCMSFields(FieldList $fields)
40
    {
41
        if (Config::inst()->get('FacetedCategory', 'show_disabled_facets_tab')) {
42
            $spec = FacetHelper::inst()->expandFacetSpec($this->getFacetSpec());
43
            $facets = array();
44
            foreach ($spec as $f => $v) {
45
                $facets[$f] = $v['Label'];
46
            }
47
            $fields->addFieldToTab('Root.Facets', CheckboxSetField::create('DisabledFacets', "Don't show the following facets for this category:", $facets));
48
        }
49
    }
50
51
52
    /**
53
     * @return Controller
54
     */
55
    protected function getController()
56
    {
57
        return ($this->owner instanceof Controller) ? $this->owner : Controller::curr();
58
    }
59
60
61
    /**
62
     * @return array
63
     */
64
    protected function getFacetSpec()
65
    {
66
        $spec = Config::inst()->get('FacetedCategory', 'facets');
67
        return (empty($spec) || !is_array($spec)) ? array() : $spec;
68
    }
69
70
71
    /**
72
     * @return array
73
     */
74
    protected function getFilters()
75
    {
76
        $qs_f       = Config::inst()->get('ShopSearch', 'qs_filters');
77
        if (!$qs_f) {
78
            return array();
79
        }
80
        $request    = $this->getController()->getRequest();
81
        $filters    = $request->requestVar($qs_f);
82
        if (empty($filters) || !is_array($filters)) {
83
            return array();
84
        }
85
        return FacetHelper::inst()->scrubFilters($filters);
86
    }
87
88
89
    /**
90
     * @param bool $recursive
91
     * @return mixed
92
     */
93
    public function FilteredProducts($recursive=true)
94
    {
95
        if (!isset($this->_filteredProducts)) {
96
            $fn = Config::inst()->get('FacetedCategory', 'products_method');
97
            if (empty($fn)) {
98
                $fn = 'ProductsShowable';
99
            }
100
            $this->_filteredProducts = $this->owner->$fn($recursive);
101
            $this->_filteredProducts = FacetHelper::inst()->addFiltersToDataList($this->_filteredProducts, $this->getFilters());
102
        }
103
104
        return $this->_filteredProducts;
105
    }
106
107
    protected $_filteredProducts;
108
109
110
    /**
111
     * @return array
112
     */
113
    public function getDisabledFacetsArray()
114
    {
115
        if (empty($this->owner->DisabledFacets)) {
116
            return array();
117
        }
118
        return explode(',', $this->owner->DisabledFacets);
119
    }
120
121
122
    /**
123
     * @return ArrayList
124
     */
125
    public function Facets()
126
    {
127
        $spec       = $this->getFacetSpec();
128
        if (empty($spec)) {
129
            return new ArrayList;
130
        }
131
132
        // remove any disabled facets
133
        foreach ($this->getDisabledFacetsArray() as $disabled) {
134
            if (isset($spec[$disabled])) {
135
                unset($spec[$disabled]);
136
            }
137
        }
138
139
        $request    = $this->getController()->getRequest();
140
        $baseLink   = $request->getURL(false);
141
        $filters    = $this->getFilters();
142
        $baseParams = array_merge($request->requestVars(), array());
143
        unset($baseParams['url']);
144
145
        $products   = $this->owner->hasMethod('ProductsForFaceting') ? $this->owner->ProductsForFaceting() : $this->FilteredProducts();
146
        $facets     = FacetHelper::inst()->buildFacets($products, $spec, (bool)Config::inst()->get('FacetedCategory', 'auto_facet_attributes'));
147
        $facets     = FacetHelper::inst()->transformHierarchies($facets);
148
        $facets     = FacetHelper::inst()->updateFacetState($facets, $filters);
149
        $facets     = FacetHelper::inst()->insertFacetLinks($facets, $baseParams, $baseLink);
150
151
        return $facets;
152
    }
153
}
154