Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upd Groups config, add getGroupsFor() #60

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ install:
before_script:
- mysql -e 'create database friendships_test;'
- cp src/database/migrations/create_friendships_table.php vendor/laravel/laravel/database/migrations/2015_11_19_053825_create_friendships_table.php
- yes | cp src/database/migrations/create_friendships_groups_table.php vendor/laravel/laravel/database/migrations/2015_11_19_053826_create_friendships_groups_table.php
- yes | cp src/database/migrations/create_friendship_groups_table.php vendor/laravel/laravel/database/migrations/2015_11_19_053826_create_friendship_groups_table.php
- yes | cp src/database/migrations/create_friendship_grouped_table.php vendor/laravel/laravel/database/migrations/2015_11_19_053827_create_friendship_grouped_table.php
- yes | cp src/database/seeds/FriendshipGroupsSeeder.php vendor/laravel/laravel/database/seeds/FriendshipGroupsSeeder.php
- yes | cp tests/config/friendships.php vendor/laravel/laravel/config/friendships.php
- yes | cp tests/Stub_User.php vendor/laravel/laravel/app/User.php
- cd vendor/laravel/laravel
- yes | cd vendor/laravel/laravel
- composer update --dev --prefer-source --no-interaction
- php artisan migrate
- cd -
- yes | php artisan db:seed --class=FriendshipGroupsSeeder
- yes | cd -

script:
- vendor/bin/phpunit
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ $user->ungroupFriend($friend, 'family');
$user->ungroupFriend($friend);
```

#### Get groups for specific friend
```php
$user->getGroupsFor($friend);
```

#### Get the number of Friends in specific group
```php
$user->getFriendsCount($group_name);
Expand Down
8 changes: 5 additions & 3 deletions src/FriendshipsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ class FriendshipsServiceProvider extends ServiceProvider
public function boot()
{

if (class_exists('CreateFriendshipsTable') || class_exists('CreateFriendshipsGroupsTable')) {
if (class_exists('CreateFriendshipsTable')) {
return;
}

$stub = __DIR__ . '/database/migrations/';
$target = database_path('migrations') . '/';

$this->publishes([
$stub . 'create_friendships_table.php' => $target . date('Y_m_d_His', time()) . '_create_friendships_table.php',
$stub . 'create_friendships_groups_table.php' => $target . date('Y_m_d_His', time() + 1) . '_create_friendships_groups_table.php'
$stub . 'create_friendships_table.php' => $target . date('Y_m_d_His', time()) . '_create_friendships_table.php',
$stub . 'create_friendship_groups_table.php' => $target . date('Y_m_d_His', time() + 1) . '_create_friendship_groups_table.php',
$stub . 'create_friendship_grouped_table.php' => $target . date('Y_m_d_His', time() + 2) . '_create_friendship_grouped_table.php'
], 'migrations');

$this->publishes([
Expand All @@ -40,4 +41,5 @@ public function boot()
public function register()
{
}

}
37 changes: 17 additions & 20 deletions src/Models/Friendship.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Hootlex\Friendships\Models;

use Hootlex\Friendships\Status;
use Illuminate\Database\Eloquent\Model;

/**
Expand Down Expand Up @@ -46,8 +45,8 @@ public function recipient()
/**
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function groups() {
return $this->hasMany(FriendFriendshipGroups::class, 'friendship_id');
public function grouped() {
return $this->hasMany(FriendshipGrouped::class, 'friendship_id');
}

/**
Expand Down Expand Up @@ -87,30 +86,28 @@ public function scopeWhereSender($query, $model)
/**
* @param $query
* @param Model $model
* @param string $groupSlug
* @param string $group
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeWhereGroup($query, $model, $groupSlug)
public function scopeWhereGroup($query, $model, $group)
{

$groupsPivotTable = config('friendships.tables.fr_groups_pivot');
$friendsPivotTable = config('friendships.tables.fr_pivot');
$groupsAvailable = config('friendships.groups', []);
$groupUserPivotTable = config('friendships.tables.fr_groups_pivot');
$groupsTable = config('friendships.tables.fr_groups');
$friendsPivotTable = config('friendships.tables.fr_pivot');

if ('' !== $groupSlug && isset($groupsAvailable[$groupSlug])) {

$groupId = $groupsAvailable[$groupSlug];

$query->join($groupsPivotTable, function ($join) use ($groupsPivotTable, $friendsPivotTable, $groupId, $model) {
$join->on($groupsPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id')
->where($groupsPivotTable . '.group_id', '=', $groupId)
->where(function ($query) use ($groupsPivotTable, $friendsPivotTable, $model) {
$query->where($groupsPivotTable . '.friend_id', '!=', $model->getKey())
->where($groupsPivotTable . '.friend_type', '=', $model->getMorphClass());
})
->orWhere($groupsPivotTable . '.friend_type', '!=', $model->getMorphClass());
});
if (!empty($group)) {

$query->join($groupUserPivotTable, $groupUserPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id')
->join($groupsTable, $groupsTable . '.id', '=', $groupUserPivotTable . '.group_id')
->where($groupsTable . '.slug', '=', $group)
->where(function ($query) use ($groupUserPivotTable, $friendsPivotTable, $model) {
$query->where($groupUserPivotTable . '.friend_id', '!=', $model->getKey())
->where($groupUserPivotTable . '.friend_type', '=', $model->getMorphClass());
})
->orWhere($groupUserPivotTable . '.friend_type', '!=', $model->getMorphClass());

}

return $query;
Expand Down
43 changes: 43 additions & 0 deletions src/Models/FriendshipGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Hootlex\Friendships\Models;

use Illuminate\Database\Eloquent\Model;

/**
* Class FriendshipGroup
* @package Hootlex\Friendships\Models
*/
class FriendshipGroup extends Model
{

/**
* @var array
*/
protected $guarded = ['id', 'created_at', 'updated_at'];

/**
* @var array
*/
protected $fillable = ['slug', 'name'];

/**
* @param array $attributes
*/
public function __construct(array $attributes = array())
{
$this->table = config('friendships.tables.fr_groups');

parent::__construct($attributes);
}

/**
* @return mixed
*/
public function grouped() {

return $this->hasMany('Hootlex\Friendships\Models\FriendshipGrouped', 'group_id');

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace Hootlex\Friendships\Models;

use Hootlex\Friendships\Status;
use Illuminate\Database\Eloquent\Model;

/**
* Class FriendFriendshipGroups
* Class FriendshipGrouped
* @package Hootlex\Friendships\Models
*/
class FriendFriendshipGroups extends Model
class FriendshipGrouped extends Model
{

/**
Expand All @@ -32,4 +31,22 @@ public function __construct(array $attributes = array())
parent::__construct($attributes);
}

/**
* @return mixed
*/
public function group() {

return $this->belongsTo('Hootlex\Friendships\Models\FriendshipGroup', 'group_id');

}

/**
* @return mixed
*/
public function friendship() {

return $this->belongsTo('Hootlex\Friendships\Models\Friendship', 'friendship_id');

}

}
Loading