Saturday, October 10, 2009

MC Sidebar top users code two

Lets try that again;

In index.php, add the following code;


$user = new User;


$select = "SELECT user_id, COUNT(*) as count ";

$from_where = " FROM " . table_comments . ", " . table_users . " WHERE comment_user_id=user_id GROUP BY comment_user_id";

$order_by = " ORDER BY count DESC ";


$rows = $db->get_var("select COUNT(*) as count $from_where $order_by");

$users = $db->get_results("$select $from_where $order_by LIMIT 0, 5");

$users_table = '';


if ($users) {

foreach($users as $dbuser) {


$user->id=$dbuser->user_id;

$user->user_read();

$user->all_stats();


$main_smarty->assign('user_userlink', getmyurl("user", $user->username));

$main_smarty->assign('user_avatar', get_avatar('large', "", $user->username, $user->email));

//function get_avatar($size = "large", $avatarsource, $user_name = "", $user_email = "", $user_id=""){

$users_table .= $main_smarty->fetch(The_Template . "/topusers_data.tpl");



}

}


$main_smarty->assign('users_table', $users_table);


In the file templates/oioioi/topusers_data.tpl reduce the code to;


[tr]

[td][a href="{$user_userlink}"][img src="{$user_avatar}" align="absmiddle" /][/a][/td]

[/tr]


Finally in templates/oioioi/sidebar2.tpl add the following code;


[div class="links"]

[/div]


[div class="headline"]

[div class="sectiontitle"]Most Commented Users[/div]

[/div]

[div class="aboutcontent"]

{$users_table}

[/div]

[div class="links"]

[/div]


This may need further modification in order to fully incorporate CSS

MC Sidebar top users code

This code adds the top five users to the sidebar.

In index.php, add the following code;


$user = new User;


$select = "SELECT user_id, COUNT(*) as count ";

$from_where = " FROM " . table_comments . ", " . table_users . " WHERE comment_user_id=user_id GROUP BY comment_user_id";

$order_by = " ORDER BY count DESC ";


$rows = $db->get_var("select COUNT(*) as count $from_where $order_by");

$users = $db->get_results("$select $from_where $order_by LIMIT 0, 5");

$users_table = '';


if ($users) {

foreach($users as $dbuser) {


$user->id=$dbuser->user_id;

$user->user_read();

$user->all_stats();


$main_smarty->assign('user_userlink', getmyurl("user", $user->username));

$main_smarty->assign('user_avatar', get_avatar('large', "", $user->username, $user->email));

//function get_avatar($size = "large", $avatarsource, $user_name = "", $user_email = "", $user_id=""){

$users_table .= $main_smarty->fetch(The_Template . "/topusers_data.tpl");



}

}


$main_smarty->assign('users_table', $users_table);


In the file templates/oioioi/topusers_data.tpl reduce the code to;



Finally in templates/oioioi/sidebar2.tpl add the following code;



Most Commented Users

{$users_table}


This may need further modification in order to fully incorporate CSS


{insert two hour rant about going back to front page here}

Wednesday, October 7, 2009

MC Final fix to the front page

The previous version of the code only seemed to work on my local machine. I am unsure why, but this new code has been checked and confirmed on several machines, and hence should be the final version.

In index.php replace/add the code with



// find the name of the current category

if(isset($_REQUEST['category'])){

$thecat = get_cached_category_data('category_safe_name', sanitize($_REQUEST['category'], 1));

$thecat = $thecat->category_name;

if (!$thecat)

{

$main_smarty->assign('tpl_center', '404error');

$main_smarty->display($the_template . '/pligg.tpl');

die();

}

}


// start a new search

$search=new Search();


// check for some get/post

if(isset($_REQUEST['from'])){

$search->newerthan = sanitize($_REQUEST['from'], 3);

}

unset($_REQUEST['search']);

unset($_POST['search']);

unset($_GET['search']);

if(isset($_REQUEST['search'])){$search->searchTerm = sanitize($_REQUEST['search'], 3);}

