<?php
/**
 * Project module
 * 
 * Project detail page
 *  
 * @package linea21\modules\project
 * @author $Author$ - linea21 <info@linea21.com>
 * @version $Id$ 
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

//////////// Check Inclusion de pages ////////////
if (!class_exists('auth')) {
  include_once("../lib/lib_common.php");
  ReloadIndex('admin');
}
////////////

//////////// Check Droits utilisateur ////////////
if (!$l21auth->hasRight('project')) ReloadIndex('admin');
////////////

if (isset($_GET['id'])) $id = $_GET['id'];
if (isset($_POST['id'])) $id = $_POST['id'];

// including sparkline code
footerAddJS('../lib/js/jquery.sparkline.min.js');
footerAddInlineJS("$('.inlinebar').sparkline('html', {type: 'bar', barColor: '#8DA8CB'} );");


// including html2canvas to render and save as image
footerAddJS('../lib/js/html2canvas/dist/html2canvas.js');
$matched_id = 'project-'.$id.'-dashboard';
$str = '
		var array_images = [];
		var array_names = [];

		if(document.getElementById("'.$matched_id.'")) {
			html2canvas(document.getElementById("'.$matched_id.'"), {
        	onrendered: function(canvas) {
            var canvasImg = canvas.toDataURL("image/png");
						array_images.push(canvasImg);
						array_names.push("'.$matched_id.'");
						$.post("../library/save-image.php", 
									{ images: array_images, names:array_names },
									function(msg) { // success
										// console.log("Image has been saved as '.$matched_id.'.png");
									}
						);
        	}
    		})
			};';
footerAddInlineJS($str);

$req_det = SQL_getoneCompleteProject($id);
$result = $sql_object->DBSelect($req_det);

// Do record exists?
if(!isset($result[0]['project_name'])) redirect_to('@module_default');

// Option [1] Only  Getting Indicators list with links
// $query = SQL_getProjectSdi($id);
// $data = $sql_object->DBSelect($query);

// if($data != 0 || count($data)!=1) {
//   $sdi_list='<ul style="padding:0">';
//   foreach ($data as &$value) {
//     $sdi_list.= '<li><a href="index.php?rub=sdi&amp;todo=det&amp;id='.$value['sdii_id'].'">'.formatText($value['sdii_name'], '2HTML').'</a></li>'.PHP_EOL;
//   }
//   $sdi_list.='</ul>';
// } else $sdi_list=_t('divers','none');

// Option [2] Getting indicators values
include_once(override('../project/display.php'));
$sdi_list = getAssociatedIndicatorsValues($id);
$coordinator = getAssociatedActors($id, 'coordinator');
$partners = getAssociatedActors($id, 'partner');
$manager = getAssociatedManagers($id);

// getting tags
include_once('../class/class.tag.php');
$otag = new tag();
$display_tags = empty_none($otag->getTags('project', $id, true));


$project_object = new project;

$display_name = formatText($result[0]['project_name'], '2HTML');
$display_priority = formatText($result[0]['project_priority'], '2HTML');
if(!is_numeric($result[0]['project_parent_id']) || $result[0]['project_parent_id']==0) {
  $display_parent = _t('divers','none');
} else {
  $display_parent = '<a href="'.$rub_link .'&amp;todo=det&amp;id='.$result[0]['project_parent_id'].'" title="'._t('project','det').' '.formatText($result[0]['parent_name'], '2ALT').'">'.formatText($result[0]['parent_name'], '2HTML').'</a>';
}
$display_goal = empty_nc($result[0]['project_goal']);
$display_description = formatText(empty_none($result[0]['project_description']), '2HTML');
$display_body = formatText($result[0]['project_body'], '2HTML');
$display_comment = empty_none(formatText($result[0]['project_comment'], '2HTML'));
$display_budget = empty_nc($result[0]['project_budget']);
$display_budget_comment = empty_nc($result[0]['project_budget_comment']);
$display_coordinator = empty_nc($coordinator);
$display_elected = empty_nc($result[0]['project_elected']);
$display_team = empty_nc($result[0]['project_team']);
$display_partners = empty_nc($partners);
$display_manager = empty_nc($manager);
$display_level = formatText($result[0]['level_name'], '2HTML');
$display_workshop = formatText($result[0]['workshop_denomination'], '2HTML');
$display_scale = formatText($result[0]['scale_denomination'], '2HTML');
$display_user = formatText($result[0]['user_login'], '2HTML');
$display_begin_date = empty_nc($result[0]['project_begin_date_display']);
$display_estimated_date = empty_nc($result[0]['project_estimated_date_display']);
$display_end_date = empty_nc($result[0]['project_end_date_display']);
$display_progression = $result[0]['project_completed'];
$display_date_crea = $result[0]['project_date_crea_display'];
$display_last_modify = $result[0]['project_last_modify_display'];
$project_posted_by = $result[0]['project_posted_by'];
$project_scale_id = $result[0]['project_scale_id'];
$project_level_id = $result[0]['project_level_id'];
$project_workshop_id = $result[0]['project_workshop_id'];

$display_finalities=getFinalities($id, 'read');

$display_statut = display_statut($result[0]['project_statut']);
if (formatDate($result[0]['project_published_date_display'], true) == formatDate('0001-01-01', true)) $display_published_date = _t('statut','draft');
else $display_published_date = $result[0]['project_published_date_display'];

$link_sup = $rub_link . "&amp;todo=sup&amp;id=";
$link_mod = $rub_link . "&amp;todo=mod&amp;id=";

?>
<div id="chemin">
<ul>
	<li><a href="index.php" class="chemin_home"> <?php echo _t('way','home'); ?></a></li>
	<li><a href="<?php echo $rub_link; ?>" id="chemin_<?php echo $rub; ?>" class="chemin_rub"> <?php echo _t('menu','project'); ?> </a></li>
	<li><span><?php echo formatNavTitle(_t('project','det') . " : " . $display_name); ?></span></li>
</ul>
</div>
<!--end barre haute -->

<div class="contentcontainer"><?php
include_once(override($dir.'/menurub.php', THEME_ADMIN_PATH));

?>
<div id="content"><?php
echo "<h2>";
echo formatTitleh2($display_name);
echo "</h2>\n";
?>

<!--  Unique value export -->
<form id="report" action="../export/report.php" method="post" class="visually-hidden">
<input type="hidden" name="what" value="project">
<input type="hidden" name="quick_export" value="1">
<input type="hidden" name="projects" value="det">
<input type="hidden" name="projects_id[]" value="<?php echo $id; ?>">
<input type="hidden" name="dashboard_values" value="1">
<input type="hidden" name="format_report" value="word"><!-- pdf / word / html -->
<input type="hidden" name="sauver" value="1">
</form>
<!-- dropdown container -->
<div class="dropdown export-button">
<!-- trigger button -->
<button><i class="icon-export"></i> <?php echo _t('export', 'action'); ?></button>
    <!-- dropdown menu -->
    <ul class="dropdown-menu">
        <li><a href="#report" data-format="pdf"><i class="icon-file-pdf"></i> <?php echo _t('export', 'format', 'pdf'); ?></a></li>
        <li><a href="#report" data-format="word"><i class="icon-file-word"></i> <?php echo _t('export', 'format', 'word'); ?></a></li>
        <li><a href="#report" data-format="html"><i class="icon-html5"></i> <?php echo _t('export', 'format', 'html'); ?></a></li>
    </ul>
</div>
<script>
$( "div.export-button li a" ).click(function() {
	var format = $(this).attr('data-format');
	$( "#report input[name=format_report]").val(format);
  $( "#report" ).submit();
  return false;
});
</script>

<?php
if ($l21auth->isSuperAdmin()) echo "\t<a href=\"" . $link_sup . $id . "\" class=\"ico_sup2\" title=\"" . _t('project','sup') . "\"><i class=\"icon-trash\"></i><span>" . _t('project','sup') . "</span></a>\n";
if ($l21auth->isProjectManager($l21auth->GetSessionElement('id'), $sql_object, $id) || $l21auth->isSuperAdmin()) echo "\t<a href=\"" . $link_mod . $id . "\" class=\"ico_mod2\" title=\"" . _t('project','mod') . "\"><i class=\"icon-pencil\"></i><span>" . _t('project','mod') . "</span></a>\n";
?>

<?php
echo "<dl class=\"summary\">\n";
echo "<dt>" . mb_ucfirst(_t('project','name')) . " :</dt>\n";
echo "<dd>" . $display_name . "</dd>\n";
echo "<dt>" . mb_ucfirst(_t('project','parent')) . " :</dt>\n";
echo "<dd>" . $display_parent . "</dd>\n";
if(defined('PROJECT_FINALITIES') && PROJECT_FINALITIES == 1) {
	echo "<dt>" . mb_ucfirst(_t('project','finalities')) . " :</dt>\n";
	echo "<dd>" . $display_finalities . "</dd>\n";
}
echo "<dt>" . mb_ucfirst(_t('project','goal')) . " :</dt>\n";
echo "<dd>" . empty_none($display_goal) . "</dd>\n";
echo "<dt>" . mb_ucfirst(_t('project','description')) . " :</dt>\n";
echo "<dd>" . $display_description . "</dd>\n";
echo "<dt>" . mb_ucfirst(_t('project','body')) . " :</dt>\n";
echo "<dd>" . empty_none($display_body) . "</dd>\n";
echo "<dt>" . mb_ucfirst(_t('project','priority')) . " :</dt>\n";
echo "<dd>" . $display_priority . "</dd>\n";
echo "<dt>" . mb_ucfirst(_t('project','level')) . " :</dt>\n";
if($project_level_id != 0) {
  echo '<dd><a href="?rub=level&amp;todo=det&amp;id='.$project_level_id.'">' . $display_level . '</a></dd>'.PHP_EOL;
} else {
  echo "<dd>" . empty_none($display_level) . "</dd>\n";
}
echo "<dt>" . mb_ucfirst(_t('project','workshop')) . " :</dt>\n";
if($project_workshop_id != 0) {
  echo '<dd><a href="?rub=workshop&amp;todo=det&amp;id='.$project_workshop_id.'">' . $display_workshop . '</a></dd>'.PHP_EOL;
} else {
  echo "<dd>" . empty_none($display_workshop) . "</dd>\n";
}
echo "<dt>" . mb_ucfirst(_t('project','scale')) . " :</dt>\n";
echo '<dd><a href="?rub=scale&amp;todo=det&amp;id='.$project_scale_id.'">' . $display_scale . '</a></dd>'.PHP_EOL;
echo "<dt>".mb_ucfirst(_t('tag','name'))." :</dt>\n";
echo "<dd>".$display_tags."</dd>\n";
echo "<dt>" . mb_ucfirst(_t('project','sdi'))." :</dt>\n";
echo "<dd>" . empty_none($sdi_list)."</dd>\n";
echo "<dt>" . mb_ucfirst(_t('project','author')) . " :</dt>\n";
echo '<dd><a href="?rub=user&amp;todo=det&amp;id='.$project_posted_by.'">' . $display_user . '</a></dd>'.PHP_EOL;
echo "<dt>" . mb_ucfirst(_t('project','comment')) . " :</dt>\n";
echo "<dd>" . $display_comment . "</dd>\n";
echo "<dt>" . mb_ucfirst(_t('statut','published_on')) . " :</dt>\n";
echo "<dd>" . $display_published_date . "</dd>\n";
echo "<dt>" . mb_ucfirst(_t('divers','date_crea')) . " :</dt>\n";
echo "<dd>" . $display_date_crea . "</dd>\n";
echo "<dt>" . mb_ucfirst(_t('divers','last_modify')) . " :</dt>\n";
echo "<dd>" . $display_last_modify . "</dd>\n";
echo "<dt>" . mb_ucfirst(_t('divers','statut')) . " :</dt>\n";
echo "<dd>" . $display_statut . "</dd>\n";
echo "</dl>\n";
?>


<h3 class="part"><?php echo mb_ucfirst(_t('project','calendar_title')); ?></h3>
<dl class="summary">
<dt><?php echo mb_ucfirst(_t('project','begin_date')); ?> : </dt>
<dd><?php echo $display_begin_date; ?></dd>
<dt><?php echo mb_ucfirst(_t('project','estimated_date')); ?> : </dt>
<dd><?php echo $display_estimated_date; ?></dd>
<dt><?php echo mb_ucfirst(_t('project','end_date')); ?> : </dt>
<dd><?php echo $display_end_date; ?></dd>
<dt><?php echo mb_ucfirst(_t('project','progression')); ?> : </dt>
<dd><?php echo getDynamicProgressbar($display_progression); ?></dd>
</dl>

<h3 class="part"><?php echo mb_ucfirst(_t('project','steering_title')); ?></h3>
<dl class="summary">
<dt><?php echo mb_ucfirst(_t('project','elected')); ?> : </dt>
<dd><?php echo $display_elected; ?></dd>
<dt><?php echo mb_ucfirst(_t('project','coordinator')); ?> : </dt>
<dd><?php echo $display_coordinator; ?></dd>
<dt><?php echo mb_ucfirst(_t('project','manager')); ?> : </dt>
<dd><?php echo $display_manager; ?></dd>
<dt><?php echo mb_ucfirst(_t('project','partners')); ?> : </dt>
<dd><?php echo $display_partners; ?></dd>
<dt><?php echo mb_ucfirst(_t('project','team')); ?> : </dt>
<dd><?php echo $display_team; ?></dd>
</dl>

<h3 class="part"><?php echo mb_ucfirst(_t('project','budget_title')); ?></h3>
<dl class="summary">
<dt><?php echo mb_ucfirst(_t('project','budget')); ?> : </dt>
<dd class="money"><?php echo empty_nc(fnumber_format($display_budget,0, false)); ?></dd>
<dt><?php echo mb_ucfirst(_t('project','budget_comment')); ?> : </dt>
<dd><?php echo $display_budget_comment; ?></dd>
</dl>

</div> <!-- closing #content -->
</div> <!-- closing .contentcontainer -->