]> gitweb.fluxo.info Git - semanticscuttle.git/commitdiff
basic ajax voting support. has a bad counting bug somewhere
authorcweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Wed, 28 Oct 2009 08:02:17 +0000 (08:02 +0000)
committercweiske <cweiske@b3834d28-1941-0410-a4f8-b48e95affb8f>
Wed, 28 Oct 2009 08:02:17 +0000 (08:02 +0000)
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@445 b3834d28-1941-0410-a4f8-b48e95affb8f

data/templates/bookmarks-vote.inc.tpl.php
src/SemanticScuttle/Service/Bookmark.php
www/ajaxVote.php [new file with mode: 0644]
www/jsScuttle.php
www/vote.php

index 5979827e4bbe0057c980900d8754e59e4a84613a..d59feb44265285877ecd64fde5030f22dd1bcba3 100644 (file)
@@ -13,11 +13,13 @@ if (isset($row['hasVoted']) && !$row['hasVoted']) {
 } else {
     $classes = 'vote-badge';
 }
-echo '<span class="' . $classes . '">';
+echo '<span class="' . $classes . '" id="bmv-' . $row['bId'] . '">';
 
 if (isset($row['hasVoted']) && !$row['hasVoted']) {
     echo '<a class="vote-for" rel="nofollow" href="'
-        . createVoteURL(true, $row['bId']) . '">+</a>';
+        . createVoteURL(true, $row['bId']) . '"'
+        . ' onclick="javascript:vote(' . $row['bId'] . ',1); return false;"'
+        . '>+</a>';
 } else {
     echo '<span class="vote-for-inactive">+</span>';
 }
@@ -26,7 +28,9 @@ echo '<span class="voting">' . $row['bVoting'] . '</span>';
 
 if (isset($row['hasVoted']) && !$row['hasVoted']) {
     echo '<a class="vote-against" rel="nofollow" href="'
-        . createVoteURL(false, $row['bId']) . '">-</a>';
+        . createVoteURL(false, $row['bId']) . '"'
+        . ' onclick="vote(' . $row['bId'] . ',-1); return false;"'
+        . '>-</a>';
 } else {
     echo '<span class="vote-against-inactive">-</span>';
 }
index 3a7edd9f55523a6d0a9522c2c49797c62e9e0011..1054360c79100eda4edc4c8463be0b2eb05bf135 100644 (file)
@@ -80,19 +80,51 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService
 
 
 
+    /**
+     * Load a single bookmark and return it
+     *
+     * @param integer $bid          Bookmark ID
+     * @param boolean $include_tags If tags shall be loaded
+     *
+     * @return mixed Array with bookmark data or false in case
+     *               of an error.
+     */
     function getBookmark($bid, $include_tags = false)
     {
-        if (!is_numeric($bid))
-        return;
+        if (!is_numeric($bid)) {
+            return false;
+        }
 
-        $sql = 'SELECT * FROM '. $this->getTableName() .' WHERE bId = '. $this->db->sql_escape($bid);
+        $userservice = SemanticScuttle_Service_Factory::get('User');
 
-        if (!($dbresult = & $this->db->sql_query($sql)))
-        message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db);
+        $query_1 = '*';
+        $query_2 = $this->getTableName() . ' as B';
+
+        //Voting system
+        //needs to be directly after FROM bookmarks
+        if ($GLOBALS['enableVoting'] && $userservice->isLoggedOn()) {
+            $currentuser = $userservice->getCurrentUser();
+            $vs = SemanticScuttle_Service_Factory::get('Vote');
+            $query_1 .= ', !ISNULL(V.bId) as hasVoted, V.vote as vote';
+            $query_2 .= ' LEFT JOIN ' . $vs->getTableName() . ' AS V'
+                . ' ON B.bId = V.bId'
+                . ' AND V.uId = ' . (int)$currentuser['uId'];
+        }
+
+        $sql = 'SELECT ' . $query_1 . ' FROM '
+            . $query_2
+            .' WHERE B.bId = '. $this->db->sql_escape($bid);
+
+        if (!($dbresult = & $this->db->sql_query($sql))) {
+            message_die(
+                GENERAL_ERROR, 'Could not get bookmark',
+                '', __LINE__, __FILE__, $sql, $this->db
+            );
+        }
 
