-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudio404_poll.php
More file actions
237 lines (219 loc) · 6.16 KB
/
studio404_poll.php
File metadata and controls
237 lines (219 loc) · 6.16 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php if(!defined("DIR")){ exit(); }
class studio404_poll extends studio404_ajax{
public $error = array();
public function __construct(){
/* Manipulate poll's style */
$this->option = array(
"main_id"=>"studio404_pollContainer",
"css"=>array(
"margin"=>"20px 0",
"padding"=>"0px",
"width"=>"100%",
"border"=>"solid 1px #f2f2f2"
), /* poll box styling */
"header"=>array(
"css"=>array(
"margin"=>"0px",
"padding"=>"0px 10px",
"width"=>"calc(100% - 20px)",
"height"=>"40px",
"line-height"=>"40px",
"background-color"=>"#f2f2f2",
"font-size"=>"16px",
"color"=>"#555555"
)
), /* header box styling */
"poll_question"=>array(
"css"=>array(
"margin"=>"0px",
"padding"=>"10px",
"font-size"=>"14px",
"line-height"=>"30px",
"color"=>"#555555"
)
), /* question styling */
"poll_answers"=>array(
"css"=>array(
"svgbox"=>array(
"margin"=>"0 10px",
"width"=>"-webkit-calc(100% - 20px)",
"width"=>"-moz-calc(100% - 20px)",
"width"=>"calc(100% - 20px)"
),
"svg"=>array(
"width"=>"calc(100% - 20px)",
"height"=>"25px",
"margin"=>"0 10px 10px 10px",
"cursor"=>"pointer"
),
"rectbox"=>array(
"fill"=>"#dddddd",
"width"=>"100%",
),
"rectanswer"=>array(
"fill"=>"#1fa67a"
),
"text"=>array(
"font-size"=>"12px",
"fill"=>"#ffffff",
)
) /* answer box styling */
)
);
}
public function lanch($mainoptions){
$this->mainoptions = $mainoptions;
if(
!empty($this->mainoptions['poll_id']) &&
!empty($this->mainoptions['header_text']) &&
!empty($this->mainoptions['poll_question']) &&
!empty($this->mainoptions['poll_answers']) &&
count($this->mainoptions['poll_answers']) >= 2
){/* important values is set */
$getPermition = $this->getPermition();
if($getPermition && ($getPermition=="0755" || $getPermition=="0775" || $getPermition=="0777")){
echo $this->makeFrontEnd();
return true;
}
$this->error[] = sprintf(
"<b>%s</b> დირექტორიას სასურველია ჰქონდეს 0755 / 0775 / 0777 ნებართვა !",
$this->mainoptions['temp_path']
);
}
$this->error[] = "გთხოვთ გადაამოწმოთ \$main_options ცვლადი !";
return false;
}
private function makeFrontEnd(){
$out = '';
if(!studio404_ajax::method("GET","ajax")){
$out .= sprintf('<div id="%s">', $this->option['main_id']);
}
$out .= sprintf(
'<div id="pollbox%s" style="%s">',
$this->mainoptions['poll_id'],
$this->arrayToStyle($this->option['css'])
);
$out .= sprintf(
'<div id="header%s" style="%s">%s</div>',
$this->mainoptions['poll_id'],
$this->arrayToStyle($this->option['header']['css']),
$this->mainoptions['header_text']
);
$out .= sprintf(
'<div id="question%s" style="%s">%s</div>',
$this->mainoptions['poll_id'],
$this->arrayToStyle($this->option['poll_question']['css']),
$this->mainoptions['poll_question']
);
$out .= sprintf(
'<div id="svg-box%s" style="%s">',
$this->mainoptions['poll_id'],
$this->arrayToStyle($this->option['poll_answers']['css']['svgbox'])
);
$x = 1;
foreach ($this->mainoptions['poll_answers'] as $t) {
$persentString = $this->countAnswers($x)."%";
$out .= sprintf(
'<svg style="%s" onclick="makeavote(%s,%s)">
<rect height="25" width="%s" style="%s" />
<rect width="%s" height="25" style="%s" />
<text x="10" y="18" style="%s">%s %s</text>
</svg>
',
$this->arrayToStyle($this->option['poll_answers']['css']['svg']),
$this->mainoptions['poll_id'],
$x,
"100%",
$this->arrayToStyle($this->option['poll_answers']['css']['rectbox']),
$persentString,
$this->arrayToStyle($this->option['poll_answers']['css']['rectanswer']),
$this->arrayToStyle($this->option['poll_answers']['css']['text']),
$t,
$persentString
);
$x++;
}
$out .= '</div>';
$out .= sprintf(
'<script>
function makeavote(q, a) {
document.getElementById("%s").innerHTML = "%s";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
location.reload();
}
};
xhttp.open("GET", "index.php?ajax=true&qid="+q+"&aid="+a+"&p=%s", true);
xhttp.send();
}
</script>',
$this->option['main_id'],
$this->mainoptions['please_wait'],
$this->mainoptions['temp_path']
);
$out .= '</div>';
if(!studio404_ajax::method("GET","ajax")){
$out .= '</div>';
}
return $out;
}
private function getPermition(){
if(file_exists($this->mainoptions['temp_path'])){
$fileperms = substr(sprintf('%o', fileperms($this->mainoptions['temp_path'])), -4);
return $fileperms;
}
$this->error[] = sprintf(
"<b>%s</b> დირექტორია ვერ მოიძებნა !",
$this->mainoptions['temp_path']
);
return false;
}
private function arrayToStyle($css){
$output = '';
try{
if(is_array($css)){
$output = implode('; ', array_map(
function ($v, $k) { return sprintf("%s:%s", $k, $v); },
$css,
array_keys($css)
));
}
}catch(Exception $e){
$this->error[] = sprintf(
"მოხდა შეცდომა ! <b>%s</b>",
$e
);
}
return $output;
}
private function countAnswers($returnN){
$pollFolder = sprintf(
"%squestion%s/",
$this->mainoptions['temp_path'],
$this->mainoptions['poll_id']
);
$globs = glob($pollFolder."*");
$num = array();
foreach($globs as $file){ // iterate files
$filePath = sprintf("%s", $file);
if(file_exists($filePath)){
$f = json_decode(file_get_contents($filePath),true);
$num[] = $f['answer_id'];
}
}
$valCount = array_count_values($num);
$allVals = count($num);
for($y=1; $y <= count($this->mainoptions['poll_answers']); $y++){
if(!empty($valCount[$y])){
$out2[$y] = sprintf('%s', (($valCount[$y] * 100) / $allVals));
}
}
if(!empty($out2[$returnN])){
return floor($out2[$returnN]);
}else{
return "0";
}
}
}
?>