ObjectsUpdated::classes_edited()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 3
b 0
f 0
nc 3
nop 1
dl 0
loc 17
rs 10
1
<?php
2
3
namespace Sunnysideup\SimpleTemplateCaching\Model;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Forms\ReadonlyField;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\DB;
9
10
/**
11
 * A blog category for generalising blog posts.
12
 *
13
 * @property string $ClassNameLastEdited
14
 */
15
class ObjectsUpdated extends DataObject
16
{
17
18
    public static function classes_edited(?string $myClass = ''): string
19
    {
20
        $query = DB::query('
21
            SELECT "ClassNameLastEdited", COUNT(*) AS "Count"
22
            FROM "ObjectsUpdated"
23
            GROUP BY "ClassNameLastEdited"
24
            ORDER BY "ClassNameLastEdited" ASC
25
        ');
26
        foreach ($query as $row) {
27
            $array[$row['ClassNameLastEdited']] =
28
                Injector::inst()->get($row['ClassNameLastEdited'])->i18n_singular_name() .
29
                ' (×' . $row['Count'] . ')';
30
            if ($myClass && $myClass === $row['ClassNameLastEdited']) {
31
                return $row['Count'];
32
            }
33
        }
34
        return '<br />' . implode('<br> ', $array);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $array seems to be defined by a foreach iteration on line 26. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
35
    }
36
37
    /**
38
     * @var string
39
     */
40
    private static $table_name = 'ObjectsUpdated';
41
42
    /**
43
     * @var array
44
     */
45
    private static $db = [
46
        'ClassNameLastEdited' => 'Varchar(255)',
47
    ];
48
49
    private static $default_sort = [
50
        'ID' => 'DESC',
51
    ];
52
53
    /**
54
     * @var array
55
     */
56
    private static $summary_fields = [
57
        'LastEdited.Ago' => 'Updated',
58
        'Title' => 'Record name',
59
    ];
60
61
    /**
62
     * @var array
63
     */
64
    private static $field_labels = [
65
        'Created' => 'Updated',
66
        'Title' => 'Human readable name',
67
    ];
68
69
    /**
70
     * @var array
71
     */
72
    private static $casting = [
73
        'Title' => 'Varchar',
74
    ];
75
76
    public function getTitle(): string
77
    {
78
        if (class_exists((string) $this->ClassNameLastEdited)) {
79
            return Injector::inst()->get($this->ClassNameLastEdited)->i18n_singular_name();
80
        }
81
82
        return 'ERROR: class not found';
83
    }
84
85
    public function getCMSFields()
86
    {
87
        $fields = parent::getCMSFields();
88
        $fields->removeByName('ClassNameLastEdited');
89
        $fields->addFieldsToTab(
90
            'Root.Main',
91
            [
92
                ReadonlyField::create('Created', 'Recorded'),
93
                ReadonlyField::create('Title', 'Title', $this->getTitle())
94
                    ->setDescription('Included ' . self::classes_edited($this->ClassNameLastEdited) . '× in the list.'),
95
            ]
96
        );
97
        return $fields;
98
    }
99
}
100