Skip to content

Commit 8857767

Browse files
author
Riari
committed
Make numerous improvements and tidy up formatting
1 parent 3702ced commit 8857767

27 files changed

+457
-447
lines changed

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This package is currently under heavy development. Feel free to post issues and
3838
* Multilingual (English and French translations available out of the box)
3939

4040
### Planned features
41+
4142
* Read/unread thread status (with icons and 'new posts' page)
4243

4344
### Demo
@@ -111,7 +112,7 @@ You can then adjust the views however you like. I suggest editing the master vie
111112

112113
### Regarding permissions
113114

114-
The default permission callbacks don't allow users to perform certain actions such as deleting threads or posts; you'll need to modify them to return TRUE based on your own criteria. For example, if you use [Zizaco/entrust](https://github.com/Zizaco/entrust), you might change your `delete_threads` callback to return `Entrust::can('forum_threads_delete');`, allowing users with a role that grants the `forum_threads_delete` permission to delete threads.
115+
The default permission callbacks don't allow users to perform certain actions such as deleting threads or posts; you'll need to modify them to return true based on your own criteria. For example, if you use [Zizaco/entrust](https://github.com/Zizaco/entrust), you might change your `delete_threads` callback to return `Entrust::can('forum_threads_delete');`, allowing users with a role that grants the `forum_threads_delete` permission to delete threads.
115116

116117
Note that the default set of views include links for deleting, editing and replying, and their visibility is controlled by the permission callbacks.
117118

src/Riari/Forum/Commands/InstallCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function fire()
2727
private function installController($name, $parent)
2828
{
2929
$file = $this->laravel->path.'/controllers/'.$name.'.php';
30-
if(file_exists($file))
30+
if (file_exists($file))
3131
{
3232
$this->info('File app/controllers/'.$name.' Exists. Action aborted.');
3333
return;

src/Riari/Forum/Controllers/BaseController.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<?php namespace Riari\Forum\Controllers;
22

3+
use App;
4+
use Config;
5+
use Controller;
6+
use Input;
7+
use Redirect;
38
use Riari\Forum\Repositories\Categories;
49
use Riari\Forum\Repositories\Threads;
510
use Riari\Forum\Repositories\Posts;
611
use Riari\Forum\Libraries\AccessControl;
712
use Riari\Forum\Libraries\Alerts;
813
use Riari\Forum\Libraries\Validation;
9-
10-
use App;
11-
use Config;
12-
use Controller;
13-
use Input;
14-
use Redirect;
1514
use Route;
1615
use View;
1716
use Validator;
@@ -36,21 +35,14 @@ public function __construct(Categories $categories, Threads $threads, Posts $pos
3635
protected function getCurrentUser()
3736
{
3837
$current_user_callback = Config::get('forum::integration.current_user');
39-
40-
$user = $current_user_callback();
41-
if (is_object($user) && get_class($user) == Config::get('forum::integration.user_model'))
42-
{
43-
return $user;
44-
}
45-
46-
return NULL;
38+
return $current_user_callback();
4739
}
4840

4941
protected function check404()
5042
{
51-
foreach($this->collections as $item)
43+
foreach ($this->collections as $item)
5244
{
53-
if($item == NULL)
45+
if ($item == null)
5446
{
5547
App::abort(404);
5648
}
@@ -89,13 +81,13 @@ protected function load($select = array(), $category_with = array())
8981

9082
$route_name = Route::current()->getAction()['as'];
9183

92-
foreach($select as $model => $id)
84+
foreach ($select as $model => $id)
9385
{
9486
$with = ($model == 'category') ? $category_with : array();
9587

9688
$this->collections[$model] = $this->$map_model_repos[$model]->getByID($id, $with);
9789

98-
if(isset($map_route_permissions[$route_name]) && $model == $map_route_models[$route_name])
90+
if (isset($map_route_permissions[$route_name]) && $model == $map_route_models[$route_name])
9991
{
10092
AccessControl::check($this->collections[$model], $map_route_permissions[$route_name]);
10193
}
@@ -190,7 +182,7 @@ public function postReplyToThread($categoryID, $categoryAlias, $threadID, $threa
190182
$user = $this->getCurrentUser();
191183

192184
$this->load(['category' => $categoryID, 'thread' => $threadID]);
193-
185+
194186
if (!$this->collections['thread']->canReply)
195187
{
196188
return Redirect::to($this->collections['thread']->route);

src/Riari/Forum/ForumServiceProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php namespace Riari\Forum;
22

3+
use Config;
34
use Illuminate\Support\ServiceProvider;
45
use Riari\Forum\Commands\InstallCommand;
56

6-
use Config;
7-
87
class ForumServiceProvider extends ServiceProvider {
98

109
/**

src/Riari/Forum/Libraries/AccessControl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php namespace Riari\Forum\Libraries;
22

3-
use Config;
43
use App;
4+
use Config;
55

66
class AccessControl {
77

8-
public static function check($context, $permission, $abort = TRUE)
8+
public static function check($context, $permission, $abort = true)
99
{
1010
// Fetch the current user
1111
$user_callback = Config::get('forum::integration.current_user');
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php namespace Riari\Forum\Libraries;
22

3-
use Riari\Forum\Libraries\Alerts;
4-
53
use Config;
64
use Input;
5+
use Riari\Forum\Libraries\Alerts;
76
use Validator;
87

98
class Validation {
109

1110
public static function processValidationMessages($messages)
1211
{
13-
foreach($messages as $message)
12+
foreach ($messages as $message)
1413
{
1514
Alerts::add('danger', $message);
1615
}
@@ -21,16 +20,13 @@ public static function check($type = 'thread')
2120
$rules = Config::get('forum::preferences.validation_rules');
2221
$validator = Validator::make(Input::all(), $rules[$type]);
2322

24-
if ($validator->passes())
25-
{
26-
return TRUE;
27-
}
28-
else
23+
if (!$validator->passes())
2924
{
3025
self::processValidationMessages($validator->messages()->all());
31-
32-
return FALSE;
26+
return false;
3327
}
28+
29+
return false;
3430
}
3531

3632
}

src/Riari/Forum/Models/Category.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php namespace Riari\Forum\Models;
22

3+
use Config;
34
use Riari\Forum\Models\Thread;
45
use Riari\Forum\Libraries\AccessControl;
5-
6-
use Config;
76
use Str;
87

98
class Category extends BaseModel {
109

1110
protected $table = 'forum_categories';
1211
public $timestamps = false;
13-
protected $appends = ['threadCount', 'replyCount', 'Route', 'newThreadRoute'];
12+
protected $appends = ['threadCount', 'replyCount', 'route', 'newThreadRoute'];
1413

1514
public function parentCategory()
1615
{
@@ -24,19 +23,29 @@ public function subcategories()
2423

2524
public function threads()
2625
{
27-
return $this->hasMany('\Riari\Forum\Models\Thread', 'parent_category')->with('category', 'posts')->orderBy('pinned', 'desc')->orderBy('updated_at', 'desc');
26+
return $this->hasMany('\Riari\Forum\Models\Thread', 'parent_category')->with('category', 'posts');
2827
}
2928

3029
public function getThreadsPaginatedAttribute()
3130
{
32-
return $this->threads()->paginate(Config::get('forum::preferences.threads_per_category'));
31+
return $this->threads()->orderBy('pinned', 'desc')->orderBy('updated_at', 'desc')->paginate(Config::get('forum::preferences.threads_per_category'));
3332
}
3433

3534
public function getPageLinksAttribute()
3635
{
3736
return $this->threadsPaginated->links(Config::get('forum::preferences.pagination_view'));
3837
}
3938

39+
public function getNewestThreadAttribute()
40+
{
41+
return $this->threads()->orderBy('created_at', 'desc')->first();
42+
}
43+
44+
public function getLatestActiveThreadAttribute()
45+
{
46+
return $this->threads()->orderBy('updated_at', 'desc')->first();
47+
}
48+
4049
public function getThreadCountAttribute()
4150
{
4251
return $this->rememberAttribute('threadCount', function(){
@@ -49,7 +58,7 @@ public function getReplyCountAttribute()
4958
return $this->rememberAttribute('replyCount', function(){
5059
$replyCount = 0;
5160

52-
$threads = $this->threads()->get(array('id'));
61+
$threads = $this->threads()->get(['id']);
5362

5463
foreach ($threads as $thread) {
5564
$replyCount += $thread->posts->count();
@@ -81,12 +90,12 @@ public function getNewThreadRouteAttribute()
8190

8291
public function getCanViewAttribute()
8392
{
84-
return AccessControl::check($this, 'access_category', FALSE);
93+
return AccessControl::check($this, 'access_category', false);
8594
}
8695

8796
public function getCanPostAttribute()
8897
{
89-
return AccessControl::check($this, 'create_threads', FALSE);
98+
return AccessControl::check($this, 'create_threads', false);
9099
}
91100

92101
}

src/Riari/Forum/Models/Post.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php namespace Riari\Forum\Models;
22

3+
use Config;
34
use Illuminate\Database\Eloquent\SoftDeletingTrait;
45
use Riari\Forum\Libraries\AccessControl;
5-
6-
use Config;
76
use Str;
87

98
class Post extends BaseModel {
@@ -61,12 +60,12 @@ public function getDeleteRouteAttribute()
6160

6261
public function getCanEditAttribute()
6362
{
64-
return AccessControl::check($this, 'edit_post', FALSE);
63+
return AccessControl::check($this, 'edit_post', false);
6564
}
6665

6766
public function getCanDeleteAttribute()
6867
{
69-
return AccessControl::check($this, 'delete_posts', FALSE);
68+
return AccessControl::check($this, 'delete_posts', false);
7069
}
7170

7271
}

src/Riari/Forum/Models/Thread.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php namespace Riari\Forum\Models;
22

3+
use Config;
34
use Illuminate\Database\Eloquent\SoftDeletingTrait;
5+
use Redirect;
46
use Riari\Forum\Libraries\AccessControl;
57
use Riari\Forum\Libraries\Alerts;
6-
7-
use Config;
8-
use Redirect;
98
use Str;
109

1110
class Thread extends BaseModel {
@@ -102,22 +101,22 @@ public function getDeleteRouteAttribute()
102101

103102
public function getCanReplyAttribute()
104103
{
105-
return AccessControl::check($this, 'reply_to_thread', FALSE);
104+
return AccessControl::check($this, 'reply_to_thread', false);
106105
}
107106

108107
public function getCanPinAttribute()
109108
{
110-
return AccessControl::check($this, 'pin_threads', FALSE);
109+
return AccessControl::check($this, 'pin_threads', false);
111110
}
112111

113112
public function getCanLockAttribute()
114113
{
115-
return AccessControl::check($this, 'lock_threads', FALSE);
114+
return AccessControl::check($this, 'lock_threads', false);
116115
}
117116

118117
public function getCanDeleteAttribute()
119118
{
120-
return AccessControl::check($this, 'delete_threads', FALSE);
119+
return AccessControl::check($this, 'delete_threads', false);
121120
}
122121

123122
public function toggle($property)

src/Riari/Forum/Repositories/BaseRepository.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php namespace Riari\Forum\Repositories;
22

3-
use stdClass;
43
use Config;
54

65
abstract class BaseRepository {

0 commit comments

Comments
 (0)