ModuleProduct::i18n_plural_name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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
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...
12
{
13
    private static $icon = "ecommerce_software/images/treeicons/ModuleProduct";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $icon 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...
14
15
    private static $api_access = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $api_access 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...
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(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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...
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(
0 ignored issues
show
Unused Code introduced by
The property $has_many 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...
42
        "ModuleProductEmails" => "ModuleProductEmail"
43
    );
44
45
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $casting 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...
46
        "ModuleTitle" => "Varchar"
47
    );
48
49
    public function ModuleTitle()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
50
    {
51
        return $this->getModuleTitle();
52
    }
53
    public function getModuleTitle()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
54
    {
55
        return $this->getField("MenuTitle");
56
    }
57
58
    private static $many_many = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $many_many 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...
59
        "Authors" => "Member"
60
    );
61
62
    private static $singular_name = "Module";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $singular_name 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...
63
    public function i18n_singular_name()
64
    {
65
        return _t("Order.MODULE", "Module");
66
    }
67
68
    private static $plural_name = "Modules";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $plural_name 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...
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(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $searchable_fields 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...
107
        'Title' => "PartialMatchFilter",
108
        'InternalItemID' => "PartialMatchFilter",
109
        'ImportID',
110
        'ShowInSearch',
111
        'AllowPurchase',
112
        'FeaturedProduct',
113
        'Price'
114
    );
115
116
117
    public function getCMSFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
118
    {
119
        $fields = new FieldList();
0 ignored issues
show
Unused Code introduced by
$fields is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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()) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) $this->EmailObject();.
Loading history...
183
            return true;
184
        }
185
        return false;
186
    }
187
188
    /**
189
     * Return the ModuleProductEmail
190
     * @return Object (ModuleProductEmail)
0 ignored issues
show
Documentation introduced by
Should the return type not be DataObject|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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 = "";
0 ignored issues
show
Unused Code introduced by
$to is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()) {
0 ignored issues
show
Unused Code introduced by
This if statement and the following return statement are superfluous as you return always true.
Loading history...
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