/engine/classes/vote.class.php

https://github.com/ButuzGOL/Open-Lamp-Engine · PHP · 216 lines · 136 code · 63 blank · 17 comment · 31 complexity · 45d9fb229996825a3359306ef971fd22 MD5 · raw file

  1. <?php
  2. /*
  3. //=============================================================================/
  4. // Open Lamp Engine version 1.0
  5. //-----------------------------------------------------------------------------/
  6. // Web-site: http://www.pamparam.net/ email: openlampengine@gmail.com
  7. //-----------------------------------------------------------------------------/
  8. // Author: r0n9.GOL email: ron9.gol@gmail.com
  9. //-----------------------------------------------------------------------------/
  10. // Copyright by r0n9.GOL © 2009
  11. //=============================================================================/
  12. // Данный код защищен авторскими правами :)
  13. //=============================================================================/
  14. // Файл: vote.class.php
  15. //-----------------------------------------------------------------------------/
  16. // Назначение: Класс управления голосованиями
  17. //=============================================================================/
  18. */
  19. class vote {
  20. var $mysql, $tn = VOTE, $tn1 = VOTES;
  21. function vote($mysql_obj) {
  22. $this->mysql = $mysql_obj;
  23. }
  24. function add($title, $date, $onoff, $is_reg, $cats, $body, $type) {
  25. $sql = "insert into $this->tn values(null, '$type', '$title', '$date', '$onoff', '$is_reg', '$cats', '$body')";
  26. if (!$this->mysql->query($sql)) return FALSE;
  27. return TRUE;
  28. }
  29. function get($id = 0) {
  30. if (!$this->check($id, "/^[0-9]+$/")) return FALSE;
  31. $sql = (!$id) ? "select * from $this->tn order by id asc" : "select * from $this->tn where id='$id'";
  32. return $this->mysql->fetch_row($sql);
  33. }
  34. function del($id) {
  35. if (!$this->check($id, "/^[0-9]+$/")) return FALSE;
  36. if (!$this->get($id)) return FALSE;
  37. $sql = "delete from $this->tn where id='$id'";
  38. if (!$this->mysql->query($sql)) return FALSE;
  39. return TRUE;
  40. }
  41. function update($id, $title, $onoff, $is_reg, $cats, $body) {
  42. if (!$this->check($id, "/^[0-9]+$/")) return FALSE;
  43. if (!$this->get($id)) return FALSE;
  44. $sql = "update $this->tn set title='$title', onoff='$onoff', is_reg='$is_reg', cats='$cats', body='$body' where id='$id'";
  45. if (!$this->mysql->query($sql)) return FALSE;
  46. return TRUE;
  47. }
  48. function updateOne($id, $pole, $value) {
  49. $sql = "update $this->tn set $pole='$value' where id='$id'";
  50. if (!$this->mysql->query($sql)) return FALSE;
  51. return TRUE;
  52. }
  53. function reset($id) {
  54. if (!$this->check($id, "/^[0-9]+$/")) return FALSE;
  55. if (!$this->get($id)) return FALSE;
  56. $sql = "delete from $this->tn1 where vote_id='$id'";
  57. if (!$this->mysql->query($sql)) return FALSE;
  58. return TRUE;
  59. }
  60. function kolVotes($id) {
  61. $sql = "select body from $this->tn where id='$id'";
  62. $body = $this->mysql->result($sql);
  63. $sql = "select answer from $this->tn1 where vote_id='$id'";
  64. $inf = $this->mysql->fetch_row($sql);
  65. $kol = 0;
  66. if (!$inf) return $kol;
  67. foreach ($inf as $n) if (strpos(" ".$body,$n[0])) $kol++;
  68. return $kol;
  69. }
  70. function getLastId() {
  71. $sql = "select id from $this->tn order by id desc limit 1";
  72. return $this->mysql->result($sql);
  73. }
  74. function makeIn($action, $selected) {
  75. if ($action=="" || !count($selected)) return FALSE;
  76. switch ($action) {
  77. case "1" : $rn = "onoff"; $mk = "1"; break;
  78. case "1_1" : $rn = "onoff"; $mk = "0"; break;
  79. case "2" : $rn = "is_reg"; $mk = "1"; break;
  80. case "2_1" : $rn = "is_reg"; $mk = "0"; break;
  81. }
  82. if ($action=="3") {
  83. for ($i = 0; $selected[$i]; $i++) {
  84. $sql = "delete from $this->tn1 where vote_id='$selected[$i]'";
  85. if (!$this->mysql->query($sql)) return FALSE;
  86. }
  87. return TRUE;
  88. }
  89. elseif ($action=="4") {
  90. for ($i = 0; $selected[$i]; $i++) {
  91. $sql = "delete from $this->tn where id='$selected[$i]'";
  92. if (!$this->mysql->query($sql)) return FALSE;
  93. }
  94. return TRUE;
  95. }
  96. elseif ($rn) {
  97. for ($i = 0; $selected[$i]; $i++) {
  98. $sql = "update $this->tn set $rn='$mk' where id='$selected[$i]'";
  99. if (!$this->mysql->query($sql)) return FALSE;
  100. }
  101. return TRUE;
  102. }
  103. return FALSE;
  104. }
  105. function getS($cate = 0, $is_reg = 0) {
  106. $qcate = ($cate) ? "and (locate(',$cate,', concat(',',cats,','))>0 or cats=0)" : "and cats=0";
  107. $qis_reg = ($is_reg) ? "" : "and is_reg=1";
  108. $sql = "select * from $this->tn where onoff='1' and type='0' $qis_reg $qcate order by rand() limit 1";
  109. return $this->mysql->fetch_row($sql);
  110. }
  111. function getKolAnsw($id, $answer) {
  112. $sql = "select count(*) from $this->tn1 where vote_id='$id' and answer='$answer'";
  113. return $this->mysql->result($sql);
  114. }
  115. function addVote($vote_id, $ip, $vote, $news_id = 0) {
  116. if (!$news_id) {
  117. if ($this->getKolVoteOnIp($vote_id, $ip)) return FALSE;
  118. $sql = "select body from $this->tn where id='$vote_id'";
  119. $body = $this->mysql->result($sql);
  120. $body = explode("\n", $body);
  121. $answer = $body[$vote-1];
  122. if ($answer=="") return FALSE;
  123. }
  124. else {
  125. if ($this->getKolRateOnIp($news_id, $ip)) return FALSE;
  126. $answer = $vote;
  127. }
  128. $sql = "insert into $this->tn1 values(null, '$vote_id', '$ip', '$answer', '$news_id')";
  129. if (!$this->mysql->query($sql)) return FALSE;
  130. return TRUE;
  131. }
  132. function getKolRateOnIp($id, $ip) {
  133. $sql = "select count(*) from $this->tn1 where news_id='$id' and ip='$ip'";
  134. return $this->mysql->result($sql);
  135. }
  136. function getKolVoteOnIp($id, $ip) {
  137. $sql = "select count(*) from $this->tn1 where vote_id='$id' and ip='$ip'";
  138. return $this->mysql->result($sql);
  139. }
  140. function SumKolVotesNews($id) {
  141. $sql = "select sum(answer), count(answer) from $this->tn1 where news_id='$id'";
  142. return $this->mysql->fetch_row($sql);
  143. }
  144. function getOne($id, $pole) {
  145. if (!$this->check($id, "/^[0-9]+$/")) return FALSE;
  146. $sql = "select $pole from $this->tn where id='$id'";
  147. return $this->mysql->result($sql);
  148. }
  149. function check($str, $type) {
  150. return preg_match($type, $str);
  151. }
  152. }
  153. ?>