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

class level {
  /* @param
   * */
  var $TDB_LEVEL = T_LEVEL; // nom de la table.
  var $URI_INPUT = "level/visual/"; // dossier racine de stockage des photos
  var $ID;
  var $NAME;
  var $DESCRIPTION;
  var $COMMENT;
  var $VISUAL_URI;
  var $RANGE;
  var $DATE_CREA;
  var $STATUT;
  var $LAST_MODIFY;

  var $UPLOAD_MAX_MO = UPLOAD_MAX_MO; // taille maximale d'upload des photos en octets
  var $MAX_PHOTO_MAX_WIDTH = 50;
  var $MAX_PHOTO_MIN_WIDTH = 50;

  /**
   * level::CheckDataIntegrity()
   * vérifie l'integrité des données d'un niveau
   *
   * @access public
   * @param array $table_level : contient les composants d'un niveau
   * @param object $sql_object
   * @return boolean true
   * si verifié, sinon string 'message d'erreur'
   */
  function CheckDataIntegrity($table_level)
  {
    if (strlen($table_level[0]) < 2) return $GLOBALS['lang']['level']['object_notdenomination'];

    return true;
  }

  /**
   * level::AddLevel()
   * Ajout d'un niveau
   *
   * @access public
   * @param array $table_level contient les composants d'un niveau
   * @param object $sql_object
   * @return integer $last_id
   */
  function AddLevel($table_level, $sql_object)
  {
    $table_level=$sql_object->DBescape($table_level);
    if ($table_level[0] != '') {
      $this->NAME = strip_input($table_level[0]);
    }
    if ($table_level[1] != '') {
      $this->DESCRIPTION = strip_input($table_level[1], true);
    }
    $this->COMMENT = strip_input($table_level[2], true);

    $this->VISUAL_URI = strip_input($table_level[3]);

    if (is_numeric($table_level[4])) {
      $this->RANGE = $table_level[4];
    }
    if ($table_level[5] != '') {
      strtoupper($table_level[5]);
      switch ($table_level[5]) {
        case 'P':
          $this->STATUT = $table_level[5];
          break;
        case 'D':
          $this->STATUT = $table_level[5];
          break;
        default:
          $this->STATUT = 'P';
          break;
      }
    } else $this->STATUT = 'P';

    $requete = "INSERT INTO " . $this->TDB_LEVEL . " (level_name, level_description, level_comment, level_visual_identity, " . "level_range, level_statut, level_date_crea) " . "VALUES('" . $this->NAME . "', '" . $this->DESCRIPTION . "', '" . $this->COMMENT . "', '" . $this->VISUAL_URI . "', " . $this->RANGE . ", '" . $this->STATUT . "', NOW());";
    $last_id = $sql_object->DBInsert ($requete, 1);
    return $last_id;
  }

  /**
   * level::ModifyLevel()
   * modification d'un niveau
   *
   * @access public
   * @param int $id identifiant niveau a modifier
   * @param array $table_level contient les composants d'un niveau
   * @param object $sql_object
   * @return bool $result
   */
  function ModifyLevel($ID, $table_level, $sql_object)
  {
    $table_level=$sql_object->DBescape($table_level);
    if (is_numeric($ID)) {
      $this->ID = $ID;
    }
    if ($table_level[0] != '') {
      $this->NAME = strip_input($table_level[0]);
    }
    if ($table_level[1] != '') {
      $this->DESCRIPTION = strip_input($table_level[1], true);
    }
    $this->COMMENT = strip_input($table_level[2], true);

    $this->VISUAL_URI = strip_input($table_level[3]);

    if (is_numeric($table_level[4])) {
      $this->RANGE = $table_level[4];
    }
    if ($table_level[5] != '') {
      strtoupper($table_level[5]);
      switch ($table_level[5]) {
        case 'P':
          $this->STATUT = $table_level[5];
          break;
        case 'D':
          $this->STATUT = $table_level[5];
          break;
        default:
          $this->STATUT = 'P';
          break;
      }
    } else $this->STATUT = 'P';

    $requete = "UPDATE  " . $this->TDB_LEVEL . " set level_name='" . $this->NAME . "', level_description='" . $this->DESCRIPTION . "' , level_comment='" . $this->COMMENT . "', level_visual_identity='" . $this->VISUAL_URI . "',level_range=" . $this->RANGE . ", level_statut='" . $this->STATUT . "', level_last_modify=NOW() WHERE level_id=" . $this->ID . ";";
    $result = $sql_object->DBQuery($requete);
    return $result;
  }