-        if ($row = $this->db->sql_fetchrow($dbresult)) {
+        if ($row = $this->db->sql_fetchrow($dbresult)) {
             if ($include_tags) {
-                $b2tservice = SemanticScuttle_Service_Factory :: get('Bookmark2Tag');
+                $b2tservice = SemanticScuttle_Service_Factory::get('Bookmark2Tag');
                 $row['tags'] = $b2tservice->getTagsForBookmark($bid);
             }
             $output = $row;
diff --git a/www/ajaxVote.php b/www/ajaxVote.php
new file mode 100644 (file)
index 0000000..27eb62c
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+/**
+ * We re-use vote.php but set the ajax flag
+ */
+$GLOBALS['ajaxRequest'] = true;
+require 'vote.php';
+
+$bs = SemanticScuttle_Service_Factory::get('Bookmark');
+$ts = SemanticScuttle_Service_Factory::get('Template');
+$bmrow = $bs->getBookmark($bookmark);
+
+header('Content-Type: text/xml; charset=utf-8');
+echo '<voteresult><bookmark>' . $bookmark . '</bookmark>'
+    . '<html xmlns="http://www.w3.org/1999/xhtml">';
+$ts->loadTemplate(
+    'bookmarks-vote.inc.tpl.php',
+    array('row' => $bmrow)
+);
+
+echo '</html></voteresult>';
+?>
\ No newline at end of file
index 6aba4229e5a8e8074d6a0552fa8e35c4cd94370c..6807e4a2074c7af348d85c8a2a82676a8b55c8cd 100644 (file)
@@ -42,7 +42,7 @@ function deleteConfirmed(ele, input, response) {
         post.style.display = 'none';
         deleted = false;
     } else {
-        loadXMLDoc('<?php echo ROOT; ?>ajaxDelete.php?id=' + input);        
+        loadXMLDocProc('<?php echo ROOT; ?>ajaxDelete.php?id=' + input);        
         post.style.display = 'none';        
     }
 }
@@ -97,7 +97,7 @@ function getTitle(input, response){
             title.style.backgroundImage = 'none';
             title.value = response;
         } else if (input.indexOf('http') > -1) {
-            loadXMLDoc('<?php echo ROOT; ?>ajaxGetTitle.php?url=' + input);
+            loadXMLDocProc('<?php echo ROOT; ?>ajaxGetTitle.php?url=' + input);
         } else {
             return false;
         }
@@ -105,11 +105,25 @@ function getTitle(input, response){
 }
 
 var xmlhttp;
-function loadXMLDoc(url) {
+function loadXMLDocProc(url) {
+    loadXMLDoc(url, processStateChange);
+}
+function vote(bookmark, vote) {
+    if (vote == 1) {
+        vote = 'for';
+    } else {
+        vote = 'against';
+    }
+    loadXMLDoc(
+        '<?php echo ROOT; ?>ajaxVote.php/' + vote + '/' + bookmark,
+        processVotingResult
+    );
+}
+function loadXMLDoc(url, callback) {
     // Native
     if (window.XMLHttpRequest) {
         xmlhttp = new XMLHttpRequest();
-        xmlhttp.onreadystatechange = processStateChange;
+        xmlhttp.onreadystatechange = callback;
         xmlhttp.open("GET", url, true);
         xmlhttp.send(null);
     // ActiveX
@@ -131,6 +145,20 @@ function processStateChange() {
         eval(method + '(\'\', result)');
     }
 }
+function processVotingResult() {
+    if (xmlhttp.readyState != 4 || xmlhttp.status != 200) {
+        return;
+    }
+    var response = xmlhttp.responseXML.documentElement;
+    var bookmark = response.getElementsByTagName('bookmark')[0]
+        .firstChild.nodeValue;
+    var bmnode = document.getElementById('bmv-'+bookmark);
+
+    bmnode.parentNode.replaceChild(
+        response.getElementsByTagName('html')[0].firstChild,
+        bmnode
+    );
+}
 
 function playerLoad() {
     var anchors = document.getElementsByTagName('a');
index 6dda31b5dd04523c93c763d337e941e0c4e512d9..406b7d04c5cb54647c6d608a9935c2c179e5c5dc 100644 (file)
@@ -65,5 +65,11 @@ if ($vs->hasVoted($bookmark, $user)) {
 }
 
 $vs->vote($bookmark, $user, $type == 'for' ? 1 : -1);
+
+if (isset($GLOBALS['ajaxRequest']) && $GLOBALS['ajaxRequest']) {
+    //we are in ajax mode and return the badge in ajaxVote.php
+    return;
+}
+
 header('Location: ' . $from);
 ?>
\ No newline at end of file