~ubuntu-branches/ubuntu/wily/phabricator/wily

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

final class NuanceItem
  extends NuanceDAO
  implements PhabricatorPolicyInterface {

  const STATUS_OPEN     = 0;
  const STATUS_ASSIGNED = 10;
  const STATUS_CLOSED   = 20;

  protected $status;
  protected $ownerPHID;
  protected $requestorPHID;
  protected $sourcePHID;
  protected $sourceLabel;
  protected $data = array();
  protected $mailKey;
  protected $dateNuanced;

  public static function initializeNewItem() {
    return id(new NuanceItem())
      ->setDateNuanced(time())
      ->setStatus(self::STATUS_OPEN);
  }

  protected function getConfiguration() {
    return array(
      self::CONFIG_AUX_PHID => true,
      self::CONFIG_SERIALIZATION => array(
        'data' => self::SERIALIZATION_JSON,
      ),
      self::CONFIG_COLUMN_SCHEMA => array(
        'ownerPHID' => 'phid?',
        'sourceLabel' => 'text255?',
        'status' => 'uint32',
        'mailKey' => 'bytes20',
        'dateNuanced' => 'epoch',
      ),
      self::CONFIG_KEY_SCHEMA => array(
        'key_source' => array(
          'columns' => array('sourcePHID', 'status', 'dateNuanced', 'id'),
        ),
        'key_owner' => array(
          'columns' => array('ownerPHID', 'status', 'dateNuanced', 'id'),
        ),
        'key_contacter' => array(
          'columns' => array('requestorPHID', 'status', 'dateNuanced', 'id'),
        ),
      ),
    ) + parent::getConfiguration();
  }

  public function generatePHID() {
    return PhabricatorPHID::generateNewPHID(
      NuanceItemPHIDType::TYPECONST);
  }

  public function save() {
    if (!$this->getMailKey()) {
      $this->setMailKey(Filesystem::readRandomCharacters(20));
    }
    return parent::save();
  }

  public function getURI() {
    return '/nuance/item/view/'.$this->getID().'/';
  }

  public function getLabel(PhabricatorUser $viewer) {
    // this is generated at the time the item is created based on
    // the configuration from the item source. It is typically
    // something like 'Twitter'.
    $source_label = $this->getSourceLabel();

    return pht(
      'Item via %s @ %s.',
      $source_label,
      phabricator_datetime($this->getDateCreated(), $viewer));
  }

  public function getRequestor() {
    return $this->assertAttached($this->requestor);
  }

  public function attachRequestor(NuanceRequestor $requestor) {
    return $this->requestor = $requestor;
  }

  public function getSource() {
    return $this->assertAttached($this->source);
  }

  public function attachSource(NuanceSource $source) {
    $this->source = $source;
  }

  public function getNuanceProperty($key, $default = null) {
    return idx($this->data, $key, $default);
  }

  public function setNuanceProperty($key, $value) {
    $this->data[$key] = $value;
    return $this;
  }

  public function getCapabilities() {
    return array(
      PhabricatorPolicyCapability::CAN_VIEW,
      PhabricatorPolicyCapability::CAN_EDIT,
    );
  }

  public function getPolicy($capability) {
    // TODO - this should be based on the queues the item currently resides in
    return PhabricatorPolicies::POLICY_USER;
  }

  public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
    // TODO - requestors should get auto access too!
    return $viewer->getPHID() == $this->ownerPHID;
  }

  public function describeAutomaticCapability($capability) {
    switch ($capability) {
      case PhabricatorPolicyCapability::CAN_VIEW:
        return pht('Owners of an item can always view it.');
      case PhabricatorPolicyCapability::CAN_EDIT:
        return pht('Owners of an item can always edit it.');
    }
    return null;
  }

  public function toDictionary() {
    return array(
      'id' => $this->getID(),
      'phid' => $this->getPHID(),
      'ownerPHID' => $this->getOwnerPHID(),
      'requestorPHID' => $this->getRequestorPHID(),
      'sourcePHID' => $this->getSourcePHID(),
      'sourceLabel' => $this->getSourceLabel(),
      'dateCreated' => $this->getDateCreated(),
      'dateModified' => $this->getDateModified(),
      'dateNuanced' => $this->getDateNuanced(),
    );
  }
}