|
1
|
|
|
<?php |
|
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 http://www.fsf.org/copyleft/gpl.html GNU public license} |
|
34
|
|
|
* @package :: xoopspoll |
|
35
|
|
|
* @subpackage:: class |
|
36
|
|
|
* @since :: 1.0 |
|
37
|
|
|
* @author :: {@link http://www.myweb.ne.jp/ Kazumi Ono (AKA onokazu)} |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
use XoopsModules\Xoopspoll\{ |
|
41
|
|
|
Helper |
|
|
|
|
|
|
42
|
|
|
}; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Class OptionHandler |
|
46
|
|
|
*/ |
|
47
|
|
|
class OptionHandler extends \XoopsPersistableObjectHandler |
|
48
|
|
|
{ |
|
49
|
|
|
/** |
|
50
|
|
|
* @var \XoopsModules\Xoopspoll\Helper |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $helper; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* PollOptionHandler::__construct() |
|
56
|
|
|
* |
|
57
|
|
|
* @param null|\XoopsDatabase $db |
|
58
|
|
|
* @param \XoopsModules\Xoopspoll\Helper|null $helper |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(\XoopsDatabase $db = null, Helper $helper = null) |
|
61
|
|
|
{ |
|
62
|
|
|
if (null === $helper) { |
|
63
|
|
|
$this->helper = Helper::getInstance(); |
|
64
|
|
|
} else { |
|
65
|
|
|
$this->helper = $helper; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
parent::__construct($db, 'xoopspoll_option', Option::class, 'option_id', 'option_text'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Update the option vote count for a Option Object |
|
73
|
|
|
* @param mixed $optionObj is an option object to update |
|
74
|
|
|
* @return mixed results @see XoopsPersistibleObjectHandler |
|
75
|
|
|
* @uses xoops_getModuleHandler |
|
76
|
|
|
* @uses XoopsPersistableObjectHandler::insert |
|
77
|
|
|
*/ |
|
78
|
|
|
public function updateCount($optionObj) |
|
79
|
|
|
{ |
|
80
|
|
|
$status = false; |
|
81
|
|
|
static $logHandler; |
|
82
|
|
|
if ($optionObj instanceof Option) { |
|
83
|
|
|
$option_id = $optionObj->getVar('option_id'); |
|
84
|
|
|
if (!isset($logHandler)) { |
|
85
|
|
|
$logHandler = $this->helper::getInstance()->getHandler('Log'); |
|
86
|
|
|
} |
|
87
|
|
|
$votes = $logHandler->getTotalVotesByOptionId($option_id); |
|
88
|
|
|
$optionObj->setVar('option_count', $votes); |
|
89
|
|
|
$status = $this->insert($optionObj); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $status; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Gets all options for poll ID |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $pid |
|
99
|
|
|
* @param string $sortby |
|
100
|
|
|
* @param string $orderby |
|
101
|
|
|
* @return array an array of Option objects |
|
102
|
|
|
* @uses CriteriaCompo |
|
103
|
|
|
* @uses XoopsPersistableObjectHandler::deleteAll |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getAllByPollId($pid = 0, $sortby = 'option_id', $orderby = 'ASC') |
|
106
|
|
|
{ |
|
107
|
|
|
// $criteria = new \CriteriaCompo(); |
|
108
|
|
|
$criteria = new \Criteria('poll_id', (int)$pid, '='); |
|
109
|
|
|
if (!empty($sortby)) { |
|
110
|
|
|
$criteria->setSort($sortby); |
|
111
|
|
|
} |
|
112
|
|
|
if (!empty($orderby)) { |
|
113
|
|
|
$criteria->setOrder($orderby); |
|
114
|
|
|
} |
|
115
|
|
|
$optionObjs = &$this->getAll($criteria); |
|
116
|
|
|
if (empty($optionObjs)) { |
|
117
|
|
|
$optionObjs = []; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $optionObjs; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Deletes the option for selected poll |
|
125
|
|
|
* |
|
126
|
|
|
* @param int $pid |
|
127
|
|
|
* @return bool $success |
|
128
|
|
|
* @uses XoopsPersistableObjectHandler::deleteAll |
|
129
|
|
|
* @uses Criteria |
|
130
|
|
|
*/ |
|
131
|
|
|
public function deleteByPollId($pid = 0) |
|
132
|
|
|
{ |
|
133
|
|
|
$success = $this->deleteAll(new \Criteria('poll_id', (int)$pid, '=')); |
|
134
|
|
|
|
|
135
|
|
|
return $success; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Reset the vote counts for the options for selected poll |
|
140
|
|
|
* |
|
141
|
|
|
* @param int $pid |
|
142
|
|
|
* @return bool $success |
|
143
|
|
|
* @uses XoopsPersistableObjectHandler::updateAll |
|
144
|
|
|
* @uses Criteria |
|
145
|
|
|
*/ |
|
146
|
|
|
public function resetCountByPollId($pid = 0) |
|
147
|
|
|
{ |
|
148
|
|
|
$success = $this->updateAll('option_count', 0, new \Criteria('poll_id', (int)$pid, '=')); |
|
149
|
|
|
|
|
150
|
|
|
return $success; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Generates an html select box with options |
|
155
|
|
|
* @param mixed $pid the select box is created for this poll id |
|
156
|
|
|
* @return string html select box |
|
157
|
|
|
*/ |
|
158
|
|
|
public function renderOptionFormTray($pid = 0) |
|
159
|
|
|
{ |
|
160
|
|
|
\xoops_load('xoopsformloader'); |
|
161
|
|
|
$pid = (int)$pid; |
|
162
|
|
|
$barcolor_array = \XoopsLists::getImgListAsArray($GLOBALS['xoops']->path('modules/xoopspoll/assets/images/colorbars/')); |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* get all the options for this poll & add some blank options to allow adding more |
|
166
|
|
|
*/ |
|
167
|
|
|
if (0 === $pid) { |
|
168
|
|
|
$newOpts = (2 * Constants::NUM_ADDTL_OPTIONS); |
|
169
|
|
|
} else { |
|
170
|
|
|
$optionObjs = $this->getAllByPollId($pid); |
|
171
|
|
|
$newOpts = Constants::NUM_ADDTL_OPTIONS; |
|
172
|
|
|
} |
|
173
|
|
|
$thisBarColorArray = $barcolor_array; |
|
174
|
|
|
unset($thisBarColorArray['blank.gif']); |
|
175
|
|
|
for ($i = 0; $i < $newOpts; ++$i) { |
|
176
|
|
|
$thisObj = $this->create(); |
|
177
|
|
|
$currentBar = \array_rand($thisBarColorArray); |
|
178
|
|
|
unset($thisBarColorArray[$currentBar]); |
|
179
|
|
|
$thisObj->setVar('option_color', $currentBar); |
|
180
|
|
|
$optionObjs[] = $thisObj; |
|
181
|
|
|
if (empty($thisBarColorArray)) { |
|
182
|
|
|
$thisBarColorArray = $barcolor_array; |
|
183
|
|
|
unset($thisBarColorArray['blank.gif']); |
|
184
|
|
|
} |
|
185
|
|
|
unset($thisObj); |
|
186
|
|
|
} |
|
187
|
|
|
/** |
|
188
|
|
|
* add the options to the form |
|
189
|
|
|
*/ |
|
190
|
|
|
$optionTray = new \XoopsFormElementTray(\_AM_XOOPSPOLL_POLLOPTIONS, ''); |
|
191
|
|
|
$i = 0; |
|
192
|
|
|
foreach ($optionObjs as $optObj) { |
|
|
|
|
|
|
193
|
|
|
$colorSelect = new \XoopsFormSelect('', "option_color[{$i}]", $optObj->getVar('option_color')); |
|
194
|
|
|
$colorSelect->addOptionArray($barcolor_array); |
|
195
|
|
|
$colorSelect->setExtra("onchange='showImgSelected(\"option_color_image[{$i}]\", \"option_color[{$i}]\", \"modules/xoopspoll/assets/images/colorbars\", \"\", \"" . $GLOBALS['xoops']->url('') . "\")'"); |
|
196
|
|
|
$colorLabel = new \XoopsFormLabel( |
|
197
|
|
|
'', |
|
198
|
|
|
"<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>" |
|
199
|
|
|
); |
|
200
|
|
|
|
|
201
|
|
|
$optionTray->addElement(new \XoopsFormText('', "option_text[{$i}]", 50, 255, $optObj->getVar('option_text'))); |
|
202
|
|
|
$optionTray->addElement(new \XoopsFormHidden("option_id[{$i}]", $optObj->getVar('option_id'))); |
|
203
|
|
|
$optionTray->addElement($colorSelect); |
|
204
|
|
|
$optionTray->addElement($colorLabel); |
|
205
|
|
|
unset($colorSelect, $colorLabel); |
|
206
|
|
|
++$i; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return $optionTray; |
|
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: