|
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'; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $plural_name = 'Recipe Categories'; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $description = 'A category for recipes.'; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $db = [ |
|
|
|
|
|
|
31
|
|
|
'RecipesPerPage' => 'Int', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $belongs_many_many = [ |
|
|
|
|
|
|
38
|
|
|
'Recipes' => RecipePage::class, |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $defaults = [ |
|
|
|
|
|
|
45
|
|
|
'RecipesPerPage' => 9, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
private static $allowed_children = [ |
|
|
|
|
|
|
52
|
|
|
RecipeCategoryPage::class, |
|
53
|
|
|
RecipePage::class, |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
private static $table_name = 'RecipeCategoryPage'; |
|
|
|
|
|
|
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
|
|
|
|