|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Watching controller |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Handles displaying watched discussions and watched categories |
| 9 | + * |
| 10 | + */ |
| 11 | +class WatchingController extends VanillaController { |
| 12 | + |
| 13 | + /** @var arrayModels to include. */ |
| 14 | + public $Uses = ['Database', 'DiscussionModel', 'Form']; |
| 15 | + |
| 16 | + /** |
| 17 | + * Highlight route and include JS, CSS, and modules used by all methods. |
| 18 | + * |
| 19 | + * Always called by dispatcher before controller's requested method. |
| 20 | + * |
| 21 | + * @since 2.0.0 |
| 22 | + * @access public |
| 23 | + */ |
| 24 | + public function initialize() { |
| 25 | + parent::initialize(); |
| 26 | + $this->Menu->highlightRoute('/watching'); |
| 27 | + |
| 28 | + /** |
| 29 | + * The default Cache-Control header does not include no-store, which can cause issues (e.g. inaccurate unread |
| 30 | + * status or new comment counts) when users visit the discussion list via the browser's back button. The same |
| 31 | + * check is performed here as in Gdn_Controller before the Cache-Control header is added, but this value |
| 32 | + * includes the no-store specifier. |
| 33 | + */ |
| 34 | + if (Gdn::session()->isValid()) { |
| 35 | + $this->setHeader('Cache-Control', 'private, no-cache, no-store, max-age=0, must-revalidate'); |
| 36 | + } |
| 37 | + |
| 38 | + $this->CountCommentsPerPage = c('Vanilla.Comments.PerPage', 30); |
| 39 | + $this->fireEvent('AfterInitialize'); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Display categorioes and discussions the user has watched |
| 44 | + * |
| 45 | + * @param string $cp Category page |
| 46 | + * @param string $dp Discussion page |
| 47 | + * @throws Exception |
| 48 | + */ |
| 49 | + public function index($cp = '', $dp = '') { |
| 50 | + $this->addJsFile('jquery.gardenmorepager.js'); |
| 51 | + $this->addJsFile('topcoder.js'); |
| 52 | + $this->permission('Garden.SignIn.Allow'); |
| 53 | + Gdn_Theme::section('CategoryList'); |
| 54 | + |
| 55 | + // Sort filter is used for categories and discussions |
| 56 | + $sort = Gdn::request()->get('sort', null); |
| 57 | + $saveSorting = $sort !== null && Gdn::request()->get('save') && Gdn::session()->validateTransientKey(Gdn::request()->get('TransientKey', '')); |
| 58 | + if($saveSorting) { |
| 59 | + Gdn::session()->setPreference('CategorySort', $sort); |
| 60 | + } |
| 61 | + $sort = Gdn::session()->getPreference('CategorySort', false); |
| 62 | + $this->setData('CategorySort', $sort); |
| 63 | + |
| 64 | + $userMetaModel = new UserMetaModel(); |
| 65 | + list($cp, $categoryLimit) = offsetLimit($cp, 30); |
| 66 | + |
| 67 | + // Validate Category Page |
| 68 | + if (!is_numeric($cp) || $cp < 0) { |
| 69 | + $cp = 0; |
| 70 | + } |
| 71 | + $categorySort = $sort == 'old'? 'asc': 'desc'; |
| 72 | + $watchedCategoryIDs = $userMetaModel->getWatchedCategories(Gdn::session()->UserID, $categorySort, $categoryLimit, $cp); |
| 73 | + $countOfWatchedCategories = $userMetaModel->userWatchedCategoriesCount(Gdn::session()->UserID); |
| 74 | + |
| 75 | + $categories = []; |
| 76 | + $categoryModel = new CategoryModel(); |
| 77 | + foreach ($watchedCategoryIDs as $item) { |
| 78 | + $category = CategoryModel::categories(val('CategoryID', $item)); |
| 79 | + // $category['Archived'] |
| 80 | + // if (!$category['PermsDiscussionsView']) { |
| 81 | + // continue; |
| 82 | + // } |
| 83 | + $categories[] = $category; |
| 84 | + } |
| 85 | + $categoryModel->joinRecent($categories); |
| 86 | + $this->setData('WatchedCategories', $categories); |
| 87 | + $this->setData('CountWatchedCategories', $countOfWatchedCategories); |
| 88 | + |
| 89 | + $pagerFactory = new Gdn_PagerFactory(); |
| 90 | + $this->WatchedCategoriesPager = $pagerFactory->getPager('MorePager', $this); |
| 91 | + $this->WatchedCategoriesPager->ClientID='WatchingCategories'; |
| 92 | + $this->WatchedCategoriesPager->MoreCode = 'More Categories'; |
| 93 | + $this->WatchedCategoriesPager->configure($cp, |
| 94 | + $categoryLimit, |
| 95 | + $countOfWatchedCategories, |
| 96 | + 'watching?cp={Page}' |
| 97 | + ); |
| 98 | + |
| 99 | + Gdn_Theme::section('DiscussionList'); |
| 100 | + |
| 101 | + list($dp, $discussionlimit) = offsetLimit($dp, 30); |
| 102 | + if (!is_numeric($dp) || $dp < 0) { |
| 103 | + $dp = 0; |
| 104 | + } |
| 105 | + |
| 106 | + $discussionModel = new DiscussionModel(); |
| 107 | + $discussionModel->setSort($sort); |
| 108 | + $discussionModel->setFilters(Gdn::request()->get()); |
| 109 | + $wheres = [ |
| 110 | + 'w.Bookmarked' => '1', |
| 111 | + 'w.UserID' => Gdn::session()->UserID |
| 112 | + ]; |
| 113 | + |
| 114 | + $this->DiscussionData = $discussionModel->get($dp, $discussionlimit, $wheres); |
| 115 | + $this->setData('Discussions', $this->DiscussionData); |
| 116 | + $countDiscussions = $discussionModel->getCount($wheres); |
| 117 | + $this->setData('CountDiscussions', $countDiscussions); |
| 118 | + |
| 119 | + $pagerFactory = new Gdn_PagerFactory(); |
| 120 | + $this->DiscussionPager = $pagerFactory->getPager('MorePager', $this); |
| 121 | + $this->DiscussionPager->ClientID='WatchingDiscussions'; |
| 122 | + $this->DiscussionPager->MoreCode = 'More Discussions'; |
| 123 | + $this->DiscussionPager->configure($dp, |
| 124 | + $discussionlimit, |
| 125 | + $countDiscussions, |
| 126 | + 'watching?dp={Page}'); |
| 127 | + |
| 128 | + $this->allowJSONP(true); |
| 129 | + |
| 130 | + // Deliver JSON data if necessary |
| 131 | + if ($this->deliveryType() != DELIVERY_TYPE_ALL) { |
| 132 | + if ($dp > 0) { |
| 133 | + //$this->setJson('LessRow', $this->DiscussionPager->toString('less')); |
| 134 | + $this->setJson('MoreRow', $this->DiscussionPager->toString('more')); |
| 135 | + $this->setJson('Loading', $dp.' to '.$discussionlimit); |
| 136 | + $this->View = 'discussions'; |
| 137 | + } else if($cp > 0) { |
| 138 | + //$this->setJson('LessRow', $this->WatchedCategoriesPager->toString('less')); |
| 139 | + $this->setJson('MoreRow', $this->WatchedCategoriesPager->toString('more')); |
| 140 | + $this->setJson('Loading', $cp.' to '.$categoryLimit); |
| 141 | + $this->View = 'categories'; |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + $this->canonicalUrl(url('/watching', true)); |
| 147 | + |
| 148 | + // Add modules |
| 149 | + $this->addModule('DiscussionFilterModule'); |
| 150 | + |
| 151 | + // Render default view |
| 152 | + $this->setData('Title', t('Watching')); |
| 153 | + $this->setData('Breadcrumbs', [['Name' => t('Watching'), 'Url' => '/watching']]); |
| 154 | + |
| 155 | + $this->render(); |
| 156 | + } |
| 157 | +} |
0 commit comments