2012-05-04 00:47:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorUserStatus extends PhabricatorUserDAO {
|
|
|
|
|
|
|
|
protected $userPHID;
|
|
|
|
protected $dateFrom;
|
|
|
|
protected $dateTo;
|
|
|
|
protected $status;
|
2012-10-24 22:22:24 +02:00
|
|
|
protected $description;
|
|
|
|
|
|
|
|
const STATUS_AWAY = 1;
|
|
|
|
const STATUS_SPORADIC = 2;
|
|
|
|
|
2012-12-22 01:02:31 +01:00
|
|
|
private static $statusTexts = array(
|
|
|
|
self::STATUS_AWAY => 'away',
|
|
|
|
self::STATUS_SPORADIC => 'sporadic',
|
|
|
|
);
|
|
|
|
|
|
|
|
public function getTextStatus() {
|
|
|
|
return self::$statusTexts[$this->status];
|
|
|
|
}
|
|
|
|
|
2012-10-24 22:22:24 +02:00
|
|
|
public function getStatusOptions() {
|
|
|
|
return array(
|
|
|
|
self::STATUS_AWAY => pht('Away'),
|
|
|
|
self::STATUS_SPORADIC => pht('Sporadic'),
|
|
|
|
);
|
|
|
|
}
|
2012-05-04 00:47:34 +02:00
|
|
|
|
2012-12-22 01:02:31 +01:00
|
|
|
public function getHumanStatus() {
|
2012-10-24 22:22:24 +02:00
|
|
|
$options = $this->getStatusOptions();
|
|
|
|
return $options[$this->status];
|
2012-05-18 02:21:07 +02:00
|
|
|
}
|
|
|
|
|
2012-10-24 22:22:24 +02:00
|
|
|
public function getTerseSummary(PhabricatorUser $viewer) {
|
2012-05-18 00:13:33 +02:00
|
|
|
$until = phabricator_date($this->dateTo, $viewer);
|
|
|
|
if ($this->status == PhabricatorUserStatus::STATUS_SPORADIC) {
|
|
|
|
return 'Sporadic until '.$until;
|
|
|
|
} else {
|
|
|
|
return 'Away until '.$until;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-18 02:21:07 +02:00
|
|
|
public function setTextStatus($status) {
|
2012-12-22 01:02:31 +01:00
|
|
|
$statuses = array_flip(self::$statusTexts);
|
2012-05-18 02:21:07 +02:00
|
|
|
return $this->setStatus($statuses[$status]);
|
|
|
|
}
|
|
|
|
|
2012-05-17 03:42:06 +02:00
|
|
|
public function loadCurrentStatuses($user_phids) {
|
2013-05-17 12:55:45 +02:00
|
|
|
if (!$user_phids) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2012-05-17 03:42:06 +02:00
|
|
|
$statuses = $this->loadAllWhere(
|
|
|
|
'userPHID IN (%Ls) AND UNIX_TIMESTAMP() BETWEEN dateFrom AND dateTo',
|
|
|
|
$user_phids);
|
2013-05-17 12:55:45 +02:00
|
|
|
|
2012-05-17 03:42:06 +02:00
|
|
|
return mpull($statuses, null, 'getUserPHID');
|
|
|
|
}
|
|
|
|
|
2012-10-24 22:22:24 +02:00
|
|
|
/**
|
|
|
|
* Validates data and throws exceptions for non-sensical status
|
|
|
|
* windows and attempts to create an overlapping status.
|
|
|
|
*/
|
|
|
|
public function save() {
|
|
|
|
|
|
|
|
if ($this->getDateTo() <= $this->getDateFrom()) {
|
|
|
|
throw new PhabricatorUserStatusInvalidEpochException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->openTransaction();
|
|
|
|
$this->beginWriteLocking();
|
|
|
|
|
|
|
|
if ($this->shouldInsertWhenSaved()) {
|
|
|
|
|
|
|
|
$overlap = $this->loadAllWhere(
|
|
|
|
'userPHID = %s AND dateFrom < %d AND dateTo > %d',
|
|
|
|
$this->getUserPHID(),
|
|
|
|
$this->getDateTo(),
|
|
|
|
$this->getDateFrom());
|
|
|
|
|
|
|
|
if ($overlap) {
|
|
|
|
$this->endWriteLocking();
|
|
|
|
$this->killTransaction();
|
|
|
|
throw new PhabricatorUserStatusOverlapException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::save();
|
|
|
|
|
|
|
|
$this->endWriteLocking();
|
|
|
|
return $this->saveTransaction();
|
|
|
|
}
|
|
|
|
|
2012-05-04 00:47:34 +02:00
|
|
|
}
|