/
controller.php
167 lines (147 loc) · 5.19 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Concrete\Block\FeatureLink;
use Concrete\Core\Application\Service\FileManager;
use Concrete\Core\Block\BlockController;
use Concrete\Core\Feature\Features;
use Concrete\Core\Feature\UsesFeatureInterface;
use Concrete\Core\File\File;
use Concrete\Core\Form\Service\DestinationPicker\DestinationPicker;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Theme\Theme;
use Concrete\Core\Permission\Checker;
use HtmlObject\Link;
defined('C5_EXECUTE') or die('Access Denied.');
class Controller extends BlockController implements UsesFeatureInterface
{
public $helpers = ['form'];
public $buttonInternalLinkCID;
public $buttonExternalLink;
public $buttonFileLinkID;
public $buttonText;
public $buttonSize;
public $buttonStyle;
protected $btDefaultSet = 'basic';
protected $btInterfaceWidth = 640;
protected $btInterfaceHeight = 500;
protected $btTable = 'btFeatureLink';
protected $btCacheBlockRecord = true;
protected $btCacheBlockOutput = true;
protected $btCacheBlockOutputOnPost = true;
protected $btCacheBlockOutputForRegisteredUsers = true;
protected $btCacheBlockOutputLifetime = 300;
/**
* {@inheritdoc}
*/
public function getBlockTypeName()
{
return t('Feature Link');
}
/**
* {@inheritdoc}
*/
public function getBlockTypeDescription()
{
return t('Add a title, body and a button/link to a page. Useful for calling out important links.');
}
/**
* @return string[]
*/
protected function getImageLinkPickers()
{
return [
'none',
'page',
'file',
'external_url' => ['maxlength' => 255],
];
}
/**
* {@inheritdoc}
*/
public function getRequiredFeatures(): array
{
return [
Features::BASICS,
];
}
public function add()
{
$this->set('titleFormat', 'h2');
$this->edit();
}
public function edit()
{
$theme = Theme::getSiteTheme();
$this->set('editor', $this->app->make('editor'));
$this->set('destinationPicker', $this->app->make(DestinationPicker::class));
$this->set('imageLinkPickers', $this->getImageLinkPickers());
$this->set('themeColorCollection', $theme->getColorCollection());
if ($this->buttonInternalLinkCID) {
$this->set('imageLinkHandle', 'page');
$this->set('imageLinkValue', $this->buttonInternalLinkCID);
} elseif ($this->buttonFileLinkID) {
$this->set('imageLinkHandle', 'file');
$this->set('imageLinkValue', $this->buttonFileLinkID);
} elseif ((string) $this->buttonExternalLink !== '') {
$this->set('imageLinkHandle', 'external_url');
$this->set('imageLinkValue', $this->buttonExternalLink);
} else {
$this->set('imageLinkHandle', 'none');
$this->set('imageLinkValue', null);
}
}
/**
* @TODO - move all this logic into the DestinationPicker somehow. Make the destination picker save
* its object into some kind of special destination object. Refactor destinationpicker into
* vue component.
*
* @return string
*/
public function getLinkURL()
{
$linkUrl = '';
if (!empty($this->externalLink)) {
$sec = $this->app->make('helper/security');
$linkUrl = $sec->sanitizeURL($this->externalLink);
} elseif (!empty($this->internalLinkCID)) {
$linkToC = Page::getByID($this->internalLinkCID);
if (is_object($linkToC) && !$linkToC->isError()) {
$linkUrl = $linkToC->getCollectionLink();
}
} elseif (!empty($this->fileLinkID)) {
$fileLinkObject = File::getByID($this->fileLinkID);
if (is_object($fileLinkObject)) {
$linkUrl = $fileLinkObject->getRelativePath();
}
}
return $linkUrl;
}
public function view()
{
if ($this->buttonText) {
$button = new Link($this->getLinkURL(), $this->buttonText);
$theme = Theme::getSiteTheme();
if ($theme && $theme->supportsFeature(Features::TYPOGRAPHY)) {
$button->addClass('btn');
$styleClass = '';
if ($this->buttonStyle == 'outline') {
$styleClass = 'outline-';
}
$colorClass = 'btn-' . $styleClass . $this->buttonColor;
$button->addClass($colorClass);
if ($this->buttonSize) {
$button->addClass('btn-' . $this->buttonSize);
}
}
$this->set('button', $button);
}
}
public function save($args)
{
list($imageLinkType, $imageLinkValue) = $this->app->make(DestinationPicker::class)->decode('imageLink', $this->getImageLinkPickers(), null, null, $args);
$args['buttonInternalLinkCID'] = $imageLinkType === 'page' ? $imageLinkValue : 0;
$args['buttonFileLinkID'] = $imageLinkType === 'file' ? $imageLinkValue : 0;
$args['buttonExternalLink'] = $imageLinkType === 'external_url' ? $imageLinkValue : '';
parent::save($args);
}
}