2012-04-12 22:09:04 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2012 Facebook, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group phame
|
|
|
|
*/
|
2012-10-15 23:51:04 +02:00
|
|
|
final class PhamePost extends PhameDAO
|
|
|
|
implements PhabricatorPolicyInterface, PhabricatorMarkupInterface {
|
|
|
|
|
|
|
|
const MARKUP_FIELD_BODY = 'markup:body';
|
2012-04-12 22:09:04 +02:00
|
|
|
|
|
|
|
const VISIBILITY_DRAFT = 0;
|
|
|
|
const VISIBILITY_PUBLISHED = 1;
|
|
|
|
|
|
|
|
protected $id;
|
|
|
|
protected $phid;
|
|
|
|
protected $bloggerPHID;
|
|
|
|
protected $title;
|
|
|
|
protected $phameTitle;
|
|
|
|
protected $body;
|
|
|
|
protected $visibility;
|
|
|
|
protected $configData;
|
|
|
|
protected $datePublished;
|
2012-10-15 23:50:04 +02:00
|
|
|
protected $blogPHID;
|
|
|
|
|
|
|
|
private $blog;
|
|
|
|
|
|
|
|
public function setBlog(PhameBlog $blog) {
|
|
|
|
$this->blog = $blog;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBlog() {
|
|
|
|
return $this->blog;
|
|
|
|
}
|
2012-04-12 22:09:04 +02:00
|
|
|
|
|
|
|
public function getViewURI($blogger_name = '') {
|
|
|
|
// go for the pretty uri if we can
|
|
|
|
if ($blogger_name) {
|
|
|
|
$phame_title = PhabricatorSlug::normalize($this->getPhameTitle());
|
|
|
|
$uri = phutil_escape_uri('/phame/posts/'.$blogger_name.'/'.$phame_title);
|
|
|
|
} else {
|
|
|
|
$uri = $this->getActionURI('view');
|
|
|
|
}
|
|
|
|
return $uri;
|
|
|
|
}
|
2012-10-15 23:50:04 +02:00
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
public function getEditURI() {
|
|
|
|
return $this->getActionURI('edit');
|
|
|
|
}
|
2012-10-15 23:50:04 +02:00
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
public function getDeleteURI() {
|
|
|
|
return $this->getActionURI('delete');
|
|
|
|
}
|
2012-10-15 23:50:04 +02:00
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
public function getChangeVisibilityURI() {
|
|
|
|
return $this->getActionURI('changevisibility');
|
|
|
|
}
|
2012-10-15 23:50:04 +02:00
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
private function getActionURI($action) {
|
|
|
|
return '/phame/post/'.$action.'/'.$this->getPHID().'/';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isDraft() {
|
|
|
|
return $this->getVisibility() == self::VISIBILITY_DRAFT;
|
|
|
|
}
|
|
|
|
|
2012-07-19 18:03:10 +02:00
|
|
|
public function getHumanName() {
|
|
|
|
if ($this->isDraft()) {
|
|
|
|
$name = 'draft';
|
|
|
|
} else {
|
|
|
|
$name = 'post';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
public function getCommentsWidget() {
|
|
|
|
$config_data = $this->getConfigData();
|
|
|
|
if (empty($config_data)) {
|
|
|
|
return 'none';
|
|
|
|
}
|
|
|
|
return idx($config_data, 'comments_widget', 'none');
|
|
|
|
}
|
2012-10-15 23:50:04 +02:00
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
public function getConfiguration() {
|
|
|
|
return array(
|
|
|
|
self::CONFIG_AUX_PHID => true,
|
|
|
|
self::CONFIG_SERIALIZATION => array(
|
|
|
|
'configData' => self::SERIALIZATION_JSON,
|
|
|
|
),
|
|
|
|
) + parent::getConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generatePHID() {
|
|
|
|
return PhabricatorPHID::generateNewPHID(
|
|
|
|
PhabricatorPHIDConstants::PHID_TYPE_POST);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getVisibilityOptionsForSelect() {
|
|
|
|
return array(
|
|
|
|
self::VISIBILITY_DRAFT => 'Draft: visible only to me.',
|
|
|
|
self::VISIBILITY_PUBLISHED => 'Published: visible to the whole world.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommentsWidgetOptionsForSelect() {
|
|
|
|
$current = $this->getCommentsWidget();
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
if ($current == 'facebook' ||
|
|
|
|
PhabricatorEnv::getEnvConfig('facebook.application-id')) {
|
|
|
|
$options['facebook'] = 'Facebook';
|
|
|
|
}
|
|
|
|
if ($current == 'disqus' ||
|
|
|
|
PhabricatorEnv::getEnvConfig('disqus.shortname')) {
|
|
|
|
$options['disqus'] = 'Disqus';
|
|
|
|
}
|
|
|
|
$options['none'] = 'None';
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2012-10-15 23:50:04 +02:00
|
|
|
|
|
|
|
/* -( PhabricatorPolicyInterface Implementation )-------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getCapabilities() {
|
|
|
|
return array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getPolicy($capability) {
|
|
|
|
// Draft posts are visible only to the author. Published posts are visible
|
|
|
|
// to whoever the blog is visible to.
|
|
|
|
|
|
|
|
switch ($capability) {
|
|
|
|
case PhabricatorPolicyCapability::CAN_VIEW:
|
|
|
|
if (!$this->isDraft() && $this->getBlog()) {
|
|
|
|
return $this->getBlog()->getViewPolicy();
|
|
|
|
} else {
|
|
|
|
return PhabricatorPolicies::POLICY_NOONE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PhabricatorPolicyCapability::CAN_EDIT:
|
|
|
|
return PhabricatorPolicies::POLICY_NOONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function hasAutomaticCapability($capability, PhabricatorUser $user) {
|
|
|
|
// A blog post's author can always view it, and is the only user allowed
|
|
|
|
// to edit it.
|
|
|
|
|
|
|
|
switch ($capability) {
|
|
|
|
case PhabricatorPolicyCapability::CAN_VIEW:
|
|
|
|
case PhabricatorPolicyCapability::CAN_EDIT:
|
|
|
|
return ($user->getPHID() == $this->getBloggerPHID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-15 23:51:04 +02:00
|
|
|
|
|
|
|
/* -( PhabricatorMarkupInterface Implementation )-------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
public function getMarkupFieldKey($field) {
|
2012-10-15 23:51:30 +02:00
|
|
|
$hash = PhabricatorHash::digest($this->getMarkupText($field));
|
|
|
|
return $this->getPHID().':'.$field.':'.$hash;
|
2012-10-15 23:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function newMarkupEngine($field) {
|
|
|
|
return PhabricatorMarkupEngine::newPhameMarkupEngine();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getMarkupText($field) {
|
|
|
|
return $this->getBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function didMarkupText(
|
|
|
|
$field,
|
|
|
|
$output,
|
|
|
|
PhutilMarkupEngine $engine) {
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function shouldUseMarkupCache($field) {
|
|
|
|
return (bool)$this->getPHID();
|
|
|
|
}
|
|
|
|
|
2012-04-12 22:09:04 +02:00
|
|
|
}
|