Example #1
0
 /**
  * Process Vote. Note that the system ONLY process the vote if there is no another equal IP.
  *
  * @param int $width
  * @param int $height
  */
 public function processVote($width = 450, $height = 400)
 {
     if ($this->_context->get("xcrt") == "") {
         // Is The Post values needed to process vote exists?
         if ($this->_context->get("xmlnuke_poll") != "" && $this->_context->get("xmlnuke_polllang") != "" && $this->_context->get("xmlnuke_pollanswer") != "") {
             $this->_poll = $this->_context->get("xmlnuke_poll");
             $this->_lang = $this->_context->get("xmlnuke_polllang");
             $ok = true;
             // Check if IP already voted -> Freeze IP for 5 days.
             if ($this->_isdb) {
                 // Remove Old Entries
                 $dbdata = new DBDataset($this->_connection);
                 $sql = "delete from :table where register < now() - interval 5 day ";
                 $sql = \ByJG\AnyDataset\Database\SQLHelper::createSafeSQL($sql, array(':table' => $this->_tbllastip));
                 $dbdata->execSQL($sql);
                 // Check if exists
                 $sql = "select count(1) from :table where ip = [[ip]] and name = [[name]] ";
                 $sql = \ByJG\AnyDataset\Database\SQLHelper::createSafeSQL($sql, array(':table' => $this->_tbllastip));
                 $param = array("ip" => $this->_context->getClientIp(), "name" => $this->_poll);
                 $count = $dbdata->getScalar($sql, $param);
                 $ok = false;
                 if ($count == 0) {
                     $ok = true;
                     $sql = "insert into :table (ip, name, register) values ([[ip]], [[name]], now()) ";
                     $sql = \ByJG\AnyDataset\Database\SQLHelper::createSafeSQL($sql, array(':table' => $this->_tbllastip));
                     $param = array("ip" => $this->_context->getClientIp(), "name" => $this->_poll);
                     try {
                         $dbdata->execSQL($sql, $param);
                     } catch (\PDOException $ex) {
                         $ok = false;
                     }
                 }
             }
             // Is My IP Unique? If true I can process the vote.
             // Note if the poll name, lang and code are wrong the system does not do anything.
             if ($ok) {
                 // Get Data
                 $itf = new IteratorFilter();
                 $itf->addRelation("name", Relation::EQUAL, $this->_poll);
                 $itf->addRelation("lang", Relation::EQUAL, $this->_lang);
                 $itf->addRelation("code", Relation::EQUAL, $this->_context->get("xmlnuke_pollanswer"));
                 if ($this->_isdb) {
                     $dbdata = new DBDataset($this->_connection);
                     $param = array();
                     $sql = "update :table set votes = IFNULL(votes,0) + 1 where :filter ";
                     $sql = \ByJG\AnyDataset\Database\SQLHelper::createSafeSQL($sql, array(':table' => $this->_tblanswer, ':filter' => $itf->getFilter(IteratorFilter::SQL, $param)));
                     $dbdata->execSQL($sql, $param);
                 } else {
                     $this->getAnyData();
                     $itAnswer = $this->_anyAnswer->getIterator($itf);
                     if ($itAnswer->hasNext()) {
                         $sr = $itAnswer->moveNext();
                         $sr->setField("votes", intval($sr->getField("votes")) + 1);
                         $this->_anyAnswer->Save();
                     }
                 }
             }
             $this->_processed = true;
         }
     } else {
         $this->_processed = true;
     }
     $this->_width = $width;
     $this->_height = $height;
 }