'Event Calendar', 'Description' => 'Adds an event date field to new discussions so that they can be treated as events', 'Version' => '0.5.0', 'RequiredApplications' => ['Vanilla' => '>=2.3'], 'SettingsUrl' => '/settings/eventcalendar', 'RequiredPlugins' => false, 'RequiredTheme' => false, 'MobileFriendly' => true, 'HasLocale' => true, 'RegisterPermissions' => false, 'RegisterPermissions' => [ 'Plugins.EventCalendar.Add', 'Plugins.EventCalendar.Manage', 'Plugins.EventCalendar.Notify', 'Plugins.EventCalendar.View' ], 'Author' => 'Robin Jurinka', 'AuthorUrl' => 'http://vanillaforums.org/profile/r_j', 'License' => 'MIT' ]; /** * Plugin that adds a date field to new discussions. * * New discussions could be entered with an "event date". Plugin creates * additional views where such discussions are shown on a calendar. * * @package EventCalendar * @author Robin Jurinka * @license MIT */ class EventCalendarPlugin extends Gdn_Plugin { /** * Prepare needed system changes. * * Initiate db structure change and create a custom * route for calendar view. * * @return void. * @package EventCalendar * @since 0.1 */ public function setup() { // Change db structure. $this->structure(); // Set initial config settings. touchConfig('EventCalendar.CustomRoute', 'eventcalendar'); // Set custom route to plugin page. $router = Gdn::router(); $pluginPage = 'vanilla/eventcalendar$1'; $newRoute = '^'.c('EventCalendar.CustomRoute', 'eventcalendar').'(/.*)?$'; if (!$router->matchRoute($newRoute)) { $router->setRoute($newRoute, $pluginPage, 'Internal'); } } /** * Add event date field to Discussion table. * * @return void. * @package EventCalendar * @since 0.1 */ public function structure() { Gdn::structure()->table('Discussion') ->column('EventCalendarDate', 'date', true) ->set(false, false); } /** * Reset custom route. * * @return void. * @package EventCalendar * @since 0.1 */ public function onDisable() { Gdn::router()->deleteRoute('^'.c('EventCalendar.CustomRoute', 'eventcalendar').'(/.*)?$'); } /** * Settings screen to choose categories and set custom url. * * @param object $sender SettingsController. * @return void. * @package EventCalendar * @since 0.3 */ public function settingsController_eventCalendar_create($sender) { $sender->permission('Garden.Settings.Manage'); $sender->title(t('EventCalendar.SettingsTitle')); $sender->addSideMenu('dashboard/settings/plugins'); $sender->description('Description', t('EventCalendar.SettingsDescription')); // IF we are posting back to this if ($sender->Form->authenticatedPostBack()) { $formPostValues = $sender->Form->formValues(); // Old code that had it saving the CategoriesIS as a serialized value // Save serialized CategoryIDs to the config.php saveToConfig( 'EventCalendar.CategoryIDs', serialize($formPostValues['CategoryIDs']) ); // New code added to remove the serialization of the CategorieIDs // Save CategoryIDs array to the config.php saveToConfig( 'EventCalendar.CategoryIDs', $formPostValues['CategoryIDs'] ); // Set new route if needed $newUrl = $formPostValues['CustomRoute']; $oldUrl = c('CustomRoute'); if ($oldUrl != $newUrl) { // Delete old custom route. $router = Gdn::router(); $router->deleteRoute('^'.$oldUrl.'(/.*)?$'); // Set new custom route. $pluginPage = 'vanilla/eventcalendar$1'; $newRoute = '^'.$newUrl.'(/.*)?$'; if (!$router->matchRoute($newRoute)) { $router->setRoute( $newRoute, 'vanilla/eventcalendar$1', 'Internal' ); } } // If we are not posting back to this } else { $sender->Form->setValue( 'CategoryIDs', c('EventCalendar.CategoryIDs') ); $sender->Form->setValue( 'CustomRoute', c('EventCalendar.CustomRoute') ); } // End of if // Get a list of our catagories in $categories = CategoryModel::categories(); unset($categories[-1]); $sender->setData('Schema', [ 'CategoryIDs' => [ 'Control' => 'CheckBoxList', 'LabelCode' => 'Categories', 'Items' => $categories, 'Description' => 'Please choose categories in which the creation of events should be allowed', 'Options' => ['ValueField' => 'CategoryID', 'TextField' => 'Name'] ], 'CustomRoute' => [ 'Control' => 'TextBox', 'LabelCode' => 'Custom Url', 'Description' => 'The event calendar will be accessible under that url' ] ]); $sender->render('settings', '', 'plugins/EventCalendar'); } /** * Add menu entry for calendar to custom menu. * * @param object $sender GardenController. * @return void. * @package EventCalendar * @since 0.1 */ public function base_render_before($sender) { if ($sender->Menu && checkPermission('Plugins.EventCalendar.View')) { $sender->Menu->addLink( t('EventCalendar'), t('Event Calendar'), c('EventCalendar.CustomRoute', 'eventcalendar') ); } } /** * Add input fields to New Discussion view. * * Check for custom permission. Allows creation of events in current * and next year. * Datefield is prefilled with current date by eventcalendar.js. * * @param object $sender PostController. * @return void. * @package EventCalendar * @since 0.1 */ public function postController_beforeBodyInput_handler($sender) { if (!checkPermission(['Plugins.EventCalendar.Add', 'Plugins.EventCalendar.Manage'])) { return; } $sender->addJsFile('eventcalendar.js', 'plugins/EventCalendar'); $sender->addDefinition( 'EventCalendarCategoryIDs', json_encode(c('EventCalendar.CategoryIDs')) ); // Old Original Code does not seem to return the CategoryID its always blank // $categoryID = $sender->Discussion->CategoryID; // Fixed Code $categoryID = valr('Category.CategoryID', Gdn::controller()); // initially don't hide elements in allowed categories $cssClass = 'P EventCalendarInput'; if (!in_array($categoryID, c('EventCalendar.CategoryIDs'))) { $cssClass .= ' Hidden'; } $year = date('Y'); $yearRange = $year.'-'.($year + 1); $fields = explode(',', t('EventCalendar.DateOrder', 'month,day,year')); echo '