|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author nicolaas [at] sunny side up . co . nz |
|
5
|
|
|
* this extension of product is for software products (modules) |
|
6
|
|
|
* |
|
7
|
|
|
* |
|
8
|
|
|
**/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class ModuleProduct extends Product |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
private static $icon = "ecommerce_software/images/treeicons/ModuleProduct"; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
private static $api_access = array( |
|
|
|
|
|
|
16
|
|
|
'view' => array( |
|
17
|
|
|
"ModuleTitle", |
|
18
|
|
|
"Code", |
|
19
|
|
|
"MainURL", |
|
20
|
|
|
"ReadMeURL", |
|
21
|
|
|
"DemoURL", |
|
22
|
|
|
"SvnURL", |
|
23
|
|
|
"GitURL", |
|
24
|
|
|
"OtherURL", |
|
25
|
|
|
//"EcommerceProductTags", |
|
26
|
|
|
"Authors" |
|
27
|
|
|
) |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
private static $db = array( |
|
|
|
|
|
|
31
|
|
|
"Code" => "Varchar", |
|
32
|
|
|
"MainURL" => "Varchar(255)", |
|
33
|
|
|
"ReadMeURL" => "Varchar(255)", |
|
34
|
|
|
"DemoURL" => "Varchar(255)", |
|
35
|
|
|
"SvnURL" => "Varchar(255)", |
|
36
|
|
|
"GitURL" => "Varchar(255)", |
|
37
|
|
|
"OtherURL" => "Varchar(255)", |
|
38
|
|
|
"ImportID" => "Int" |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
42
|
|
|
"ModuleProductEmails" => "ModuleProductEmail" |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
private static $casting = array( |
|
|
|
|
|
|
46
|
|
|
"ModuleTitle" => "Varchar" |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
public function ModuleTitle() |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
return $this->getModuleTitle(); |
|
52
|
|
|
} |
|
53
|
|
|
public function getModuleTitle() |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
return $this->getField("MenuTitle"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private static $many_many = array( |
|
|
|
|
|
|
59
|
|
|
"Authors" => "Member" |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
private static $singular_name = "Module"; |
|
|
|
|
|
|
63
|
|
|
public function i18n_singular_name() |
|
64
|
|
|
{ |
|
65
|
|
|
return _t("Order.MODULE", "Module"); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private static $plural_name = "Modules"; |
|
|
|
|
|
|
69
|
|
|
public function i18n_plural_name() |
|
70
|
|
|
{ |
|
71
|
|
|
return _t("Order.Modules", "Modules"); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function canEdit($member = null) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($member = Member::currentUser()) { |
|
77
|
|
|
if ($member->IsShopAdmin()) { |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
if ($authors = $this->Authors()) { |
|
81
|
|
|
foreach ($authors as $author) { |
|
82
|
|
|
if ($author->ID == $member->ID) { |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function canDelete($member = null) |
|
92
|
|
|
{ |
|
93
|
|
|
if ($member = Member::currentUser()) { |
|
94
|
|
|
if ($member->IsShopAdmin()) { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function canEmail($member = null) |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->canDelete($member); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private static $searchable_fields = array( |
|
|
|
|
|
|
107
|
|
|
'Title' => "PartialMatchFilter", |
|
108
|
|
|
'InternalItemID' => "PartialMatchFilter", |
|
109
|
|
|
'ImportID', |
|
110
|
|
|
'ShowInSearch', |
|
111
|
|
|
'AllowPurchase', |
|
112
|
|
|
'FeaturedProduct', |
|
113
|
|
|
'Price' |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
public function getCMSFields() |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$fields = new FieldList(); |
|
|
|
|
|
|
120
|
|
|
$fields = parent::getCMSFields(); |
|
121
|
|
|
$authors = $this->Authors(); |
|
122
|
|
|
$sortString = ""; |
|
123
|
|
|
if ($authors) { |
|
124
|
|
|
$authorsArray = $authors->map("ID", "ScreenName")->toArray(); |
|
125
|
|
|
$sortStringEnd = ""; |
|
126
|
|
|
if (is_array($authorsArray) && count($authorsArray)) { |
|
127
|
|
|
foreach ($authorsArray as $ID => $ScreenName) { |
|
128
|
|
|
$sortString .= "IF(Member.ID = $ID, 1, "; |
|
129
|
|
|
$sortStringEnd .= ")"; |
|
130
|
|
|
} |
|
131
|
|
|
$sortString .= " 0".$sortStringEnd." DESC, \"Email\""; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
$fields->addFieldToTab('Root.Software', new TextField('Code', 'Code (this should be the same as the recommended folder name)')); |
|
135
|
|
|
$fields->addFieldToTab('Root.Software', new TextareaField('MetaDescription', 'Three sentence introduction')); |
|
136
|
|
|
$fields->addFieldToTab('Root.Software', new TextField('MainURL', 'Link to home page for the module - e.g. http://www.mymodule.com/')); |
|
137
|
|
|
$fields->addFieldToTab('Root.Software', new TextField('ReadMeURL', 'Link to read me file - e.g. http://www.mymodule.com/readme.md')); |
|
138
|
|
|
$fields->addFieldToTab('Root.Software', new TextField('DemoURL', 'Link to a demo - e.g. http://demo.mymodule.com/')); |
|
139
|
|
|
$fields->addFieldToTab('Root.Software', new TextField('SvnURL', 'Link to the SVN URL - allowing you to checkout trunk or latest version directly - e.g. http://svn.mymodule.com/svn/trunk/')); |
|
140
|
|
|
$fields->addFieldToTab('Root.Software', new TextField('GitURL', 'Link to the GIT URL - e.g. https://github.com/my-git-username/silverstripe-my-module')); |
|
141
|
|
|
$fields->addFieldToTab('Root.Software', new TextField('OtherURL', 'Link to other repository or download URL - e.g. http://www.mymodule.com/downloads/')); |
|
142
|
|
|
$fields->addFieldToTab('Root.Software', new ReadonlyField('ImportID', 'Import Identifier')); |
|
143
|
|
|
$fields->addFieldToTab('Root.Software', new HeaderField("AuthorsHeading", "Authors")); |
|
144
|
|
|
return $fields; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* return the first Author |
|
150
|
|
|
* @return Int |
|
151
|
|
|
*/ |
|
152
|
|
|
public function DefaultMemberID() |
|
153
|
|
|
{ |
|
154
|
|
|
$memberID = 0; |
|
155
|
|
|
$authors = $this->Authors(); |
|
156
|
|
|
if ($authors && $authors->count()) { |
|
157
|
|
|
$memberID = $authors->First()->ID; |
|
158
|
|
|
} |
|
159
|
|
|
return $memberID; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Has an email been sent? |
|
165
|
|
|
* @return Boolean |
|
166
|
|
|
* |
|
167
|
|
|
*/ |
|
168
|
|
|
public function HasMemberContact() |
|
169
|
|
|
{ |
|
170
|
|
|
return ModuleProductEmail::get() |
|
171
|
|
|
->filter(array("MemberID" => $this->DefaultMemberID())) |
|
172
|
|
|
->count() ? true : false; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Has an email been sent? |
|
177
|
|
|
* @return Boolean |
|
178
|
|
|
* |
|
179
|
|
|
*/ |
|
180
|
|
|
public function HasEmail() |
|
181
|
|
|
{ |
|
182
|
|
|
if ($this->EmailObject()) { |
|
|
|
|
|
|
183
|
|
|
return true; |
|
184
|
|
|
} |
|
185
|
|
|
return false; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Return the ModuleProductEmail |
|
190
|
|
|
* @return Object (ModuleProductEmail) |
|
|
|
|
|
|
191
|
|
|
* |
|
192
|
|
|
*/ |
|
193
|
|
|
public function EmailObject() |
|
194
|
|
|
{ |
|
195
|
|
|
return ModuleProductEmail::get() |
|
196
|
|
|
->filter(array("ModuleProductID" => $this->ID)) |
|
197
|
|
|
->first(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function EmailDefaults() |
|
201
|
|
|
{ |
|
202
|
|
|
$to = ""; |
|
|
|
|
|
|
203
|
|
|
$authorEmailArray = array(); |
|
204
|
|
|
$authorFirstNameArray = array(); |
|
205
|
|
|
if ($authors = $this->Authors()) { |
|
206
|
|
|
foreach ($authors as $author) { |
|
207
|
|
|
$authorEmailArray[$author->ScreenName] = $author->Email; |
|
208
|
|
|
if ($author->FirstName) { |
|
209
|
|
|
$authorFirstNameArray[$author->ScreenName] = $author->FirstName; |
|
210
|
|
|
} else { |
|
211
|
|
|
$authorFirstNameArray[$author->ScreenName] = $author->ScreenName; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
$to = implode(", ", $authorEmailArray); |
|
216
|
|
|
$subject = _t("ModuleProduct.SUBJECT", "Please check your module: ").$this->Title; |
|
217
|
|
|
$body = $this->createBodyAppendix(implode(", ", $authorFirstNameArray)); |
|
218
|
|
|
return new ArrayData( |
|
219
|
|
|
array( |
|
220
|
|
|
"To" => $to, |
|
221
|
|
|
"Subject" => $subject, |
|
222
|
|
|
"Body" => $body |
|
223
|
|
|
) |
|
224
|
|
|
); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
protected function createBodyAppendix($screenName) |
|
|
|
|
|
|
228
|
|
|
{ |
|
229
|
|
|
$pageLink = Director::absoluteURL($this->Link()); |
|
230
|
|
|
$passwordResetLink = Director::absoluteURL("Security/lostpassword"); |
|
231
|
|
|
$logInLink = Director::absoluteURL("Security/login"); |
|
232
|
|
|
$editYourDetailsLink = Director::absoluteURL(RegisterAndEditDetailsPage::get()->first()->Link()); |
|
233
|
|
|
$customisationArray = array( |
|
234
|
|
|
"ID" => $this->ID, |
|
235
|
|
|
"PageLink" => $pageLink, |
|
236
|
|
|
"PasswordResetLink" => $passwordResetLink, |
|
237
|
|
|
"LogInLink" => $logInLink, |
|
238
|
|
|
"Title" => $this->Title, |
|
239
|
|
|
"ScreenName" => $screenName, |
|
240
|
|
|
"EditYourDetailsLink" => $editYourDetailsLink |
|
241
|
|
|
); |
|
242
|
|
|
$body = $this->customise($customisationArray)->renderWith("ModuleProductEmailBody"); |
|
243
|
|
|
return $body; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
public function ReadMeContent() |
|
248
|
|
|
{ |
|
249
|
|
|
if ($this->ReadMeURL) { |
|
250
|
|
|
$this->ReadMeURL = str_replace("http://raw.github", "https://raw.github", $this->ReadMeURL); |
|
251
|
|
|
if ($this->checkIfExternalLinkWorks($this->ReadMeURL)) { |
|
252
|
|
|
return file_get_contents($this->ReadMeURL); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
protected function checkIfExternalLinkWorks($url) |
|
258
|
|
|
{ |
|
259
|
|
|
// Version 4.x supported |
|
260
|
|
|
$handle = curl_init($url); |
|
261
|
|
|
if (false === $handle) { |
|
262
|
|
|
return false; |
|
263
|
|
|
} |
|
264
|
|
|
curl_setopt($handle, CURLOPT_HEADER, false); |
|
265
|
|
|
curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works |
|
266
|
|
|
curl_setopt($handle, CURLOPT_HTTPHEADER, array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15")); // request as if Firefox |
|
267
|
|
|
curl_setopt($handle, CURLOPT_NOBODY, true); |
|
268
|
|
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, false); |
|
269
|
|
|
$connectable = curl_exec($handle); |
|
270
|
|
|
curl_close($handle); |
|
271
|
|
|
return $connectable; |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
class ModuleProduct_Controller extends Product_Controller |
|
|
|
|
|
|
278
|
|
|
{ |
|
279
|
|
|
public function init() |
|
280
|
|
|
{ |
|
281
|
|
|
parent::init(); |
|
282
|
|
|
Requirements::javascript(THIRDPARTY_DIR."/jquery-form/jquery.form.js"); |
|
283
|
|
|
Requirements::javascript("ecommerce_software/javascript/Markdown.Converter.js"); |
|
284
|
|
|
Requirements::javascript("ecommerce_software/javascript/ModuleProduct.js"); |
|
285
|
|
|
Requirements::themedCSS("ModuleProduct", "ecommerce_software"); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
public function Form() |
|
289
|
|
|
{ |
|
290
|
|
|
if ($this->canEdit()) { |
|
291
|
|
|
return new AddingModuleProduct_Form($this, "Form", $this->ID); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* |
|
297
|
|
|
* @return Object Product |
|
298
|
|
|
*/ |
|
299
|
|
View Code Duplication |
public function PreviousProduct() |
|
|
|
|
|
|
300
|
|
|
{ |
|
301
|
|
|
return ModuleProduct::get() |
|
302
|
|
|
->where("\"Sort\" < ".$this->Sort." AND ParentID = ".$this->ParentID) |
|
303
|
|
|
->sort("Sort", "DESC") |
|
304
|
|
|
->limit(1) |
|
305
|
|
|
->first(); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* |
|
310
|
|
|
* @return Object Product |
|
311
|
|
|
*/ |
|
312
|
|
View Code Duplication |
public function NextProduct() |
|
|
|
|
|
|
313
|
|
|
{ |
|
314
|
|
|
return ModuleProduct::get() |
|
315
|
|
|
->where("\"Sort\" > ".$this->Sort." AND ParentID = ".$this->ParentID) |
|
316
|
|
|
->sort("Sort", "ASC") |
|
317
|
|
|
->limit(1) |
|
318
|
|
|
->first(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
|
|
322
|
|
|
public function HasPreviousOrNextProduct() |
|
323
|
|
|
{ |
|
324
|
|
|
if ($this->NextProduct()) { |
|
325
|
|
|
return true; |
|
326
|
|
|
} |
|
327
|
|
|
if ($this->PreviousProduct()) { |
|
|
|
|
|
|
328
|
|
|
return true; |
|
329
|
|
|
} |
|
330
|
|
|
return true; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
public function EmailForm() |
|
334
|
|
|
{ |
|
335
|
|
|
if ($this->canEdit()) { |
|
336
|
|
|
if (!$this->HasEmail()) { |
|
337
|
|
|
return new ModuleProductEmail_Form($this, "EmailForm", $this->dataRecord); |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|
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.