Cheetah
ChPollDb.php
Go to the documentation of this file.
1 <?php
2 
8 require_once( CH_DIRECTORY_PATH_CLASSES . 'ChWsbModuleDb.php' );
9 
10 class ChPollDb extends ChWsbModuleDb
11 {
12  var $_oConfig;
13  var $_sTable;
15 
16  /*
17  * Constructor.
18  */
19  function __construct(&$oConfig)
20  {
21  parent::__construct();
22 
23  $this -> _oConfig = $oConfig;
24  $this -> _sTable = $oConfig -> sTableName;
25  $this -> sTablePrefix = $oConfig -> sTablePrefix;
26  }
27 
41  function getAllPolls($sLimit, $iMemberID = 0, $bAllPolls = false, $iApproval = 1, $sExtraSql = null, $sOrder = '`poll_date` DESC')
42  {
43  $sLimit = $this -> escape($sLimit, false);
44  $iMemberID = (int) $iMemberID;
45  $iApproval = (int) $iApproval;
46  $sExtraSql = $this -> escape($sExtraSql, false);
47  $sOrder = $this -> escape($sOrder, false);
48 
49  // ** init some needed variables ;
50 
51  $sAddonSql = null;
52  $aWhereCondition = array();
53 
54  $aWhereCondition[0] = " AND `poll_status` = 'active' ";
55  $aWhereCondition[1] = " AND `poll_approval` = {$iApproval}";
56  $aWhereCondition[2] = " AND `id_profile` = {$iMemberID} ";
57 
58  // if need the only member's pools ;
59  if ( !$bAllPolls ) {
60  if ( $iMemberID ) {
61  $sAddonSql = $aWhereCondition[1] . $aWhereCondition[2];
62  } else {
63  $sAddonSql = $aWhereCondition[0] . $aWhereCondition[1];
64  }
65  }
66 
67  $sQuery =
68  "
69  SELECT
70  `id_poll`,
71  `id_profile`,
72  `poll_date`,
73  (UNIX_TIMESTAMP() - `poll_date`) AS 'sec',
74  `poll_approval`,
75  `poll_featured`
76  FROM
77  `{$this -> _sTable}`
78  WHERE
79  1
80  {$sAddonSql}
81  {$sExtraSql}
82  ORDER BY
83  {$sOrder}
84  {$sLimit}
85  ";
86 
87  return $this -> getAll($sQuery);
88  }
89 
97  function getFeaturedCount($iStatus = 1, $bOnlyPublic = false)
98  {
99  $iStatus = (int) $iStatus;
100  settype($bOnlyPublic, 'boolean');
101 
102  $sExtraSql = $bOnlyPublic ? ' AND `allow_view_to` = ' . CH_WSB_PG_ALL : null;
103  $sQuery = "SELECT COUNT(*) FROM `{$this -> _sTable}` WHERE `poll_featured` = {$iStatus} {$sExtraSql}";
104  return $this -> getOne($sQuery);
105  }
106 
113  function getAllFeaturedPolls($sLimit, $iStatus = 1, $bOnlyPublic = false)
114  {
115  $sLimit = $this -> escape($sLimit, false);
116  $iStatus = (int) $iStatus;
117  settype($bOnlyPublic, 'boolean');
118 
119  $sExtraSql = $bOnlyPublic ? ' AND `allow_view_to` = ' . CH_WSB_PG_ALL : null;
120  $sQuery = "SELECT *, UNIX_TIMESTAMP()-`poll_date` AS `poll_ago` FROM `{$this -> _sTable}` WHERE `poll_status`='active' AND `poll_approval`='1' AND `poll_featured`='{$iStatus}' {$sExtraSql} ORDER BY `poll_date` DESC {$sLimit}";
121  return $this -> getAll($sQuery);
122  }
123 
132  function getPollsCount($iMemberID = 0, $bAllPools = false, $iApproval = 1, $sExtraSql = null)
133  {
134  $iMemberID = (int) $iMemberID;
135 // $bOnlyPublic = (bool) $bOnlyPublic;
136  $iApproval = (int) $iApproval;
137  $sExtraSql = $this -> escape($sExtraSql, false);
138 
139  // ** init some needed variables ;
140 
141  $sAddonSql = null;
142  $aWhereCondition = array();
143 
144  $aWhereCondition[0] = " AND `poll_status` = 'active' ";
145  $aWhereCondition[1] = " AND `poll_approval` = {$iApproval} ";
146  $aWhereCondition[2] = " AND `id_profile` = {$iMemberID} ";
147 
148  // if need the only member's pools ;
149  $sIsEmpty = true;
150  if ( $iMemberID ) {
151  if ( !$bAllPools ) {
152  // only approved member's polls ;
153  $sAddonSql = $aWhereCondition[1] . $aWhereCondition[2];
154  } else {
155  $sAddonSql = $aWhereCondition[2];
156  }
157  } else {
158  if ( !$bAllPools ) {
159  $sAddonSql = $aWhereCondition[0] . $aWhereCondition[1];
160  }
161  }
162 
163  $sQuery =
164  "
165  SELECT
166  COUNT(*)
167  FROM
168  `{$this -> _sTable}`
169  WHERE
170  1
171  {$sAddonSql}
172  {$sExtraSql}
173  ";
174 
175  return $this -> getOne($sQuery);
176  }
177 
178 
186  {
187  $iProfileId = (int) $iProfileId;
188 
189  $sQuery = "SELECT COUNT(*) FROM `{$this -> _sTable}` WHERE `poll_approval` = 0 AND `id_profile` = {$iProfileId}";
190  return $this -> getOne($sQuery);
191  }
192 
204  {
205  $iPollId = (int) $iPollId;
206 
207  $sQuery ="SELECT *, UNIX_TIMESTAMP()-`poll_date` AS 'poll_ago' FROM `{$this -> _sTable}` WHERE `id_poll` = {$iPollId}";
208  return $this -> getAll($sQuery);
209  }
210 
219  function setVotes( $iPollId, $sVotes, $iVotesCount )
220  {
221  $iPollId = (int) $iPollId;
222  $sVotes = process_db_input($sVotes, CH_TAGS_STRIP);
223  $iVotesCount = (int) $iVotesCount;
224 
225  $sQuery =
226  "
227  UPDATE
228  `{$this -> _sTable}`
229  SET
230  `poll_results` = '{$sVotes}',
231  `poll_total_votes` = {$iVotesCount}
232  WHERE
233  `id_poll` = {$iPollId}
234  ";
235 
236  return $this -> query($sQuery);
237  }
238 
245  function setOption($iPollId, $sAction = 'approval')
246  {
247  $iPollId = (int) $iPollId;
248 
249  $sQuery =
250  "
251  UPDATE
252  `{$this -> _sTable}`
253  SET
254  `poll_{$sAction}` = IF (`poll_{$sAction}` = 1, 0, 1)
255  WHERE
256  `id_poll` = {$iPollId}
257  ";
258 
259  return $this -> query($sQuery);
260  }
261 
274  function createPoll($aPoolInfo, $isAdmin = false)
275  {
276  $isAdmin = (bool) $isAdmin;
277 
278  if( !is_array($aPoolInfo) ) {
279  return;
280  }
281 
282  // process recived array
283  foreach($aPoolInfo as $sKey => $sValue) {
284  $aPoolInfo[$sKey] = process_db_input($sValue, CH_TAGS_NO_ACTION
285  , CH_SLASHES_AUTO);
286  }
287 
288  // ** init some needed variables ;
289 
290  $iPollsCount = -1;
291  $iAutoActive = 1;
292  $iResponse = 0;
293 
294  if ($aPoolInfo['owner_id']) {
295  $iPollsCount = $this -> getPollsCount( $aPoolInfo['owner_id'], true );
296  $iAutoActive = ($this -> _oConfig -> iAutoActivate) ? 1 : 0;
297 
298  if (!$this -> _oConfig -> iAlowMembersPolls) {
299  return 0;
300  }
301  }
302 
303  if ($iPollsCount < $this -> _oConfig -> iAlowPollNumber || $isAdmin) {
304  $iCurrentDate = date('U');
305  $sQuery =
306  "
307  INSERT INTO
308  `{$this -> _sTable}`
309  SET
310  `id_profile` = {$aPoolInfo['owner_id']},
311  `poll_question` = '{$aPoolInfo['question']}',
312  `poll_answers` = '{$aPoolInfo['answers']}',
313  `poll_results` = '{$aPoolInfo['results']}',
314  `poll_status` = 'active',
315  `poll_approval` = {$iAutoActive},
316  `poll_date` = {$iCurrentDate},
317  `poll_tags` = '{$aPoolInfo['tags']}',
318  `allow_comment_to` = {$aPoolInfo['allow_comment']},
319  `allow_vote_to` = {$aPoolInfo['allow_vote']},
320  `poll_categories` = '{$aPoolInfo['category']}',
321  `allow_view_to` = '{$aPoolInfo['allow_view']}'
322  ";
323 
324  $this -> query($sQuery);
325  $iResponse = 1;
326  } else {
327  $iResponse = 2;
328  }
329 
330  return $iResponse;
331  }
332 
345  function editPoll($aPoolInfo)
346  {
347  if( !is_array($aPoolInfo) ) {
348  return;
349  }
350 
351  // procces recived array
352  foreach($aPoolInfo as $sKey => $sValue) {
353  if($sKey != 'answers' ) {
354  $aPoolInfo[$sKey] = process_db_input($sValue, CH_TAGS_STRIP);
355  } else {
356  $aPoolInfo[$sKey] = process_db_input($sValue);
357  }
358  }
359 
360  // ** init some neede variables ;
361 
362  $sAddonSql = null;
363 
364  if ( isset($aPoolInfo['approve']) ) {
365  $sAddonSql = ( $aPoolInfo['approve'] )
366  ? ',`poll_approval` = 1'
367  : ',`poll_approval` = 0';
368  }
369 
370  $sQuery =
371  "
372  UPDATE
373  `{$this -> _sTable}`
374  SET
375  `poll_question` = '{$aPoolInfo['question']}',
376  `poll_answers` = '{$aPoolInfo['answers']}',
377  `poll_status` = '{$aPoolInfo['status']}',
378  `poll_tags` = '{$aPoolInfo['tags']}',
379  `allow_comment_to` = {$aPoolInfo['allow_comment']},
380  `allow_vote_to` = {$aPoolInfo['allow_vote']},
381  `poll_categories` = '{$aPoolInfo['category']}',
382  `allow_view_to` = '{$aPoolInfo['allow_view']}'
383  {$sAddonSql}
384  WHERE
385  `id_poll` = {$aPoolInfo['Id']}
386  ";
387 
388  return $this -> query($sQuery);
389  }
390 
398  {
399  $iPollId = (int) $iPollId;
400  $this -> clearAttachetData($iPollId);
401  $sQuery = "DELETE FROM `{$this -> _sTable}` WHERE `id_poll` = {$iPollId}";
402  return $this -> query($sQuery);
403  }
404 
412  {
413  $iPollId = (int) $iPollId;
414 
415  // delete from comments;
416  $sQuery =
417  "
418  DELETE
419  `{$this -> sTablePrefix}cmts`,
420  `{$this -> sTablePrefix}cmts_track`
421  FROM
422  `{$this -> sTablePrefix}cmts`
423  LEFT JOIN
424  `{$this -> sTablePrefix}cmts_track`
425  ON
426  `{$this -> sTablePrefix}cmts_track`.`cmt_id` = `{$this -> sTablePrefix}cmts`.`cmt_id`
427  WHERE
428  `{$this -> sTablePrefix}cmts`.`cmt_object_id` = {$iPollId}
429  ";
430  db_res($sQuery);
431 
432  // delete from votings;
433  $sQuery =
434  "
435  DELETE
436  `{$this -> sTablePrefix}rating`,
437  `{$this -> sTablePrefix}voting_track`
438  FROM
439  `{$this -> sTablePrefix}rating`
440  LEFT JOIN
441  `{$this -> sTablePrefix}voting_track`
442  ON
443  `{$this -> sTablePrefix}voting_track`.`id` = `{$this -> sTablePrefix}rating`.`id`
444  WHERE
445  `{$this -> sTablePrefix}rating`.`id` = {$iPollId}
446  ";
447  db_res($sQuery);
448  }
449 
460  function getPollsByMonth ($iYear, $iMonth, $iNextYear, $iNextMonth)
461  {
462  $iYear = (int) $iYear;
463  $iMonth = (int) $iMonth;
464  $iNextYear = (int) $iNextYear;
465  $iNextMonth = (int) $iNextMonth;
466 
467  return $this->getAll ("SELECT `id_poll`, DAYOFMONTH(FROM_UNIXTIME(`poll_date`)) AS `Day`
468  FROM `{$this -> _sTable}`
469  WHERE `poll_date` >= UNIX_TIMESTAMP('{$iYear}-{$iMonth}-1') AND `poll_date` < UNIX_TIMESTAMP('{$iNextYear}-{$iNextMonth}-1') AND `poll_approval` = 1");
470  }
471 
477  function getSettingsCategory($sCatName)
478  {
479  $sCatName = process_db_input($sCatName, CH_TAGS_STRIP);
480  return $this -> getOne('SELECT `kateg` FROM `sys_options` WHERE `Name` = "' . $sCatName . '"');
481  }
482 }
process_db_input
process_db_input($sText, $iStripTags=0)
Definition: utils.inc.php:256
$iPollId
$iPollId
Definition: index.php:20
ChPollDb\getSettingsCategory
getSettingsCategory($sCatName)
Definition: ChPollDb.php:477
ChPollDb\clearAttachetData
clearAttachetData($iPollId)
Definition: ChPollDb.php:411
ChPollDb\deletePoll
deletePoll($iPollId)
Definition: ChPollDb.php:397
php
ChWsbModuleDb
Definition: ChWsbModuleDb.php:12
ChWsbDb\getAll
getAll($sQuery, $aBindings=[], $iFetchType=PDO::FETCH_ASSOC)
Definition: ChWsbDb.php:206
ChPollDb\$_sTable
$_sTable
Definition: ChPollDb.php:13
ChPollDb\setOption
setOption($iPollId, $sAction='approval')
Definition: ChPollDb.php:245
ChPollDb\getAllFeaturedPolls
getAllFeaturedPolls($sLimit, $iStatus=1, $bOnlyPublic=false)
Definition: ChPollDb.php:113
ChPollDb\createPoll
createPoll($aPoolInfo, $isAdmin=false)
Definition: ChPollDb.php:274
ChPollDb\setVotes
setVotes( $iPollId, $sVotes, $iVotesCount)
Definition: ChPollDb.php:219
ChPollDb\$sTablePrefix
$sTablePrefix
Definition: ChPollDb.php:14
ChPollDb\getAllPolls
getAllPolls($sLimit, $iMemberID=0, $bAllPolls=false, $iApproval=1, $sExtraSql=null, $sOrder='`poll_date` DESC')
Definition: ChPollDb.php:41
ChWsbDb\query
query($sQuery, $aBindings=[])
Definition: ChWsbDb.php:386
CH_TAGS_NO_ACTION
const CH_TAGS_NO_ACTION
Definition: utils.inc.php:21
ChPollDb\__construct
__construct(&$oConfig)
Definition: ChPollDb.php:19
ChPollDb
Definition: ChPollDb.php:11
ChPollDb\getPollsByMonth
getPollsByMonth($iYear, $iMonth, $iNextYear, $iNextMonth)
Definition: ChPollDb.php:460
CH_WSB_PG_ALL
const CH_WSB_PG_ALL
Definition: ChWsbPrivacy.php:12
ChWsbDb\getOne
getOne($sQuery, $aBindings=[], $iIndex=0)
Definition: ChWsbDb.php:263
ChPollDb\getPollInfo
getPollInfo($iPollId)
Definition: ChPollDb.php:203
CH_TAGS_STRIP
const CH_TAGS_STRIP
Definition: utils.inc.php:22
ChPollDb\getFeaturedCount
getFeaturedCount($iStatus=1, $bOnlyPublic=false)
Definition: ChPollDb.php:97
db_res
db_res($query, $bindings=[])
Definition: db.inc.php:39
ChPollDb\getPollsCount
getPollsCount($iMemberID=0, $bAllPools=false, $iApproval=1, $sExtraSql=null)
Definition: ChPollDb.php:132
ChPollDb\editPoll
editPoll($aPoolInfo)
Definition: ChPollDb.php:345
ChPollDb\getUnApprovedPolls
getUnApprovedPolls($iProfileId)
Definition: ChPollDb.php:185
$sAction
$sAction
Definition: categories.php:274
CH_SLASHES_AUTO
const CH_SLASHES_AUTO
Definition: utils.inc.php:27
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbDb\escape
escape($sText, $bReal=true)
Definition: ChWsbDb.php:624
ChPollDb\$_oConfig
$_oConfig
Definition: ChPollDb.php:12
$iProfileId
if( $sMembersList) $iProfileId
Definition: communicator.php:29