<?php
class messageComponents extends sfComponents {

  public function executeListmail() {
    $c = new Criteria();
    if ($this->box_type == 'inbox') {
      $c->add(TblusermessagePeer::MS_TO, $this->us_id);
      $c->add(TblusermessagePeer::MS_DELETED, array(0, 1), Criteria::IN);
      if ($this->partner) $c->add(TblusermessagePeer::MS_FROM, $this->partner);
    } else {
      $c->add(TblusermessagePeer::MS_FROM, $this->us_id);
      $c->add(TblusermessagePeer::MS_DELETED, array(0, 2), Criteria::IN);
      if ($this->partner) $c->add(TblusermessagePeer::MS_TO, $this->partner);
    }
    $c->addDescendingOrderByColumn(TblusermessagePeer::MS_TIME);
    $page = $this->getRequestParameter('page', 1);
    $this->pager = new PropelPager($c, 'TblusermessagePeer', 'doSelect', $page, 20);
  }

  public function executeCompose() {
    $this->us_to = $this->getRequestParameter('us_to');
    $this->title = urldecode($this->getRequestParameter('title'));
    $this->content = urldecode($this->getRequestParameter('content'));
    if ($this->mail) {
      $this->title = $this->mail->getMsSubject();
      if (substr($this->title, 0, 3) != 'Re:') $this->title = 'Re:'.$this->title;
      $this->us_to = $this->mail->getMsFrom();
    }
    if ($this->us_to && $user = TbluserPeer::retrieveByPK($this->us_to)) {
      $username = $user->getUsUsername();
      $name = $user->getUsFullname();
      $this->address = "$name <$username>";
    }
  }

  public function executeShowmail() {
    if ($this->mail->getMsTo() == $this->us_id) {
      $this->mail->setMsStatus(1);
      $this->mail->save();
      $unread = TblusermessagePeer::getUserUnread($this->us_id);
      $this->getUser()->setAttribute('message', $unread, 'user');
    }
  }

  public function executeShowcontent() {
  }
}
