<?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;
  protected $dispatcher = null;

  public function __construct()
  {
    $this->dispatcher = $GLOBALS['dispatcher'];
  }

  public function __call($method, $arguments)
  {
    $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'level.extensible_function', array(
      'method'    => $method,
      'arguments' => $arguments
    )));
    if (!$event->isProcessed())
    {
      throw new Exception(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
    }

    return $event->getReturnValue();
  }


  /**
   * 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)
  {
  	// Filter data event + return value
  	$r = $this->dispatcher->filter(new sfEvent($this, 'level.before_datacheck', array('data' => $table_level)), $table_level);
  	$table_level = $r->getReturnValue();
  	
    if (strlen($table_level[0]) < 2) return _t('level','object_notdenomination');
    
    // Notify the beginning of the current method
    $this->dispatcher->notify(new sfEvent($this, 'level.after_datacheck', array('data' => $table_level)));

    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)
  {
  	
  	// Filter data event + return value
  	$r = $this->dispatcher->filter(new sfEvent($this, 'level.before_add', array('data' => $table_level)), $table_level);
  	$table_level = $r->getReturnValue();
  	
    $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';

    $q = "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 ($q, 1);
    
    // Notify the end of the current method
    $this->dispatcher->notify(new sfEvent($this, 'level.after_add', array('data' => $table_level, 'id' => $last_id)));
    
    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)
  {
  	// Filter data event + return value
  	$r = $this->dispatcher->filter(new sfEvent($this, 'level.before_modify', array('data' => $table_level, 'id' => $ID)), $table_level);
  	$table_level = $r->getReturnValue();
  	
    $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';

    $q = "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($q);
    
    // Notify the end of the current method
    $this->dispatcher->notify(new sfEvent($this, 'level.after_modify', array('data' => $table_level, 'id' => $this->ID)));
    
    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)
  {
  	
  	// Notify the end of the current method
  	$this->dispatcher->notify(new sfEvent($this, 'level.change_state', array('id' => $ID, 'state' => $state)));
  	
    $this->ID = $ID;
    strtoupper($state);
    switch ($state) {
      case 'P':
        $this->STATUT = $state;
        break;
      case 'D':
        $this->STATUT = $state;
        break;
      default:
        $this->STATUT = 'P';
        break;
    }
    $q = "UPDATE  " . $this->TDB_LEVEL . " set level_statut='" . $this->STATUT . "' WHERE level_id=" . $this->ID . ";";
    $result = $sql_object->DBQuery($q);
    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;
    }
    
    // Notify the end of the current method
    $this->dispatcher->notify(new sfEvent($this, 'level.delete', array('id' => $ID, 'type' => $type, 'new_id' => $ID_new)));
    
    if ($type == 'MASS_MODIFY') {
      if ($ID_new != -1 && is_numeric($ID_new)) {
        $q = "UPDATE  " . $this->TDB_LEVEL . " set level_statut='E', level_last_modify=NOW() WHERE level_id=" . $this->ID . ";";
        $result = $sql_object->DBQuery($q);
        if ($result == true) {
          $q_news = "UPDATE  " . T_NEWS . " set news_level=" . $ID_new . " WHERE  news_level=" . $this->ID . ";";
          $result = $sql_object->DBQuery($q_news);
          $affected += $sql_object->DBaffectedRow();
          $q_publication = "UPDATE  " . T_PUBLI . " set publi_level=" . $ID_new . " WHERE  publi_level=" . $this->ID . ";";
          $result = $sql_object->DBQuery($q_publication);
          $affected += $sql_object->DBaffectedRow();
        }
      }
    }
    
    if ($type == 'MASS_DELETE') {
      $q = "UPDATE  " . $this->TDB_LEVEL . " set level_statut='E', level_last_modify=NOW() WHERE level_id=" . $this->ID . ";";
      $result = $sql_object->DBQuery($q);
      if ($result == true) {
        $q_news = "UPDATE  " . T_NEWS . " set news_statut='E' WHERE  news_level=" . $this->ID . ";";
        $result = $sql_object->DBQuery($q_news);
        $affected += $sql_object->DBaffectedRow();
        $q_publication = "UPDATE  " . T_PUBLI . " set publi_statut='E' WHERE  publi_level=" . $this->ID . ";";
        $result = $sql_object->DBQuery($q_publication);
        $affected += $sql_object->DBaffectedRow();
      }
    }
    
    return $affected;
  }


}

?>