private static function loadSkinSpecification($path)
 {
     $config_path = $path . DIRECTORY_SEPARATOR . 'skin.json';
     $config = array();
     if (Filesystem::pathExists($config_path)) {
         $config = Filesystem::readFile($config_path);
         try {
             $config = phutil_json_decode($config);
         } catch (PhutilJSONParserException $ex) {
             throw new PhutilProxyException(pht("Skin configuration file '%s' is not a valid JSON file.", $config_path), $ex);
         }
         $type = idx($config, 'type', self::TYPE_BASIC);
     } else {
         $type = self::TYPE_BASIC;
     }
     $spec = new PhameSkinSpecification();
     $spec->setRootDirectory($path);
     $spec->setConfig($config);
     switch ($type) {
         case self::TYPE_BASIC:
             $spec->setSkinClass('PhameBasicTemplateBlogSkin');
             break;
         case self::TYPE_ADVANCED:
             $spec->setSkinClass($config['class']);
             $spec->addPhutilLibrary($path . DIRECTORY_SEPARATOR . 'src');
             break;
         default:
             throw new Exception(pht('Unknown skin type!'));
     }
     $spec->setType($type);
     return $spec;
 }
Esempio n. 2
0
 public function getSkinRenderer(AphrontRequest $request)
 {
     $spec = PhameSkinSpecification::loadOneSkinSpecification($this->getSkin());
     if (!$spec) {
         $spec = PhameSkinSpecification::loadOneSkinSpecification(self::SKIN_DEFAULT);
     }
     if (!$spec) {
         throw new Exception('This blog has an invalid skin, and the default skin failed to ' . 'load.');
     }
     $skin = newv($spec->getSkinClass(), array($request));
     $skin->setSpecification($spec);
     return $skin;
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $request->getViewer();
     $id = $request->getURIData('id');
     if ($id) {
         $blog = id(new PhameBlogQuery())->setViewer($viewer)->withIDs(array($id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$blog) {
             return new Aphront404Response();
         }
         $submit_button = pht('Save Changes');
         $page_title = pht('Edit Blog');
         $cancel_uri = $this->getApplicationURI('blog/view/' . $blog->getID() . '/');
         $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs($blog->getPHID(), PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
         $v_projects = array_reverse($v_projects);
         $v_cc = PhabricatorSubscribersQuery::loadSubscribersForPHID($blog->getPHID());
     } else {
         $this->requireApplicationCapability(PhameBlogCreateCapability::CAPABILITY);
         $blog = PhameBlog::initializeNewBlog($viewer);
         $submit_button = pht('Create Blog');
         $page_title = pht('Create Blog');
         $cancel_uri = $this->getApplicationURI();
         $v_projects = array();
         $v_cc = array();
     }
     $name = $blog->getName();
     $description = $blog->getDescription();
     $custom_domain = $blog->getDomain();
     $skin = $blog->getSkin();
     $can_view = $blog->getViewPolicy();
     $can_edit = $blog->getEditPolicy();
     $e_name = true;
     $e_custom_domain = null;
     $e_view_policy = null;
     $validation_exception = null;
     if ($request->isFormPost()) {
         $name = $request->getStr('name');
         $description = $request->getStr('description');
         $custom_domain = nonempty($request->getStr('custom_domain'), null);
         $skin = $request->getStr('skin');
         $can_view = $request->getStr('can_view');
         $can_edit = $request->getStr('can_edit');
         $v_projects = $request->getArr('projects');
         $v_cc = $request->getArr('cc');
         $xactions = array(id(new PhameBlogTransaction())->setTransactionType(PhameBlogTransaction::TYPE_NAME)->setNewValue($name), id(new PhameBlogTransaction())->setTransactionType(PhameBlogTransaction::TYPE_DESCRIPTION)->setNewValue($description), id(new PhameBlogTransaction())->setTransactionType(PhameBlogTransaction::TYPE_DOMAIN)->setNewValue($custom_domain), id(new PhameBlogTransaction())->setTransactionType(PhameBlogTransaction::TYPE_SKIN)->setNewValue($skin), id(new PhameBlogTransaction())->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)->setNewValue($can_view), id(new PhameBlogTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)->setNewValue($can_edit), id(new PhameBlogTransaction())->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS)->setNewValue(array('=' => $v_cc)));
         $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
         $xactions[] = id(new PhameBlogTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDGE)->setMetadataValue('edge:type', $proj_edge_type)->setNewValue(array('=' => array_fuse($v_projects)));
         $editor = id(new PhameBlogEditor())->setActor($viewer)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true);
         try {
             $editor->applyTransactions($blog, $xactions);
             return id(new AphrontRedirectResponse())->setURI($this->getApplicationURI('blog/view/' . $blog->getID() . '/'));
         } catch (PhabricatorApplicationTransactionValidationException $ex) {
             $validation_exception = $ex;
             $e_name = $validation_exception->getShortMessage(PhameBlogTransaction::TYPE_NAME);
             $e_custom_domain = $validation_exception->getShortMessage(PhameBlogTransaction::TYPE_DOMAIN);
             $e_view_policy = $validation_exception->getShortMessage(PhabricatorTransactions::TYPE_VIEW_POLICY);
         }
     }
     $policies = id(new PhabricatorPolicyQuery())->setViewer($viewer)->setObject($blog)->execute();
     $skins = PhameSkinSpecification::loadAllSkinSpecifications();
     $skins = mpull($skins, 'getName');
     $form = id(new AphrontFormView())->setUser($viewer)->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Name'))->setName('name')->setValue($name)->setID('blog-name')->setError($e_name))->appendChild(id(new PhabricatorRemarkupControl())->setUser($viewer)->setLabel(pht('Description'))->setName('description')->setValue($description)->setID('blog-description')->setUser($viewer)->setDisableMacros(true))->appendControl(id(new AphrontFormTokenizerControl())->setLabel(pht('Subscribers'))->setName('cc')->setValue($v_cc)->setUser($viewer)->setDatasource(new PhabricatorMetaMTAMailableDatasource()))->appendChild(id(new AphrontFormPolicyControl())->setUser($viewer)->setCapability(PhabricatorPolicyCapability::CAN_VIEW)->setPolicyObject($blog)->setPolicies($policies)->setError($e_view_policy)->setValue($can_view)->setName('can_view'))->appendChild(id(new AphrontFormPolicyControl())->setUser($viewer)->setCapability(PhabricatorPolicyCapability::CAN_EDIT)->setPolicyObject($blog)->setPolicies($policies)->setValue($can_edit)->setName('can_edit'))->appendControl(id(new AphrontFormTokenizerControl())->setLabel(pht('Projects'))->setName('projects')->setValue($v_projects)->setDatasource(new PhabricatorProjectDatasource()))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Custom Domain'))->setName('custom_domain')->setValue($custom_domain)->setCaption(pht('Must include at least one dot (.), e.g. %s', 'blog.example.com'))->setError($e_custom_domain))->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Skin'))->setName('skin')->setValue($skin)->setOptions($skins))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue($submit_button));
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($page_title)->setValidationException($validation_exception)->setForm($form);
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb(pht('Blogs'), $this->getApplicationURI('blog/'));
     $crumbs->addTextCrumb($page_title, $this->getApplicationURI('blog/new'));
     return $this->newPage()->setTitle($page_title)->setCrumbs($crumbs)->appendChild(array($form_box));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($this->id) {
         $blog = id(new PhameBlogQuery())->setViewer($user)->withIDs(array($this->id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$blog) {
             return new Aphront404Response();
         }
         $submit_button = pht('Save Changes');
         $page_title = pht('Edit Blog');
         $cancel_uri = $this->getApplicationURI('blog/view/' . $blog->getID() . '/');
     } else {
         $blog = id(new PhameBlog())->setCreatorPHID($user->getPHID());
         $blog->setViewPolicy(PhabricatorPolicies::POLICY_USER);
         $blog->setEditPolicy(PhabricatorPolicies::POLICY_USER);
         $blog->setJoinPolicy(PhabricatorPolicies::POLICY_USER);
         $submit_button = pht('Create Blog');
         $page_title = pht('Create Blog');
         $cancel_uri = $this->getApplicationURI();
     }
     $e_name = true;
     $e_custom_domain = null;
     $errors = array();
     if ($request->isFormPost()) {
         $name = $request->getStr('name');
         $description = $request->getStr('description');
         $custom_domain = $request->getStr('custom_domain');
         $skin = $request->getStr('skin');
         if (empty($name)) {
             $errors[] = pht('You must give the blog a name.');
             $e_name = pht('Required');
         } else {
             $e_name = null;
         }
         $blog->setName($name);
         $blog->setDescription($description);
         $blog->setDomain(nonempty($custom_domain, null));
         $blog->setSkin($skin);
         $blog->setViewPolicy($request->getStr('can_view'));
         $blog->setEditPolicy($request->getStr('can_edit'));
         $blog->setJoinPolicy($request->getStr('can_join'));
         if (!empty($custom_domain)) {
             list($error_label, $error_text) = $blog->validateCustomDomain($custom_domain);
             if ($error_label) {
                 $errors[] = $error_text;
                 $e_custom_domain = $error_label;
             }
             if ($blog->getViewPolicy() != PhabricatorPolicies::POLICY_PUBLIC) {
                 $errors[] = pht('For custom domains to work, the blog must have a view policy of ' . 'public.');
                 // Prefer earlier labels for the multiple error scenario.
                 if (!$e_custom_domain) {
                     $e_custom_domain = pht('Invalid Policy');
                 }
             }
         }
         // Don't let users remove their ability to edit blogs.
         PhabricatorPolicyFilter::mustRetainCapability($user, $blog, PhabricatorPolicyCapability::CAN_EDIT);
         if (!$errors) {
             try {
                 $blog->save();
                 return id(new AphrontRedirectResponse())->setURI($this->getApplicationURI('blog/view/' . $blog->getID() . '/'));
             } catch (AphrontDuplicateKeyQueryException $ex) {
                 $errors[] = pht('Domain must be unique.');
                 $e_custom_domain = pht('Not Unique');
             }
         }
     }
     $policies = id(new PhabricatorPolicyQuery())->setViewer($user)->setObject($blog)->execute();
     $skins = PhameSkinSpecification::loadAllSkinSpecifications();
     $skins = mpull($skins, 'getName');
     $form = id(new AphrontFormView())->setUser($user)->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Name'))->setName('name')->setValue($blog->getName())->setID('blog-name')->setError($e_name))->appendChild(id(new PhabricatorRemarkupControl())->setLabel(pht('Description'))->setName('description')->setValue($blog->getDescription())->setID('blog-description')->setUser($user)->setDisableMacros(true))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_VIEW)->setPolicyObject($blog)->setPolicies($policies)->setName('can_view'))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_EDIT)->setPolicyObject($blog)->setPolicies($policies)->setName('can_edit'))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_JOIN)->setPolicyObject($blog)->setPolicies($policies)->setName('can_join'))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Custom Domain'))->setName('custom_domain')->setValue($blog->getDomain())->setCaption(pht('Must include at least one dot (.), e.g. blog.example.com'))->setError($e_custom_domain))->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Skin'))->setName('skin')->setValue($blog->getSkin())->setOptions($skins))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue($submit_button));
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($page_title)->setFormErrors($errors)->setForm($form);
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb($page_title, $this->getApplicationURI('blog/new'));
     $nav = $this->renderSideNavFilterView();
     $nav->selectFilter($this->id ? null : 'blog/new');
     $nav->appendChild(array($crumbs, $form_box));
     return $this->buildApplicationPage($nav, array('title' => $page_title));
 }
 public function handleRequest(AphrontRequest $request)
 {
     $user = $request->getUser();
     $id = $request->getURIData('id');
     if ($id) {
         $blog = id(new PhameBlogQuery())->setViewer($user)->withIDs(array($id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$blog) {
             return new Aphront404Response();
         }
         $submit_button = pht('Save Changes');
         $page_title = pht('Edit Blog');
         $cancel_uri = $this->getApplicationURI('blog/view/' . $blog->getID() . '/');
     } else {
         $blog = PhameBlog::initializeNewBlog($user);
         $submit_button = pht('Create Blog');
         $page_title = pht('Create Blog');
         $cancel_uri = $this->getApplicationURI();
     }
     $name = $blog->getName();
     $description = $blog->getDescription();
     $custom_domain = $blog->getDomain();
     $skin = $blog->getSkin();
     $can_view = $blog->getViewPolicy();
     $can_edit = $blog->getEditPolicy();
     $can_join = $blog->getJoinPolicy();
     $e_name = true;
     $e_custom_domain = null;
     $e_view_policy = null;
     $validation_exception = null;
     if ($request->isFormPost()) {
         $name = $request->getStr('name');
         $description = $request->getStr('description');
         $custom_domain = nonempty($request->getStr('custom_domain'), null);
         $skin = $request->getStr('skin');
         $can_view = $request->getStr('can_view');
         $can_edit = $request->getStr('can_edit');
         $can_join = $request->getStr('can_join');
         $xactions = array(id(new PhameBlogTransaction())->setTransactionType(PhameBlogTransaction::TYPE_NAME)->setNewValue($name), id(new PhameBlogTransaction())->setTransactionType(PhameBlogTransaction::TYPE_DESCRIPTION)->setNewValue($description), id(new PhameBlogTransaction())->setTransactionType(PhameBlogTransaction::TYPE_DOMAIN)->setNewValue($custom_domain), id(new PhameBlogTransaction())->setTransactionType(PhameBlogTransaction::TYPE_SKIN)->setNewValue($skin), id(new PhameBlogTransaction())->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)->setNewValue($can_view), id(new PhameBlogTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)->setNewValue($can_edit), id(new PhameBlogTransaction())->setTransactionType(PhabricatorTransactions::TYPE_JOIN_POLICY)->setNewValue($can_join));
         $editor = id(new PhameBlogEditor())->setActor($user)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true);
         try {
             $editor->applyTransactions($blog, $xactions);
             return id(new AphrontRedirectResponse())->setURI($this->getApplicationURI('blog/view/' . $blog->getID() . '/'));
         } catch (PhabricatorApplicationTransactionValidationException $ex) {
             $validation_exception = $ex;
             $e_name = $validation_exception->getShortMessage(PhameBlogTransaction::TYPE_NAME);
             $e_custom_domain = $validation_exception->getShortMessage(PhameBlogTransaction::TYPE_DOMAIN);
             $e_view_policy = $validation_exception->getShortMessage(PhabricatorTransactions::TYPE_VIEW_POLICY);
         }
     }
     $policies = id(new PhabricatorPolicyQuery())->setViewer($user)->setObject($blog)->execute();
     $skins = PhameSkinSpecification::loadAllSkinSpecifications();
     $skins = mpull($skins, 'getName');
     $form = id(new AphrontFormView())->setUser($user)->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Name'))->setName('name')->setValue($name)->setID('blog-name')->setError($e_name))->appendChild(id(new PhabricatorRemarkupControl())->setUser($user)->setLabel(pht('Description'))->setName('description')->setValue($description)->setID('blog-description')->setUser($user)->setDisableMacros(true))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_VIEW)->setPolicyObject($blog)->setPolicies($policies)->setError($e_view_policy)->setValue($can_view)->setName('can_view'))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_EDIT)->setPolicyObject($blog)->setPolicies($policies)->setValue($can_edit)->setName('can_edit'))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_JOIN)->setPolicyObject($blog)->setPolicies($policies)->setValue($can_join)->setName('can_join'))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Custom Domain'))->setName('custom_domain')->setValue($custom_domain)->setCaption(pht('Must include at least one dot (.), e.g. %s', 'blog.example.com'))->setError($e_custom_domain))->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Skin'))->setName('skin')->setValue($skin)->setOptions($skins))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue($submit_button));
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($page_title)->setValidationException($validation_exception)->setForm($form);
     $crumbs = $this->buildApplicationCrumbs();
     $crumbs->addTextCrumb($page_title, $this->getApplicationURI('blog/new'));
     $nav = $this->renderSideNavFilterView();
     $nav->selectFilter($id ? null : 'blog/new');
     $nav->appendChild(array($crumbs, $form_box));
     return $this->buildApplicationPage($nav, array('title' => $page_title));
 }