<?php 
require_once "../tool/projeqtor.php";
require_once "../tool/formatter.php";
$categ=null;
if (isset($_REQUEST['idCategory'])) {
  $categ=$_REQUEST['idCategory'];
}
$lstIdOfSubCateg=null;
if(RequestHandler::isCodeSet('lstRepId')){
  $lstIdOfSubCateg=RequestHandler::getValue('lstRepId');
  $namSubCateg=RequestHandler::getValue('nameSubCat');
}
$hr=new HabilitationReport();
$user=getSessionUser();
$allowedReport=array();
$allowedCategory=array();
$lst=$hr->getSqlElementsFromCriteria(array('idProfile'=>$user->idProfile, 'allowAccess'=>'1'), false);
foreach ($lst as $h) {
  $report=$h->idReport;
  $nameReport=SqlList::getNameFromId('Report', $report, false);
  if (! Module::isReportActive($nameReport)) continue;
  $allowedReport[$report]=$report;
  $category=SqlList::getFieldFromId('Report', $report, 'idReportCategory',false);
  $allowedCategory[$category]=$category;
}

if (!$categ) {
  echo "<div class='messageData headerReport' style= 'white-space:nowrap;margin-top:5px'>";
  echo ucfirst(i18n('colCategory'));
  echo "</div>";
  $listCateg=SqlList::getList('ReportCategory');
  echo "<ul class='bmenu'>";
  foreach ($listCateg as $id=>$name) {
    if (isset($allowedCategory[$id])) {
      echo "<li class='sectionCategorie' onClick='loadDiv(\"../view/reportListMenu.php?idCategory=$id\",\"reportMenuList\");'><div class='bmenuCategText'>$name</div></li>";
    }
  }
  echo "</ul>";
} else {
  $catObj=new ReportCategory($categ);
  $title=i18n($catObj->name);
  if($lstIdOfSubCateg)$title=i18n($namSubCateg);
  echo "<div class='messageData headerReport' style= 'white-space:nowrap;margin-top:5px'>";
  echo $title;
  echo "</div>";
  echo "<div class='arrowBack' style='position:absolute;top:5px;left:25px;'>";
  if($lstIdOfSubCateg)$undo="loadDiv(\"../view/reportListMenu.php?idCategory=$categ\",\"reportMenuList\")";
  else $undo="loadDiv(\"../view/reportListMenu.php\",\"reportMenuList\")";
  echo "<span class='dijitInline dijitButtonNode backButton noRotate'  onClick='$undo' style='border:unset;'>";
  if(isNewGui()){
    echo formatNewGuiButton('Back', 22);
  }else{
    echo formatBigButton('Back'); 
  }
  echo "</div>";
  echo '</span>';
  
  $report=new Report();
  $res=array();
  if(!$lstIdOfSubCateg){
    $crit=array('idReportCategory'=>$categ);
    $listReport=$report->getSqlElementsFromCriteria($crit, false, null, 'sortOrder asc');
    
    $nameOfFiles=SqlList::getListWithCrit("Report","idReportCategory = $categ","file");
    $lstReportName=array();
    $lstNewListReport=array();
    foreach ($nameOfFiles as $idN=>$nameFile){
      $lstReportName[]=pq_substr($nameFile, 0,pq_strpos($nameFile, '.php'));
    }
    $countNameFil=array_count_values($lstReportName);
    foreach ($countNameFil as $name=>$val){
      if($val==1)unset($countNameFil[$name]);
      else $lstNewListReport[]=$name;
    }
    storReportsView($listReport, $res, $lstNewListReport, 0);
  }else{
    $where="idReportCategory=$categ and id in ($lstIdOfSubCateg) ";
    $listReport=$report->getSqlElementsFromCriteria(null, false, $where, 'sortOrder asc');
  }
  
  echo "<ul class='bmenu report' style=''>";
  if(!empty($res)){
    foreach ($res as $rpt) {
      $id=$rpt['object']['id'];
      $name=$rpt['object']['name'];
      if($rpt['level']==0 and $rpt['objectType']=='reportSubMenu'){
        $lstRepId=$rpt['object']['lstRepId'];
        if($name=='../tool/jsonPlanning')$name='GanttPlan';
        echo "<li class='sectionCategorie' onClick='loadDiv(\"../view/reportListMenu.php?idCategory=$categ&lstRepId=$lstRepId&nameSubCat=$name\",\"reportMenuList\");'><div class='bmenuCategText'>".i18n($name)."</div></li>";
      }else{
        if (isset($allowedReport[$id])) {
          echo "<li class='section' id='report$id' onClick='reportSelectReport($id);'><div class='bmenuText'>".ucfirst(i18n($name))."</div></li>";
        }
      }
    }
  }else{
    foreach ($listReport as $rpt) {
      if (isset($allowedReport[$rpt->id])) {
        echo "<li class='section' id='report$rpt->id' onClick='reportSelectReport($rpt->id);'><div class='bmenuText'>".ucfirst(i18n($rpt->name))."</div></li>";
      }
    }
  }
  echo "</ul>";
}  



function storReportsView($listReport, &$res, $lstNewListReport, $level ) { //store report
  $count=array();
  $isNewPId=array();
  foreach ($listReport as $id=>$report){
    if($report->id=="108" and !Module::isModuleActive('moduleTechnicalProgress'))continue;
    $file=pq_substr($report->file, 0,pq_strpos($report->file, '.php'));
    if(in_array($file, $lstNewListReport)){
      if(!isset($count[$file])){
        $count[$file]=1;
        $keyParent=$level.'-'.numericFixLengthFormatter($report->sortOrder,5);
        $isNewPId[$file]=$report->sortOrder;
        $obj= array('id'=>$isNewPId[$file],'name'=>ucfirst($file),'lstRepId'=>$report->id);
        $res[$keyParent]=array('level'=>$level,'objectType'=>'reportSubMenu','object'=>$obj);
      }else{
        $key=$level.'-'.numericFixLengthFormatter($isNewPId[$file],5);
        $res[$key]['object']['lstRepId'].=','.$report->id;
      }
    }else{
      $keyParent=$level.'-'.numericFixLengthFormatter($report->sortOrder,5);
      $obj= array('id'=>$report->id,'name'=>$report->name);
      $res[$keyParent]=array('level'=>$level,'objectType'=>'reportDirect','object'=>$obj);
    }
  }
}
?>