PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Ecart/Poll/Model/Vote.php

https://code.google.com/p/ecartcommerce/
PHP | 165 lines | 114 code | 16 blank | 35 comment | 15 complexity | 8fac88d23e5c0f6cc72b308ae63b3850 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Ecart
  4. *
  5. * This file is part of Ecart.
  6. *
  7. * Ecart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Ecart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Ecart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Ecart
  21. * @package Ecart_Poll
  22. * @copyright Copyright 2008-2009 E-Cart LLC
  23. * @license GNU Public License V3.0
  24. */
  25. /**
  26. *
  27. * @category Ecart
  28. * @package Ecart_Poll
  29. * @subpackage Model
  30. * @author Ecart Core Team <core@ecartcommerce.com>
  31. */
  32. class Ecart_Poll_Model_Vote extends Ecart_Db_Table
  33. {
  34. protected $_name = 'poll_vote';
  35. public function hasVoteInTable($questionId, $customerId = false, $ip = null)
  36. {
  37. if (false === $customerId) {
  38. $customerId = Ecart::getCustomerId();
  39. }
  40. if (null === $customerId) {
  41. $cWhere = 'AND pv.customer_id IS NULL ';
  42. if (null === $ip) {
  43. $ip = ip2long($_SERVER['REMOTE_ADDR']);
  44. }
  45. $ipWhere = $this->getAdapter()->quoteInto('AND pv.ip = ? ', $ip);
  46. } else {
  47. $cWhere = "AND pv.customer_id = {$customerId} ";
  48. $ipWhere = '';
  49. //usually not used
  50. if (null !== $ip) {
  51. $ipWhere = $this->getAdapter()->quoteInto('AND pv.ip = ? ', $ip);
  52. }
  53. }
  54. $query = "SELECT COUNT(*) FROM " . $this->_prefix . 'poll_vote' . " as pv " .
  55. 'INNER JOIN ' . $this->_prefix . 'poll_answer pa ON pa.id = pv.answer_id ' .
  56. "WHERE pa.question_id = ? " .
  57. $ipWhere .
  58. $cWhere;
  59. return (bool) $this->getAdapter()->fetchOne($query, $questionId);
  60. }
  61. public function getVoteCount($customerId = false)
  62. {
  63. if (false === $customerId) {
  64. $cWhere = '';
  65. } elseif (null === $customerId) {
  66. $cWhere = 'AND v.customer_id IS NULL ';
  67. } else {
  68. $cWhere = "AND v.customer_id = {$customerId} ";
  69. }
  70. $rows = $this->getAdapter()->fetchAll(
  71. "SELECT a.question_id, count(v.id) as cnt FROM " . $this->_prefix . "poll_answer a
  72. LEFT JOIN " . $this->_prefix . "poll_vote AS v ON v.answer_id = a.id " .
  73. 'WHERE a.language_id = ? ' .
  74. $cWhere .
  75. 'GROUP BY a.question_id',
  76. Ecart_Locale::getLanguageId()
  77. );
  78. $assigns = array();
  79. foreach ($rows as $row) {
  80. $assigns[intval($row['question_id'])] = intval($row['cnt']);
  81. }
  82. return $assigns;
  83. }
  84. public function getResults()
  85. {
  86. $rowset = $this->getAdapter()->fetchAssoc(
  87. 'SELECT pv.answer_id, pa.question_id, COUNT(*) as cnt ' .
  88. 'FROM ' . $this->_prefix . 'poll_vote pv ' .
  89. 'INNER JOIN ' . $this->_prefix . 'poll_answer pa ON pa.id = pv.answer_id ' .
  90. 'WHERE pa.language_id = ? '.
  91. 'GROUP BY pv.answer_id ' .
  92. 'ORDER BY cnt',
  93. Ecart_Locale::getLanguageId()
  94. );
  95. $assigns = array();
  96. foreach ($rowset as $row) {
  97. $assigns[$row['question_id']][$row['answer_id']] = $row;
  98. }
  99. return $assigns;
  100. }
  101. /**
  102. *
  103. * @return array
  104. */
  105. public function getQuestionIdsFromCookie($name = 'polls')
  106. {
  107. $questionIds = array();
  108. if (!isset($_COOKIE[$name])) {
  109. return array();
  110. }
  111. $questionIds = array();
  112. $items = array_filter(explode(',', $_COOKIE[$name]));
  113. foreach ($items as $item) {
  114. if (strstr($item, ':')) {
  115. list($questionId, $customerId) = explode(':', $item);
  116. $customerId = (int) $customerId;
  117. $questionId = (int) $questionId;
  118. } else {
  119. $questionId = (int) $item;
  120. $customerId = false;
  121. }
  122. if ($customerId == Ecart::getCustomerId()) {
  123. $questionIds[$questionId] = $questionId;
  124. }
  125. }
  126. return array_values($questionIds);
  127. }
  128. public function addToCookie(
  129. $values, $path, $time = 2592000, $name = 'polls')
  130. {
  131. $cookieValues = array();
  132. if (isset($_COOKIE[$name])) {
  133. $cookieValues = array_filter(explode(',', $_COOKIE[$name]));
  134. }
  135. if (!is_array($values)) {
  136. $values = array($values);
  137. }
  138. $customerId = Ecart::getCustomerId();
  139. if ($customerId) {
  140. foreach ($values as &$value) {
  141. $value .= ':' . $customerId;
  142. }
  143. }
  144. $cookieValues = implode(',', array_unique(array_merge(
  145. $values, $cookieValues
  146. )));
  147. return setcookie($name, $cookieValues, time() + $time, $path);
  148. }
  149. }