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

class publication {
  /* @param
   * */
  var $TDB_PUBLI = T_PUBLI; // nom de la table.
  var $ID;
  var $TITLE;
  var $RESUME;
  var $THEME;
  var $SCALE;
  var $LEVEL;
  var $POSTED_BY;
  var $PUBLISHED_DATE;
  var $COMMENT;
  var $DATE_CREA;
  var $LAST_MODIFY;
  var $STATUT;

  /**
   * publication::CheckDataIntegrity()
   * Vérification intégrité des données
   *
   * @access public
   * @param array $table : contient les composants d'une publication
   * @return boolean true
   * si verifié, sinon string 'message d'erreur'
   */
  function CheckDataIntegrity($table, $sql_object)
  {
    if (strlen($table[0]) < 3) return $GLOBALS['lang']['publication']['no_title'];
    if (strlen($table[1]) < 3) return $GLOBALS['lang']['publication']['no_resume'];
    if ($table[2] == -1) return $GLOBALS['lang']['publication']['no_theme'];
    if ($table[3] == -1) return $GLOBALS['lang']['publication']['no_scale'];
    if ($table[4] == -1) return $GLOBALS['lang']['publication']['no_level'];
    if (strlen(trim($table[5])) < 2) return $GLOBALS['lang']['publication']['no_author'];
    $result = $this->_CheckUserValidity($table[5], $sql_object);
    if (!is_numeric($result['user_id'])) return $GLOBALS['lang']['publication']['author_not_valid'];
    if ($result['publi_right'] == 'U') return $GLOBALS['lang']['publication']['author_not_rights'];
    else return $result;
  }

  /**
   * publication::_CheckUserValidity()
   * Vérification validité de l'utilisateur
   *
   * @access private
   * @param string $login
   * @param object $sql_object
   * @return array contenant $user_id et droit si login associé au workshop
   * sinon renvoie false
   */
  function _CheckUserValidity($login, $sql_object)
  {
    $user = array('user_id' => '', 'publi_right' => '');

    $requete = "SELECT user_id, rights_publication FROM " . T_USER . " LEFT OUTER JOIN " . T_RIGHT . " ON user_rights=rights_id WHERE user_login= '" . $login . "' AND user_validity='Y';";
    $result = $sql_object->DBSelect($requete);

    if ($result == 0) return false;
    if (count($result) > 1) exit();
    else {
      $user['user_id'] = $result[0]['user_id'];
      $user['publi_right'] = $result[0]['rights_publication'];
    }
    return $user;
  }

  /**
   * publication::AddPublication()
   * Ajout d'une nouvelle publication/dossier
   *
   * @access public
   * @param array $table_publi contient les composants d'une publication
   * @param object $sql_object
   * @return integer $last_id
   */
  function AddPublication($table_publi, $sql_object)
  {
    $table_publi=$sql_object->DBescape($table_publi);

    $this->TITLE = strip_input(trim($table_publi[0]), true);
    $this->RESUME = strip_input(trim($table_publi[1]), true);
    $this->THEME = $table_publi[2];
    $this->SCALE = $table_publi[3];
    $this->LEVEL = $table_publi[4];
    $this->POSTED_BY = $table_publi[5];
    $this->COMMENT = strip_input(trim($table_publi[6], true));

    if ($table_publi[7] != '') {
      $this->STATUT = strtoupper($table_publi[7]);
    }
    if ($this->STATUT == 'P') {
      $this->PUBLISHED_DATE = "NOW()";
    } else {
      $this->PUBLISHED_DATE = "'0001-01-01'";
    }
    $requete = "INSERT INTO " . $this->TDB_PUBLI . " (publi_title, publi_resume, publi_theme, publi_scale, publi_level, publi_posted_by, publi_published_date, publi_comment, publi_statut, publi_date_crea) VALUES('" . $this->TITLE . "', '" . $this->RESUME . "', " . $this->THEME . ", " . $this->SCALE . ", " . $this->LEVEL . ", " . $this->POSTED_BY . ", " . $this->PUBLISHED_DATE . ", '" . $this->COMMENT . "', '" . $this->STATUT . "', NOW());";
    $last_id = $sql_object->DBInsert ($requete, 1);
    return $last_id;
  }

  /**
   * publication::ModifyPublication()
   * modification d'une publication
   *
   * @access public
   * @param int $ID identifiant de la publication
   * @param array $table_publi contient les composants d'une publication
   * @param object $sql_object
   * @return bool $result
   */
  function ModifyPublication($ID, $table_publi, $sql_object)
  {

    $table_publi=$sql_object->DBescape($table_publi);
     
    if (is_numeric($ID)) {
      $this->ID = $ID;
    } else exit;
    $this->TITLE = strip_input(trim($table_publi[0]), true);
    $this->RESUME = strip_input(trim($table_publi[1]), true);
    $this->THEME = $table_publi[2];
    $this->SCALE = $table_publi[3];
    $this->LEVEL = $table_publi[4];
    $this->POSTED_BY = $table_publi[5];
    $this->COMMENT = strip_input(trim($table_publi[6], true));
    $this->STATUT = $table_publi[7];

    $mask = $this->_HavePublishedDate($table_publi[8]);

    $requete = "UPDATE  " . $this->TDB_PUBLI . " set publi_title='" . $this->TITLE . "', publi_resume='" . $this->RESUME . "' , publi_theme='" . $this->THEME . "', publi_scale='" . $this->SCALE . "', publi_level='" . $this->LEVEL . "', publi_comment='" . $this->COMMENT . "', publi_posted_by='" . $this->POSTED_BY . "', publi_last_modify=NOW(), publi_statut='" . $this->STATUT . "'" . $mask . " WHERE publi_id='" . $this->ID . "';";
    $result = $sql_object->DBQuery($requete);
    return $result;
  }

  /**
   * publication::_HavePublishedDate()
   * Détermine la date de publication a inserer dans la bdd
   *
   * @access private
   * @param string $current_status : statut actuel de la publication
   * @return string $sql_mask
   */
  function _HavePublishedDate($current_status)
  {
    switch ($this->STATUT) {
      case 'P':
        if ($current_status == 'D') $sql_mask = ", publi_published_date= NOW()";
        else $sql_mask = '';
        break;
      case 'D':
        $sql_mask = ", publi_published_date= '0001-01-01'";
        break;
      default:
        $sql_mask = '';
    }
    return $sql_mask;
  }

  /**
   * publication::DeletePublication()
   * suppression d'une publication
   *
   * @access public
   * @param int $ID identifiant de la publication a supprimer
   * @param object $sql_object
   * @return bool $result
   */
  function DeletePublication($ID, $sql_object)
  {
    $this->ID = $ID;
    $requete = "UPDATE  " . $this->TDB_PUBLI . " set publi_statut='E', publi_last_modify=NOW() WHERE publi_id=" . $this->ID . ";";
    $result = $sql_object->DBQuery($requete);

    return $result;
  }
}

?>
