Skip to content

Commit

Permalink
Merge pull request #29 from Kovah/dev
Browse files Browse the repository at this point in the history
v0.0.5
  • Loading branch information
Kovah authored Jan 30, 2019
2 parents da1777f + e2a7d02 commit f2b9795
Show file tree
Hide file tree
Showing 48 changed files with 362 additions and 33 deletions.
139 changes: 139 additions & 0 deletions app/Helper/LinkIconMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace App\Helper;

/**
* Class LinkIconMapper
*
* @package App\Helper
*/
class LinkIconMapper
{
/** @var string Default icon as fallback if no specific icon was found */
public static $default_icon = 'fa fa-link';

/** @var array Array holding the pattern => icon map, sorted alphabetically */
public static $icon_map = [
'500px\.com' => 'fab fa-500px',
'adobe\.com' => 'fab fa-adobe',
'aws\.amazon\.com' => 'fab fa-aws', // put before amazon pattern to keep it easy
'amazon\.([a-zA-Z]*(\.)?[a-zA-Z]*)' => 'fab fa-amazon', // match amazon with every TLD
'angel\.co' => 'fab fa-angellist',
'itunes\.apple\.com\/app' => 'fab fa-app-store',
'apple\.com' => 'fab fa-apple',
'artstation\.com' => 'fab fa-artstation',
'atlassian\.(com|net)' => 'fab fa-atlassian',
'bandcamp\.com' => 'fab fa-bandcamp',
'behance\.net' => 'fab fa-behance',
'bitbucket\.org' => 'fab fa-bitbucket',
'blockchain\.com' => 'fab fa-bitcoin',
'blogger\.com' => 'fab fa-blogger-b',
'chrome\.google\.com\/webstore' => 'fab fa-chrome',
'codepen\.io' => 'fab fa-codepen',
'dev\.to' => 'fab fa-dev',
'deviantart\.com' => 'fab fa-deviantart',
'digitalocean\.com' => 'fab fa-digital-ocean',
'(discordapp\.com|discord\.gg)' => 'fab fa-discord',
'meta\.discourse\.org' => 'fab fa-discourse',
'docker\.com' => 'fab fa-docker',
'dribbble\.com' => 'fab fa-dribbble',
'dropbox\.com' => 'fab fa-dropbox',
'drupal\.org' => 'fab fa-drupal',
'ebay\.([a-zA-Z]*(\.)?[a-zA-Z]*)' => 'fab fa-ebay',
'ello\.co' => 'fab fa-ello',
'etsy\.com' => 'fab fa-etsy',
'(facebook\.com|fb\.me)' => 'fab fa-facebook-f',
'addons\.mozilla\.org\/[a-zA-Z-_]+\/firefox' => 'fab fa-firefox',
'figma\.com' => 'fab fa-figma',
'flickr\.com' => 'fab fa-flickr',
'(flipboard\.com|flip\.it)' => 'fab fa-flipboard',
'foursquare\.com' => 'fab fa-foursquare',
'(getpocket\.com|pocket\.co)' => 'fab fa-get-pocket',
'(github\.com|github\.io)' => 'fab fa-github',
'gitlab\.com' => 'fab fa-gitlab',
'gitter\.im' => 'fab fa-gitter',
'drive\.google\.com' => 'fab fa-google-drive',
'play\.google\.com' => 'fab fa-google-play',
'google\.([a-zA-Z]*(\.)?[a-zA-Z]*)' => 'fab fa-google',
'news\.ycombinator\.com' => 'fab fa-hacker-news',
'hackerrank\.com' => 'fab fa-hackerrank',
'houzz\.com' => 'fab fa-houzz',
'imdb\.com' => 'fab fa-imdb',
'instagram\.com' => 'fab fa-instagram',
'invisionapp\.com' => 'fab fa-invision',
'jsfiddle\.net' => 'fab fa-jsfiddle',
'kaggle\.com' => 'fab fa-kaggle',
'keybase\.io' => 'fab fa-keybase',
'kickstarter\.com' => 'fab fa-kickstarter',
'last\.fm' => 'fab fa-lastfm',
'leanpub\.com' => 'fab fa-leanpub',
'linkedin\.com' => 'fab fa-linkedin-in',
'mastodon\.social' => 'fab fa-mastodon',
'medium\.com' => 'fab fa-medium-m',
'meetup\.com' => 'fab fa-meetup',
'microsoft\.com' => 'fab fa-microsoft',
'mixcloud\.com' => 'fab fa-mixcloud',
'store\.nintendo\.com' => 'fab fa-nintendo-switch',
'npmjs\.com' => 'fab fa-npm',
'openid\.net' => 'fab fa-openid',
'opensource\.org' => 'fab fa-osi',
'patreon\.com' => 'fab fa-patreon ',
'paypal\.([a-zA-Z]*(\.)?[a-zA-Z]*)' => 'fab fa-paypal',
'periscope\.tv' => 'fab fa-periscope',
'php\.net' => 'fab fa-php',
'pinterest\.com' => 'fab fa-pinterest',
'playstation\.com' => 'fab fa-playstation',
'producthunt\.com' => 'fab fa-product-hunt',
'python\.org' => 'fab fa-python',
'quora\.com' => 'fab fa-quora',
'readme\.io' => 'fab fa-readme',
'reddit\.com' => 'fab fa-reddit',
'researchgate\.net' => 'fab fa-researchgate',
'rocket\.chat' => 'fab fa-rocketchat',
'scribd\.com' => 'fab fa-scribd',
'skype\.com' => 'fab fa-skype',
'slack\.com' => 'fab fa-slack',
'slideshare\.net' => 'fab fa-slideshare',
'snapchat\.com' => 'fab fa-snapchat',
'soundcloud\.com' => 'fab fa-soundcloud',
'spotify\.com' => 'fab fa-spotify',
'squarespace\.com' => 'fab fa-squarespace',
'stackexchange\.com' => 'fab fa-stack-exchange',
'stackoverflow\.com' => 'fab fa-stack-overflow',
'(steampowered\.com|steamcommunity\.com)' => 'fab fa-steam',
'strava\.com' => 'fab fa-strava',
'(telegram\.org|t\.me)' => 'fab fa-telegram',
'trello\.com' => 'fab fa-trello',
'tripadvisor\.com' => 'fab fa-tripadvisor',
'tumblr\.com' => 'fab fa-tumblr',
'twitch\.tv' => 'fab fa-twitch',
'twitter\.com' => 'fab fa-twitter',
'vimeo\.com' => 'fab fa-vimeo',
'weibo\.com' => 'fab fa-weibo',
'wikipedia\.org' => 'fab fa-wikipedia-w',
'wordpress\.com' => 'fab fa-wordpress',
'wordpress\.org' => 'fab fa-wordpress-simple',
'xbox\.com' => 'fab fa-xbox',
'xing\.com' => 'fab fa-xing',
'yahoo\.com' => 'fab fa-yahoo',
'yelp\.com' => 'fab fa-yelp',
'youtube\.com' => 'fab fa-youtube',
];

/**
* Check if the given url matches an icon
*
* @param $url
* @return string
*/
public static function mapLink($url): string
{
foreach (self::$icon_map as $pattern => $icon) {
if (preg_match('/' . $pattern . '/i', $url)) {
return $icon;
}
}

return self::$default_icon;
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/App/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DashboardController extends Controller
public function index()
{
$recent_links = Link::byUser(auth()->user()->id)
->orderBy('created_at', 'asc')
->orderBy('created_at', 'DESC')
->limit(10)
->get();

Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/App/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\App;

use App\Helper\LinkAce;
use App\Helper\LinkIconMapper;
use App\Http\Controllers\Controller;
use App\Http\Requests\DoImportRequest;
use App\Models\Link;
Expand Down Expand Up @@ -59,6 +60,7 @@ public function doImport(DoImportRequest $request)
'url' => $link['uri'],
'title' => $title,
'description' => $link['note'],
'icon' => LinkIconMapper::mapLink($link['uri']),
'is_private' => $link['pub'],
'created_at' => Carbon::createFromTimestamp($link['time']),
'updated_at' => Carbon::now(),
Expand Down
7 changes: 6 additions & 1 deletion app/Http/Controllers/Models/LinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Models;

use App\Helper\LinkAce;
use App\Helper\LinkIconMapper;
use App\Http\Controllers\Controller;
use App\Http\Requests\LinkDeleteRequest;
use App\Http\Requests\LinkStoreRequest;
Expand Down Expand Up @@ -70,6 +71,7 @@ public function store(LinkStoreRequest $request)

// Set the user ID
$data['user_id'] = auth()->user()->id;
$data['icon'] = LinkIconMapper::mapLink($data['url']);

$data['category_id'] = isset($data['category_id']) && $data['category_id'] > 0 ?: null;

Expand Down Expand Up @@ -183,7 +185,10 @@ public function update(LinkUpdateRequest $request, $id)
}

// Update the existing link with new data
$link->update($request->except('tags'));
$data = $request->except('tags');
$data['icon'] = LinkIconMapper::mapLink($data['url']);

$link->update($data);

Link::flushCache();

Expand Down
15 changes: 12 additions & 3 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace App\Models;

use App\Scopes\OrderNameScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Link;
use App\Models\User;

/**
* Class Category
Expand All @@ -27,7 +26,7 @@
* @method static Builder|Category parentOnly()
* @method static Builder|Category byUser($user_id)
*/
class Category extends Model
class Category extends RememberedModel
{
use SoftDeletes;

Expand All @@ -43,6 +42,16 @@ class Category extends Model

public $rememberCacheTag = 'category_queries';

/**
* Add the OrderNameScope to the Tag model
*/
protected static function boot()
{
parent::boot();

static::addGlobalScope(new OrderNameScope());
}

/*
| ========================================================================
| SCOPES
Expand Down
31 changes: 24 additions & 7 deletions app/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @property string $url
* @property string $title
* @property string|null $description
* @property string|null $icon
* @property int $is_private
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
Expand All @@ -26,7 +27,7 @@
* @property-read User $user
* @method static Builder|Link byUser($user_id)
*/
class Link extends Model
class Link extends RememberedModel
{
use SoftDeletes;

Expand All @@ -38,6 +39,7 @@ class Link extends Model
'url',
'title',
'description',
'icon',
'is_private',
];

Expand All @@ -51,8 +53,8 @@ class Link extends Model
/**
* Scope for the user relation
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $user_id
* @param Builder $query
* @param int $user_id
* @return mixed
*/
public function scopeByUser($query, $user_id)
Expand Down Expand Up @@ -82,31 +84,31 @@ public function scopePrivateOnly($query, bool $is_private)
*/
public function user()
{
return $this->belongsTo('App\Models\User', 'user_id');
return $this->belongsTo(User::class, 'user_id');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function category()
{
return $this->belongsTo('App\Models\Category', 'category_id');
return $this->belongsTo(Category::class, 'category_id');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
return $this->belongsToMany('App\Models\Tag', 'link_tags', 'link_id', 'tag_id');
return $this->belongsToMany(Tag::class, 'link_tags', 'link_id', 'tag_id');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function notes()
{
return $this->hasMany('App\Models\Note', 'link_id');
return $this->hasMany(Note::class, 'link_id');
}

/*
Expand All @@ -128,6 +130,21 @@ public function tagsForInput()
return $tags->implode('name', ',');
}

/**
* @param $additional_classes
* @return string
*/
public function getIcon($additional_classes = null): string
{
if ($this->icon === null) {
return '';
}

$classes = 'fa-fw ' . $this->icon . ($additional_classes ? ' ' . $additional_classes : '');

return '<i class="' . $classes . '"></i>';
}

/**
* Output a relative time inside a span with real time information
*
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @property-read User $user
* @method static Builder|Link byUser($user_id)
*/
class Note extends Model
class Note extends RememberedModel
{
use SoftDeletes;

Expand Down
4 changes: 2 additions & 2 deletions app/Models/Model.php → app/Models/RememberedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Watson\Rememberable\Rememberable;

/**
* Class Model
*
* @package App\Models
*/
abstract class Model extends Eloquent
abstract class RememberedModel extends BaseModel
{
use Rememberable;

Expand Down
13 changes: 12 additions & 1 deletion app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Scopes\OrderNameScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -21,7 +22,7 @@
* @property-read User $user
* @method static Builder|Tag byUser($user_id)
*/
class Tag extends Model
class Tag extends RememberedModel
{
use SoftDeletes;

Expand All @@ -35,6 +36,16 @@ class Tag extends Model

public $rememberCacheTag = 'tag_queries';

/**
* Add the OrderNameScope to the Tag model
*/
protected static function boot()
{
parent::boot();

static::addGlobalScope(new OrderNameScope());
}

/*
| ========================================================================
| SCOPES
Expand Down
Loading

0 comments on commit f2b9795

Please sign in to comment.