Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Word filters #311

Open
wants to merge 2 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
43 changes: 43 additions & 0 deletions app/Database/Models/WordFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @license http://www.mybb.com/licenses/bsd3 BSD-3
*/

namespace MyBB\Core\Database\Models;

use Illuminate\Database\Eloquent\Model;
use McCool\LaravelAutoPresenter\HasPresenter;
use MyBB\Core\Presenters\WordFilterPresenter;

/**
* @property int id
*/
class WordFilter extends Model implements HasPresenter
{
/**
* @var string
*/
protected $table = 'parser_badwords';

/**
* @var array
*/
protected $casts = [
'id' => 'int',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation here is a little weird for some reason.

'find' => 'string',
'replace' => 'string',
];

public function getID() : int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer this styled as getId() personally, and I think our style guide says something similar.

{
return $this->id;
}

public function getPresenterClass() : string
{
return WordFilterPresenter::class;
}
}
34 changes: 34 additions & 0 deletions app/Database/Repositories/Eloquent/WordFilterRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @license http://www.mybb.com/licenses/bsd3 BSD-3
*/

namespace MyBB\Core\Database\Repositories\Eloquent;

use MyBB\Core\Database\Repositories\Collection;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Group the two Repository classes together like:

use MyBB\Core\Database\Repositories\{
    Collection,
    WordFilterRepositoryInterface
};

use MyBB\Core\Database\Models\WordFilter;
use MyBB\Core\Database\Repositories\WordFilterRepositoryInterface;

class WordFilterRepository implements WordFilterRepositoryInterface
{
/**
* @var WordFilter
*/
protected $wordFilter;

/**
* @param WordFilter $wordFilter
*/
public function __construct(WordFilter $wordFilter)
{
$this->wordFilter = $wordFilter;
}

public function getAll() : \Illuminate\Database\Eloquent\Collection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a collection interface/contract, it's better to state that we return an interface rather than a concrete type.

{
return $this->wordFilter->all();
}
}
21 changes: 21 additions & 0 deletions app/Database/Repositories/WordFilterRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @license http://www.mybb.com/licenses/bsd3 BSD-3
*/

namespace MyBB\Core\Database\Repositories;

//use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Collection;
use MyBB\Core\Database\Models\WordFilter;

interface WordFilterRepositoryInterface
{
// TODO: Implement this
//public function find(int $id) : WordFilter;

public function getAll() : \Illuminate\Database\Eloquent\Collection;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I think there's a collection interface/contract, it's better to state that we return an interface rather than a concrete type.

It's probably also best to have a paginate() method and paginate them in the ACP. If you have many filters, looking through them all is difficult.

}
85 changes: 85 additions & 0 deletions app/Http/Controllers/Admin/Users/WordFilterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @license http://www.mybb.com/licenses/bsd3 BSD-3
*/

namespace MyBB\Core\Http\Controllers\Admin\Users;

use Illuminate\Http\Request;
use Illuminate\View\View;
use DaveJamesMiller\Breadcrumbs\Manager as Breadcrumbs;
use MyBB\Core\Database\Repositories\WordFilterRepositoryInterface;
use MyBB\Core\Http\Controllers\Admin\AdminController;

