<?php
// lang vars (also in window.php)
$lang['l_comment_helpful'] = "%1\$s of %2\$s people find this comment to be helpful";
$lang['l_conc_num'] = "There are %1\$s replies on this comment";
$lang['l_conc_non'] = "Reply to this";
function get_conc ($i)
{
global $conc_txt, $db_prefix, $tpl_section;
$cres = sql_query ("SELECT * FROM ".$db_prefix."qcomment WHERE mod_id='conc' AND item_id='$i' ORDER BY comment_id");
while ($crow = sql_fetch_array ($cres))
{
$crow['conc_id'] = $crow['comment_id'];
$crow['conc_title'] = $crow['comment_title'];
$crow['conc_title_encrypted'] = safe_send ('[CONC] Comment on '.$crow['comment_title']);
$conc_txt .= quick_tpl ($tpl_section['conc_item'], $crow);
get_conc ($crow['comment_id']);
}
}
$m = get_param ('m'); // identifier (eg, page, comment, review, portal, etc), also used to get rules
$i = get_param ('i'); // index, or item_id
$p = get_param ('p'); // page
$t = get_param ('t'); // title (safe_send)
$c = get_param ('comment_id'); // comment id
$box = get_param ('box'); // show/hide comment form
$rate = get_param ('rate'); //
$save = post_param ('save'); // save comment?
$title = safe_receive ($t); // title
$helpful = get_param ('helpful'); // helpful mode
$window = get_param ('window'); // windowed mode? (eg. included in ajax?)
if (empty ($m)) $m = post_param ('m');
if (empty ($i)) $i = post_param ('i');
$mode = 'blank';
if ($t && !$box) $mode = 'show_comment'; // if title defined but not show comment form => show comment only
if ($t && $box) $mode = 'show_box'; // if title defined but not show comment form => show comment & form
if ($save) $mode = 'save_comment'; // if save mode, save comment
if ($rate) $mode = 'save_rate'; // if save mode, save rating
if ($helpful) $mode = 'helpful'; // if helpful mode, save helpful info
// get rules
$row = sql_qquery ("SELECT * FROM ".$db_prefix."qcomment_set WHERE mod_id = '$m' LIMIT 1");
// define rules from database
if (!empty ($row))
{
$comment_mode = $row['comment_mode'];
$approval = $row['comment_approval'];
$member_only = $row['member_only'];
$unique = $row['unique_comment'];
$conc = $row['comment_on_comment'];
$captcha = $row['captcha'];
$helpful = $row['comment_helpful'];
$detail = $row['detail'];
}
else
{
if ($helpful)
$unique = $comment_mode = $conc = FALSE;
else
msg_die ('echo', 'Invalid mod_id, please define it in ACP!');
}
if ($unique && !$member_only) $member_only = TRUE; // member only for unique
if ($comment_mode == 2) $rating_box = FALSE; else $rating_box = TRUE; // rating box only in rating and comment & rating mode
if ($window) $ipp = 5;
if ($helpful && $window) $helpful_js = FALSE; // helpful form only avail in full screen
elseif ($helpful && !$window) $helpful_js = TRUE;
elseif (!$helpful && !$window) $helpful_js = FALSE;
// comment mode
switch ($mode)
{
// show comment with box OR/AND show input box only OR/AND windowed mode (aka shown with inside another page, but without comment box & pagination)
// as input box depends on several factors (like login, unique, etc) AND windowed mode basically the same as show comment;
// so, it would be wasting my time to recreate separate functions to do the same job
case 'show_comment':
case 'show_box':
if (empty ($title)) msg_die ('echo', 'Unknown Page!');
// open tpl
if ($member_only && !$login) $show_box = FALSE; else $show_box = TRUE;
if ($unique)
{
$foo = sql_qquery ("SELECT comment_user FROM ".$db_prefix."qcomment WHERE mod_id='$m' AND item_id='$i' AND comment_user='$current_user_id' LIMIT 1");
if (!empty ($foo)) $show_box = FALSE;
}
$tpl = load_tpl ('mod', 'module_qcomment.tpl');
$foo = load_section ('mod', 'module_qcomment_section.tpl');
$txt['block_comment'] = '';
// get num of comments
if ($mode == 'show_comment')
{
$txt['block_comment'] = qcache_get ('qcomment_main_'.$m.'_'.$i);
if (empty ($txt['block_comment']))
{
$f = sql_qquery ("SELECT COUNT(*) AS total FROM ".$db_prefix."qcomment WHERE mod_id='$m' AND item_id='$i' AND comment_approve='1'");
$num = $f[0];
// get comments
$foo = sql_multipage ($db_prefix."qcomment", '*', "mod_id='$m' AND item_id='$i' AND comment_approve='1'", "comment_id", $p, "task.php?mod=qcomment&m=$m&i=$i&t=$t");
foreach ($foo as $row)
{
$conc_txt = '';
if (empty ($row['comment_user'])) $row['comment_user'] = $lang['l_guest'];
// rating?
if (($rating_box) && ($row['comment_rate'])) $row['rating'] = rating_img ($row['comment_rate']); else $row['rating'] = '';
// helpful?
$f = explode ('|', $row['comment_helpful']);
if (empty ($f[1]))
$row['comment_helpful'] = '';
else
$row['comment_helpful'] = sprintf ($lang['l_comment_helpful'], $f[0], $f[1]);
// conc?
if ($conc)
{
get_conc ($row['comment_id']);
$row['conc_id'] = $row['comment_id'];
$row['conc_title'] = $row['comment_title'];
$row['conc_title_encrypted'] = safe_send ('[CONC] Comment on '.$row['comment_title']);
$row['conc_msg'] = $conc_txt;
}
$row['t'] = safe_send ($row['comment_title']);
$row['comment_date'] = convert_date ($row['comment_date']);
$row['comment_body'] = bbcode (word_censor ($row['comment_body']));
$txt['block_comment'] .= quick_tpl ($tpl_block['comment'], $row);
}
qcache_update ('qcomment_main_'.$m.'_'.$i, $txt['block_comment']);
}
}
// show avg rating
$txt['rating_avg'] = '';
if ($rating_box)
{
$avg = sql_qquery ("SELECT AVG(comment_rate) FROM ".$db_prefix."qcomment WHERE mod_id='$m' AND item_id='$i' AND comment_approve='1' AND comment_rate>0");
$txt['rating_avg'] = rating_img ($avg[0]);
}
// the rest
qvc_init ();
if (!$row = load_form ('comment')) $row = create_blank_tbl ($db_prefix.'qcomment');
if ($rating_box) $txt['rating_select'] = create_select_form ('comment_rate', $rating_def);
$txt = array_merge ($txt, $row);
$txt['t'] = safe_send ($title);
$txt['num'] = ($mode == 'show_box') ? 0 : num_format ($num);
$txt['jtitle'] = str_replace (array ("'", '''), "\'", $title);
$txt['title'] = $title;
$txt['item_title'] = safe_send ($title);
$txt['item_id'] = $i;
$txt['mod_id'] = $m;
$txt['current_user_id'] = $login ? $current_user_id : $lang['l_guest'];
$txt['the_title'] = ($window) ? '' : quick_tpl ($tpl_section['mod_the_title'], $txt);
$txt['comment_area'] = bbc_area ('comment_body', $row['comment_body'], 500, 150);
// output
if ($show_box)
$txt['comment_box'] = quick_tpl (load_tpl ('var', $tpl_section['mod_comment_box']), $txt);
else
$txt['comment_box'] = quick_tpl ($tpl_section['mod_no_comment_box'], $txt);
if ($helpful_js)
$txt['helpful_js'] = quick_tpl ($tpl_section['mod_helpful_js'], $txt);
else
$txt['helpful_js'] = '';
if ($window) // windowed mode (as i'm too lazy to repeat the script in window.php)
{
$popup = TRUE;
$show_box = FALSE;
$txt['pagination'] = '';
$txt['comment_box'] = quick_tpl ($tpl_section['mod_more_comment'], $txt);
}
if ($m == 'conc' && !$box) { $popup = TRUE;; $txt['comment_box'] = ''; }
if ($m == 'conc' && $box) { $popup = TRUE;; }
// flush
if ($mode == 'show_comment')
$txt['main_body'] = quick_tpl ($tpl, $txt);
else
$txt['main_body'] = $txt['comment_box'];
break;
case 'save_comment':
// member only?
if ($member_only && !$login) msg_die ('not_member');
// unique? (NEED MEMBER ONLY RULE)
if ($unique)
{
$foo = sql_qquery ("SELECT comment_user FROM ".$db_prefix."qcomment WHERE mod_id='$m' AND item_id='$i' AND comment_user='$current_user_id' LIMIT 1");
if (!empty ($foo)) msg_die ('echo', 'One person, one comment, please!');
}
// save comment
$item_title = safe_receive (post_param ('t'));
$comment_title = post_param ('comment_title');
$comment_body = post_param ('comment_body', '', 'bbcode');
$comment_rate = post_param ('comment_rate');
$visual = post_param ('visual');
save_form ('comment');
// verify entries
if (($captcha) && (sha1 ($visual) != qvc_value ())) msg_die ('echo', '<b>Warning!</b> Visual confirmation not match!');
if (!$rating_box) $comment_rate = 0;
if (($comment_rate < 0) || ($comment_rate > 5) || (!is_numeric ($comment_rate))) $comment_rate = 0;
if (empty ($comment_body) || (strlen ($comment_body) < 1)) msg_die ('echo', '<b>Warning!</b> You didn\'t enter any message! Press back to retry!');
if (empty ($comment_title)) $comment_title = $lang['l_untitled'];
if (!$login) $comment_user = ''; else $comment_user = $current_user_id;
// set status to?
reset_form ();
if (!$approval) $approved = 1; else $approved = 0;
// reset cache
qcache_clear ();
// insert into db
sql_query ("INSERT INTO ".$db_prefix."qcomment VALUES ('', '$m', '$i', '$item_title', '$comment_user', '$comment_title', '$comment_body', '$sql_today', '$comment_rate', '0|0', $approved)");
msg_die ('echo', 'Comment accepted. We will post it as soon as possible. Thank your for your participation!');
break;
case 'save_rate':
// check cookies
$ident = $m.'_'.$i;
$cookies = empty ($_COOKIE['rating']) ? '' : $_COOKIE['rating'];
if (!empty ($cookies[$ident])) msg_die ('echo', 'You have casted your vote.');
if (($rate > 5) || ($rate < 1)) msg_die ('echo', 'Invalid rating value!');
// check in db
$item_title = safe_receive ($t);
$row = sql_qquery ("SELECT * FROM ".$db_prefix."qcomment WHERE mod_id='$m*rate' AND item_id='$i' LIMIT 1");
if (empty ($row)) $row['comment_title'] = '0|0';
// rate
$f = explode ('|', $row['comment_title']);
$avg = ($f[0] * $f[1] + $rate) / (++$f[1]);
$t = "$avg|$f[1]";
if (!empty ($row['item_id']))
sql_query ("UPDATE ".$db_prefix."qcomment
SET comment_title = '$t'
WHERE mod_id='$m*rate' AND item_id='9999999' LIMIT 1");
else
sql_query ("INSERT INTO ".$db_prefix."qcomment VALUES ('', '$m*rate', '$i', '$item_title', 'rate', '$t', '', '$sql_today', 0, '0|0', 1)");
// set cookies for 1 year
$exp = time() + 31536000;
setcookie ("rating[$ident]", 1, $exp);
// reset cache
qcache_clear ();
// done
msg_die ('echo', 'Thank you! Your rating has been received!');
break;
case 'helpful':
$yes = get_param ('yes');
$no = get_param ('no');
$cookies = empty ($_COOKIE['helpful']) ? '' : $_COOKIE['helpful'];
if (!empty ($cookies[$c])) msg_die ('echo', 'You have casted your vote.');
// get help
$res = sql_query ("SELECT comment_helpful FROM ".$db_prefix."qcomment WHERE comment_id='$c' LIMIT 1");
$row = sql_fetch_array ($res);
if (empty ($row['comment_helpful'])) $row['comment_helpful'] = '0|0';
$f = explode ('|', $row['comment_helpful']);
if ($yes) { $f[0]++; $f[1]++; }
elseif ($no) { $f[1]++; }
// set cookies for 1 year
$exp = time() + 31536000;
setcookie ("helpful[$c]", 1, $exp);
// reset cache
qcache_clear ();
$h = implode ('|', $f);
sql_query ("UPDATE ".$db_prefix."qcomment SET comment_helpful='$h' WHERE comment_id='$c' LIMIT 1");
msg_die ('sql_ok');
break;
default:
redir();
break;
}
?>