*** empty log message ***
[Packages/TYPO3.CMS.git] / misc / 3.0.0 / oldscripts / poll_submit.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  *
29  * poll_submit.inc
30  *
31  * version 0.91
32  */
33
34
35
36 if (is_object($this))   {
37
38         if (is_array($this->newData["tt_poll"]))        {
39
40
41                 list($uid,)=each($this->newData["tt_poll"]);
42
43                 $cookieName = "t3_tt_poll_voted_".$uid;
44
45                         // poll is allowed if cookie not set or the user is logged in the backend
46                 if (((!isset($GLOBALS["HTTP_COOKIE_VARS"][$cookieName])) OR ($GLOBALS["TSFE"]->beUserLogin)) && ($this->newData["tt_poll"][$uid]["vote"])) {
47
48
49                                 // we need the old data to calc the new one
50                         $query = "SELECT * FROM tt_poll WHERE uid = (".$uid.")";
51                         $res = mysql(TYPO3_db,$query);
52                         $actPoll = mysql_fetch_assoc($res);
53
54                                 // we got the data so let's start
55                         if (is_array ($actPoll)) {
56
57                                         // calc the new data
58                                 if ($submittedVote = $this->newData["tt_poll"][$uid]["vote"]) {
59                                         $answers = explode("\n",$actPoll["answers"]);
60                                         $newAnswers = array();
61                                         $votesTotal = 0;
62                                         reset($answers);
63                                         while(list(,$value)=each($answers))     {
64                                                 list($votes,$answer) = explode("|",$value);
65                                                 $answer=trim($answer);
66                                                 $votes=intval($votes);
67                                                 if (md5($answer) == $submittedVote) {
68                                                         $votes++;
69                                                 }
70                                                 $newAnswers[] = $votes."|".$answer;
71                                                 $votesTotal += $votes;
72                                         }
73
74                                         $this->newData["tt_poll"][$uid]["votes"] = $votesTotal;
75                                         $this->newData["tt_poll"][$uid]["answers"] = implode("\n",$newAnswers);
76
77                                                 // prepare for database update
78                                         $fp=array();
79                                         unset($this->newData["tt_poll"][$uid]["uid"]);
80                                         unset($this->newData["tt_poll"][$uid]["pid"]);
81                                         $this->newData["tt_poll"][$uid]["tstamp"]=time();
82
83                                         while(list($f,$v)=each($this->newData["tt_poll"][$uid]))        {
84                                                 if (t3lib_div::inList("tstamp",$f) || isset($GLOBALS["TCA"]["tt_poll"]["columns"][$f])) {
85                                                         $fp[] = $f . "='" . addslashes($v) . "'";
86                                                 }
87                                         }
88                                                 // write to database
89                                         $query = "UPDATE tt_poll SET " . implode($fp,", ") . " WHERE uid = (".$uid.")";
90                                         $res = mysql(TYPO3_db,$query);
91                                         echo mysql_error();
92
93                                                 // set a cookie for preventing double polls
94                                         setcookie($cookieName,$submittedVote,time()+(3600*24*10)); // delete cookie after 10 days
95
96                                                 // clear the page cache
97                                         if ( $id = t3lib_div::GPvar("clearCachePid") )  {
98                                                 $this->clear_cacheCmd(intval($id));
99                                         }
100                                         if ( $id = t3lib_div::GPvar("clearCacheTargetPid") )    {
101                                                 $this->clear_cacheCmd(intval($id));
102                                         }
103                                 }
104                         }
105                 } else {
106                         // user voted already - what to do?
107                 }
108         }
109 }
110
111
112
113
114 ?>