[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phame/storage/ -> PhamePost.php (source)

   1  <?php
   2  
   3  final class PhamePost extends PhameDAO
   4    implements
   5      PhabricatorPolicyInterface,
   6      PhabricatorMarkupInterface,
   7      PhabricatorTokenReceiverInterface {
   8  
   9    const MARKUP_FIELD_BODY    = 'markup:body';
  10    const MARKUP_FIELD_SUMMARY = 'markup:summary';
  11  
  12    const VISIBILITY_DRAFT     = 0;
  13    const VISIBILITY_PUBLISHED = 1;
  14  
  15    protected $bloggerPHID;
  16    protected $title;
  17    protected $phameTitle;
  18    protected $body;
  19    protected $visibility;
  20    protected $configData;
  21    protected $datePublished;
  22    protected $blogPHID;
  23  
  24    private $blog;
  25  
  26    public static function initializePost(
  27      PhabricatorUser $blogger,
  28      PhameBlog $blog) {
  29  
  30      $post = id(new PhamePost())
  31        ->setBloggerPHID($blogger->getPHID())
  32        ->setBlogPHID($blog->getPHID())
  33        ->setBlog($blog)
  34        ->setDatePublished(0)
  35        ->setVisibility(self::VISIBILITY_DRAFT);
  36      return $post;
  37    }
  38  
  39    public function setBlog(PhameBlog $blog) {
  40      $this->blog = $blog;
  41      return $this;
  42    }
  43  
  44    public function getBlog() {
  45      return $this->blog;
  46    }
  47  
  48    public function getViewURI() {
  49      // go for the pretty uri if we can
  50      $domain = ($this->blog ? $this->blog->getDomain() : '');
  51      if ($domain) {
  52        $phame_title = PhabricatorSlug::normalize($this->getPhameTitle());
  53        return 'http://'.$domain.'/post/'.$phame_title;
  54      }
  55      $uri = '/phame/post/view/'.$this->getID().'/';
  56      return PhabricatorEnv::getProductionURI($uri);
  57    }
  58  
  59    public function getEditURI() {
  60      return '/phame/post/edit/'.$this->getID().'/';
  61    }
  62  
  63    public function isDraft() {
  64      return $this->getVisibility() == self::VISIBILITY_DRAFT;
  65    }
  66  
  67    public function getHumanName() {
  68      if ($this->isDraft()) {
  69        $name = 'draft';
  70      } else {
  71        $name = 'post';
  72      }
  73  
  74      return $name;
  75    }
  76  
  77    public function getCommentsWidget() {
  78      $config_data = $this->getConfigData();
  79      if (empty($config_data)) {
  80        return 'none';
  81      }
  82      return idx($config_data, 'comments_widget', 'none');
  83    }
  84  
  85    public function getConfiguration() {
  86      return array(
  87        self::CONFIG_AUX_PHID   => true,
  88        self::CONFIG_SERIALIZATION => array(
  89          'configData' => self::SERIALIZATION_JSON,
  90        ),
  91        self::CONFIG_COLUMN_SCHEMA => array(
  92          'title' => 'text255',
  93          'phameTitle' => 'sort64',
  94          'visibility' => 'uint32',
  95  
  96          // T6203/NULLABILITY
  97          // These seem like they should always be non-null?
  98          'blogPHID' => 'phid?',
  99          'body' => 'text?',
 100          'configData' => 'text?',
 101  
 102          // T6203/NULLABILITY
 103          // This one probably should be nullable?
 104          'datePublished' => 'epoch',
 105        ),
 106        self::CONFIG_KEY_SCHEMA => array(
 107          'key_phid' => null,
 108          'phid' => array(
 109            'columns' => array('phid'),
 110            'unique' => true,
 111          ),
 112          'phameTitle' => array(
 113            'columns' => array('bloggerPHID', 'phameTitle'),
 114            'unique' => true,
 115          ),
 116          'bloggerPosts' => array(
 117            'columns' => array(
 118              'bloggerPHID',
 119              'visibility',
 120              'datePublished',
 121              'id',
 122            ),
 123          ),
 124        ),
 125      ) + parent::getConfiguration();
 126    }
 127  
 128    public function generatePHID() {
 129      return PhabricatorPHID::generateNewPHID(
 130        PhabricatorPhamePostPHIDType::TYPECONST);
 131    }
 132  
 133    public function toDictionary() {
 134      return array(
 135        'id'            => $this->getID(),
 136        'phid'          => $this->getPHID(),
 137        'blogPHID'      => $this->getBlogPHID(),
 138        'bloggerPHID'   => $this->getBloggerPHID(),
 139        'viewURI'       => $this->getViewURI(),
 140        'title'         => $this->getTitle(),
 141        'phameTitle'    => $this->getPhameTitle(),
 142        'body'          => $this->getBody(),
 143        'summary'       => PhabricatorMarkupEngine::summarize($this->getBody()),
 144        'datePublished' => $this->getDatePublished(),
 145        'published'     => !$this->isDraft(),
 146      );
 147    }
 148  
 149    public static function getVisibilityOptionsForSelect() {
 150      return array(
 151        self::VISIBILITY_DRAFT     => 'Draft: visible only to me.',
 152        self::VISIBILITY_PUBLISHED => 'Published: visible to the whole world.',
 153      );
 154    }
 155  
 156    public function getCommentsWidgetOptionsForSelect() {
 157      $current = $this->getCommentsWidget();
 158      $options = array();
 159  
 160      if ($current == 'facebook' ||
 161          PhabricatorFacebookAuthProvider::getFacebookApplicationID()) {
 162        $options['facebook'] = 'Facebook';
 163      }
 164      if ($current == 'disqus' ||
 165          PhabricatorEnv::getEnvConfig('disqus.shortname')) {
 166        $options['disqus'] = 'Disqus';
 167      }
 168      $options['none'] = 'None';
 169  
 170      return $options;
 171    }
 172  
 173  
 174  /* -(  PhabricatorPolicyInterface Implementation  )-------------------------- */
 175  
 176  
 177    public function getCapabilities() {
 178      return array(
 179        PhabricatorPolicyCapability::CAN_VIEW,
 180        PhabricatorPolicyCapability::CAN_EDIT,
 181      );
 182    }
 183  
 184  
 185    public function getPolicy($capability) {
 186      // Draft posts are visible only to the author. Published posts are visible
 187      // to whoever the blog is visible to.
 188  
 189      switch ($capability) {
 190        case PhabricatorPolicyCapability::CAN_VIEW:
 191          if (!$this->isDraft() && $this->getBlog()) {
 192            return $this->getBlog()->getViewPolicy();
 193          } else {
 194            return PhabricatorPolicies::POLICY_NOONE;
 195          }
 196          break;
 197        case PhabricatorPolicyCapability::CAN_EDIT:
 198          return PhabricatorPolicies::POLICY_NOONE;
 199      }
 200    }
 201  
 202  
 203    public function hasAutomaticCapability($capability, PhabricatorUser $user) {
 204      // A blog post's author can always view it, and is the only user allowed
 205      // to edit it.
 206  
 207      switch ($capability) {
 208        case PhabricatorPolicyCapability::CAN_VIEW:
 209        case PhabricatorPolicyCapability::CAN_EDIT:
 210          return ($user->getPHID() == $this->getBloggerPHID());
 211      }
 212    }
 213  
 214  
 215    public function describeAutomaticCapability($capability) {
 216      return pht(
 217        'The author of a blog post can always view and edit it.');
 218    }
 219  
 220  
 221  /* -(  PhabricatorMarkupInterface Implementation  )-------------------------- */
 222  
 223  
 224    public function getMarkupFieldKey($field) {
 225      $hash = PhabricatorHash::digest($this->getMarkupText($field));
 226      return $this->getPHID().':'.$field.':'.$hash;
 227    }
 228  
 229  
 230    public function newMarkupEngine($field) {
 231      return PhabricatorMarkupEngine::newPhameMarkupEngine();
 232    }
 233  
 234  
 235    public function getMarkupText($field) {
 236      switch ($field) {
 237        case self::MARKUP_FIELD_BODY:
 238          return $this->getBody();
 239        case self::MARKUP_FIELD_SUMMARY:
 240          return PhabricatorMarkupEngine::summarize($this->getBody());
 241      }
 242    }
 243  
 244    public function didMarkupText(
 245      $field,
 246      $output,
 247      PhutilMarkupEngine $engine) {
 248      return $output;
 249    }
 250  
 251  
 252    public function shouldUseMarkupCache($field) {
 253      return (bool)$this->getPHID();
 254    }
 255  
 256  /* -(  PhabricatorTokenReceiverInterface  )---------------------------------- */
 257  
 258    public function getUsersToNotifyOfTokenGiven() {
 259      return array(
 260        $this->getBloggerPHID(),
 261      );
 262    }
 263  
 264  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1