Last active
January 1, 2016 06:29
-
-
Save kaecyra/8105537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Hook early and perform wintery-type actions | |
* | |
* @param Gdn_Dispatcher $sender | |
* @return void | |
*/ | |
public function Gdn_Dispatcher_AppStartup_Handler($sender) { | |
// Don't do anything if its not Winter Time | |
if (!$this->enabled) return; | |
// Don't work with incomplete data | |
if (!$this->loungeParentID || !$this->loungeStorageID) return; | |
// Don't mess with Embed | |
if (preg_match('`discussion/embed/?`', Gdn::request()->path())) return; | |
// Has the lounge been opened this year? | |
$loungeOpen = (C('Plugins.Winter.LoungeOpen', false) == date('Y')); | |
// Only administrators can trigger lounge management events | |
if (Gdn::session()->checkPermission('Garden.Settings.Manage')) { | |
// Find the lounge on the boards | |
$lounge = CategoryModel::categories($this->loungeID); | |
switch ($this->loungeEnabled) { | |
case true: | |
if (!$loungeOpen) { | |
// Open it | |
$categoryModel = new CategoryModel(); | |
// Defense against deleted lounge | |
if (!$lounge) { | |
saveToConfig('Plugins.Winter.LoungeID', null); | |
saveToConfig('Plugins.Winter.LoungeOpen', false); | |
$this->loungeID = null; | |
} | |
// Create lounge (it was deleted by a fuckwit, apparently) | |
if (!$lounge) { | |
$permissionModel = new PermissionModel(); | |
// Get default role | |
$defaultRoles = C('Garden.Registration.DefaultRoles'); | |
$defaultMemberRoleID = val(0, $defaultRoles); | |
// Lounge Wrapper | |
$loungeWrapperID = $categoryModel->save(array( | |
'Name' => T('Christmas'), | |
'ParentCategoryID' => $this->loungeStorageID, | |
'UrlCode' => 'christmas', | |
'CssClass' => 'Christmas', | |
'CustomPermissions' => 1, | |
'AllowDiscussions' => 0 | |
)); | |
// Give it no permissions by default | |
$permissionModel->save(array( | |
'RoleID' => $defaultMemberRoleID, | |
'JunctionTable' => 'Category', | |
'JunctionColumn' => 'PermissionCategoryID', | |
'JunctionID' => $loungeWrapperID | |
)); | |
// Create Lounge Category in storage | |
$loungeID = $categoryModel->save(array( | |
'Name' => T('The Christmas Hangout'), | |
'ParentCategoryID' => $loungeWrapperID, | |
'UrlCode' => 'the-christmas-hangout', | |
'CssClass' => 'ChristmasHangout', | |
'AllowDiscussions' => 1 | |
)); | |
// Give it sparse permissions by default | |
$permissionModel->save(array( | |
'RoleID' => $defaultMemberRoleID, | |
'JunctionTable' => 'Category', | |
'JunctionColumn' => 'PermissionCategoryID', | |
'JunctionID' => $loungeID | |
)); | |
// We manage the permissions of the wrapper, not the actual lounge | |
saveToConfig('Plugins.Winter.LoungeID', $loungeWrapperID); | |
$this->loungeID = $loungeWrapperID; | |
CategoryModel::clearCache(); | |
CategoryModel::$Categories = null; | |
$lounge = CategoryModel::categories($this->loungeID); | |
} | |
// Move lounge to parent | |
$lounge['ParentCategoryID'] = $this->loungeParentID; | |
$loungeUpdate = array_intersect_key($lounge, array_fill_keys(array( | |
'CategoryID', 'ParentCategoryID', 'UrlCode' | |
), true)); | |
$id = $categoryModel->save($loungeUpdate); | |
// If the move was successful, consider the lounge open | |
if ($id) { | |
saveToConfig('Plugins.Winter.LoungeOpen', date('Y')); | |
$loungeOpen = true; | |
} | |
} | |
break; | |
case null: | |
case false: | |
if ($loungeOpen) { | |
// Close it | |
$categoryModel = new CategoryModel(); | |
// Move lounge to storage | |
$lounge['ParentCategoryID'] = $this->loungeStorageID; | |
$loungeUpdate = array_intersect_key($lounge, array_fill_keys(array( | |
'CategoryID', 'ParentCategoryID', 'UrlCode' | |
), true)); | |
$id = $categoryModel->save($loungeUpdate); | |
// If the move was successful, consider the lounge closed | |
if ($id) { | |
saveToConfig('Plugins.Winter.LoungeOpen', false); | |
$loungeOpen = false; | |
} | |
} | |
break; | |
} | |
} | |
// Lounge not enabled? No custom permissions needed | |
if (!$this->loungeEnabled || !$loungeOpen) return; | |
// Turn on the lounge for regulars | |
$preserveCategories = array(5,86,87,39,77,16,97,79,82,21,88,90,23,3); | |
// Let all people see the lounge | |
$permissionSections = array( | |
'Vanilla.Discussions.View', | |
); | |
// Let logged-in people add comments and threads as well | |
if (Gdn::session()->isValid()) { | |
$permissionSections = array_merge($permissionSections, array( | |
'Vanilla.Discussions.Add', | |
'Vanilla.Comments.Add' | |
)); | |
} | |
// Do we strip out non-preserved categories for this person? | |
$strip = !Gdn::session()->checkPermission('Garden.Moderation.Manage'); | |
foreach ($permissionSections as $permission) { | |
$sectionPermissions = val($permission, Gdn::session()->getPermissions()); | |
if ($strip) | |
$sectionPermissions = array_intersect($sectionPermissions, $preserveCategories); | |
$sectionPermissions[] = $this->loungeID; | |
Gdn::session()->setPermission($permission, $sectionPermissions); | |
} | |
// Ensure view access to these categories | |
if ($strip) { | |
$view = array(5); | |
$viewPermissions = val('Vanilla.Discussions.View', Gdn::session()->getPermissions()); | |
foreach ($view as $viewcat) { | |
if (!in_array($viewcat, $viewPermissions)) | |
$viewPermissions[] = $viewcat; | |
} | |
Gdn::session()->setPermission('Vanilla.Discussions.View', $viewPermissions); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment