Skip to content

Commit 614bec5

Browse files
committed
Implemented setting storage.
1 parent 8993f44 commit 614bec5

File tree

5 files changed

+280
-114
lines changed

5 files changed

+280
-114
lines changed

controller.php

Lines changed: 7 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,6 @@ class Controller extends Package
6060
*/
6161
protected $pkgAutoloaderMapCoreExtensions = false;
6262

63-
/**
64-
* Package class autoloader registrations
65-
* The package install helper class, included with this boilerplate,
66-
* is activated by default.
67-
*
68-
* @see https://goo.gl/4wyRtH
69-
* @var array
70-
*/
71-
protected $pkgAutoloaderRegistries = [
72-
//'src/MyVendor/Statistics' => '\MyVendor\ConcreteStatistics'
73-
];
74-
7563
/**
7664
* The packages handle.
7765
* Note that this must be unique in the
@@ -108,17 +96,9 @@ class Controller extends Package
10896
* @var array
10997
*/
11098
protected $providers = [
111-
// Register your concrete5 service providers here
11299
'Concrete\Package\SeoUpdateServiceNotifier\Src\Providers\UpdateServiceNotifierServiceProvider',
113100
];
114101

115-
/**
116-
* How often should we allow pinging?
117-
*
118-
* @var integer
119-
*/
120-
protected $rate_limit_ttl = 43200; // Every 12 hours
121-
122102
/**
123103
* Register the packages defined service providers.
124104
*
@@ -143,76 +123,8 @@ public function on_start()
143123
// Register defined service providers
144124
$this->registerServiceProviders();
145125

146-
// Plugin the event listeners.
147-
$this->addEventListeners();
148-
}
149-
150-
/**
151-
* Get the list of update service URLs to ping.
152-
*
153-
* @return array
154-
*/
155-
protected function getUpdateServiceUrls()
156-
{
157-
return [
158-
'https://requestb.in/1i2ftut1'
159-
];
160-
}
161-
162-
/**
163-
* Checks to see whether we should ping the update services.
164-
* Currently this only checks that we are not above our rate limit.
165-
*
166-
* @return boolean
167-
*/
168-
protected function canPing()
169-
{
170-
$cache = Core::make('cache/expensive');
171-
$item = $cache->getItem('seo.update.notifier.expiry');
172-
173-
if ($item->isMiss()) {
174-
$item->set(time() + $this->rate_limit_ttl, $this->rate_limit_ttl);
175-
176-
return true;
177-
}
178-
179-
return false;
180-
}
181-
182-
/**
183-
* Notifys update services that a page has been added / updated
184-
* and should be (re)indexed.
185-
*
186-
* @param \Page $page [description]
187-
* @return boolean
188-
*/
189-
protected function pingPage(\Page $page)
190-
{
191-
if ($this->canPing()) {
192-
$notifier = \Core::make('seo.update.notifier');
193-
194-
return $notifier->ping(
195-
$this->getUpdateServiceUrls(),
196-
\Config::get('concrete.site'),
197-
\View::url('/'),
198-
\View::url($page->getCollectionPath())
199-
);
200-
}
201-
}
202-
203-
/**
204-
* Wires the page event listeners to the update service
205-
* notification service.
206-
*
207-
* @return void
208-
*/
209-
public function addEventListeners()
210-
{
211-
\Events::addListener('on_page_version_approve', function($event) {
212-
$page_id = $event->getCollectionVersionObject()->getCollectionID();
213-
$page = \Page::getById($page_id);
214-
$this->pingPage($page);
215-
});
126+
// Listen for page approvals so we can ping the update services.
127+
Core::make('seo.update.notifier')->listen();
216128
}
217129

218130
/**
@@ -222,11 +134,13 @@ public function addEventListeners()
222134
*/
223135
public function install()
224136
{
225-
// Add your custom logic here that needs to be executed BEFORE package install.
226-
227137
$pkg = parent::install();
228138

229-
// Add your custom logic here that needs to be executed AFTER package install.
139+
// Install the dashboard page.
140+
$sp = \Concrete\Core\Page\Single::add('/dashboard/system/seo/notifications', $pkg);
141+
$sp->update([
142+
'cName' => 'Update Services',
143+
]);
230144

231145
return $pkg;
232146
}
@@ -238,11 +152,7 @@ public function install()
238152
*/
239153
public function upgrade()
240154
{
241-
// Add your custom logic here that needs to be executed BEFORE package install.
242-
243155
parent::upgrade();
244-
245-
// Add your custom logic here that needs to be executed AFTER package upgrade.
246156
}
247157

