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

BlogFilter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

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

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