IsPublishedFilter::excludeOne()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Sunnnysideup\SearchOnVersionStatus\Search;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\ORM\DataQuery;
7
use SilverStripe\ORM\DB;
8
use SilverStripe\ORM\Filters\SearchFilter;
9
use SilverStripe\Versioned\Versioned;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Versioned\Versioned 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...
10
11
class IsPublishedFilter extends SearchFilter
12
{
13
    protected function applyOne(DataQuery $query)
14
    {
15
        $sql = '';
16
        $array = [0 => 0];
17
        $schema = DataObject::getSchema();
18
        $className = $query->dataClass();
19
        $baseTable = $schema->baseDataTable($className);        
20
        switch ($this->getValue()) {
21
            
22
            case 'MODIFIED':
23
                $className = $query->dataClass();
24
                $objects = $className::get();
25
                foreach ($objects as $obj) {
26
                    if ($obj->isModifiedOnDraft() && $obj->isPublished()) {
27
                        $array[$obj->ID] = $obj->ID;
28
                    }
29
                }
30
31
                break;
32
            
33
            case Versioned::LIVE:
34
                $sql = 'SELECT "ID" FROM "'.$baseTable.'_Live"';
35
36
                break;
37
            
38
            case Versioned::DRAFT:
39
                $sql = '
40
                    SELECT "'.$baseTable.'"."ID"
41
                    FROM "'.$baseTable.'"
42
                    LEFT JOIN "'.$baseTable.'_Live" ON "'.$baseTable.'_Live"."ID" = "'.$baseTable.'"."ID"
43
                    WHERE "'.$baseTable.'_Live"."ID" IS NULL';
44
45
                break;
46
            
47
            case 'DRAFT_ERROR':
48
                $sql = '
49
                    SELECT "'.$baseTable.'_Live"."ID"
50
                    FROM "'.$baseTable.'_Live"
51
                    LEFT JOIN '.$baseTable.' ON '.$baseTable.'_Live.ID = '.$baseTable.'.ID
52
                    WHERE '.$baseTable.'.ID IS NULL';
53
54
                break;
55
56
            case 'PUBLISHED_CLEAN':
57
                $objects = $className::get();
58
                foreach ($objects as $obj) {
59
                    if (!$obj->isModifiedOnDraft() && $obj->isPublished()) {
60
                        $array[$obj->ID] = $obj->ID;
61
                    }
62
                }                
63
                break;
64
            default:
65
                return $query;
66
        }
67
68
        if ($sql) {
69
            $rows = DB::query($sql);
70
            foreach ($rows as $row) {
71
                $array[$row['ID']] = $row['ID'];
72
            }
73
        }
74
75
        return $query->where("\"{$baseTable}\".\"ID\" IN (" . implode(',', $array) . ')');
76
    }
77
78
    protected function excludeOne(DataQuery $query)
79
    {
80
        $this->model = $query->applyRelation($this->relation);
81
        $predicate = sprintf('NOT MATCH (%s) AGAINST (?)', $this->getDbName());
82
83
        return $query->where([$predicate => $this->getValue()]);
84
    }
85
}
86