/
controller.php
121 lines (105 loc) · 3.32 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
<?php
namespace Concrete\Attribute\CalendarEvent;
use Concrete\Core\Attribute\FontAwesomeIconFormatter;
use Concrete\Core\Calendar\Event\Formatter\LinkFormatterInterface;
use Concrete\Core\Entity\Attribute\Value\Value\NumberValue;
use Concrete\Core\Calendar\Calendar;
use Concrete\Core\Calendar\Event\Event;
use Concrete\Core\Entity\Calendar\CalendarEvent;
use Concrete\Core\Attribute\Context\BasicFormContext;
class Controller extends \Concrete\Attribute\Number\Controller
{
protected $helpers = ['form'];
protected $calendar;
public function getIconFormatter()
{
return new FontAwesomeIconFormatter('calendar-check-o');
}
/**
* @param $value CalendarEvent
*/
public function createAttributeValue($value)
{
$av = new NumberValue();
$av->setValue($value->getID());
return $av;
}
public function getSearchIndexValue()
{
$value = $this->getAttributeValue()->getValueObject();
if ($value) {
return intval($value->getValue());
}
}
public function getPlainTextValue()
{
$value = $this->getValue();
/**
* @var $linkFormatter LinkFormatterInterface
*/
$linkFormatter = \Core::make('calendar/event/formatter/link');
$event = Event::getByID($value);
if ($event) {
$text = $event->getName();
$url = $linkFormatter->getEventFrontendViewLink($event);
if ($url) {
$text .= ' (' . $url . ')';
}
return $text;
}
}
public function createAttributeValueFromRequest()
{
$data = $this->post();
$event = Event::getByID($data['eventID']);
if (is_object($event)) {
return $this->createAttributeValue($event);
} else {
$av = new NumberValue();
$av->setValue(0);
return $av;
}
}
public function getValue()
{
$value = $this->getAttributeValue()->getValueObject();
if ($value) {
return Event::getByID(intval($value->getValue()));
}
}
public function form()
{
$event = null;
if (is_object($this->attributeValue)) {
$event = $this->getValue();
}
if (!$event) {
if ($this->request->query->has($this->attributeKey->getAttributeKeyHandle())) {
$event = Event::getByID(intval($this->request->query->get($this->attributeKey->getAttributeKeyHandle())));
}
}
if ($event) {
$this->set('eventID', $event->getID());
$this->set('calendarID', $event->getCalendar()->getID());
$this->set('event', $event);
}
$this->requireAsset('core/calendar/event-selector');
$calendars = ['' => t('** Choose a Calendar')];
foreach (Calendar::getList() as $calendar) {
$calendars[$calendar->getID()] = $calendar->getName();
}
$this->set('calendars', $calendars);
}
public function search()
{
$this->form();
$v = $this->getView();
$v->render();
}
public function searchForm($list)
{
$eventID = (int) ($this->request('eventID'));
$list->filterByAttribute($this->attributeKey->getAttributeKeyHandle(), $eventID, '=');
return $list;
}
}