|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Hooks for Conversations. |
| 4 | + * |
| 5 | + * @copyright 2009-2019 Vanilla Forums Inc. |
| 6 | + * @license GPL-2.0-only |
| 7 | + * @package Conversations |
| 8 | + * @since 2.0 |
| 9 | + */ |
| 10 | + |
| 11 | +use Garden\Container\Container; |
| 12 | +use Garden\Container\Reference; |
| 13 | + |
| 14 | +/** |
| 15 | + * Handles hooks into Dashboard and Vanilla. |
| 16 | + */ |
| 17 | +class ConversationsHooks implements Gdn_IPlugin { |
| 18 | + /** |
| 19 | + * Handle the container init event to register things with the container. |
| 20 | + * |
| 21 | + * @param Container $dic |
| 22 | + */ |
| 23 | + public function container_init(Container $dic) { |
| 24 | + $dic->rule(\Vanilla\Menu\CounterModel::class) |
| 25 | + ->addCall('addProvider', [new Reference(ConversationCounterProvider::class)]) |
| 26 | + ; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * |
| 31 | + * |
| 32 | + * @param DbaController $sender |
| 33 | + */ |
| 34 | + public function dbaController_countJobs_handler($sender) { |
| 35 | + $counts = [ |
| 36 | + 'Conversation' => ['CountMessages', 'CountParticipants', 'FirstMessageID', 'LastMessageID', 'DateUpdated', 'UpdateUserID'] |
| 37 | + ]; |
| 38 | + |
| 39 | + foreach ($counts as $table => $columns) { |
| 40 | + foreach ($columns as $column) { |
| 41 | + $name = "Recalculate $table.$column"; |
| 42 | + $url = "/dba/counts.json?".http_build_query(['table' => $table, 'column' => $column]); |
| 43 | + |
| 44 | + $sender->Data['Jobs'][$name] = $url; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Remove data when deleting a user. |
| 51 | + * |
| 52 | + * @since 2.0.0 |
| 53 | + * @access public |
| 54 | + */ |
| 55 | + public function userModel_beforeDeleteUser_handler($sender) { |
| 56 | + $userID = val('UserID', $sender->EventArguments); |
| 57 | + $options = val('Options', $sender->EventArguments, []); |
| 58 | + $options = is_array($options) ? $options : []; |
| 59 | + |
| 60 | + $deleteMethod = val('DeleteMethod', $options, 'delete'); |
| 61 | + if ($deleteMethod == 'delete') { |
| 62 | + /** @var Gdn_SQLDriver $sql */ |
| 63 | + $sql = $sender->SQL; |
| 64 | + $sql |
| 65 | + ->from('UserConversation as uc') |
| 66 | + ->join('Conversation as c', 'c.ConversationID = uc.ConversationID') |
| 67 | + ->where(['c.InsertUserID' => $userID]) |
| 68 | + ->orWhere(['c.UpdateUserID' => $userID]) |
| 69 | + ->delete(); |
| 70 | + $sql |
| 71 | + ->from('ConversationMessage as cm') |
| 72 | + ->join('Conversation as c', 'c.ConversationID = cm.ConversationID') |
| 73 | + ->where(['c.InsertUserID' => $userID]) |
| 74 | + ->orWhere(['c.UpdateUserID' => $userID]) |
| 75 | + ->delete(); |
| 76 | + |
| 77 | + $sender->SQL->delete('Conversation', ['InsertUserID' => $userID]); |
| 78 | + $sender->SQL->delete('Conversation', ['UpdateUserID' => $userID]); |
| 79 | + } elseif ($deleteMethod == 'wipe') { |
| 80 | + $sender->SQL->update('ConversationMessage') |
| 81 | + ->set('Body', t('The user and all related content has been deleted.')) |
| 82 | + ->set('Format', 'Deleted') |
| 83 | + ->where('InsertUserID', $userID) |
| 84 | + ->put(); |
| 85 | + } |
| 86 | + // Remove the user's profile information related to this application |
| 87 | + $sender->SQL->update('User') |
| 88 | + ->set('CountUnreadConversations', 0) |
| 89 | + ->where('UserID', $userID) |
| 90 | + ->put(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Add 'Inbox' to profile menu. |
| 95 | + * |
| 96 | + * @since 2.0.0 |
| 97 | + * @access public |
| 98 | + */ |
| 99 | + public function profileController_addProfileTabs_handler($sender) { |
| 100 | + if (Gdn::session()->isValid()) { |
| 101 | + $inbox = t('Inbox'); |
| 102 | + $inboxHtml = sprite('SpInbox').' '.$inbox; |
| 103 | + $inboxLink = '/messages/all'; |
| 104 | + |
| 105 | + if (Gdn::session()->UserID != $sender->User->UserID) { |
| 106 | + // Accomodate admin access |
| 107 | + if (c('Conversations.Moderation.Allow', false) && Gdn::session()->checkPermission('Conversations.Moderation.Manage')) { |
| 108 | + $countUnread = $sender->User->CountUnreadConversations; |
| 109 | + $inboxLink .= "?userid={$sender->User->UserID}"; |
| 110 | + } else { |
| 111 | + return; |
| 112 | + } |
| 113 | + } else { |
| 114 | + // Current user |
| 115 | + $countUnread = Gdn::session()->User->CountUnreadConversations; |
| 116 | + } |
| 117 | + |
| 118 | + if (is_numeric($countUnread) && $countUnread > 0) { |
| 119 | + $inboxHtml .= ' <span class="Aside"><span class="Count">'.$countUnread.'</span></span>'; |
| 120 | + } |
| 121 | + $sender->addProfileTab($inbox, $inboxLink, 'Inbox', $inboxHtml); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Add "Message" option to profile options. |
| 127 | + */ |
| 128 | + public function profileController_beforeProfileOptions_handler($sender, $args) { |
| 129 | + if (!$sender->EditMode && |
| 130 | + Gdn::session()->UserID != $sender->User->UserID && |
| 131 | + Gdn::session()->checkPermission('Conversations.Conversations.Add') |
| 132 | + ) { |
| 133 | + $sender->EventArguments['MemberOptions'][] = [ |
| 134 | + 'Text' => sprite('SpMessage').' '.t('Message'), |
| 135 | + 'Url' => '/messages/add/'.rawurlencode($sender->User->Name), |
| 136 | + 'CssClass' => 'MessageUser' |
| 137 | + ]; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + /** |
| 143 | + * Additional options for the Preferences screen. |
| 144 | + * |
| 145 | + * @since 2.0.0 |
| 146 | + * @access public |
| 147 | + */ |
| 148 | + public function profileController_afterPreferencesDefined_handler($sender) { |
| 149 | + if (c('Garden.Profile.ShowActivities', true)) { |
| 150 | + $sender->Preferences['Notifications']['Email.ConversationMessage'] = t('Notify me of private messages.'); |
| 151 | + $sender->Preferences['Notifications']['Popup.ConversationMessage'] = t('Notify me of private messages.'); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Add 'Inbox' to global menu. |
| 157 | + * |
| 158 | + * @since 2.0.0 |
| 159 | + * @access public |
| 160 | + */ |
| 161 | + public function base_render_before($sender) { |
| 162 | + // Add the menu options for conversations |
| 163 | + if ($sender->Menu && Gdn::session()->isValid()) { |
| 164 | + $inbox = t('Inbox'); |
| 165 | + $countUnreadConversations = val('CountUnreadConversations', Gdn::session()->User); |
| 166 | + if (is_numeric($countUnreadConversations) && $countUnreadConversations > 0) { |
| 167 | + $inbox .= ' <span class="Alert">'.$countUnreadConversations.'</span>'; |
| 168 | + } |
| 169 | + |
| 170 | + $sender->Menu->addLink('Conversations', $inbox, '/messages/all', false, ['Standard' => true]); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Let us add Messages to the Inbox page. |
| 176 | + */ |
| 177 | + public function base_afterGetLocationData_handler($sender, $args) { |
| 178 | + $args['ControllerData']['Conversations/messages/inbox'] = t('Inbox Page'); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Provide default permissions for roles, based on the value in their Type column. |
| 183 | + * |
| 184 | + * @param PermissionModel $sender Instance of permission model that fired the event |
| 185 | + */ |
| 186 | + public function permissionModel_defaultPermissions_handler($sender) { |
| 187 | + $sender->addDefault( |
| 188 | + RoleModel::TYPE_MEMBER, |
| 189 | + ['Conversations.Conversations.Add' => 1] |
| 190 | + ); |
| 191 | + $sender->addDefault( |
| 192 | + RoleModel::TYPE_MODERATOR, |
| 193 | + ['Conversations.Conversations.Add' => 1] |
| 194 | + ); |
| 195 | + $sender->addDefault( |
| 196 | + RoleModel::TYPE_ADMINISTRATOR, |
| 197 | + ['Conversations.Conversations.Add' => 1] |
| 198 | + ); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Database & config changes to be done upon enable. |
| 203 | + * |
| 204 | + * @since 2.0.0 |
| 205 | + * @access public |
| 206 | + */ |
| 207 | + public function setup() { |
| 208 | + $Database = Gdn::database(); |
| 209 | + $Config = Gdn::factory(Gdn::AliasConfig); |
| 210 | + $Drop = false; |
| 211 | + $Validation = new Gdn_Validation(); // This is going to be needed by structure.php to validate permission names |
| 212 | + include(PATH_APPLICATIONS.DS.'conversations'.DS.'settings'.DS.'structure.php'); |
| 213 | + include(PATH_APPLICATIONS.DS.'conversations'.DS.'settings'.DS.'stub.php'); |
| 214 | + } |
| 215 | +} |
0 commit comments