2011-02-06 01:57:21 +01:00
|
|
|
<?php
|
|
|
|
|
2012-03-13 19:18:11 +01:00
|
|
|
final class PhabricatorDraft extends PhabricatorDraftDAO {
|
2011-02-06 01:57:21 +01:00
|
|
|
|
|
|
|
protected $authorPHID;
|
|
|
|
protected $draftKey;
|
|
|
|
protected $draft;
|
2012-09-20 23:11:11 +02:00
|
|
|
protected $metadata = array();
|
|
|
|
|
2014-02-19 01:25:16 +01:00
|
|
|
private $deleted = false;
|
|
|
|
|
2012-09-20 23:11:11 +02:00
|
|
|
public function getConfiguration() {
|
|
|
|
return array(
|
|
|
|
self::CONFIG_SERIALIZATION => array(
|
|
|
|
'metadata' => self::SERIALIZATION_JSON,
|
|
|
|
),
|
|
|
|
) + parent::getConfiguration();
|
|
|
|
}
|
2011-02-06 01:57:21 +01:00
|
|
|
|
2012-10-02 00:50:47 +02:00
|
|
|
public function replaceOrDelete() {
|
|
|
|
if ($this->draft == '' && !array_filter($this->metadata)) {
|
|
|
|
queryfx(
|
|
|
|
$this->establishConnection('w'),
|
|
|
|
'DELETE FROM %T WHERE authorPHID = %s AND draftKey = %s',
|
|
|
|
$this->getTableName(),
|
|
|
|
$this->authorPHID,
|
|
|
|
$this->draftKey);
|
2014-02-19 01:25:16 +01:00
|
|
|
$this->deleted = true;
|
2012-10-02 00:50:47 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
return parent::replace();
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:25:16 +01:00
|
|
|
protected function didDelete() {
|
|
|
|
$this->deleted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isDeleted() {
|
|
|
|
return $this->deleted;
|
|
|
|
}
|
|
|
|
|
2012-12-21 14:57:14 +01:00
|
|
|
public static function newFromUserAndKey(PhabricatorUser $user, $key) {
|
|
|
|
if ($user->getPHID() && strlen($key)) {
|
|
|
|
$draft = id(new PhabricatorDraft())->loadOneWhere(
|
|
|
|
'authorPHID = %s AND draftKey = %s',
|
|
|
|
$user->getPHID(),
|
|
|
|
$key);
|
|
|
|
if ($draft) {
|
|
|
|
return $draft;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$draft = new PhabricatorDraft();
|
|
|
|
if ($user->getPHID()) {
|
|
|
|
$draft
|
|
|
|
->setAuthorPHID($user->getPHID())
|
|
|
|
->setDraftKey($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $draft;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function buildFromRequest(AphrontRequest $request) {
|
|
|
|
$user = $request->getUser();
|
|
|
|
if (!$user->getPHID()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$request->getStr('__draft__')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$draft = id(new PhabricatorDraft())
|
|
|
|
->setAuthorPHID($user->getPHID())
|
|
|
|
->setDraftKey($request->getStr('__draft__'));
|
|
|
|
|
|
|
|
// If this is a preview, add other data. If not, leave the draft empty so
|
|
|
|
// that replaceOrDelete() will delete it.
|
|
|
|
if ($request->isPreviewRequest()) {
|
|
|
|
$other_data = $request->getPassthroughRequestData();
|
|
|
|
unset($other_data['comment']);
|
|
|
|
|
|
|
|
$draft
|
|
|
|
->setDraft($request->getStr('comment'))
|
|
|
|
->setMetadata($other_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $draft;
|
|
|
|
}
|
|
|
|
|
2011-02-06 01:57:21 +01:00
|
|
|
}
|