[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phame/conduit/ -> PhameCreatePostConduitAPIMethod.php (source)

   1  <?php
   2  
   3  final class PhameCreatePostConduitAPIMethod extends PhameConduitAPIMethod {
   4  
   5    public function getAPIMethodName() {
   6      return 'phame.createpost';
   7    }
   8  
   9    public function getMethodDescription() {
  10      return pht('Create a phame post.');
  11    }
  12  
  13    public function getMethodStatus() {
  14      return self::METHOD_STATUS_UNSTABLE;
  15    }
  16  
  17    public function defineParamTypes() {
  18      return array(
  19        'blogPHID'      => 'required phid',
  20        'title'         => 'required string',
  21        'body'          => 'required string',
  22        'phameTitle'    => 'optional string',
  23        'bloggerPHID'   => 'optional phid',
  24        'isDraft'       => 'optional bool',
  25      );
  26    }
  27  
  28    public function defineReturnType() {
  29      return 'list<dict>';
  30    }
  31  
  32    public function defineErrorTypes() {
  33      return array(
  34        'ERR-INVALID-PARAMETER' =>
  35          pht('Missing or malformed parameter.'),
  36        'ERR-INVALID-BLOG'      =>
  37          pht('Invalid blog PHID or user can not post to blog.'),
  38      );
  39    }
  40  
  41    protected function execute(ConduitAPIRequest $request) {
  42      $user = $request->getUser();
  43      $blog_phid = $request->getValue('blogPHID');
  44      $title = $request->getValue('title');
  45      $body = $request->getValue('body');
  46      $exception_description = array();
  47      if (!$blog_phid) {
  48        $exception_description[] = pht('No blog phid.');
  49      }
  50      if (!strlen($title)) {
  51        $exception_description[] = pht('No post title.');
  52      }
  53      if (!strlen($body)) {
  54        $exception_description[] = pht('No post body.');
  55      }
  56      if ($exception_description) {
  57        throw id(new ConduitException('ERR-INVALID-PARAMETER'))
  58          ->setErrorDescription(implode("\n", $exception_description));
  59      }
  60  
  61      $blogger_phid = $request->getValue('bloggerPHID');
  62      if ($blogger_phid) {
  63        $blogger = id(new PhabricatorPeopleQuery())
  64          ->setViewer($user)
  65          ->withPHIDs(array($blogger_phid))
  66          ->executeOne();
  67      } else {
  68        $blogger = $user;
  69      }
  70  
  71      $blog = id(new PhameBlogQuery())
  72        ->setViewer($blogger)
  73        ->withPHIDs(array($blog_phid))
  74        ->requireCapabilities(
  75          array(
  76            PhabricatorPolicyCapability::CAN_JOIN,
  77          ))
  78        ->executeOne();
  79  
  80      if (!$blog) {
  81        throw new ConduitException('ERR-INVALID-BLOG');
  82      }
  83  
  84      $post = PhamePost::initializePost($blogger, $blog);
  85      $is_draft = $request->getValue('isDraft', false);
  86      if (!$is_draft) {
  87        $post->setDatePublished(time());
  88        $post->setVisibility(PhamePost::VISIBILITY_PUBLISHED);
  89      }
  90      $post->setTitle($title);
  91      $phame_title = $request->getValue(
  92        'phameTitle',
  93        id(new PhutilUTF8StringTruncator())
  94        ->setMaximumCodepoints(64)
  95        ->truncateString($title));
  96      $post->setPhameTitle(PhabricatorSlug::normalize($phame_title));
  97      $post->setBody($body);
  98      $post->save();
  99  
 100      return $post->toDictionary();
 101    }
 102  
 103  }


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