Marco Colia
3 years ago
commit
87a4f81293
27 changed files with 4634 additions and 0 deletions
@ -0,0 +1,19 @@
|
||||
# EditorConfig helps developers define and maintain consistent |
||||
# coding styles between different editors and IDEs |
||||
# editorconfig.org |
||||
|
||||
root = true |
||||
|
||||
[*] |
||||
end_of_line = lf |
||||
charset = utf-8 |
||||
trim_trailing_whitespace = true |
||||
insert_final_newline = true |
||||
indent_style = space |
||||
indent_size = 2 |
||||
|
||||
[*.{diff,md}] |
||||
trim_trailing_whitespace = false |
||||
|
||||
[*.{php,xml,json}] |
||||
indent_size = 4 |
@ -0,0 +1,4 @@
|
||||
js/src export-ignore |
||||
.git* export-ignore |
||||
|
||||
js/dist/*.js -diff |
@ -0,0 +1,11 @@
|
||||
/vendor |
||||
composer.lock |
||||
composer.phar |
||||
node_modules |
||||
.DS_Store |
||||
Thumbs.db |
||||
tests/.phpunit.result.cache |
||||
/tests/integration/tmp |
||||
.vagrant |
||||
.idea/* |
||||
.vscode |
@ -0,0 +1,9 @@
|
||||
MIT License |
||||
|
||||
Copyright (c) <year> <copyright holders> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
@ -0,0 +1,27 @@
|
||||
# User Map Location |
||||
|
||||
![License](https://img.shields.io/badge/license-MIT-blue.svg) [![Latest Stable Version](https://img.shields.io/packagist/v/justoverclock/users-map-location.svg)](https://packagist.org/packages/justoverclock/users-map-location) [![Total Downloads](https://img.shields.io/packagist/dt/justoverclock/users-map-location.svg)](https://packagist.org/packages/justoverclock/users-map-location) |
||||
|
||||
A [Flarum](http://flarum.org) extension. Add Location Attribute and a map to users settings |
||||
|
||||
## Installation |
||||
|
||||
Install with composer: |
||||
|
||||
```sh |
||||
composer require justoverclock/users-map-location:"*" |
||||
``` |
||||
|
||||
## Updating |
||||
|
||||
```sh |
||||
composer update justoverclock/users-map-location:"*" |
||||
php flarum migrate |
||||
php flarum cache:clear |
||||
``` |
||||
|
||||
## Links |
||||
|
||||
- [Packagist](https://packagist.org/packages/justoverclock/users-map-location) |
||||
- [GitHub](https://github.com/justoverclock/users-map-location) |
||||
- [Discuss](https://discuss.flarum.org/d/PUT_DISCUSS_SLUG_HERE) |
After Width: | Height: | Size: 1.4 KiB |
@ -0,0 +1,39 @@
|
||||
{ |
||||
"name": "justoverclock/users-map-location", |
||||
"description": "Add Location Attribute and a map to users settings", |
||||
"keywords": [ |
||||
"users location", |
||||
"location map" |
||||
], |
||||
"type": "flarum-extension", |
||||
"license": "MIT", |
||||
"require": { |
||||
"flarum/core": "^1.0.0" |
||||
}, |
||||
"require-dev": { |
||||
"flarum/testing": "^1.0.0" |
||||
}, |
||||
"authors": [ |
||||
{ |
||||
"name": "Marco Colia", |
||||
"email": "info@flarum.it", |
||||
"role": "Developer" |
||||
} |
||||
], |
||||
"autoload": { |
||||
"psr-4": { |
||||
"Justoverclock\\UsersMapLocation\\": "src/" |
||||
} |
||||
}, |
||||
"extra": { |
||||
"flarum-extension": { |
||||
"title": "User Map Location", |
||||
"category": "features", |
||||
"icon": { |
||||
"name": "fas fa-map-marked-alt", |
||||
"backgroundColor": "purple", |
||||
"color": "white" |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,38 @@
|
||||
<?php |
||||
|
||||
/* |
||||
* This file is part of justoverclock/users-map-location. |
||||
* |
||||
* Copyright (c) 2022 Marco Colia. |
||||
* |
||||
* For the full copyright and license information, please view the LICENSE.md |
||||
* file that was distributed with this source code. |
||||
*/ |
||||
|
||||
namespace Justoverclock\UsersMapLocation; |
||||
|
||||
|
||||
use Justoverclock\UsersMapLocation\Listeners\SaveLocationToDatabase; |
||||
use Justoverclock\UsersMapLocation\Listeners\AddLocationAttribute; |
||||
use Flarum\Api\Serializer\UserSerializer; |
||||
use Flarum\Extend; |
||||
use Flarum\User\Event\Saving; |
||||
use Flarum\Api\Event\Serializing; |
||||
|
||||
|
||||
return [ |
||||
(new Extend\Frontend('forum')) |
||||
->js(__DIR__.'/js/dist/forum.js') |
||||
->css(__DIR__.'/less/forum.less'), |
||||
(new Extend\Frontend('admin')) |
||||
->js(__DIR__.'/js/dist/admin.js') |
||||
->css(__DIR__.'/less/admin.less'), |
||||
new Extend\Locales(__DIR__.'/locale'), |
||||
(new Extend\Event()) |
||||
->listen(Saving::class, SaveLocationToDatabase::class), |
||||
|
||||
(new Extend\ApiSerializer(UserSerializer::class)) |
||||
->attributes(AddLocationAttribute::class), |
||||
|
||||
(new Extend\Settings)->serializeToForum('justoverclock-users-map-location.mapBox-api-key', 'justoverclock-users-map-location.mapBox-api-key'), |
||||
]; |
@ -0,0 +1,2 @@
|
||||
module.exports=function(e){var t={};function r(o){if(t[o])return t[o].exports;var n=t[o]={i:o,l:!1,exports:{}};return e[o].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=e,r.c=t,r.d=function(e,t,o){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(r.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)r.d(o,n,function(t){return e[t]}.bind(null,n));return o},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=10)}({10:function(e,t,r){"use strict";r.r(t);var o=r(2),n=r.n(o);n.a.initializers.add("justoverclock/users-map-location",(function(){n.a.extensionData.for("justoverclock-users-map-location").registerSetting({setting:"justoverclock-users-map-location.mapBox-api-key",name:"justoverclock-users-map-location.mapBox-api-key",type:"text",label:n.a.translator.trans("justoverclock-users-map-location.admin.mapBox-api-key"),help:n.a.translator.trans("justoverclock-users-map-location.admin.mapBox-api-key-help")})}))},2:function(e,t){e.exports=flarum.core.compat["admin/app"]}}); |
||||
//# sourceMappingURL=admin.js.map
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,23 @@
|
||||
{ |
||||
"name": "@justoverclock/users-map-location", |
||||
"version": "0.0.0", |
||||
"private": true, |
||||
"prettier": "@flarum/prettier-config", |
||||
"dependencies": { |
||||
"@flarum/prettier-config": "^1.0.0", |
||||
"flarum-tsconfig": "^1.0.0", |
||||
"flarum-webpack-config": "^1.0.0", |
||||
"leaflet": "^1.7.1", |
||||
"webpack": "^4.26.0", |
||||
"webpack-cli": "^3.0.7" |
||||
}, |
||||
"devDependencies": { |
||||
"prettier": "^2.3.0" |
||||
}, |
||||
"scripts": { |
||||
"dev": "webpack --mode development --watch", |
||||
"build": "webpack --mode production", |
||||
"format": "prettier --write src", |
||||
"format-check": "prettier --check src" |
||||
} |
||||
} |
@ -0,0 +1,13 @@
|
||||
import app from 'flarum/admin/app'; |
||||
|
||||
app.initializers.add('justoverclock/users-map-location', () => { |
||||
app.extensionData |
||||
.for('justoverclock-users-map-location') |
||||
.registerSetting({ |
||||
setting: 'justoverclock-users-map-location.mapBox-api-key', |
||||
name: 'justoverclock-users-map-location.mapBox-api-key', |
||||
type: 'text', |
||||
label: app.translator.trans('justoverclock-users-map-location.admin.mapBox-api-key'), |
||||
help: app.translator.trans('justoverclock-users-map-location.admin.mapBox-api-key-help'), |
||||
}); |
||||
}); |
@ -0,0 +1,28 @@
|
||||
import app from 'flarum/forum/app'; |
||||
import Component from 'flarum/Component'; |
||||
|
||||
export default class AddLocationComponent extends Component { |
||||
oninit(vnode) { |
||||
super.oninit(vnode); |
||||
|
||||
this.location = app.session.user.location(); |
||||
} |
||||
|
||||
view(vnode) { |
||||
return ( |
||||
<fieldset className="Settings-theme"> |
||||
<legend>{app.translator.trans('justoverclock-users-map-location.forum.location')}</legend> |
||||
<p className="location-description">{app.translator.trans('justoverclock-users-map-location.forum.locationDescription')}</p> |
||||
<input type="text" className="FormControl location" id="location" name="location" value={this.location} onblur={this.saveValue.bind(this)} /> |
||||
</fieldset> |
||||
); |
||||
} |
||||
|
||||
saveValue(e) { |
||||
const user = app.session.user; |
||||
user.save({ |
||||
location: e.target.value, |
||||
}) |
||||
.then(app.alerts.show({type: 'success'}, app.translator.trans('justoverclock-users-map-location.forum.locationSaved'))); |
||||
} |
||||
} |
@ -0,0 +1,94 @@
|
||||
import app from 'flarum/forum/app'; |
||||
import { extend } from 'flarum/common/extend'; |
||||
import SettingsPage from 'flarum/forum/components/SettingsPage'; |
||||
import User from 'flarum/common/models/User'; |
||||
import Model from 'flarum/common/Model'; |
||||
import AddLocationComponent from './components/AddLocationComponent'; |
||||
import UserCard from 'flarum/forum/components/UserCard'; |
||||
import Leaflet from 'leaflet'; |
||||
|
||||
app.initializers.add('justoverclock/users-map-location', () => { |
||||
User.prototype.location = Model.attribute('location'); |
||||
extend(UserCard.prototype, 'oncreate', function () { |
||||
|
||||
const user = this.attrs.user; |
||||
let UserLocation = user.location(); |
||||
const publicToken = app.forum.attribute('justoverclock-users-map-location.mapBox-api-key'); |
||||
const geocode = 'https://nominatim.openstreetmap.org/search?city=' + UserLocation + '&format=json'; |
||||
|
||||
if (UserLocation === '') return; |
||||
|
||||
let GetCoordinates = fetch(geocode) |
||||
.then((response) => response.json()) |
||||
.then((coordinates) => { |
||||
this.latitude = coordinates[0].lat; |
||||
this.longitude = coordinates[0].lon; |
||||
|
||||
const markerIconPath = app.forum.attribute('baseUrl') + '/assets/extensions/justoverclock-users-map-location/marker-icon.png'; |
||||
|
||||
let markerIcon = L.icon({ |
||||
iconUrl: markerIconPath, |
||||
iconSize: [28, 45], // size of the icon
|
||||
}) |
||||
|
||||
let map2 = L.map('map2').setView([this.latitude, this.longitude], 13); |
||||
let layerUserCard = L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', { |
||||
attribution: |
||||
'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>, Developed by <a href="https://flarum.it/">Marco Colia</a>', |
||||
maxZoom: 18, |
||||
id: 'mapbox/streets-v11', |
||||
tileSize: 512, |
||||
zoomOffset: -1, |
||||
accessToken: publicToken, |
||||
}).addTo(map2); |
||||
}) |
||||
}) |
||||
extend(SettingsPage.prototype, 'oncreate', function () { |
||||
const location = app.session.user.location(); |
||||
const publicToken = app.forum.attribute('justoverclock-users-map-location.mapBox-api-key'); |
||||
const geocode = 'https://nominatim.openstreetmap.org/search?city=' + location + '&format=json'; |
||||
|
||||
if (location === '') return; |
||||
|
||||
let GetCoordinates = fetch(geocode) |
||||
.then((response) => response.json()) |
||||
.then((coordinates) => { |
||||
this.latitude = coordinates[0].lat; |
||||
this.longitude = coordinates[0].lon; |
||||
|
||||
const markerIconPath = app.forum.attribute('baseUrl') + '/assets/extensions/justoverclock-users-map-location/marker-icon.png'; |
||||
|
||||
let markerIcon = L.icon({ |
||||
iconUrl: markerIconPath, |
||||
iconSize: [28, 45], // size of the icon
|
||||
}); |
||||
|
||||
let map = L.map('map').setView([this.latitude, this.longitude], 13); |
||||
let marker = L.marker([this.latitude, this.longitude], { icon: markerIcon }).addTo(map); |
||||
let layer = L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', { |
||||
attribution: |
||||
'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> ' + |
||||
'contributors, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>, ' + |
||||
'Developed by <a href="https://flarum.it/">Marco Colia</a>', |
||||
maxZoom: 18, |
||||
id: 'mapbox/streets-v11', |
||||
tileSize: 512, |
||||
zoomOffset: -1, |
||||
accessToken: publicToken, |
||||
}).addTo(map); |
||||
}) |
||||
}); |
||||
extend(SettingsPage.prototype, 'settingsItems', function (items) { |
||||
items.add('location', <AddLocationComponent />); |
||||
items.add('mapDiv', <div className="map-div" id="map" />); |
||||
}); |
||||
extend(UserCard.prototype, 'infoItems', function (items) { |
||||
const user = this.attrs.user; |
||||
let UserLocation = user.location(); |
||||
if (UserLocation === '') return; |
||||
items.add( |
||||
'mapLocation', |
||||
<div className="map-div" id="map2" />, -100 |
||||
) |
||||
}) |
||||
}); |
@ -0,0 +1,11 @@
|
||||
{ |
||||
"extends": "flarum-tsconfig", |
||||
"include": ["src/**/*"], |
||||
"compilerOptions": { |
||||
"declarationDir": "./dist-typings", |
||||
"baseUrl": ".", |
||||
"paths": { |
||||
"flarum/*": ["../vendor/flarum/core/js/dist-typings/*"] |
||||
} |
||||
} |
||||
} |
@ -0,0 +1 @@
|
||||
module.exports = require('flarum-webpack-config')(); |
File diff suppressed because one or more lines are too long
@ -0,0 +1,8 @@
|
||||
justoverclock-users-map-location: |
||||
admin: |
||||
mapBox-api-key: MapBox Public Token |
||||
mapBox-api-key-help: "retrieve your free public token on <a>https://account.mapbox.com/</a>" |
||||
forum: |
||||
locationDescription: Here you can setup your location. Be sure to specify your City name only. |
||||
location: Location |
||||
locationSaved: Location saved successfully! |
@ -0,0 +1,8 @@
|
||||
<?php |
||||
|
||||
use Flarum\Database\Migration; |
||||
|
||||
|
||||
return Migration::addColumns('users', [ |
||||
'location' => ['type' => 'text'] |
||||
]); |
@ -0,0 +1,18 @@
|
||||
<?php |
||||
|
||||
namespace Justoverclock\UsersMapLocation\Listeners; |
||||
|
||||
use Flarum\Api\Serializer\UserSerializer; |
||||
use Flarum\User\User; |
||||
|
||||
class AddLocationAttribute |
||||
{ |
||||
public function __invoke(UserSerializer $serializer, User $user, array $attributes): array |
||||
{ |
||||
$actor = $serializer->getActor(); |
||||
|
||||
$attributes['location'] = $user->location; |
||||
|
||||
return $attributes; |
||||
} |
||||
} |
@ -0,0 +1,27 @@
|
||||
<?php |
||||
|
||||
namespace Justoverclock\UsersMapLocation\Listeners; |
||||
|
||||
use Flarum\User\Event\Saving; |
||||
use Illuminate\Support\Arr; |
||||
|
||||
class SaveLocationToDatabase |
||||
{ |
||||
public function handle(Saving $event) |
||||
{ |
||||
$user = $event->user; |
||||
$data = $event->data; |
||||
$actor = $event->actor; |
||||
|
||||
$isSelf = $actor->id === $user->id; |
||||
$canEdit = $actor->can('edit', $user); |
||||
$attributes = Arr::get($data, 'attributes', []); |
||||
|
||||
if (isset($attributes['location'])) { |
||||
if (!$isSelf) { |
||||
$actor->assertPermission($canEdit); |
||||
} |
||||
$user->location = $attributes['location']; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue