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

class theme {
  /* @param
   * */
  var $TDB_THEME = T_THEME; // nom de la table.
  var $TDB_SDTHEME = T_SD_THEME; // nom de la table.
  var $ID;
  var $NAME;
  var $VISUAL_URI;
  var $ROOT;
  var $SD_THEME;
  var $DESC_PROJECT;
  var $DESC_DASHBOARD;
  var $DESC_PUBLICATION;
  var $DESC_NEWS;
  var $DESC_WORKSHOP;
  var $RANGE_PROJECT;
  var $RANGE_DASHBOARD;
  var $RANGE_PUBLICATION;
  var $RANGE_NEWS;
  var $RANGE_WORKSHOP;
  var $DATE_CREA;
  var $STATUT;
  var $LAST_MODIFY;

  /**
   * theme::CheckDataIntegrity()
   * Vérification des données d'un thème
   *
   * @access public
   * @param array $table_theme : contient les composants d'un thème
   * @return boolean si vrai renvoie true sinon message d'erreurs (string)
   */
  function CheckDataIntegrity($table_theme)
  {
    if (strlen($table_theme[0]) < 2) return _t('theme','object_notdenomination');

    if(!$this->isRoot()) {
      if (!is_numeric($table_theme[2]) || $table_theme[2] == -1) return _t('theme','object_notSDtheme');

      if (defined('MOD_PROJECT') && strlen($table_theme[3]) < 2) return _t('theme','object_notdesc_completed');
      if (defined('MOD_SDI') && strlen($table_theme[4]) < 2) return _t('theme','object_notdesc_completed');
      if (defined('MOD_PUBLICATION') && strlen($table_theme[5]) < 2) return _t('theme','object_notdesc_completed');
      if (defined('MOD_NEWS') && strlen($table_theme[6]) < 2) return _t('theme','object_notdesc_completed');
      if (defined('MOD_WORKSHOP') && strlen($table_theme[7]) < 2) return _t('theme','object_notdesc_completed');

    }
    return true;
  }

  /**
   * theme::AddTheme()
   * Ajout d'un nouveau theme
   *
   * @access public
   * @param array $table_theme : contient les composants d'un theme
   * @param object $sql_object
   * @return integer $last_id
   */
  function AddTheme($table_theme, $sql_object)
  {
    $table_theme=$sql_object->DBescape($table_theme);
    if ($table_theme[14] == 1) $this->SetRoot();

    if ($table_theme[0] != '') {
      $this->NAME = strip_input(trim($table_theme[0]));
    }
    if ($table_theme[1] != '') {
      $this->VISUAL_URI = strip_input(trim($table_theme[1]));
    }
    if (is_numeric($table_theme[2])) {
      $this->SD_THEME = $table_theme[2];
    }
    $this->DESC_PROJECT = strip_input(trim($table_theme[3]), true);
    $this->DESC_DASHBOARD = strip_input(trim($table_theme[4]), true);
    $this->DESC_PUBLICATION = strip_input(trim($table_theme[5]), true);
    $this->DESC_NEWS = strip_input(trim($table_theme[6]), true);
    $this->DESC_WORKSHOP = strip_input(trim($table_theme[7]), true);

    if (is_numeric($table_theme[8])) {
      $this->RANGE_PROJECT = $table_theme[8];
    }
    if (is_numeric($table_theme[9])) {
      $this->RANGE_DASHBOARD = $table_theme[9];
    }
    if (is_numeric($table_theme[10])) {
      $this->RANGE_PUBLICATION = $table_theme[10];
    }
    if (is_numeric($table_theme[11])) {
      $this->RANGE_NEWS = $table_theme[11];
    }
    if (is_numeric($table_theme[12])) {
      $this->RANGE_WORKSHOP = $table_theme[12];
    }

    if ($table_theme[13] != '') {
      strtoupper($table_theme[13]);
      switch ($table_theme[13]) {
        case 'P':
          $this->STATUT = $table_theme[13];
          break;
        case 'D':
          $this->STATUT = $table_theme[13];
          break;
        default:
          $this->STATUT = 'P';
          break;
      }
    } else $this->STATUT = 'P';

    if($this->isRoot()) {
      $query = 	"INSERT INTO " . $this->TDB_SDTHEME .
                " (sdtheme_name, sdtheme_description, sdtheme_date_crea) " .
                "VALUES('".$this->NAME."','".$this->DESC_PROJECT . "', NOW());";
    } else {
      $query = "INSERT INTO " . $this->TDB_THEME .
    		 " (theme_name, theme_visual_uri, theme_sd_theme, theme_desc_project, theme_desc_dashboard, " . 
    		 "theme_desc_publication, theme_desc_news, theme_desc_workshop, theme_range_project, ". 
    		 "theme_range_dashboard, theme_range_publication, theme_range_news, theme_range_workshop, " .
             "theme_statut, theme_date_crea) " . 
    		 "VALUES('" . $this->NAME . "', '" . $this->VISUAL_URI . "', " . $this->SD_THEME . 
    		 ", '" . $this->DESC_PROJECT ."', '" . $this->DESC_DASHBOARD . 
    		 "', '" . $this->DESC_PUBLICATION . "', '" . $this->DESC_NEWS . 
    		 "', '" . $this->DESC_WORKSHOP . "', " . $this->RANGE_PROJECT . 
    		 ", " . $this->RANGE_DASHBOARD . ", " . $this->RANGE_PUBLICATION . 
    		 ", " . $this->RANGE_NEWS . ", " . $this->RANGE_WORKSHOP . ", '" . $this->STATUT . "', NOW());";
    }
    $last_id = $sql_object->DBInsert ($query, 1);
    return $last_id;
  }

  /**
   * theme::ModifyTheme()
   * modification d'un theme
   *
   * @access public
   * @param int $ID : identifiant du theme
   * @param array $table_theme : contient les composants d'un theme
   * @param object $sql_object
   * @return bool $result
   */
  function ModifyTheme($ID, $table_theme, $sql_object)
  {
    $table_theme=$sql_object->DBescape($table_theme);

    if ($table_theme[9] == 1) $this->SetRoot();
    if (is_numeric($ID)) {
      $this->ID = $ID;
    }
    if ($table_theme[0] != '') {
      $this->NAME = strip_input(trim($table_theme[0]));
    }
    if ($table_theme[1] != '') {
      $this->VISUAL_URI = strip_input(trim($table_theme[1]));
    }
    if (is_numeric($table_theme[2])) {
      $this->SD_THEME = $table_theme[2];
    }
    $this->DESC_PROJECT = strip_input(trim($table_theme[3]), true);
    $this->DESC_DASHBOARD = strip_input(trim($table_theme[4]), true);
    $this->DESC_PUBLICATION = strip_input(trim($table_theme[5]), true);
    $this->DESC_NEWS = strip_input(trim($table_theme[6]), true);
    $this->DESC_WORKSHOP = strip_input(trim($table_theme[7]), true);
    if ($table_theme[8] != '') {
      strtoupper($table_theme[8]);
      switch ($table_theme[8]) {
        case 'P':
          $this->STATUT = $table_theme[8];
          break;
        case 'D':
          $this->STATUT = $table_theme[8];
          break;
        default:
          $this->STATUT = 'P';
          break;
      }
    } else $this->STATUT = 'P';

    if($this->isRoot()) {
      $query = "UPDATE  " . $this->TDB_SDTHEME . " set sdtheme_name='" . $this->NAME . "', sdtheme_description='" . $this->DESC_PROJECT . "', sdtheme_last_modify=NOW() WHERE sdtheme_id='" . $this->ID . "';";

    } else {
      $query = "UPDATE  " . $this->TDB_THEME . " set theme_name='" . $this->NAME . "', theme_visual_uri='" . $this->VISUAL_URI . "' , theme_sd_theme='" . $this->SD_THEME . "', theme_desc_project='" . $this->DESC_PROJECT . "', theme_desc_dashboard='" . $this->DESC_DASHBOARD . "',theme_desc_publication= '" . $this->DESC_PUBLICATION . "', theme_desc_news='" . $this->DESC_NEWS . "' , theme_desc_workshop='" . $this->DESC_WORKSHOP . "' , theme_statut='" . $this->STATUT . "', theme_last_modify=NOW() WHERE theme_id='" . $this->ID . "';";
    }

    $result = $sql_object->DBQuery($query);
    return $result;
  }

