<?php
/**
 * @package linea21.utils
 * @subpackage lib
 * @author linea21 <info@linea21.com>
 * @version $id SVN
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

/**
 * ThemeSelectBox()
 * Génére une select-box pour les thèmes
 *
 * @param array $data
 * @param string $field_name
 * @param integer $current_theme
 * @param string $URL
 * @return string $select_theme
 */
function ThemeSelectBox($data, $field_name, $current_theme = -1, $URL = -1)
{
  $current_sdtheme = '';
  $theme_list = '';
  $close = '';
  for ($i = 0;$i < count($data);$i++) {
    if ($data[$i]['sdtheme_id'] != $current_sdtheme) {
      $current_sdtheme = $data[$i]['sdtheme_id'];
      $current_sdtheme_display = formatText(cutText($data[$i]['sdtheme_name'], 46));
      $theme_list .= $close . "<optgroup label=\"" . $current_sdtheme_display . "\">\n";
      $close = "</optgroup>\n";
    }
    if ($data[$i]['theme_id'] == $current_theme) {
      $mask = " selected=\"selected\"";
    } else $mask = "";
    $current_theme_display = formatText(cutText($data[$i]['theme_name'], 46));
    if ($URL <> -1)$theme_list .= "<option value=\"" . $URL . $data[$i]['theme_id'] . "\"" . $mask . ">" . $current_theme_display . "</option>\n";
    else $theme_list .= "<option value=\"" . $data[$i]['theme_id'] . "\"" . $mask . ">" . $current_theme_display . "</option>\n";
  }
  $theme_list .= "</optgroup>\n";
  if ($URL <> -1) {
    $select_theme = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" onchange=\"ChangeLocation(this)\" class=\"textfield\">\n";
    $select_theme .= "<option value=\"-1\">" . _t('divers','select_choice') . "</option>\n";
  } else {
    $select_theme = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" class=\"textfield\">\n";
    $select_theme .= "<option value=\"-1\">" . _t('divers','select_choice') . "</option>\n";
  }
  $select_theme .= $theme_list;
  $select_theme .= "</select>\n";

  return $select_theme;
}

/**
 * SDThemeSelectBox()
 * Génére une select-box pour les thèmes Développement Durable
 *
 * @param array $data
 * @param string $field_name
 * @param integer $current_sdtheme
 * @return string $select_sdtheme
 */
function SDThemeSelectBox($data, $field_name, $current_sdtheme = -1)
{
  $sdtheme_list = '';
  for($i = 0; $i < count($data); $i++) {
    if ($data[$i]['sdtheme_id'] == $current_sdtheme) $mask = " selected=\"selected\"";
    else $mask = "";
    $sdtheme_display = formatText(cutText($data[$i]['sdtheme_name'], 46), '2HTML');
    $sdtheme_list .= "<option value=\"" . $data[$i]['sdtheme_id'] . "\"" . $mask . ">" . $sdtheme_display . "</option>\n";
  }
  $select_sdtheme = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" class=\"textfield\">";
  $select_sdtheme .= "<option value=\"-1\">" . _t('divers','select_choice2') . "</option>";
  $select_sdtheme .= $sdtheme_list;
  $select_sdtheme .= "</select>";

  return $select_sdtheme;
}

/**
 * LevelSelectBox()
 * Génére une select-box pour les échelles
 *
 * @param array $data
 * @param string $field_name
 * @param integer $current_level
 * @return string $select_level
 */
function LevelSelectBox($data, $field_name, $current_level = -1)
{
  $level_list = '';
  for ($i = 0;$i < count($data);$i++) {
    if ($data[$i]['level_id'] == $current_level) {
      $mask = " selected=\"selected\"";
    } else $mask = "";
    $level_display = formatText(cutText($data[$i]['level_name'], 46));
    $level_list .= "<option value=\"" . $data[$i]['level_id'] . "\"" . $mask . ">" . $level_display . "</option>\n";
  }
  $select_level = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" class=\"textfield\">\n";
  $select_level .= "<option value=\"-1\">" . _t('divers','select_choice') . "</option>\n";
  $select_level .= $level_list;
  $select_level .= "</select>\n";

  return $select_level;
}

