Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 2001-2004 René Fritz (r.fritz@colorcube.de) 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00044 /*************************************************************** 00045 TODO 00046 00047 --- 00048 show message if user tried to vote twice 00049 --- 00050 check double votes with IPs - are we paranoid? 00051 --- 00052 00053 ****************************************************************/ 00054 00055 00056 00057 require_once(PATH_tslib."class.tslib_pibase.php"); 00058 00059 class tx_ttpoll extends tslib_pibase { 00060 var $cObj; // The backReference to the mother cObj object set at call time 00061 00062 var $enableFields =""; // The enablefields of the tt_poll table. 00063 00064 var $config=array(); 00065 var $conf=array(); 00066 00067 var $pollTable = ""; 00068 var $pollTableUid = ""; 00069 var $pollTablePid = ""; 00070 00074 function main_poll($content,$conf) { 00075 00076 // getting configuration values: 00077 00078 $this->conf=$conf; 00079 00080 $this->enableFields = $this->cObj->enableFields("tt_poll"); 00081 00082 // "CODE" decides what will be rendered: 00083 $this->config["code"] = $this->cObj->stdWrap($this->conf["code"],$this->conf["code."]); 00084 if (!$this->config["code"]) { $this->config["code"]="SHOWHELP"; } // dummy code to display help 00085 00086 // we decide if we are in another object or in content.shortcut 00087 $items=array(); 00088 if ( $this->conf["pollTable"] ) { 00089 $this->pollTable = $this->conf["pollTable"]; 00090 $this->pollTablePid = intval($this->cObj->stdWrap($this->conf["pollTablePid"],$this->conf["pollTablePid."])); 00091 $this->pollTablePid = $this->pollTablePid ? $this->pollTablePid : $GLOBALS["TSFE"]->id; 00092 $this->pollTableUid = intval($this->cObj->stdWrap($this->conf["pollTableUid"],$this->conf["pollTableUid."])); 00093 if (!$this->pollTableUid) { 00094 list (,$this->pollTableUid) = explode(":",$this->cObj->currentRecord); 00095 } 00096 $item = $this->getItempollLink($this->pollTable."_".$this->pollTableUid, $this->pollTablePid); 00097 if (is_array($item)) { 00098 $items[] = $item; 00099 } 00100 } else { 00101 $items[] = $this->cObj->data; 00102 } 00103 00104 $afterBegin = $this->cObj->data["starttime"] ? ($this->cObj->data["starttime"] <= time()) : TRUE; 00105 $inProgress = ($afterBegin AND $this->cObj->data["endtime"]) ? (($this->cObj->data["starttime"] <= time()) AND (time() <=$this->cObj->data["endtime"])) : $afterBegin; 00106 $openEnd = ($afterBegin AND !$this->cObj->data["endtime"]); 00107 00108 // $items[] as array is not neccessary but I didn't change it from previous code 00109 // maybe used later to create poll verview lists 00110 00111 00112 // ************************************* 00113 // *** Let's go 00114 // ************************************* 00115 00116 $codes=t3lib_div::trimExplode(",", strtoupper($this->config["code"]),1); 00117 00118 $content=""; 00119 reset($items); 00120 while(list(,$item)=each($items)) { 00121 00122 $answers = explode("\n",$item["answers"]); 00123 $POST = t3lib_div::_POST(); 00124 00125 // look for a submitted user vote 00126 unset($submittedVoteKey); 00127 if($GLOBALS["HTTP_COOKIE_VARS"]["t3_tt_poll_voted_".$itema["uid"]]){ 00128 $submittedVoteKey = $GLOBALS["HTTP_COOKIE_VARS"]["t3_tt_poll_voted_".$itema["uid"]]; 00129 }else{ 00130 $datakeys = explode(":",$POST["locationData"]); 00131 $submittedVoteKey = $POST["data"][$datakeys[1]][$datakeys[2]]["vote"]; 00132 } 00133 $submittedVoteText = ""; 00134 $submittedVote = ""; 00135 $voteMsg = ""; 00136 00137 // searching for the vote text of the submitted vote 00138 if ( $submittedVoteKey ) { 00139 reset($answers); 00140 while(list(,$value)=each($answers)) { 00141 list(,$answer) = explode("|",$value); 00142 $answer=trim($answer); 00143 if ($submittedVoteKey == md5($answer)) { 00144 $submittedVoteText = $answer; 00145 } 00146 } 00147 } 00148 00149 reset($codes); 00150 while(list(,$theCode)=each($codes)) { 00151 $theCode = (string)trim($theCode); 00152 switch($theCode) { 00153 case "VOTEFORM": 00154 if ($inProgress) { 00155 $lConf = $this->conf["voteform."]; 00156 00157 list ($submitButton) = array_values($lConf["dataArray."]); 00158 00159 unset($lConf["dataArray."]); 00160 $count = 10; 00161 reset($answers); 00162 while(list(,$value)=each($answers)) { 00163 list(,$answer) = explode("|",$value); 00164 $answer=trim($answer); 00165 $lConf["dataArray."][$count."."]["type"] = "*data[tt_poll][".$item["uid"]."][vote]=radio"; 00166 $lConf["dataArray."][$count."."]["value"] = $answer."=".md5($answer); 00167 $count += 10; 00168 } 00169 $lConf["dataArray."]["9990."] = $submitButton; 00170 00171 $lConf["dataArray."]["9998."] = array( 00172 "type" => "clearCachePid=hidden", 00173 "value" => $GLOBALS["TSFE"]->id 00174 ); 00175 $target_id = intval($this->cObj->stdWrap($lConf["redirect"],$lConf["redirect."])); 00176 $target_id = $target_id ? $target_id : intval($this->cObj->stdWrap($lConf["type"],$lConf["type."])); 00177 if ($target_id AND ($target_id != $GLOBALS["TSFE"]->id)) { 00178 $lConf["dataArray."]["9999."] = array( 00179 "type" => "clearCacheTargetPid=hidden", 00180 "value" => $target_id 00181 ); 00182 } 00183 //debug($lConf); 00184 $content .= $this->cObj->FORM($lConf); 00185 } 00186 break; 00187 00188 case "RESULT": 00189 if ($afterBegin) { 00190 $content .= $this->cObj->cObjGetSingle ($this->conf["resultObj"], $this->conf["resultObj."]); 00191 00192 if (isset ($this->conf["resultItemObj."])) { 00193 $contentItems = ""; 00194 $answers = explode("\n",$item["answers"]); 00195 reset($answers); 00196 while(list(,$value)=each($answers)) { 00197 if (trim($value)) { 00198 $lConf = $this->conf["resultItemObj."]; 00199 list($votes,$answer) = explode("|",$value); 00200 $markContentArray = array(); 00201 $markContentArray["###ITEMVOTES###"] = $votes; 00202 $markContentArray["###PERCENT###"] = $item["votes"]?(string)(round((double)($votes * 100.0 / (double)$item["votes"]),1)):0; 00203 $markContentArray["###ANSWER###"] = $answer; 00204 $markContentArray["###POLLFULLWIDTH###"] = $this->conf["pollOutputWidth"]; 00205 $markContentArray["###POLLWIDTH###"] = $item["votes"]?(int)((double)$this->conf["pollOutputWidth"]*($votes / (double)$item["votes"])):0; 00206 $markContentArray["###POLLREMAINWIDTH###"] = (int)$markContentArray["###POLLFULLWIDTH###"] - (int)$markContentArray["###POLLWIDTH###"]; 00207 $this->cObj->substituteMarkerInObject ($lConf, $markContentArray); 00208 $contentItems .= $this->cObj->cObjGetSingle ($this->conf["resultItemObj"], $lConf); 00209 } 00210 } 00211 } 00212 } 00213 break; 00214 00215 case "SUBMITTEDVOTE": 00216 if (($inProgress OR $openEnd) AND $submittedVoteText AND $GLOBALS["no_cache"]) { 00217 $voteMsg = $this->cObj->cObjGetSingle ($conf["submittedVoteObj"], $conf["submittedVoteObj."]); 00218 $submittedVote = $submittedVoteText; 00219 } 00220 break; 00221 00222 default: 00223 $langKey = strtoupper($GLOBALS["TSFE"]->config["config"]["language"]); 00224 $helpTemplate = $this->cObj->fileResource("EXT:tt_poll/pi/poll_help.tmpl"); 00225 00226 // Get language version 00227 $helpTemplate_lang=""; 00228 if ($langKey) {$helpTemplate_lang = $this->cObj->getSubpart($helpTemplate,"###TEMPLATE_".$langKey."###");} 00229 $helpTemplate = $helpTemplate_lang ? $helpTemplate_lang : $this->cObj->getSubpart($helpTemplate,"###TEMPLATE_DEFAULT###"); 00230 00231 // Markers and substitution: 00232 $markerArray["###CODE###"] = ($theCode=="SHOWHELP") ? "" : $theCode; 00233 $content.=$this->cObj->substituteMarkerArray($helpTemplate,$markerArray); 00234 break; 00235 } 00236 } 00237 $markContentArray = array(); 00238 $markContentArray["###RESULTITEMS###"] = $contentItems; 00239 $markContentArray["###TITLE###"] = $item["title"]; 00240 $markContentArray["###QUESTION###"] = $item["question"]; 00241 $markContentArray["###TOTALVOTES###"] = $item["votes"]; 00242 $markContentArray["###VOTEMSG###"] = $voteMsg; 00243 $markContentArray["###SUBMITTEDVOTE###"] = $submittedVote; 00244 $markContentArray["###PROGRESSMSG###"] = ""; 00245 if ($inProgress) { 00246 if (!$openEnd) { 00247 $markContentArray["###PROGRESSMSG###"] = $this->cObj->cObjGetSingle ($this->conf["inProgressObj"], $this->conf["inProgressObj."]); 00248 } 00249 } else { 00250 $markContentArray["###PROGRESSMSG###"] = $this->cObj->cObjGetSingle ($this->conf["finishedObj"], $this->conf["finishedObj."]); 00251 } 00252 $markContentArray["###STARTTIME###"] = $this->cObj->stdWrap($this->cObj->data["starttime"],$conf["date_stdWrap."]); 00253 $markContentArray["###ENDTIME###"] = $this->cObj->stdWrap($this->cObj->data["endtime"],$conf["date_stdWrap."]); 00254 $content = $this->cObj->substituteMarkerArray ($content, $markContentArray); 00255 } 00256 00257 return $this->cObj->wrap($content,$conf["wrap."]); 00258 } 00259 00260 00261 00262 // get a 'linked' poll record 00263 function getItemPollLink($recordlink, $pid) { 00264 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tt_poll', 'recordlink="'.$GLOBALS['TYPO3_DB']->quoteStr($recordlink, 'tt_poll').'" AND pid='.intval($pid).$this->enableFields); 00265 return $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res); 00266 } 00267 00268 00269 } 00270 00271 00272 00273 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/tt_poll/pi/class.tx_ttpoll.php"]) { 00274 include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/tt_poll/pi/class.tx_ttpoll.php"]); 00275 } 00276 00277 ?>