1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Xoopspoll; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
XOOPS - PHP Content Management System |
7
|
|
|
Copyright (c) 2000-2020 XOOPS.org |
8
|
|
|
<https://xoops.org> |
9
|
|
|
This program is free software; you can redistribute it and/or modify |
10
|
|
|
it under the terms of the GNU General Public License as published by |
11
|
|
|
the Free Software Foundation; either version 2 of the License, or |
12
|
|
|
(at your option) any later version. |
13
|
|
|
|
14
|
|
|
You may not change or alter any portion of this comment or credits |
15
|
|
|
of supporting developers from this source code or any supporting |
16
|
|
|
source code which is considered copyrighted (c) material of the |
17
|
|
|
original comment or credit authors. |
18
|
|
|
|
19
|
|
|
This program is distributed in the hope that it will be useful, |
20
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
GNU General Public License for more details. |
23
|
|
|
|
24
|
|
|
You should have received a copy of the GNU General Public License |
25
|
|
|
along with this program; if not, write to the Free Software |
26
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Poll Option class for the XoopsPoll Module |
31
|
|
|
* |
32
|
|
|
* @copyright :: {@link https://xoops.org/ XOOPS Project} |
33
|
|
|
* @license :: {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2.0 or later} |
34
|
|
|
* @subpackage:: class |
35
|
|
|
* @since :: 1.0 |
36
|
|
|
* @author :: {@link https://www.myweb.ne.jp/ Kazumi Ono (AKA onokazu)} |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
use XoopsModules\Xoopspoll\{ |
40
|
|
|
Helper |
|
|
|
|
41
|
|
|
}; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Class OptionHandler |
45
|
|
|
*/ |
46
|
|
|
class OptionHandler extends \XoopsPersistableObjectHandler |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* @var \XoopsModules\Xoopspoll\Helper |
50
|
|
|
*/ |
51
|
|
|
protected \XoopsModules\Xoopspoll\Helper $helper; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* PollOptionHandler::__construct() |
55
|
|
|
* |
56
|
|
|
* @param null|\XoopsDatabase $db |
57
|
|
|
* @param \XoopsModules\Xoopspoll\Helper|null $helper |
58
|
|
|
*/ |
59
|
|
|
public function __construct(\XoopsDatabase $db = null, Helper $helper = null) |
60
|
|
|
{ |
61
|
|
|
$this->helper = $helper ?? Helper::getInstance(); |
62
|
|
|
|
63
|
|
|
parent::__construct($db, 'xoopspoll_option', Option::class, 'option_id', 'option_text'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Update the option vote count for an Option Object |
68
|
|
|
* @param mixed $optionObj is an option object to update |
69
|
|
|
* @return mixed results @see XoopsPersistibleObjectHandler |
70
|
|
|
* @uses xoops_getModuleHandler |
71
|
|
|
* @uses XoopsPersistableObjectHandler::insert |
72
|
|
|
*/ |
73
|
|
|
public function updateCount(mixed $optionObj): mixed |
74
|
|
|
{ |
75
|
|
|
$status = false; |
76
|
|
|
static $logHandler; |
77
|
|
|
if ($optionObj instanceof Option) { |
78
|
|
|
$option_id = $optionObj->getVar('option_id'); |
79
|
|
|
if (!isset($logHandler)) { |
80
|
|
|
$logHandler = $this->helper->getHandler('Log'); |
81
|
|
|
} |
82
|
|
|
$votes = $logHandler->getTotalVotesByOptionId($option_id); |
83
|
|
|
$optionObj->setVar('option_count', $votes); |
84
|
|
|
$status = $this->insert($optionObj); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $status; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Gets all options for poll ID |
92
|
|
|
* |
93
|
|
|
* @param int $pid |
94
|
|
|
* @param string $sortby |
95
|
|
|
* @param string $orderby |
96
|
|
|
* @return array an array of Option objects |
97
|
|
|
* @uses CriteriaCompo |
98
|
|
|
* @uses XoopsPersistableObjectHandler::deleteAll |
99
|
|
|
*/ |
100
|
|
|
public function getAllByPollId(int $pid = 0, string $sortby = 'option_id', string $orderby = 'ASC'): array |
101
|
|
|
{ |
102
|
|
|
// $criteria = new \CriteriaCompo(); |
103
|
|
|
$criteria = new \Criteria('poll_id', (int)$pid, '='); |
104
|
|
|
if (!empty($sortby)) { |
105
|
|
|
$criteria->setSort($sortby); |
106
|
|
|
} |
107
|
|
|
if (!empty($orderby)) { |
108
|
|
|
$criteria->setOrder($orderby); |
109
|
|
|
} |
110
|
|
|
$optionObjs = &$this->getAll($criteria); |
111
|
|
|
if (empty($optionObjs)) { |
112
|
|
|
$optionObjs = []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $optionObjs; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Deletes the option for selected poll |
120
|
|
|
* |
121
|
|
|
* @param int $pid |
122
|
|
|
* @return bool $success |
123
|
|
|
* @uses XoopsPersistableObjectHandler::deleteAll |
124
|
|
|
* @uses Criteria |
125
|
|
|
*/ |
126
|
|
|
public function deleteByPollId(int $pid = 0): bool |
127
|
|
|
{ |
128
|
|
|
$success = $this->deleteAll(new \Criteria('poll_id', (int)$pid, '=')); |
129
|
|
|
|
130
|
|
|
return $success; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Reset the vote counts for the options for selected poll |
135
|
|
|
* |
136
|
|
|
* @param int $pid |
137
|
|
|
* @return bool $success |
138
|
|
|
* @uses XoopsPersistableObjectHandler::updateAll |
139
|
|
|
* @uses Criteria |
140
|
|
|
*/ |
141
|
|
|
public function resetCountByPollId(int $pid = 0): bool |
142
|
|
|
{ |
143
|
|
|
$success = $this->updateAll('option_count', 0, new \Criteria('poll_id', (int)$pid, '=')); |
144
|
|
|
|
145
|
|
|
return $success; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Generates a HTML select box with options |
150
|
|
|
* @param int $pid the select box is created for this poll id |
151
|
|
|
* @return \XoopsFormElementTray html select box |
152
|
|
|
*/ |
153
|
|
|
public function renderOptionFormTray(int $pid = 0): \XoopsFormElementTray |
154
|
|
|
{ |
155
|
|
|
\xoops_load('xoopsformloader'); |
156
|
|
|
$pid = (int)$pid; |
157
|
|
|
$barcolor_array = \XoopsLists::getImgListAsArray($GLOBALS['xoops']->path('modules/xoopspoll/assets/images/colorbars/')); |
158
|
|
|
$optionObjs = []; |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* get all the options for this poll & add some blank options to allow adding more |
162
|
|
|
*/ |
163
|
|
|
if (0 === $pid) { |
164
|
|
|
$newOpts = (2 * Constants::NUM_ADDTL_OPTIONS); |
165
|
|
|
} else { |
166
|
|
|
$optionObjs = $this->getAllByPollId($pid); |
167
|
|
|
$newOpts = Constants::NUM_ADDTL_OPTIONS; |
168
|
|
|
} |
169
|
|
|
$thisBarColorArray = $barcolor_array; |
170
|
|
|
unset($thisBarColorArray['blank.gif']); |
171
|
|
|
for ($i = 0; $i < $newOpts; ++$i) { |
172
|
|
|
$thisObj = $this->create(); |
173
|
|
|
$currentBar = \array_rand($thisBarColorArray); |
174
|
|
|
unset($thisBarColorArray[$currentBar]); |
175
|
|
|
$thisObj->setVar('option_color', $currentBar); |
176
|
|
|
$optionObjs[] = $thisObj; |
177
|
|
|
if (empty($thisBarColorArray)) { |
178
|
|
|
$thisBarColorArray = $barcolor_array; |
179
|
|
|
unset($thisBarColorArray['blank.gif']); |
180
|
|
|
} |
181
|
|
|
unset($thisObj); |
182
|
|
|
} |
183
|
|
|
/** |
184
|
|
|
* add the options to the form |
185
|
|
|
*/ |
186
|
|
|
$optionTray = new \XoopsFormElementTray(\_AM_XOOPSPOLL_POLLOPTIONS, ''); |
187
|
|
|
$i = 0; |
188
|
|
|
foreach ($optionObjs as $optObj) { |
189
|
|
|
$colorSelect = new \XoopsFormSelect('', "option_color[{$i}]", $optObj->getVar('option_color')); |
190
|
|
|
$colorSelect->addOptionArray($barcolor_array); |
191
|
|
|
$colorSelect->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[{$i}]\", \"modules/xoopspoll/assets/images/colorbars\", \"\", \"" . $GLOBALS['xoops']->url('') . "\")'"); |
192
|
|
|
$colorLabel = new \XoopsFormLabel( |
193
|
|
|
'', |
194
|
|
|
"<img src='" . $GLOBALS['xoops']->url('modules/xoopspoll' . '/assets/images/colorbars/' . $optObj->getVar('option_color')) . "'" . " name='option_color_image[{$i}]'" . " id='option_color_image[{$i}]'" . " style='width: 30px; height: 15px;'" . " class='alignmiddle'" . " alt=''><br>" |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
$optionTray->addElement(new \XoopsFormText('', "option_text[{$i}]", 50, 255, $optObj->getVar('option_text'))); |
198
|
|
|
$optionTray->addElement(new \XoopsFormHidden("option_id[{$i}]", $optObj->getVar('option_id'))); |
199
|
|
|
$optionTray->addElement($colorSelect); |
200
|
|
|
$optionTray->addElement($colorLabel); |
201
|
|
|
unset($colorSelect, $colorLabel); |
202
|
|
|
++$i; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $optionTray; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: