Skip to content

Commit

Permalink
Merge pull request #1852 from Obi-Wana/master
Browse files Browse the repository at this point in the history
(Feature) Add Internals System for Staff
  • Loading branch information
HDVinnie committed Jul 31, 2021
2 parents b0a0974 + 47e1635 commit de9f3d9
Show file tree
Hide file tree
Showing 13 changed files with 589 additions and 18 deletions.
5 changes: 3 additions & 2 deletions app/Http/Controllers/PageController.php
Expand Up @@ -58,9 +58,10 @@ public function staff(): \Illuminate\Contracts\View\Factory | \Illuminate\View\V
*/
public function internal(): \Illuminate\Contracts\View\Factory | \Illuminate\View\View
{
$internal = DB::table('users')->leftJoin('groups', 'users.group_id', '=', 'groups.id')->select(['users.id', 'users.title', 'users.username', 'groups.name', 'groups.color', 'groups.icon'])->where('groups.is_internal', 1)->get();
$internal_group = DB::table('internals')->get();
$internal_user = DB::table('users')->leftJoin('groups', 'users.group_id', '=', 'groups.id')->select(['users.id', 'users.title', 'users.username', 'groups.name', 'groups.color', 'groups.icon', 'users.internal_id'])->where('groups.is_internal', 1)->get();

return \view('page.internal', ['internal' => $internal]);
return \view('page.internal', ['internal_user' => $internal_user, 'internal_group' => $internal_group]);
}

/**
Expand Down
159 changes: 159 additions & 0 deletions app/Http/Controllers/Staff/InternalController.php
@@ -0,0 +1,159 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/

namespace App\Http\Controllers\Staff;

use App\Http\Controllers\Controller;
use App\Models\Internal;
use Carbon\Carbon;
use Illuminate\Http\Request;

/**
* @see \Tests\Feature\Http\Controllers\Staff\GroupControllerTest
*/
class InternalController extends Controller
{
/**
* Display All Internal Groups.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(Request $request)
{
$user = $request->user();
\abort_unless($user->group->is_modo, 403);

$internals = Internal::all()->sortBy('name');

return \view('Staff.internals.index', ['internals' => $internals]);
}

/**
* Edit A group.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\UsersVIP $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function edit(Request $request, $id)
{
$user = $request->user();
\abort_unless($user->group->is_modo, 403);

$date = Carbon::now();
$internal = Internal::findOrFail($id);

return \view('Staff.internals.edit', ['internal' => $internal]);
}

/**
* Save a group change.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\UsersVIP $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, $id)
{
$user = $request->user();
\abort_unless($user->group->is_modo, 403);

$internal = Internal::findOrFail($id);

$internal->name = $request->input('name');
$internal->icon = $request->input('icon');
$internal->effect = $request->input('effect');

$v = \validator($internal->toArray(), [
'name' => 'required',
'icon' => 'required',
'effect' => 'required',
]);

if ($v->fails()) {
return \redirect()->route('staff.internals.index')
->withErrors($v->errors());
}
$internal->save();

return \redirect()->route('staff.internals.index')
->withSuccess('Internal Group Was Updated Successfully!');
}

/**
* Internal Add Form.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create()
{
return \view('Staff.internals.create');
}

/**
* Store A New Internal Group.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{
$user = $request->user();
\abort_unless($user->group->is_admin, 403);

$internal = new Internal();
$internal->name = $request->input('name');
$internal->icon = $request->input('icon');
$internal->effect = $request->input('effect');

$v = \validator($internal->toArray(), [
'name' => 'required|unique:internals',
'icon',
'effect',
]);

if ($v->fails()) {
return \redirect()->route('staff.internals.index')
->withErrors($v->errors());
}
$internal->save();

return \redirect()->route('staff.internals.index')
->withSuccess('New Internal Group added!');
}

/**
* Delete A Internal Group.
*
* @param \Illuminate\Http\Request $request
* @param $commentId
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Request $request, $id)
{
$user = $request->user();
$internal = Internal::findOrFail($id);

\abort_unless($user->group->is_admin, 403);
$internal->delete();

return \redirect()->route('staff.internals.index')
->withSuccess('Group Has Been Removed.');
}
}
10 changes: 7 additions & 3 deletions app/Http/Controllers/Staff/UserController.php
Expand Up @@ -17,6 +17,7 @@
use App\Models\Comment;
use App\Models\Follow;
use App\Models\Group;
use App\Models\Internal;
use App\Models\Invite;
use App\Models\Like;
use App\Models\Message;
Expand Down Expand Up @@ -61,12 +62,14 @@ public function settings($username): \Illuminate\Contracts\View\Factory | \Illum
{
$user = User::where('username', '=', $username)->firstOrFail();
$groups = Group::all();
$internals = Internal::all();
$notes = Note::where('user_id', '=', $user->id)->latest()->paginate(25);

return \view('Staff.user.edit', [
'user' => $user,
'groups' => $groups,
'notes' => $notes,
'user' => $user,
'groups' => $groups,
'internals' => $internals,
'notes' => $notes,
]);
}

Expand Down Expand Up @@ -116,6 +119,7 @@ public function edit(Request $request, $username)
$user->title = $request->input('title');
$user->about = $request->input('about');
$user->group_id = (int) $request->input('group_id');
$user->internal_id = (int) $request->input('internal_id');
$user->save();

return \redirect()->route('users.show', ['username' => $user->username])
Expand Down
65 changes: 65 additions & 0 deletions app/Models/Internal.php
@@ -0,0 +1,65 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/

namespace App\Models;

use App\Traits\Auditable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
* App\Models\Group.
*
* @property int $id
* @property string $name
* @property string $icon
* @property string $effect
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Internal newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Internal newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Inernal query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Internal whereIcon($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Internal whereEffect($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Internal whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Internal whereName($value)
* @mixin \Eloquent
*/
class Internal extends Model
{
use HasFactory;
use Auditable;

/**
* The Attributes That Aren't Mass Assignable.
*
* @var array
*/
protected $guarded = ['id'];

/**
* Indicates If The Model Should Be Timestamped.
*
* @var bool
*/
public $timestamps = false;

/**
* Has Many Users.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function users()
{
return $this->hasMany(User::class);
}
}
10 changes: 10 additions & 0 deletions app/Models/User.php
Expand Up @@ -84,6 +84,16 @@ public function group()
]);
}

/**
* Belongs To A Internal Group.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function internal()
{
return $this->belongsTo(Internal::class, 'internal_id', 'id', 'name');
}

/**
* Belongs To A Chatroom.
*
Expand Down
45 changes: 45 additions & 0 deletions database/migrations/2021_04_18_085155_add_internals_table.php
@@ -0,0 +1,45 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddInternalsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//Create Table
Schema::create('internals', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('icon')->default('none');
$table->string('effect')->default('none');
});

//Update Users Table
Schema::table('users', function (Blueprint $table) {
$table->integer('internal_id')->index('fk_users_internal_idx')->after('group_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//Delete Table
Schema::dropIfExists('internals');

//Update Users Table
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('internal_id');
});
}
}

0 comments on commit de9f3d9

Please sign in to comment.