Completed
Push — master ( 3b65eb...dc665d )
by Nicolaas
11:00 queued 02:51
created

code/cms/dev/EcommerceDatabaseAdmin.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
4
/**
5
 * One stop shop for massaging e-commerce related data
6
 * AND running tests.
7
 *
8
 * You can customise this menu by "decorating" this class
9
 * and adding the method: "updateEcommerceDevMenu".
10
 *
11
 * Here is an example:
12
13
 <code php>
14
 <?php
15
16
 ####################### in mysite/code/tasks/MyMigration.php
17
18
 class MyMigration extends BuildTask {
19
20
 protected $title = "Mysite Database Fixes";
21
22
 protected $description = "General DB fixes";
23
24
 function run(SS_HTTPRequest $request) {
25
 DB::query("TRUNCATE TABLE MyUselessTable;");
26
 }
27
28
 }
29
30
 class MyMigration_EXT extends Extension {
31
32
 private static $allowed_actions = array(
33
 "mymigration" => true
34
 );
35
36
 //NOTE THAT updateEcommerceDevMenuConfig adds to Config options
37
 //but you can als have: updateEcommerceDevMenuDebugActions, or updateEcommerceDevMenuMaintenanceActions
38
 function updateEcommerceDevMenuConfig($buildTasks){
39
 $buildTasks[] = "mymigration";
40
 return $buildTasks;
41
 }
42
43
 function mymigration(SS_HTTPRequest $request){
44
 $this->owner->runTask("MyMigration", $request);
45
 }
46
47
 }
48
49
50
 ####################### in mysite/_config.php:
51
52
 Object::add_extension("EcommerceDatabaseAdmin", "MyMigration_EXT");
53
54
55
 </code>
56
57
 *
58
 * SECTIONS
59
 *
60
 * 0. check settings
61
 * 1. ecommerce setup (default records)
62
 * 2. data review
63
 * 3. regular maintance
64
 * 4. debug
65
 * 5. migration
66
 * 6. reset
67
 * 7. tests
68
 *
69
 * @todo: work out a standard "silent" option and a display option the "display" options shows all output when running it from ecommerce/dev/
70
 * We also have to work out an easy way to extend this.
71
 *
72
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
73
 * @package: ecommerce
74
 * @sub-package: cms
75
 * @inspiration: Silverstripe Ltd, Jeremy
76
 **/