if(isset($_REQUEST['search'])){$search->filterToStatus = "all";}

if(!isset($_REQUEST['search'])){$search->orderBy = "link_published_date DESC";}

if(isset($_REQUEST['tag'])){$search->searchTerm = sanitize($_REQUEST['search'], 3); $search->isTag = true;}

if(isset($thecat)){

$search->category = $thecat;


} else {

$thecat = News; // Search with only opinion

$search->category = $thecat;


}


// figure out what "page" of the results we're on

$search->offset = (get_current_page()-1)*$page_size;


// pagesize set in the admin panel

$search->pagesize = $page_size;


// since this is index, we only want to view "published" stories

$search->filterToStatus = "published";


// this is for the tabs on the top that filter

if(isset($_GET['part'])){$search->setmek = $db->escape($_GET['part']);}

$search->do_setmek();


// do the search

$search->doSearch();


$linksum_count = $search->countsql;

$linksum_sql = $search->sql;




$search2=new Search();


if(isset($_REQUEST['from'])){

$search2->newerthan = sanitize($_REQUEST['from'], 3);

}

if(isset($_REQUEST['search'])){$search2->searchTerm = sanitize($_REQUEST['search'], 3);}

if(isset($_REQUEST['search'])){$search2->filterToStatus = "all";}

if(!isset($_REQUEST['search'])){$search2->orderBy = "link_published_date DESC";}

if(isset($_REQUEST['tag'])){$search2->searchTerm = sanitize($_REQUEST['search'], 3); $search->isTag = true;}

if(!isset($_REQUEST['category'])){ // Activates when on the index page and no category select has been activated

$catnameopinion = Opinion; // Search with only opinion

}

if(isset($catnameopinion)){


$search2->category = $catnameopinion;

}


// figure out what "page" of the results we're on

$search2->offset = (get_current_page()-1)*$page_size;


// pagesize set in the admin panel

$search2->pagesize = $page_size;


// since this is index, we only want to view "published" stories

$search2->filterToStatus = "published";


// this is for the tabs on the top that filter

if(isset($_GET['part'])){$search2->setmek = $db->escape($_GET['part']);}

$search2->do_setmek();


// do the search

$search2->doSearch();


$linksum_count_coltwo = $search2->countsql;

$linksum_sql_coltwo = $search2->sql;


// misc smarty

if(isset($from_text)){$main_smarty->assign('from_text', $from_text);}


if(isset($search->setmek)){

$main_smarty->assign('setmeka', $search->setmek);

$main_smarty->assign('setmeka', $search2->setmek);

}else{

$main_smarty->assign('setmeka', '');

}


$fetch_link_summary = true; // this feed information back to libs/link_summary.php

$fetch_news_link_summary = true; // this feed information back to libs/link_summary.php for news

$fetch_opinion_link_summary = true; // this feed information back to libs/link_summary.php for opinion

include('./libs/link_summary.php'); // this is the code that show the links / stories

$main_smarty->assign('link_pagination', do_pages($rows, $page_size, "published", true));



In libs/link_summary.php, replace the code with;





