BlogFilter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

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