class WordFilterController extends AdminController
{
/**
* @var Breadcrumbs
*/
private $breadcrumbs;

/**
* @var WordFilterRepositoryInterface
*/
private $wordFilterRepository;

public function __construct(
Breadcrumbs $breadcrumbs,
WordFilterRepositoryInterface $wordFilterRepository
) {
$this->breadcrumbs = $breadcrumbs;
$this->wordFilterRepository = $wordFilterRepository;
}

public function index() : \Illuminate\View\View
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I prefer to return a response interface in the function signature, in case something changes in the future.

{
$this->breadcrumbs->setCurrentRoute('admin.word_filters.index');

return view('admin.users.word_filters', [
'word_filter_items' => $this->wordFilterRepository->getAll(),
])->withActive('word-filters');
}

// TODO Implement this properly, this is a placeholder
public function add() : \Illuminate\View\View
{
$this->breadcrumbs->setCurrentRoute('admin.word_filters.index');

return view('admin.users.word_filters', [
'word_filter_items' => $this->wordFilterRepository->getAll(),
])->withActive('word-filters');
}

// TODO Implement this properly, this is a placeholder
/*public function addSubmit(WordFilterRequest $request) : \Illuminate\View\View
{
$this->breadcrumbs->setCurrentRoute('admin.word_filters.index');

return view('admin.users.word_filters', [
'word_filter_items' => $this->wordFilterRepository->getAll(),
])->withActive('word-filters');
}*/

// TODO Implement this properly, this is a placeholder
public function edit() : \Illuminate\View\View
{
$this->breadcrumbs->setCurrentRoute('admin.word_filters.index');

return view('admin.users.word_filters', [
'word_filter_items' => $this->wordFilterRepository->getAll(),
])->withActive('word-filters');
}

// TODO Implement this properly, this is a placeholder
public function delete() : \Illuminate\View\View
{
$this->breadcrumbs->setCurrentRoute('admin.word_filters.index');

return view('admin.users.word_filters', [
'word_filter_items' => $this->wordFilterRepository->getAll(),
])->withActive('word-filters');
}
}
20 changes: 20 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@
'as' => 'admin.users.add',
'uses' => 'Admin\Users\UserController@create',
]);
Route::get('/word-filters/',[
'as' => 'admin.word_filters.index',
'uses' => 'Admin\Users\WordFilterController@index',
]);
Route::get('/word-filters/add/',[
'as' => 'admin.word_filters.add',
'uses' => 'Admin\Users\WordFilterController@add',
]);
/*Route::post('/word-filters/add/',[
'as' => 'admin.word_filters.add',
'uses' => 'Admin\Users\WordFilterController@addSubmit',
]);*/
Route::get('/word-filters/edit/',[
'as' => 'admin.word_filters.edit',
'uses' => 'Admin\Users\WordFilterController@edit',
]);
Route::get('/word-filters/delete/',[
'as' => 'admin.word_filters.delete',
'uses' => 'Admin\Users\WordFilterController@delete',
]);
Route::get('/profile-fields', [
'as' => 'admin.users.profile_fields',
'uses' => 'Admin\Users\ProfileFieldController@profileFields',
Expand Down
25 changes: 5 additions & 20 deletions app/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ public function unreadConversations()
return $conversations;
}

/**
* @return bool
*/
public function isOnline() : bool
{
$minutes = $this->settings->get('wio.minutes', 15);
Expand All @@ -224,14 +221,11 @@ public function isOnline() : bool
if ($this->wrappedObject->last_page == 'auth/logout') {
return false;
}

// This user isn't online

if (new \DateTime($this->wrappedObject->last_visit) < new \DateTime("{$minutes} minutes ago")) {
return false;
}

// The user is online, now permissions

// We're either testing our own account or have permissions to view everyone
if ($this->permissionChecker->hasPermission('user', null, 'canViewAllOnline')
|| $this->guard->user()->id == $this->wrappedObject->id
Expand All @@ -240,21 +234,12 @@ public function isOnline() : bool
}

// Next we need to get the setting for this user

// First get the id of our setting
$settingId = Setting::where('name', 'user.showonline')->first()->id;

// Now the value
$settingValue = SettingValue::where('user_id', '=', $this->wrappedObject->id)
->where('setting_id', '=', $settingId)->first();

// Either the value isn't set (good) or true (better), let's show this user as online
if ($settingValue == null || $settingValue->value == true) {
return true;
}

// Still here? Then the viewing user doesn't have the permissions and we show him as offline
return false;
->where('setting_id', '=', $settingId)
->first();

return ($settingValue == null || $settingValue->value == true);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions app/Presenters/WordFilterPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @license http://www.mybb.com/licenses/bsd3 BSD-3
*/

namespace MyBB\Core\Presenters;

use Illuminate\Contracts\Auth\Guard;
use McCool\LaravelAutoPresenter\BasePresenter;
use MyBB\Core\Database\Models\WordFilter as WordFilterModel;
use MyBB\Core\Form\RenderableInterface;

class WordFilterPresenter extends BasePresenter
{
/**
* @var Guard
*/
protected $guard;

public function __construct(WordFilterModel $resource, Guard $guard) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ should be on the line below.

parent::__construct($resource);
$this->guard = $guard;
}

public function find() : string
{
return $this->wrappedObject->find;
}

public function replace() : string
{
return $this->wrappedObject->replace;
}
}
7 changes: 7 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use MyBB\Core\Database\Repositories\Eloquent\ProfileFieldGroupRepository;
use MyBB\Core\Database\Repositories\Eloquent\ProfileFieldOptionRepository;
use MyBB\Core\Database\Repositories\Eloquent\ProfileFieldRepository;
use MyBB\Core\Database\Repositories\Eloquent\WordFilterRepository;
use MyBB\Core\Database\Repositories\Eloquent\RoleRepository;
use MyBB\Core\Database\Repositories\Eloquent\SearchRepository;
use MyBB\Core\Database\Repositories\Eloquent\TasksRepository;
Expand All @@ -41,6 +42,7 @@
use MyBB\Core\Database\Repositories\ProfileFieldGroupRepositoryInterface;
use MyBB\Core\Database\Repositories\ProfileFieldOptionRepositoryInterface;
use MyBB\Core\Database\Repositories\ProfileFieldRepositoryInterface;
use MyBB\Core\Database\Repositories\WordFilterRepositoryInterface;
use MyBB\Core\Database\Repositories\RoleRepositoryInterface;
use MyBB\Core\Database\Repositories\SearchRepositoryInterface;
use MyBB\Core\Database\Repositories\TasksRepositoryInterface;
Expand Down Expand Up @@ -134,6 +136,11 @@ public function register()
UserProfileFieldRepositoryInterface::class,
UserProfileFieldRepository::class
);

