forked from segoddnja/Comments-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommentsModule.php
127 lines (115 loc) · 3.8 KB
/
CommentsModule.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
<?php
/**
* Comments module class file.
*
* @author Dmitry Zasjadko <[email protected]>
* @link https://github.com/segoddnja/ECommentable
* @version 1.0
* @package Comments module
*
*/
class CommentsModule extends CWebModule
{
public $defaultController = 'comment';
/*
* captcha action route
*/
const CAPTCHA_ACTION_ROUTE = 'comments/comment/captcha';
/*
* delete comment action route
*/
const DELETE_ACTION_ROUTE = 'comments/comment/delete';
/*
* approve comment action route
*/
const APPROVE_ACTION_ROUTE = 'comments/comment/approve';
/**
* Commentable models
* @var array commentableModels
*/
public $commentableModels = array();
/**
* Action for posting comments, where add comment form is submited
* @var postCommentAction
*/
public $postCommentAction;
/**
* Settings for User model, used in application
* @var userSettings
*/
public $userConfig;
/**
* Expression to access controller action
* @var isAdminExpr
*/
public $isAdminExpr = 'false';
/**
* Default config for model
* @var _defaultModelConfig
*/
protected $_defaultModelConfig = array(
//only registered users can post comments
'registeredOnly' => false,
'useCaptcha' => false,
//allow comment tree
'allowSubcommenting' => true,
//display comments after moderation
'premoderate' => false,
//action for postig comment
'postCommentAction' => 'comments/comment/postComment',
//super user condition(display comment list in admin view and automoderate comments)
'isSuperuser'=>'false',
//order direction for comments
'orderComments'=>'DESC',
//settings for comments page url
'pageUrl'=>null
);
public function init()
{
// import the module-level models and components
$this->setImport(array(
'comments.models.*',
'comments.components.*',
));
}
/*
* Display ECommentsWidget for defined model and controller
* @param CActiveRecord $model
* @param CController $controller
*/
public function outComments($model, $controller)
{
//if this model is commentable
if(($modelConfig = $this->getModelConfig($model)) !== null)
{
$this->outCommentsList($model, $controller);
}
}
/*
* Returns settings for model. Model can be CActiveRecord instance or string.
* If there is no model settings, then return null
* @param mixed $model
* @return mixed
*/
public function getModelConfig($model)
{
$modelName = is_object($model) ? get_class($model) : $model;
$modelConfig = array();
if(in_array($modelName, $this->commentableModels) || isset($this->commentableModels[$modelName]))
{
$modelConfig = isset($this->commentableModels[$modelName]) ?
array_merge($this->_defaultModelConfig, $this->commentableModels[$modelName]) :
$this->_defaultModelConfig;
}
return $modelConfig;
}
/*
* Sets default config for models
* @param array $config
*/
public function setDefaultModelConfig($config)
{
if(is_array($config))
$this->_defaultModelConfig = array_merge($this->_defaultModelConfig, $config);
}
}