if ($new_search) {

// used on the search page

$rows = $new_search['count'];

$new_search = $new_search['rows'];

$the_results = $new_search;

} else {

// used in the index and upcoming pages

$ls_debug = false;

if($ls_debug == true){

echo '--' . sanitize($linksum_count,3) . '--
';

echo '--' . sanitize($linksum_count_coltwo,3) . '--
';

}

if($linksum_count == "SELECT count(*) FROM " . table_links . " WHERE link_status='published' "){

if($ls_debug == true){echo 'p';}

$rows = $cached_totals['published'];

} elseif ($linksum_count == "SELECT count(*) FROM " . table_links . " WHERE link_status='queued' ") {

if($ls_debug == true){echo 'u';}

$rows = $cached_totals['queued'];

} else {

if($ls_debug == true){

echo 'r';}

$rows = $db->get_var($linksum_count);

$rows_opinion = $db->get_var($linksum_count_coltwo);

}

if($ls_debug == true){echo '
' . sanitize($rows,3) . '
';}

$links = $db->get_col($linksum_sql);

$the_results = $links;

$links_opinion = $db->get_col($linksum_sql_coltwo); // Brings in second search data from index.php

$the_results_opinion = $links_opinion; // Brings in second search data from index.php

}



if($the_results_opinion){

// find out if the logged in user voted / reported each of

// the stories that the search found and cache the results

require_once(mnminclude.'votes.php');

// DB 03/02/09

// $vote = new Vote;

// $vote->type='links';

// $vote->user=$current_user->user_id;

// $vote->link=$the_results;

// $results = $vote->user_list_all_votes();

/////

$vote = '';

$results_opinion = ''; // we don't actually need the results

// we're just calling this to cache the results

// so when we foreach the links we don't have to

// run 1 extra query for each story to determine

// current user votes

// setup the link cache

$sql = "SELECT " . table_links . ".* FROM " . table_links . " WHERE ";

$sql_saved = "SELECT * FROM " . table_saved_links . " WHERE saved_user_id=" . $current_user->user_id . " AND ";

$ids_opinion = array();

foreach($the_results_opinion as $opinion_link_id) {

// first make sure we don't already have it cached

if(!isset($cached_links[$opinion_link_id])){

$ids_opinion[] = $opinion_link_id;

}

if(!isset($cached_saved_links[$opinion_link_id])){

$saved_ids[] = $opinion_link_id;

}

}

// if count = 0 then all the links are already cached

// so don't touch the db

// if count > 0 then there is at least 1 link to get

// so get the SQL and add results to the cache


if ( count ( $ids_opinion ) ) {

$sql .= 'link_id IN ('.implode(',',$ids_opinion).')';

foreach ( $db->get_results($sql) as $row ) {

$cached_links[$row->opinion_link_id] = $row;

if(!isset($link_authors[$row->link_author])){

$link_authors[$row->link_author] = $row->link_author;

}

}

}


// get all authors at once from the users table

$sql = 'SELECT * FROM ' . table_users . ' WHERE ';

if ( count ( $link_authors ) ) {

$sql .= 'user_id IN (' . implode(',', $link_authors) . ')';


foreach ( $db->get_results($sql) as $user ) {

$cached_users[$user->user_id] = $user;

}

}


// user saved _links

if ( count ( $saved_ids ) ) {

$sql_saved .= 'saved_link_id IN ('.implode(',',$ids_opinion).')';

$results_opinion = $db->get_results($sql_saved);


if($results){

foreach($results_opinion as $row){

$sl[$row->saved_link_id] = 1;

}

}

foreach($the_results_opinion as $opinion_link_id) {

if(isset($sl[$opinion_link_id])){

$cached_saved_links[$opinion_link_id] = 1;

} else {

$cached_saved_links[$opinion_link_id] = 0;

}

}

}

// end link cache setup

}


global $display_grouplinks;

if(!isset($link_summary_output)){$link_summary_output = '';}

if ($new_search) {

foreach($new_search as $link_id) {

$link->id=$link_id;

$link->read();

if( $display_grouplinks ) $link->link_group_id = 0;

$link_summary_output .= $link->print_summary('summary', true); // standard function call

$news_link_summary_output .= $link-> print_news_summary('summary', true); // news only function call

}

} else {

if ($links) {

foreach($links as $link_id) {

$link->id=$link_id;

$link->read();

$link_summary_output .= $link->print_summary('summary', true); // standard function call

$news_link_summary_output .= $link-> print_news_summary('summary', true); // news only function call

}

}

}

if ($new_search) {

foreach($new_search as $opinion_link_id) { //function to apply second search function

$link_opinion->id_opinion=$opinion_link_id;

$link_opinion->read_opinion();

if( $display_grouplinks ) $link->link_group_id = 0;

$opinion_link_summary_output .= $link_opinion-> print_opinion_summary('summary', true); // opinion only function call

}

} else {

if ($links_opinion) {

foreach($links_opinion as $opinion_link_id) { //function to apply second search function

$link_opinion->id_opinion=$opinion_link_id;

$link_opinion->read_opinion();

$opinion_link_summary_output .= $link_opinion-> print_opinion_summary('summary', true); // opinion only function call

}

}

}

if(isset($fetch_link_summary) && $fetch_link_summary == true){ // standard feedback from index.php

$main_smarty->assign('link_summary_output', $link_summary_output);

} else {

echo $link_summary_output;

}

if(isset($fetch_news_link_summary) && $fetch_news_link_summary == true){ // news feedback from index.php

$main_smarty->assign('news_link_summary_output', $news_link_summary_output);

} else {

echo $news_link_summary_output;

}

if(isset($fetch_opinion_link_summary) && $fetch_opinion_link_summary == true){ // opinion feedback from index.php

$main_smarty->assign('opinion_link_summary_output', $opinion_link_summary_output);

} else {

echo $opinion_link_summary_output;

}




In libs/link.php, add the following code;





function read_opinion($usecache = TRUE) {

global $db, $current_user, $cached_links;

$id_opinion = $this->id_opinion;

$this->rating = 0;

if(!is_numeric($id_opinion)){return false;}

// check to see if the link is cached

// if it is, use it

// if not, get from mysql and save to cache

if (isset($cached_links[$id_opinion]) && $usecache == TRUE) {

$link_opinion = $cached_links[$id_opinion];

} else {

$link_opinion = $db->get_row("SELECT " . table_links . ".* FROM " . table_links . " WHERE link_id = $id_opinion");

$cached_links[$id_opinion] = $link_opinion;

}

if($link_opinion) {

$this->author=$link_opinion->link_author;

$this->userid=$link_opinion->link_author;

$this->status=$link_opinion->link_status;

$this->votes=$link_opinion->link_votes;

$this->reports=$link_opinion->link_reports;

$this->comments=$link_opinion->link_comments;

$this->randkey=$link_opinion->link_randkey;

$this->category=$link_opinion->link_category;

$this->url= $link_opinion->link_url;

$this->url= str_replace('&', '&', $link_opinion->link_url);

$this->url_title=$link_opinion->link_url_title;

$this->url_description=$link_opinion->link_url_description;

$this->title=$link_opinion->link_title;

$this->title_url=$link_opinion->link_title_url;

$this->tags=$link_opinion->link_tags;

$this->content=$link_opinion->link_content;

// DB 01/08/09

$this->date=strtotime($link_opinion->link_date);

// $date=$link_opinion->link_date;

// $this->date=$db->get_var("SELECT UNIX_TIMESTAMP('$date')");

$this->published_date=strtotime($link_opinion->link_published_date);

// $date=$link_opinion->link_published_date;

// $this->published_date=$db->get_var("SELECT UNIX_TIMESTAMP('$date')");

$this->modified=strtotime($link_opinion->link_modified);

// $date=$link_opinion->link_modified;

// $this->modified=$db->get_var("SELECT UNIX_TIMESTAMP('$date')");

/////

$this->fullread = $this->read = true;

$this->link_summary = $link_opinion->link_summary;


$this->link_field1=$link_opinion->link_field1;

$this->link_field2=$link_opinion->link_field2;

$this->link_field3=$link_opinion->link_field3;

$this->link_field4=$link_opinion->link_field4;

$this->link_field5=$link_opinion->link_field5;

$this->link_field6=$link_opinion->link_field6;

$this->link_field7=$link_opinion->link_field7;

$this->link_field8=$link_opinion->link_field8;

$this->link_field9=$link_opinion->link_field9;

$this->link_field10=$link_opinion->link_field10;

$this->link_field11=$link_opinion->link_field11;

$this->link_field12=$link_opinion->link_field12;

$this->link_field13=$link_opinion->link_field13;

$this->link_field14=$link_opinion->link_field14;

$this->link_field15=$link_opinion->link_field15;

$this->link_group_id=$link_opinion->link_group_id;


return true;

}

$this->fullread = $this->read = false;

return false;

}

Friday, October 2, 2009

jct - redirect after submission fix

OK, found submit.php with all of the main non-template driven submission actions.

In the third step, I changed the redirect location from

header("Location: " . getmyurl('upcoming'));
to

header("Location: " .getmyurl('index'));


Which seems to be working fine now. Or at least it's taking me to the error home page, so I'm pretty sure it is...

Adding top users to bottom of page

Once we've sorted all the bugs, this site might help us add in the top users to tht bottom of the page.

http://forums.pligg.com/questions-comments/13605-2-free-modules-top-users-published-top-users-comments.htmlhttp://forums.pligg.com/questions-comments/13605-2-free-modules-top-users-published-top-users-comments.html

This is after bug solving though - I discovered this morning that the page redirect after submitting an article isn't working anymore (why are these things happening!?) so before I go forward trying to add new things, I'm frantically trying to fix things, between juggling work...

Thursday, October 1, 2009

MC Fixing the front page issue with differing number of front page stories

The problem with this was that the index.php file did only a single search, an then as the information recovered here worked it's way through the other files, my added code split it up between the two categories.

The problem with this is that if all eight latest stories are only News, then no Opinion stories show up on the front page. To solve this, I had to establish two separate searches, which started off being split by category.

Fortunately, the GET[category] code already existed to do this, so it was possible to adapt this code to create a search of only one category. The difficulty was how, but fortunately, after chaining a hundred monkeys to a hundred computers, I lucked out on a way of doing this without major changes.

Emphisis should be placed on the fact that this hasn't been fully tested yet; my local copy only has four stories at the moment, so I am unsure how it will effect multiple copies.

Anyway, here are the changes.

index.php


For the code


// find the name of the current category

if(isset($_REQUEST['category'])){

$thecat = get_cached_category_data('category_safe_name', sanitize($_REQUEST['category'], 1));

$thecat = $thecat->category_name;

if (!$thecat)

{

$main_smarty->assign('tpl_center', '404error');

$main_smarty->display($the_template . '/pligg.tpl');

die();

}

}



// start a new search

$search=new Search();


// check for some get/post

if(isset($_REQUEST['from'])){

$search->newerthan = sanitize($_REQUEST['from'], 3);

}

unset($_REQUEST['search']);

unset($_POST['search']);

unset($_GET['search']);

if(isset($_REQUEST['search'])){$search->searchTerm = sanitize($_REQUEST['search'], 3);}

if(isset($_REQUEST['search'])){$search->filterToStatus = "all";}

if(!isset($_REQUEST['search'])){$search->orderBy = "link_published_date DESC";}

if(isset($_REQUEST['tag'])){$search->searchTerm = sanitize($_REQUEST['search'], 3); $search->isTag = true;}

if(isset($thecat)){

$search->category = $thecat;

}


// figure out what "page" of the results we're on

$search->offset = (get_current_page()-1)*$page_size;


// pagesize set in the admin panel

$search->pagesize = $page_size;


// since this is index, we only want to view "published" stories

$search->filterToStatus = "published";


// this is for the tabs on the top that filter

if(isset($_GET['part'])){$search->setmek = $db->escape($_GET['part']);}

$search->do_setmek();


// do the search

$search->doSearch();


$linksum_count = $search->countsql;

$linksum_sql = $search->sql;


Alter to


// find the name of the current category

if(isset($_REQUEST['category'])){

$thecat = get_cached_category_data('category_safe_name', sanitize($_REQUEST['category'], 1));

$thecat = $thecat->category_name;

if (!$thecat)

{

$main_smarty->assign('tpl_center', '404error');

$main_smarty->display($the_template . '/pligg.tpl');

die();

}

}elseif(!isset($_REQUEST['category'])){

$catnameopinion = opinion;

if(isset($catnameopinion)){

$thecatopinion = get_cached_category_data('category_safe_name', sanitize($catnameopinion, 1));

$thecatopinion = $thecatopinion->category_name;

if (!$thecatopinion)

{

$main_smarty->assign('tpl_center', '404error');

$main_smarty->display($the_template . '/pligg.tpl');

die();

}

}

}


// start a new search

$search=new Search();

$search2=new Search();


// check for some get/post

if(isset($_REQUEST['from'])){

$search->newerthan = sanitize($_REQUEST['from'], 3);

$search2->newerthan = sanitize($_REQUEST['from'], 3);

}

unset($_REQUEST['search']);

unset($_POST['search']);

unset($_GET['search']);

if(isset($_REQUEST['search'])){$search->searchTerm = sanitize($_REQUEST['search'], 3);

$search2->searchTerm = sanitize($_REQUEST['search'], 3);}

if(isset($_REQUEST['search'])){$search->filterToStatus = "all";

$search2->filterToStatus = "all";}

if(!isset($_REQUEST['search'])){$search->orderBy = "link_published_date DESC";

$search2->orderBy = "link_published_date DESC";}

if(isset($_REQUEST['tag'])){$search->searchTerm = sanitize($_REQUEST['search'], 3); $search->isTag = true;

$search2->searchTerm = sanitize($_REQUEST['search'], 3); $search2->isTag = true;}

if(isset($thecat)){

$search->category = $thecat;

}

if(isset($thecatopinion)){

$search2->category = $thecatopinion;

}


// figure out what "page" of the results we're on

$search->offset = (get_current_page()-1)*$page_size;

$search2->offset = (get_current_page()-1)*$page_size;


// pagesize set in the admin panel

$search->pagesize = $page_size;

$search2->pagesize = $page_size;


// since this is index, we only want to view "published" stories

$search->filterToStatus = "published";

$search2->filterToStatus = "published";


// this is for the tabs on the top that filter

if(isset($_GET['part'])){$search->setmek = $db->escape($_GET['part']);

$search2->setmek = $db->escape($_GET['part']);}

$search->do_setmek();

$search2->do_setmek();


// do the search

$search->doSearch();

$search2->doSearch();


$linksum_count = $search->countsql;

$linksum_sql = $search->sql;

$linksum_count_opinion = $search2->countsql;

$linksum_sql_opinion = $search2->sql;


And for


// misc smarty

if(isset($from_text)){$main_smarty->assign('from_text', $from_text);}

if(isset($search->setmek)){$main_smarty->assign('setmeka', $search->setmek);}else{$main_smarty->assign('setmeka', '');}


to


// misc smarty

if(isset($from_text)){$main_smarty->assign('from_text', $from_text);}

if(isset($search->setmek)){$main_smarty->assign('setmeka', $search->setmek);

$main_smarty->assign('setmeka', $search2->setmek);}else{$main_smarty->assign('setmeka', '');}




libs/link_summery.php


at the top of the page, add


if($ls_debug == true){echo '
' . sanitize($rows,3) . '
';}

$links = $db->get_col($linksum_sql);

$the_results = $links;

$links_opinion = $db->get_col($linksum_sql_opinion);

$the_results_opinion = $links_opinion;


And at the bottom of the page, for


if ($links) {

foreach($links as $link_id) {

$link->id=$link_id;

$link->read();

$link_summary_output .= $link->print_summary('summary', true);

$news_link_summary_output .= $link-> print_news_summary('summary', true);

$opinion_link_summary_output .= $link-> print_opinion_summary('summary', true);

}

}


alter to


if ($links || $links_opinion) {

foreach($links as $link_id) {

$link->id=$link_id;

$link->read();

$link_summary_output .= $link->print_summary('summary', true);

$news_link_summary_output .= $link-> print_news_summary('summary', true);

}

foreach($links_opinion as $link_id) {

$link->id=$link_id;

$link->read();

$opinion_link_summary_output .= $link-> print_opinion_summary('summary', true);

}

}