PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/polls.php

https://bitbucket.org/ryanhowdy/family-connections
PHP | 655 lines | 493 code | 82 blank | 80 comment | 30 complexity | 6a8932413230998f2a7de573b9937914 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Polls
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category FCMS
  8. * @package FamilyConnections
  9. * @author Ryan Haudenschilt <r.haudenschilt@gmail.com>
  10. * @copyright 2007 Haudenschilt LLC
  11. * @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
  12. * @link http://www.familycms.com/wiki/
  13. */
  14. session_start();
  15. define('URL_PREFIX', '../');
  16. define('GALLERY_PREFIX', '../gallery/');
  17. require URL_PREFIX.'fcms.php';
  18. load('alerts');
  19. init('admin/');
  20. // Globals
  21. $alert = new Alerts($fcmsUser->id);
  22. $TMPL = array(
  23. 'sitename' => getSiteName(),
  24. 'nav-link' => getAdminNavLinks(),
  25. 'pagetitle' => T_('Administration: Polls'),
  26. 'path' => URL_PREFIX,
  27. 'displayname' => $fcmsUser->displayName,
  28. 'version' => getCurrentVersion(),
  29. 'year' => date('Y')
  30. );
  31. control();
  32. exit();
  33. /**
  34. * control
  35. *
  36. * The controlling structure for this script.
  37. *
  38. * @return void
  39. */
  40. function control ()
  41. {
  42. global $fcmsUser;
  43. if (checkAccess($fcmsUser->id) > 2)
  44. {
  45. displayInvalidAccessLevel();
  46. return;
  47. }
  48. elseif (isset($_GET['alert']))
  49. {
  50. displayRemoveAlertSubmit();
  51. }
  52. elseif (isset($_POST['delsubmit']))
  53. {
  54. if (!isset($_GET['confirmed']))
  55. {
  56. displayConfirmDeleteForm();
  57. }
  58. else
  59. {
  60. displayDeleteSubmit();
  61. }
  62. }
  63. // Edit
  64. elseif (isset($_GET['editpoll']))
  65. {
  66. displayEditForm();
  67. }
  68. elseif (isset($_POST['editsubmit']))
  69. {
  70. displayEditFormSubmit();
  71. }
  72. // Add
  73. elseif (isset($_GET['addpoll']))
  74. {
  75. displayAddForm();
  76. }
  77. elseif (isset($_POST['addsubmit']))
  78. {
  79. displayAddFormSubmit();
  80. }
  81. else
  82. {
  83. displayPolls();
  84. }
  85. }
  86. /**
  87. * displayHeader
  88. *
  89. * @return void
  90. */
  91. function displayHeader ()
  92. {
  93. global $fcmsUser, $TMPL;
  94. $TMPL['javascript'] = '
  95. <script src="'.URL_PREFIX.'ui/js/prototype.js" type="text/javascript"></script>
  96. <script src="'.URL_PREFIX.'ui/js/fcms.js" type="text/javascript"></script>
  97. <script type="text/javascript">
  98. //<![CDATA[
  99. Event.observe(window, \'load\', function() {
  100. deleteConfirmationLinks("delpoll", "'.T_('Are you sure you want to DELETE this?').'");
  101. });
  102. //]]>
  103. </script>';
  104. include_once URL_PREFIX.'ui/admin/header.php';
  105. echo '
  106. <div id="polls" class="centercontent">
  107. <p><a class="btn" href="?addpoll=yes">'.T_('Add New Poll').'</a></p>';
  108. }
  109. /**
  110. * displayFooter
  111. *
  112. * @return void
  113. */
  114. function displayFooter ()
  115. {
  116. global $fcmsUser, $TMPL;
  117. echo '
  118. </div><!--/centercontent-->';
  119. include_once URL_PREFIX.'ui/admin/footer.php';
  120. }
  121. /**
  122. * displayInvalidAccessLevel
  123. *
  124. * @return void
  125. */
  126. function displayInvalidAccessLevel ()
  127. {
  128. displayHeader();
  129. echo '
  130. <p class="alert-message block-message error">
  131. <b>'.T_('You do not have access to view this page.').'</b><br/>
  132. '.T_('This page requires an access level 2 (Helper) or better.').'
  133. <a href="'.URL_PREFIX.'contact.php">'.T_('Please contact your website\'s administrator if you feel you should have access to this page.').'</a>
  134. </p>';
  135. displayFooter();
  136. }
  137. /**
  138. * displayRemoveAlertSubmit
  139. *
  140. * @return void
  141. */
  142. function displayRemoveAlertSubmit ()
  143. {
  144. global $fcmsUser;
  145. $sql = "INSERT INTO `fcms_alerts` (`alert`, `user`)
  146. VALUES (
  147. '".escape_string($_GET['alert'])."',
  148. '$fcmsUser->id'
  149. )";
  150. if (!mysql_query($sql))
  151. {
  152. displayHeader();
  153. displaySqlError($sql, mysql_error());
  154. displayFooter();
  155. return;
  156. }
  157. header("Location: polls.php");
  158. }
  159. /**
  160. * displayPolls
  161. *
  162. * @return void
  163. */
  164. function displayPolls ()
  165. {
  166. global $fcmsUser, $alert;
  167. displayHeader();
  168. $alert->displayPoll($fcmsUser->id);
  169. $page = getPage();
  170. $from = (($page * 10) - 10);
  171. $sql = "SELECT `id`, `question`, `started`
  172. FROM fcms_polls
  173. ORDER BY `started` DESC
  174. LIMIT $from, 10";
  175. $result = mysql_query($sql);
  176. if (!$result)
  177. {
  178. displaySqlError($sql, mysql_error());
  179. displayFooter();
  180. return;
  181. }
  182. echo '
  183. <h3>'.T_('Past Polls').'</h3>
  184. <table class="zebra-striped">
  185. <thead>
  186. <tr>
  187. <th>'.T_('Question').'</th>
  188. <th>'.T_('Created').'</th>
  189. <th>'.T_('Actions').'</th>
  190. </tr>
  191. </thead>
  192. <tbody>';
  193. if (mysql_num_rows($result) > 0)
  194. {
  195. while ($r = mysql_fetch_array($result))
  196. {
  197. echo '
  198. <tr>
  199. <td>'.cleanOutput($r['question']).'</td>
  200. <td>'.$r['started'].'</td>
  201. <td>
  202. <form action="polls.php" method="post">
  203. <a class="btn" href="?editpoll='.$r['id'].'">'.T_('Edit').'</a>
  204. <input type="submit" name="delsubmit" class="btn danger delpoll" value="'.T_('Delete').'" title="'.T_('Delete').'"/>
  205. <input type="hidden" name="pollid" value="'.$r['id'].'"/>
  206. </form>
  207. </td>
  208. </tr>';
  209. }
  210. // Remove the LIMIT from the $sql statement
  211. // used above, so we can get the total count
  212. $sql = substr($sql, 0, strpos($sql, 'LIMIT'));
  213. $result = mysql_query($sql);
  214. if (!$result)
  215. {
  216. displaySqlError($sql, mysql_error());
  217. displayFooter();
  218. return;
  219. }
  220. $count = mysql_num_rows($result);
  221. $total_pages = ceil($count / 10);
  222. displayPages("polls.php", $page, $total_pages);
  223. }
  224. else
  225. {
  226. echo '<tr><td colspan="3">'.T_('No Previous Polls').'</td></tr>';
  227. }
  228. echo '
  229. </tbody>
  230. </table>';
  231. displayFooter();
  232. }
  233. /**
  234. * displayEditForm
  235. *
  236. * @return void
  237. */
  238. function displayEditForm ()
  239. {
  240. displayHeader();
  241. $id = (int)$_GET['editpoll'];
  242. $sql = "SELECT `question`, o.`id`, `option`
  243. FROM `fcms_polls` AS p, `fcms_poll_options` AS o
  244. WHERE p.`id` = o.`poll_id`
  245. AND p.`id` = '$id'";
  246. $result = mysql_query($sql);
  247. if (!$result)
  248. {
  249. displaySqlError($sql, mysql_error());
  250. displayFooter();
  251. return;
  252. }
  253. if (isset($_SESSION['success']))
  254. {
  255. echo '
  256. <div class="alert-message success">
  257. <a class="close" href="#" onclick="$(this).up(\'div\').hide(); return false;">&times;</a>
  258. '.T_('Changes Updated Successfully').'
  259. </div>';
  260. unset($_SESSION['success']);
  261. }
  262. echo '
  263. <form id="editform" name="editform" action="?page=admin_polls" method="post">
  264. <fieldset>
  265. <legend><span>'.T_('Edit Poll').'</span></legend>';
  266. $i = 1;
  267. while ($row = mysql_fetch_assoc($result))
  268. {
  269. if ($i < 2)
  270. {
  271. echo '
  272. <h3>'.cleanOutput($row['question']).'</h3>';
  273. }
  274. echo '
  275. <div class="clearfix">
  276. <label for="show'.$i.'">'.sprintf(T_('Option %s'), $i).'</label>
  277. <div class="input">
  278. <input type="text" name="show'.$i.'" id="show'.$i.'" ';
  279. if ($i < 3)
  280. {
  281. echo "class=\"required\"";
  282. }
  283. echo ' size="50" value="'.cleanOutput($row['option']).'"/>
  284. <input type="hidden" name="option'.$i.'" value="'.$row['id'].'"/>';
  285. // Needs to be created by js
  286. if ($i >= 3)
  287. {
  288. echo '
  289. <input type="button" name="deleteoption" class="btn small danger" style="width:auto;" value="'.T_('Delete').'"
  290. onclick="document.editform.show'.$i.'.value=\'\';"/>';
  291. }
  292. echo '
  293. </div>
  294. </div>';
  295. $i++;
  296. }
  297. while ($i < 11)
  298. {
  299. echo '
  300. <div class="clearfix">
  301. <label for="show'.$i.'">'.sprintf(T_('Option %s'), $i).'</label>
  302. <div class="input">
  303. <input type="text" id="show'.$i.'" name="show'.$i.'" size="50" value=""/>
  304. <input type="hidden" name="option'.$i.'" value="new"/>
  305. </div>
  306. </div>';
  307. $i++;
  308. }
  309. echo '
  310. <p class="actions">
  311. <input class="btn primary" type="submit" name="editsubmit" id="editsubmit" value="'.T_('Edit').'"/>
  312. <a class="btn secondary" href="polls.php">'.T_('Cancel').'</a>
  313. </p>
  314. </fieldset>
  315. </form>';
  316. displayFooter();
  317. }
  318. /**
  319. * displayEditFormSubmit
  320. *
  321. * @return void
  322. */
  323. function displayEditFormSubmit ()
  324. {
  325. $sql = "SELECT MAX(id) AS c
  326. FROM `fcms_polls`";
  327. $result = mysql_query($sql);
  328. if (!$result)
  329. {
  330. displayHeader();
  331. displaySqlError($sql, mysql_error());
  332. displayFooter();
  333. return;
  334. }
  335. $found = mysql_fetch_array($result);
  336. $latestId = $found['c'];
  337. $i = 1;
  338. while ($i <= 10)
  339. {
  340. if ($_POST['show'.$i])
  341. {
  342. if ($_POST['option'.$i] == 'new')
  343. {
  344. $sql = "INSERT INTO `fcms_poll_options`
  345. (`poll_id`, `option`, `votes`)
  346. VALUES (
  347. '$latestId',
  348. '".escape_string($_POST['show'.$i])."',
  349. 0
  350. )";
  351. if (!mysql_query($sql))
  352. {
  353. displayHeader();
  354. displaySqlError($sql, mysql_error());
  355. displayFooter();
  356. return;
  357. }
  358. }
  359. else
  360. {
  361. $sql = "UPDATE `fcms_poll_options`
  362. SET `option` = '".escape_string($_POST['show'.$i])."'
  363. WHERE `id` = '".escape_string($_POST['option'.$i])."'";
  364. if (!mysql_query($sql))
  365. {
  366. displayHeader();
  367. displaySqlError($sql, mysql_error());
  368. displayFooter();
  369. return;
  370. }
  371. }
  372. }
  373. elseif ($_POST['option'.$i] != 'new')
  374. {
  375. $sql = "DELETE FROM `fcms_poll_options`
  376. WHERE `id` = '".escape_string($_POST['option'.$i])."'";
  377. if (!mysql_query($sql))
  378. {
  379. displayHeader();
  380. displaySqlError($sql, mysql_error());
  381. displayFooter();
  382. return;
  383. }
  384. }
  385. $i++;
  386. }
  387. $_SESSION['success'] = 1;
  388. header("Location: polls.php");
  389. }
  390. /**
  391. * displayAddForm
  392. *
  393. * @return void
  394. */
  395. function displayAddForm ()
  396. {
  397. displayHeader();
  398. if (isset($_SESSION['success']))
  399. {
  400. echo '
  401. <div class="alert-message success">
  402. <a class="close" href="#" onclick="$(this).up(\'div\').hide(); return false;">&times;</a>
  403. '.T_('Changes Updated Successfully').'
  404. </div>';
  405. unset($_SESSION['success']);
  406. }
  407. echo '
  408. <script type="text/javascript" src="'.URL_PREFIX.'ui/js/livevalidation.js"></script>
  409. <form id="addform" action="polls.php" method="post">
  410. <fieldset>
  411. <legend><span>'.T_('Add New Poll').'</span></legend>
  412. <div class="clearfix">
  413. <label for="question">'.T_('Poll Question').'</label>
  414. <div class="input"><input type="text" name="question" id="question" class="required span8"/></div>
  415. </div>
  416. <script type="text/javascript">
  417. var fq = new LiveValidation(\'question\', { onlyOnSubmit: true });
  418. fq.add(Validate.Presence, { failureMessage: "'.T_('Required').'" });
  419. </script>
  420. <div class="clearfix">
  421. <label for="option1">'.sprintf(T_('Option %s'), '1').'</label>
  422. <div class="input"><input type="text" name="option1" id="option1" class="required"/></div>
  423. </div>
  424. <script type="text/javascript">
  425. var foption1 = new LiveValidation(\'option1\', { onlyOnSubmit: true });
  426. foption1.add(Validate.Presence, {failureMessage: "'.T_('Without at least 2 options, it\'s not much of a poll is it?').'"});
  427. </script>
  428. <div class="clearfix">
  429. <label for="option2">'.sprintf(T_('Option %s'), '2').'</label>
  430. <div class="input"><input type="text" name="option2" id="option2" class="required"/></div>
  431. </div>
  432. <script type="text/javascript">
  433. var foption2 = new LiveValidation(\'option2\', { onlyOnSubmit: true });
  434. foption2.add(Validate.Presence, {failureMessage: "'.T_('Without at least 2 options, it\'s not much of a poll is it?').'"});
  435. </script>
  436. <div class="clearfix">
  437. <label for="option3">'.sprintf(T_('Option %s'), '3').'</label>
  438. <div class="input"><input type="text" name="option3" id="option3"/></div>
  439. </div>
  440. <div class="clearfix">
  441. <label for="option4">'.sprintf(T_('Option %s'), '4').'</label>
  442. <div class="input"><input type="text" name="option4" id="option4"/></div>
  443. </div>
  444. <div class="clearfix">
  445. <label for="option5">'.sprintf(T_('Option %s'), '5').'</label>
  446. <div class="input"><input type="text" name="option5" id="option5"/></div>
  447. </div>
  448. <div class="clearfix">
  449. <label for="option6">'.sprintf(T_('Option %s'), '6').'</label>
  450. <div class="input"><input type="text" name="option6" id="option6"/></div>
  451. </div>
  452. <div class="clearfix">
  453. <label for="option7">'.sprintf(T_('Option %s'), '7').'</label>
  454. <div class="input"><input type="text" name="option7" id="option7"/></div>
  455. </div>
  456. <div class="clearfix">
  457. <label for="option8">'.sprintf(T_('Option %s'), '8').'</label>
  458. <div class="input"><input type="text" name="option8" id="option8"/></div>
  459. </div>
  460. <div class="clearfix">
  461. <label for="option9">'.sprintf(T_('Option %s'), '9').'</label>
  462. <div class="input"><input type="text" name="option9" id="option9"/></div>
  463. </div>
  464. <div class="clearfix">
  465. <label for="option10">'.sprintf(T_('Option %s'), '10').'</label>
  466. <div class="input"><input type="text" name="option10" id="option10"/></div>
  467. </div>
  468. <p class="actions">
  469. <input class="btn primary" type="submit" name="addsubmit" value="'.T_('Add').'"/>
  470. <a class="btn secondary" href="polls.php">'.T_('Cancel').'</a>
  471. </p>
  472. </fieldset>
  473. </form>';
  474. displayFooter();
  475. }
  476. /**
  477. * displayAddFormSubmit
  478. *
  479. * @return void
  480. */
  481. function displayAddFormSubmit ()
  482. {
  483. $question = strip_tags($_POST['question']);
  484. $question = escape_string($question);
  485. $sql = "INSERT INTO `fcms_polls`(`question`, `started`)
  486. VALUES (
  487. '$question',
  488. NOW()
  489. )";
  490. if (!mysql_query($sql))
  491. {
  492. displayHeader();
  493. displaySqlError($sql, mysql_error());
  494. displayFooter();
  495. return;
  496. }
  497. $pollId = mysql_insert_id();
  498. $i = 1;
  499. while ($i <= 10)
  500. {
  501. if ($_POST['option'.$i])
  502. {
  503. $option = strip_tags($_POST['option'.$i]);
  504. $option = escape_string($option);
  505. $sql = "INSERT INTO `fcms_poll_options`(`poll_id`, `option`, `votes`)
  506. VALUES (
  507. '$pollId',
  508. '$option',
  509. 0
  510. )";
  511. if (!mysql_query($sql))
  512. {
  513. displayHeader();
  514. displaySqlError($sql, mysql_error());
  515. displayFooter();
  516. return;
  517. }
  518. }
  519. $i++;
  520. }
  521. $_SESSION['success'] = 1;
  522. header("Location: polls.php");
  523. }
  524. /**
  525. * displayConfirmDeleteForm
  526. *
  527. * @return void
  528. */
  529. function displayConfirmDeleteForm ()
  530. {
  531. displayHeader();
  532. echo '
  533. <div class="alert-message block-message warning">
  534. <form action="polls.php?confirmed=1" method="post">
  535. <h2>'.T_('Are you sure you want to DELETE this?').'</h2>
  536. <p><b><i>'.T_('This can NOT be undone.').'</i></b></p>
  537. <div class="alert-actions">
  538. <input type="hidden" name="pollid" value="'.(int)$_POST['pollid'].'"/>
  539. <input class="btn danger" type="submit" id="delsubmit" name="delsubmit" value="'.T_('Yes, Delete').'"/>
  540. <a class="btn secondary" href="polls.php">'.T_('No, Cancel').'</a>
  541. </div>
  542. </form>
  543. </div>';
  544. displayFooter();
  545. }
  546. /**
  547. * displayDeleteSubmit
  548. *
  549. * @return void
  550. */
  551. function displayDeleteSubmit ()
  552. {
  553. $id = (int)$_POST['pollid'];
  554. $sql = "DELETE FROM fcms_poll_options
  555. WHERE id = '$id'";
  556. if (!mysql_query($sql))
  557. {
  558. displayHeader();
  559. displaySqlError($sql, mysql_error());
  560. displayFooter();
  561. return;
  562. }
  563. $sql = "DELETE FROM `fcms_polls`
  564. WHERE `id` = '$id'";
  565. if (!mysql_query($sql))
  566. {
  567. displayHeader();
  568. displaySqlError($sql, mysql_error());
  569. displayFooter();
  570. return;
  571. }
  572. header("Location: polls.php");
  573. }