  /**
   * theme::StateTheme()
   * modification du statut d'un theme
   * 	-- NE PREND PAS EN CHARGE l'EFFACEMENT --
   *
   * @access public
   * @param int $ID identifiant de l'entreprise
   * @param string $state (facultatif) 'P' Public/'D' Draft
   * @param object $sql_object
   * @return bool $result
   */
  function StateTheme($ID, $state, $sql_object)
  {
    if (is_numeric($ID)) {
      $this->ID = $ID;
    }
    if ($state != '') {
      strtoupper($state);
      switch ($state) {
        case 'P':
          $this->STATUT = $state;
          break;
        case 'D':
          $this->STATUT = $state;
          break;
        default:
          $this->STATUT = $state;
          break;
      }
    } else $this->STATUT = 'P';
    $query = "UPDATE  " . $this->TDB_THEME . " set theme_statut='" . $this->STATUT . "', theme_last_modify=NOW() WHERE theme_id=" . $this->ID . ";";
    $result = $sql_object->DBQuery($query);
    return $result;
  }

  /**
   * theme::DeleteTheme()
   * suppression d'un theme
   *
   * @access public
   * @param int $ID identifiant du theme
   * @param string $type 'MASS_DELETE' toutes les ressources associées aus themes sont rendues inactives
   * 'MASS_MODIFY' remplacement de l'ancien theme par $ID_new
   * @param int $id_new nouvel id de rattachement
   * @param object $sql_object
   * @return bool $result
   */
  function DeleteTheme($ID, $sql_object, $type, $ID_new = -1)
  {
    $affected = 0;
    if (is_numeric($ID)) {
      $this->ID = $ID;
    }
    if($this->isRoot()) {
      $query = "UPDATE  " . $this->TDB_THEME . " set theme_sd_theme=" . $ID_new . " WHERE  theme_sd_theme=" . $this->ID . ";";
      $result = $sql_object->DBQuery($query);
      $affected += $sql_object->DBaffectedRow();
      $query = "DELETE FROM  " . $this->TDB_SDTHEME . " where sdtheme_id='".$this->ID ."';";
      $result = $sql_object->DBQuery($query);
    }
    if($this->isRoot() == false) {
      if ($type == 'MASS_MODIFY') {
        if ($ID_new != -1 && is_numeric($ID_new)) {
          $query = "UPDATE  " . $this->TDB_THEME . " set theme_statut='E', theme_last_modify=NOW() WHERE theme_id=" . $this->ID . ";";
          $result = $sql_object->DBQuery($query);
          if ($result == true) {
            $query_news = "UPDATE  " . T_NEWS . " set news_theme=" . $ID_new . " WHERE  news_theme=" . $this->ID . ";";
            $result = $sql_object->DBQuery($query_news);
            $affected += $sql_object->DBaffectedRow();
            $query_publication = "UPDATE  " . T_PUBLI . " set publi_theme=" . $ID_new . " WHERE  publi_theme=" . $this->ID . ";";
            $result = $sql_object->DBQuery($query_publication);
            $affected += $sql_object->DBaffectedRow();
            $query_workshop = "UPDATE  " . T_WORK . " set workshop_theme=" . $ID_new . " WHERE  workshop_theme=" . $this->ID . ";";
            $result = $sql_object->DBQuery($query_workshop);
            $affected += $sql_object->DBaffectedRow();
            $query_sdiinfo = "UPDATE  " . T_SDI_INFO . " set sdii_theme=" . $ID_new . " WHERE  sdii_theme=" . $this->ID . ";";
            $result = $sql_object->DBQuery($query_workshop);
            $affected += $sql_object->DBaffectedRow();
            $query_yellowpages = "UPDATE  " . T_YELLOWPAGES . " set yellowp_theme=" . $ID_new . " WHERE  yellowp_theme=" . $this->ID . ";";
            $result = $sql_object->DBQuery($query_yellowpages);
            $affected += $sql_object->DBaffectedRow();
          }
        }
      }
      if ($type == 'MASS_DELETE') {
        $query = "UPDATE  " . $this->TDB_THEME . " SET theme_statut='E', theme_last_modify=NOW() WHERE theme_id=" . $this->ID . ";";
        $result = $sql_object->DBQuery($query);
        if ($result == true) {
          $query_news = "UPDATE  " . T_NEWS . " SET news_statut='E' WHERE  news_theme=" . $this->ID . ";";
          $result = $sql_object->DBQuery($query_news);
          $affected += $sql_object->DBaffectedRow();
          $query_publication = "UPDATE  " . T_PUBLI . " SET publi_statut='E' WHERE  publi_theme=" . $this->ID . ";";
          $result = $sql_object->DBQuery($query_publication);
          $affected += $sql_object->DBaffectedRow();
          $query_workshop = "UPDATE  " . T_WORK . " SET workshop_statut='E' WHERE  workshop_theme=" . $this->ID . ";";
          $result = $sql_object->DBQuery($query_workshop);
          $affected += $sql_object->DBaffectedRow();
          $query_sdiinfo = "UPDATE " . T_SDI_INFO . " SET sdii_statut='E' WHERE  sdii_theme=" . $this->ID . ";";
          $result = $sql_object->DBQuery($query_sdiinfo);
          $affected += $sql_object->DBaffectedRow();
          $query_sdivalue = "UPDATE " . T_SDI_VALUE . " SET sdiv_statut='E' WHERE sdiv_sdi_info=". $this->ID . ";";
          $result = $sql_object->DBQuery($query_sdivalue);
          $affected += $sql_object->DBaffectedRow();
          $query_yellowpages = "UPDATE  " . T_YELLOWPAGES . " SET yellowp_statut='AA' WHERE  yellowp_theme=" . $this->ID . ";";
          $result = $sql_object->DBQuery($query_yellowpages);
          $affected += $sql_object->DBaffectedRow();
        }
      }
    }
    return $affected;
  }

  /**
   * theme::SetRoot()
   * Set the ROOT attribute to 1
   *
   * @access public
   * @return void
   */
  function SetRoot()
  {
    $this->ROOT = 1;
  }

  /**
   * theme::isRoot()
   * Is the current theme root?
   *
   * @access public
   * @return bool
   */
  function isRoot()
  {
    if($this->ROOT == 1) {
      return true;
    } else{
      return false;
    }
  }

  /**
   * theme::SetThemeRange()
   * modification des RANGE des themes
   *   --- NON IMPLEMENTE/NON TESTE ---
   *
   * @access public
   * @param string $module spécifie le module concernée : valeur 'DASHBOARD', 'PUBLICATION', 'NEWS', 'WORKSHOP', 'ALL'
   * @param array $table_range tableau contenant les id des themes
   * @param object $sql_object
   * @return bool $result
   */
  /**
   * function SetThemeRange($table_range, $sql_object, $module)
   * {
   * $result = false;
   * if (count($table_range) > 0) {
   * if (strlen($module) > 2) {
   * if ($module != 'ALL') {
   * $champ = 'theme_range_';
   * if ($module == 'DASHBOARD') $champ .= 'dashboard';
   * if ($module == 'PUBLICATION') $champ .= 'publication';
   * if ($module == 'NEWS') $champ .= 'news';
   * if ($module == 'WORKSHOP') $champ .= 'workshop';
   * for($i = 0; $i < count($table_range); $i++) {
   * $range = $i + 1;
   * $query = "UPDATE  " . $this->TDB_THEME . " SET " . $champ . "=" . $range . " WHERE theme_id='" . $table_range[$i] . "';";
   * $result = $sql_object->DBQuery($query);
   * echo $query . "<br>";
   * }
   * } elseif ($module == 'ALL') {
   * for($i = 0; $i < count($table_range); $i++) {
   * $range = $i + 1;
   * $to_update = "theme_range_dashboard=" . $range . ", theme_range_publication=" . $range . ", theme_range_news=" . $range . ", theme_range_workshop=" . $range;
   * $query = "UPDATE  " . $this->TDB_THEME . " SET " . $to_update . " WHERE theme_id=" . $table_range[$i] . ";";
   * $result = $sql_object->DBQuery($query);
   * echo $query . "<br>";
   * }
   * }
   * }
   * }
   *
   * return $result;
   * }
   */
}

?>
