RecipeCategoryPage::getRecipeList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\RecipeBook\Page;
4
5
use SilverStripe\Dev\Debug;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\NumericField;
8
use SilverStripe\ORM\DB;
9
10
class RecipeCategoryPage extends \Page
11
{
12
    /**
13
     * @var string
14
     */
15
    private static $singular_name = 'Recipe Category';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
16
17
    /**
18
     * @var string
19
     */
20
    private static $plural_name = 'Recipe Categories';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
21
22
    /**
23
     * @var string
24
     */
25
    private static $description = 'A category for recipes.';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
26
27
    /**
28
     * @var array
29
     */
30
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
31
        'RecipesPerPage' => 'Int',
32
    ];
33
34
    /**
35
     * @var array
36
     */
37
    private static $belongs_many_many = [
0 ignored issues
show
introduced by
The private property $belongs_many_many is not used, and could be removed.
Loading history...
38
        'Recipes' => RecipePage::class,
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
45
        'RecipesPerPage' => 9,
46
    ];
47
48
    /**
49
     * @var array
50
     */
51
    private static $allowed_children = [
0 ignored issues
show
introduced by
The private property $allowed_children is not used, and could be removed.
Loading history...
52
        RecipeCategoryPage::class,
53
        RecipePage::class,
54
    ];
55
56
    /**
57
     * @var string
58
     */
59
    private static $table_name = 'RecipeCategoryPage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
60
61
    public function getCMSFields()
62
    {
63
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
64
            $fields->addFieldToTab(
65
                'Root.Main',
66
                NumericField::create('RecipesPerPage')
67
                    ->setTitle(_t(__CLASS__ . '.RecipesPerPage', 'Recipes Per Page')),
68
                'Content'
69
            );
70
        });
71
72
        $fields = parent::getCMSFields();
73
74
        $fields->removeByName([
75
            'Sidebar',
76
        ]);
77
78
        return $fields;
79
    }
80
81
    public function getRecipeList()
82
    {
83
        $categories = RecipeCategoryPage::get()->filter('ParentID', $this->data()->ID)->column('ID');
84
        $categories[] = $this->data()->ID;
85
86
        $recipes = RecipePage::get()
87
            ->filterAny([
88
                'ParentID' => $categories,
89
                'Categories.ID' => $categories,
90
            ]);
91
92
        return $recipes;
93
    }
94
95
    /**
96
     * @return \SilverStripe\ORM\DataList
97
     */
98
    public function getFeaturedRecipes()
99
    {
100
        $recipes = $this->getRecipeList()
101
            ->sort('Weight DESC')
102
            ->limit(15);
103
104
        $random = DB::get_conn()->random();
105
        return $recipes->sort($random);
106
    }
107
}
108