/**
 * ScaleSelectBox()
 * Génére une select-box pour les échelles
 *
 * @param array $data
 * @param string $field_name
 * @param integer $current_scale
 * @param string $URL
 * @return string $select_scale
 */
function ScaleSelectBox($data, $field_name, $current_scale = -1, $URL = -1)
{
  $scale_list = '';
  for ($i = 0;$i < count($data);$i++) {
    if ($data[$i]['scale_id'] == $current_scale) {
      $mask = " selected=\"selected\"";
    } else $mask = "";
    $scale_display = formatText(cutText($data[$i]['scale_denomination'], 46));
    if ($URL <> -1)$scale_list .= "<option value=\"" . $URL . $data[$i]['scale_id'] . "\"" . $mask . ">" . $scale_display . "</option>\n";
    else $scale_list .= "<option value=\"" . $data[$i]['scale_id'] . "\"" . $mask . ">" . $scale_display . "</option>\n";
  }
  if ($URL <> -1) {
    $select_scale = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" onchange=\"ChangeLocation(this)\">\n";
    $select_scale .= "<option value=\"-1\">" . _t('divers','select_choice') . "</option>\n";
  } else {
    $select_scale = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" class=\"textfield\">\n";
    $select_scale .= "<option value=\"-1\">" . _t('divers','select_choice') . "</option>\n";
  }
  $select_scale .= $scale_list;
  $select_scale .= "</select>\n";

  return $select_scale;
}

/**
 * PrioritySelectBox()
 * Génére une select-box pour les priorités
 *
 * @param array $data
 * @param string $field_name
 * @param integer $current_priority
 * @param string $URL
 * @return string $select_priority
 */
function PrioritySelectBox($data, $field_name, $current_priority = -1, $URL = -1)
{
  $priority_list = '';
  for ($i = 0;$i < count($data);$i++) {
    if ($data[$i]['priority_id'] == $current_priority) {
      $mask = " selected=\"selected\"";
    } else $mask = "";
    $priority_display = formatText(cutText($data[$i]['priority_name'], 46));
    if ($URL <> -1) $priority_list .= "<option value=\"" . $URL . $data[$i]['priority_id'] . "\"" . $mask . ">" . $priority_display . "</option>\n";
    else $priority_list .= "<option value=\"" . $data[$i]['priority_id'] . "\"" . $mask . ">" . $priority_display . "</option>\n";
  }
  if ($URL <> -1) {
    $select_priority = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" onchange=\"ChangeLocation(this)\">\n";
    $select_priority .= "<option value=\"-1\">" . _t('divers','select_choice') . "</option>\n";
  } else {
    $select_priority = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" class=\"textfield\">\n";
    $select_priority .= "<option value=\"-1\">" . _t('divers','select_choice') . "</option>\n";
  }
  $select_priority .= $priority_list;
  $select_priority .= "</select>\n";

  return $select_priority;
}

/**
 * ProjectSelectBox()
 * Génére une select-box pour les projets
 *
 * @param array $data
 * @param string $field_name
 * @param integer $current_project
 * @param string $URL
 * @return string $select_project
 */
function ProjectSelectBox($data, $field_name, $current_project = -1, $URL = -1, $exclude= array())
{

  $project_list = '';
  for ($i = 0;$i < count($data);$i++) {
    if(!in_array($data[$i]['project_id'], $exclude)) {
      if ($data[$i]['project_id'] == $current_project) {
        $mask = " selected=\"selected\"";
      } else $mask = "";
      $project_display = formatText(cutText($data[$i]['project_name'], 46));
      if ($URL <> -1) $project_list .= "<option value=\"" . $URL . $data[$i]['project_id'] . "\"" . $mask . ">" . $project_display . "</option>\n";
      else $project_list .= "<option value=\"" . $data[$i]['project_id'] . "\"" . $mask . ">" . $project_display . "</option>\n";
    }
  }
  if ($URL <> -1) {
    $select_project = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" onchange=\"ChangeLocation(this)\">\n";
    $select_project .= "<option value=\"-1\">" . _t('divers','none') . "</option>\n";
  } else {
    $select_project = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" class=\"textfield\">\n";
    $select_project .= "<option value=\"-1\">" . _t('divers','none') . "</option>\n";
  }
  $select_project .= $project_list;
  $select_project .= "</select>\n";

  return $select_project;
}

