API Make use of the new AdminController (#151)

This commit is contained in:
Guy Sartorelli 2024-10-21 11:54:20 +13:00 committed by GitHub
parent a40106d606
commit 20112b9aec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 33 deletions

View File

@ -1,7 +0,0 @@
---
Name: externallinkroutes
Before: '#adminroutes'
---
SilverStripe\Control\Director:
rules:
'admin/externallinks//$Action': SilverStripe\ExternalLinks\Controllers\CMSExternalLinksController

View File

@ -1 +1 @@
!function(){"use strict";var t={669:function(t){t.exports=jQuery}},e={};var n=function n(o){var r=e[o];if(void 0!==r)return r.exports;var s=e[o]={exports:{}};return t[o](s,s.exports,n),s.exports}(669);n.entwine("ss",(t=>{t(".external-links-report__create-report").entwine({PollTimeout:null,ButtonIsLoading:!1,ReloadContent:!1,onclick(t){t.preventDefault(),this.buttonLoading(),this.start()},onmatch(){this.poll()},start(){const e=this;t(".external-links-report__report-progress").empty().text("Running report 0%"),t.ajax({url:"admin/externallinks/start",async:!0,timeout:3e3,success(){e.setReloadContent(!0),e.poll()},error(){e.buttonReset()}})},getButton(){return t(".external-links-report__create-report")},buttonLoading(){if(this.getButtonIsLoading())return;this.setButtonIsLoading(!0);const e=this.getButton();e.addClass("btn--loading loading"),e.attr("disabled",!0),e.is("button")&&(e.append(t('<div class="btn__loading-icon"><span class="btn__circle btn__circle--1" /><span class="btn__circle btn__circle--2" /><span class="btn__circle btn__circle--3" /></div>')),e.css(`${e.outerWidth()}px`))},buttonReset(){this.setButtonIsLoading(!1);const t=this.getButton();t.removeClass("btn--loading loading"),t.attr("disabled",!1),t.find(".btn__loading-icon").remove(),t.css("width","auto")},poll(){const e=this;this.buttonLoading(),t.ajax({url:"admin/externallinks/getJobStatus",async:!0,success(n){if(!n)return void e.buttonReset();const o=n.Completed?n.Completed:0,r=n.Total?n.Total:0;if("Completed"===n.Status)return e.getReloadContent()&&(t(".cms-container").loadPanel(document.location.href,null,{},!0,!1),e.setReloadContent(!1)),t(".external-links-report__report-progress").text(`Report finished ${o}/${r}`),void e.buttonReset();if(o<r){const e=o/r*100;t(".external-links-report__report-progress").text(`Running report ${o}/${r} (${e.toFixed(2)}%)`)}null!==e.getPollTimeout()&&clearTimeout(e.getPollTimeout()),e.setPollTimeout(setTimeout((()=>{t(".external-links-report__create-report").poll()}),1e3))},error(){e.buttonReset()}})}})}))}();
!function(){"use strict";var t={669:function(t){t.exports=jQuery}},e={};var n=function n(o){var r=e[o];if(void 0!==r)return r.exports;var s=e[o]={exports:{}};return t[o](s,s.exports,n),s.exports}(669);n.entwine("ss",(t=>{t(".external-links-report__create-report").entwine({PollTimeout:null,ButtonIsLoading:!1,ReloadContent:!1,onclick(t){t.preventDefault(),this.buttonLoading(),this.start()},onmatch(){this.poll()},start(){const e=this;t(".external-links-report__report-progress").empty().text("Running report 0%"),t.ajax({url:"admin/externallinks/start",async:!0,timeout:3e3,success(){e.setReloadContent(!0),e.poll()},error(){e.buttonReset()}})},getButton(){return t(".external-links-report__create-report")},buttonLoading(){if(this.getButtonIsLoading())return;this.setButtonIsLoading(!0);const e=this.getButton();e.addClass("btn--loading loading"),e.attr("disabled",!0),e.is("button")&&(e.append(t('<div class="btn__loading-icon"><span class="btn__circle btn__circle--1" /><span class="btn__circle btn__circle--2" /><span class="btn__circle btn__circle--3" /></div>')),e.css(`${e.outerWidth()}px`))},buttonReset(){this.setButtonIsLoading(!1);const t=this.getButton();t.removeClass("btn--loading loading"),t.attr("disabled",!1),t.find(".btn__loading-icon").remove(),t.css("width","auto")},poll(){const e=this;this.buttonLoading(),t.ajax({url:"admin/externallinks/getJobStatus",async:!0,success(n){if(!n||"object"==typeof n&&n.length<1)return void e.buttonReset();const o=n.Completed?n.Completed:0,r=n.Total?n.Total:0;if("Completed"===n.Status)return e.getReloadContent()&&(t(".cms-container").loadPanel(document.location.href,null,{},!0,!1),e.setReloadContent(!1)),t(".external-links-report__report-progress").text(`Report finished ${o}/${r}`),void e.buttonReset();if(o<r){const e=o/r*100;t(".external-links-report__report-progress").text(`Running report ${o}/${r} (${e.toFixed(2)}%)`)}null!==e.getPollTimeout()&&clearTimeout(e.getPollTimeout()),e.setPollTimeout(setTimeout((()=>{t(".external-links-report__create-report").poll()}),1e3))},error(){e.buttonReset()}})}})}))}();

View File

@ -99,7 +99,7 @@
async: true,
success(data) {
// No report, so let user create one
if (!data) {
if (!data || (typeof data === 'object' && data.length < 1)) {
self.buttonReset();
return;
}

View File

@ -2,17 +2,22 @@
namespace SilverStripe\ExternalLinks\Controllers;
use SilverStripe\Admin\AdminController;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
use SilverStripe\ExternalLinks\Jobs\CheckExternalLinksJob;
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
use SilverStripe\Control\Controller;
use Symbiote\QueuedJobs\Services\QueuedJobService;
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\Security\Permission;
class CMSExternalLinksController extends Controller
class CMSExternalLinksController extends AdminController
{
private static ?string $url_segment = 'externallinks';
private static string|array $required_permission_codes = [
'CMS_ACCESS_CMSMain',
];
private static $allowed_actions = [
'getJobStatus',
'start'
@ -20,45 +25,31 @@ class CMSExternalLinksController extends Controller
/**
* Respond to Ajax requests for info on a running job
*
* @return string JSON string detailing status of the job
*/
public function getJobStatus()
public function getJobStatus(): HTTPResponse
{
if (!Permission::check('CMS_ACCESS_CMSMain')) {
return $this->httpError(403, 'You do not have permission to access this resource');
}
// Set headers
HTTPCacheControlMiddleware::singleton()->setMaxAge(0);
$this->response
->addHeader('Content-Type', 'application/json')
->addHeader('Content-Encoding', 'UTF-8')
->addHeader('X-Content-Type-Options', 'nosniff');
// Format status
$this->getResponse()->addHeader('X-Content-Type-Options', 'nosniff');
$track = BrokenExternalPageTrackStatus::get_latest();
if ($track) {
return json_encode([
return $this->jsonSuccess(200, [
'TrackID' => $track->ID,
'Status' => $track->Status,
'Completed' => $track->getCompletedPages(),
'Total' => $track->getTotalPages()
]);
}
return $this->jsonSuccess(200, []);
}
/**
* Starts a broken external link check
*/
public function start()
public function start(): HTTPResponse
{
if (!Permission::check('CMS_ACCESS_CMSMain')) {
return $this->httpError(403, 'You do not have permission to access this resource');
}
// return if the a job is already running
$status = BrokenExternalPageTrackStatus::get_latest();
if ($status && $status->Status == 'Running') {
return;
return $this->jsonSuccess(200, []);
}
// Create a new job
@ -71,5 +62,6 @@ class CMSExternalLinksController extends Controller
$task = CheckExternalLinksTask::create();
$task->runLinksCheck(PolyOutput::create(PolyOutput::FORMAT_HTML));
}
return $this->jsonSuccess(200, []);
}
}