Location: PHPKode > projects > DarkCity > includes/MessageManager.php
<?php 
/**
 * MessageManager.php : Manager pour la messagerie
 *
 * Date    : 31/05/2009
 * Auteur  : Philippe Bousquet
 * Version : 1.0
 * Contact : <hide@address.com>
 *
 * Copyright (c) 2008-2009 Philippe & Thierry Bousquet.
 *
 * Ce logiciel est distribué selon les termes de la GNU General Public License v2.
 */

class MessageManager {
  
  var $datasource;
  var $sessionManager;
  
  function MessageManager($datasource, $sessionManager) {
    $this->datasource = $datasource;
    $this->sessionManager = $sessionManager;
  }
  
  function hasNewMessages() {
    $messageDao = new Darkcity_messageDao;
    $nbMessage = $this->sessionManager->load("nb_messages");
    $user_name = $this->sessionManager->load("user_name");
    $sql = "SELECT * FROM darkcity_message WHERE message_user = '$user_name' AND message_validite <= ".time().";";
    $result = $messageDao->listQuery(&$this->datasource, $sql); 
    $new_nbMessage = sizeof($result);
    if ($nbMessage == "") $this->sessionManager->save("nb_messages",$new_nbMessages);
    return ($nbMessage != "" && $new_nbMessage > $nbMessage);
  }

  function hasNotReadMessages() {
    $messageDao = new Darkcity_messageDao;
    $user_name = $this->sessionManager->load("user_name");
    $sql = "SELECT * FROM darkcity_message WHERE message_user = '$user_name' AND message_validite <= ".time()." AND message_lu = 0;";
    $result = $messageDao->listQuery(&$this->datasource, $sql); 
    $new_nbMessage = sizeof($result);
    return $new_nbMessage;
  }

  function getLastSystemMessage() {
    $res = array();
    $messageDao = new Darkcity_messageDao;
    $user_name = $this->sessionManager->load("user_name");
    $sql = "SELECT * FROM darkcity_message WHERE message_user = '$user_name' AND message_validite <= ".time()." ORDER BY message_validite DESC;";
    $result = $messageDao->listQuery(&$this->datasource, $sql); 
    if (sizeof($result) > 0 && $result[0]->getMessage_from() == "" && !$result[0]->getMessage_lu()) {
      $result[0]->setMessage_lu(1);
      $messageDao->save(&$this->datasource, $result[0]);
      array_push($res, $result[0]->getMessage_id());
      array_push($res, $result[0]->getMessage_titre());
      return $res;
    }
    return $res;
  }

  function getLastUserNotReadMessage() {
    $res = array();
    $messageDao = new Darkcity_messageDao;
    $user_name = $this->sessionManager->load("user_name");
    $sql = "SELECT * FROM darkcity_message WHERE message_user = '$user_name' AND message_validite <= ".time()." ORDER BY message_validite DESC;";
    $result = $messageDao->listQuery(&$this->datasource, $sql); 
    if (sizeof($result) > 0 && $result[0]->getMessage_from() != "" && !$result[0]->getMessage_lu()) {
      $result[0]->setMessage_lu(1);
      $messageDao->save(&$this->datasource, $result[0]);
      array_push($res, $result[0]->getMessage_id());
      array_push($res, $result[0]->getMessage_titre());
      array_push($res, $result[0]->getMessage_from());
      return $res;
    }
    return $res;
  }
  
  function sendMessage($from, $to, $title, $content, $validite) {
    $message = new Darkcity_message;
    $message->setAll('',
          $to,
          $from,
          $title,
          $content,
          $validite,
          0);
    $messageDao = new Darkcity_messageDao;
//    return $message;
    $messageDao->create(&$this->datasource, $message);
  }
  
  function getListMessages() {
    $messageDao = new Darkcity_messageDao;
    $user_name = $this->sessionManager->load("user_name");
    $sql = "SELECT * FROM darkcity_message WHERE message_user = '$user_name' AND message_validite <= ".time()." ORDER BY message_validite DESC;";
    $result = $messageDao->listQuery(&$this->datasource, $sql); 
    return $result;
  }

  function getMessage($id) {
    $messageDao = new Darkcity_messageDao;
    $result = $messageDao->getObject(&$this->datasource, $id); 
    return $result;
  }

  function purgeMessages() {
    $messageDao = new Darkcity_messageDao;
    $sql = "DELETE FROM darkcity_message WHERE (message_lu = 1 AND message_validite < ".(time()-259200).") OR (message_lu = 0 AND message_validite < ".(time()-2592000).");";
    $result = $messageDao->databaseUpdate(&$this->datasource, $sql); 
  }
}
?>
Return current item: DarkCity