[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @task paging Paging 5 * @task internal Internals 6 */ 7 abstract class PhameBasicBlogSkin extends PhameBlogSkin { 8 9 private $pager; 10 private $title; 11 private $description; 12 private $oGType; 13 private $uriPath; 14 15 public function setURIPath($uri_path) { 16 $this->uriPath = $uri_path; 17 return $this; 18 } 19 public function getURIPath() { 20 return $this->uriPath; 21 } 22 23 protected function setOGType($og_type) { 24 $this->oGType = $og_type; 25 return $this; 26 } 27 protected function getOGType() { 28 return $this->oGType; 29 } 30 31 protected function setDescription($description) { 32 $this->description = $description; 33 return $this; 34 } 35 protected function getDescription() { 36 return $this->description; 37 } 38 39 protected function setTitle($title) { 40 $this->title = $title; 41 return $this; 42 } 43 protected function getTitle() { 44 return $this->title; 45 } 46 47 public function processRequest() { 48 $request = $this->getRequest(); 49 50 $content = $this->renderContent($request); 51 52 if (!$content) { 53 $content = $this->render404Page(); 54 } 55 56 $content = array( 57 $this->renderHeader(), 58 $content, 59 $this->renderFooter(), 60 ); 61 62 $view = id(new PhabricatorBarePageView()) 63 ->setRequest($request) 64 ->setController($this) 65 ->setDeviceReady(true) 66 ->setTitle($this->getBlog()->getName()); 67 68 if ($this->getPreview()) { 69 $view->setFrameable(true); 70 } 71 72 73 $view->appendChild($content); 74 75 $response = new AphrontWebpageResponse(); 76 $response->setContent($view->render()); 77 78 return $response; 79 } 80 81 public function getSkinName() { 82 return get_class($this); 83 } 84 85 abstract protected function renderHeader(); 86 abstract protected function renderFooter(); 87 88 protected function renderPostDetail(PhamePostView $post) { 89 return $post; 90 } 91 92 protected function renderPostList(array $posts) { 93 $summaries = array(); 94 foreach ($posts as $post) { 95 $summaries[] = $post->renderWithSummary(); 96 } 97 98 $list = phutil_tag( 99 'div', 100 array( 101 'class' => 'phame-post-list', 102 ), 103 id(new AphrontNullView())->appendChild($summaries)->render()); 104 105 $pager = null; 106 if ($this->renderOlderPageLink() || $this->renderNewerPageLink()) { 107 $pager = phutil_tag( 108 'div', 109 array( 110 'class' => 'phame-pager', 111 ), 112 array( 113 $this->renderOlderPageLink(), 114 $this->renderNewerPageLink(), 115 )); 116 } 117 118 return array( 119 $list, 120 $pager, 121 ); 122 } 123 124 protected function render404Page() { 125 return phutil_tag('h2', array(), pht('404 Not Found')); 126 } 127 128 final public function getResourceURI($resource) { 129 $root = $this->getSpecification()->getRootDirectory(); 130 $path = $root.DIRECTORY_SEPARATOR.$resource; 131 132 $data = Filesystem::readFile($path); 133 $hash = PhabricatorHash::digest($data); 134 $hash = substr($hash, 0, 6); 135 $id = $this->getBlog()->getID(); 136 137 $uri = '/phame/r/'.$id.'/'.$hash.'/'.$resource; 138 $uri = PhabricatorEnv::getCDNURI($uri); 139 140 return $uri; 141 } 142 143 /* -( Paging )------------------------------------------------------------- */ 144 145 146 /** 147 * @task paging 148 */ 149 public function getPageSize() { 150 return 100; 151 } 152 153 154 /** 155 * @task paging 156 */ 157 protected function getOlderPageURI() { 158 if ($this->pager) { 159 $next = $this->pager->getNextPageID(); 160 if ($next) { 161 return $this->getURI('older/'.$next.'/'); 162 } 163 } 164 return null; 165 } 166 167 168 /** 169 * @task paging 170 */ 171 protected function renderOlderPageLink() { 172 $uri = $this->getOlderPageURI(); 173 if (!$uri) { 174 return null; 175 } 176 return phutil_tag( 177 'a', 178 array( 179 'class' => 'phame-page-link phame-page-older', 180 'href' => $uri, 181 ), 182 pht("\xE2\x80\xB9 Older")); 183 } 184 185 186 /** 187 * @task paging 188 */ 189 protected function getNewerPageURI() { 190 if ($this->pager) { 191 $next = $this->pager->getPrevPageID(); 192 if ($next) { 193 return $this->getURI('newer/'.$next.'/'); 194 } 195 } 196 return null; 197 } 198 199 200 /** 201 * @task paging 202 */ 203 protected function renderNewerPageLink() { 204 $uri = $this->getNewerPageURI(); 205 if (!$uri) { 206 return null; 207 } 208 return phutil_tag( 209 'a', 210 array( 211 'class' => 'phame-page-link phame-page-newer', 212 'href' => $uri, 213 ), 214 pht("Newer \xE2\x80\xBA")); 215 } 216 217 218 /* -( Internals )---------------------------------------------------------- */ 219 220 221 /** 222 * @task internal 223 */ 224 protected function renderContent(AphrontRequest $request) { 225 $user = $request->getUser(); 226 227 $matches = null; 228 $path = $request->getPath(); 229 // default to the blog-wide values 230 $this->setTitle($this->getBlog()->getName()); 231 $this->setDescription($this->getBlog()->getDescription()); 232 $this->setOGType('website'); 233 $this->setURIPath(''); 234 if (preg_match('@^/post/(?P<name>.*)$@', $path, $matches)) { 235 $post = id(new PhamePostQuery()) 236 ->setViewer($user) 237 ->withBlogPHIDs(array($this->getBlog()->getPHID())) 238 ->withPhameTitles(array($matches['name'])) 239 ->executeOne(); 240 241 if ($post) { 242 $description = $post->getMarkupText(PhamePost::MARKUP_FIELD_SUMMARY); 243 $this->setTitle($post->getTitle()); 244 $this->setDescription($description); 245 $this->setOGType('article'); 246 $this->setURIPath('post/'.$post->getPhameTitle()); 247 $view = head($this->buildPostViews(array($post))); 248 return $this->renderPostDetail($view); 249 } 250 } else { 251 $pager = new AphrontCursorPagerView(); 252 253 if (preg_match('@^/older/(?P<before>\d+)/$@', $path, $matches)) { 254 $pager->setAfterID($matches['before']); 255 } else if (preg_match('@^/newer/(?P<after>\d)/$@', $path, $matches)) { 256 $pager->setBeforeID($matches['after']); 257 } else if (preg_match('@^/$@', $path, $matches)) { 258 // Just show the first page. 259 } else { 260 return null; 261 } 262 263 $pager->setPageSize($this->getPageSize()); 264 265 $posts = id(new PhamePostQuery()) 266 ->setViewer($user) 267 ->withBlogPHIDs(array($this->getBlog()->getPHID())) 268 ->executeWithCursorPager($pager); 269 270 $this->pager = $pager; 271 272 if ($posts) { 273 $views = $this->buildPostViews($posts); 274 return $this->renderPostList($views); 275 } 276 } 277 278 return null; 279 } 280 281 private function buildPostViews(array $posts) { 282 assert_instances_of($posts, 'PhamePost'); 283 $user = $this->getRequest()->getUser(); 284 285 $engine = id(new PhabricatorMarkupEngine()) 286 ->setViewer($user); 287 288 $phids = array(); 289 foreach ($posts as $post) { 290 $engine->addObject($post, PhamePost::MARKUP_FIELD_BODY); 291 $engine->addObject($post, PhamePost::MARKUP_FIELD_SUMMARY); 292 293 $phids[] = $post->getBloggerPHID(); 294 } 295 296 $handles = id(new PhabricatorHandleQuery()) 297 ->setViewer($user) 298 ->withPHIDs($phids) 299 ->execute(); 300 301 $engine->process(); 302 303 $views = array(); 304 foreach ($posts as $post) { 305 $view = id(new PhamePostView()) 306 ->setUser($user) 307 ->setSkin($this) 308 ->setPost($post) 309 ->setBody($engine->getOutput($post, PhamePost::MARKUP_FIELD_BODY)) 310 ->setSummary($engine->getOutput($post, PhamePost::MARKUP_FIELD_SUMMARY)) 311 ->setAuthor($handles[$post->getBloggerPHID()]); 312 313 $post->makeEphemeral(); 314 if (!$post->getDatePublished()) { 315 $post->setDatePublished(time()); 316 } 317 318 $views[] = $view; 319 } 320 321 return $views; 322 } 323 324 }
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 |