cvs: pearweb /include pear-election.php /public_html election-edit.php election-new.php /templates election-listforedit.tpl.php
election-new-step1.tpl.php election-new-step2.tpl.php election-new-step3.tpl.php election-vote.tpl.php

From: Date: Fri, 10 Nov 2006 03:46:28 +0000
Subject: cvs: pearweb /include pear-election.php /public_html election-edit.php election-new.php /templates election-listforedit.tpl.php
election-new-step1.tpl.php election-new-step2.tpl.php election-new-step3.tpl.php election-vote.tpl.php
Groups: php.pear.cvs php.pear.core 
Request: Send a blank email to pear-core+get-5163@lists.php.net to get a copy of this message
cellog Fri Nov 10 03:46:28 2006 UTC Added files: /pearweb/public_html election-edit.php /pearweb/templates election-listforedit.tpl.php Modified files: /pearweb/include pear-election.php /pearweb/public_html election-new.php /pearweb/templates election-new-step1.tpl.php election-new-step2.tpl.php election-new-step3.tpl.php election-vote.tpl.php Log: add the ability to edit existing elections Election support is now 100% complete except for how it should be listed on the front page

http://cvs.php.net/viewvc.cgi/pearweb/include/pear-election.php?r1=1.2&r2=1.3&diff_format=u Index: pearweb/include/pear-election.php diff -u pearweb/include/pear-election.php:1.2 pearweb/include/pear-election.php:1.3 --- pearweb/include/pear-election.php:1.2 Thu Nov 9 05:10:17 2006 +++ pearweb/include/pear-election.php Fri Nov 10 03:46:27 2006 @@ -11,7 +11,76 @@ $this->karma =& new Damblan_Karma($this->dbh); } - function validateStep1() + function listElections() + { + if ($this->karma->has($this->user, 'pear.admin')) { + return $this->dbh->getAll(' + SELECT + IF(votestart > NOW(),"no","yes") as active, elections.* + FROM elections + ORDER BY votestart DESC + ', array(), DB_FETCHMODE_ASSOC); + } else { + // if we aren't admin, we can't touch other people's elections + return $this->dbh->getAll(' + SELECT + IF(votestart > NOW(),"no","yes") as active, elections.* + FROM elections WHERE + votestart > NOW() AND + creator=? + ', array($this->user), DB_FETCHMODE_ASSOC); + } + } + + function electionExists($id) + { + return $this->dbh->getOne('SELECT id FROM elections WHERE id=?', array($id)); + } + + function setupChoices($id, $info) + { + $all = $this->dbh->getAssoc(' + SELECT choice,summary,summary_link + FROM election_choices + WHERE election_id=? + ORDER BY choice + ', false, array($id), DB_FETCHMODE_ASSOC); + for ($i = 1; $i <= $_POST['choices']; $i++) { + if (isset($all[$i])) { + $info['summary' . $i] = $all[$i]['summary']; + $info['summary_link' . $i] = $all[$i]['summary_link']; + } else { + $info['summary' . $i] = + empty($_POST['summary' . $i]) ? '' : $_POST['summary' . $i]; + $info['summary_link' . $i] = + empty($_POST['summary_link' . $i]) ? '' : $_POST['summary_link' . $i]; + } + } + $info['choices'] = $_POST['choices']; + return $info; + } + + function getInfo($id) + { + $all = $this->dbh->getAll(' + SELECT + purpose, detail, + YEAR(votestart) as year, MONTH(votestart) as month, + DAYOFMONTH(votestart) as day, voteend - votestart as length, + minimum_choices as minimum, maximum_choices maximum, + eligiblevoters, COUNT(c.choice) as choices + FROM elections e, election_choices c + WHERE id=? AND c.election_id = e.id + GROUP BY c.election_id + ', array($id), DB_FETCHMODE_ASSOC); + if (!count($all)) { + return false; + } + $info = $all[0]; + return $info; + } + + function validateStep1($new = true) { $error = array(); if (empty($_POST['purpose'])) { @@ -76,7 +145,7 @@ if (strtotime($date) != strtotime(date('Y-m-d', strtotime($date)))) { $error[] = 'Full date is invalid'; } - if ((strtotime($date) - time()) / 86400 < 29.0) { + if ($new && (strtotime($date) - time()) / 86400 < 29.0) { $error[] = 'Voting must start at least 30 days from today'; } if (empty($_POST['length'])) { @@ -175,8 +244,65 @@ } } + function saveEditedElection() + { + $id = $_POST['election_id']; + $info = $this->getInfo($id); + $startdate = date('Y-m-d', + strtotime($_POST['year'] . '-' . $_POST['month'] . '-' . $_POST['day'])); + $enddate = date('Y-m-d', + strtotime($startdate . '. +' . $_POST['length'] . ' days')); + $this->dbh->query(' + UPDATE elections + SET + purpose=?, + detail=?, + votestart=?, + voteend=?, + minimum_choices=?, + maximum_choices=?, + eligiblevoters=? + WHERE + id=? + ', array( + $_POST['purpose'], + $_POST['detail'], + $startdate, + $enddate, + $_POST['minimum'], + $_POST['maximum'], + $_POST['eligiblevoters'], + $id + )); + $this->dbh->query('DELETE FROM election_choices WHERE election_id=?', array($id)); + for ($i = 1; $i <= $_POST['choices']; $i++) { + $this->dbh->query(' + INSERT INTO election_choices + (election_id, choice, summary, summary_link) + VALUES(?,?,?,?) + ', array($id, $i, $_POST['summary' . $i], $_POST['summary_link' . $i])); + } + } + function canEdit($id) { - + if ('yes' == $this->dbh->getOne(' + SELECT + IF(votestart > NOW(),"no","yes") FROM elections + WHERE id=?', array($id))) { + // cannot edit active or old elections + return false; + } + if ($this->karma->has($this->user, 'pear.admin')) { + return true; + } + if (!$this->electionExists($id)) { + return false; + } + if ($this->user == $this->dbh->getOne('SELECT creator FROM elections WHERE id=?', + array($id))) { + return true; + } + return false; } } \ No newline at end of file http://cvs.php.net/viewvc.cgi/pearweb/public_html/election-new.php?r1=1.2&r2=1.3&diff_format=u Index: pearweb/public_html/election-new.php diff -u pearweb/public_html/election-new.php:1.2 pearweb/public_html/election-new.php:1.3 --- pearweb/public_html/election-new.php:1.2 Thu Nov 9 05:10:18 2006 +++ pearweb/public_html/election-new.php Fri Nov 10 03:46:27 2006 @@ -1,5 +1,6 @@ <?php auth_require('pear.election'); +$new = 'new'; $year = date('Y') + 1; $years = array($year--, $year); if (!isset($_POST['step'])) { http://cvs.php.net/viewvc.cgi/pearweb/templates/election-new-step1.tpl.php?r1=1.2&r2=1.3&diff_format=u Index: pearweb/templates/election-new-step1.tpl.php diff -u pearweb/templates/election-new-step1.tpl.php:1.2 pearweb/templates/election-new-step1.tpl.php:1.3 --- pearweb/templates/election-new-step1.tpl.php:1.2 Thu Nov 9 05:10:18 2006 +++ pearweb/templates/election-new-step1.tpl.php Fri Nov 10 03:46:28 2006 @@ -1,7 +1,7 @@ <?php -response_header('Election :: New'); +response_header('Election :: ' . ucfirst($new)); ?> -<h1>Create New Election [Step 1]</h1> +<h1><?php echo ucfirst($new) ?> Election [Step 1]</h1> <?php if ($error) { foreach ($error as $err) { @@ -9,8 +9,11 @@ } } ?> -<form name="newelection" action="/election-new.php" method="post"> +<form name="newelection" action="/election-<?php echo $new ?>.php" method="post"> <input type="hidden" name="step" value="2" /> +<?php if ($new == 'edit'): ?> +<input type="hidden" name="election_id" value="<?php echo $election_id ?>" /> +<?php endif; // if ($new == 'edit'): ?> <table> <tr> <th class="form-label_left" colspan="2">(All entries are required)</th> @@ -63,7 +66,10 @@ </td> </tr> <tr> - <th class="form-label_left">Election start date (must be at least 30 days from now)</th> + <th class="form-label_left">Election start date <?php if ($new == 'new'): ?> + (must be at least 30 days from now) + <?php endif; // if ($new == 'new') ?> + </th> <td class="form-input"> <select name="year"> <?php @@ -157,6 +163,6 @@ </td> </tr> </table> - <input type="submit" name="newelection" value="Create New Election" /> + <input type="submit" name="newelection" value="Save Election" /> </form> <?php response_footer(); \ No newline at end of file http://cvs.php.net/viewvc.cgi/pearweb/templates/election-new-step2.tpl.php?r1=1.2&r2=1.3&diff_format=u Index: pearweb/templates/election-new-step2.tpl.php diff -u pearweb/templates/election-new-step2.tpl.php:1.2 pearweb/templates/election-new-step2.tpl.php:1.3 --- pearweb/templates/election-new-step2.tpl.php:1.2 Thu Nov 9 05:10:18 2006 +++ pearweb/templates/election-new-step2.tpl.php Fri Nov 10 03:46:28 2006 @@ -1,7 +1,7 @@ <?php -response_header('Election :: New'); +response_header('Election :: ' . ucfirst($new)); ?> -<h1>Create New Election [Step 2]</h1> +<h1><?php echo ucfirst($new) ?> Election [Step 2]</h1> <?php if ($error) { foreach ($error as $err) { @@ -9,8 +9,11 @@ } } ?> -<form name="newelection" action="/election-new.php" method="post"> +<form name="newelection" action="/election-<?php echo $new ?>.php" method="post"> <input type="hidden" name="step" value="3" /> +<?php if ($new == 'edit'): ?> +<input type="hidden" name="election_id" value="<?php echo $election_id ?>" /> +<?php endif; // if ($new == 'edit'): ?> <input type="hidden" name="choices" value="<?php echo $info['choices'] ?>" /> <table> <?php for ($i = 1; $i <= $info['choices']; $i++): ?> @@ -90,6 +93,6 @@ </td> </tr> </table> - <input type="submit" name="newelection" value="Create New Election" /> + <input type="submit" name="newelection" value="Save Election" /> </form> <?php response_footer(); \ No newline at end of file http://cvs.php.net/viewvc.cgi/pearweb/templates/election-new-step3.tpl.php?r1=1.2&r2=1.3&diff_format=u Index: pearweb/templates/election-new-step3.tpl.php diff -u pearweb/templates/election-new-step3.tpl.php:1.2 pearweb/templates/election-new-step3.tpl.php:1.3 --- pearweb/templates/election-new-step3.tpl.php:1.2 Thu Nov 9 05:10:18 2006 +++ pearweb/templates/election-new-step3.tpl.php Fri Nov 10 03:46:28 2006 @@ -1,5 +1,5 @@ <?php -response_header('Election :: New'); +response_header('Election :: ' . ucfirst($new)); ?> <h1>Confirm Election Details [Step 3]</h1> <?php @@ -9,8 +9,11 @@ } } ?> -<form name="newelection" action="/election-new.php" method="post"> +<form name="newelection" action="/election-<?php echo $new ?>.php" method="post"> <input type="hidden" name="step" value="4" /> +<?php if ($new == 'edit'): ?> +<input type="hidden" name="election_id" value="<?php echo $election_id ?>" /> +<?php endif; // if ($new == 'edit'): ?> <input type="hidden" name="choices" value="<?php echo $info['choices'] ?>" /> <table> <?php for ($i = 1; $i <= $info['choices']; $i++): ?> @@ -83,6 +86,6 @@ </td> </tr> </table> - <input type="submit" name="newelection" value="Create New Election" /><input type="submit" name="cancel" value="Cancel" /> + <input type="submit" name="newelection" value="Save Election" /><input type="submit" name="cancel" value="Cancel" /> </form> <?php response_footer(); \ No newline at end of file http://cvs.php.net/viewvc.cgi/pearweb/templates/election-vote.tpl.php?r1=1.2&r2=1.3&diff_format=u Index: pearweb/templates/election-vote.tpl.php diff -u pearweb/templates/election-vote.tpl.php:1.2 pearweb/templates/election-vote.tpl.php:1.3 --- pearweb/templates/election-vote.tpl.php:1.2 Mon Nov 6 05:54:53 2006 +++ pearweb/templates/election-vote.tpl.php Fri Nov 10 03:46:28 2006 @@ -79,4 +79,5 @@ <input type="text" size="17" name="salt" /> <input type="submit" value="Retrieve Vote" /> </form> -<?php endif; // if (count($allelections)) \ No newline at end of file +<?php endif; // if (count($allelections)) +response_footer(); \ No newline at end of file http://cvs.php.net/viewvc.cgi/pearweb/public_html/election-edit.php?view=markup&rev=1.1 Index: pearweb/public_html/election-edit.php +++ pearweb/public_html/election-edit.php <?php /** * Interface for inputing/editing an election. * * This source file is subject to version 3.01 of the PHP license, * that is bundled with this package in the file LICENSE, and is * available through the world-wide-web at the following URI: * http://www.php.net/license/3_01.txt. * If you did not receive a copy of the PHP license and are unable to * obtain it through the world-wide-web, please send a note to * license@php.net so we can mail you a copy immediately. * * @category pearweb * @package pearweb_election * @author Gregory Beaver <cellog@php.net> * @copyright Copyright (c) 2006 The PHP Group * @license http://www.php.net/license/3_01.txt PHP License * @version $Id: election-edit.php,v 1.1 2006/11/10 03:46:27 cellog Exp $ */ auth_require('pear.election'); require 'pear-election.php'; $new = 'edit'; $year = date('Y'); $years = array($year + 1, $year--, $year--, $year--); $election = new PEAR_Election; if (isset($_GET['election'])) { $info = $election->getInfo($_GET['election']); if (!$info) { $error = 'Unknown election'; $elections = $election->listElections(); require dirname(dirname(__FILE__)) . '/templates/election-listforedit.tpl.php'; exit; } if (!$election->canEdit($_GET['election'])) { $error = 'Cannot edit that election'; $elections = $election->listElections(); require dirname(dirname(__FILE__)) . '/templates/election-listforedit.tpl.php'; exit; } $error = array(); $election_id = (int) $_GET['election']; require dirname(dirname(__FILE__)) . '/templates/election-new-step1.tpl.php'; exit; } if (isset($_POST['step'])) { if (!isset($_POST['election_id']) || !$election->electionExists($_POST['election_id'])) { $error = 'Unknown election'; $elections = $election->listElections(); require dirname(dirname(__FILE__)) . '/templates/election-listforedit.tpl.php'; exit; } $election_id = (int) $_POST['election_id']; if (!$election->canEdit($election_id)) { $error = 'Cannot edit that election'; $elections = $election->listElections(); require dirname(dirname(__FILE__)) . '/templates/election-listforedit.tpl.php'; exit; } switch ($_POST['step']) { case '2' : $error = $election->validateStep1(false); $info['purpose'] = $_POST['purpose']; $info['detail'] = $_POST['detail']; $info['choices'] = (int) $_POST['choices']; $info['year'] = (int) $_POST['year']; $info['month'] = (int) $_POST['month']; $info['day'] = (int) $_POST['day']; $info['length'] = (int) $_POST['length']; $info['minimum'] = (int) $_POST['minimum']; $info['maximum'] = (int) $_POST['maximum']; $info['eligiblevoters'] = (int) $_POST['eligiblevoters']; if ($error) { require dirname(dirname(__FILE__)) . '/templates/election-new-step1.tpl.php'; exit; } $info = $election->setupChoices($election_id, $info); require dirname(dirname(__FILE__)) . '/templates/election-new-step2.tpl.php'; exit; case '3' : $error = $election->validateStep1(false); $info['purpose'] = $_POST['purpose']; $info['detail'] = $_POST['detail']; $info['choices'] = (int) $_POST['choices']; $info['year'] = (int) $_POST['year']; $info['month'] = (int) $_POST['month']; $info['day'] = (int) $_POST['day']; $info['length'] = (int) $_POST['length']; $info['minimum'] = (int) $_POST['minimum']; $info['maximum'] = (int) $_POST['maximum']; $info['eligiblevoters'] = (int) $_POST['eligiblevoters']; if ($error) { require dirname(dirname(__FILE__)) . '/templates/election-new-step1.tpl.php'; exit; } $error = $election->validateStep2(); $info = $election->setupChoices($election_id, $info); if ($error) { require dirname(dirname(__FILE__)) . '/templates/election-new-step2.tpl.php'; exit; } require dirname(dirname(__FILE__)) . '/templates/election-new-step3.tpl.php'; exit; case '4' : if (isset($_POST['cancel'])) { $error = 'Cancelled edit'; break; } $error = $election->validateStep1(false); $info['purpose'] = $_POST['purpose']; $info['detail'] = $_POST['detail']; $info['choices'] = (int) $_POST['choices']; $info['year'] = (int) $_POST['year']; $info['month'] = (int) $_POST['month']; $info['day'] = (int) $_POST['day']; $info['length'] = (int) $_POST['length']; $info['minimum'] = (int) $_POST['minimum']; $info['maximum'] = (int) $_POST['maximum']; $info['eligiblevoters'] = (int) $_POST['eligiblevoters']; if ($error) { require dirname(dirname(__FILE__)) . '/templates/election-new-step1.tpl.php'; exit; } $error = $election->validateStep2(); for ($i = 1; $i <= $info['choices']; $i++) { $info['summary' . $i] = empty($_POST['summary' . $i]) ? '' : $_POST['summary' . $i]; $info['summary_link' . $i] = empty($_POST['summary_link' . $i]) ? '' : $_POST['summary_link' . $i]; } if ($error) { require dirname(dirname(__FILE__)) . '/templates/election-new-step2.tpl.php'; exit; } // safe to save $election->saveEditedElection($election_id); $error = 'Election saved'; } } $elections = $election->listElections(); require dirname(dirname(__FILE__)) . '/templates/election-listforedit.tpl.php'; http://cvs.php.net/viewvc.cgi/pearweb/templates/election-listforedit.tpl.php?view=markup&rev=1.1 Index: pearweb/templates/election-listforedit.tpl.php +++ pearweb/templates/election-listforedit.tpl.php <?php response_header('Edit Elections'); ?> <h1>Edit PEAR Elections</h1> <?php if (isset($error)): ?> <div class="errors"><?php echo $error; ?></div> <?php endif; // if (isset($error)): ?> Current date is <strong><?php echo date('Y-m-d'); ?></strong> <p> If you are a developer with pear.admin karma, you can edit any election except for active elections. Otherwise you can only edit elections that you have created. </p> <?php if (!count($elections)): ?> <h2>No Editable Elections for your user/karma level</h2> <?php else: // if (!count($elections)): ?> <h2>Editable Elections:</h2> <table> <tr> <th class="form-label_top">Election Issue</th> <th class="form-label_top">Election dates</th> <th class="form-label_top">Creator</th> </tr> <?php foreach ($elections as $election): $class = $election['active'] == 'yes' ? 'vote-active' : 'vote-inactive'; ?> <tr> <td class="<?php echo $class; ?>"><a href="/election-edit.php?election=<?php echo $election['id']; ?>"><?php echo htmlspecialchars($election['purpose']); ?></a></td> <td class="<?php echo $class; ?>"><?php echo $election['votestart'] . ' until ' . $election['voteend']; ?></td> <td class="<?php echo $class; ?>"><?php echo $election['creator']; ?></td> </tr> <?php endforeach; // foreach ($elections as $election): ?> </table> <?php endif; // if (count($elections)) response_footer();
« previous php.pear.core (#5163) next »