Completed
Pull Request — master (#421)
by Robbie
12:12
created

BlogFilter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 11
dl 0
loc 94
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B stageChildren() 0 25 6
A subclassForBlog() 0 4 1
A liveChildren() 0 17 4
A isBlog() 0 4 1
A updateCMSFields() 0 22 2
1
<?php
2
3
namespace SilverStripe\Blog\Model;
4
5
use SilverStripe\Blog\Model\Blog;
6
use SilverStripe\Blog\Model\BlogPost;
7
use SilverStripe\Core\ClassInfo;
8
use SilverStripe\Core\Convert;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\FormTransformation;
11
use SilverStripe\Forms\GridField\GridField;
12
use SilverStripe\Forms\Tab;
13
use SilverStripe\Lumberjack\Model\Lumberjack;
14
use SilverStripe\ORM\DataObject;
15
use SilverStripe\ORM\FieldType\DBDatetime;
16
use SilverStripe\ORM\Versioning\Versioned;
17
use SilverStripe\Security\Permission;
18
19
/**
20
 * This class is responsible for filtering the SiteTree when necessary and also overlaps into
21
 * filtering only published posts.
22
 *
23
 * @package silverstripe
24
 * @subpackage blog
25
 */
26
class BlogFilter extends Lumberjack
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function stageChildren($showAll = false)
32
    {
33
        $staged = parent::stageChildren($showAll);
34
35
        if (!$this->shouldFilter() && $this->subclassForBlog() && !Permission::check('VIEW_DRAFT_CONTENT')) {
36
            $stage = Versioned::get_stage();
37
38
            if ($stage == 'Stage') {
39
                $stage = '';
40
            } elseif ($stage) {
41
                $stage = '_' . $stage;
42
            }
43
44
            $dataQuery = $staged->dataQuery()
45
                ->innerJoin(
46
                    DataObject::getSchema()->tableName(BlogPost::class),
47
                    sprintf('"BlogPost%s"."ID" = "SiteTree%s"."ID"', $stage, $stage)
48
                )
49
                ->where(sprintf('"PublishDate" < \'%s\'', Convert::raw2sql(DBDatetime::now())));
50
51
            $staged = $staged->setDataQuery($dataQuery);
52
        }
53
54
        return $staged;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    protected function subclassForBlog()
61
    {
62
        return in_array(get_class($this->owner), ClassInfo::subclassesFor('SilverStripe\\Blog\\Model\\Blog'));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function liveChildren($showAll = false, $onlyDeletedFromStage = false)
69
    {
70
        $staged = parent::liveChildren($showAll, $onlyDeletedFromStage);
71
72
        if (!$this->shouldFilter() && $this->isBlog() && !Permission::check('VIEW_DRAFT_CONTENT')) {
73
            $dataQuery = $staged->dataQuery()
74
                ->innerJoin(
75
                    DataObject::getSchema()->tableName(BlogPost::class),
76
                    '"BlogPost_Live"."ID" = "SiteTree_Live"."ID"'
77
                )
78
                ->where(sprintf('"PublishDate" < \'%s\'', Convert::raw2sql(DBDatetime::now())));
79
80
            $staged = $staged->setDataQuery($dataQuery);
81
        }
82
83
        return $staged;
84
    }
85
86
    /**
87
     * @return bool
88
     */
89
    protected function isBlog()
90
    {
91
        return $this->owner instanceof Blog;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function updateCMSFields(FieldList $fields)
98
    {
99
        $excluded = $this->owner->getExcludedSiteTreeClassNames();
100
101
        if (!empty($excluded)) {
102
            $pages = BlogPost::get()->filter(array(
103
                'ParentID' => $this->owner->ID,
104
                'ClassName' => $excluded
105
            ));
106
107
            $gridField = BlogFilter_GridField::create(
108
                'ChildPages',
109
                $this->getLumberjackTitle(),
110
                $pages,
111
                $this->getLumberjackGridFieldConfig()
112
            );
113
114
            $tab = Tab::create('ChildPages', $this->getLumberjackTitle(), $gridField);
115
116
            $fields->insertBefore($tab, 'Main');
0 ignored issues
show
Documentation introduced by
'Main' is of type string, but the function expects a object<SilverStripe\Forms\FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
        }
118
    }
119
}
120
121
122
/**
123
 * Enables children of non-editable pages to be edited.
124
 */
125
class BlogFilter_GridField extends GridField
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...
126
{
127
    /**
128
     * @param FormTransformation $transformation
129
     *
130
     * @return $this
131
     */
132
    public function transform(FormTransformation $transformation)
133
    {
134
        return $this;
135
    }
136
}
137