|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Services\TwigBridge\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Banner; |
|
6
|
|
|
use Coyote\Http\Factories\CacheFactory; |
|
7
|
|
|
use Coyote\Repositories\Contracts\BlockRepositoryInterface as BlockRepository; |
|
8
|
|
|
use Coyote\Repositories\Contracts\CampaignRepositoryInterface as CampaignRepository; |
|
9
|
|
|
use Coyote\Repositories\Contracts\WikiRepositoryInterface as WikiRepository; |
|
10
|
|
|
use Coyote\Block as BlockModel; |
|
11
|
|
|
use Coyote\Campaign as CampaignModel; |
|
12
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
|
13
|
|
|
use Illuminate\Support\Collection; |
|
14
|
|
|
use Twig_Extension; |
|
15
|
|
|
use Twig_SimpleFunction; |
|
16
|
|
|
|
|
17
|
|
|
class Block extends Twig_Extension |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
use CacheFactory; |
|
20
|
|
|
|
|
21
|
|
|
private Filesystem $filesystem; |
|
22
|
|
|
private CampaignRepository $campaignRepository; |
|
23
|
|
|
private BlockRepository $blockRepository; |
|
24
|
|
|
private WikiRepository $wikiRepository; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(Filesystem $filesystem, BlockRepository $blockRepository, CampaignRepository $campaignRepository, WikiRepository $wikiRepository) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->campaignRepository = $campaignRepository; |
|
29
|
|
|
$this->blockRepository = $blockRepository; |
|
30
|
|
|
$this->filesystem = $filesystem; |
|
31
|
|
|
$this->wikiRepository = $wikiRepository; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritDoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getName() |
|
38
|
|
|
{ |
|
39
|
|
|
return 'TwigBridge_Extension_Block'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return Twig_SimpleFunction[] |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getFunctions() |
|
46
|
|
|
{ |
|
47
|
|
|
return [ |
|
48
|
|
|
/** |
|
49
|
|
|
* Read the block content from database (or cache) |
|
50
|
|
|
*/ |
|
51
|
|
|
new Twig_SimpleFunction('render_region', [&$this, 'renderRegion'], ['is_safe' => ['html']]), |
|
|
|
|
|
|
52
|
|
|
new Twig_SimpleFunction('render_block', [&$this, 'renderBlock'], ['is_safe' => ['html']]), |
|
|
|
|
|
|
53
|
|
|
new Twig_SimpleFunction('render_help_context', [&$this, 'renderHelpContext'], ['is_safe' => ['html']]) |
|
|
|
|
|
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function renderRegion($name) |
|
62
|
|
|
{ |
|
63
|
|
|
/** @var \Coyote\Block $block */ |
|
64
|
|
|
$blocks = $this->getBlocks()->where('region', $name); |
|
65
|
|
|
$html = ''; |
|
66
|
|
|
|
|
67
|
|
|
foreach ($blocks as $block) { |
|
68
|
|
|
$html .= $this->renderBlock($block); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$campaigns = $this->getCampagins()->where('region', $name); |
|
72
|
|
|
|
|
73
|
|
|
foreach ($campaigns as $campaign) { |
|
74
|
|
|
$html .= $this->renderCampaign($campaign); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $html; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $name |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function renderBlock(BlockModel $block) |
|
85
|
|
|
{ |
|
86
|
|
|
if (!$block || !$block->is_enabled || !$this->shouldDisplayForSponsor($block) || !$this->shouldDisplayForPrivilegeUsers($block)) { |
|
|
|
|
|
|
87
|
|
|
return ''; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $block->content; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function renderCampaign(CampaignModel $campaign): string |
|
94
|
|
|
{ |
|
95
|
|
|
if (!$campaign->is_enabled || !$this->shouldDisplayForSponsor($campaign) || !$this->shouldDisplayForPrivilegeUsers($campaign)) { |
|
96
|
|
|
return ''; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** @var Banner $banner */ |
|
100
|
|
|
$banner = $campaign->banners->first(); |
|
101
|
|
|
|
|
102
|
|
|
if (!$banner) { |
|
|
|
|
|
|
103
|
|
|
return ''; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$banner->increment('impressions'); |
|
107
|
|
|
$html = app('html'); |
|
108
|
|
|
|
|
109
|
|
|
return (string) $html->link($banner->url, $html->image($this->filesystem->url($banner->filename)), ['class' => 'revive', 'target' => '_blank'], null, false); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function shouldDisplayForSponsor($block): bool |
|
113
|
|
|
{ |
|
114
|
|
|
if ($block->enable_sponsor || auth()->guest()) { |
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return !auth()->user()->is_sponsor; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function shouldDisplayForPrivilegeUsers($block): bool |
|
122
|
|
|
{ |
|
123
|
|
|
if (!$block->max_reputation || auth()->guest()) { |
|
124
|
|
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return auth()->user()->reputation < $block->max_reputation; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param string $helpId |
|
132
|
|
|
* @param \Coyote\Wiki $wiki |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
|
|
public function renderHelpContext($helpId, $wiki) |
|
136
|
|
|
{ |
|
137
|
|
|
$children = $this->wikiRepository->children($helpId); |
|
|
|
|
|
|
138
|
|
|
$html = '<ul>'; |
|
139
|
|
|
|
|
140
|
|
|
foreach ($children as $idx => $row) { |
|
141
|
|
|
$depth = $row['depth']; |
|
142
|
|
|
$nextDepth = isset($children[$idx + 1]) ? $children[$idx + 1]['depth'] : 1; |
|
143
|
|
|
|
|
144
|
|
|
$link = link_to($row['path'], $row['title'], $row['id'] == $wiki->id ? ['class' => 'active'] : []); |
|
145
|
|
|
|
|
146
|
|
|
if ($nextDepth > $depth) { |
|
147
|
|
|
$html .= '<li>' . $link . "<ul>"; |
|
148
|
|
|
} else { |
|
149
|
|
|
$html .= '<li>' . $link . "</li>"; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if ($nextDepth < $depth) { |
|
153
|
|
|
while ($nextDepth < $depth) { |
|
154
|
|
|
$html .= "\n</ul></li>"; |
|
155
|
|
|
--$depth; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$html .= '</ul>'; |
|
161
|
|
|
|
|
162
|
|
|
return $html; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return Collection |
|
167
|
|
|
*/ |
|
168
|
|
|
private function getBlocks() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->getCacheFactory()->rememberForever('blocks', function () { |
|
171
|
|
|
return $this->blockRepository->all(['name', 'is_enabled', 'content', 'region', 'max_reputation', 'enable_sponsor']); |
|
172
|
|
|
}); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @return Collection|\Coyote\Campaign[] |
|
177
|
|
|
*/ |
|
178
|
|
|
private function getCampagins() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->campaignRepository->campaigns(); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|