ResetGloballyEnabledElements   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 16
c 1
b 0
f 0
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 19 5
1
<?php
2
3
namespace DNADesign\ElementalVirtual\Tasks;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Dev\BuildTask;
7
use SilverStripe\Core\ClassInfo;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\ORM\DB;
10
11
class ResetGloballyEnabledElements extends BuildTask
12
{
13
    protected $title = 'Reset Globally Enabled elements';
14
15
    protected $description = 'Reset individual elements \'AvailableGlobally\' setting with the default_global_elements config';
16
17
    public function run($request)
18
    {
19
        // get all classes of BaseElement
20
        $elementClasses = ClassInfo::subclassesFor(BaseElement::class);
21
        $default = Config::inst()->get(BaseElement::class, 'default_global_elements') ? 1 : 0;
22
23
        // first update all to the default
24
        DB::query("UPDATE Element SET AvailableGlobally = $default");
25
        DB::query("UPDATE Element_Live SET AvailableGlobally = $default");
26
        DB::query("UPDATE Element_Versions SET AvailableGlobally = $default");
27
28
        foreach ($elementClasses as $class) {
29
            $isGlobal = Config::inst()->get($class, 'default_global_elements') ? 1 : 0;
30
            $ids = $class::get()->getIDList();
31
            if (!empty($ids)) {
32
                $idStr = implode("','", $ids);
33
                DB::query("UPDATE Element SET AvailableGlobally = $isGlobal WHERE ID IN ('$idStr')");
34
                DB::query("UPDATE Element_Live SET AvailableGlobally = $isGlobal WHERE ID IN ('$idStr')");
35
                DB::query("UPDATE Element_Versions SET AvailableGlobally = $isGlobal WHERE RecordID IN ('$idStr')");
36
            }
37
        }
38
    }
39
}
40