Skip to content

Commit

Permalink
Added RestoreUtility
Browse files Browse the repository at this point in the history
  • Loading branch information
Saboteur777 committed Oct 26, 2020
1 parent f414213 commit 54fb9af
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 5 deletions.
15 changes: 15 additions & 0 deletions .stylelintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "stylelint-config-recommended",
"rules": {
"at-rule-no-unknown": [ true, {
"ignoreAtRules": [
"screen",
"extends",
"responsive",
"tailwind"
]
}],
"block-no-empty": null,
"no-descending-specificity": null
}
}
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@
"changelog": "auto-changelog -p",
"version": "npm run replace-version && npm run changelog && git add -A",
"postversion": "git push && git push --tags",
"replace-version": "replace-in-file --configFile ./.replace-in-file.config.js"
"replace-version": "replace-in-file --configFile ./.replace-in-file.config.js",
"dev": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run dev -- --watch",
"build": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"auto-changelog": "^1.16.4",
"cross-env": "^7.0.2",
"laravel-mix": "^5.0.7",
"replace-in-file": "^4.2.0"
},
"dependencies": {
"vue": "^2.6.12"
}
}
50 changes: 46 additions & 4 deletions src/Reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@

use webmenedzser\reporter\jobs\CallBackendJob;
use webmenedzser\reporter\models\Settings;
use webmenedzser\reporter\utilities\RestoreUtility;

use Craft;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\services\ProjectConfig;
use craft\events\PluginEvent;
use craft\web\UrlManager;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\log\FileTarget;
use craft\events\RegisterUserPermissionsEvent;
use craft\helpers\UrlHelper;
use craft\log\FileTarget;
use craft\services\Plugins;
use craft\services\ProjectConfig;
use craft\services\UserPermissions;
use craft\services\Utilities;
use craft\web\UrlManager;

use yii\base\Event;

Expand Down Expand Up @@ -74,6 +79,8 @@ public function init()
$this->_registerLogger();
$this->_afterInstall();
$this->_registerEvents();
$this->_registerPermissions();
$this->_registerUtilities();
}

// Protected Methods
Expand Down Expand Up @@ -190,6 +197,41 @@ function(Event $event) {
}
}

private function _registerPermissions()
{
// If Craft edition is pro
if (Craft::$app->getEdition() === Craft::Pro) {
Event::on(
UserPermissions::class,
UserPermissions::EVENT_REGISTER_PERMISSIONS,
function(RegisterUserPermissionsEvent $event) {
$event->permissions['Reporter'] = [
'craft-reporter:restore-utility' => [
'label' => Craft::t(
'craft-reporter',
'Restore Backups'
)
]
];
}
);
}
}

private function _registerUtilities()
{
// Register utility
Event::on(
Utilities::class,
Utilities::EVENT_REGISTER_UTILITY_TYPES,
function(RegisterComponentTypesEvent $event) {
if (Craft::$app->getUser()->checkPermission('craft-reporter:restore-utility')) {
$event->types[] = RestoreUtility::class;
}
}
);
}

private function _registerLogger()
{
// Create a new file target
Expand Down
3 changes: 3 additions & 0 deletions src/assetbundles/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"/utilities/dist/js/app.js": "/utilities/dist/js/app.js"
}
39 changes: 39 additions & 0 deletions src/assetbundles/utilities/RestoreUtilityAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Reporter plugin for Craft CMS 3.x
*
* Reporter plugin for Craft CMS.
*
* @link https://www.webmenedzser.hu
* @copyright Copyright (c) 2020 Ottó Radics
*/

namespace webmenedzser\reporter\assetbundles\utilities;

use craft\web\AssetBundle;
use craft\web\assets\cp\CpAsset;

/**
* Asset bundle for Restore Utility
*/
class RestoreUtilityAsset extends AssetBundle
{
/**
* @inheritdoc
*/
public $sourcePath = __DIR__ . '/dist';

/**
* @inheritdoc
*/
public $depends = [
CpAsset::class,
];

/**
* @inheritdoc
*/
public $js = [
'js/app.js',
];
}
10 changes: 10 additions & 0 deletions src/assetbundles/utilities/src/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Vue from 'vue';

Vue.component('restoreform', require('../vue/RestoreForm').default);

const app = document.getElementById('craft-reporter-vue');
if (app) {
new Vue({
el: app
});
}
4 changes: 4 additions & 0 deletions src/assetbundles/utilities/src/js/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Configure the GraphQL api endpoint
export const axiosConfiguration = (url) => ({
baseURL: url,
});
130 changes: 130 additions & 0 deletions src/assetbundles/utilities/src/vue/RestoreForm.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<template>
<div class="restore-form">
<div class="progress pane" v-if="this.status === 'loading'">
<div class="spinner"></div>

