Skip to content

Commit

Permalink
Added pane and table column attribute to see status or a user > athel…
Browse files Browse the repository at this point in the history
…ete to Strava
  • Loading branch information
bymayo committed May 31, 2019
1 parent 2e62679 commit 4a0bbf0
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 81 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Strava Sync Changelog

## 1.0.6 - 2019-05-31
### Added
- Table attribute to the users table view to see if user is connected/disconnected from Strava
- Pane inside a user to see if user is connected/disconnected from Strava and output Athlete ID

## 1.0.5 - 2019-04-26
### Fixed
- Fixed issue where new Strava users produced an error when trying to save profile photos (Because they didn't exist)
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ https://plugins.craftcms.com/strava-sync
- Automatically fills profile data (First Name, Last Name, Profile Photo etc)
- Map additional athlete data to user fields (City, Country, Sex etc)
- Get data from the Strava API (Athletes, activities, segments etc)
- Sync data directly in to Craft CMS via Webhooks [Soon]
- Sync data directly in to Craft CMS via Webhooks
- Pane inside a user to show athlete data and if connected/disconnected to Strava
- Table attribute column on 'Users' table, to show if user is connected/disconnected to Strava

## Requirements

Expand Down Expand Up @@ -173,6 +175,10 @@ Depending on your scope type when you authorised the account, the supported requ
- getStreamsSegment
- getStreamsRoute

## Webhooks

@TODO Documentation on this feature. Open issue if any questions.

## Support

If you have any issues (Surely not!) then I'll aim to reply to these as soon as possible. If it's a site-breaking-oh-no-what-has-happened moment, then hit me up on the Craft CMS Slack / Discord - @bymayo
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "bymayo/strava-sync",
"description": "Connect to Strava with oAuth and sync activities etc to Craft CMS ",
"type": "craft-plugin",
"version": "1.0.5",
"version": "1.0.6",
"keywords": [
"craft",
"cms",
Expand Down
40 changes: 40 additions & 0 deletions src/StravaSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use bymayo\stravasync\variables\StravaSyncVariable;
use bymayo\stravasync\models\Settings;
use bymayo\stravasync\assetbundles\stravasync\StravaSyncAsset;

use Craft;
use craft\base\Plugin;
Expand All @@ -12,6 +13,8 @@
use craft\web\UrlManager;
use craft\web\twig\variables\CraftVariable;
use craft\events\RegisterUrlRulesEvent;
use craft\events\RegisterElementTableAttributesEvent;
use craft\events\SetElementTableAttributeHtmlEvent;
use craft\helpers\FileHelper;
use craft\services\Elements;
use craft\elements\User as UserElement;
Expand Down Expand Up @@ -111,6 +114,43 @@ function (Event $event) {
),
__METHOD__
);

Event::on(
UserElement::class,
UserElement::EVENT_REGISTER_TABLE_ATTRIBUTES,
function(RegisterElementTableAttributesEvent $event) {
$event->tableAttributes['stravaSync'] = 'Strava';
}
);

Event::on(
UserElement::class,
UserElement::EVENT_SET_TABLE_ATTRIBUTE_HTML,
function(SetElementTableAttributeHtmlEvent $event) {
if ($event->attribute === 'stravaSync') {
Craft::$app->getView()->registerAssetBundle(StravaSyncAsset::class);
$user = $event->sender;
$userConnected = StravaSync::getInstance()->userService->checkUserLinkExists($user->id);
$event->html = Craft::$app->getView()->renderTemplate(
'strava-sync/table-attribute',
[
'status' => $userConnected,
]
);
}
}
);

Craft::$app->getView()->hook(
'cp.users.edit.details',
function(&$context) {
if ($context['user'] && $context['user']->id) {
Craft::$app->getView()->registerAssetBundle(StravaSyncAsset::class);
return Craft::$app->getView()->renderTemplate('strava-sync/user-pane', $context);
}
}
);

}

