BlocksLeftAndMainExtension   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 61
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B doPublishBlocks() 0 51 8
A init() 0 2 1
1
<?php
2
3
namespace LeKoala\Blocks;
4
5
use SilverStripe\Admin\LeftAndMainExtension;
6
use SilverStripe\ORM\DB;
7
8
/**
9
 * Extend leftandmain to publish blocks
10
 *
11
 * @property \SilverStripe\Admin\LeftAndMain|\SilverStripe\Admin\ModelAdmin $owner
12
 */
13
class BlocksLeftAndMainExtension extends LeftAndMainExtension
14
{
15
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
16
        'doPublishBlocks'
17
    ];
18
19
    public function doPublishBlocks()
20
    {
21
        $owner = $this->owner;
22
        $ID = $owner->getRequest()->param('ID');
23
        $Page = BlocksPage::get()->byID($ID);
0 ignored issues
show
Bug introduced by
$ID of type string is incompatible with the type integer expected by parameter $id of SilverStripe\ORM\DataList::byID(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
        $Page = BlocksPage::get()->byID(/** @scrutinizer ignore-type */ $ID);
Loading history...
24
25
        Block::$auto_update_page = false;
26
27
        $DataCopy = [];
28
        $Blocks = $Page->Blocks();
29
        foreach ($Blocks as $Block) {
30
            $DataCopy[$Block->ID] = DB::query('SELECT BlockData FROM Block WHERE ID = ' . $Block->ID)->value();
31
            if (!$Block->BlockData) {
32
                $Block->BlockData = $DataCopy[$Block->ID];
33
            }
34
            $Block->write();
35
            // DB::prepared_query('UPDATE Block SET BlockData = ? WHERE ID = ' . $Block->ID, [$DataCopy[$Block->ID]]);
36
        }
37
        $Page->publishRecursive();
38
39
        // Publish in all other locales as well!
40
        if ($Page->has_extension("\\TractorCow\\Fluent\\Extension\\FluentExtension")) {
41
            $state = \TractorCow\Fluent\State\FluentState::singleton();
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\State\FluentState was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
            $currentLocale = $state->getLocale();
43
            $allLocales = \TractorCow\Fluent\Model\Locale::get()->exclude('Locale', $currentLocale);
0 ignored issues
show
Bug introduced by
The type TractorCow\Fluent\Model\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
            foreach ($allLocales as $locale) {
45
                $state->withState(function ($state) use ($locale, $Page, $DataCopy) {
46
                    $state->setLocale($locale->Locale);
47
48
                    foreach ($Page->Blocks() as $Block) {
49
                        if (!$Block->BlockData) {
50
                            $Block->BlockData = $DataCopy[$Block->ID];
51
                        }
52
                        $Block->write();
53
                    }
54
55
                    $Page->publishRecursive();
56
                });
57
            }
58
59
            // Preserve original data
60
            // TODO: understand why Data is emptied
61
            foreach ($Blocks as $Block) {
62
                DB::prepared_query('UPDATE Block SET BlockData = ? WHERE ID = ' . $Block->ID, [$DataCopy[$Block->ID]]);
63
            }
64
        }
65
66
        $message = "Blocks published";
67
68
        $owner->getResponse()->addHeader('X-Status', rawurlencode($message));
69
        return $owner->getResponseNegotiator()->respond($owner->getRequest());
70
    }
71
72
    public function init()
73
    {
74
    }
75
}
76