$this->app->bind(
WordFilterRepositoryInterface::class,
WordFilterRepository::class
);

$this->app->bind(
ConversationRepositoryInterface::class,
Expand Down
16 changes: 16 additions & 0 deletions resources/lang/admin/en/word_filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @license http://www.mybb.com/licenses/bsd3 BSD-3
*/

return [
'title' => 'Word Filters',
'add_button' => 'Add Word Filter',
'find_field' => 'Find',
'replace_field' => 'Replacement',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't bother trying to line all the values up, it just gets annoying trying to maintain it later down the line 😉

'edit' => 'Edit',
'delete' => 'Delete',
];
37 changes: 37 additions & 0 deletions resources/views/admin/users/word_filters.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% extends "layouts.admin" %}
{% block title %}{% endblock %}
{% block contents %}
<div class="admin-body__header">
<h1 class="admin-body__header__title">{{ trans('admin::word_filters.title') }}</h1>
<div class="admin-body__header__buttons">
<a href="{{ url_route('admin.word_filters.add') }}" class="button"><i class="fa fa-plus"></i> {{ trans('admin::word_filters.add_button') }}</a>
</div>
</div>
<div class="admin-body__content" style="display: flex;flex-direction: row;">
<table class="admin-table admin-table--bordered users" style="width:79%;">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paging @Eric-Jackson 😛

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned to @Azareal in Discord, we will want to avoid inline style and tables. For now the classes already available on these elements should work fine for removing that inline stuff, the tables can be replaced later so I wouldn't worry about them at this moment. The class "users" should be replaced with something more appropriate word-filters.

<thead>
<tr>
<th>{{ trans('admin::word_filters.find_field') }}</th>
<th>{{ trans('admin::word_filters.replace_field') }}</th>
<th>{{ trans('admin::general.actions_field') }}</th>
</tr>
</thead>
<tbody>
{% for filter in word_filter_items %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea to use an else block to display a message if the collection is empty.

<tr>
<td>{{ filter.find }}</td>
<td>{{ filter.replace }}</td>
<td>
<a href="{{ url_route('admin.word_filters.edit', {'id': filter.id}) }}" class="button button--secondary"><i class="fa fa-pencil"></i> {{ trans('admin::word_filters.edit') }}</a>

{{ form_open({'route': ['admin.word_filters.delete'], 'method': 'post', 'style': 'display: inline;'}) }}
<input type="hidden" name="word_filter_id" value="{{ filter.id }}">
<button type="submit" class="button button--secondary button--danger"><i class="fa fa-remove"></i> {{ trans('admin::word_filters.delete') }}</button>
{{ form_close() }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
Loading