1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class SecondHandProductGroup extends ProductGroup |
|
|
|
|
5
|
|
|
{ |
6
|
|
|
private static $db = array( |
|
|
|
|
7
|
|
|
'RootParent' => 'Boolean' |
8
|
|
|
); |
9
|
|
|
|
10
|
|
|
private static $allowed_children = array( |
|
|
|
|
11
|
|
|
'SecondHandProductGroup', |
12
|
|
|
'SecondHandProduct' |
13
|
|
|
); |
14
|
|
|
|
15
|
|
|
private static $icon = 'ecommerce_second_hand_product/images/treeicons/SecondHandProductGroup'; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Standard SS variable. |
19
|
|
|
*/ |
20
|
|
|
private static $singular_name = 'Second Hand Product Holder'; |
|
|
|
|
21
|
|
|
public function i18n_singular_name() |
22
|
|
|
{ |
23
|
|
|
return self::$singular_name; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Standard SS variable. |
28
|
|
|
*/ |
29
|
|
|
private static $plural_name = 'Second Hand Product Holders'; |
|
|
|
|
30
|
|
|
public function i18n_plural_name() |
31
|
|
|
{ |
32
|
|
|
return self::$plural_name; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Standard SS variable. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private static $description = 'A product category page specifically for second had products'; |
|
|
|
|
41
|
|
|
|
42
|
|
|
public function getCMSFields() |
43
|
|
|
{ |
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
|
|
|
* @return SecondHandProductGroup |
96
|
|
|
*/ |
97
|
|
|
public function BestRootParentPage() |
98
|
|
|
{ |
99
|
|
|
$obj = DataObject::get_one( |
100
|
|
|
'SecondHandProductGroup', |
101
|
|
|
array('RootParent' => 1) |
102
|
|
|
); |
103
|
|
|
if ($obj) { |
104
|
|
|
return $obj; |
105
|
|
|
} else { |
106
|
|
|
return SecondHandProductGroup::get()->first(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns the class we are working with. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function getBuyableClassName() |
116
|
|
|
{ |
117
|
|
|
return 'SecondHandProduct'; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
class SecondHandProductGroup_Controller extends ProductGroup_Controller |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
private static $allowed_actions = array( |
|
|
|
|
124
|
|
|
'SearchSecondHandProducts', |
125
|
|
|
'search' |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
public function init() |
129
|
|
|
{ |
130
|
|
|
Config::inst()->update( |
131
|
|
|
'ProductGroup', |
132
|
|
|
'base_buyable_class', |
133
|
|
|
'SecondHandProduct' |
134
|
|
|
); |
135
|
|
|
parent::init(); |
136
|
|
|
$this->showFullList = true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function SearchSecondHandProducts() |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$fields = new FieldList( |
142
|
|
|
new TextField('searchterm', 'Keyword', isset($_GET['searchterm']) ? $_GET['searchterm'] : '') |
143
|
|
|
); |
144
|
|
|
$actions = new FieldList( |
145
|
|
|
new FormAction('doSearchSecondHandProducts', 'Search') |
146
|
|
|
); |
147
|
|
|
$validator = new RequiredFields('searchterm'); |
148
|
|
|
$form = Form::create($this, 'SearchSecondHandProducts', $fields, $actions, $validator); |
149
|
|
|
$form->setFormMethod('GET'); |
150
|
|
|
$form->disableSecurityToken(); |
151
|
|
|
|
152
|
|
|
return $form; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function doSearchSecondHandProducts($data, $form) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$page = SecondHandProductGroup::get()->first(); |
158
|
|
|
if ($page) { |
159
|
|
|
return $this->redirect($this->link('search').'?searchterm='.$data['searchterm']); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function search($request) |
164
|
|
|
{ |
165
|
|
|
$term = Convert::raw2sql($request->param('searchterm')); |
|
|
|
|
166
|
|
|
//uncompleted |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function HasSearchFilterAndSort() |
170
|
|
|
{ |
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
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.