  /**
   * level::StateLevel()
   * modification du statut d'un niveau
   * -- NE PREND PAS EN CHARGE l'EFFACEMENT --
   *
   * @access public
   * @param int $ID identifiant du niveau
   * @param string $state 'P' Public | 'D' Draft/
   * @param object $sql_object
   * @return bool $result
   */
  function StateLevel($ID, $state, $sql_object)
  {
    $this->ID = $ID;
    strtoupper($state);
    switch ($state) {
      case 'P':
        $this->STATUT = $state;
        break;
      case 'D':
        $this->STATUT = $state;
        break;
      default:
        $this->STATUT = 'P';
        break;
    }
    $requete = "UPDATE  " . $this->TDB_LEVEL . " set level_statut='" . $this->STATUT . "' WHERE level_id=" . $this->ID . ";";
    $result = $sql_object->DBQuery($requete);
    return $result;
  }

  /**
   * level::DeleteLevel()
   * suppression d'un niveau
   *  -- NON IMPLEMENTEE/NON TESTEE DANS L'APPLICATION --
   *
   * @access public
   * @param int $ID identifiant du niveau
   * @param object $sql_object
   * @param string $type type : 'MASS_DELETE' toutes les ressources associées à l'échelle sont rendues inactives
   * 'MASS_MODIFY' remplacement de l'ancienne échelle par $ID_new
   * @param int $ID_new identifiant du nouveau niveau a rattacher
   * @return object $affected
   */

  function DeleteLevel($ID, $sql_object, $type, $ID_new = -1)
  {
    $affected = 0;
    if (is_numeric($ID)) {
      $this->ID = $ID;
    }
    if ($type == 'MASS_MODIFY') {
      if ($ID_new != -1 && is_numeric($ID_new)) {
        $requete = "UPDATE  " . $this->TDB_LEVEL . " set level_statut='E', level_last_modify=NOW() WHERE level_id=" . $this->ID . ";";
        $result = $sql_object->DBQuery($requete);
        if ($result == true) {
          $requete_news = "UPDATE  " . T_NEWS . " set news_level=" . $ID_new . " WHERE  news_level=" . $this->ID . ";";
          $result = $sql_object->DBQuery($requete_news);
          $affected += $sql_object->DBaffectedRow();
          $requete_publication = "UPDATE  " . T_PUBLI . " set publi_level=" . $ID_new . " WHERE  publi_level=" . $this->ID . ";";
          $result = $sql_object->DBQuery($requete_publication);
          $affected += $sql_object->DBaffectedRow();
        }
      }
    }
    if ($type == 'MASS_DELETE') {
      $requete = "UPDATE  " . $this->TDB_LEVEL . " set level_statut='E', level_last_modify=NOW() WHERE level_id=" . $this->ID . ";";
      $result = $sql_object->DBQuery($requete);
      if ($result == true) {
        $requete_news = "UPDATE  " . T_NEWS . " set news_statut='E' WHERE  news_level=" . $this->ID . ";";
        $result = $sql_object->DBQuery($requete_news);
        $affected += $sql_object->DBaffectedRow();
        $requete_publication = "UPDATE  " . T_PUBLI . " set publi_statut='E' WHERE  publi_level=" . $this->ID . ";";
        $result = $sql_object->DBQuery($requete_publication);
        $affected += $sql_object->DBaffectedRow();
      }
    }
    return $affected;
  }


}

?>
