Skip to content

Commit

Permalink
fix votes
Browse files Browse the repository at this point in the history
  • Loading branch information
danwdart committed Jan 21, 2018
1 parent 06a41e5 commit 6fda9b7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/php/Chaplin/Controller/Dispatcher/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getActionMethod(Request $request)
$formatted = $this->_formatName($action, true);

if ("Error" == $formatted) {
$strVerb = "all";
return "allError";
}

return strtolower($strVerb) . $formatted . ($bIsXHR ? "_API" : "");
Expand Down
16 changes: 14 additions & 2 deletions src/php/Chaplin/Module/Api/Controller/VideoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ public function getDownload()
echo file_get_contents($strPath);
}

public function getVote()
public function getVote_API()
{
$strVideoId = $this->_request->getParam('id', null);

Expand All @@ -455,7 +455,6 @@ public function getVote()
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);


$strVote = $this->_request->getParam('vote', null);
if(is_null($strVideoId)) {
$this->_redirect('/');
Expand All @@ -467,6 +466,19 @@ public function getVote()
} elseif('down' == $strVote) {
Gateway::getVote()->addVote($modelUser, $modelVideo, 0);
}

// Refresh my video
$modelVideo = Gateway::getInstance()
->getVideo()
->getByVideoId($strVideoId, $modelUser);

// hack for no view
echo json_encode(
[
"ups" => $modelVideo->getVotesUp(),
"downs" => $modelVideo->getVotesDown()
]
);
}

public function getUpload()
Expand Down
13 changes: 6 additions & 7 deletions src/php/Chaplin/Module/Api/views/scripts/video/watch.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
<br/>Licence: <a href="<?php echo $this->video->getLicence()->getURL(); ?>"><?php echo $this->video->getLicence()->getText(); ?></a>
<br/><?php echo $this->video->getDescription(); ?>
<p>Short URL: <input type="text" disabled value="<?php echo $this->short; ?>"/></p>
<p><strong>&uparrow; <?php echo $this->video->getVotesUp(); ?> / <?php echo $this->video->getVotesDown(); ?> &downarrow;</strong></p>
<?php if ($this->video->getPrivacy()->isPublic()): ?>
<div class="row bg-light">
<div class="col-lg-2 col-md-2 col-sm-4 col-12">
Expand Down Expand Up @@ -109,18 +108,18 @@
<br/><br/>
<div class="btn-group">
<?php if(\Chaplin\Auth::getInstance()->hasIdentity()): ?>
<a class="atotext btn btn-secondary<?php echo (1 === $this->video->getYourVote()) ? " active" : ""; ?>" href="/video/vote/id/<?php echo $this->video->getVideoId();?>/vote/up">
<i class="fa fa-arrow-up"></i>
<a class="vote btn btn-secondary<?php echo (1 === $this->video->getYourVote()) ? " active" : ""; ?>" href="/video/vote/id/<?php echo $this->video->getVideoId();?>/vote/up">
<span class="ups"><?php echo $this->video->getVotesUp(); ?></span>&nbsp;&nbsp;<i class="fa fa-arrow-up"></i>
</a>
<a class="atotext btn btn-secondary<?php echo (0 === $this->video->getYourVote()) ? " active" : ""; ?>" href="/video/vote/id/<?php echo $this->video->getVideoId();?>/vote/down">
<i class="fa fa-arrow-down"></i>
<a class="vote btn btn-secondary<?php echo (0 === $this->video->getYourVote()) ? " active" : ""; ?>" href="/video/vote/id/<?php echo $this->video->getVideoId();?>/vote/down">
<span class="downs"><?php echo $this->video->getVotesDown(); ?></span>&nbsp;&nbsp;<i class="fa fa-arrow-down"></i>
</a>
<?php endif; ?>
</div>
<br/><br/>
<?php if(\Chaplin\Auth::getInstance()->hasIdentity()): echo $this->formComment; else: echo 'Login above to comment.'; endif; ?>
<br/>
<div id="comments" rel="/video/comments/id/<?php echo $this->video->getVideoId();?>">
<div id="comments" data-refresh-from="/video/comments/id/<?php echo $this->video->getVideoId();?>">
<?php foreach($this->ittComments as $modelComment):?>
<p>
<i>
Expand All @@ -129,7 +128,7 @@
<?php echo $modelComment->getComment();?>
<?php if ($modelComment->isMine()): ?>
<a class="ajax" href="/video/deletecomment/id/<?php echo $modelComment->getCommentId(); ?>"
rel="comments">
data-results-in="comments">
(X)
</a>
<?php endif;?>
Expand Down
91 changes: 45 additions & 46 deletions src/php/public/js/src/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* @link https://github.com/danwdart/projectchaplin
**/
import $ from 'jquery';
import k from './k';

$(document).ready(() => {
$('.effect').change(
Expand Down Expand Up @@ -55,54 +54,62 @@ $(document).ready(() => {

}
);
$('a.atotext').click(
function(e) {
e.preventDefault();
el = $(this);
$.ajax(
{
url: $(this).attr('href'),
method: 'GET',
success: function() {
el.after(el.html());
el.hide();
}
}
);
console.log('tried');
return false;
}
);
// <a class="ajax" rel="ready" href="http://clickedurl"></a>
// <div id="ready" rel="http://refreshfrom">Result</div>
$('a.ajax').click(
function(e) {
e.preventDefault();
el = $(this);
const el = $(this);
$.ajax(
{
url: el.attr('href'),
method: 'GET',
success: function() {
rel = el.attr('rel');
if (null != rel) {
elRel = $('#'+rel);
elRelUrl = elRel.attr('rel');
if (null != elRelUrl) {
$.ajax(
{
url: elRelUrl,
method: 'GET',
success: function(data) {
elRel.html(data);
}
}
);
}
const rel = el.attr('data-result-in');
if (null === rel) {
return;
}
const elRel = $('#'+rel),
elRelUrl = elRel.attr('data-refresh-from');
if (null === elRelUrl) {
return;
}
$.ajax(
{
url: elRelUrl,
method: 'GET',
success: function(data) {
elRel.html(data);
}
}
);
}
}
)
}
)
$('.vote').click(function(e) {
e.preventDefault();
const $ups = $(".vote .ups"),
$downs = $(".vote .downs"),
url = $(this).attr("href");
console.log("Sending vote");
$.ajax(
{
url,
type: "GET",
dataType: "json",
success: function(data) {
$ups.html(data.ups);
$downs.html(data.downs);
},
error: function(err) {
console.error(err);
}
}
)
})

$('form.ajax input[type="submit"]').click(
function(e) {
e.preventDefault();
Expand All @@ -116,8 +123,8 @@ $(document).ready(() => {
parent.append('<span class="success">Comment posted... </span>');
parent.children('.success').fadeOut(3000, function() {$(this).remove();});
},
failure: function() {
parent.append('<span class="failure">Sorry, we couldn\'t post your comment.</span>');
error: function() {
parent.append('<span class="error">Sorry, we couldn\'t post your comment.</span>');
}
}
);
Expand All @@ -133,7 +140,7 @@ $(document).ready(() => {
success: function(data) {
el.html(data);
},
failure: function() {
error: function() {
console.log('failed to update');
}
}
Expand All @@ -143,12 +150,4 @@ $(document).ready(() => {
return false;
}
);


// nothing important :P
let kk = new k();
kk.code = function() {
$('#fun').show();
};
kk.load();
});

0 comments on commit 6fda9b7

Please sign in to comment.