PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/applications/vanilla/settings/class.hooks.php

https://github.com/Dykam/Garden
PHP | 202 lines | 140 code | 24 blank | 38 comment | 19 complexity | 2d70a48fadc3d6dfe56ecbc5915cdaf3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Vanilla Forums Inc.
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
  8. Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
  9. */
  10. class VanillaHooks implements Gdn_IPlugin {
  11. public function UserModel_SessionQuery_Handler(&$Sender) {
  12. // Add some extra fields to the session query
  13. $Sender->SQL->Select('u.CountDiscussions, u.CountUnreadDiscussions, u.CountDrafts, u.CountBookmarks');
  14. }
  15. // Remove data when deleting a user
  16. public function UserModel_BeforeDeleteUser_Handler($Sender) {
  17. $UserID = GetValue('UserID', $Sender->EventArguments);
  18. $Options = GetValue('Options', $Sender->EventArguments, array());
  19. $Options = is_array($Options) ? $Options : array();
  20. $Sender->SQL->Delete('UserDiscussion', array('UserID' => $UserID));
  21. $Sender->SQL->Delete('Draft', array('InsertUserID' => $UserID));
  22. $DeleteMethod = GetValue('DeleteMethod', $Options, 'delete');
  23. if ($DeleteMethod == 'delete') {
  24. $Sender->SQL->Delete('Comment', array('InsertUserID' => $UserID));
  25. } else if ($DeleteMethod == 'wipe') {
  26. $Sender->SQL->From('Comment')
  27. ->Join('Discussion d', 'c.DiscussionID = d.DiscussionID')
  28. ->Delete('Comment c', array('d.InsertUserID' => $UserID));
  29. $Sender->SQL->Update('Comment')
  30. ->Set('Body', T('The user and all related content has been deleted.'))
  31. ->Set('Format', 'Deleted')
  32. ->Where('InsertUserID', $UserID)
  33. ->Put();
  34. } else {
  35. // Leave comments
  36. }
  37. $Sender->SQL->Delete('Discussion', array('InsertUserID' => $UserID));
  38. // Remove the user's profile information related to this application
  39. $Sender->SQL->Update('User')
  40. ->Set(array(
  41. 'CountDiscussions' => 0,
  42. 'CountUnreadDiscussions' => 0,
  43. 'CountComments' => 0,
  44. 'CountDrafts' => 0,
  45. 'CountBookmarks' => 0
  46. ))
  47. ->Where('UserID', $UserID)
  48. ->Put();
  49. }
  50. public function Base_Render_Before(&$Sender) {
  51. // Add menu items.
  52. $Session = Gdn::Session();
  53. if ($Sender->Menu) {
  54. $Sender->Menu->AddLink(T('Discussions'), T('Discussions'), '/discussions', FALSE);
  55. // if ($Session->IsValid())
  56. // $Sender->Menu->AddLink(T('Discussions'), T('New Discussion'), '/post/discussion', FALSE);
  57. }
  58. }
  59. public function ProfileController_AddProfileTabs_Handler(&$Sender) {
  60. if (is_object($Sender->User) && $Sender->User->UserID > 0 && $Sender->User->CountDiscussions > 0) {
  61. // Add the discussion tab
  62. $Sender->AddProfileTab(T('Discussions'), 'profile/discussions/'.$Sender->User->UserID.'/'.urlencode($Sender->User->Name));
  63. // Add the discussion tab's css
  64. $Sender->AddCssFile('vanillaprofile.css', 'vanilla');
  65. $Sender->AddJsFile('/js/library/jquery.gardenmorepager.js');
  66. $Sender->AddJsFile('discussions.js');
  67. }
  68. }
  69. public function ProfileController_AfterPreferencesDefined_Handler(&$Sender) {
  70. $Sender->Preferences['Email Notifications']['Email.DiscussionComment'] = T('Notify me when people comment on my discussions.');
  71. $Sender->Preferences['Email Notifications']['Email.DiscussionMention'] = T('Notify me when people mention me in discussion titles.');
  72. $Sender->Preferences['Email Notifications']['Email.CommentMention'] = T('Notify me when people mention me in comments.');
  73. $Sender->Preferences['Email Notifications']['Email.BookmarkComment'] = T('Notify me when people comment on my bookmarked discussions.');
  74. }
  75. /**
  76. * Add the discussion search to the search.
  77. * @param SearchController $Sender
  78. */
  79. public function SearchController_Search_Handler($Sender) {
  80. include_once(dirname(__FILE__).DS.'..'.DS.'models'.DS.'class.vanillasearchmodel.php');
  81. $SearchModel = new VanillaSearchModel();
  82. $SearchModel->Search($Sender->SearchModel);
  83. }
  84. // Load some information into the BuzzData collection
  85. public function SettingsController_DashboardData_Handler(&$Sender) {
  86. $DiscussionModel = new DiscussionModel();
  87. // Number of Discussions
  88. $CountDiscussions = $DiscussionModel->GetCount();
  89. $Sender->AddDefinition('CountDiscussions', $CountDiscussions);
  90. $Sender->BuzzData[T('Discussions')] = number_format($CountDiscussions);
  91. // Number of New Discussions in the last day
  92. $Sender->BuzzData[T('New discussions in the last day')] = number_format($DiscussionModel->GetCount(array('d.DateInserted >=' => Gdn_Format::ToDateTime(strtotime('-1 day')))));
  93. // Number of New Discussions in the last week
  94. $Sender->BuzzData[T('New discussions in the last week')] = number_format($DiscussionModel->GetCount(array('d.DateInserted >=' => Gdn_Format::ToDateTime(strtotime('-1 week')))));
  95. $CommentModel = new CommentModel();
  96. // Number of Comments
  97. $CountComments = $CommentModel->GetCountWhere();
  98. $Sender->AddDefinition('CountComments', $CountComments);
  99. $Sender->BuzzData[T('Comments')] = number_format($CountComments);
  100. // Number of New Comments in the last day
  101. $Sender->BuzzData[T('New comments in the last day')] = number_format($CommentModel->GetCountWhere(array('DateInserted >=' => Gdn_Format::ToDateTime(strtotime('-1 day')))));
  102. // Number of New Comments in the last week
  103. $Sender->BuzzData[T('New comments in the last week')] = number_format($CommentModel->GetCountWhere(array('DateInserted >=' => Gdn_Format::ToDateTime(strtotime('-1 week')))));
  104. }
  105. public function ProfileController_Discussions_Create(&$Sender) {
  106. $UserReference = ArrayValue(0, $Sender->RequestArgs, '');
  107. $Username = ArrayValue(1, $Sender->RequestArgs, '');
  108. $Offset = ArrayValue(2, $Sender->RequestArgs, 0);
  109. // Tell the ProfileController what tab to load
  110. $Sender->GetUserInfo($UserReference, $Username);
  111. $Sender->SetTabView('Discussions', 'Profile', 'Discussions', 'Vanilla');
  112. // Load the data for the requested tab.
  113. if (!is_numeric($Offset) || $Offset < 0)
  114. $Offset = 0;
  115. $Limit = Gdn::Config('Vanilla.Discussions.PerPage', 30);
  116. $DiscussionModel = new DiscussionModel();
  117. $Sender->DiscussionData = $DiscussionModel->Get($Offset, $Limit, array('d.InsertUserID' => $Sender->User->UserID));
  118. $CountDiscussions = $Offset + $Sender->DiscussionData->NumRows();
  119. if ($Sender->DiscussionData->NumRows() == $Limit)
  120. $CountDiscussions = $Offset + $Limit + 1;
  121. // Build a pager
  122. $PagerFactory = new Gdn_PagerFactory();
  123. $Sender->Pager = $PagerFactory->GetPager('MorePager', $Sender);
  124. $Sender->Pager->MoreCode = 'More Discussions';
  125. $Sender->Pager->LessCode = 'Newer Discussions';
  126. $Sender->Pager->ClientID = 'Pager';
  127. $Sender->Pager->Configure(
  128. $Offset,
  129. $Limit,
  130. $CountDiscussions,
  131. 'profile/discussions/'.$Sender->User->UserID.'/'.Gdn_Format::Url($Sender->User->Name).'/%1$s/'
  132. );
  133. // Deliver json data if necessary
  134. if ($Sender->DeliveryType() != DELIVERY_TYPE_ALL && $Offset > 0) {
  135. $Sender->SetJson('LessRow', $Sender->Pager->ToString('less'));
  136. $Sender->SetJson('MoreRow', $Sender->Pager->ToString('more'));
  137. $Sender->View = 'discussions';
  138. }
  139. // Set the handlertype back to normal on the profilecontroller so that it fetches it's own views
  140. $Sender->HandlerType = HANDLER_TYPE_NORMAL;
  141. // Do not show discussion options
  142. $Sender->ShowOptions = FALSE;
  143. // Render the ProfileController
  144. $Sender->Render();
  145. }
  146. /**
  147. * Make sure that vanilla administrators can see the garden admin pages.
  148. */
  149. public function SettingsController_DefineAdminPermissions_Handler(&$Sender) {
  150. if (isset($Sender->RequiredAdminPermissions)) {
  151. $Sender->RequiredAdminPermissions[] = 'Vanilla.Settings.Manage';
  152. $Sender->RequiredAdminPermissions[] = 'Vanilla.Categories.Manage';
  153. $Sender->RequiredAdminPermissions[] = 'Vanilla.Spam.Manage';
  154. }
  155. }
  156. public function Base_GetAppSettingsMenuItems_Handler(&$Sender) {
  157. $Menu = &$Sender->EventArguments['SideMenu'];
  158. $Menu->AddLink('Forum', T('Categories'), 'vanilla/settings/managecategories', 'Vanilla.Categories.Manage');
  159. $Menu->AddLink('Forum', T('Spam'), 'vanilla/settings/spam', 'Vanilla.Spam.Manage');
  160. $Menu->AddLink('Forum', T('Advanced'), 'vanilla/settings/advanced', 'Vanilla.Settings.Manage');
  161. }
  162. public function Setup() {
  163. $Database = Gdn::Database();
  164. $Config = Gdn::Factory(Gdn::AliasConfig);
  165. $Drop = Gdn::Config('Vanilla.Version') === FALSE ? TRUE : FALSE;
  166. $Explicit = TRUE;
  167. $Validation = new Gdn_Validation(); // This is going to be needed by structure.php to validate permission names
  168. include(PATH_APPLICATIONS . DS . 'vanilla' . DS . 'settings' . DS . 'structure.php');
  169. $ApplicationInfo = array();
  170. include(CombinePaths(array(PATH_APPLICATIONS . DS . 'vanilla' . DS . 'settings' . DS . 'about.php')));
  171. $Version = ArrayValue('Version', ArrayValue('Vanilla', $ApplicationInfo, array()), 'Undefined');
  172. $Save = array(
  173. 'Vanilla.Version' => $Version,
  174. 'Routes.DefaultController' => 'discussions'
  175. );
  176. SaveToConfig($Save);
  177. }
  178. }