/*   -*- coding: utf-8 -*-  */
/***************************************************************************************

   Module   : Explor
   Fichier  : ExplorGenerPhpProc.c

   set of functions for generating code in html pages
   see also: ExplorPhpTools.c
************************************************************************************/

#include "Explor.h"
#include "Wicri.h"
#include "Except.h"

void ExplorSiteGenerShellExplorVar(char *stream, char *step)
{
  char *dir;
  char *wiki;
  char *area;
  printf("       echo  \"<pre>\\nEXPLOR_STEP=\";\n");
  wiki=ExplorGetFromDict("Area/wikiCode");
  dir=ExplorGetFromDict("Area/areaDirPath");
 
  if(wiki)
    {
      printf("       echo \"\\$WICRI_ROOT/%s/explor/\";\n;", wiki);
    }
  else if (dir) 
    {
      if (dir[0]=='$')printf("       echo \"\\%s/\";\n;", dir);
      else printf("       echo \"%s/\";\n;", dir);
    }
  printf("       echo  \"%s/Data/%s/%s\\n</pre>\n\";", ExplorAreaCode, stream, step);
}

void ExplorSiteGenerExplorLien(char *stream, char *step)
{
  char *wiki;
  printf ("        echo \"<pre>{{Explor lien\\n\";\n" );
  wiki=ExplorGetFromDict("Area/wikiCode");
  if (wiki)   printf ("        echo \"   |wiki=    %s\\n\";\n", wiki );
  else  printf ("        echo \"   |wiki=   *** parameter Area/wikiCode missing *** \\n\";\n");
  printf ("        echo \"   |area=    %s\\n\";\n", ExplorAreaCode);
  printf ("        echo \"   |flux=    %s\\n\";\n", stream);
  printf ("        echo \"   |étape=   %s\\n\";\n", step);
}

void ExplorSiteGenerUnitIndex (char *lang, char *stream, char *step, int treeCode)
{
 printf ("<?php \n $ExplorParamIndex=htmlspecialchars($_GET[\"index\"]);\n ");
  printf ("$indexParamKey=\"shortNameIndex\". $ExplorParamIndex;\n");
  printf("$ExplorParamIndexShortName=ExplorGetParamK5($ExplorParamTable, \"Area\", \"%s\", \"%s\", \"indexHead.php\",  $indexParamKey);\n", stream, step);
  /* printf ("$ExplorDomTree=ExplorGetDomAreaTree($ExplorParamTable);\n"); */
  printf ("$ExplorIndexTreeNode= ExplorAreaTreeGetNodeFromIndex($ExplorDomAreaTree,\"%s\", \"%s\",$ExplorParamIndex);\n", stream, step);
  printf ("echo '<!--  ';\n");
  printf ("echo $ExplorDomAreaTree->saveXML($ExplorIndexTreeNode);\n");
  printf ("echo '    -->      ';\n");
  printf("$ExplorParamIndexName=ExplorTreeNodeGetName($ExplorIndexTreeNode);\n");
  printf("$ExplorParamIndexShortName=ExplorTreeNodeGetShortName($ExplorIndexTreeNode);\n");
  printf ("?>\n");
}