/**
 * WorkshopSelectBox()
 * Generate a workshop select-box
 *
 * @param array $data
 * @param string $field_name
 * @param integer $current_level
 * @return string $select_level
 */
function WorkshopSelectBox($data, $field_name, $current = -1)
{
  $list = '';
  for ($i = 0;$i < count($data);$i++) {
    if ($data[$i]['workshop_id'] == $current) {
      $mask = " selected=\"selected\"";
    } else $mask = "";
    $display = formatText(cutText($data[$i]['workshop_denomination'], 46));
    if($data[$i]['workshop_id'] <> "") $list .= "<option value=\"" . $data[$i]['workshop_id'] . "\"" . $mask . ">" . $display . "</option>\n";
  }
  $select = "<select name=\"" . $field_name . "\" id=\"" . $field_name . "\" class=\"textfield\">\n";
  $select .= "<option value=\"-1\">" . _t('divers','select_choice') . "</option>\n";
  $select .= $list;
  $select .= "</select>\n";

  return $select;
}

/**
 * SDIMultiSelectBox()
 * Generate a SDI multiple select-box
 * (using jquery)
 * @param array $data
 * @param string $field_name
 * @param integer $current_level
 * @return string $select_level
 */
function SDIMultiSelectBox($data, $field_name, $current)
{
  $list = '';
  for ($i = 0;$i < count($data);$i++) {
    if (in_array ($data[$i]['sdii_id'], $current)) {
      $mask = " selected=\"selected\"";
    } else $mask = "";
    $display = formatText(cutText($data[$i]['sdii_name'], 200));
    $list .= "<option value=\"" . $data[$i]['sdii_id'] . "\"" . $mask . ">" . $display . "</option>\n";
  }
  $select  = "<select name=\"" . $field_name . "[]\" id=\"" . $field_name . "\" multiple=\"multiple\">\n";
  //$select .= "<option value=\"-1\">" . _t('divers','select_choice') . "</option>\n";
  $select .= $list;
  $select .= "</select>\n";
  

  return $select;
}

/**
 * input_date()
 * Return input fields 
 * with good direction
 * depending on DATE_FORMAT
 * as filter criteria
 * @param string $y
 * @param string $m
 * @param string $d
 * @return string $content
 */
function input_date($y, $m, $d) {
  
  $content = '';

  if(DATE_FORMAT == 'dd-mm-yyyy') 
  {
    $content.='<input name="date_d" type="text" class="shorttextfield" size="2" id="date_d" maxlength="2" value="'.$d.'" />'.END_LINE;
	$content.='<input name="date_m" type="text" class="shorttextfield" size="2" id="date_m" maxlength="2" value="'.$m.'" />'.END_LINE;
	$content.='<input name="date_y" type="text" class="shorttextfield" size="4" id="date_y" maxlength="4" value="'.$y.'" />'.END_LINE;
  } 
  elseif(DATE_FORMAT == 'yyyy-mm-dd') 
  {
    $content.='<input name="date_y" type="text" class="shorttextfield" size="4" id="date_y" maxlength="4" value="'.$y.'" />'.END_LINE;
	$content.='<input name="date_m" type="text" class="shorttextfield" size="2" id="date_m" maxlength="2" value="'.$m.'" />'.END_LINE;
	$content.='<input name="date_d" type="text" class="shorttextfield" size="2" id="date_d" maxlength="2" value="'.$d.'" />'.END_LINE;
  } 
  else 
  {
    die(ERROR_SYSTEM);
  }
  return $content;
}

?>