/
area.php
194 lines (153 loc) · 4.65 KB
/
area.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Forum Area model
*
* @package Forum
* @author Antti Qvickström
* @copyright (c) 2010-2011 Antti Qvickström
* @license http://www.opensource.org/licenses/mit-license.php MIT license
*/
class Anqh_Model_Forum_Area extends AutoModeler_ORM implements Permission_Interface {
/** Permission to post new topic */
const PERMISSION_POST = 'post';
/** Everybody can read area */
const READ_NORMAL = 0;
/** Only memberes can read area */
const READ_MEMBERS = 1;
/** Visible area */
const STATUS_NORMAL = 0;
/** Hidden area */
const STATUS_HIDDEN = 1;
/** Normal area */
const TYPE_NORMAL = 0;
/** Topics bound to foreign model */
const TYPE_BIND = 1;
/** Private area */
const TYPE_PRIVATE = 2;
/** Members can start new topics */
const WRITE_NORMAL = 0;
/** Only admins can start new topics */
const WRITE_ADMINS = 1;
protected $_table_name = 'forum_areas';
protected $_data = array(
'id' => null,
'forum_group_id' => null,
'name' => null,
'description' => null,
'sort' => 0,
'created' => null,
'author_id' => null,
'access_read' => self::READ_NORMAL,
'access_write' => self::WRITE_NORMAL,
'status' => self::STATUS_NORMAL,
'area_type' => self::TYPE_NORMAL,
'bind' => null,
'post_count' => null,
'topic_count' => null,
'last_topic_id' => null,
);
protected $_has_many = array(
'forum_topics'
);
protected $_rules = array(
'forum_group_id' => array('digit'),
'name' => array('not_empty', 'max_length' => array(':value', 64)),
'description' => array('max_length' => array(':value', 250)),
'access_read' => array('not_empty', 'in_array' => array(':value', array(self::READ_MEMBERS, self::READ_NORMAL))),
'access_write' => array('not_empty', 'in_array' => array(':value', array(self::WRITE_ADMINS, self::WRITE_NORMAL))),
'status' => array('not_empty', 'in_array' => array(':value', array(self::STATUS_HIDDEN, self::STATUS_NORMAL))),
'area_type' => array('not_empty', 'in_array' => array(':value', array(self::TYPE_PRIVATE, self::TYPE_BIND, self::TYPE_NORMAL))),
);
/** @var array User editable fields */
public static $editable_fields = array(
'group', 'name', 'description', 'sort', 'access_read', 'access_write', 'status', 'area_type', 'bind'
);
/**
* Find area's paginated active topics
*
* @param integer $offset
* @param integer $limit
* @return Model_Forum_Topic[]
*/
public function find_active_topics($offset = 0, $limit = 10) {
$topic = new Model_Forum_Topic();
return $topic->load(
DB::select_array($topic->fields())
->where('forum_area_id', '=', $this->id)
->order_by('sticky', 'DESC')
->order_by('last_posted', 'DESC')
->offset(max(0, $offset)),
$limit
);
}
/**
* Get list of possible model bindings
*
* @param boolean|string true = short list, false = full list, string = specific bind
* @return array
*/
public static function get_binds($bind = true) {
$config = Kohana::$config->load('forum.binds');
if ($bind === true) {
// Short list for selects etc
$list = array();
foreach ($config as $type => $data) {
$list[$type] = $data['name'];
}
return $list;
} else if ($bind === false) {
// Full bind config
return $config;
} else if (is_string($bind)) {
// Specific config
return Arr::get($config, $bind);
}
}
/**
* Get comment image
*
* @return Model_Forum_Group
*/
public function group() {
try {
return $this->forum_group_id ? Model_Forum_Group::factory($this->forum_group_id) : null;
} catch (AutoModeler_Exception $e) {
return null;
}
}
/**
* Check permission
*
* @param string $permission
* @param Model_User $user
* @return boolean
*/
public function has_permission($permission, $user) {
switch ($permission) {
case self::PERMISSION_CREATE:
case self::PERMISSION_DELETE:
case self::PERMISSION_UPDATE:
return $user && $user->has_role('admin');
case self::PERMISSION_POST:
return $user
&& ($this->access_write != self::WRITE_ADMINS
&& $this->area_type != self::TYPE_BIND
&& $this->status != self::STATUS_HIDDEN
|| $user->has_role('admin')
);
case self::PERMISSION_READ:
return $this->status == self::STATUS_NORMAL
&& $this->area_type != self::TYPE_PRIVATE
&& ($this->access_read == self::READ_NORMAL || $user);
}
return false;
}
/**
* Get area last topic
*
* @return Model_Forum_Topic
*/
public function last_topic() {
return Model_Forum_Topic::factory($this->last_topic_id);
}
}