int ExplorSiteGenerIndexHead (char *lang, char *stream, char *step, int treeCode)
{
  SxmlNode *stepTreeNode;
  stepTreeNode=ExplorSiteTreeNode(stream, step, NULL);
  printf("<?dilib file=\"%s/Site/%s/%s/%s/indexHead.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);
  ExplorSiteGenerUnitIndex (lang, stream, step, treeCode);
  printf ("<title>%s, %s, %s, indexHead, <?php echo $ExplorParamIndex; ?></title>\n", ExplorAreaCode, stream, step);
  ExplorHtmlBeginBody();
  ExplorSidebar(stream, step, EXPLOR_PHP_TYPE_INDEX_HEAD,  "indexHead.php");
  /* ExplorHtmlHeader( stream, step, "indexHead.php", NULL, NULL); */
  ExplorHeaderPhpBegin(stepTreeNode, "indexHead.php");
  ExplorPhpPrintParam4("Area", stream, step, "headerLine2@indexHead.php" );
  ExplorHeaderPhpEnd(stepTreeNode, "indexHead.php");
  printf("<div class=\"main\">");
  ExplorSitePhpIntro();
  printf (" echo \"<h2>Index : \".$ExplorParamIndexShortName.\"</h2>\";\n");
  printf("IndexHeadPrintListCluster($ExplorParamTable, \"%s\", \"%s\", $ExplorParamIndex);\n", stream, step);
  printf("ExplorPrintIndexAccess($ExplorParamIndex);\n");
  printf (" echo \"<h3>Les termes de plus forte occurence</h3>\";\n");
  printf ("IndexHeadPrintSortedList($ExplorParamTable, $ExplorIndexTreeNode, $ExplorParamIndex, \"%s\",\"%s\",\"indexHead.php\");\n", stream, step);
  
 printf ("        echo \"<hr/><h3>Manipulations en shell (Unix/Dilib)</h3>\";\n" );
 printf ("        echo \"<pre>\\nHfdCat %s/Data/%s/%s/\".$ExplorParamIndex.\".hfd", ExplorAreaDir, stream, step);
  printf ("| SxmlCut idx/l | grep ...  \\n</pre>\n\";");

  ExplorSitePhpEnd();
  ExplorHtmlBottomDilib(0);
  printf("</div>");
  ExplorHtmlEndBody();
  return 0;
}

int ExplorSiteGenerAssocHead (char *lang, char *stream, char *step, int treeCode)
{
  printf("<?dilib file=\"%s/Site/%s/%s/%s/assocHead.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);
  printf ("<?php \n $ExplorParamAssoc=htmlspecialchars($_GET[\"assocCode\"]); ");
  printf (" $ExplorParamOffset=0;\n");
  printf (" $ExplorParamLimit=50;\n");
  printf ("?>\n");
  printf ("<title>%s, %s, %s, assocHead, <?php echo $ExplorParamAssoc; ?></title>\n", ExplorAreaCode, stream, step);
  ExplorHtmlBeginBody();
  ExplorSidebarTop(treeCode);
  ExplorSidebarBottom();
  ExplorHtmlHeader( stream, step, "assocHead.php", NULL, NULL);
  printf("<div class=\"main\">");
  ExplorSitePhpIntro();
  printf (" echo \"<h3>Les associations de l\'index \".$ExplorParamAssoc.\"</h3>\";\n");
  /*  printf(" echo ExplorGetIndexName($ExplorParamTable,$ExplorParamAssoc);\n");*/
  printf ("AssocHeadPrintSortedList($ExplorParamTable, $ExplorParamAssoc, \"%s\",\"%s\",\"assocHead.php\",$ExplorParamOffset,$ExplorParamLimit);\n", stream, step);
  ExplorSitePhpEnd();
  ExplorHtmlBottomDilib(0);
  printf("</div>");
  ExplorHtmlEndBody();
  return 0;
}

int ExplorSiteGenerClusterHead (char *lang, char *stream, char *step, int treeCode)
{
  printf("<?dilib file=\"%s/Site/%s/%s/%s/clusterHead.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);
  printf ("<?php \n $ExplorParamCluster=htmlspecialchars($_GET[\"clusterCode\"]); ");
  printf ("$ExplorParamIndex=ExplorGetParamK5($ExplorParamTable, \"Area\", \"%s\", \"%s\",  $ExplorParamCluster, \"fromIndex\");\n", stream, step);
printf ("$indexParamKey=\"shortNameIndex\". $ExplorParamIndex;\n");
 printf("$ExplorParamIndexShortName=ExplorGetParamK5($ExplorParamTable, \"Area\", \"%s\", \"%s\", \"indexHead.php\",  $indexParamKey);\n", stream, step);
  printf ("?>\n");
  printf ("<title>%s, %s, %s, clusterHead, <?php echo $ExplorParamCluster.\"(\". $ExplorParamIndex.\")\" ;  ?></title>\n", ExplorAreaCode, stream, step);
  ExplorHtmlBeginBody();
  ExplorSidebar(stream, step, EXPLOR_PHP_TYPE_CLUSTER_HEAD,  "clusterHead.php");
  ExplorHtmlHeader( stream, step, "clusterHead.php", NULL, NULL);
  printf("<div class=\"main\">");
  ExplorSitePhpIntro();
  printf (" echo \"<h3>Les clusters : \".$ExplorParamCluster.\"</h3>\";\n");
  printf ("ClusterHeadPrintSortedList($ExplorParamTable, $ExplorParamCluster, \"%s\",\"%s\",\"clusterHead.php\");\n", stream, step);
  ExplorSitePhpEnd();
  ExplorHtmlBottomDilib(0);
  printf("</div>");
  ExplorHtmlEndBody();
  return 0;
}

void ExplorSiteHeadListBiblio(char *lang, char *stream, char *step, char *page)
{
  ExplorPhpPrintParam5("Area", stream, step, page, "titleListBiblio" );
  ExplorPhpPrintParam5("Area", stream, step, page, "messLenghtListBiblio" );
  if (strcmp(page, "indexItem.php")==0) 
    {
      printf ("IndexItemNewCall($ExplorParamTable, $ExplorCallPageIndexItem, $ExplorLenghtListBiblio, $ExplorParamFrom, $ExplorParamSize);\n");
    }
  printf ("echo \"<table><colgroup/><colgroup/><colgroup style=\\\"width:50%c;\\\"/><tr><th>\";\n", '%');
  ExplorPhpPrintParam5("Area", stream, step, page, "titleIdentCol" );
  printf ("echo \"</th><th>\";\n");
  ExplorPhpPrintParam5("Area", stream, step, page, "titleAuthorCol" );
  printf ("echo \"</th><th>\";\n");
  ExplorPhpPrintParam5("Area", stream, step, page, "titleTitleCol" );
  printf ("echo \"</th></tr>\";\n");
}

void ExplorSiteGenerIndexItemFistLine(char *phpProc)
{
  printf(" $ExplorIndexHcs=HfdGetDomHcs($ExplorIndexPath); ");
  printf ("$domIndex=HfdGetXmlDomFromIndexKey($ExplorParamTable, $ExplorIndexPath, $ExplorParamKey); \n");
  printf(" $ExplorIndexRecordPreviousHfdKey=HfdHcsPreviousHfdKey($ExplorIndexHcs->documentElement, $ExplorParamTable[\"#HfdKey\"]);\n");
  printf ("        $ExplorIndexNextRecordKey=false;\n");

  printf ("\n       if ($ExplorIndexRecordPreviousHfdKey) \n      {\n");
  printf (" $indexPreviousRecord= HfdGetXmlDomFromNumKey($ExplorParamTable, $ExplorIndexPath, $ExplorIndexRecordPreviousHfdKey  );\n");
  printf ("    $ExplorIndexPreviousRecordKey=$indexPreviousRecord->documentElement->firstChild->nodeValue;\n");
  printf ("    $ExplorAnchorToPreviousIndex= \" <a href=\\\"%s.php?index=\".$ExplorParamIndex.\"&key=\".urlencode($ExplorIndexPreviousRecordKey).\"\\\">\";", phpProc);
  printf ("\n       echo $ExplorAnchorToPreviousIndex.$ExplorIndexPreviousRecordKey.\"</a> <b>&lt;</b> \";\n");
  printf ("\n       }\n");

  printf ("if ($domIndex) \n   {\n");
  printf ("\n         echo \"<b>\".$ExplorParamKey ;\n");
  printf("           $ExplorIndexRecordNextHfdKey=HfdHcsNextHfdKey($ExplorIndexHcs->documentElement, $ExplorParamTable[\"#HfdKey\"]);\n");
  printf ("\n       if ($ExplorIndexRecordNextHfdKey)  \n    {\n      echo \" &lt;</b>\";\n");
  printf ("         $indexNextRecord= HfdGetXmlDomFromNumKey($ExplorParamTable, $ExplorIndexPath, $ExplorIndexRecordNextHfdKey  );\n");
  printf ("        $ExplorIndexNextRecordKey=$indexNextRecord->documentElement->firstChild->nodeValue;\n");
  printf ("\n       }\n");
  printf ("\n       else  echo \"</b>\\n\";\n");
  printf ("\n       }\n");
  printf ("\n       else\n    {\n");
  printf ("\n       $ExplorIndexNextRecordKey= $ExplorParamTable[\"#HfdIndexKey\"]; \n");
  printf ("\n       echo \" **** </b> key : \".$ExplorParamKey.\"  <b>not founded</b> \";\n"); 
  printf ("\n       echo \"**** <b>&lt;</b>\";\n");
  printf ("\n       }\n");

  printf ("\n       if ($ExplorIndexNextRecordKey) \n      {\n");

  /* printf (" echo  $indexNextRecord->saveXML();\n"); */
  printf ("    $ExplorAnchorToNextIndex= \" <a href=\\\"%s.php?index=\".$ExplorParamIndex.\"&key=\".urlencode($ExplorIndexNextRecordKey).\"\\\">\";",phpProc);
  printf ("\n       echo $ExplorAnchorToNextIndex.$ExplorIndexNextRecordKey.\"</a>\";\n");
  printf ("\n       }\n");
  printf ("\n       echo \"\\n\";\n");
}

void ExplorSiteGenerIndexItemFacet  (SxmlNode *stepTreeNode)
{
  int hasFacet;
  SxmlNode *stepUnit;

  /*
  printf ("        echo \"<a href=\\\"\";\n" );
  printf ("        echo \"indexFacet.php?index=\".$ExplorParamIndex.\"&key=\".$ExplorParamKey.\"\";\n"); 
  printf ("        echo \"\\\">\";\n" );  
  printf ("        echo \"facette auteur\";\n" ); 
  printf ("        echo \"</a>\";\n" ); 
  */
  hasFacet=0;
  SxmlReset(stepTreeNode);
  while ((stepUnit=SxmlNextNode(stepTreeNode)))
    {
      if ((SxmlNodeHasName(stepUnit,"facet")))
	{
	  char *codeFacet;
	  if (hasFacet==0)
	    {
	      hasFacet=1;
	      /*	      printf("echo \"\\n<form method=\\\"GET\\\" action=\\\"indexFacet.php\\\">\\n\";\n"); */
	      printf("echo \"\\n<form action=\\\"indexFacet.php\\\">\\n\";\n");
	      printf("echo \"<select name=\\\"facet\\\">\\n\";\n");
	    }
	  codeFacet= SxmlGetAttribute(stepUnit,"code");
	  printf ("echo \"<option value=\\\"%s\\\">%s</option>\\n\";\n", codeFacet, codeFacet);
	}
    }
  if (hasFacet==1)
    {
      printf("echo \"</select>\";\n");
      printf("echo \"<input type=\\\"submit\\\" value=\\\"Submit\\\">\\n\";\n");
      printf("echo \"<input type=\\\"hidden\\\" name=\\\"index\\\" value=\\\"\".$ExplorParamIndex.\"\\\">\\n\";\n" );
      printf("echo \"<input type=\\\"hidden\\\" name=\\\"key\\\" value=\\\"\".$ExplorParamKey.\"\\\">\\n\";\n" );

      printf("echo \"</form>\";\n");
    }
}

int ExplorSiteGenerIndexItem (char *lang, char *stream, char *step, int treeCode)
{
  SxmlNode *stepTreeNode;
  SxmlNode *stepUnit;
  int hasFacet;

  stepTreeNode=ExplorSiteTreeNode(stream, step, NULL);
  printf("<?dilib file=\"%s/Site/%s/%s/%s/indexItem.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);
  printf("   <!-- the pho file is generated by ExplorGenerPhpProc / ExplorSiteGenerIndexItem -->\n");

  printf ("<?php \n");
  printf ("$ExplorParamIndex=htmlspecialchars($_GET[\"index\"]);\n ");

  printf ("$ExplorParamKey1=htmlspecialchars($_GET[\"key\"]); \n");
  printf ("$ExplorParamKey2=str_replace (\"\\\\\'\", \"\'\",$ExplorParamKey1) ; \n");
  printf ("$ExplorParamKey=str_replace (\"&#39;\", \"\'\",$ExplorParamKey2) ; \n");
  printf ("$indexParamKey=\"shortNameIndex\". $ExplorParamIndex;\n");

  printf("$ExplorParamIndexShortName=ExplorGetParamK5($ExplorParamTable, \"Area\", \"%s\", \"%s\", \"indexHead.php\",  $indexParamKey);\n", stream, step);
  printf ("if(isset($_GET[\"size\"]))$ExplorParamSize=htmlspecialchars($_GET[\"size\"]); \n");
  printf("else $ExplorParamSize=20;\n");
  printf ("if(isset($_GET[\"from\"]))$ExplorParamFrom=htmlspecialchars($_GET[\"from\"]); \n");
  printf("else $ExplorParamFrom=0;\n");
  printf("$ExplorCallPageIndexItem=\"indexItem.php?index=\".$ExplorParamIndex.\"&key=\".$ExplorParamKey1;\n");
  printf ("?>\n");
  ExplorSiteGenerUnitIndex (lang, stream, step, treeCode);
  printf ("<title>%s, %s, %s, indexItem, <?php echo $ExplorParamIndex.\", \".$ExplorParamKey; ?></title>\n", ExplorAreaCode, stream, step);
  ExplorHtmlBeginBody();
  ExplorSidebar(stream, step, EXPLOR_PHP_TYPE_INDEX_ITEM, "indexItem.php");

  ExplorHeaderPhpBegin(stepTreeNode, "indexItem.php");
  ExplorPhpPrintParam4("Area", stream, step, "headerLine2@indexItem.php" );
  ExplorHeaderPhpEnd(stepTreeNode, "indexItem.php");

  printf("<div class=\"main\">");
  ExplorSitePhpIntro();
  printf ("$ExplorIndexPath=$ExplorStepDataPath.$ExplorParamIndex;\n");

  printf("echo \"<table width=\\\"95%s\\\"><tr><td width=\\\"50%s\\\">\\n\";", "%", "%");
  ExplorSiteGenerIndexItemFistLine("indexItem");
  printf("echo \"</td><td width=\\\"10%s\\\">&nbsp;</td><td width=\\\"40%s\\\"><b>Facettes :</b>\\n\";",  "%", "%");
  ExplorSiteGenerIndexItemFacet  (stepTreeNode);
  printf("echo \"</td></tr></table>\\n\";");

  printf ("if ($domIndex) \n   {\n");
  printf ("$ExplorLenghtListBiblio=SxmlNodeGetFirstChildByTagName($domIndex->documentElement,\"f\")->nodeValue;\n");
  ExplorSiteHeadListBiblio(lang, stream, step, "indexItem.php");
  printf ("IndexItemPrintListRef($ExplorParamTable, $domIndex, \"%s\", \"%s\", \"indexItem.php\", $ExplorParamFrom, $ExplorParamSize);\n", stream, step);
  printf ("echo \"\\n</table>\";\n");

  printf ("$ExplorIndexListCrossDom= IndexGetDomListCross($ExplorParamTable,\"%s\",\"%s\",\"indexItem.php\",$ExplorParamIndex);\n", stream, step);
  printf ("if ($ExplorIndexListCrossDom)  IndexPrintListListCross($ExplorParamTable,\"%s\",\"%s\",\"indexItem.php\",$ExplorIndexListCrossDom, $ExplorParamIndex,$ExplorParamKey );\n", stream, step);


  printf ("        echo \"<hr/>\";\n" );
  ExplorPhpPrintParam5("Area", stream, step, "indexItem","titleRecordShell");
  ExplorSiteGenerShellExplorVar(stream, step);
  printf ("        echo \"<pre>\\nHfdIndexSelect -h \\$EXPLOR_AREA/Data/%s/%s/\".$ExplorParamIndex.\"", stream, step);
  printf (" -k \".\'\"\'.$ExplorParamKey.\'\"\'.\" \\n</pre>\n\";");
  printf ("        echo \"<pre>\\nHfdIndexSelect -h \\$EXPLOR_AREA/Data/%s/%s/\".$ExplorParamIndex", stream, step);
  printf (".\"  \\\n                -Sk \".\'\"\'.$ExplorParamKey.\'\"\'.\" \\\n");
  printf ("         | HfdSelect -Kh \\$EXPLOR_AREA/Data/%s/%s/biblio.hfd \\n</pre>\n\";", stream, step);

  ExplorPhpPrintParam5("Area", stream, step, "bibRecord","titleRecordWikiLink");
  ExplorSiteGenerExplorLien(stream, step);
  printf ("        echo \"   |type=    indexItem\\n\";\n" );
  printf ("        echo \"   |index=    \".$ExplorParamIndex.\"\\n\";\n" );
  printf ("        echo \"   |clé=    \".$ExplorParamKey.\"\\n\";\n" );
  printf ("        echo \"}}\\n</pre>\\n\";\n" );

  printf ("\n       }\n        else {\n");
  printf ("\n       }\n");
  ExplorSitePhpEnd();
  ExplorHtmlBottomDilib(0);
  printf("</div>");
  ExplorHtmlEndBody();
  return 0;
}

int ExplorSiteGenerIndexFacet (char *lang, char *stream, char *step, int treeCode)
{
  SxmlNode *stepTreeNode;
  stepTreeNode=ExplorSiteTreeNode(stream, step, NULL);
  printf("<?dilib file=\"%s/Site/%s/%s/%s/indexFacet.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);
  printf("   <!-- the pho file is generated by ExplorGenerPhpProc / ExplorSiteGenerIndexFacet -->\n");

  printf ("<?php \n");
  printf ("$ExplorParamIndex=htmlspecialchars($_GET[\"index\"]);\n ");
  printf ("$ExplorParamFacet=htmlspecialchars($_GET[\"facet\"]);\n ");

  printf ("$ExplorParamKey1=htmlspecialchars($_GET[\"key\"]); \n");
  printf ("$ExplorParamKey2=str_replace (\"\\\\\'\", \"\'\",$ExplorParamKey1) ; \n");
  printf ("$ExplorParamKey=str_replace (\"&#39;\", \"\'\",$ExplorParamKey2) ; \n");
  printf ("$indexParamKey=\"shortNameIndex\". $ExplorParamIndex;\n");

  printf ("?>\n");

  ExplorSiteGenerUnitIndex (lang, stream, step, treeCode);
  printf ("<title>%s, %s, %s, indexFacet, <?php echo $ExplorParamIndex.\", \".$ExplorParamKey; ?></title>\n", ExplorAreaCode, stream, step);

  ExplorHtmlBeginBody();
  ExplorSidebar(stream, step, EXPLOR_PHP_TYPE_INDEX_ITEM, "indexFacet.php");
  ExplorHeaderPhpBegin(stepTreeNode, "indexItem.php");
  ExplorPhpPrintParam4("Area", stream, step, "headerLine2@indexFacet.php" );
  ExplorHeaderPhpEnd(stepTreeNode, "indexFacet.php");

  printf("<div class=\"main\">");
  ExplorSitePhpIntro();
  printf ("$ExplorIndexPath=$ExplorStepDataPath.$ExplorParamIndex;\n");

  ExplorSiteGenerIndexItemFistLine("indexFacet");

  printf ("if ($domIndex) \n   {\n");
  printf ("$ExplorLenghtListBiblio=SxmlNodeGetFirstChildByTagName($domIndex->documentElement,\"f\")->nodeValue;\n");
  printf ("echo \"\\n<p>Nombre de documents :\".$ExplorLenghtListBiblio.\"</p>\";\n");
  
  printf ("$ExplorFacetXpath=ExplorGetParamK3($ExplorParamTable, \"facet\", $ExplorParamFacet ,\"xPath\");\n");
  printf ("$ExplorFacetTargetIndex=ExplorGetParamK3($ExplorParamTable, \"facet\", $ExplorParamFacet ,\"index\");\n");

  printf ("IndexFacetComputePrintListRef($ExplorParamTable, $domIndex, \"%s\", \"%s\", \"indexFacet.php\", $ExplorParamIndex, $ExplorParamKey , $ExplorFacetXpath, $ExplorFacetTargetIndex);\n", stream, step);
  printf ("\n       }\n");

  printf("echo \"<hr>xpath= \".$ExplorFacetXpath;");
  ExplorSitePhpEnd();
  ExplorHtmlBottomDilib(0);

  ExplorHtmlEndBody();
  return 0;
}

int ExplorSiteGenerAssoc (char *lang, char *stream, char *step, int treeCode)
{
  SxmlNode *stepTreeNode;
  stepTreeNode=ExplorSiteTreeNode(stream, step, NULL);
  printf("<?dilib file=\"%s/Site/%s/%s/%s/assoc.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);
  printf("   <!-- the php file is generated by ExplorGenerPhpProc / ExplorSiteGenerAssoc -->\n");

  printf ("<?php \n ");
  printf ("$ExplorInputParamK1=htmlspecialchars($_GET[\"k1\"]);\n");
  printf ("$ExplorParamK1=str_replace (\"\\\\\'\", \"\'\",$ExplorInputParamK1) ; \n");
  printf ("$ExplorInputParamK2=htmlspecialchars($_GET[\"k2\"]);\n");
  printf ("$ExplorParamK2=str_replace (\"\\\\\'\", \"\'\",$ExplorInputParamK2) ; \n");
  printf ("$ExplorParamI1=htmlspecialchars($_GET[\"i1\"]);\n");
  printf ("$ExplorParamI2=htmlspecialchars($_GET[\"i2\"]);\n");
  printf ("$indexParamI1=\"shortNameIndex\". $ExplorParamI1;\n");
  printf("$ExplorParamIndex1ShortName=ExplorGetParamK5($ExplorParamTable, \"Area\", \"%s\", \"%s\", \"indexHead.php\",  $indexParamI1);\n", stream, step);
  printf ("$indexParamI2=\"shortNameIndex\". $ExplorParamI2;\n");
  printf("$ExplorParamIndex2ShortName=ExplorGetParamK5($ExplorParamTable, \"Area\", \"%s\", \"%s\", \"indexHead.php\",  $indexParamI2);\n", stream, step);
  printf ("?>\n");

  ExplorHtmlBeginBody();
  /* ExplorSidebarTop(treeCode);
     ExplorSidebarBottom(); */
  ExplorSidebar(stream, step, EXPLOR_PHP_TYPE_ASSOC, "assoc.php");
  ExplorHtmlHeader(stream, step, "assoc.php", NULL, NULL);

  printf("<div class=\"main\">");
  ExplorSitePhpIntro();
  
  printf ("$indexPath1=$ExplorStepDataPath.$ExplorParamI1;\n");
  printf ("$indexPath2=$ExplorStepDataPath.$ExplorParamI2;\n");
  printf ("$array1= HfdGetArrayFromIndexKey($paramTable,$indexPath1, $ExplorParamK1);\n");
  printf ("$array2= HfdGetArrayFromIndexKey($paramTable,$indexPath2, $ExplorParamK2);\n");
  printf ("$intersectArray=array_intersect ( $array1 , $array2 );\n");

  printf ("IndexAssocSetParam($ExplorParamTable,  \"%s\", \"%s\", \"assoc.php\", $ExplorStepDataPath, $array1, $array2, $intersectArray, $ExplorParamK1, $ExplorParamI1, $ExplorParamK2, $ExplorParamI2);\n", stream, step);
  printf (" echo \"<h3>Eléments de l'association</h3>\";\n");
  printf ("IndexAssocPrintBox($ExplorParamTable, \"%s\", \"%s\", \"assoc.php\", $array1, $array2, $intersectArray,$ExplorParamK1, $ExplorParamI1, $ExplorParamK2, $ExplorParamI2);\n", stream, step);
  printf ("$ExplorLenghtListBiblio=count($intersectArray);\n");
  ExplorSiteHeadListBiblio(lang, stream, step, "assoc.php");
  printf ("IndexPrintArray($ExplorParamTable, $intersectArray,  \"%s\", \"%s\", \"assoc.php\");\n", stream, step);
  printf ("echo \"</table>\";\n");
  ExplorSitePhpEnd();
  ExplorHtmlBottomDilib(0);
  printf("</div>");
  ExplorHtmlEndBody();
  return 0;
}

int ExplorSiteGenerAndNot (char *lang, char *stream, char *step, int treeCode)
{
  printf("<?dilib file=\"%s/Site/%s/%s/%s/andNot.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);

  printf ("<?php \n ");
  printf ("echo \"<!--    This part is generated by ExplorGenerPhpProc / ExplorSiteGenerAndNot -->\\n\";\n");
  printf ("$ExplorParamK1=htmlspecialchars($_GET[\"k1\"]);\n");
  printf ("$ExplorParamK2=htmlspecialchars($_GET[\"k2\"]);\n");
  printf ("$ExplorParamI1=htmlspecialchars($_GET[\"i1\"]);\n");
  printf ("$ExplorParamI2=htmlspecialchars($_GET[\"i2\"]);\n");
  printf("$ExplorParamIndex1ShortName=ExplorGetParamK5($ExplorParamTable, \"Area\", \"%s\", \"%s\", \"indexHead.php\",  $ExplorParamI1);\n", stream, step);
  printf("$ExplorParamIndex2ShortName=ExplorGetParamK5($ExplorParamTable, \"Area\", \"%s\", \"%s\", \"indexHead.php\",  $ExplorParamI2);\n", stream, step);

  printf ("?>\n");
  ExplorHtmlBeginBody();

  ExplorSidebar(stream, step, EXPLOR_PHP_TYPE_AND_NOT, "andNot.php");
  ExplorHtmlHeader(stream, step, "andNot.php", NULL, NULL);

  printf("<div class=\"main\">");
  ExplorSitePhpIntro();

  printf ("$indexPath1=$ExplorStepDataPath.$ExplorParamI1;\n");
  printf ("$indexPath2=$ExplorStepDataPath.$ExplorParamI2;\n");
  printf ("$array1= HfdGetArrayFromIndexKey($paramTable,$indexPath1, $ExplorParamK1);\n");
  printf ("$array2= HfdGetArrayFromIndexKey($paramTable,$indexPath2, $ExplorParamK2);\n");
  printf ("$intersectArray=array_diff ( $array1 , $array2 );\n");
  printf ("$ExplorLenghtListBiblio=count($intersectArray);\n");
  printf ("echo \"<h2>\".$ExplorParamK1.\"<i> And Not</i>\" .$ExplorParamK2.\"</h2>\";");
  ExplorSiteHeadListBiblio(lang, stream, step, "assoc.php");
  printf ("IndexPrintArray($ExplorParamTable, $intersectArray,  \"%s\", \"%s\", \"assoc.php\");\n", stream, step);
  printf ("echo \"</table>\";\n");
  ExplorSitePhpEnd();
  ExplorHtmlBottomDilib(0);
  printf("</div>");
  ExplorHtmlEndBody();
  return 0;
}

int ExplorSiteGenerCluster (char *lang, char *stream, char *step, int treeCode)
{
  printf("<?dilib file=\"%s/Site/%s/%s/%s/cluster.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);
  printf ("<?php \n ");
  printf ("$ExplorParamCluster=htmlspecialchars($_GET[\"clusterCode\"]);\n");
  printf ("$ExplorParamId=htmlspecialchars($_GET[\"id\"]);\n");
  printf ("$clusterDom=IndexGetClusterDomDocument ($ExplorParamTable, $ExplorParamCluster, $ExplorParamId);\n");
  printf ("$clusterName=SxmlNodeGetFirstChildByTagName($clusterDom->documentElement,\"name\")->nodeValue;\n");
  printf ("$indexCode=ExplorGetParamK5($ExplorParamTable, \"Area\", \"%s\", \"%s\", $ExplorParamCluster, \"fromIndex\");\n", stream, step);
  printf("$ExplorParamIndexShortName=ExplorGetParamK5($ExplorParamTable, \"Area\", \"%s\", \"%s\", \"indexHead.php\",  $indexCode);\n", stream, step);
  printf("$ExplorParamIndex=$indexCode;\n"); /* for Sidebar to be verified */
  printf ("?>\n");
  ExplorHtmlBeginBody();
  /* ExplorSidebarTop(treeCode);
     ExplorSidebarBottom()*/
  ExplorSidebar(stream, step, EXPLOR_PHP_TYPE_CLUSTER_ITEM, "clusterItem.php");
  ExplorHtmlHeader(stream, step, "clusterItem.php", NULL, NULL);
  printf("<div class=\"main\">");
  ExplorSitePhpIntro();
  printf (" echo \"<h3>Le cluster \".$clusterName.\"</h3>\";\n");
  printf (" IndexPrintCluster($ExplorParamTable, \"%s\",  \"%s\", \"cluster.php\", $ExplorParamCluster, $clusterDom)", stream,step);
  ExplorSitePhpEnd();
  ExplorHtmlBottomDilib(0);
  printf("</div>");
  ExplorHtmlEndBody();
  return 0;
}

void ExplorSiteGenerBibRecordBegin (char *lang, char *stream, char *step, int treeCode)
{
  printf("<?dilib file=\"%s/Site/%s/%s/%s/bibRecord.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);
  printf("   <!-- this part is generated by ExplorGenerPhpProc / ExplorGenerBibRecordBegin -->\n");
  printf ("<?php \n $ExplorParamRecordNumber=htmlspecialchars($_GET[\"hk\"]); ");
  printf ("?>\n");
  printf ("<title>%s, %s, %s, bibRecord, <?php echo $ExplorParamRecordNumber; ?></title>\n", ExplorAreaCode, stream, step);
  ExplorHtmlBeginBody();
  ExplorSidebar(stream, step, EXPLOR_PHP_TYPE_BIBLIO, "bibRecord.php");
  ExplorHtmlHeader(stream, step, "bibRecord.php", NULL, NULL);
  printf("<div class=\"main\">");
  ExplorSitePhpIntro();

  printf(" $domRecord=ExplorGetBiblioFromHfdKey($ExplorParamTable, \"%s\", \"%s\", $ExplorParamRecordNumber);\n", stream, step);
  printf(" if (!$domRecord) echo \"*****  Acces problem to record *****\\\n\";");
  /*  printf(" $domHcs=HfdGetDomHcs(&$paramTable, \"Data/%s/%s/biblio\");\n", stream, step); */
  printf(" $domHcs=HfdGetDomHcs(\"Data/%s/%s/biblio\");\n", stream, step); 
  printf("  $ExplorTeiBiblioRoot=SxmlNodeGetFirstChildByTagName($domRecord->documentElement,\"TEI\");\n");
  printf("  $ExplorBiblioRecordNextHfdKey=HfdHcsNextHfdKey($domHcs->documentElement, $ExplorParamRecordNumber);\n");
  printf(" $ExplorBiblioRecordPreviousHfdKey=HfdHcsPreviousHfdKey($domHcs->documentElement, $ExplorParamRecordNumber);\n");
  printf(" if($ExplorBiblioRecordPreviousHfdKey) $ExplorAnchorToPreviousRecord=\'<a href=\"bibRecord.php?hk=\'.$ExplorBiblioRecordPreviousHfdKey.\'\">\';\n");
  printf(" if($ExplorBiblioRecordNextHfdKey) $ExplorAnchorToNextRecord=\'<a href=\"bibRecord.php?hk=\'.$ExplorBiblioRecordNextHfdKey.\'\">\';\n");
  printf(" $ExplorBiblioRecordTitle=TeiGetTitle($ExplorParamTable,$domRecord, $ExplorTeiBiblioRoot);\n");
  printf(" $ExplorBiblioRecordRBID=TeiGetRBID($ExplorParamTable,$domRecord, $ExplorTeiBiblioRoot);\n");
  ExplorPhpPrintParam5("Area", stream, step, "bibRecord", "titleRefBiblio" );
  printf(" $returnDoc=TeiExplorRecordPrint($ExplorParamTable, $domRecord, \"%s\",  \"%s\" );\n", stream, step);
  printf("if (!$returnDoc) echo \"\\n*****  probable Xml problem with record *****\\n\";\n");
}

void ExplorSiteGenerBibRecordAffiliationSection (char *lang, char *stream, char *step)
{
 if ((strcmp(step, "Exploration")==0)||(strcmp(step, "Analysis")==0)||(strcmp(step, "Checkpoint")==0))
    {
      printf("  $ExplorTeiBiblioAffiliations=SxmlNodeGetFirstChildByTagName($domRecord->documentElement,\"affiliations\");\n");
      printf("  if ($ExplorTeiBiblioAffiliations) \n{\n"); 
      printf("  $ExplorTeiBiblioAffiliationsList=SxmlNodeGetFirstChildByTagName($ExplorTeiBiblioAffiliations,\"list\");\n");
      printf("  if ($ExplorTeiBiblioAffiliationsList) TeiExplorRecordAffPrint($ExplorParamTable, $ExplorTeiBiblioAffiliationsList, \"%s\", \"%s\");\n", stream, step);
      printf("  \n}\n"); 
    }
}

void ExplorSiteGenerBibRecordLinkSection (char *lang, char *stream, char *step)
{
 if (strcmp(step,"Corpus")!=0)
    {
        printf ("        echo \"<hr/>\";");
	ExplorPhpPrintParam5("Area", stream, step, "bibRecord","titleLinkCuration");
	printf ("    ExplorPrintCurationLinks($ExplorParamTable, \"%s\",\"%s\",\"bibRecord.php\", $domRecord);\n", stream, step);
    }
  if (strcmp(step,"Exploration")!=0)
    {
        printf ("        echo \"<hr/>\";");
	ExplorPhpPrintParam5("Area", stream, step, "bibRecord","titleLinkExploration");
	printf ("       $rbid= TeiGetRbid($paramTable,$domRecord, $ExplorTeiBiblioRoot);\n");
	printf ("        echo \'<a href=\"../../Main/Exploration/bibRecordById.php?id=\'.$rbid.\'\">\';\n");
	printf ("        echo $rbid.\"</a>\";\n");
    }
}

void ExplorSiteGenerBibRecordCurationSection (char *lang, char *stream, char *step)
{
  if (strcmp(step, "Curation")==0)
    {
      printf(" if (($noCountryList=ExplorTestHasNoCountry($ExplorParamTable, $domRecord,  $ExplorTeiBiblioRoot, \"%s\",  \"%s\" )))\n ", stream, step);
      printf ("   {\n");
      printf ("        echo \"<hr/>\";\n" );
      printf ("        echo \"<h2>Curation</h2>\";\n" );
      printf ("        echo \"<h3>No country items</h3>\";\n" );
      printf ("        ExplorPrintNoCountryList($noCountryList); \n");
      printf ("   }\n");

    }
}

void ExplorSiteGenerBibRecordXmlSection (char *lang, char *stream, char *step)
{
  printf ("        echo \"<hr/>\";");
  ExplorPhpPrintParam5("Area", stream, step, "bibRecord","titleRecordXml");
  printf ("        SxmlNodePrintIndent($domRecord->documentElement);\n" );
}

void ExplorSiteGenerBibRecordShellSection (char *lang, char *stream, char *step)
{
  printf ("        echo \"<hr/>\";\n" );
  ExplorPhpPrintParam5("Area", stream, step, "bibRecord","titleRecordShell");
  ExplorSiteGenerShellExplorVar(stream, step);
  printf ("        echo \"<pre>\\nHfdSelect -h \\$EXPLOR_STEP/biblio.hfd -nk \".$ExplorParamRecordNumber.\" | SxmlIndent | more\\n</pre>\n\";");
  printf("\n");
  if (strcmp(lang,"fr")==0) printf("echo \"<p>Ou</p>\\n\";\n"); /* else printf("<p>Or</p>\n"); */
  printf ("        echo \"<pre>\\nHfdSelect -h \\$EXPLOR_AREA/Data/%s/%s/biblio.hfd -nk \".$ExplorParamRecordNumber.\" | SxmlIndent | more\\n</pre>\n\";", stream, step);
  printf("\n");
  ExplorPhpPrintParam5("Area", stream, step, "bibRecord","titleRecordWikiLink");
  ExplorSiteGenerExplorLien(stream, step);
  printf ("        echo \"   |type=    RBID\\n\";\n" );
  printf ("        echo \"   |clé=     \". $ExplorBiblioRecordRBID.\"\\n\";\n" );
  printf ("        echo \"   |texte=   \".$ExplorBiblioRecordTitle.\"\\n\";\n" );
  printf ("        echo \"}}\\n</pre>\\n\";\n" );

  printf("$ExplorBiblioRecordPmid=TeiGetPmid($ExplorParamTable,$domRecord,$ExplorTeiBiblioRoot);\n" );
  printf  ("if ($ExplorBiblioRecordPmid) { \n" );
  ExplorPhpPrintParam5("Area", stream, step, "bibRecord","titleRecordGenerWikiPage");
printf ("        echo \"<pre>\\nHfdIndexSelect -h \\$EXPLOR_AREA/Data/%s/%s/RBID.i   -Sk \\\"pubmed:\".$ExplorBiblioRecordPmid.\"\\\" \\\\\\n\";\n", stream, step);
 printf ("  echo \"       | HfdSelect -Kh \\$EXPLOR_AREA/Data/%s/%s/biblio.hfd   \\\\\\n\";\n", stream, step );
 printf (" echo  \"       | NlmPubMed2Wicri -a %s \\n\";\n", ExplorAreaCode);
 printf ("  echo \"</pre>\n\";\n");

  printf ("}\n");
}

void ExplorSiteGenerBibRecordEnd (char *lang, char *stream, char *step, int treeCode)
{
  ExplorSiteGenerBibRecordAffiliationSection (lang, stream, step);
  ExplorSiteGenerBibRecordLinkSection (lang, stream, step);
  ExplorSiteGenerBibRecordCurationSection (lang, stream, step);
  ExplorSiteGenerBibRecordXmlSection (lang, stream, step);
  ExplorSiteGenerBibRecordShellSection (lang, stream, step);
  ExplorSitePhpProcEnd();
}

void ExplorSiteGenerBibRecord (char *lang, char *stream, char *step, int treeCode)
{
  ExplorSiteGenerBibRecordBegin (lang, stream, step, treeCode);
  ExplorSiteGenerBibRecordEnd (lang, stream, step, treeCode);
}

void ExplorSiteGenerBibRecordByIdBegin (char *lang, char *stream, char *step, int treeCode)
{
  printf("<?dilib file=\"%s/Site/%s/%s/%s/bibRecordById.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);
  printf("   <!-- this part is generated by ExplorGenerPhpProc / ExplorGenerBibRecordByIdBegin -->\n");
  printf ("<?php \n $ExplorParamRecordId=htmlspecialchars($_GET[\"id\"]); ");
  printf ("?>\n");
  printf ("<title>%s, %s, %s, bibRecordById, <?php echo $ExplorParamRecordId; ?></title>\n", ExplorAreaCode, stream, step);
  ExplorHtmlBeginBody();
  ExplorSidebar(stream, step, EXPLOR_PHP_TYPE_BIBLIO, "bibRecordBy.php");
  ExplorHtmlHeader(stream, step, "bibRecordBy.php", NULL, NULL);
  printf("<div class=\"main\">");
  ExplorSitePhpIntro();

  printf ("$indexPath=$ExplorStepDataPath.\"RBID.i\";\n");
  printf ("$domIndex=HfdGetXmlDomFromIndexKey($ExplorParamTable, $indexPath, $ExplorParamRecordId); \n");
  printf ("if (!$domIndex) {exit (\"**** Key not founded ****\");}");

  printf ("$ExplorParamRecordNumber=SxmlNodeGetFirstChildByTagName($domIndex->documentElement,\"l\")->firstChild->nodeValue;\n");

  printf(" $domRecord=ExplorGetBiblioFromHfdKey($ExplorParamTable, \"%s\", \"%s\", $ExplorParamRecordNumber);\n", stream, step);
  printf(" $domHcs=HfdGetDomHcs(\"Data/%s/%s/biblio\");\n", stream, step); 
  printf("  $ExplorBiblioRecordNextHfdKey=HfdHcsNextHfdKey($domHcs->documentElement, $ExplorParamRecordNumber);\n");
  printf(" $ExplorBiblioRecordPreviousHfdKey=HfdHcsPreviousHfdKey($domHcs->documentElement, $ExplorParamRecordNumber);\n");
  printf(" if($ExplorBiblioRecordPreviousHfdKey) $ExplorAnchorToPreviousRecord=\'<a href=\"bibRecord.php?hk=\'.$ExplorBiblioRecordPreviousHfdKey.\'\">\';\n");
  printf(" if($ExplorBiblioRecordNextHfdKey) $ExplorAnchorToNextRecord=\'<a href=\"bibRecord.php?hk=\'.$ExplorBiblioRecordNextHfdKey.\'\">\';\n");  

  printf("  $ExplorTeiBiblioRoot=SxmlNodeGetFirstChildByTagName($domRecord->documentElement,\"TEI\");\n");
  printf(" $ExplorBiblioRecordTitle=TeiGetTitle($ExplorParamTable,$domRecord, $ExplorTeiBiblioRoot);\n");
  printf(" $ExplorBiblioRecordRBID=TeiGetRBID($ExplorParamTable,$domRecord, $ExplorTeiBiblioRoot);\n");
  ExplorPhpPrintParam5("Area", stream, step, "bibRecord", "titleRefBiblio" );
  printf("   TeiExplorRecordPrint($ExplorParamTable, $domRecord, \"%s\",  \"%s\" );\n", stream, step);
}

void ExplorSiteGenerBibRecordById (char *lang, char *stream, char *step, int treeCode)
{
  ExplorSiteGenerBibRecordByIdBegin (lang, stream, step, treeCode);
  ExplorSiteGenerBibRecordEnd (lang, stream, step, treeCode);
}

void ExplorSiteGenerIndexAutAff(char *lang, char *stream, char *step, int treeCode)
{
  printf("<?dilib file=\"%s/Site/%s/%s/%s/autAff.php\" ?>\n", ExplorAreaDir, lang, stream, step);
  ExplorGenPhpHeadPage (lang, stream, step);
  printf("   <!-- the pho file is generated by InistExplorSiteHtmlPhp / generIndexItemAutAff -->\n");
  printf ("<?php \n $ExplorParamIndex=htmlspecialchars($_GET[\"index\"]); ");
  printf ("$ExplorParamKey=htmlspecialchars($_GET[\"key\"]); ");
  printf ("?>\n");
  printf ("<title>%s, %s, %s, indexHead, <?php echo $ExplorParamIndex; ?></title>\n", ExplorAreaCode, stream, step);
  ExplorHtmlBeginBody();
  ExplorSidebar(stream, step, EXPLOR_PHP_TYPE_INDEX_ITEM, "autAff.php");
  ExplorHtmlHeader(stream, step, "indexItem.php", NULL, NULL);

  printf("<div class=\"main\">");
  ExplorSitePhpIntro();
  printf ("$indexPath=$ExplorStepDataPath.$ExplorParamIndex;\n");
  printf (" $ExplorIndexPath=$indexPath;\n");
  ExplorSiteGenerIndexItemFistLine("autAff");
  /*
  printf ("$domIndex=HfdGetXmlDomFromIndexKey($ExplorParamTable, $indexPath, $ExplorParamKey); \n");
  */
  printf ("       ExplorPrintAutAff($ExplorParamTable, $domIndex, %s, %s, \"autAff.php\");\n", stream, step);
  printf ("        SxmlNodePrintIndent($domIndex->documentElement);\n" );
  ExplorSitePhpEnd();
  ExplorHtmlBottomDilib(0);
  printf("</div>");
  ExplorHtmlEndBody();
}
