<?php
/*=======================================================================
 *
 * inc_category_dbfields.inc.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/


// Gets the "supersetfield" from the database.
// and sets the following fields which have to be defined before calling this function.
// VARS: $idArray, $fieldArray, $titleArray, $supersetFieldString
// Usage e.g. in EXPORT and STATISTIC
function getCategoryDBFields($f_exp_category) {
  global $db, $idArray, $fieldArray, $titleArray, $fieldSortArray, $supersetFieldString;
  $idArrayLen = 0;

  if ($f_exp_category != "0") :
     $sqlquery = "SELECT expcf_id, expcf_field, expcf_field_sql, expcf_title, expcf_field_sort"
                 . " FROM exportcategoryfields"
                 . " WHERE expc_id = '$f_exp_category'"               
                 . " ORDER BY expcf_id";         

     $result = $db->query($sqlquery);                    
     if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());                                      
     // all defined db-fields and titles shall become content of the following arrays
     while ($row = $result->fetch_assoc()):
           $idArray[]    = $row["expcf_id"];
           $fieldArray[] = $row["expcf_field"];
           $fieldSqlArray[] = $row["expcf_field_sql"];
           $titleArray[] = $row["expcf_title"];
           $fieldSortArray[] = $row["expcf_field_sort"];
     endwhile;
     $result->free();

     // $fieldSqlArray[] ONLY is for the generation of the fields of the SQL-SELECT statement in $supersetFieldString
     if ($fieldArray != "") : $supersetFieldString = implode(",", $fieldSqlArray); endif;
  endif;

  $idArrayLen = count($idArray);    
  return $idArrayLen;
}


// Expands category fields (insert or removes an element or changes sorting)
// PARAMETERS:
// $elementArray : (<id><fieldName>,<fieldSqlName>,<titleOfField>,<orderByFieldNames>)
// $mode         : (= 0) <=> Inserts the element on position "$position"  /  (= 1) <=> Removes an element by name or position (if set)
// $position     : Position an element will be inserted ("0" <=> at the beginning  /  "" <=> at the end)
function changeCategoryDBFields($elementArray, $mode = "0", $position = "", $offset = "") {
  global $db, $idArray, $fieldArray, $titleArray, $fieldSqlArray, $fieldSortArray, $alignArray, $supersetFieldString;

  $supersetFieldString = "";

  // Insert element
  if ($mode == "0") :
     if (count($elementArray) > 0) :
        if ($position == "" || !is_numeric($position)) :
           $position = count($idArray);
        endif;

        if (count($idArray) > 0) :
           array_splice($idArray, $position, 0, array($elementArray[0]));
        else : 
           $idArray = array($elementArray[0]);
        endif;
        if (count($fieldArray) > 0) :
           array_splice($fieldArray, $position, 0, array($elementArray[1]));
        else :
           $fieldArray = array($elementArray[1]);
        endif;
        if (count($fieldSqlArray) > 0) :
           array_splice($fieldSqlArray, $position, 0, array($elementArray[2]));
        else :
           $fieldSqlArray = array($elementArray[2]);
         endif;
        if (count($titleArray) > 0) : 
           array_splice($titleArray, $position, 0, array($elementArray[3]));
        else :
           $titleArray = array($elementArray[3]);
        endif;
        if (count($fieldSortArray) > 0) :
           array_splice($fieldSortArray, $position, 0, array($elementArray[4]));
        else :
           $fieldSortArray = array($elementArray[4]);
        endif;
        if (count($alignArray) > 0) :
           array_splice($alignArray, $position, 0, array($elementArray[5]));
        else :
           $alignArray = array($elementArray[5]);
        endif;
     endif;
  endif;

  // Remove element
  if ($mode == "1") :
     if ($position == "" || !is_numeric($position)) :
        $position = count($idArray) - 1;  // Remove last element by default if no position is specified
     endif;
     if ($offset == "" || !is_numeric($offset)) :
        $offset = count($idArray);   // Init to last element to be removed
     endif;

     if (count($idArray) > 0) :        array_splice($idArray, $position, $offset);        endif;
     if (count($fieldArray) > 0) :     array_splice($fieldArray, $position, $offset);     endif;
     if (count($fieldSqlArray) > 0) :  array_splice($fieldSqlArray, $position, $offset);  endif;
     if (count($titleArray) > 0) :     array_splice($titleArray, $position, $offset);     endif;
     if (count($fieldSortArray) > 0) : array_splice($fieldSortArray, $position, $offset); endif;
     if (count($alignArray) > 0) :     array_splice($alignArray, $position, $offset);     endif;
  endif;

  // $fieldSqlArray[] ONLY is for the generation of the fields of the SQL-SELECT statement in $supersetFieldString
  if ($fieldSqlArray != "") : $supersetFieldString = implode(",", $fieldSqlArray); endif;

  $idArrayLen = count($idArray);    
  return $idArrayLen;
}
?>