|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
class ImportModulesTask extends BuildTask |
|
|
|
|
|
|
6
|
|
|
{ |
|
7
|
|
|
private static $parent_url_segment = "new-modules"; |
|
8
|
|
|
public function get_parent_url_segment() |
|
9
|
|
|
{ |
|
10
|
|
|
return self::$parent_url_segment; |
|
11
|
|
|
} |
|
12
|
|
|
public function set_parent_url_segment($s) |
|
13
|
|
|
{ |
|
14
|
|
|
self::$parent_url_segment = $s; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
private static $data_source = "/mysite/data/modules.csv"; |
|
18
|
|
|
public function get_data_source() |
|
19
|
|
|
{ |
|
20
|
|
|
return self::$data_source; |
|
21
|
|
|
} |
|
22
|
|
|
public function set_data_source($s) |
|
23
|
|
|
{ |
|
24
|
|
|
self::$data_source = $s; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getTitle() |
|
28
|
|
|
{ |
|
29
|
|
|
return "Import modules from csv"; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getDescription() |
|
33
|
|
|
{ |
|
34
|
|
|
return "Opens the local csv file and imports all the modules"; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function run($request) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->createAuthorGroup(); |
|
40
|
|
|
$this->importmodules(); |
|
41
|
|
|
$this->sortPagesAlphabetically(); |
|
42
|
|
|
return "BBBBBBBBBBBBBBB"; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function cleantags() |
|
46
|
|
|
{ |
|
47
|
|
|
DB::query("DELETE FROM \"EcommerceProductTag\" WHERE TRIM(Title) = '' OR TRIM(Code) = '' OR Title IS NULL or Code IS NULL;"); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function importmodules() |
|
51
|
|
|
{ |
|
52
|
|
|
$rowPosition = 1; |
|
53
|
|
|
$fields = array( |
|
54
|
|
|
0 => "ImportID", |
|
55
|
|
|
1 => "ScreenName", |
|
56
|
|
|
2 => "Email", |
|
57
|
|
|
3 => "GithubURL", |
|
58
|
|
|
4 => "SilverstripeDotOrgURL", |
|
59
|
|
|
5 => "CompanyName", |
|
60
|
|
|
6 => "CompanyURL", |
|
61
|
|
|
7 => "Code", |
|
62
|
|
|
8 => "Title", |
|
63
|
|
|
9=> "MainURL", |
|
64
|
|
|
10=> "ReadMeURL", |
|
65
|
|
|
11=> "DemoURL", |
|
66
|
|
|
12=> "SvnURL", |
|
67
|
|
|
13=> "GitURL", |
|
68
|
|
|
14=> "OtherURL", |
|
69
|
|
|
15=> "Tags", |
|
70
|
|
|
16=> "Description" |
|
71
|
|
|
); |
|
72
|
|
|
$fullArray = array(); |
|
73
|
|
|
$file = Director::baseFolder().self::get_data_source(); |
|
74
|
|
|
if (($handle = fopen($file, "r")) !== false) { |
|
75
|
|
|
while (($row = fgetcsv($handle)) !== false) { |
|
76
|
|
|
$numberOfFields = count($row); |
|
77
|
|
|
//echo "<p> $num fields in line $rowPosition: <br /></p>\n"; |
|
78
|
|
|
$rowPosition++; |
|
79
|
|
|
for ($fieldPosition = 0; $fieldPosition < $numberOfFields; $fieldPosition++) { |
|
80
|
|
|
$fullArray[$rowPosition][$fields[$fieldPosition]] = $row[$fieldPosition]; |
|
81
|
|
|
//echo $fields[$fieldPosition]." = ".$row[$fieldPosition] . "<br />\n"; |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
fclose($handle); |
|
85
|
|
|
} |
|
86
|
|
|
$this->makeModules($fullArray); |
|
87
|
|
|
return $fullArray; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function createAuthorGroup() |
|
91
|
|
|
{ |
|
92
|
|
|
if (!$group = Group::get() |
|
93
|
|
|
->filter(array("Code" => Config::inst()->get("SoftwareAuthorMemberDOD", "register_group_code"))) |
|
94
|
|
|
->first() |
|
95
|
|
|
) { |
|
96
|
|
|
$group = new Group(); |
|
97
|
|
|
$group->Code = SoftwareAuthorMemberDOD::get_register_group_code(); |
|
98
|
|
|
$group->Title = SoftwareAuthorMemberDOD::get_register_group_title(); |
|
99
|
|
|
$group->write(); |
|
100
|
|
|
Permission::grant($group->ID, SoftwareAuthorMemberDOD::get_register_group_access_key()); |
|
101
|
|
|
DB::alteration_message("GROUP: ".SoftwareAuthorMemberDOD::get_register_group_code().' ('.SoftwareAuthorMemberDOD::get_register_group_title().')', "created"); |
|
102
|
|
|
} elseif (DB::query("SELECT * FROM Permission WHERE GroupID = ".$group->ID." AND Code = '".SoftwareAuthorMemberDOD::get_register_group_access_key()."'")->numRecords() == 0) { |
|
103
|
|
|
Permission::grant($group->ID, SoftwareAuthorMemberDOD::get_register_group_access_key()); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function makeModules($rows) |
|
108
|
|
|
{ |
|
109
|
|
|
increase_time_limit_to(600); |
|
110
|
|
|
DB::query("DELETE FROM \"EcommerceProductTag\" WHERE TRIM(Title) = '';"); |
|
111
|
|
|
$parent = ModuleProductGroup::get() |
|
112
|
|
|
->filter(array("URLSegment" => self::$parent_url_segment)) |
|
113
|
|
|
->first(); |
|
114
|
|
|
if ($parent) { |
|
115
|
|
|
$parentID = $parent->ID; |
|
116
|
|
|
unset($parent); |
|
117
|
|
|
} else { |
|
118
|
|
|
$parentID = 0; |
|
119
|
|
|
} |
|
120
|
|
|
$group = Group::get() |
|
121
|
|
|
->filter(array("Code" => SoftwareAuthorMemberDOD::get_register_group_code())) |
|
122
|
|
|
->first(); |
|
123
|
|
|
if (!$group) { |
|
124
|
|
|
user_error("Group for authors could not be found!"); |
|
125
|
|
|
} |
|
126
|
|
|
if ($parentID) { |
|
127
|
|
|
if ($rows) { |
|
128
|
|
|
foreach ($rows as $row) { |
|
129
|
|
|
$new = null; |
|
|
|
|
|
|
130
|
|
|
$ImportID = intval($row["ImportID"]); |
|
131
|
|
|
if ($ImportID) { |
|
132
|
|
|
$ScreenName = Convert::raw2sql($row["ScreenName"]); |
|
133
|
|
|
$Email = Convert::raw2sql($row["Email"]); |
|
134
|
|
|
$GithubURL = Convert::raw2sql($row["GithubURL"]); |
|
135
|
|
|
$SilverstripeDotOrgURL = Convert::raw2sql($row["SilverstripeDotOrgURL"]); |
|
136
|
|
|
$CompanyName = Convert::raw2sql($row["CompanyName"]); |
|
137
|
|
|
$CompanyURL = Convert::raw2sql($row["CompanyURL"]); |
|
138
|
|
|
$Code = Convert::raw2sql($row["Code"]); |
|
139
|
|
|
$Title = Convert::raw2sql($row["Title"]); |
|
140
|
|
|
$MainURL = Convert::raw2sql($row["MainURL"]); |
|
141
|
|
|
$ReadMeURL = Convert::raw2sql($row["ReadMeURL"]); |
|
142
|
|
|
$DemoURL = Convert::raw2sql($row["DemoURL"]); |
|
143
|
|
|
$SvnURL = Convert::raw2sql($row["SvnURL"]); |
|
144
|
|
|
$GitURL = Convert::raw2sql($row["GitURL"]); |
|
145
|
|
|
$OtherURL = Convert::raw2sql($row["OtherURL"]); |
|
146
|
|
|
$Tags = Convert::raw2sql($row["Tags"]); |
|
147
|
|
|
$Description = Convert::raw2sql($row["Description"]); |
|
148
|
|
|
$page = ModuleProduct::get() |
|
149
|
|
|
->filter(array("ImportID" => $ImportID)) |
|
150
|
|
|
->first(); |
|
151
|
|
|
if (!$page) { |
|
152
|
|
|
$new = true; |
|
153
|
|
|
$page = new ModuleProduct(); |
|
154
|
|
|
} else { |
|
155
|
|
|
$new = false; |
|
156
|
|
|
} |
|
157
|
|
|
if ($new == false && isset($page->ParentID) && $page->ParentID && $page->ParentID > 0 && $page->ParentID != $parentID) { |
|
|
|
|
|
|
158
|
|
|
//do nothing |
|
159
|
|
|
} else { |
|
160
|
|
|
if (!$new) { |
|
161
|
|
|
DB::query("DELETE FROM \"EcommerceProductTag_Products\" WHERE ProductID = ".$page->ID); |
|
162
|
|
|
} |
|
163
|
|
|
if ($Title && $Code) { |
|
164
|
|
|
$member = null; |
|
165
|
|
|
//member |
|
166
|
|
|
if ($ScreenName) { |
|
167
|
|
|
$member = Member::get() |
|
168
|
|
|
->filter(array("ScreenName" => $ScreenName)) |
|
169
|
|
|
->first(); |
|
170
|
|
|
} |
|
171
|
|
|
$identifierField = Member::get_unique_identifier_field(); |
|
|
|
|
|
|
172
|
|
|
if (!$member) { |
|
173
|
|
|
$member = Member::get() |
|
174
|
|
|
->filter(array($identifierField, $Email)); |
|
175
|
|
|
} |
|
176
|
|
|
if ($member) { |
|
177
|
|
|
$i = 0; |
|
178
|
|
|
while ($replaceMember = Member::get() |
|
|
|
|
|
|
179
|
|
|
->filter(array($identifierField => $Email)) |
|
180
|
|
|
->exclude("ID", $member->ID) |
|
181
|
|
|
->first() |
|
182
|
|
|
&& $i < 100000 |
|
183
|
|
|
) { |
|
184
|
|
|
if ($replaceMember) { |
|
185
|
|
|
$i++; |
|
186
|
|
|
$member = $replaceMember; |
|
187
|
|
|
$Email = $Email."_DOUBLE_$i"; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
if (!$member) { |
|
192
|
|
|
$member = new Member(); |
|
193
|
|
|
} |
|
194
|
|
|
if ($ScreenName) { |
|
195
|
|
|
$member->ScreenName = $ScreenName; |
|
196
|
|
|
$member->Email = $Email; |
|
197
|
|
|
$member->GithubURL = $GithubURL; |
|
198
|
|
|
$member->SilverstripeDotOrgURL = $SilverstripeDotOrgURL; |
|
199
|
|
|
$member->CompanyName = $CompanyName; |
|
200
|
|
|
$member->CompanyURL = $CompanyURL; |
|
201
|
|
|
if (!$member->Password) { |
|
202
|
|
|
$member->Password = Member::create_new_password(); |
|
203
|
|
|
} |
|
204
|
|
|
$member->write(); |
|
|
|
|
|
|
205
|
|
|
$member->Groups()->add($group); |
|
206
|
|
|
} else { |
|
207
|
|
|
DB::alteration_message("no screen name provided for <b>$Title</b>", "deleted"); |
|
208
|
|
|
} |
|
209
|
|
|
//page |
|
210
|
|
|
$page->ImportID = $ImportID; |
|
211
|
|
|
$page->ParentID = $parentID; |
|
212
|
|
|
$page->ShowInSearch = 1; |
|
213
|
|
|
$page->ShowInMenus = 1; |
|
214
|
|
|
$page->Title = $Title; |
|
215
|
|
|
$page->MenuTitle = $Title; |
|
216
|
|
|
$page->MetaDescription = strip_tags($Description); |
|
217
|
|
|
$page->Code = $Code; |
|
218
|
|
|
$page->InternalItemID = $Code; |
|
219
|
|
|
$page->URLSegment = $Code; |
|
220
|
|
|
$page->ProvideComments = true; |
|
221
|
|
|
|
|
222
|
|
|
$page->MainURL = $MainURL; |
|
223
|
|
|
$page->ReadMeURL = $ReadMeURL; |
|
224
|
|
|
$page->DemoURL= $DemoURL; |
|
225
|
|
|
$page->SvnURL = $SvnURL; |
|
226
|
|
|
$page->GitURL = $GitURL; |
|
227
|
|
|
$page->OtherURL = $OtherURL; |
|
228
|
|
|
|
|
229
|
|
|
$page->writeToStage('Stage'); |
|
230
|
|
|
$page->Publish('Stage', 'Live'); |
|
231
|
|
|
$page->Status = "Published"; |
|
232
|
|
|
$tagsArray = explode(",", $Tags); |
|
233
|
|
|
if ($tagsArray && count($tagsArray)) { |
|
|
|
|
|
|
234
|
|
|
foreach ($tagsArray as $tag) { |
|
235
|
|
|
$tag = Convert::raw2sql(trim($tag)); |
|
236
|
|
|
if ($tag) { |
|
237
|
|
|
$tagObject = EcommerceProductTag::get() |
|
238
|
|
|
->filter(array("Title" => $tag)) |
|
239
|
|
|
->First(); |
|
240
|
|
|
if (!$tagObject) { |
|
241
|
|
|
$tagObject = EcommerceProductTag::get() |
|
242
|
|
|
->filter(array("Synonyms:PartialMatch" => $tag)) |
|
243
|
|
|
->first(); |
|
244
|
|
|
} |
|
245
|
|
|
if (!$tagObject) { |
|
246
|
|
|
$tagObject = new EcommerceProductTag(); |
|
247
|
|
|
$tagObject->Title = $tag; |
|
248
|
|
|
$tagObject->write(); |
|
249
|
|
|
} |
|
250
|
|
|
$existingTags = $page->EcommerceProductTags(); |
|
251
|
|
|
$existingTags->add($tagObject); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
if ($member) { |
|
256
|
|
|
DB::query("DELETE FROM \"ModuleProduct_Authors\" WHERE \"ModuleProductID\" = ".$page->ID." AND MemberID <> ".$member->ID); |
|
257
|
|
|
$existingAuthors = $page->Authors(); |
|
258
|
|
|
$existingAuthors->add($member); |
|
259
|
|
|
} else { |
|
260
|
|
|
DB::alteration_message("no member for <b>$Title</b>", "deleted"); |
|
261
|
|
|
} |
|
262
|
|
|
if ($new === true) { |
|
263
|
|
|
DB::alteration_message("added <b>$Title</b>", "created"); |
|
264
|
|
|
} elseif ($new === false) { |
|
265
|
|
|
DB::alteration_message("updated <b>$Title</b>", "edited"); |
|
266
|
|
|
} elseif ($new === null) { |
|
267
|
|
|
DB::alteration_message("error updating <b>$Title</b>", "deleted"); |
|
268
|
|
|
} else { |
|
269
|
|
|
DB::alteration_message("BIG error updating <b>$Title</b>", "deleted"); |
|
270
|
|
|
} |
|
271
|
|
|
} else { |
|
272
|
|
|
DB::alteration_message("row found without title or code", "deleted"); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
} else { |
|
276
|
|
|
DB::alteration_message("row found without import id", "deleted"); |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
} else { |
|
280
|
|
|
DB::alteration_message("no data found", "deleted"); |
|
281
|
|
|
} |
|
282
|
|
|
} else { |
|
283
|
|
|
DB::alteration_message("no parent group page found (a ModuleProductGroup with new-modules as URL Segment", "deleted"); |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
private function sortPagesAlphabetically() |
|
288
|
|
|
{ |
|
289
|
|
|
$parent = ModuleProductGroup::get() |
|
290
|
|
|
->filter(array("URLSegment" => self::$parent_url_segment)) |
|
291
|
|
|
->first(); |
|
292
|
|
|
if ($parent) { |
|
293
|
|
|
$pages = ModuleProduct::get() |
|
294
|
|
|
->filter(array("ParentID" => $parent->ID)) |
|
295
|
|
|
->sort("Title", "ASC"); |
|
296
|
|
|
$i = 0; |
|
297
|
|
|
foreach ($pages as $page) { |
|
298
|
|
|
$i++; |
|
299
|
|
|
DB::query("Update \"SiteTree\" SET \"Sort\" = $i WHERE \"ID\" = ".$page->ID); |
|
300
|
|
|
DB::query("Update \"SiteTree_Live\" SET \"Sort\" = $i WHERE \"ID\" = ".$page->ID); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
public function deleteobsoletemoduleowners($request = null) |
|
|
|
|
|
|
306
|
|
|
{ |
|
307
|
|
|
$group = Group::get() |
|
308
|
|
|
->filter(array("Code" => SoftwareAuthorMemberDOD::get_register_group_code())) |
|
309
|
|
|
->first(); |
|
310
|
|
|
if ($group) { |
|
311
|
|
|
$members = $group->Members(); |
|
312
|
|
|
if ($members) { |
|
313
|
|
|
foreach ($members as $member) { |
|
314
|
|
|
if ($member->ModuleProducts() && $member->ModuleProducts()->count()) { |
|
315
|
|
|
DB::alteration_message("The following member own modules so will not be deleted ...".$member->Email.": ".$member->Title, "created"); |
|
316
|
|
|
} else { |
|
317
|
|
|
DB::alteration_message("The following member does not seem to have any module products ...".$member->Email.": ".$member->Title, "deleted"); |
|
318
|
|
|
if (!$member->inGroup("ADMIN")) { |
|
319
|
|
|
$member->delete(); |
|
320
|
|
|
} else { |
|
321
|
|
|
DB::alteration_message("The following member is an Admin so will not be deleted ...".$member->Email.": ".$member->Title, "created"); |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
} else { |
|
326
|
|
|
DB::alteration_message("could not find members for group with code = ".self::get_register_group_code(), "deleted"); |
|
|
|
|
|
|
327
|
|
|
} |
|
328
|
|
|
} else { |
|
329
|
|
|
DB::alteration_message("could not find group with code = ".self::get_register_group_code(), "deleted"); |
|
|
|
|
|
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
|
|
335
|
|
|
|
|
336
|
|
|
|
|
337
|
|
|
|
|
338
|
|
|
|
|
339
|
|
|
|
|
340
|
|
|
class ImportModulesTask_AdminDecorator extends Extension |
|
|
|
|
|
|
341
|
|
|
{ |
|
342
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
343
|
|
|
"importmodulestask" => true, |
|
344
|
|
|
"deleteobsoletemoduleowners" => true |
|
345
|
|
|
); |
|
346
|
|
|
|
|
347
|
|
|
public function updateEcommerceDevMenuMigrations($buildTasks) |
|
348
|
|
|
{ |
|
349
|
|
|
$buildTasks[] = "importmodulestask"; |
|
350
|
|
|
//$buildTasks[] = "deleteobsoletemoduleowners"; |
|
|
|
|
|
|
351
|
|
|
return $buildTasks; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* executes build task: ImportModulesTask |
|
357
|
|
|
* |
|
358
|
|
|
*/ |
|
359
|
|
|
public function importmodulestask($request) |
|
360
|
|
|
{ |
|
361
|
|
|
$buildTask = new ImportModulesTask($request); |
|
|
|
|
|
|
362
|
|
|
$buildTask->run($request); |
|
363
|
|
|
$this->owner->displayCompletionMessage($buildTask); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
|
|
367
|
|
|
public function deleteobsoletemoduleowners() |
|
368
|
|
|
{ |
|
369
|
|
|
$this->runTask("ImportModulesTask", $request); |
|
|
|
|
|
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
|
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.