'activeHelper_liveHelp_timeByChatList',
'view' => 'activeHelper_liveHelp_timeByChatView',
'email' => 'activeHelper_liveHelp_timeByChatEmail'
);
if (!empty($_REQUEST['action']) && isset($actions[$_REQUEST['action']]))
return $actions[$_REQUEST['action']]();
return $actions['list']();
}
function activeHelper_liveHelp_timeByChatPost()
{
$actions = array(
'export' => 'activeHelper_liveHelp_timeByChatExportPost',
'delete' => 'activeHelper_liveHelp_timeByChatDeletePost',
'list' => 'activeHelper_liveHelp_timeByChatListPost',
'view' => 'activeHelper_liveHelp_timeByChatViewPost',
'viewExport' => 'activeHelper_liveHelp_timeByChatViewExportPost',
'email' => 'activeHelper_liveHelp_timeByChatEmailPost'
);
if (!empty($_REQUEST['action']) && isset($actions[$_REQUEST['action']]))
return $actions[$_REQUEST['action']]();
if (empty($_REQUEST['action']))
return $actions['list']();
}
function activeHelper_liveHelp_timeByChatEmailPost()
{
global $wpdb, $activeHelper_liveHelp;
$_REQUEST['id'] = !empty($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
$_POST['email_subject'] = !empty($_POST['email_subject']) ? (string) $_POST['email_subject'] : '';
$_POST['email_from_name'] = !empty($_POST['email_from_name']) ? (string) $_POST['email_from_name'] : '';
$_POST['email_from'] = !empty($_POST['email_from']) ? (string) $_POST['email_from'] : '';
$_POST['email_to'] = !empty($_POST['email_to']) ? (string) $_POST['email_to'] : '';
if (empty($_REQUEST['id']))
{
wp_redirect('admin.php?page=' . strtolower('activeHelper_liveHelp_timeByChat') . '&miss');
exit;
}
$_POST['content'] = !empty($_POST['content']) ? (string) $_POST['content'] : '';
if (!isset($_POST['submit']))
{
$_POST['content'] = '';
$statistic = $wpdb->get_row("
SELECT
CONCAT(jlu.firstname, ' ', jlu.lastname) AS agent, jls.department, jls.server,
jls.username, jls.email, DATE_FORMAT(jls.datetime, '%Y-%m-%d') AS date,
if(jls.rating = -1, '" . __('Not rate', 'activehelper_livehelp') . "', jls.rating) AS rating,
jlr.country, jlr.city
FROM
{$wpdb->prefix}livehelp_sessions AS jls,
{$wpdb->prefix}livehelp_users AS jlu,
{$wpdb->prefix}livehelp_requests AS jlr
WHERE jls.id = '{$_REQUEST['id']}'
AND jls.id_user = jlu.id
AND jls.request = jlr.id
", ARRAY_A);
if (!empty($statistic))
{
$_POST['content'] = '
' . __('Statistic', 'activehelper_livehelp') . '
' . "\n";
$_POST['content'] .= 'Domain: ' . $statistic['server'] . "\n" .
'Department: ' . $statistic['department'] . "\n" .
'Agent: ' . $statistic['agent'] . "\n" .
'Visitor: ' . $statistic['username'] . "\n" .
'Country: ' . $statistic['country'] . "\n" .
'City: ' . $statistic['city'] . "\n" .
'Email: ' . $statistic['email'] . "\n" .
'Date: ' . $statistic['date'] . "\n" .
'Rating: ' . $statistic['rating'] . "
\n";
}
$chats = $wpdb->get_results("
SELECT username, message, TIME_FORMAT(jlm.datetime, '%H:%i:%s') AS time
FROM {$wpdb->prefix}livehelp_messages AS jlm
WHERE session = '{$_REQUEST['id']}'
ORDER BY id
", ARRAY_A);
if (!empty($chats))
{
$_POST['content'] .= '' . __('Full chat', 'activehelper_livehelp') . '
' . "\n";
$_POST['content'] .= '';
foreach ($chats as $chat)
$_POST['content'] .= '[' . $chat['time'] . '] ' .
$chat['username'] . ': ' . $chat['message'] . "\n";
$_POST['content'] .= '
';
}
$session = $wpdb->get_row("
SELECT ls.email, ls.username, lu.firstname, lu.lastname, DATE_FORMAT(ls.datetime, '%Y-%m-%d') AS datetime
FROM {$wpdb->prefix}livehelp_sessions AS ls
LEFT JOIN {$wpdb->prefix}livehelp_users AS lu ON(lu.id = ls.id_user)
WHERE ls.id = '{$_REQUEST['id']}'
LIMIT 1
", ARRAY_A);
if (!empty($session))
{
if (!empty($session['lastname']))
$session['firstname'] = $session['firstname'] . ' ' . $session['lastname'];
$_POST['email_subject'] = 'LiveHelp - Time by chat: Within ' . $session['username'] . ' and ' . $session['firstname'] . ' on ' . $session['datetime'];
$_POST['email_from_name'] = $session['username'];
$_POST['email_from'] = $session['email'];
}
}
$errors = array();
$activeHelper_liveHelp['errors'] = &$errors;
$_POST['content'] = stripcslashes($_POST['content']);
while (isset($_POST['submit']))
{
unset($_POST['submit']);
if (empty($_POST['email_subject']))
$errors['email_subject'] = sprintf(__('You must insert a %s', 'activehelper_livehelp'), __('subject', 'activehelper_livehelp')); // error
if (empty($_POST['email_to']))
$errors['email_to'] = sprintf(__('You must insert an %s', 'activehelper_livehelp'), __('email', 'activehelper_livehelp')); // error
if (empty($_POST['email_from']))
$errors['email_from'] = sprintf(__('You must insert an %s', 'activehelper_livehelp'), __('email', 'activehelper_livehelp')); // error
// errors ...
if (!empty($errors))
break;
if (!preg_match("/^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$/", $_POST['email_to']))
$errors['email_to'] = sprintf(__('You must insert a %s', 'activehelper_livehelp'), __('valid email', 'activehelper_livehelp')); // error
if (!preg_match("/^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$/", $_POST['email_from']))
$errors['email_from'] = sprintf(__('You must insert a %s', 'activehelper_livehelp'), __('valid email', 'activehelper_livehelp')); // error
// errors ...
if (!empty($errors))
break;
if (!empty($_POST['email_from_name']))
$_POST['email_from'] = $_POST['email_from_name'] . '<' . $_POST['email_from'] . '>';
$to = $_POST['email_to'];
$subject = $_POST['email_subject'];
$message = apply_filters('the_content', $_POST['content']);
$headers = 'From: ' . $_POST['email_from'] . '' . "\r\n" .
'Reply-To: ' . $_POST['email_from'] . '' . "\r\n" .
'Content-type: text/html; charset=utf-8' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
wp_mail($to, $subject, $message, $headers);
wp_redirect('admin.php?page=' . strtolower('activeHelper_liveHelp_timeByChat') . '&action=email&id=' . $_REQUEST['id'] . '&email');
exit;
}
add_action('wp_enqueue_scripts', 'activeHelper_liveHelp_timeByChatEmailEditor');
add_action('admin_head', 'activeHelper_liveHelp_timeByChatEmailEditorHead');
add_filter('mce_buttons', 'activeHelper_liveHelp_timeByChatEmailEditorButtons');
}
function activeHelper_liveHelp_timeByChatEmailEditorButtons($buttons)
{
array_unshift($buttons, 'fullscreen');
return $buttons;
}
function activeHelper_liveHelp_timeByChatEmailEditorHead()
{
wp_tiny_mce(false);
}
function activeHelper_liveHelp_timeByChatEmailEditor()
{
global $activeHelper_liveHelp;
wp_enqueue_script(array('jquery', 'editor', 'thickbox', 'media-upload'));
wp_enqueue_style('thickbox');
}
function activeHelper_liveHelp_timeByChatEmail()
{
global $activeHelper_liveHelp;
if (!empty($activeHelper_liveHelp['errors']))
$errors = $activeHelper_liveHelp['errors'];
$tabindex = 1;
echo '
LiveHelp » ' . __('Time by chat', 'activehelper_livehelp') . ' » Send by email
';
if (isset($_GET['email']))
echo '
' . __('The email has been sent. Would you like to send it again to another recipient?', 'activehelper_livehelp') . '
';
echo '
';
}
function activeHelper_liveHelp_timeByChatDeletePost()
{
global $wpdb, $activeHelper_liveHelp;
$_REQUEST['id'] = !empty($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
if (empty($_REQUEST['id']))
{
wp_redirect('admin.php?page=' . strtolower('activeHelper_liveHelp_timeByChat') . '&miss');
exit;
}
$wpdb->query("
DELETE FROM {$wpdb->prefix}livehelp_sessions, {$wpdb->prefix}livehelp_messages
USING {$wpdb->prefix}livehelp_sessions
INNER JOIN {$wpdb->prefix}livehelp_messages
WHERE {$wpdb->prefix}livehelp_sessions.id = '{$_REQUEST['id']}'
AND {$wpdb->prefix}livehelp_messages.session = {$wpdb->prefix}livehelp_sessions.id
");
wp_redirect('admin.php?page=' . strtolower('activeHelper_liveHelp_timeByChat') . '&delete');
exit;
}
function activeHelper_liveHelp_timeByChatExportPost()
{
global $wpdb, $activeHelper_liveHelp;
$timeEnd = !empty($_REQUEST['export_end_date']) ? strtotime((string) $_REQUEST['export_end_date']) : time();
$timeStart = !empty($_REQUEST['export_start_date']) ? strtotime((string) $_REQUEST['export_start_date']) : mktime(0, 0, 0, date("n", $timeEnd) - 1, date("j", $timeEnd), date("Y", $timeEnd));
$timeByChatList = $wpdb->get_results("
SELECT
jls.id AS session, CONCAT(jlu.firstname, ' ', jlu.lastname) AS name, jld.name AS domain,
jls.username AS visitor, jls.email AS email, if(jls.rating = -1, '" . __('Not rate', 'activehelper_livehelp') . "', jls.rating) AS rating,
(TIMEDIFF(jls.refresh, jls.datetime)) AS time, DATE_FORMAT(jls.datetime, '%m/%d/%Y') AS date
FROM
{$wpdb->prefix}livehelp_sessions AS jls,
{$wpdb->prefix}livehelp_users AS jlu,
{$wpdb->prefix}livehelp_domains AS jld
WHERE
DATE_FORMAT(jls.datetime, '%Y%m%d') >= DATE_FORMAT('" . date("Y-m-d", $timeStart) . "', '%Y%m%d')
AND DATE_FORMAT(jls.datetime,'%Y%m%d') <=DATE_FORMAT('" . date("Y-m-d", $timeEnd) . "', '%Y%m%d')
AND jls.id_user = jlu.id
AND jls.id_domain = jld.id_domain
GROUP BY jls.id, jls.username
ORDER BY 1 DESC, 2, 3, 4
", ARRAY_A);
$export = '"ID","Agent","Domain name","Visitor name","Visitor email","Raiting","Duration","Date"';
if (!empty($timeByChatList))
foreach ($timeByChatList as $timeByChat)
{
$export .= "\n" . '"' .
$timeByChat['session'] . '","' .
$timeByChat['name'] . '","' .
$timeByChat['domain'] . '","' .
$timeByChat['visitor'] . '","' .
$timeByChat['email'] . '","' .
$timeByChat['rating'] . '","' .
$timeByChat['time'] . '","' .
$timeByChat['date'] .
'"';
}
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=LiveHelp_TimeByChat_" . date("Y-m-d", $timeStart) . "~" . date("Y-m-d", $timeEnd) . ".csv");
header("Content-Type: text/csv; text/comma-separated-values");
header("Content-Transfer-Encoding: binary");
echo $export;
exit();
}
function activeHelper_liveHelp_timeByChatViewExportPost()
{
global $wpdb, $activeHelper_liveHelp;
$_REQUEST['id'] = !empty($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
if (empty($_REQUEST['id']))
{
wp_redirect('admin.php?page=' . strtolower('activeHelper_liveHelp_timeByChat') . '&miss');
exit;
}
$chats = $wpdb->get_results("
SELECT username, message, TIME_FORMAT(jlm.datetime, '%H:%i:%s') AS time
FROM {$wpdb->prefix}livehelp_messages AS jlm
WHERE session = '{$_REQUEST['id']}'
ORDER BY id
", ARRAY_A);
$statistic = $wpdb->get_row("
SELECT
CONCAT(jlu.firstname, ' ', jlu.lastname) AS agent, jls.department, jls.server,
jls.username, jls.email, DATE_FORMAT(jls.datetime, '%Y-%m-%d') AS date,
if(jls.rating = -1, '" . __('Not rate', 'activehelper_livehelp') . "', jls.rating) AS rating,
jlr.country, jlr.city
FROM
{$wpdb->prefix}livehelp_sessions AS jls,
{$wpdb->prefix}livehelp_users AS jlu,
{$wpdb->prefix}livehelp_requests AS jlr
WHERE jls.id = '{$_REQUEST['id']}'
AND jls.id_user = jlu.id
AND jls.request = jlr.id
", ARRAY_A);
$export = '"Domain","Department","Agent","Visitor","Country","City","Email","Date","Rating"';
$export .= "\n" . '"' .
$statistic['server'] . '","' .
$statistic['department'] . '","' .
$statistic['agent'] . '","' .
$statistic['username'] . '","' .
$statistic['country'] . '","' .
$statistic['city'] . '","' .
$statistic['email'] . '","' .
$statistic['date'] . '","' .
$statistic['rating'] .
'"';
$export .= "\n\n" . '"Username","Message","Time"';
if (!empty($chats))
foreach ($chats as $chat)
{
$export .= "\n" . '"' .
$chat['username'] . '","' .
$chat['message'] . '","' .
$chat['time'] .
'"';
}
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=LiveHelp_" . $statistic['server'] . "~" . $statistic['date'] . "_" . urlencode($statistic['agent'] . '~' . $statistic['username']) . ".csv");
header("Content-Type: text/csv; text/comma-separated-values; charset=utf-8");
header("Content-Transfer-Encoding: binary");
echo $export;
exit();
}
function activeHelper_liveHelp_timeByChatListPost()
{
global $wpdb, $activeHelper_liveHelp;
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-datepicker', $activeHelper_liveHelp['baseUrl'] . '/scripts/jquery.ui.datepicker.min.js', array('jquery', 'jquery-ui-core') );
wp_enqueue_style('jquery.ui.theme', $activeHelper_liveHelp['baseUrl'] . '/scripts/smoothness/jquery-ui-1.8.16.custom.css');
}
function activeHelper_liveHelp_timeByChatList()
{
global $wpdb, $activeHelper_liveHelp;
$timeEnd = !empty($_REQUEST['export_end_date']) ? strtotime((string) $_REQUEST['export_end_date']) : time();
$timeStart = !empty($_REQUEST['export_start_date']) ? strtotime((string) $_REQUEST['export_start_date']) : mktime(0, 0, 0, date("n", $timeEnd) - 1, date("j", $timeEnd), date("Y", $timeEnd));
$timeByChatList = $wpdb->get_results("
SELECT
jls.id AS session, CONCAT(jlu.firstname, ' ', jlu.lastname) AS name, jld.name AS domain,
jls.username AS visitor, jls.email AS email, if(jls.rating = -1, '" . __('Not rate', 'activehelper_livehelp') . "', jls.rating) AS rating,
(TIMEDIFF(jls.refresh, jls.datetime)) AS time, DATE_FORMAT(jls.datetime, '%m/%d/%Y') AS date
FROM
{$wpdb->prefix}livehelp_sessions AS jls,
{$wpdb->prefix}livehelp_users AS jlu,
{$wpdb->prefix}livehelp_domains AS jld
WHERE
DATE_FORMAT(jls.datetime, '%Y%m%d') >= DATE_FORMAT('" . date("Y-m-d", $timeStart) . "', '%Y%m%d')
AND DATE_FORMAT(jls.datetime,'%Y%m%d') <=DATE_FORMAT('" . date("Y-m-d", $timeEnd) . "', '%Y%m%d')
AND jls.id_user = jlu.id
AND jls.id_domain = jld.id_domain
GROUP BY jls.id, jls.username
ORDER BY 1 DESC, 2, 3, 4
", ARRAY_A);
echo '
';
}
function activeHelper_liveHelp_timeByChatViewPost()
{
global $wpdb, $activeHelper_liveHelp;
$_REQUEST['id'] = !empty($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
if (empty($_REQUEST['id']))
{
wp_redirect('admin.php?page=' . strtolower('activeHelper_liveHelp_timeByChat') . '&miss');
exit;
}
wp_enqueue_style('dashboard');
}
function activeHelper_liveHelp_timeByChatView()
{
global $wpdb, $activeHelper_liveHelp;
$chats = $wpdb->get_results("
SELECT username, message, TIME_FORMAT(jlm.datetime, '%H:%i:%s') AS time
FROM {$wpdb->prefix}livehelp_messages AS jlm
WHERE session = '{$_REQUEST['id']}'
ORDER BY id
", ARRAY_A);
$statistic = $wpdb->get_row("
SELECT
CONCAT(jlu.firstname, ' ', jlu.lastname) AS agent, jls.department, jls.server,
jls.username, jls.email, DATE_FORMAT(jls.datetime, '%Y-%m-%d') AS date,
if(jls.rating = -1, '" . __('Not rate', 'activehelper_livehelp') . "', jls.rating) AS rating,
jlr.country, jlr.city
FROM
{$wpdb->prefix}livehelp_sessions AS jls,
{$wpdb->prefix}livehelp_users AS jlu,
{$wpdb->prefix}livehelp_requests AS jlr
WHERE jls.id = '{$_REQUEST['id']}'
AND jls.id_user = jlu.id
AND jls.request = jlr.id
", ARRAY_A);
echo '
';
}