From a739be41764c66ff428df32fa6439eaa172d3502 Mon Sep 17 00:00:00 2001 From: CaMer0n Date: Sat, 4 Aug 2012 21:50:47 +0000 Subject: [PATCH] jquery pagination - currently used on comments and media-manager. --- comment.php | 10 ++ e107_admin/image.php | 2 +- e107_files/jslib/core/all.jquery.js | 118 +++++++++++++-- e107_files/jslib/core/mediaManager.js | 67 +++----- e107_handlers/comment_class.php | 210 +++++++++++++++----------- e107_handlers/media_class.php | 2 +- 6 files changed, 263 insertions(+), 146 deletions(-) diff --git a/comment.php b/comment.php index ce9395e20..ac62acca9 100644 --- a/comment.php +++ b/comment.php @@ -43,6 +43,16 @@ if(e_AJAX_REQUEST) // TODO improve security $ret = array(); + // Comment Pagination + if(varset($_GET['mode']) == 'list' && vartrue($_GET['id']) && vartrue($_GET['type'])) + { + $clean_type = preg_replace("/[^\w\d]/","",$_GET['type']); + + $tmp = e107::getComment()->getComments($clean_type,intval($_GET['id']),intval($_GET['from']),$att); + echo $tmp['comments']; + exit; + } + if(varset($_GET['mode']) == 'reply' && vartrue($_POST['itemid'])) diff --git a/e107_admin/image.php b/e107_admin/image.php index 071e0a11c..0fb8011eb 100644 --- a/e107_admin/image.php +++ b/e107_admin/image.php @@ -27,7 +27,7 @@ if (!getperms("A") && ($_GET['action'] != 'dialog')) e107::js('core', 'plupload/plupload.full.js', 'jquery', 2); e107::css('core', 'plupload/jquery.plupload.queue/css/jquery.plupload.queue.css', 'jquery'); e107::js('core', 'plupload/jquery.plupload.queue/jquery.plupload.queue.js', 'jquery', 2); -e107::js("core", "core/mediaManager.js","jquery",3); +e107::js("core", "core/mediaManager.js","jquery",5); /* * CLOSE - GO TO MAIN SCREEN diff --git a/e107_files/jslib/core/all.jquery.js b/e107_files/jslib/core/all.jquery.js index 1beb450c5..ae36edc0c 100644 --- a/e107_files/jslib/core/all.jquery.js +++ b/e107_files/jslib/core/all.jquery.js @@ -398,17 +398,68 @@ $(document).ready(function() // Text-area AutoGrow // $("textarea.e-autoheight").elastic(); + + // ajax next/prev mechanism - updates url from value. + $(".e-nav").click(function(){ // should be run before ajax. + /* + var src = $(this).attr("data-src"); + var inc = parseInt($(this).attr("data-nav-inc")); + var dir = $(this).attr("data-nav-dir"); + var tot = parseInt($(this).attr("data-nav-total")); + var val = src.match(/from=(\d+)/); + var amt = parseInt(val[1]); + + var oldVal = 'from='+ amt; + + var sub = amt - inc; + var add = amt + inc; + + $(this).show(); + + if(add > tot) + { + add = amt; + } + + if(sub < 0) + { + sub = 0 + } + + if(dir == 'down') + { + var newVal = 'from='+ sub; + } + else + { + var newVal = 'from='+ add; + } + + alert('nav'); + src = src.replace(oldVal, newVal); + $(".e-nav").attr("data-src",src); + */ + }); - + + $(".e-ajax").click(function(){ - var id = $(this).attr("href"); - var src = $(this).attr("data-src"); - var target = $(this).attr("data-target"); // support for input buttons etc. - var loading = $(this).attr('data-loading'); // image to show loading. + var id = $(this).attr("href"); + + var target = $(this).attr("data-target"); // support for input buttons etc. + var loading = $(this).attr('data-loading'); // image to show loading. + var nav = $(this).attr('data-nav-inc'); + + if(nav !== null) + { + eNav(this,'.e-ajax'); //modify data-src value for next/prev. 'from=' + } + + var src = $(this).attr("data-src"); if(target != null) { @@ -427,7 +478,7 @@ $(document).ready(function() src = tmp[0]; } // var effect = $(this).attr("data-effect"); - // alert(src); + // alert(id); $(id).load(src,function() { // alert(src); @@ -435,9 +486,14 @@ $(document).ready(function() // $(this).fadeIn(); }); + return false; }); + + + + // Does the same as externalLinks(); @@ -454,10 +510,54 @@ $(document).ready(function() }) + + /** + * dynamic next/prev + * @param e object (eg. from selector) + * @param navid - class with data-src that needs 'from=' value updated. (often 2 of them eg. next/prev) + */ + function eNav(e,navid) + { + var src = $(e).attr("data-src"); + var inc = parseInt($(e).attr("data-nav-inc")); + var dir = $(e).attr("data-nav-dir"); + var tot = parseInt($(e).attr("data-nav-total")); + var val = src.match(/from=(\d+)/); + var amt = parseInt(val[1]); + + var oldVal = 'from='+ amt; + + var sub = amt - inc; + var add = amt + inc; + + $(e).show(); + + if(add > tot) + { + add = amt; + // $(e).hide(); + } + + if(sub < 0) + { + sub = 0 + } + + if(dir == 'down') + { + var newVal = 'from='+ sub; + } + else + { + var newVal = 'from='+ add; + } + + src = src.replace(oldVal, newVal); + $(navid).attr("data-src",src); + + } + // Legacy Stuff to be converted. - - - // BC Expandit() function var nowLocal = new Date(); /* time at very beginning of js execution */ diff --git a/e107_files/jslib/core/mediaManager.js b/e107_files/jslib/core/mediaManager.js index 870603735..f4a3ca4d4 100644 --- a/e107_files/jslib/core/mediaManager.js +++ b/e107_files/jslib/core/mediaManager.js @@ -170,52 +170,13 @@ $(document).ready(function() }); - $(".e-nav").click(function(){ // should be run before ajax. - - var src = $(this).attr("data-src"); - var inc = parseInt($(this).attr("data-nav-inc")); - var dir = $(this).attr("data-nav-dir"); - var tot = parseInt($(this).attr("data-nav-total")); - var val = src.match(/from=(\d+)/); - var amt = parseInt(val[1]); - - var oldVal = 'from='+ amt; - - var sub = amt - inc; - var add = amt + inc; - - $(this).show(); - - if(add > tot) - { - add = amt; - } - - if(sub < 0) - { - sub = 0 - } - - if(dir == 'down') - { - var newVal = 'from='+ sub; - } - else - { - var newVal = 'from='+ add; - } - - - src = src.replace(oldVal, newVal); - $(".e-nav").attr("data-src",src); - - }); + $(".e-media-nav").click(function(){ - return mediaNav(this); + return mediaNav(this,'.e-media-nav'); /* var id = $(this).attr("href"); var src = $(this).attr("data-src"); @@ -257,19 +218,27 @@ $(document).ready(function() }); $("#media-search").keyup(function(){ - mediaNav(this); + mediaNav(this,null); }); - function mediaNav(e) + function mediaNav(e,navid) { - var id = $(e).attr("href"); - var src = $(e).attr("data-src"); - var target = $(e).attr("data-target"); // support for input buttons etc. - var loading = $(e).attr('data-loading'); // image to show loading. - var search = $('#media-search').val(); // image to show loading. - + var id = $(e).attr("href"); + + var target = $(e).attr("data-target"); // support for input buttons etc. + var loading = $(e).attr('data-loading'); // image to show loading. + var search = $('#media-search').val(); // image to show loading. + var nav = $(e).attr('data-nav-inc'); + + if(nav !== null && navid !==null) + { + eNav(e,navid); + } + + var src = $(e).attr("data-src"); + if(target != null) { id = '#' + target; diff --git a/e107_handlers/comment_class.php b/e107_handlers/comment_class.php index 57bbc985a..44adf53e1 100644 --- a/e107_handlers/comment_class.php +++ b/e107_handlers/comment_class.php @@ -46,16 +46,22 @@ class comment private $totalComments = 0; private $moderator = false; + + private $commentsPerPage = 5; + + private $table = null; function __construct() { - if(getperms('0')) // moderator perms. + if(getperms('B')) // moderator perms. { $this->moderator = true; } - + //TODO - add a pref for comments per page. + // $this->commentsPerPage = pref; + global $COMMENTSTYLE; if (!$COMMENTSTYLE) @@ -92,7 +98,7 @@ class comment $COMMENT_TEMPLATE['ITEM_START'] = ""; $COMMENT_TEMPLATE['ITEM'] = $COMMENTSTYLE; $COMMENT_TEMPLATE['ITEM_END'] = ""; - $COMMENT_TEMPLATE['LAYOUT'] = "{COMMENTS}{COMMENTFORM}{MODERATE}"; + $COMMENT_TEMPLATE['LAYOUT'] = "{COMMENTS}{COMMENTFORM}{MODERATE}{COMMENTNAV}"; $COMMENT_TEMPLATE['FORM'] = " {SUBJECT_INPUT} {AUTHOR_INPUT} @@ -265,8 +271,8 @@ class comment 'comval' => strip_tags(trim($comval)), 'itemid' => $itemid, 'pid' => $pid, - 'eaction' => $eaction, - 'rate' => $rate + 'eaction' => varset($eaction), + 'rate' => $rating ); e107::getScBatch('comment')->setParserVars($data); @@ -311,8 +317,12 @@ class comment } } - - function isPending($row) + /** + * Check if comment is pending approval. + * @param array - a row from the comments table. + * @return boolean True/False + */ + private function isPending($row) { if($row['comment_blocked'] > 0 && ($row['comment_author_id'] != USERID || ($row['comment_author_id']==0 && $row['comment_author_name'] != $_SESSION['comment_author_name'])) && $this->moderator == false) { @@ -327,19 +337,17 @@ class comment /** - * Enter description here... + * Render a single comment and any nested comments it may have. * - * @param unknown_type $row - * @param unknown_type $table - * @param unknown_type $action - * @param unknown_type $id - * @param unknown_type $width - * @param unknown_type $subject - * @param unknown_type $addrating - * @return unknown + * @param array $row + * @param string $table + * @param string $action + * @param integer $id + * @param interger $width + * @param string $subject + * @param integer $addrating + * @return html */ - - function render_comment($row, $table, $action, $id, $width, $subject, $addrating = FALSE) { //addrating : boolean, to show rating system in rendered comment @@ -388,7 +396,8 @@ class comment $row['rating_enabled'] = true; // Toggles rating shortcode. //TODO add pref e107::getScBatch('comment')->setParserVars($row); - $COMMENT_TEMPLATE = $this->template; + + $COMMENT_TEMPLATE = $this->template; $COMMENT_TEMPLATE['ITEM_START'] = "\n\n
\n"; $COMMENT_TEMPLATE['ITEM_END'] = "\n
\n"; @@ -887,6 +896,8 @@ class comment //rate : boolean, to show/hide rating system in comment, default FALSE global $e107cache, $totcc; + + $tp = e107::getParser(); $ns = e107::getRender(); $pref = e107::getPref(); @@ -897,69 +908,15 @@ class comment // ------------- TODO move the 'listing' into separate function so that ajax can access it easily. - $sql = e107::getDb(); - $type = $this->getCommentType($table); - $sort = vartrue($pref['comments_sort'],'desc'); - - if(vartrue($pref['nested_comments'])) - { - $query = "SELECT c.*, u.*, ue.*, r.* FROM #comments AS c - LEFT JOIN #user AS u ON c.comment_author_id = u.user_id - LEFT JOIN #user_extended AS ue ON c.comment_author_id = ue.user_extended_id - LEFT JOIN #rate AS r ON c.comment_id = r.rate_itemid AND r.rate_table = 'comments' - - WHERE c.comment_item_id='".intval($id)."' AND c.comment_type='".$tp->toDB($type, true)."' AND c.comment_pid='0' - ORDER BY c.comment_datestamp ".$sort; - } - else - { - $query = "SELECT c.*, u.*, ue.*, r.* FROM #comments AS c - LEFT JOIN #user AS u ON c.comment_author_id = u.user_id - LEFT JOIN #user_extended AS ue ON c.comment_author_id = ue.user_extended_id - LEFT JOIN #rate AS r ON c.comment_id = r.rate_itemid AND r.rate_table = 'comments' "; - $query .= "WHERE c.comment_item_id='".intval($id)."' AND c.comment_type='".$tp->toDB($type, true)."' - ORDER BY c.comment_datestamp ".$sort; - } - - - $text = ""; - $comment = ''; - $modcomment = ''; - $lock = ''; - $ret['comment'] = ''; - $moderator = getperms('0'); - - if ($this->totalComments = $sql->db_Select_gen($query)) - { - - $width = 0; - //Shortcodes could use $sql, so just grab all results - $rows = $sql->db_getList(); - - //while ($row = $sql->db_Fetch()) - foreach ($rows as $row) - { - - if($this->isPending($row,$moderator)) - { - continue; - } - - $lock = $row['comment_lock']; - // $subject = $tp->toHTML($subject); - if ($pref['nested_comments']) - { - $text .= $this->render_comment($row, $table, $action, $id, $width, $tp->toHTML($subject), $rate); - } - else - { - $text .= $this->render_comment($row, $table, $action, $id, $width, $tp->toHTML($subject), $rate); - } - } // end loop - - } // end if - + $options = array( + 'action' => $action, + 'subject' => $subject, + 'rate' => $rate + ); + $tmp = $this->getComments($table,$id,0,$options); // render all comments; + $text = $tmp['comments']; + $lock = $tmp['lock']; // ------------------------------------------------------- @@ -971,7 +928,7 @@ class comment $modcomment .= ""; } - + $modcomment .= $this->nextprev($table,$id,$from); // --------------------------- if ($lock != '1') @@ -983,8 +940,8 @@ class comment $comment = "
".COMLAN_328."
"; } - $search = array("{MODERATE}","{COMMENTS}","{COMMENTFORM}"); - $replace = array($modcomment,"
\n".$text."\n
",$comment); + $search = array("{MODERATE}","{COMMENTS}","{COMMENTFORM}","{COMMENTNAV}"); + $replace = array($modcomment,"
\n".$text."\n
",$comment,$pagination); $TEMPL = str_replace($search,$replace,$this->template['LAYOUT']); @@ -1021,17 +978,98 @@ class comment - function renderComments($table,$id,$from,$options=null) + function getComments($table,$id,$from=0,$att=null) { + // global $e107cache, $totcc; + //TODO Cache + $sql = e107::getDb(); + $tp = e107::getParser(); + $pref = e107::getPref(); + $action = varset($att['action']); + $subject = varset($att['subject']); + $rate = varset($att['rate']); + + $type = $this->getCommentType($table); + $sort = vartrue($pref['comments_sort'],'desc'); + if(vartrue($pref['nested_comments'])) + { + $query = "SELECT c.*, u.*, ue.*, r.* FROM #comments AS c + LEFT JOIN #user AS u ON c.comment_author_id = u.user_id + LEFT JOIN #user_extended AS ue ON c.comment_author_id = ue.user_extended_id + LEFT JOIN #rate AS r ON c.comment_id = r.rate_itemid AND r.rate_table = 'comments' + + WHERE c.comment_item_id='".intval($id)."' AND c.comment_type='".$tp->toDB($type, true)."' AND c.comment_pid='0' + ORDER BY c.comment_datestamp ".$sort; + } + else + { + $query = "SELECT c.*, u.*, ue.*, r.* FROM #comments AS c + LEFT JOIN #user AS u ON c.comment_author_id = u.user_id + LEFT JOIN #user_extended AS ue ON c.comment_author_id = ue.user_extended_id + LEFT JOIN #rate AS r ON c.comment_id = r.rate_itemid AND r.rate_table = 'comments' "; + $query .= "WHERE c.comment_item_id='".intval($id)."' AND c.comment_type='".$tp->toDB($type, true)."' + ORDER BY c.comment_datestamp ".$sort; + } + + $this->totalComments = $sql->db_Select_gen($query); + + $query .= " LIMIT ".$from.",".$this->commentsPerPage; + + $text = ""; + $lock = ''; + + if ($sql->db_Select_gen($query)) + { + $width = 0; + $rows = $sql->db_getList(); //Shortcodes could use $sql, so just grab all results + + foreach ($rows as $row) + { + + if($this->isPending($row)) + { + continue; + } + + $lock = $row['comment_lock']; + + if ($pref['nested_comments']) + { + $text .= $this->render_comment($row, $table, $action, $id, $width, $tp->toHTML($subject), $rate); + } + else + { + $text .= $this->render_comment($row, $table, $action, $id, $width, $tp->toHTML($subject), $rate); + } + } // end loop + + } // end if + + return array('comments'=> $text,'lock'=> $lock); } - + function nextprev($table,$id,$from=0) + { + //return "table=".$table." id=".$id." from=".$from; + //$from = $from + $this->commentsPerPage; + + + // from calculations are done by eNav() js. + return " + Previous + + Next + + "; + + + } diff --git a/e107_handlers/media_class.php b/e107_handlers/media_class.php index 080bba8ec..a36b95250 100644 --- a/e107_handlers/media_class.php +++ b/e107_handlers/media_class.php @@ -497,7 +497,7 @@ class e_media if($navMode === false) { - $text .= "
Filter: "; + $text .= "
Filter: "; $text .= " "; // Manual filter, if onkeyup ajax fails for some reason. // $text .= ""; // see previous page of images. $text .= ""; // see next page of images.