<?php
/*=======================================================================
 *
 * customer_service_acceptance_protocol.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/


include_once ("../include/mcglobal.inc.php");
include_once ("../include/auth.inc.php");


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdActual",
                  "f_service", "objId", "objType", "f_question_new", "f_mail_new", "f_text", "deactivateMenu"));

// Get number of all services
$metatypeInstallationPhrase = " AND mt_sort != '1' ";
$numOfAllServices = getCountOfTable("metatype", "mt_type = 'service' AND mt_objtype = '' AND mt_objid = '0'" . $metatypeInstallationPhrase);  // Number of ALL services

// Check HTTP-Parameters for the questions
$f_question_array = array();
$f_mail_array = array();
for ($i = 0; $i <= 20; $i++) :
    // Question
    list($tmpQuestion) = getSecHttpVars("1",array("f_question_" . pad($i,2)));
    if ($tmpQuestion != "") :
       $f_question_array[$i] = $tmpQuestion;
       list($tmpMail) = getSecHttpVars("1",array("f_mail_" . pad($i,2)));
       $f_mail_array[$i] = $tmpMail;
    endif;
endfor;
if ($f_question_new != "") :
   array_push($f_question_array, $f_question_new);
endif;
if ($f_mail_new != "") :
   array_push($f_mail_array, $f_mail_new);
endif;


getLanguage(__FILE__);

$deactivateMenuStatic = "1";
$pageTitel = getLngt("ABNAHMEPROTOKOLL - Fragen und Text");
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");
getCurrentScript(__FILE__);

// Check for authentication access and granted rights
$usrAccessArray["hq"] = "1"; $usrAccessArray["cs"] = "1";
authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);


// Get global definitions of the services
$output = getLngt("Services") . ":&nbsp;";
$output .= "<select class=\"f8np1\" name=\"f_service\" id=\"f_service\" onChange=\"serviceFinishPage('');\">";
$output .= "  <option value=\"\" " . ($f_service == "" ? "selected" : "") . ">" . getLngt("Keine Auswahl") . "</option>";
$output .=    addOptionsFromTable("metatype", "mt_sort", "mt_value", "mt_sort", "mt_type = 'service' AND mt_objtype = '' AND mt_objid = '0'" . $metatypeInstallationPhrase, $f_service , "");
$output .= "</select>";


// Display questions of the service acceptance protocol 
$outputQuestions = "";
$outputText = "";
if ($f_service != "" && $objType != "" && $objId != "") :

   // Set special database fieldname regarding the generic data container structure
   $specialDbFieldNameQuestions = "service_acceptance_protocol_questions";
   $specialDbFieldNameMailState = "service_acceptance_protocol_mail_state_question";
   $specialDbFieldNameText = "service_acceptance_protocol_text";

   // Save questions
   if ($f_act == "saveCustomerServicesAcceptanceProtocol") :

      // **** Questions ****
      // Load current questions of the customer
      $gdcContentQuestions = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameQuestions . "'");
      $currArray = getKeyValueArrayFromString($gdcContentQuestions);    // Convert key-value-string to array
      $currArray[$f_service] = implode(",", $f_question_array);         // Implode new questions of the selected service and store to current structure
      // Get new string of ALL services to store into the database
      $newContentArray = array();
      for ($i = 0; $i < $numOfAllServices; $i++) :
          if ($currArray[$i] != "") :
             $newContentArray[] = $i . "=" . $currArray[$i];
          endif;
      endfor;
      $gdcContentQuestions = implode("|", $newContentArray);
      // Update or insert questions
      if (existsEntry("genericdatacontainer",array("gdc_obj_type",$objType,"gdc_obj_id",$objId,"gdc_gen_fieldname",$specialDbFieldNameQuestions))) :
         updateStmt("genericdatacontainer","gdc_obj_type",$objType,array("gdc_content", $gdcContentQuestions),"gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameQuestions . "'");
      else :
         insertStmt("genericdatacontainer", array("gdc_obj_type", $objType, "gdc_obj_id", $objId, "gdc_gen_fieldname", $specialDbFieldNameQuestions, "gdc_content", $gdcContentQuestions));
      endif;

      // **** Mail state for questions ****
      // Load current mail states of the customer
      $gdcContentMails = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameMailState . "'");
      $currArray = getKeyValueArrayFromString($gdcContentMails);    // Convert key-value-string to array
      $currArray[$f_service] = implode(",", $f_mail_array);             // Implode new questions of the selected service and store to current structure
      // Get new string of ALL services to store into the database
      $newContentArray = array();
      for ($i = 0; $i < $numOfAllServices; $i++) :
          if ($currArray[$i] != "") :
             $newContentArray[] = $i . "=" . $currArray[$i];
          endif;
      endfor;
      $gdcContentQuestions = implode("|", $newContentArray);
      // Update or insert questions
      if (existsEntry("genericdatacontainer",array("gdc_obj_type",$objType,"gdc_obj_id",$objId,"gdc_gen_fieldname",$specialDbFieldNameMailState))) :
         updateStmt("genericdatacontainer","gdc_obj_type",$objType,array("gdc_content", $gdcContentQuestions),"gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameMailState . "'");
      else :
         insertStmt("genericdatacontainer", array("gdc_obj_type", $objType, "gdc_obj_id", $objId, "gdc_gen_fieldname", $specialDbFieldNameMailState, "gdc_content", $gdcContentQuestions));
      endif;

      // **** Text ****
      $gdcContentTexts = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameText . "'");
      $currArray = getKeyValueArrayFromString($gdcContentTexts);      // Convert key-value-string to array
      $currArray[$f_service] = $f_text;                               // Implode new text of the selected service and store to current structure
      // Get new string of ALL services to store into the database
      $newContentArray = array();
      for ($i = 0; $i < $numOfAllServices; $i++) :
          if ($currArray[$i] != "") :
             $newContentArray[] = $i . "=" . $currArray[$i];
          endif;
      endfor;
      $gdcContentTexts = implode("|", $newContentArray);
      // Update or insert text
      if (existsEntry("genericdatacontainer",array("gdc_obj_type",$objType,"gdc_obj_id",$objId,"gdc_gen_fieldname",$specialDbFieldNameText))) :
         updateStmt("genericdatacontainer","gdc_obj_type",$objType,array("gdc_content", $gdcContentTexts),"gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameText . "'");
      else :
         insertStmt("genericdatacontainer", array("gdc_obj_type", $objType, "gdc_obj_id", $objId, "gdc_gen_fieldname", $specialDbFieldNameText, "gdc_content", $gdcContentTexts));
      endif;
   endif;


   // **** Prepare output questions ****
   // (Re-)Load current questions and mail states of the customer
   $gdcContentQuestions = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameQuestions . "'");
   $gdcContentMails = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameMailState . "'");
   $i = "";  // Init
   $outputQuestions .= "<table>\n";
   if ($gdcContentQuestions != "") :
      // Split content to get the questions of the current selected service
      // E.g.: "0=Q1,Q2,Q3|1=Q1,Q2,Q3,Q4,Q5|2=Q1..."  => x(0) = Q1,Q2,Q3, x(1) = Q1,Q2,Q3,Q4,Q5, ...
      // [=> 0=Q1,Q2,Q3 => Service "0" (= Lieferung) is mapped to the questions]
      $f_question_array = getKeyValueArrayFromString($gdcContentQuestions);
      $f_question_string = $f_question_array[$f_service];  // Take the questions (array) of the current service
      $f_mail_array = getKeyValueArrayFromString($gdcContentMails);
      $f_mail_string = $f_mail_array[$f_service];  // Take the mail states (array) of the questions of the current service
      if ($f_question_string != "") :
         $f_question_array = spliti(",", $f_question_string);
         $f_question_array_len = count($f_question_array);
         $f_mail_array = spliti(",", $f_mail_string);
         $f_f_mail_array_len = count($f_mail_array);
         if ($f_question_array_len > 0) :
            for ($i = 0; $i < $f_question_array_len; $i++) :
                $outputQuestions .= "<tr>\n";
                $outputQuestions .= "<td>" . ($i + 1) . ".&nbsp;</td>";
                $outputQuestions .= "<td>";
                $outputQuestions .= "<input class=\"f10np1\" type=\"text\" name=\"f_question_" . pad($i,2) . "\" value=\"" . $f_question_array[$i] . "\" size=\"100\">";
                $outputQuestions .= "</td>";
                $outputQuestions .= "<td>";
                $outputQuestions .= "<select class=\"f8np1\" name=\"f_mail_" . pad($i,2) . "\">";
                $outputQuestions .= "  <option value=\"\" " . ($f_mail_array[$i] == "" ? "selected" : "") . ">" . getLngt("Keine Mail") . "</option>";
                $outputQuestions .= "  <option value=\"1\" " . ($f_mail_array[$i] == "1" ? "selected" : "") . ">" . getLngt("Mail bei JA") . "</option>";
                $outputQuestions .= "  <option value=\"2\" " . ($f_mail_array[$i] == "2" ? "selected" : "") . ">" . getLngt("Mail bei NEIN") . "</option>";
                $outputQuestions .= "</select>";
                $outputQuestions .= "</td>";
                $outputQuestions .= "</tr>\n";
            endfor;
         endif;
      endif;
   endif;
   if ($i == "") : $i = 1; else : $i++; endif;
   $outputQuestions .= "<tr>\n";
   $outputQuestions .= "<td>" . $i . ".&nbsp;</td>\n";
   $outputQuestions .= "<td>\n";
   $outputQuestions .= "<input class=\"f10np1\" type=\"text\" name=\"f_question_new\" value=\"\" size=\"100\">";
   $outputQuestions .= "</td>\n";
   $outputQuestions .= "<td>";
   $outputQuestions .= "<select class=\"f8np1\" name=\"f_mail_new\">";
   $outputQuestions .= "  <option value=\"\" selected>" . getLngt("Keine Mail") . "</option>";
   $outputQuestions .= "  <option value=\"1\">" . getLngt("Mail bei JA") . "</option>";
   $outputQuestions .= "  <option value=\"2\">" . getLngt("Mail bei NEIN") . "</option>";
   $outputQuestions .= "</select>";
   $outputQuestions .= "</tr>\n";
   $outputQuestions .= "</table>\n";
   
   
   // **** Prepare output text ****
   // (Re-)Load current text of the customer
   $gdcContentText = getFieldValueFromClause("genericdatacontainer", "gdc_content", "gdc_obj_type = '" . $objType . "' AND gdc_obj_id = '" . $objId . "' AND gdc_gen_fieldname = '" . $specialDbFieldNameText . "'");
   $f_text_array = getKeyValueArrayFromString($gdcContentText);
   $f_text = $f_text_array[$f_service];  // Take the text of the current service
   $outputText .= "<table>";
   $outputText .= "  <tr>";
   $outputText .= "    <td>";
   $outputText .= "      <textarea name=\"f_text\" cols=\"100\" rows=\"10\">" . $f_text . "</textarea>";
   $outputText .= "    </td>";
   $outputText .= "  </tr>";
   $outputText .= "</table>";
endif;


// Only for output
if ($objId != "" && ($objType == "cs" || $objType == "cr")) :
   $cmpId = getFieldValueFromId("customer", "cs_id", $objId, "cmp_id");
   $tmpFields = getFieldsValueFromId("company","cmp_id",$cmpId,array("cmp_comp","cmp_comp2"));
   $title = $tmpFields[0] . " " . $tmpFields[1];
endif;
?>

<html>

<head>
<title><?php echo $pageTitel ?></title>

<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
  <?php include_once ("../css/navigation.css.php"); ?>
</style>

<?php include_once ("../include/js_framework.inc.php"); ?>

<script type="text/javascript">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>


function serviceFinishPage(f_act) {
  document.forms[0].f_act.value = f_act;
  document.forms[0].submit();
};
-->
</script>
</head>

<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?>displayStatusMessage();">

<?php echo $phpMenuOut ?>
<?php echo $phpReducedMenuOut ?>
<?php echo $phpPageTitelOut ?>

<div class="maincontent" name="maincontent" id="maincontent">

<form action="customer_service_acceptance_protocol.php" method="post">
  <input type="hidden" name="f_act" value="">  
  <input type="hidden" name="customerId" value="<?php echo $customerId ?>">
  <input type="hidden" name="cscIdRoot" value="<?php echo $cscIdRoot ?>">
  <input type="hidden" name="cscIdActual" value="<?php echo $cscIdActual ?>">  
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">
  <input type="hidden" name="objType" value="<?php echo $objType ?>">  
  <input type="hidden" name="objId" value="<?php echo $objId ?>">

  <?php echo htmlDivLineSpacer("20px"); ?>

  <?php if ($title != "") : ?>
    <div class="f12bp1_blue">
      <?php echo $title ?>
    </div>
    <?php echo htmlDivLineSpacer("30px"); ?>
  <?php endif; ?>

  <div>
    <div class="f12bp1_blue"><?php echo $pageTitel ?></div>
  </div>
  <?php echo htmlDivLineSpacer("20px"); ?>

  <div>
    <div class="f10np1"><?php echo $output ?></div>
  </div>
  <?php echo htmlDivLineSpacer("20px"); ?>

  <div>
    <div class="f10np1"><?php echo $outputQuestions ?></div>
  </div>
  <?php echo htmlDivLineSpacer("30px"); ?>
  
  <?php if ($f_service != "") : ?>
    <div>
      <div class="f10np1"><?php echo $outputText ?></div>
    </div>
    <?php echo htmlDivLineSpacer("30px"); ?>
  <?php endif; ?>

  <?php if ($f_service != "") : ?>
    <div>
      <?php echo defineButtonType10(getLngt("Speichern"), "action_save", "serviceFinishPage('saveCustomerServicesAcceptanceProtocol');", "150", "left", "3"); ?>
      <?php echo defineButtonType10(getLngt("Schließen"), "action_close", "window.close();", "150", "left", "3"); ?>
    </div>
    <?php echo htmlDivLineSpacer("5px", "", "left"); ?>
  <?php endif; ?>
</form>
</div>
</body>

</html>