| 
									
										
										
										
											2012-04-12 13:09:04 -07: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 14:50:04 -07:00
										 |  |  | final class PhamePostViewController extends PhameController { | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   private $postPHID; | 
					
						
							|  |  |  |   private $phameTitle; | 
					
						
							|  |  |  |   private $bloggerName; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private function setPostPHID($post_phid) { | 
					
						
							|  |  |  |     $this->postPHID = $post_phid; | 
					
						
							|  |  |  |     return $this; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   private function getPostPHID() { | 
					
						
							|  |  |  |     return $this->postPHID; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   private function setPhameTitle($phame_title) { | 
					
						
							|  |  |  |     $this->phameTitle = $phame_title; | 
					
						
							|  |  |  |     return $this; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   private function getPhameTitle() { | 
					
						
							|  |  |  |     return $this->phameTitle; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   private function setBloggerName($blogger_name) { | 
					
						
							|  |  |  |     $this->bloggerName = $blogger_name; | 
					
						
							|  |  |  |     return $this; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   private function getBloggerName() { | 
					
						
							|  |  |  |     return $this->bloggerName; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   protected function getSideNavFilter() { | 
					
						
							|  |  |  |     $filter = 'post/view/'.$this->getPostPHID(); | 
					
						
							|  |  |  |     return $filter; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   protected function getSideNavExtraPostFilters() { | 
					
						
							|  |  |  |       $filters =  array( | 
					
						
							|  |  |  |         array('key'  => $this->getSideNavFilter(), | 
					
						
							|  |  |  |               'name' => $this->getPhameTitle()) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       return $filters; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-05 15:43:14 -07:00
										 |  |  |   public function shouldRequireLogin() { | 
					
						
							|  |  |  |     // TODO -- get policy logic going
 | 
					
						
							|  |  |  |     // return PhabricatorEnv::getEnvConfig('policy.allow-public');
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |   public function willProcessRequest(array $data) { | 
					
						
							|  |  |  |     $this->setPostPHID(idx($data, 'phid')); | 
					
						
							|  |  |  |     $this->setPhameTitle(idx($data, 'phametitle')); | 
					
						
							|  |  |  |     $this->setBloggerName(idx($data, 'bloggername')); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public function processRequest() { | 
					
						
							|  |  |  |     $request   = $this->getRequest(); | 
					
						
							|  |  |  |     $user      = $request->getUser(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($this->getPostPHID()) { | 
					
						
							| 
									
										
										
										
											2012-10-15 14:50:04 -07:00
										 |  |  |       $post = id(new PhamePostQuery()) | 
					
						
							|  |  |  |         ->setViewer($user) | 
					
						
							|  |  |  |         ->withPHIDs(array($this->getPostPHID())) | 
					
						
							|  |  |  |         ->executeOne(); | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-15 14:50:04 -07:00
										 |  |  |       if (!$post) { | 
					
						
							|  |  |  |         return new Aphront404Response(); | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-15 14:50:04 -07:00
										 |  |  |       $this->setPhameTitle($post->getPhameTitle()); | 
					
						
							|  |  |  |       $blogger = PhabricatorObjectHandleData::loadOneHandle( | 
					
						
							|  |  |  |         $post->getBloggerPHID(), | 
					
						
							|  |  |  |         $user); | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |     } else if ($this->getBloggerName() && $this->getPhameTitle()) { | 
					
						
							|  |  |  |       $phame_title = $this->getPhameTitle(); | 
					
						
							|  |  |  |       $phame_title = PhabricatorSlug::normalize($phame_title); | 
					
						
							| 
									
										
										
										
											2012-10-12 16:01:33 -07:00
										 |  |  |       $blogger_user = id(new PhabricatorUser())->loadOneWhere( | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |         'username = %s', | 
					
						
							|  |  |  |         $this->getBloggerName()); | 
					
						
							| 
									
										
										
										
											2012-10-12 16:01:33 -07:00
										 |  |  |       if (!$blogger_user) { | 
					
						
							|  |  |  |         return new Aphront404Response(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       $blogger = PhabricatorObjectHandleData::loadOneHandle( | 
					
						
							|  |  |  |         $blogger_user->getPHID(), | 
					
						
							|  |  |  |         $user); | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |       if (!$blogger) { | 
					
						
							|  |  |  |         return new Aphront404Response(); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2012-07-19 09:03:10 -07:00
										 |  |  |       $posts = id(new PhamePostQuery()) | 
					
						
							| 
									
										
										
										
											2012-10-15 14:50:04 -07:00
										 |  |  |         ->setViewer($user) | 
					
						
							|  |  |  |         ->withBloggerPHIDs(array($blogger->getPHID())) | 
					
						
							|  |  |  |         ->withPhameTitles(array($phame_title)) | 
					
						
							| 
									
										
										
										
											2012-07-19 09:03:10 -07:00
										 |  |  |         ->execute(); | 
					
						
							|  |  |  |       $post = reset($posts); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-11 16:22:08 -07:00
										 |  |  |       if ($post && $phame_title != $this->getPhameTitle()) { | 
					
						
							|  |  |  |         $uri = $post->getViewURI($this->getBloggerName()); | 
					
						
							|  |  |  |         return id(new AphrontRedirectResponse())->setURI($uri); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!$post) { | 
					
						
							|  |  |  |       return new Aphront404Response(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($post->isDraft() && | 
					
						
							|  |  |  |         $post->getBloggerPHID() != $user->getPHID()) { | 
					
						
							|  |  |  |       return new Aphront404Response(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($post->isDraft()) { | 
					
						
							| 
									
										
										
										
											2012-10-12 16:01:33 -07:00
										 |  |  |       $notice = array( | 
					
						
							|  |  |  |         'title' => 'You are previewing a draft.', | 
					
						
							|  |  |  |         'body'  => 'Only you can see this draft until you publish it. '. | 
					
						
							|  |  |  |                    'If you chose a comment widget it will show up when '. | 
					
						
							|  |  |  |                    'you publish.' | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2012-07-19 09:03:10 -07:00
										 |  |  |     } else if ($request->getExists('saved')) { | 
					
						
							|  |  |  |       $new_link = phutil_render_tag( | 
					
						
							|  |  |  |         'a', | 
					
						
							|  |  |  |         array( | 
					
						
							|  |  |  |           'href' => '/phame/post/new/', | 
					
						
							|  |  |  |           'class' => 'button green', | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         'write another blog post' | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2012-10-12 16:01:33 -07:00
										 |  |  |       $notice = array( | 
					
						
							|  |  |  |         'title' => 'Saved post successfully.', | 
					
						
							|  |  |  |         'body'  => 'Seek even more phame and '.$new_link.'.' | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2012-10-12 16:01:33 -07:00
										 |  |  |       $notice = array(); | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-12 16:01:33 -07:00
										 |  |  |     $actions = array('more'); | 
					
						
							|  |  |  |     if ($post->getBloggerPHID() == $user->getPHID()) { | 
					
						
							|  |  |  |       $actions[] = 'edit'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-15 14:50:12 -07:00
										 |  |  |     $skin = new PhabricatorBlogSkin(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-12 16:01:33 -07:00
										 |  |  |     $skin | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |       ->setUser($user) | 
					
						
							|  |  |  |       ->setRequestURI($request->getRequestURI()) | 
					
						
							| 
									
										
										
										
											2012-10-12 16:01:33 -07:00
										 |  |  |       ->setBloggers(array($blogger->getPHID() => $blogger)) | 
					
						
							|  |  |  |       ->setPosts(array($post)) | 
					
						
							|  |  |  |       ->setNotice($notice) | 
					
						
							|  |  |  |       ->setShowPostComments(true) | 
					
						
							|  |  |  |       ->setActions($actions); | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $this->setShowSideNav(false); | 
					
						
							| 
									
										
										
										
											2012-10-12 16:01:33 -07:00
										 |  |  |     $this->setShowChrome($skin->getShowChrome()); | 
					
						
							|  |  |  |     $this->setDeviceReady($skin->getDeviceReady()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |     return $this->buildStandardPageResponse( | 
					
						
							|  |  |  |       array( | 
					
						
							| 
									
										
										
										
											2012-10-12 16:01:33 -07:00
										 |  |  |         $skin | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |       ), | 
					
						
							|  |  |  |       array( | 
					
						
							| 
									
										
										
										
											2012-05-01 11:50:51 +02:00
										 |  |  |         'title'   => $post->getTitle(), | 
					
						
							| 
									
										
										
										
											2012-04-12 13:09:04 -07:00
										 |  |  |       )); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |