<?php
// Make sure we can't access this file directly from the browser.
if(!defined('IN_MYBB'))
{
	die('This file cannot be accessed directly.');
}


// my first hook!
$plugins->add_hook("global_start", "valck_run");

function valck_info() 
{


/* Versioning of files - dev */
// probleme de droit corrigé ajout ck to nginx group puis 770 / ck & nginx
$versionAPP = exec('cd /home/ck/AddOn_Mybb/ckval/ && git rev-parse --short HEAD', $returnval );
$versionAPP_long = exec('cd /home/ck/AddOn_Mybb/ckval/ && git rev-parse HEAD', $returnval );

// a retravailler voir : https://stackoverflow.com/questions/7447472/how-could-i-display-the-current-git-branch-name-at-the-top-of-the-page-of-my-de


// descrition of plugin	
$ckdescription = <<<EOT
Var for badge about treads count to index <br />
<span style="display: inline-block; margin: 2px 0; padding: 4px; background-color:mediumseagreen; font-size: 9px; color: #fff">
Version du git <a href="https://github.nailis.fr/ckbox/ckval/commit/{$versionAPP_long}" target="_blank" >{$versionAPP_long}</a>
</span>
EOT;

	return array(
		'name'			=> "valck",
		'description'	=> $ckdescription,
		'website'		=> "https://www.ckforum.com",
		'author'			=> "ck",
		'authorsite'		=> "http://github.nailis.fr/ckbox/ckval/-/blob/master/upload/inc/plugins/valck.php",
		'version'		=> "1.3.6 - ".$versionAPP,
		'compatibility' => "18*"
	);
}

// function execute by index
function valck_run()
{
global $mybb, $lang, $db, $valck_run;

			// requete pour les nouveaux messages like getdaily
			// Get number of new posts, threads
			// via https://community.mybb.com/thread-140350-post-1182934.html#pid1182934
			// requete pour le badge message aujourdui
				if($mybb->get_input('days', MyBB::INPUT_INT) < 1)
				{
					$days = 1;
				}
				else
				{
					$days = $mybb->get_input('days', MyBB::INPUT_INT);
				}
					$datecut = TIME_NOW-(86400*$days);

					$sql = "
					SELECT count(*) FROM `".TABLE_PREFIX."threads` 
					where lastpost >= ".$datecut." 
					and visible >= 0
					ORDER BY `tid` DESC";
					
					$query 	= $db->query("
					SELECT count(*) as count FROM `" . TABLE_PREFIX . "threads` 
					where lastpost >= ".$datecut." 
					and visible >= 0
					ORDER BY `tid` DESC");
					
					$newposts = $db->fetch_field($query, "count");
					$db->free_result($query);
			// fin requete message aujourdui


			if ($newposts > 0) {
			// Get new treads  - nouveau sujet like search getnew
            // If there aren't any new posts, there is no point in wasting two more queries
				$query = $db->simple_select("threads", "COUNT(tid) AS newthreads", "visible=1 AND dateline>'".$mybb->user['lastvisit']."' $unviewwhere");
				$newthreads = $db->fetch_field($query, "newthreads");
				} else {
				$newthreads = 0;
			}

//si nouveau message alors on badge en rouge
$mybb->user['pms_unread'] > 0 ? $badgecolornewmesg = "danger blink_me" :  $badgecolornewmesg = "warning" ;

// le code pour le forum
$valck_run1 = <<<EOT
<li>
	<div class="ck-b1">
		<a href="{$mybb->settings['bburl']}/search.php?action=getdaily">
			<h5><span data-toggle="tooltip" title="{$lang->welcome_todaysposts}" 
			class="align-middle badge badge-primary	 badge-pill shadow">{$newposts}</span></h5>
		</a>
	</div>
</li>
EOT;
$valck_run2 =<<<EOT
<li>
	<div class="ck-b2">
		<a href="{$mybb->settings['bburl']}/search.php?action=getnew">
			<h5><span data-toggle="tooltip" title="{$lang->welcome_newposts}" 
			class="align-middle badge badge-success	 badge-pill shadow">{$newthreads}</span></h5>
		</a>
	</div>
</li>
EOT;

$valck_run3 =<<<EOT
<li>
	<div class="ck-b3">
		<a href="{$mybb->settings['bburl']}/private.php">
			<h5><span data-toggle="tooltip" title="Message privé non lu!" 
			class="align-middle badge badge-{$badgecolornewmesg} badge-pill shadow">{$mybb->user['pms_unread']}</span></h5>
		</a>
	</div>
</li>
EOT;

$valck_run4=<<<EOT
<li>
	<div class="ck-b4">
	<a href="{$mybb->settings['bburl']}/private.php">
		<h5><span data-toggle="tooltip" title="Tous les messages Privés" 
		class="align-middle badge badge-info badge-pill shadow">{$mybb->user['totalpms']}</span></h5>
	</a>
	</div>
</li>
EOT;

$valck_run[1] = $valck_run1;
$valck_run[2] = $valck_run2;
$valck_run[3] = $valck_run3;
$valck_run[4] = $valck_run4;

return $valck_run;
}