1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class SecondHandProductGroup extends ProductGroup |
|
|
|
|
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
private static $db = array( |
|
|
|
|
8
|
|
|
'RootParent' => 'Boolean' |
9
|
|
|
); |
10
|
|
|
|
11
|
|
|
private static $allowed_children = array( |
|
|
|
|
12
|
|
|
'SecondHandProductGroup', |
13
|
|
|
'SecondHandProduct' |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
private static $icon = 'ecommerce_second_hand_product/images/treeicons/SecondHandProductGroup'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Standard SS variable. |
20
|
|
|
*/ |
21
|
|
|
private static $singular_name = 'Second Hand Product Holder'; |
|
|
|
|
22
|
|
|
public function i18n_singular_name() |
23
|
|
|
{ |
24
|
|
|
return self::$singular_name; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Standard SS variable. |
29
|
|
|
*/ |
30
|
|
|
private static $plural_name = 'Second Hand Product Holders'; |
|
|
|
|
31
|
|
|
public function i18n_plural_name() |
32
|
|
|
{ |
33
|
|
|
return self::$plural_name; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Standard SS variable. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private static $description = 'A product category page specifically for second had products'; |
|
|
|
|
42
|
|
|
|
43
|
|
|
public function getCMSFields() { |
44
|
|
|
$fields = parent::getCMSFields(); |
45
|
|
|
$fields->addFieldToTab( |
46
|
|
|
'Root.SecondHand', |
47
|
|
|
CheckboxField::create( |
48
|
|
|
'RootParent', |
49
|
|
|
_t('SecondHandProductGroup.LANDING_PAGE', 'Landing Page') |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
return $fields; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Event handler called before writing to the database. |
57
|
|
|
*/ |
58
|
|
|
public function onBeforeWrite() |
59
|
|
|
{ |
60
|
|
|
parent::onBeforeWrite(); |
61
|
|
|
if($this->ParentID) { |
62
|
|
|
$parent = SiteTree::get()->byID($this->ParentID); |
63
|
|
|
if($parent) { |
64
|
|
|
if($parent instanceof SecondHandProductGroup) { |
65
|
|
|
$this->RootParent = false; |
66
|
|
|
} else { |
67
|
|
|
if(! $this->hasOtherSecondHandProductGroupsOnThisLevel()) { |
68
|
|
|
$this->RootParent = true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} else { |
73
|
|
|
if( ! $this->hasOtherSecondHandProductGroupsOnThisLevel()) { |
74
|
|
|
$this->RootParent = true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Level is within SiteTree hierarchy |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
|
|
protected function hasOtherSecondHandProductGroupsOnThisLevel() |
84
|
|
|
{ |
85
|
|
|
if(!$this->ParentID) { |
86
|
|
|
$this->ParentID = 0; |
87
|
|
|
} |
88
|
|
|
return SecondHandProductGroup::get() |
89
|
|
|
->filter(array('ParentID' => $this->ParentID)) |
90
|
|
|
->exclude(array('ID' => $this->ID)) |
91
|
|
|
->count() > 0 ? true : false; |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return SecondHandProductGroup |
97
|
|
|
*/ |
98
|
|
|
function BestRootParentPage() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$obj = DataObject::get_one( |
101
|
|
|
'SecondHandProductGroup', |
102
|
|
|
array('RootParent' => 1) |
103
|
|
|
); |
104
|
|
|
if($obj) { |
105
|
|
|
return $obj; |
106
|
|
|
} else { |
107
|
|
|
return SecondHandProductGroup::get()->first(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns the class we are working with. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
protected function getBuyableClassName() |
117
|
|
|
{ |
118
|
|
|
return 'SecondHandProduct'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
class SecondHandProductGroup_Controller extends ProductGroup_Controller |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
private static $allowed_actions = array( |
|
|
|
|
126
|
|
|
'SearchSecondHandProducts', |
127
|
|
|
'search' |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
public function init() |
131
|
|
|
{ |
132
|
|
|
Config::inst()->update( |
133
|
|
|
'ProductGroup', |
134
|
|
|
'base_buyable_class', |
135
|
|
|
'SecondHandProduct' |
136
|
|
|
); |
137
|
|
|
parent::init(); |
138
|
|
|
$this->showFullList = true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function SearchSecondHandProducts() |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$fields = new FieldList( |
144
|
|
|
new TextField('searchterm', 'Keyword', isset($_GET['searchterm']) ? $_GET['searchterm'] : '') |
145
|
|
|
); |
146
|
|
|
$actions = new FieldList( |
147
|
|
|
new FormAction('doSearchSecondHandProducts', 'Search') |
148
|
|
|
); |
149
|
|
|
$validator = new RequiredFields('searchterm'); |
150
|
|
|
$form = Form::create($this, 'SearchSecondHandProducts', $fields, $actions, $validator); |
151
|
|
|
$form->setFormMethod('GET'); |
152
|
|
|
$form->disableSecurityToken(); |
153
|
|
|
|
154
|
|
|
return $form; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function doSearchSecondHandProducts($data, $form) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$page = SecondHandProductGroup::get()->first(); |
160
|
|
|
if ($page) { |
161
|
|
|
return $this->redirect($this->link('search').'?searchterm='.$data['searchterm']); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function search($request) |
166
|
|
|
{ |
167
|
|
|
$term = Convert::raw2sql($request->param('searchterm')); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function HasSearchFilterAndSort() |
171
|
|
|
{ |
172
|
|
|
return true; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
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.