/
controller.php
135 lines (120 loc) · 3.83 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace Concrete\Block\DateNavigation;
defined('C5_EXECUTE') or die("Access Denied.");
use Concrete\Core\Block\BlockController;
use Concrete\Core\Page\PageList;
use Concrete\Core\Page\Type\Type;
use Loader;
class Controller extends BlockController
{
public $helpers = ['form'];
protected $btInterfaceWidth = 400;
protected $btInterfaceHeight = 450;
protected $btExportPageColumns = ['cParentID', 'cTargetID'];
protected $btExportPageTypeColumns = ['ptID'];
protected $btTable = 'btDateNavigation';
public function getBlockTypeDescription()
{
return t("Displays a list of months to filter a page list by.");
}
public function getBlockTypeName()
{
return t("Date Navigation");
}
public function add()
{
$this->edit();
$this->set('maxResults', 3);
$this->set('title', t('Archives'));
}
public function edit()
{
$types = Type::getList();
$this->set('pagetypes', $types);
}
public function getDateLink($dateArray = null)
{
if ($this->cTargetID) {
$c = \Page::getByID($this->cTargetID);
} else {
$c = \Page::getCurrentPage();
}
if ($dateArray) {
return \URL::page($c, $dateArray['year'], $dateArray['month']);
} else {
return \URL::page($c);
}
}
public function getDateLabel($dateArray)
{
return \Punic\Calendar::getMonthName($dateArray['month'], 'wide', '', true).' '.$dateArray['year'];
}
public function getPassThruActionAndParameters($parameters)
{
if (Loader::helper("validation/numbers")->integer($parameters[0])) {
// then we're going to treat this as a year.
$method = 'action_filter_by_date';
$parameters[0] = intval($parameters[0]);
if (isset($parameters[1])) {
$parameters[1] = intval($parameters[1]);
}
} else {
$parameters = $method = null;
}
return [$method, $parameters];
}
public function action_filter_by_date($year = false, $month = false)
{
$this->selectedYear = $year;
$this->selectedMonth = $month;
$this->view();
}
public function isSelectedDate($dateArray)
{
if (isset($this->selectedYear) && isset($this->selectedMonth)) {
return $dateArray['year'] == $this->selectedYear && $dateArray['month'] == $this->selectedMonth;
}
}
public function view()
{
$pl = new PageList();
if ($this->ptID) {
$pl->filterByPageTypeID($this->ptID);
}
if ($this->cParentID) {
$pl->filterByParentID($this->cParentID);
}
$query = $pl->deliverQueryObject();
$query->select('date_format(cv.cvDatePublic, "%Y") as navYear, date_format(cv.cvDatePublic, "%m") as navMonth');
$query->groupBy('navYear, navMonth');
$query->orderBy('navYear', 'desc')->addOrderBy('navMonth', 'desc');
$r = $query->execute();
$dates = [];
while ($row = $r->fetch()) {
$dates[] = ['year' => $row['navYear'], 'month' => $row['navMonth']];
}
$this->set('dates', $dates);
}
public function save($data)
{
$data += [
'redirectToResults' => 0,
'cTargetID' => 0,
'filterByParent' => 0,
'cParentID' => 0,
'ptID' => 0,
];
if ($data['redirectToResults']) {
$data['cTargetID'] = intval($data['cTargetID']);
} else {
$data['cTargetID'] = 0;
}
if ($data['filterByParent']) {
$data['cParentID'] = intval($data['cParentID']);
} else {
$data['cParentID'] = 0;
}
$data['ptID'] = intval($data['ptID']);
parent::save($data);
}
}