[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhameBlogEditController 4 extends PhameController { 5 6 private $id; 7 8 public function willProcessRequest(array $data) { 9 $this->id = idx($data, 'id'); 10 } 11 12 public function processRequest() { 13 $request = $this->getRequest(); 14 $user = $request->getUser(); 15 16 if ($this->id) { 17 $blog = id(new PhameBlogQuery()) 18 ->setViewer($user) 19 ->withIDs(array($this->id)) 20 ->requireCapabilities( 21 array( 22 PhabricatorPolicyCapability::CAN_EDIT, 23 )) 24 ->executeOne(); 25 if (!$blog) { 26 return new Aphront404Response(); 27 } 28 29 $submit_button = pht('Save Changes'); 30 $page_title = pht('Edit Blog'); 31 $cancel_uri = $this->getApplicationURI('blog/view/'.$blog->getID().'/'); 32 } else { 33 $blog = id(new PhameBlog()) 34 ->setCreatorPHID($user->getPHID()); 35 36 $blog->setViewPolicy(PhabricatorPolicies::POLICY_USER); 37 $blog->setEditPolicy(PhabricatorPolicies::POLICY_USER); 38 $blog->setJoinPolicy(PhabricatorPolicies::POLICY_USER); 39 40 $submit_button = pht('Create Blog'); 41 $page_title = pht('Create Blog'); 42 $cancel_uri = $this->getApplicationURI(); 43 } 44 45 $e_name = true; 46 $e_custom_domain = null; 47 $errors = array(); 48 49 if ($request->isFormPost()) { 50 $name = $request->getStr('name'); 51 $description = $request->getStr('description'); 52 $custom_domain = $request->getStr('custom_domain'); 53 $skin = $request->getStr('skin'); 54 55 if (empty($name)) { 56 $errors[] = pht('You must give the blog a name.'); 57 $e_name = pht('Required'); 58 } else { 59 $e_name = null; 60 } 61 62 $blog->setName($name); 63 $blog->setDescription($description); 64 $blog->setDomain(nonempty($custom_domain, null)); 65 $blog->setSkin($skin); 66 $blog->setViewPolicy($request->getStr('can_view')); 67 $blog->setEditPolicy($request->getStr('can_edit')); 68 $blog->setJoinPolicy($request->getStr('can_join')); 69 70 if (!empty($custom_domain)) { 71 list($error_label, $error_text) = 72 $blog->validateCustomDomain($custom_domain); 73 if ($error_label) { 74 $errors[] = $error_text; 75 $e_custom_domain = $error_label; 76 } 77 if ($blog->getViewPolicy() != PhabricatorPolicies::POLICY_PUBLIC) { 78 $errors[] = pht( 79 'For custom domains to work, the blog must have a view policy of '. 80 'public.'); 81 // Prefer earlier labels for the multiple error scenario. 82 if (!$e_custom_domain) { 83 $e_custom_domain = pht('Invalid Policy'); 84 } 85 } 86 } 87 88 // Don't let users remove their ability to edit blogs. 89 PhabricatorPolicyFilter::mustRetainCapability( 90 $user, 91 $blog, 92 PhabricatorPolicyCapability::CAN_EDIT); 93 94 if (!$errors) { 95 try { 96 $blog->save(); 97 return id(new AphrontRedirectResponse()) 98 ->setURI($this->getApplicationURI('blog/view/'.$blog->getID().'/')); 99 } catch (AphrontDuplicateKeyQueryException $ex) { 100 $errors[] = pht('Domain must be unique.'); 101 $e_custom_domain = pht('Not Unique'); 102 } 103 } 104 } 105 106 $policies = id(new PhabricatorPolicyQuery()) 107 ->setViewer($user) 108 ->setObject($blog) 109 ->execute(); 110 111 $skins = PhameSkinSpecification::loadAllSkinSpecifications(); 112 $skins = mpull($skins, 'getName'); 113 114 $form = id(new AphrontFormView()) 115 ->setUser($user) 116 ->appendChild( 117 id(new AphrontFormTextControl()) 118 ->setLabel(pht('Name')) 119 ->setName('name') 120 ->setValue($blog->getName()) 121 ->setID('blog-name') 122 ->setError($e_name)) 123 ->appendChild( 124 id(new PhabricatorRemarkupControl()) 125 ->setUser($user) 126 ->setLabel(pht('Description')) 127 ->setName('description') 128 ->setValue($blog->getDescription()) 129 ->setID('blog-description') 130 ->setUser($user) 131 ->setDisableMacros(true)) 132 ->appendChild( 133 id(new AphrontFormPolicyControl()) 134 ->setUser($user) 135 ->setCapability(PhabricatorPolicyCapability::CAN_VIEW) 136 ->setPolicyObject($blog) 137 ->setPolicies($policies) 138 ->setName('can_view')) 139 ->appendChild( 140 id(new AphrontFormPolicyControl()) 141 ->setUser($user) 142 ->setCapability(PhabricatorPolicyCapability::CAN_EDIT) 143 ->setPolicyObject($blog) 144 ->setPolicies($policies) 145 ->setName('can_edit')) 146 ->appendChild( 147 id(new AphrontFormPolicyControl()) 148 ->setUser($user) 149 ->setCapability(PhabricatorPolicyCapability::CAN_JOIN) 150 ->setPolicyObject($blog) 151 ->setPolicies($policies) 152 ->setName('can_join')) 153 ->appendChild( 154 id(new AphrontFormTextControl()) 155 ->setLabel(pht('Custom Domain')) 156 ->setName('custom_domain') 157 ->setValue($blog->getDomain()) 158 ->setCaption( 159 pht('Must include at least one dot (.), e.g. blog.example.com')) 160 ->setError($e_custom_domain)) 161 ->appendChild( 162 id(new AphrontFormSelectControl()) 163 ->setLabel(pht('Skin')) 164 ->setName('skin') 165 ->setValue($blog->getSkin()) 166 ->setOptions($skins)) 167 ->appendChild( 168 id(new AphrontFormSubmitControl()) 169 ->addCancelButton($cancel_uri) 170 ->setValue($submit_button)); 171 172 $form_box = id(new PHUIObjectBoxView()) 173 ->setHeaderText($page_title) 174 ->setFormErrors($errors) 175 ->setForm($form); 176 177 $crumbs = $this->buildApplicationCrumbs(); 178 $crumbs->addTextCrumb($page_title, $this->getApplicationURI('blog/new')); 179 180 $nav = $this->renderSideNavFilterView(); 181 $nav->selectFilter($this->id ? null : 'blog/new'); 182 $nav->appendChild( 183 array( 184 $crumbs, 185 $form_box, 186 )); 187 188 return $this->buildApplicationPage( 189 $nav, 190 array( 191 'title' => $page_title, 192 )); 193 } 194 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |