summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCallan Barrett <wizzomafizzo@gmail.com>2008-10-04 20:54:13 +0200
committerLoui Chang <louipc.ist@gmail.com>2008-10-30 15:45:31 +0100
commit132fd7cfc791a76f084140b2901018a046160b84 (patch)
treeacf444ea3966793c426cfdfa449621408bc29e05
parent5d4303d0b6afacb2b3a8731bdb9fdbd6c40f5c0e (diff)
downloadaur-132fd7cfc791a76f084140b2901018a046160b84.tar.gz
aur-132fd7cfc791a76f084140b2901018a046160b84.tar.xz
Convert package voting to a function
Signed-off-by: Callan Barrett <wizzomafizzo@gmail.com> Signed-off-by: Loui Chang <louipc.ist@gmail.com>
-rw-r--r--web/html/packages.php112
-rw-r--r--web/lib/pkgfuncs.inc77
2 files changed, 83 insertions, 106 deletions
diff --git a/web/html/packages.php b/web/html/packages.php
index 5935ebd3..91b0e6e0 100644
--- a/web/html/packages.php
+++ b/web/html/packages.php
@@ -61,113 +61,13 @@ if ($_POST['action'] == "do_Flag" || isset($_POST['do_Flag'])) {
print pkg_adopt($atype, $ids, True);
print "</p>";
} elseif ($_POST['action'] == "do_Vote" || isset($_POST['do_Vote'])) {
- if (!$atype) {
- print __("You must be logged in before you can vote for packages.");
- print "<br />\n";
-
- } else {
- # vote on the packages in $ids array.
- #
- if (!empty($ids)) {
- $dbh = db_connect();
- $my_votes = pkgvotes_from_sid($_COOKIE["AURSID"]);
- $uid = uid_from_sid($_COOKIE["AURSID"]);
- # $vote_ids will contain the string of Package.IDs that
- # the visitor hasn't voted for already
- #
- $first = 1;
- while (list($pid, $v) = each($ids)) {
- if (!isset($my_votes[$pid])) {
- # cast a vote for this package
- #
- if ($first) {
- $first = 0;
- $vote_ids = $pid;
- $vote_clauses = "(".$uid.", ".$pid.")";
- } else {
- $vote_ids .= ", ".$pid;
- $vote_clauses .= ", (".$uid.", ".$pid.")";
- }
- }
- }
- # only vote for packages the user hasn't already voted for
- #
- $q = "UPDATE Packages SET NumVotes = NumVotes + 1 ";
- $q.= "WHERE ID IN (".$vote_ids.")";
- db_query($q, $dbh);
-
- $q = "INSERT INTO PackageVotes (UsersID, PackageID) VALUES ";
- $q.= $vote_clauses;
- db_query($q, $dbh);
-
- # Update the LastVoted field for this user
- #
- $q = "UPDATE Users SET LastVoted = UNIX_TIMESTAMP() ";
- $q.= "WHERE ID = ".$uid;
- db_query($q, $dbh);
-
- print "<p>\n";
- print __("Your votes have been cast for the selected packages.");
- print "</p>\n";
-
- } else {
- print "<p>\n";
- print __("You did not select any packages to vote for.");
- print "</p>\n";
- }
- }
-
-
+ print "<p>";
+ print pkg_vote($atype, $ids, True);
+ print "</p>";
} elseif ($_POST['action'] == "do_UnVote" || isset($_POST['do_UnVote'])) {
- if (!$atype) {
- print __("You must be logged in before you can un-vote for packages.");
- print "<br />\n";
-
- } else {
- # un-vote on the packages in $ids array.
- #
- if (!empty($ids)) {
- $dbh = db_connect();
- $my_votes = pkgvotes_from_sid($_COOKIE["AURSID"]);
- $uid = uid_from_sid($_COOKIE["AURSID"]);
- # $unvote_ids will contain the string of Package.IDs that
- # the visitor has voted for and wants to unvote.
- #
- $first = 1;
- while (list($pid, $v) = each($ids)) {
- if (isset($my_votes[$pid])) {
- # cast a un-vote for this package
- #
- if ($first) {
- $first = 0;
- $unvote_ids = $pid;
- } else {
- $unvote_ids .= ", ".$pid;
- }
- }
- }
- # only un-vote for packages the user has already voted for
- #
- $q = "UPDATE Packages SET NumVotes = NumVotes - 1 ";
- $q.= "WHERE ID IN (".$unvote_ids.")";
- db_query($q, $dbh);
-
- $q = "DELETE FROM PackageVotes WHERE UsersID = ".$uid." ";
- $q.= "AND PackageID IN (".$unvote_ids.")";
- db_query($q, $dbh);
-
- print "<p>\n";
- print __("Your votes have been removed from the selected packages.");
- print "</p>\n";
-
- } else {
- print "<p>\n";
- print __("You did not select any packages to un-vote for.");
- print "</p>\n";
- }
- }
-
-
+ print "<p>";
+ print pkg_vote($atype, $ids, False);
+ print "</p>";
} elseif (isset($_GET["ID"])) {
if (!intval($_GET["ID"])) {
diff --git a/web/lib/pkgfuncs.inc b/web/lib/pkgfuncs.inc
index 415f3e71..7fe3f317 100644
--- a/web/lib/pkgfuncs.inc
+++ b/web/lib/pkgfuncs.inc
@@ -1189,3 +1189,80 @@ function pkg_adopt ($atype, $ids, $action = True) {
return __("The selected packages have been disowned.");
}
}
+
+function pkg_vote ($atype, $ids, $action = True) {
+ if (!$atype) {
+ if ($action) {
+ return __("You must be logged in before you can vote for packages.");
+ } else {
+ return __("You must be logged in before you can un-vote for packages.");
+ }
+ }
+
+ if (empty($ids)) {
+ if ($action) {
+ return __("You did not select any packages to vote for.");
+ } else {
+ return __("Your votes have been removed from the selected packages.");
+ }
+ }
+
+ $dbh = db_connect();
+ $my_votes = pkgvotes_from_sid($_COOKIE["AURSID"]);
+ $uid = uid_from_sid($_COOKIE["AURSID"]);
+
+ $first = 1;
+ foreach ($ids as $pid => $v) {
+ if ($action) {
+ $check = !isset($my_votes[$pid]);
+ } else {
+ $check = isset($my_votes[$pid]);
+ }
+
+ if ($check) {
+ if ($first) {
+ $first = 0;
+ $vote_ids = $pid;
+ if ($action) {
+ $vote_clauses = "($uid, $pid)";
+ }
+ } else {
+ $vote_ids .= ", $pid";
+ if ($action) {
+ $vote_clauses .= ", ($uid, $pid)";
+ }
+ }
+ }
+ }
+
+ # only vote for packages the user hasn't already voted for
+ #
+ $op = $action ? "+" : "-";
+ $q = "UPDATE Packages SET NumVotes = NumVotes $op 1 ";
+ $q.= "WHERE ID IN ($vote_ids)";
+
+ db_query($q, $dbh);
+
+ if ($action) {
+ $q = "INSERT INTO PackageVotes (UsersID, PackageID) VALUES ";
+ $q.= $vote_clauses;
+ } else {
+ $q = "DELETE FROM PackageVotes WHERE UsersID = $uid ";
+ $q.= "AND PackageID IN ($vote_ids)";
+ }
+
+ db_query($q, $dbh);
+
+ if ($action) {
+ $q = "UPDATE Users SET LastVoted = UNIX_TIMESTAMP() ";
+ $q.= "WHERE ID = $uid";
+
+ db_query($q, $dbh);
+ }
+
+ if ($action) {
+ return __("Your votes have been cast for the selected packages.");
+ } else {
+ return __("Your votes have been removed from the selected packages.");
+ }
+}