1
|
|
|
<?php namespace XoopsModules\Newbb; |
|
|
|
|
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* NewBB 5.0x, the forum module for XOOPS project |
5
|
|
|
* |
6
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
7
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
8
|
|
|
* @author Taiwen Jiang (phppp or D.J.) <[email protected]> |
9
|
|
|
* @since 4.00 |
10
|
|
|
* @package module::newbb |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
use XoopsModules\Newbb; |
14
|
|
|
use XoopsModules\Xoopspoll; |
|
|
|
|
15
|
|
|
|
16
|
|
|
// defined('XOOPS_ROOT_PATH') || die('Restricted access'); |
|
|
|
|
17
|
|
|
|
18
|
|
|
defined('NEWBB_FUNCTIONS_INI') || include $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php'); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Topic |
22
|
|
|
*/ |
23
|
|
|
class Topic extends \XoopsObject |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
parent::__construct(); |
31
|
|
|
$this->initVar('topic_id', XOBJ_DTYPE_INT); |
|
|
|
|
32
|
|
|
$this->initVar('topic_title', XOBJ_DTYPE_TXTBOX); |
|
|
|
|
33
|
|
|
$this->initVar('topic_poster', XOBJ_DTYPE_INT); |
34
|
|
|
$this->initVar('topic_time', XOBJ_DTYPE_INT); |
35
|
|
|
$this->initVar('topic_views', XOBJ_DTYPE_INT); |
36
|
|
|
$this->initVar('topic_replies', XOBJ_DTYPE_INT); |
37
|
|
|
$this->initVar('topic_last_post_id', XOBJ_DTYPE_INT); |
38
|
|
|
$this->initVar('forum_id', XOBJ_DTYPE_INT); |
39
|
|
|
$this->initVar('topic_status', XOBJ_DTYPE_INT); |
40
|
|
|
$this->initVar('type_id', XOBJ_DTYPE_INT); |
41
|
|
|
$this->initVar('topic_sticky', XOBJ_DTYPE_INT); |
42
|
|
|
$this->initVar('topic_digest', XOBJ_DTYPE_INT); |
43
|
|
|
$this->initVar('digest_time', XOBJ_DTYPE_INT); |
44
|
|
|
$this->initVar('approved', XOBJ_DTYPE_INT); |
45
|
|
|
$this->initVar('poster_name', XOBJ_DTYPE_TXTBOX); |
46
|
|
|
$this->initVar('rating', XOBJ_DTYPE_OTHER); |
|
|
|
|
47
|
|
|
$this->initVar('votes', XOBJ_DTYPE_INT); |
48
|
|
|
$this->initVar('topic_haspoll', XOBJ_DTYPE_INT); |
49
|
|
|
$this->initVar('poll_id', XOBJ_DTYPE_INT); |
50
|
|
|
$this->initVar('topic_tags', XOBJ_DTYPE_SOURCE); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// irmtfan add LAST_INSERT_ID to enhance the mysql performances |
54
|
|
|
public function incrementCounter() |
55
|
|
|
{ |
56
|
|
|
$sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('newbb_topics') . ' SET topic_views = LAST_INSERT_ID(topic_views + 1) WHERE topic_id =' . $this->getVar('topic_id'); |
57
|
|
|
$GLOBALS['xoopsDB']->queryF($sql); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create full title of the topic |
62
|
|
|
* |
63
|
|
|
* the title is composed of [type_name] if type_id is greater than 0 plus topic_title |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
|
|
public function getFullTitle() |
67
|
|
|
{ |
68
|
|
|
$topic_title = $this->getVar('topic_title'); |
69
|
|
|
if (!$this->getVar('type_id')) { |
70
|
|
|
return $topic_title; |
71
|
|
|
} |
72
|
|
|
$typeHandler = Newbb\Helper::getInstance()->getHandler('Type'); |
73
|
|
|
if (!$typeObject = $typeHandler->get($this->getVar('type_id'))) { |
74
|
|
|
return $topic_title; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
include_once __DIR__ . '/../include/functions.topic.php'; |
78
|
|
|
|
79
|
|
|
return getTopicTitle($topic_title, $typeObject->getVar('type_name'), $typeObject->getVar('type_color')); |
80
|
|
|
} |
81
|
|
|
// START irmtfan loadOldPoll function |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Load functions needed for old xoopspoll (older than version 1.4 by zyspec) and umfrage modules |
85
|
|
|
* |
86
|
|
|
* @access public |
87
|
|
|
* @param string $pollModule dirname of the poll module |
88
|
|
|
* @return string|false $classPoll = the name of the old poll class eg: "XoopsPoll" | "Umfrage" |
89
|
|
|
*/ |
90
|
|
|
|
91
|
|
|
public function loadOldPoll($pollModule = null) |
92
|
|
|
{ |
93
|
|
|
static $classPoll = false; |
94
|
|
|
if ($classPoll && empty($pollModule)) { |
95
|
|
|
return $classPoll; |
96
|
|
|
} |
97
|
|
|
$newbbConfig = newbbLoadConfig(); |
98
|
|
|
if (!empty($pollModule)) { |
99
|
|
|
$newbbConfig['poll_module'] = $pollModule; |
100
|
|
|
} |
101
|
|
|
// $relPath = $GLOBALS['xoops']->path('modules/' . $newbbConfig['poll_module'] . '/class/' . $newbbConfig['poll_module']); |
|
|
|
|
102
|
|
|
// include_once $relPath . '.php'; |
103
|
|
|
// include_once $relPath . 'option.php'; |
104
|
|
|
// include_once $relPath . 'log.php'; |
105
|
|
|
// include_once $relPath . 'renderer.php'; |
106
|
|
|
$classes = get_declared_classes(); |
107
|
|
|
foreach (array_reverse($classes) as $class) { |
108
|
|
|
if (strtolower($class) == $newbbConfig['poll_module']) { |
109
|
|
|
$classPoll = $class; |
110
|
|
|
|
111
|
|
|
return $classPoll; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
// END irmtfan loadOldPoll function |
118
|
|
|
// START irmtfan add deletePoll function |
119
|
|
|
/** |
120
|
|
|
* delete a poll in database |
121
|
|
|
* |
122
|
|
|
* @access public |
123
|
|
|
* @param int $poll_id |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function deletePoll($poll_id) |
127
|
|
|
{ |
128
|
|
|
if (empty($poll_id)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
/** @var \XoopsModuleHandler $moduleHandler */ |
132
|
|
|
$moduleHandler = xoops_getHandler('module'); |
|
|
|
|
133
|
|
|
$newbbConfig = newbbLoadConfig(); |
134
|
|
|
$pollModuleHandler = $moduleHandler->getByDirname($newbbConfig['poll_module']); |
135
|
|
|
if (!is_object($pollModuleHandler) || !$pollModuleHandler->getVar('isactive')) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
// new xoopspoll module |
139
|
|
|
if ($pollModuleHandler->getVar('version') >= 140) { |
140
|
|
|
/** @var \XoopsPollHandler $pollHandler */ |
141
|
|
|
$pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll'); |
|
|
|
|
142
|
|
|
if (false !== $pollHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='))) { |
|
|
|
|
143
|
|
|
/** @var XoopsPoll\OptionHandler $optionHandler */ |
144
|
|
|
$optionHandler = Xoopspoll\Helper::getInstance()->getHandler('Option'); |
145
|
|
|
$optionHandler->deleteAll(new \Criteria('poll_id', $poll_id, '=')); |
146
|
|
|
/** @var XoopsPoll\LogHandler $logHandler */ |
147
|
|
|
$logHandler = Xoopspoll\Helper::getInstance()->getHandler('Log'); |
148
|
|
|
$logHandler->deleteAll(new \Criteria('poll_id', $poll_id, '=')); |
149
|
|
|
xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
// old Xoopspoll or Umfrage or any clone from them |
152
|
|
|
} else { |
153
|
|
|
$classPoll = $this->loadOldPoll(); |
154
|
|
|
/** @var \XoopsPoll $poll */ |
155
|
|
|
$poll = new $classPoll($poll_id); |
156
|
|
|
if (false !== $poll->delete()) { |
157
|
|
|
$classOption = $classPoll . 'Option'; |
|
|
|
|
158
|
|
|
$classOption::deleteByPollId($poll->getVar('poll_id')); |
159
|
|
|
$classLog = $classPoll . 'Log'; |
160
|
|
|
$classLog::deleteByPollId($poll->getVar('poll_id')); |
161
|
|
|
xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll->getVar('poll_id')); |
162
|
|
|
} |
163
|
|
|
} // end poll_module new or old |
164
|
|
|
|
165
|
|
|
return true; |
166
|
|
|
} |
167
|
|
|
// END irmtfan add deletePoll function |
168
|
|
|
|
169
|
|
|
// START irmtfan add getPoll function |
170
|
|
|
/** |
171
|
|
|
* get a poll object from a poll module. |
172
|
|
|
* note: can be used to find if a poll exist in a module |
173
|
|
|
* @access public |
174
|
|
|
* @param int $poll_id |
175
|
|
|
* @param string $pollModule dirname of the poll module |
176
|
|
|
* @return bool|\XoopsObject poll |
177
|
|
|
*/ |
178
|
|
|
public function getPoll($poll_id, $pollModule = null) |
179
|
|
|
{ |
180
|
|
|
if (empty($poll_id)) { |
181
|
|
|
return false; |
182
|
|
|
} |
183
|
|
|
$moduleHandler = xoops_getHandler('module'); |
|
|
|
|
184
|
|
|
$newbbConfig = newbbLoadConfig(); |
185
|
|
|
if (!empty($pollModule)) { |
186
|
|
|
$newbbConfig['poll_module'] = $pollModule; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$pollModuleHandler = $moduleHandler->getByDirname($newbbConfig['poll_module']); |
190
|
|
|
if (!is_object($pollModuleHandler) || !$pollModuleHandler->getVar('isactive')) { |
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
// new xoopspoll module |
194
|
|
|
if ($pollModuleHandler->getVar('version') >= 140) { |
195
|
|
|
$pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll'); |
196
|
|
|
$pollObject = $pollHandler->get($poll_id); |
197
|
|
|
// old xoopspoll or umfrage or any clone from them |
198
|
|
|
} else { |
199
|
|
|
$classPoll = $this->loadOldPoll($newbbConfig['poll_module']); |
200
|
|
|
$pollObject = new $classPoll($poll_id); |
201
|
|
|
} // end poll_module new or old |
202
|
|
|
|
203
|
|
|
return $pollObject; |
204
|
|
|
} |
205
|
|
|
// END irmtfan add getPoll function |
206
|
|
|
} |
207
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.