*** empty log message ***
[Packages/TYPO3.CMS.git] / misc / 3.0.0 / oldscripts / pollLib.inc
1 <?php
2 /***************************************************************
3 *  Copyright notice
4 *
5 *  (c) 2001 RenĂ© Fritz (r.fritz@colorcube.de)
6 *  All rights reserved
7 *
8 *  This script is part of the Typo3 project. The Typo3 project is
9 *  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 *  The GNU General Public License can be found at
15 *  http://www.gnu.org/copyleft/gpl.html.
16 *  A copy is found in the textfile GPL.txt and important notices to the license
17 *  from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 *  This script is distributed in the hope that it will be useful,
21 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 *  GNU General Public License for more details.
24 *
25 *  This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28  * pollLib.inc
29  *
30  * version 0.91
31  *
32  * Creates a poll object
33  *
34  * TypoScript config:
35  * - See static_template "plugin.tt_poll"
36  * - See TS_ref.pdf
37  *
38  * Other resources:
39  * 'poll_submit.inc' is used for submission of the poll value to the database. This is done through the FEData TypoScript object. See the static_template 'plugin.tt_poll' for an example of how to set this up.
40  *
41  * @author      RenĂ© Fritz <r.fritz@colorcube.de>
42  */
43
44 /***************************************************************
45 TODO
46
47 ---
48 show message if user tried to vote twice
49 ---
50 check double votes with IPs - are we paranoid?
51 ---
52
53 ****************************************************************/
54
55
56
57 class user_poll {
58
59         var $cObj;              // The backReference to the mother cObj object set at call time
60
61         var $cObj="";
62
63         var $enableFields ="";          // The enablefields of the tt_poll table.
64
65         var $config=array();
66         var $conf=array();
67
68         var $pollTable = "";
69         var $pollTableUid = "";
70         var $pollTablePid = "";
71
72         /**
73          * Main method. Call this from TypoScript by a USER cObject.
74          */
75         function main_poll($content,$conf)      {
76
77                         // getting configuration values:
78
79                 $this->conf=$conf;
80
81                 $this->enableFields = $this->cObj->enableFields("tt_poll");
82
83                         // "CODE" decides what will be rendered:
84                 $this->config["code"] = $this->cObj->stdWrap($this->conf["code"],$this->conf["code."]);
85                 if (!$this->config["code"]) { $this->config["code"]="SHOWHELP"; } // dummy code to display help
86
87                         //  we decide if we are in another object or in content.shortcut
88                 $items=array();
89                 if ( $this->conf["pollTable"] ) {
90                         $this->pollTable = $this->conf["pollTable"];
91                         $this->pollTablePid = intval($this->cObj->stdWrap($this->conf["pollTablePid"],$this->conf["pollTablePid."]));
92                         $this->pollTablePid = $this->pollTablePid ? $this->pollTablePid : $GLOBALS["TSFE"]->id;
93                         $this->pollTableUid = intval($this->cObj->stdWrap($this->conf["pollTableUid"],$this->conf["pollTableUid."]));
94                         if (!$this->pollTableUid) {
95                                 list (,$this->pollTableUid) = explode(":",$this->cObj->currentRecord);
96                         }
97                         $item = $this->getItempollLink($this->pollTable."_".$this->pollTableUid, $this->pollTablePid);
98                         if (is_array($item)) {
99                                 $items[] = $item;
100                         }
101                 } else {
102                         $items[] = $this->cObj->data;
103                 }
104
105                 $afterBegin = $this->cObj->data["starttime"] ? ($this->cObj->data["starttime"] <= time()) : TRUE;
106                 $inProgress = ($afterBegin AND $this->cObj->data["endtime"]) ? (($this->cObj->data["starttime"] <= time()) AND (time() <=$this->cObj->data["endtime"])) : $afterBegin;
107                 $openEnd = ($afterBegin AND !$this->cObj->data["endtime"]);
108
109                 // $items[] as array is not neccessary but I didn't change it from previous code
110                 // maybe used later to create poll verview lists
111
112
113                         // *************************************
114                         // *** Let's go
115                         // *************************************
116
117                 $codes=t3lib_div::trimExplode(",", strtoupper($this->config["code"]),1);
118
119                 $content="";
120                 reset($items);
121                 while(list(,$item)=each($items))        {
122
123                         $answers = explode("\n",$item["answers"]);
124
125                                 // look for a submitted user vote
126                         unset($submittedVoteKey);
127                         if($GLOBALS["HTTP_COOKIE_VARS"]["t3_tt_poll_voted_".$itema["uid"]]){
128                                 $submittedVoteKey = $GLOBALS["HTTP_COOKIE_VARS"]["t3_tt_poll_voted_".$itema["uid"]];
129                         }else{
130                                 $datakeys = explode(":",$GLOBALS["HTTP_POST_VARS"]["locationData"]);
131                                 $submittedVoteKey = $GLOBALS["HTTP_POST_VARS"]["data"][$datakeys[1]][$datakeys[2]]["vote"];
132                         }
133                         $submittedVoteText = "";
134                         $submittedVote = "";
135                         $voteMsg = "";
136
137                                 // searching for the vote text of the submitted vote
138                         if ( $submittedVoteKey ) {
139                                 reset($answers);
140                                 while(list(,$value)=each($answers))     {
141                                         list(,$answer) = explode("|",$value);
142                                         $answer=trim($answer);
143                                         if ($submittedVoteKey == md5($answer)) {
144                                                 $submittedVoteText = $answer;
145                                         }
146                                 }
147                         }
148
149                         reset($codes);
150                         while(list(,$theCode)=each($codes))     {
151                                 $theCode = (string)trim($theCode);
152                                 switch($theCode)        {
153                                         case "VOTEFORM":
154                                                 if ($inProgress) {
155                                                         $lConf = $this->conf["voteform."];
156
157                                                         list ($submitButton) = array_values($lConf["dataArray."]);
158
159                                                         unset($lConf["dataArray."]);
160                                                         $count = 10;
161                                                         reset($answers);
162                                                         while(list(,$value)=each($answers))     {
163                                                                 list(,$answer) = explode("|",$value);
164                                                                 $answer=trim($answer);
165                                                                 $lConf["dataArray."][$count."."]["type"] = "*data[tt_poll][".$item["uid"]."][vote]=radio";
166                                                                 $lConf["dataArray."][$count."."]["value"] = $answer."=".md5($answer);
167                                                                 $count += 10;
168                                                         }
169                                                         $lConf["dataArray."]["9990."] = $submitButton;
170
171                                                         $lConf["dataArray."]["9998."] = array(
172                                                                 "type" => "clearCachePid=hidden",
173                                                                 "value" => $GLOBALS["TSFE"]->id
174                                                         );
175                                                         $target_id = intval($this->cObj->stdWrap($lConf["redirect"],$lConf["redirect."]));
176                                                         $target_id = $target_id ? $target_id : intval($this->cObj->stdWrap($lConf["type"],$lConf["type."]));
177                                                         if ($target_id AND ($target_id != $GLOBALS["TSFE"]->id)) {
178                                                                 $lConf["dataArray."]["9999."] = array(
179                                                                         "type" => "clearCacheTargetPid=hidden",
180                                                                         "value" => $target_id
181                                                                 );
182                                                         }
183 //debug($lConf);
184                                                         $content .= $this->cObj->FORM($lConf);
185                                                 }
186                                         break;
187
188                                         case "RESULT":
189                                                 if ($afterBegin) {
190                                                         $content .= $this->cObj->cObjGetSingle ($this->conf["resultObj"], $this->conf["resultObj."]);
191
192                                                         if (isset ($this->conf["resultItemObj."])) {
193                                                                 $contentItems = "";
194                                                                 $answers = explode("\n",$item["answers"]);
195                                                                 reset($answers);
196                                                                 while(list(,$value)=each($answers))     {
197                                                                         if (trim($value)) {
198                                                                                 $lConf = $this->conf["resultItemObj."];
199                                                                                 list($votes,$answer) = explode("|",$value);
200                                                                                 $markContentArray = array();
201                                                                                 $markContentArray["###ITEMVOTES###"] = $votes;
202                                                                                 $markContentArray["###PERCENT###"] = $item["votes"]?(string)(round((double)($votes * 100.0 / (double)$item["votes"]),1)):0;
203                                                                                 $markContentArray["###ANSWER###"] = $answer;
204                                                                                 $markContentArray["###POLLFULLWIDTH###"] = $this->conf["pollOutputWidth"];
205                                                                                 $markContentArray["###POLLWIDTH###"] = $item["votes"]?(int)((double)$this->conf["pollOutputWidth"]*($votes / (double)$item["votes"])):0;
206                                                                                 $markContentArray["###POLLREMAINWIDTH###"] = (int)$markContentArray["###POLLFULLWIDTH###"] - (int)$markContentArray["###POLLWIDTH###"];
207                                                                                 $this->cObj->substituteMarkerInObject ($lConf, $markContentArray);
208                                                                                 $contentItems .= $this->cObj->cObjGetSingle ($this->conf["resultItemObj"], $lConf);
209                                                                         }
210                                                                 }
211                                                         }
212                                                 }
213                                         break;
214
215                                         case "SUBMITTEDVOTE":
216                                                 if (($inProgress OR $openEnd) AND $submittedVoteText AND $GLOBALS["no_cache"]) {
217                                                         $voteMsg = $this->cObj->cObjGetSingle ($conf["submittedVoteObj"], $conf["submittedVoteObj."]);
218                                                         $submittedVote = $submittedVoteText;
219                                                 }
220                                         break;
221
222                                         default:
223                                                 $langKey = strtoupper($GLOBALS["TSFE"]->config["config"]["language"]);
224                                                 $helpTemplate = $this->cObj->fileResource("media/scripts/poll_help.tmpl");
225
226                                                         // Get language version
227                                                 $helpTemplate_lang="";
228                                                 if ($langKey)   {$helpTemplate_lang = $this->cObj->getSubpart($helpTemplate,"###TEMPLATE_".$langKey."###");}
229                                                 $helpTemplate = $helpTemplate_lang ? $helpTemplate_lang : $this->cObj->getSubpart($helpTemplate,"###TEMPLATE_DEFAULT###");
230
231                                                         // Markers and substitution:
232                                                 $markerArray["###CODE###"] = ($theCode=="SHOWHELP") ? "" : $theCode;
233                                                 $content.=$this->cObj->substituteMarkerArray($helpTemplate,$markerArray);
234                                         break;
235                                 }
236                         }
237                 $markContentArray = array();
238                 $markContentArray["###RESULTITEMS###"] = $contentItems;
239                 $markContentArray["###TITLE###"] = $item["title"];
240                 $markContentArray["###QUESTION###"] = $item["question"];
241                 $markContentArray["###TOTALVOTES###"] = $item["votes"];
242                 $markContentArray["###VOTEMSG###"] = $voteMsg;
243                 $markContentArray["###SUBMITTEDVOTE###"] = $submittedVote;
244                 $markContentArray["###PROGRESSMSG###"] = "";
245                 if ($inProgress) {
246                         if (!$openEnd) {
247                                 $markContentArray["###PROGRESSMSG###"] = $this->cObj->cObjGetSingle ($this->conf["inProgressObj"], $this->conf["inProgressObj."]);
248                         }
249                 } else {
250                         $markContentArray["###PROGRESSMSG###"] = $this->cObj->cObjGetSingle ($this->conf["finishedObj"], $this->conf["finishedObj."]);
251                 }
252                 $markContentArray["###STARTTIME###"] = $this->cObj->stdWrap($this->cObj->data["starttime"],$conf["date_stdWrap."]);
253                 $markContentArray["###ENDTIME###"] = $this->cObj->stdWrap($this->cObj->data["endtime"],$conf["date_stdWrap."]);
254                 $content = $this->cObj->substituteMarkerArray ($content, $markContentArray);
255                 }
256
257                 return $this->cObj->wrap($content,$conf["wrap."]);
258         }
259
260
261
262                 // get a 'linked' poll record
263         function getItemPollLink($recordlink, $pid)             {
264                 $query = "SELECT * FROM tt_poll WHERE recordlink = ('".$recordlink."') AND pid = (".$pid.")".$this->enableFields;
265                 $res = mysql(TYPO3_db,$query);
266                 echo mysql_error();
267                 return mysql_fetch_assoc($res);
268         }
269
270
271 }
272
273
274
275 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["media/scripts/pollLib.inc"])       {
276         include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["media/scripts/pollLib.inc"]);
277 }
278
279 ?>