<div class="progress-text">
Restoring...
</div>
</div>

<div class="result pane" v-if="this.status === 'finished' || this.status === 'error'">
<div class="" v-if="this.status === 'finished'">
<p>
<span class="checkmark-icon"></span>

<span class="progress-text">
Done!
</span>
</p>
</div>

<code v-else>
{{ this.result }}
</code>
</div>

<button
@click="startDbRestore"
class="btn submit"
:class="this.status === 'loading' ? 'disabled' : ''"
:disabled="this.status === 'loading'"
v-if="this.propKey"
>
Restore Database
</button>

<div class="btn submit disabled" disabled="disabled" v-else>
Craft Report API Key is missing!
</div>
</div>
</template>

<script>
import axios from 'axios';
import { axiosConfiguration } from '../js/utils.js';
export default {
name: 'RestoreForm',
props: {
propActionUrl: {
default: '',
type: String
},
propCsrfToken: {
default: '',
type: String
},
propKey: {
default: '',
type: String
}
},
computed: {
result() {
return this.response.message ?? this.response.statusText ?? '';
},
status() {
if (this.loading === true) {
return 'loading';
}
if (this.result === 'OK') {
return 'finished';
}
if (this.result) {
return 'error';
}
return 'ready';
}
},
data() {
return {
loading: false,
response: {}
}
},
methods: {
startDbRestore() {
if (!confirm('Are you sure want to restore your last backup from Craft Report? This will destroy your current database.')) {
return;
}
const api = axios.create(axiosConfiguration(this.propActionUrl));
this.loading = true;
api.post('', {
csrfToken: this.propCsrfToken
}).then(response => {
this.response = response;
this.loading = false;
}).catch(error => {
this.response = error;
this.loading = false;
});
}
},
};
</script>
<style>
.progress,
.result {
margin-top: 2rem;
margin-bottom: 2rem;
}
.progress {
display: flex;
align-items: center;
justify-content: start;
}
.progress-text {
margin-left: 1rem;
font-weight: bold;
}
</style>
25 changes: 25 additions & 0 deletions src/templates/_restore-utility.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% import "_includes/forms" as forms %}

{% set canRestore = currentUser.can('craft-reporter:restore-utility') ?? false %}
{% do view.registerAssetBundle("webmenedzser\\reporter\\assetbundles\\utilities\\RestoreUtilityAsset") %}

{% block content %}
{% if canRestore %}
<p class="light">
{{ 'You can restore the last Database Backup from Craft Report.' | t('craft-reporter') }}
</p>

<div id="craft-reporter-vue">
<RestoreForm
prop-action-url="{{ actionUrl('craft-reporter/backup/restore') }}"
prop-csrf-token="{{ craft.app.request.csrfToken }}"
prop-key="{{ settings.apiKey }}"
></RestoreForm>
</div>
{% else %}
<p class="light">
{{ 'You don\'t have permissions to restore Database Backup from Craft Report.' | t('craft-reporter') }}
</p>
{% endif %}
{% endblock %}

50 changes: 50 additions & 0 deletions src/utilities/RestoreUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Reporter plugin for Craft CMS 3.x
*
* Reporter plugin for Craft CMS.
*
* @link https://www.webmenedzser.hu
* @copyright Copyright (c) 2020 Ottó Radics
*/

namespace webmenedzser\reporter\utilities;

use webmenedzser\reporter\assetbundles\utilities\RestoreUtilityAsset;
use webmenedzser\reporter\Reporter;

use Craft;
use craft\base\Utility;

/**
* Class RestoreUtility
*
* @package webmenedzser\reporter\utilities
* @since: 1.10.0
*/
class RestoreUtility extends Utility
{
public static function displayName() : string
{
return Craft::t('craft-reporter', 'Restore Database Backup');
}

public static function id() : string
{
return 'craft-reporter-restore-utility';
}

public static function iconPath()
{
return Craft::getAlias('@vendor/webmenedzser/craft-reporter/src/icon-mask.svg');
}

public static function contentHtml() : string
{
$view = Craft::$app->getView();

return Craft::$app->getView()->renderTemplate('craft-reporter/_restore-utility', [
'settings' => Reporter::$plugin->getSettings(),
]);
}
}
Loading

0 comments on commit 54fb9af

Please sign in to comment.