// Protected Methods
Expand Down
38 changes: 27 additions & 11 deletions src/assetbundles/stravasync/dist/css/StravaSync.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
/**
* Strava Sync plugin for Craft CMS
*
* Strava Sync CSS
*
* @author bymayo
* @copyright Copyright (c) 2019 bymayo
* @link http://bymayo.co.uk
* @package StravaSync
* @since 1.0.0
*/
.rounded {
border-radius: .25rem;
}

.font-medium {
font-weight: 500;
}

.text-sm {
font-size: .75rem;
}

.p-2 {
padding: 0.3rem 0.5rem;
}

.text-white {
color: #fff;
}

.bg-strava {
background-color: #fc4c02;
}

.bg-gray-300 {
background-color: #e2e8f0;
}
52 changes: 0 additions & 52 deletions src/assetbundles/stravasync/dist/img/StravaSync-icon.svg

This file was deleted.

11 changes: 0 additions & 11 deletions src/assetbundles/stravasync/dist/js/StravaSync.js
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
/**
* Strava Sync plugin for Craft CMS
*
* Strava Sync JS
*
* @author bymayo
* @copyright Copyright (c) 2019 bymayo
* @link http://bymayo.co.uk
* @package StravaSync
* @since 1.0.0
*/
11 changes: 8 additions & 3 deletions src/services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ class UserService extends Component
// Public Methods
// =========================================================================

public function checkUserLinkExists()
public function checkUserLinkExists($userId)
{

$user = Craft::$app->getUser()->getIdentity();
$userRecord = UsersRecord::findOne(['userId' => $user->id]);
if (!$userId)
{
$user = Craft::$app->getUser()->getIdentity();
$userId = $user->id;
}

$userRecord = UsersRecord::findOne(['userId' => $userId]);

if ($userRecord) {
return $userRecord;
Expand Down
5 changes: 5 additions & 0 deletions src/templates/table-attribute.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% if status %}
<span class="bg-strava text-white font-medium text-sm p-2 rounded">Connected</span>
{% else %}
<span class="bg-gray-300 font-medium text-sm p-2 rounded">Disconnected</span>
{% endif %}
39 changes: 39 additions & 0 deletions src/templates/user-pane.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% if user.id == currentUser.id or currentUser.admin %}

<div class="meta">
<h2>Strava</h2>
{% set stravaSyncUser = craft.stravaSync.connected(user.id) %}
{% if stravaSyncUser %}
<div class="data first">
<span class="heading">ID</span>
<span class="value">
<a href="https://strava.com/athletes/{{ stravaSyncUser.athleteId }}" target="_blank">#{{ stravaSyncUser.athleteId }}</a>
</span>
</div>
<div class="data">
<span class="heading">Token Expiry</span>
<span class="value">{{ stravaSyncUser.expires|datetime('short') }}</span>
</div>
<div class="data">
<span class="heading">
{% if user.id == currentUser.id %}
<a href="{{ craft.stravaSync.disconnectUrl() }}" class="btn small delete" title="Disconnect from Strava">Disconnect from Strava</a>
{% elseif currentUser.admin %}
<a href="{{ craft.stravaSync.disconnectUrl() }}" class="btn small delete" title="Disconnect from Strava">Disconnect from Strava</a>
{% else %}
<div class="btn small disabled">Disconnect from Strava</div>
{% endif %}
</span>
</div>
{% else %}
<div class="data">
{% if user.id == currentUser.id %}
<a href="{{ craft.stravaSync.connectUrl() }}" class="btn small submit">Connect to Strava</a>
{% else %}
<div class="btn small submit disabled">Connect to Strava</div>
{% endif %}
</div>
{% endif %}
</div>

{% endif %}
4 changes: 2 additions & 2 deletions src/variables/StravaSyncVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public function request($method, $params = null, $userId = null)
return $connect = StravaSync::getInstance()->oauthService->request($method, $params, $userId);
}

public function connected()
public function connected($userId)
{
return StravaSync::getInstance()->userService->checkUserLinkExists();
return StravaSync::getInstance()->userService->checkUserLinkExists($userId);
}

public function connectUrl()
Expand Down

0 comments on commit 4a0bbf0

Please sign in to comment.