248158
/**
@@ -252,10 +162,6 @@ public function upgrade()
252162
*/
253163
public function uninstall()
254164
{
255-
// Add your custom logic here that needs to be executed BEFORE package uninstall.
256-
257165
parent::uninstall();
258-
259-
// Add your custom logic here that needs to be executed AFTER package uninstall.
260166
}
261167
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
namespace Concrete\Package\SeoUpdateServiceNotifier\Controller\SinglePage\Dashboard\System\Seo;
3+
4+
use Loader;
5+
use Core;
6+
use Concrete\Core\Page\Controller\DashboardPageController;
7+
8+
class Notifications extends DashboardPageController
9+
{
10+
/**
11+
* Show the form.
12+
*
13+
* @param string $param
14+
* @return void
15+
*/
16+
public function view($param = '')
17+
{
18+
// Fire up and / or set some helpers.
19+
$this->set('fh', Core::make('helper/form'));
20+
$this->set('interface', Loader::helper('concrete/ui'));
21+
$config = Core::make(\Concrete\Core\Config\Repository\Repository::class);
22+
23+
// Show the successful save message.
24+
if ('saved' === $param) {
25+
$this->set('message', ('Notification settings saved.'));
26+
}
27+
28+
// Set the page title.
29+
$this->set('pageTitle', t('Update Services'));
30+
31+
// Set the current configuration
32+
$hosts = $config->get('concrete.seo.notify.hosts', ['http://rpc.pingomatic.com']);
33+
$this->set('enabled', $config->get('concrete.seo.notify.enabled', false));
34+
$this->set('hosts', implode(PHP_EOL, $hosts));
35+
$this->set('log', $config->get('concrete.seo.notify.log', false));
36+
$this->set('ttl', $config->get('concrete.seo.notify.ttl', 900));
37+
38+
// Dashboard help text.
39+
Core::make('help/dashboard')->registerMessages([
40+
'/dashboard/system/seo/notifications' =>
41+
t('Notifying update services of new content helps improve your sites ranking and also helps new content rank faster.'),
42+
]);
43+
}
44+
45+
/**
46+
* Save the POST data.
47+
*
48+
* @return void
49+
*/
50+
public function save_notifications()
51+
{
52+
if ($this->isPost()) {
53+
// Get the data and split the host URLs.
54+
$data = $this->post('concrete')['seo']['notify'];
55+
$data['hosts'] = explode(PHP_EOL, $data['hosts']);
56+
57+
// Validate token.
58+
if (! $this->token->validate('save_notifications')) {
59+
$this->error->add($this->token->getErrorMessage());
60+
}
61+
62+
// Validate hosts.
63+
foreach ($data['hosts'] as $host) {
64+
if (! filter_var($host, FILTER_VALIDATE_URL)) {
65+
$this->error->add(sprintf(
66+
t('The host [%s] is invalid.'),
67+
htmlentities($host))
68+
);
69+
}
70+
}
71+
72+
if (! $this->error->has()) {
73+
// If checkboxes aren't set, set them as false.
74+
foreach (['enabled', 'log'] as $key) {
75+
if (empty($data[$key])) {
76+
$data[$key] = false;
77+
}
78+
}
79+
80+
// Save the configuration.
81+
$config = Core::make(\Concrete\Core\Config\Repository\Repository::class);
82+
$config->save('concrete.seo.notify.enabled', boolval($data['enabled']));
83+
$config->save('concrete.seo.notify.log', boolval($data['log']));
84+
$config->save('concrete.seo.notify.hosts', $data['hosts']);
85+
$config->save('concrete.seo.notify.ttl', intval($data['ttl']));
86+
87+
$this->redirect('/dashboard/system/seo/notifications', 'saved');
88+
}
89+
}
90+
91+
$this->view();
92+
}
93+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
defined('C5_EXECUTE') or die("Access Denied.");
3+
?>
4+
<form method="post" action="<?php echo $view->action('save_notifications'); ?>">
5+
<?php echo $this->controller->token->output('save_notifications'); ?>
6+
<fieldset>
7+
<div class="checkbox">
8+
<label>
9+
<?php echo $fh->checkbox('concrete[seo][notify][enabled]', 1, $enabled) ?>
10+
<?php echo t('Notify update services when a new page version is approved.'); ?>
11+
</label>
12+
</div>
13+
<div class="form-group" style="margin-top: 30px;">
14+
<label class="control-label"><?php echo t('Update Service List')?></label>
15+
<textarea name="concrete[seo][notify][hosts]" rows="8" class="form-control"><?php echo $hosts ?></textarea>
16+
<div class="alert alert-info" style="margin-top: 30px;">Separate multiple service URLs with line breaks.</div>
17+
</div>
18+
</fieldset>
19+
<fieldset>
20+
<legend>Advanced Settings</legend>
21+
<div class="checkbox">
22+
<label>
23+
<?php echo $fh->checkbox('concrete[seo][notify][log]', 1, $log) ?>
24+
<?php echo t('Log all notification requests & the responses.'); ?>
25+
</label>
26+
</div>
27+
28+
<div class="form-group" style="margin-top: 30px;">
29+
<label class="control-label"><?php echo t('Rate limiting')?></label>
30+
<br>
31+
<?php echo t('Only allow one request to be sent every')?>
32+
<select name="concrete[seo][notify][ttl]">
33+
<option value="900"<?php echo $ttl === 900 ? 'selected="selected"' : '' ?>>15 minutes</option>
34+
<option value="1800"<?php echo $ttl === 1800 ? 'selected="selected"' : '' ?>>30 minutes</option>
35+
<option value="3600"<?php echo $ttl === 3600 ? 'selected="selected"' : '' ?>>1 hour</option>
36+
<option value="14400"<?php echo $ttl === 14400 ? 'selected="selected"' : '' ?>>4 hours</option>
37+
<option value="28800"<?php echo $ttl === 28800 ? 'selected="selected"' : '' ?>>8 hours</option>
38+
<option value="43200"<?php echo $ttl === 43200 ? 'selected="selected"' : '' ?>>12 hours</option>
39+
</select>
40+
<span style="color: #ccc;">(<?php echo t('Note that additional requests in this time period will be discarded.'); ?>)</span>
41+
</div>
42+
</fieldset>
43+
44+
<div class="ccm-dashboard-form-actions-wrapper">
45+
<div class="ccm-dashboard-form-actions">
46+
<?php echo $interface->submit(t('Save'), 'url-form', 'right', 'btn-primary'); ?>
47+
</div>
48+
</div>
49+
</form>

0 commit comments

Comments
 (0)