Initial commit
This commit is contained in:
54
formwidgets/CalendarSelector.php
Normal file
54
formwidgets/CalendarSelector.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php namespace NicoSt\GCalendar\FormWidgets;
|
||||
|
||||
use Backend\Classes\FormWidgetBase;
|
||||
use Log;
|
||||
use Flash;
|
||||
use Lang;
|
||||
|
||||
use NicoSt\GCalendar\Models\Settings;
|
||||
use NicoSt\GCalendar\Classes\GoogleCalendarService;
|
||||
|
||||
class CalendarSelector extends FormWidgetBase {
|
||||
|
||||
protected $defaultAlias = 'calendarSelector';
|
||||
|
||||
public function getSaveValue($value) {
|
||||
if(!isset($value)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$calendarsToSave = [];
|
||||
foreach ($value as &$calendar) {
|
||||
if (array_key_exists('checked', $calendar)) {
|
||||
$calendarsToSave[] = $calendar;
|
||||
}
|
||||
}
|
||||
return $calendarsToSave;
|
||||
}
|
||||
|
||||
public function render() {
|
||||
|
||||
$service = new GoogleCalendarService();
|
||||
$calendars = $service->calendarList();
|
||||
|
||||
if (isset($calendars['error'])) {
|
||||
$calendars = '';
|
||||
}
|
||||
|
||||
$this->vars['calendars'] = $calendars;
|
||||
$this->vars['saved_selector'] = $this->extractIds();
|
||||
|
||||
return $this->makePartial('selector');
|
||||
}
|
||||
|
||||
private function extractIds() {
|
||||
$calendarSelectors = Settings::get('calendar_selector', []);
|
||||
|
||||
$ids = [];
|
||||
foreach ($calendarSelectors as &$selector) {
|
||||
$ids[] = $selector['id'];
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
}
|
||||
57
formwidgets/OAuth.php
Normal file
57
formwidgets/OAuth.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php namespace NicoSt\GCalendar\FormWidgets;
|
||||
|
||||
use Backend\Classes\FormWidgetBase;
|
||||
use Log;
|
||||
use Flash;
|
||||
use Lang;
|
||||
use Redirect;
|
||||
|
||||
use NicoSt\GCalendar\Models\Settings;
|
||||
use NicoSt\GCalendar\Classes\GoogleCalendarClient;
|
||||
|
||||
class OAuth extends FormWidgetBase {
|
||||
|
||||
protected $defaultAlias = 'googleOAuth';
|
||||
|
||||
public function render() {
|
||||
|
||||
$class = new GoogleCalendarClient(true);
|
||||
$client = $class->getClient();
|
||||
|
||||
$this->vars['redirectUrl'] = $client->getRedirectUri();
|
||||
$this->vars['isAccessTokenExpired'] = $client->isAccessTokenExpired() ? '1' : '0';
|
||||
$this->vars['clientIdExist'] = null != Settings::get('client_id', null) ? '1' : '0';
|
||||
$this->vars['accessToken'] = print_r(Settings::get('access_token'), true);
|
||||
|
||||
return $this->makePartial('oauth');
|
||||
}
|
||||
|
||||
public function onRequestAccessToken() {
|
||||
|
||||
$class = new GoogleCalendarClient(true);
|
||||
$client = $class->getClient();
|
||||
|
||||
if ($client->isAccessTokenExpired()) {
|
||||
// Request authorization from the user.
|
||||
$authUrl = $client->createAuthUrl();
|
||||
Log::info('G-Calendar: Request authorization with URL ['. $authUrl .'].');
|
||||
|
||||
$this->vars['auth_url'] = $authUrl;
|
||||
|
||||
return $this->makePartial('gaccess');
|
||||
}
|
||||
|
||||
Log::info('G-Calendar: Access token not expired.');
|
||||
Flash::error(Lang::get('nicost.gcalendar::lang.message.accessTokenNotExpired'));
|
||||
}
|
||||
|
||||
public function onClearAccessToken() {
|
||||
Settings::set('access_token', '');
|
||||
Flash::success(Lang::get('nicost.gcalendar::lang.message.accessTokenRemoved'));
|
||||
|
||||
$this->vars['isAccessTokenExpired'] = '1';
|
||||
|
||||
return Redirect::refresh();
|
||||
}
|
||||
|
||||
}
|
||||
72
formwidgets/calendarselector/partials/_selector.htm
Normal file
72
formwidgets/calendarselector/partials/_selector.htm
Normal file
@@ -0,0 +1,72 @@
|
||||
<div class="control-list">
|
||||
<table class="table data">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="list-checkbox">
|
||||
<div class="checkbox custom-checkbox nolabel">
|
||||
<input type="checkbox" id="checkboxAll" />
|
||||
<label for="checkboxAll"></label>
|
||||
</div>
|
||||
</th>
|
||||
<th></th>
|
||||
<th><a><?= e(trans('nicost.gcalendar::lang.settings.calendarList.columnName')) ?></a></th>
|
||||
<th><a><?= e(trans('nicost.gcalendar::lang.settings.calendarList.columnRole')) ?></a></th>
|
||||
<th><a><?= e(trans('nicost.gcalendar::lang.settings.calendarList.columnId')) ?></a></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (empty($calendars)): ?>
|
||||
<tr class="no-data">
|
||||
<td colspan="100" class="nolink">
|
||||
<p class="no-data">
|
||||
<?= e(trans('nicost.gcalendar::lang.settings.calendarList.emptyList')) ?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<?php else: ?>
|
||||
<?php foreach ($calendars as $key=>$calendar): ?>
|
||||
<tr>
|
||||
<td class="list-checkbox nolink">
|
||||
<div class="checkbox custom-checkbox nolabel">
|
||||
<input id="checkbox_<?= $key ?>"
|
||||
type="checkbox"
|
||||
name="Settings[calendar_selector][<?= $key ?>][checked]"
|
||||
value="1"
|
||||
<?php if (isset($saved_selector) && in_array($calendar->id, $saved_selector)): ?>
|
||||
checked
|
||||
<?php endif ?>
|
||||
/>
|
||||
<label for="checkbox_<?= $key ?>"></label>
|
||||
</div>
|
||||
</td>
|
||||
<td><span style="width: 12px;height: 12px;display: inline-block;border-radius: 50%;background-color: <?= $calendar->backgroundColor ?>"></span>
|
||||
<input type="hidden"
|
||||
name="Settings[calendar_selector][<?= $key ?>][color]"
|
||||
value="<?= $calendar->backgroundColor ?>"
|
||||
/>
|
||||
</td>
|
||||
<td><?= $calendar->summary ?>
|
||||
<input type="hidden"
|
||||
name="Settings[calendar_selector][<?= $key ?>][name]"
|
||||
value="<?= $calendar->summary ?>"
|
||||
/>
|
||||
</td>
|
||||
<td><?= $calendar->accessRole ?></td>
|
||||
<td><?= $calendar->id ?>
|
||||
<input type="hidden"
|
||||
name="Settings[calendar_selector][<?= $key ?>][id]"
|
||||
value="<?= $calendar->id ?>"
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach ?>
|
||||
<?php endif ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
$("#checkboxAll").click(function () {
|
||||
$('input:checkbox').not(this).prop('checked', this.checked);
|
||||
});
|
||||
</script>
|
||||
6
formwidgets/oauth/partials/_gaccess.htm
Normal file
6
formwidgets/oauth/partials/_gaccess.htm
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php if (!empty($auth_url)): ?>
|
||||
<p>
|
||||
<?= e(trans('nicost.gcalendar::lang.settings.gaccess.text')) ?><br>
|
||||
<a href="<?= $auth_url ?>" target="_blank"><?= e(trans('nicost.gcalendar::lang.settings.gaccess.button')) ?></a>
|
||||
</p>
|
||||
<?php endif ?>
|
||||
47
formwidgets/oauth/partials/_oauth.htm
Normal file
47
formwidgets/oauth/partials/_oauth.htm
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php if ($isAccessTokenExpired == '1'): ?>
|
||||
<p class="flash-message static error w-300">
|
||||
Access Token <b>not</b> valid.
|
||||
</p>
|
||||
<div class="callout fade in callout-info no-subheader">
|
||||
<div class="header">
|
||||
<i class="icon-info"></i>
|
||||
<h3>Setup G-Calendar Plugin</h3>
|
||||
</div>
|
||||
<div class="content">
|
||||
<ol>
|
||||
<li>Go to <a href="https://console.developers.google.com/apis">Google APIs</a> to create a OAuth Client</li>
|
||||
<li>Go to <i>"Credentials"</i> and create a new OAuth Client ID. (Create credentials > OAuth Client ID)</li>
|
||||
<li>Select "Web application" and give it a name.</li>
|
||||
<li>Add <b><?= $redirectUrl ?></b> to <i>"Authorised redirect URIs"</i> and create the client.</li>
|
||||
<li>Copy the <i>"Client ID"</i> and <i>"Client Secret"</i> to the form above and save it.</li>
|
||||
<li>Last step is to Request the access token with the button below.</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="callout fade in callout-success no-icon">
|
||||
<div class="header">
|
||||
<h3><?= e(trans('nicost.gcalendar::lang.settings.oauth.tokenValid')) ?></h3>
|
||||
</div>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
<br>
|
||||
<p>
|
||||
<?php if ($isAccessTokenExpired == '1' && $clientIdExist == '1'): ?>
|
||||
<button type="button"
|
||||
data-request="onRequestAccessToken"
|
||||
data-request-update="gaccess: '#grand-access'"
|
||||
class="btn btn-default">
|
||||
<?= e(trans('nicost.gcalendar::lang.settings.button.requestToken')) ?>
|
||||
</button>
|
||||
<?php endif ?>
|
||||
<?php if ($isAccessTokenExpired == '0'): ?>
|
||||
<button type="button"
|
||||
data-request="onClearAccessToken"
|
||||
class="btn btn-danger">
|
||||
<?= e(trans('nicost.gcalendar::lang.settings.button.clearToken')) ?>
|
||||
</button>
|
||||
<?php endif ?>
|
||||
</p>
|
||||
|
||||
<div id="grand-access"></div>
|
||||
Reference in New Issue
Block a user