1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\DownloadableProducts; |
4
|
|
|
|
5
|
|
|
use Product; |
|
|
|
|
6
|
|
|
use SilverStripe\Assets\File; |
7
|
|
|
use SilverStripe\Forms\TextField; |
8
|
|
|
use SilverStripe\Security\Security; |
9
|
|
|
use SilverStripe\Security\Member; |
10
|
|
|
use SilverStripe\Core\Config\Config; |
11
|
|
|
use SilverCommerce\OrdersAdmin\Model\Invoice; |
12
|
|
|
use SilverStripe\AssetAdmin\Forms\UploadField; |
13
|
|
|
use SilverCommerce\DownloadableProducts\DownloadFolder; |
14
|
|
|
use SilverCommerce\DownloadableProducts\FileDownloadController; |
15
|
|
|
use SilverStripe\Core\Injector\Injector; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Product class that will allow adding of product to the CMS. |
19
|
|
|
*/ |
20
|
|
|
class DownloadableProduct extends Product |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Set the default DB table name |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private static $table_name = "DownloadableProduct"; |
|
|
|
|
29
|
|
|
|
30
|
|
|
private static $singular_name = 'Downloadable Product'; |
|
|
|
|
31
|
|
|
|
32
|
|
|
private static $plural_name = 'Downloadable Products'; |
|
|
|
|
33
|
|
|
|
34
|
|
|
private static $description = "A product that can be downloaded"; |
|
|
|
|
35
|
|
|
|
36
|
|
|
private static $db = [ |
|
|
|
|
37
|
|
|
'LinkLife' => 'Int' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
private static $has_one = [ |
|
|
|
|
41
|
|
|
"File" => File::class |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
private static $casting = [ |
|
|
|
|
45
|
|
|
"DownloadLink" => "Varchar", |
46
|
|
|
"Deliverable" => "Boolean" |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
private static $owns = [ |
|
|
|
|
50
|
|
|
"File" |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
private static $defaults = [ |
|
|
|
|
54
|
|
|
'LinkLife' => 7 |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Downloadable products are not deliverable. This will be |
59
|
|
|
* detected by the shopping cart to disable delivery options. |
60
|
|
|
* |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
|
|
public function getDeliverable() |
64
|
|
|
{ |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the link to download the file associated with this product |
70
|
|
|
* |
71
|
|
|
* @param int $invoice_id the ID of an associated invoice |
72
|
|
|
* @param string $access_key key of assocaiated invoice |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getDownloadLink($invoice_id, $access_key) |
77
|
|
|
{ |
78
|
|
|
$file = $this->File(); |
79
|
|
|
|
80
|
|
|
if ($file->exists()) { |
81
|
|
|
$download = Injector::inst() |
82
|
|
|
->get(FileDownloadController::class); |
83
|
|
|
|
84
|
|
|
return $download->DownloadLink( |
85
|
|
|
$file->ID, |
86
|
|
|
$invoice_id, |
87
|
|
|
$access_key, |
88
|
|
|
$file->Name |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return ""; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the folder to add downloads to |
97
|
|
|
* |
98
|
|
|
* @return DownloadFolder |
99
|
|
|
*/ |
100
|
|
|
public function getDownloadFolder() |
101
|
|
|
{ |
102
|
|
|
return DownloadFolder::find_or_make( |
103
|
|
|
Config::inst()->get(DownloadFolder::class, "folder_name") |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
public function getCMSFields() |
109
|
|
|
{ |
110
|
|
|
$fields = parent::getCMSFields(); |
111
|
|
|
|
112
|
|
|
$fields->removeByName("Weight"); |
113
|
|
|
|
114
|
|
|
$fields->addFieldsToTab( |
115
|
|
|
"Root.Settings", |
116
|
|
|
[ |
117
|
|
|
TextField::create('LinkLife', 'Life of download link (in days)'), |
118
|
|
|
UploadField::create("File") |
119
|
|
|
->setFolderName($this->config()->folder_name) |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
return $fields; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Ensure weight is removed on save and that attached files are moved to |
128
|
|
|
* the correct folder |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function onBeforeWrite() |
133
|
|
|
{ |
134
|
|
|
parent::onBeforeWrite(); |
135
|
|
|
|
136
|
|
|
// Set weight |
137
|
|
|
$this->Weight = 0; |
|
|
|
|
138
|
|
|
|
139
|
|
|
// Deal with moving file |
140
|
|
|
$file = $this->File(); |
141
|
|
|
$folder = $this->getDownloadFolder(); |
142
|
|
|
$move = true; |
143
|
|
|
|
144
|
|
|
// If the file is in the download folder (or an ancestor), don't move |
145
|
|
|
if ($file->exists() && $file->ParentID == $folder->ID) { |
146
|
|
|
$move = false; |
147
|
|
|
} elseif ($file->exists()) { |
148
|
|
|
$id_list = $folder->getDescendantIDList(); |
|
|
|
|
149
|
|
|
if (in_array($file->ParentID, $id_list)) { |
150
|
|
|
$move = false; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// If needed, move the attached file to a new folder |
155
|
|
|
if ($move) { |
156
|
|
|
$file->ParentID = $folder->ID; |
157
|
|
|
$file->write(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Special permission to see if this product can be downloaded by the current member |
163
|
|
|
* |
164
|
|
|
* @param Member $member The current member object |
165
|
|
|
* |
166
|
|
|
* @return boolean |
167
|
|
|
*/ |
168
|
|
|
public function canDownload(Member $member = null) |
169
|
|
|
{ |
170
|
|
|
if (!$member) { |
171
|
|
|
$member = Security::getCurrentUser(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$contact = null; |
175
|
|
|
|
176
|
|
|
if (isset($member)) { |
177
|
|
|
$contact = $member->Contact(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if (isset($contact)) { |
181
|
|
|
$items = $contact |
182
|
|
|
->Invoices() |
183
|
|
|
->filter( |
184
|
|
|
[ |
185
|
|
|
"Status" => Config::inst() |
186
|
|
|
->get(Invoice::class, "paid_statuses"), |
187
|
|
|
"Items.StockID" => $this->StockID |
188
|
|
|
] |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
return $items->exists(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths