1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
* |
8
|
|
|
**/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class AddingModuleProduct extends Page |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
private static $icon = "ecommerce_software/images/treeicons/AddingModuleProduct"; |
|
|
|
|
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class AddingModuleProduct_Controller extends Page_Controller |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
public function init() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
parent::init(); |
22
|
|
|
if (!Member::currentUser()) { |
23
|
|
|
$link = RegisterAndEditDetailsPage::link_for_going_to_page_via_making_user($this->Link()); |
24
|
|
|
$this->redirect($link); |
25
|
|
|
} |
26
|
|
|
if (isset($_REQUEST["ModuleProductID"])) { |
27
|
|
|
$this->moduleProductID = intval($_REQUEST["ModuleProductID"]); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function Form() |
32
|
|
|
{ |
33
|
|
|
if (Member::currentUser()) { |
34
|
|
|
return new AddingModuleProduct_Form($this, "Form", $this->moduleProductID); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
class AddingModuleProduct_Form extends Form |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
public function __construct($controller, $name, $moduleProductID = 0) |
42
|
|
|
{ |
43
|
|
|
$fields = new FieldList(); |
44
|
|
|
$moduleProduct = null; |
45
|
|
|
if ($moduleProductID) { |
46
|
|
|
$fields->push(new HeaderField('AddEditModule', 'Edit '.$controller->dataRecord->Title, 2)); |
47
|
|
|
$fields->push(new HiddenField('ModuleProductID', $moduleProductID, $moduleProductID)); |
48
|
|
|
$moduleProduct = ModuleProduct::get()->byID($moduleProductID); |
49
|
|
|
} else { |
50
|
|
|
$fields->push(new HeaderField('AddEditModule', $controller->dataRecord->Title, 2)); |
51
|
|
|
$fields->push(new HiddenField('ModuleProductID', 0, 0)); |
52
|
|
|
} |
53
|
|
|
$fields->push(new TextField('Code', 'Code (folder name)')); |
54
|
|
|
$moduleProductGroups = ModuleProductGroup::get() |
|
|
|
|
55
|
|
|
->filter(array("ParentID:GreaterThan" => 0)); |
56
|
|
|
if (ModuleProductGroup::get()->count()) { |
57
|
|
|
$types = array("" => " --- please select ---"); |
58
|
|
|
$types += ModuleProductGroup::get()->map($index = 'ID', $titleField = 'MenuTitle')->toArray(); |
59
|
|
|
} else { |
60
|
|
|
$types = array(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
//$fields->push(new DropdownField('ParentID','Type', $types, $controller->dataRecord->ID)); |
|
|
|
|
63
|
|
|
$fields->push(new TextField('Title', 'Title')); |
64
|
|
|
$fields->push(new TextareaField('MetaDescription', 'Three sentence Introduction')); |
65
|
|
|
$fields->push(new HtmlEditorField('Content', 'Long Description')); |
66
|
|
|
$fields->push(new TextField('AdditionalTags', 'Additional Keyword(s), comma separated')); |
67
|
|
|
$fields->push(new HeaderField('LinkHeader', 'Links', 4)); |
68
|
|
|
$fields->push(new TextField('MainURL', 'Home page')); |
69
|
|
|
$fields->push(new TextField('ReadMeURL', 'Read me file - e.g. http://www.mymodule.com/readme.md')); |
70
|
|
|
$fields->push(new TextField('DemoURL', 'Demo - e.g. http://demo.mymodule.com/')); |
71
|
|
|
$fields->push(new TextField('SvnURL', 'SVN repository - allowing you to checkout trunk or latest version - e.g. http://svn.mymodule.com/svn/trunk/')); |
72
|
|
|
$fields->push(new TextField('GitURL', 'GIT repository - e.g. https://github.com/my-github-username/silverstripe-my-module')); |
73
|
|
|
$fields->push(new TextField('OtherURL', 'Link to other repository or download URL - e.g. http://www.mymodule.com/downloads/')); |
74
|
|
|
$fields->push(new CheckboxSetField('EcommerceProductTags', 'Tags', EcommerceProductTag::get()->map()->toArray())); |
75
|
|
|
$member = Member::currentUser(); |
76
|
|
|
if ($member->inGroup("ADMIN")) { |
77
|
|
|
$fields->push(new CheckboxSetField('Authors', 'Author(s)', Member::get()->exclude("Email", "")->map("ID", "Email")->toArray())); |
78
|
|
|
$fields->push(new DropdownField('ParentID', 'Move to', ProductGroup::get()->map()->toArray())); |
79
|
|
|
$fields->push(new CheckboxField('ShowInMenus', 'Show in menus (unticking both boxes here will hide the module)')); |
80
|
|
|
$fields->push(new CheckboxField('ShowInSearch', 'Show in search (unticking both boxes here will hide the module)')); |
81
|
|
|
} else { |
82
|
|
|
$fields->push(new HiddenField('ShowInMenus', '', 0)); |
83
|
|
|
$fields->push(new HiddenField('ShowInSearch', '', 0)); |
84
|
|
|
$fields->push(new HiddenField('ParentID', '', $controller->dataRecord->ID)); |
85
|
|
|
if ($moduleProduct) { |
86
|
|
|
$moduleProduct->ParentID = $controller->dataRecord->ID; |
87
|
|
|
$moduleProduct->ShowInSearch = 0; |
88
|
|
|
$moduleProduct->ShowInMenus = 0; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
if ($moduleProduct && $moduleProduct->canEdit()) { |
92
|
|
|
if ($authors = $moduleProduct->Authors()) { |
93
|
|
|
$authorsIDArray = $authors->map("ID", "ID")->toArray(); |
94
|
|
|
$authorsIDArray[0] = 0; |
95
|
|
|
$fields->push($this->ManyManyComplexTableFieldAuthorsField($controller, $authorsIDArray)); |
96
|
|
|
//$controller, $name, $sourceClass, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "" |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
$actions = new FieldList(new FormAction("submit", "submit")); |
100
|
|
|
$validator = new AddingModuleProduct_RequiredFields($moduleProductID, array('Code', 'Name', 'ParentID', 'MainURL')); |
101
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator); |
102
|
|
|
if ($moduleProduct) { |
103
|
|
|
$this->loadDataFrom($moduleProduct); |
104
|
|
|
} |
105
|
|
|
return $this; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function submit($data, $form) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$member = Member::currentUser(); |
111
|
|
|
if (!$member) { |
112
|
|
|
$form->setMessage("You need to be logged in to edit this module.", "bad"); |
113
|
|
|
$this->redirectBack(); |
|
|
|
|
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
$data = Convert::raw2sql($data); |
117
|
|
|
$page = null; |
118
|
|
|
if (isset($data["ModuleProductID"])) { |
119
|
|
|
$page = ModuleProduct::get()->byID(intval($data["ModuleProductID"])); |
120
|
|
|
} |
121
|
|
|
if (!$page) { |
122
|
|
|
$page = new ModuleProduct(); |
123
|
|
|
} |
124
|
|
|
if (isset($page->ParentID)) { |
125
|
|
|
$oldParentID = $page->ParentID; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
$form->saveInto($page); |
128
|
|
|
$page->Title = $data["Title"]; |
129
|
|
|
$page->MenuTitle = $data["Title"]; |
130
|
|
|
if (!$member->inGroup("ADMIN")) { |
131
|
|
|
$page->ShowInMenus = 0; |
132
|
|
|
$page->ShowInMenus = 0; |
133
|
|
|
$parentPage = AddingModuleProduct::get()->First(); |
134
|
|
|
if ($parentPage) { |
135
|
|
|
$page->ParentID = $parentPage->ID; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
$page->writeToStage('Stage'); |
139
|
|
|
$page->Publish('Stage', 'Live'); |
140
|
|
|
$page->Status = "Published"; |
141
|
|
|
$page->flushCache(); |
142
|
|
|
if ($page->Authors()->count() == 0 && $member) { |
143
|
|
|
$page->Authors()->addMany(array($member->ID => $member->ID)); |
144
|
|
|
} |
145
|
|
|
if (!isset($data["EcommerceProductTags"]) || ! is_array($data["EcommerceProductTags"]) || !count($data["EcommerceProductTags"])) { |
146
|
|
|
$data["EcommerceProductTags"] = array(-1 => -1); |
147
|
|
|
} |
148
|
|
|
if (isset($data["AdditionalTags"]) && $data["AdditionalTags"]) { |
149
|
|
|
$extraTagsArray = explode(",", $data["AdditionalTags"]); |
150
|
|
|
if (is_array($extraTagsArray) && count($extraTagsArray)) { |
151
|
|
|
foreach ($extraTagsArray as $tag) { |
152
|
|
|
$tag = trim($tag); |
153
|
|
|
$obj = EcommerceProductTag::get() |
154
|
|
|
->filter(array("Title" => $tag)) |
155
|
|
|
->first(); |
156
|
|
|
if (!$obj) { |
157
|
|
|
$obj = new EcommerceProductTag(); |
158
|
|
|
$obj->Title = $tag; |
159
|
|
|
$obj->write(); |
160
|
|
|
} |
161
|
|
|
$data["EcommerceProductTags"][$obj->ID] = $obj->ID; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
DB::query("DELETE FROM \"EcommerceProductTag_Products\" WHERE \"ProductID\" = ".$page->ID. " AND \"EcommerceProductTagID\" NOT IN (".implode(",", $data["EcommerceProductTags"]).")"); |
166
|
|
|
if (is_array($data["EcommerceProductTags"]) && count($data["EcommerceProductTags"])) { |
167
|
|
|
$page->EcommerceProductTags()->addMany($data["EcommerceProductTags"]); |
168
|
|
|
} |
169
|
|
|
if (Director::is_ajax()) { |
170
|
|
|
return $page->renderWith("ModuleProductInner"); |
171
|
|
|
} else { |
172
|
|
|
$this->redirect($page->Link()); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
protected function ManyManyComplexTableFieldAuthorsField($controller, $authorsIDArray) |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
$detailFields = new FieldList(); |
180
|
|
|
$detailFields->push(new TextField("ScreenName")); |
181
|
|
|
$detailFields->push(new TextField("FirstName")); |
182
|
|
|
$detailFields->push(new TextField("Surname")); |
183
|
|
|
$detailFields->push(new TextField("Email")); |
184
|
|
|
$detailFields->push(new TextField("GithubURL", "Github URL")); |
185
|
|
|
$detailFields->push(new TextField("SilverstripeDotOrgURL", "www.silverstripe.org URL")); |
186
|
|
|
$detailFields->push(new TextField("CompanyName", "Company Name")); |
187
|
|
|
$detailFields->push(new TextField("CompanyURL", "Company URL")); |
188
|
|
|
$detailFields->push(new CheckboxField("AreYouHappyForPeopleToContactYou", "are you happy for people to contact you about your module?")); |
189
|
|
|
$detailFields->push(new TextField("ContactDetailURL", "Contact details URL")); |
190
|
|
|
$detailFields->push(new TextField("OtherURL", "Other URL")); |
191
|
|
|
$detailFields->push(new CheckboxField("AreYouAvailableForPaidSupport", "are you available for paid support?")); |
192
|
|
|
$detailFields->push(new NumericField("Rate15Mins", "Indicative rate for 15 minute skype chat")); |
193
|
|
|
$detailFields->push(new NumericField("Rate120Mins", "Indicative rate for two hour work slot")); |
194
|
|
|
$detailFields->push(new NumericField("Rate480Mins", "Indicative rate for one day of work")); |
195
|
|
|
$field = new GridField( |
196
|
|
|
'Authors', |
197
|
|
|
'Member', |
198
|
|
|
Member::get() |
199
|
|
|
); |
200
|
|
|
/* |
|
|
|
|
201
|
|
|
array( |
202
|
|
|
"ScreenName" => "Screen name", |
203
|
|
|
"FirstName" => "First name", |
204
|
|
|
"Surname" => "Surname", |
205
|
|
|
"Email" => "Email" |
206
|
|
|
),//fieldList |
207
|
|
|
$detailFields,//detailFormFields |
208
|
|
|
"\"Member\".\"ID\" IN (".implode(",", $authorsIDArray).")",//sourceFilter |
209
|
|
|
"",//sourceSort |
210
|
|
|
null//sourceJoin |
211
|
|
|
*/ |
212
|
|
|
//$field->setPopupCaption("Edit Author"); |
|
|
|
|
213
|
|
|
//$field->setAddTitle("Author"); |
|
|
|
|
214
|
|
|
return $field; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
class AddingModuleProduct_RequiredFields extends RequiredFields |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
protected $currentID = 0; |
221
|
|
|
|
222
|
|
|
public function __construct($currentID, $array) |
223
|
|
|
{ |
224
|
|
|
$this->currentID = $currentID; |
225
|
|
|
parent::__construct($array); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function javascript() |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
$codes = DB::query("SELECT \"Code\" FROM ModuleProduct WHERE ModuleProduct.ID <> ".($this->currentID - 0))->column(); |
231
|
|
|
if ($codes) { |
|
|
|
|
232
|
|
|
$js = ' |
233
|
|
|
jQuery(document).ready( |
234
|
|
|
function() { |
235
|
|
|
var AddingModuleProductCodes = new Array(\''.implode("','", $codes).'\'); |
236
|
|
|
jQuery("#Code input").change( |
237
|
|
|
function(){ |
238
|
|
|
var val = jQuery("#Code input").val(); |
239
|
|
|
jQuery("#Code input").css("color", "green"); |
240
|
|
|
for(i = 0; i < AddingModuleProductCodes.length; i++) { |
241
|
|
|
if(AddingModuleProductCodes[i] == val) { |
242
|
|
|
i = 999999999; |
243
|
|
|
alert("Your code \'"+val+"\' is already in use - please use an alternative code."); |
244
|
|
|
jQuery("#Code input").focus().css("color", "red"); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
); |
251
|
|
|
'; |
252
|
|
|
Requirements::customScript($js, "AddingModuleProductCodes"); |
253
|
|
|
} |
254
|
|
|
return parent::javascript(); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Allows validation of fields via specification of a php function for validation which is executed after |
260
|
|
|
* the form is submitted |
261
|
|
|
*/ |
262
|
|
|
public function php($data) |
263
|
|
|
{ |
264
|
|
|
$valid = true; |
265
|
|
|
if (isset($data["Code"])) { |
266
|
|
|
$type = Convert::raw2sql($data["Code"]); |
267
|
|
|
$extension = ''; |
|
|
|
|
268
|
|
|
if (Versioned::current_stage() == "Live") { |
269
|
|
|
$extension = "_Live"; |
|
|
|
|
270
|
|
|
} |
271
|
|
|
if (ModuleProduct::get() |
272
|
|
|
->filter(array("Code" => $type)) |
273
|
|
|
->exclude("ModuleProduct", $this->currentID - 0) |
274
|
|
|
->count() |
275
|
|
|
) { |
276
|
|
|
$errorMessage = sprintf(_t('Form.CODEALREADYINUSE', "Your code %s is already in use - please check if your code is listed already or use an alternative code."), $type); |
277
|
|
|
$this->validationError( |
278
|
|
|
$fieldName = "Code", |
279
|
|
|
$errorMessage, |
280
|
|
|
"required" |
281
|
|
|
); |
282
|
|
|
$valid = false; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
if (!$valid) { |
286
|
|
|
return false; |
287
|
|
|
} |
288
|
|
|
return parent::php($data); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
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.