77
class EcommerceDatabaseAdmin extends TaskRunner
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...
78
{
79
80
81
    //##############################
82
    // BASIC FUNCTIONS
83
    //##############################
84
85
    public function index()
86
    {
87
        if (Director::is_cli()) {
88
            echo "SILVERSTRIPE ECOMMERCE TOOLS: Tasks\n--------------------------\n\n";
89
            foreach ($tasks as $task) {
90
                echo " * $task[title]: sake dev/tasks/".$task['class']."\n";
91
            }
92
        } else {
93
            $renderer = new DebugView_EcommerceDatabaseAdmin();
94
            $renderer->writeHeader();
95
            $renderer->writeInfo('SilverStripe Ecommerce Tools', Director::absoluteBaseURL());
96
            $renderer->writeContent($this);
97
            $renderer->writeFooter();
98
        }
99
    }
100
101
    /**
102
     * standard, required method.
103
     *
104
     * @param string $action
105
     *
106
     * @return string link for the "Controller"
107
     */
108
    public function Link($action = null)
109
    {
110
        return Controller::join_links(
111
            Director::BaseURL(),
112
            'dev/ecommerce/',
113
            $action
114
        );
115
    }
116
117
    //##############################
118
    // 0. OVERALL CONFIG
119
    //##############################
120
121
    /**
122
     * List of overall configuration BuildTasks.
123
     *
124
     * @var array
125
     */
126
    protected $overallconfig = array(
127
        'ecommercetaskcheckconfiguration',
128
        'ecommercetaskapiandmore',
129
    );
130
131
    /**
132
     * list of config tasks.
133
     *
134
     * @return ArrayList
135
     */
136
    public function OverallConfig()
137
    {
138
        return $this->createMenuDOSFromArray($this->overallconfig, $type = 'Config');
139
    }
140
141
    //##############################
142
    // 1. ECOMMERCE SETUP (DEFAULT RECORDS)
143
    //##############################
144
145
    /**
146
     * List of setup BuildTasks.
147
     *
148
     * @var array
149
     */
150
    protected $ecommerceSetup = array(
151
        'ecommercetasksetorderidstartingnumber',
152
        'ecommercetaskcreatemembergroups',
153
        'ecommercetaskdefaultrecords',
154
        'ecommercetaskcountryandregion',
155
        'ecommercetaskcountryandregion_disallowallcountries',
156
        'ecommercetaskcountryandregion_allowallcountries',
157
        'ecommercetaskadddefaultproducts',
158
        'ecommercetasklinkproductwithimages',
159
    );
160
161
    /**
162
     * list of data setup tasks.
163
     *
164
     * @return ArrayList
165
     */
166
    public function EcommerceSetup()
167
    {
168
        return $this->createMenuDOSFromArray($this->ecommerceSetup, $type = 'EcommerceSetup');
169
    }
170
171
    //##############################
172
    // 2. DATA REVIEW MAINTENANCE
173
    //##############################
174
175
    /**
176
     * List of regular maintenance BuildTasks.
177
     *
178
     * @var array
179
     */
180
    protected $dataReview = array(
181
        'ecommercetaskreviewreports',
182
        'ecommercetaskreviewsearches',
183
        'ecommercetaskorderitemspercustomer'
184
    );
185
186
    /**
187
     * regular data cleanup tasks.
188
     *
189
     * @return ArrayList
190
     */
191
    public function DataReview()
192
    {
193
        return $this->createMenuDOSFromArray($this->dataReview, $type = 'DataReview');
194
    }
195
196
    //##############################
197
    // 3. REGULAR MAINTENANCE
198
    //##############################
199
200
    /**
201
     * List of regular maintenance BuildTasks.
202
     *
203
     * @var array
204
     */
205
    protected $regularMaintenance = array(
206
        'ecommercetaskcartcleanup',
207
        'ecommercetaskaddcustomerstocustomergroups',
208
        'ecommercetaskfixbrokenordersubmissiondata',
209
        'ecommercetaskcleanupproductfullsitetreesorting',
210
        'ecommercetaskproductvariationsfixes',
211
        'ecommercetaskproductimagereset',
212
        'ecommercetasktrytofinaliseorders',
213
        'ecommercetaskprocessorderqueue',
214
        'ecommercetaskarchiveallsubmittedorders',
215
        'ecommercetasklinkorderaddressesatbothends',
216
        'EcommerceTaskCleanupProducts',
217
        'EcommerceTaskOrdersWithoutOrderSteps'
218
    );
219
220
    /**
221
     * regular data cleanup tasks.
222
     *
223
     * @return ArrayList
224
     */
225
    public function RegularMaintenance()
226
    {
227
        return $this->createMenuDOSFromArray($this->regularMaintenance, $type = 'RegularMaintenance');
228
    }
229
230
    //##############################
231
    // 4. DEBUG ACTIONS
232
    //##############################
233
234
    /**
235
     * List of debug actions BuildTasks.
236
     *
237
     * @var array
238
     */
239
    protected $debugActions = array(
240
        'ecommercetasktemplatetest',
241
        'ecommercetaskcartmanipulation_current',
242
        'ecommercetaskcartmanipulation_debug',
243
        'ecommercetaskbuilding_model',
244
        'ecommercetaskbuilding_extending',
245
    );
246
247
    /**
248
     * list of data debug actions.
249
     *
250
     * @return ArrayList
251
     */
252
    public function DebugActions()
253
    {
254
        return $this->createMenuDOSFromArray($this->debugActions, $type = 'DebugActions');
255
    }
256
257
    //##############################
258
    // 5. MIGRATIONS
259
    //##############################
260
261
    /**
262
     * List of migration BuildTasks.
263
     *
264
     * @var array
265
     */
266
    protected $migrations = array(
267
        'ecommercetaskmigration',
268
        'ecommercetaskcheckconfiguration',
269
        'ecommercetasksetdefaultproductgroupvalues',
270
    );
271
272
    /**
273
     * list of migration tasks.
274
     *
275
     * @return ArrayList
276
     */
277
    public function Migrations()
278
    {
279
        return $this->createMenuDOSFromArray($this->migrations, $type = 'Migrations');
280
    }
281
282
    //##############################
283
    // 6. CRAZY SHIT
284
    //##############################
285
286
    /**
287
     * List of crazy shit BuildTasks.
288
     *
289
     * @var array
290
     */
291
    protected $crazyshit = array(
292
        'ecommercetaskdeleteallorders',
293
        'ecommercetaskdeleteproducts',
294
        'ecommercetaskarchiveallorderswithitems',
295
    );
296
297
    /**
298
     * list of crazy actions tasks.
299
     *
300
     * @return ArrayList
301
     */
302
    public function CrazyShit()
303
    {
304
        return $this->createMenuDOSFromArray($this->crazyshit, $type = 'CrazyShit');
305
    }
306
307
    //##############################
308
    // 7. TESTS
309
    //##############################
310
311
    /**
312
     * List of tests.
313
     *
314
     * @var array
315
     */
316
    protected $tests = array(
317
        //'ShoppingCartTest' => 'Shopping Cart'
318
    );
319
320
    public function Tests()
321
    {
322
        $arrayList = new ArrayList();
323
        foreach ($this->tests as $class => $name) {
324
            $arrayList->push(
325
                new ArrayData(
326
                    array(
327
                        'Name' => $name,
328
                        'Class' => $class,
329
                    )
330
                )
331
            );
332
        }
333
334
        return $arrayList;
335
    }
336
337
    /**
338
     * @return array ????
339
     */
340
    public function AllTests()
341
    {
342
        return implode(',', array_keys($this->tests));
343
    }
344
345
    //##############################
346
    // INTERNAL FUNCTIONS
347
    //##############################
348
349
    /**
350
     * @param array  $buildTasksArray array of build tasks
351
     * @param string $type
352
     *
353
     * @return ArrayList(ArrayData(Link, Title, Description))
354
     */
355
    protected function createMenuDOSFromArray(array $buildTasksArray, $type = '')
356
    {
357
        $extendedArray = $this->extend('updateEcommerceDevMenu'.$type, $buildTasksArray);
358
        if ($extendedArray !== null && is_array($extendedArray) && count($extendedArray)) {
359
            foreach ($extendedArray as $extendedBuildTasks) {
360
                $buildTasksArray = array_merge($buildTasksArray, $extendedBuildTasks);
361
            }
362
        }
363
        $buildTasksArray = array_unique($buildTasksArray);
364
        $arrayList = new ArrayList();
365
        foreach ($buildTasksArray as $buildTask) {
366
            $obj = new $buildTask();
367
            $do = new ArrayData(
368
                array(
369
                    'Link' => $this->Link($buildTask),
370
                    'Title' => $obj->getTitle(),
371
                    'Description' => $obj->getDescription(),
372
                )
373
            );
374
            $arrayList->push($do);
375
        }
376
377
        return $arrayList;
378
    }
379
380
    public function runTask($request)
381
    {
382
        $taskName = $request->param('TaskName');
383
        $renderer = new DebugView_EcommerceDatabaseAdmin();
384
        $renderer->writeHeader();
385
        $renderer->writeInfo('SilverStripe Ecommerce Tools', Director::absoluteBaseURL());
386
        $renderer->writePreOutcome();
387
        if (class_exists($taskName) && is_subclass_of($taskName, 'BuildTask')) {
388
            $title = singleton($taskName)->getTitle();
389
            if (Director::is_cli()) {
390
                echo "Running task '$title'...\n\n";
391
            } elseif (!Director::is_ajax()) {
392
                echo "<h1>Running task '$title'...</h1>\n";
393
            }
394
395
            $task = new $taskName();
396
            if ($task->isEnabled()) {
397
                $task->verbose = true;
398
                $task->run($request);
399
            } else {
400
                echo "<p>{$title} is disabled</p>";
401
            }
402
        } else {
403
            echo "Build task '$taskName' not found.";
404
            if (class_exists($taskName)) {
405
                echo "  It isn't a subclass of BuildTask.";
406
            }
407
            echo "\n";
408
        }
409
        $this->displayCompletionMessage($task);
410
        $renderer->writePostOutcome();
411
        $renderer->writeContent($this);
412
        $renderer->writeFooter();
413
    }
414
415
    /**
416
     * shows a "Task Completed Message" on the screen.
417
     *
418
     * @param BuildTask $buildTask
419
     * @param string    $extraMessage
420
     */
421
    protected function displayCompletionMessage(BuildTask $buildTask, $extraMessage = '')
422
    {
423
        DB::alteration_message('
424
425
            ------------------------------------------------------- <br />
426
            COMPLETED THE FOLLOWING TASK:<br />
427
            <strong>'.$buildTask->getTitle().'</strong><br />
428
            '.$buildTask->getDescription()." <br />
429
            ------------------------------------------------------- <br />
430
            $extraMessage
431
        ");
432
    }
433
}
434
435
class DebugView_EcommerceDatabaseAdmin extends DebugView
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...
436
{
437
    public function writePreOutcome()
438
    {
439
        echo "<div id='TaskHolder' style=\"background-color: #e8e8e8; border-radius: 15px; margin: 20px; padding: 20px\">";
440
    }
441
442
    public function writePostOutcome()
443
    {
444
        echo '</div>';
445
    }
446
447
    public function writeContent(Controller $controller)
448
    {
449
        echo $controller->RenderWith($controller->class);
450
    }
451
}
452