/** * Loads default page view. * * @param string $PageUrlCode ; Unique page URL stub identifier. */ public function Index($PageUrlCode = '') { $this->Page = $this->PageModel->GetByUrlCode($PageUrlCode); // Require the custom view permission if it exists. // Otherwise, the page is public by default. $ViewPermissionName = 'BasicPages.' . $PageUrlCode . '.View'; if (array_key_exists($ViewPermissionName, Gdn::PermissionModel()->PermissionColumns())) { $this->Permission($ViewPermissionName); } // If page doesn't exist. if ($this->Page == null) { throw new Exception(sprintf(T('%s Not Found'), T('Page')), 404); return null; } $this->SetData('Page', $this->Page, false); // Add body CSS class. $this->CssClass = 'Page-' . $this->Page->UrlCode; if (IsMobile()) { $this->CssClass .= ' PageMobile'; } // Set the canonical URL to have the proper page link. $this->CanonicalUrl(PageModel::PageUrl($this->Page)); // Add modules $this->AddModule('GuestModule'); $this->AddModule('SignedInModule'); // Add CSS files $this->AddCssFile('page.css'); $this->AddModule('NewDiscussionModule'); $this->AddModule('DiscussionFilterModule'); $this->AddModule('BookmarkedModule'); $this->AddModule('DiscussionsModule'); $this->AddModule('RecentActivityModule'); // Setup head. if (!$this->Data('Title')) { $Title = C('Garden.HomepageTitle'); $DefaultControllerDestination = Gdn::Router()->GetDestination('DefaultController'); if ($Title != '' && strpos($DefaultControllerDestination, 'page/' . $this->Page->UrlCode) !== false) { // If the page is set as DefaultController. $this->Title($Title, ''); // Add description meta tag. $this->Description(C('Garden.Description', null)); } else { // If the page is NOT the DefaultController. $this->Title($this->Page->Name); // Add description meta tag. $this->Description(SliceParagraph(Gdn_Format::PlainText($this->Page->Body, $this->Page->Format), 160)); } } $this->Render(); }
/** * Add pages in the config value array by ID to header site menu. */ public function Base_Render_Before($Sender) { if (isset($Sender->Menu) && $Sender->MasterView != 'admin') { $PageModel = new PageModel(); $Pages = $PageModel->GetAllSiteMenuLink()->Result(); foreach ($Pages as $Page) { // Check permission. $Permission = false; if ($Page->ViewPermission == '1') { $Permission = 'BasicPages.' . $Page->UrlCode . '.View'; } // Add link to the menu. $Sender->Menu->AddLink('BasicPages-' . $Page->PageID, $Page->Name, PageModel::PageUrl($Page), $Permission, array('class' => 'Page-' . $Page->UrlCode)); } } }
foreach ($Pages as $Page) { ?> <li id="list_<?php echo $Page->PageID; ?> " class="NoNesting"> <div> <table> <tbody> <tr id="<?php echo 'Page_' . $Page->PageID; ?> "> <td><?php echo '<strong>' . $Page->Name . '</strong>'; $PageUrl = PageModel::PageUrl($Page); echo '<br />' . Anchor($PageUrl, $PageUrl); ?> </td> <td class="Buttons"><?php echo Anchor(T('BasicPages.Settings.AllPages.PageEdit', 'Edit'), '/pagessettings/editpage/' . $Page->PageID, array('class' => 'SmallButton Edit')); echo ' '; echo Anchor(T('BasicPages.Settings.AllPages.PageDelete', 'Delete'), '/pagessettings/deletepage/' . $Page->PageID, array('class' => 'SmallButton Delete Popup')); ?> </td> </tr> </tbody> </table> </div> </li> <?php
/** * Loads view for creating a new page. * * @param object $Page ; Not NULL when editing a valid page. */ public function NewPage($Page = null) { // Check permission $this->Permission('Garden.Settings.Manage'); // Add JavaScript files. $this->AddJsFile('jquery-ui.js'); $this->AddJsFile('jquery.autogrow.js'); $this->AddJsFile('pagessettings-newpage.js'); // Prep Model $this->Form->SetModel($this->PageModel); // Set format data. $this->SetData('Formats', $this->GetFormats()); $this->AddDefinition('DefaultFormat', C('BasicPages.DefaultFormatter', C('Garden.InputFormatter', 'Html'))); // If form wasn't submitted. if ($this->Form->IsPostBack() == false) { // Prep form with current data for editing if (isset($Page)) { $this->SetData('Page', $Page); $this->Form->SetData($Page); // Send CurrentFormat value to the page to be used for // setting the selected value of the formats drop-down. $this->AddDefinition('CurrentFormat', $Page->Format); $this->Form->AddHidden('UrlCodeIsDefined', '1'); if (Gdn::Router()->MatchRoute($Page->UrlCode . $this->PageModel->RouteExpressionSuffix)) { $this->Form->SetValue('HidePageFromURL', '1'); $this->Form->SetFormValue('HidePageFromURL', '1'); } } else { $this->Form->AddHidden('UrlCodeIsDefined', '0'); } } else { // Form was submitted. $FormValues = $this->Form->FormValues(); if (isset($Page)) { $FormValues['PageID'] = $Page->PageID; $this->Form->SetFormValue('PageID', $Page->PageID); } // Validate form values. if ($FormValues['Name'] == '') { $this->Form->AddError(T('BasicPages.Settings.NewPage.ErrorName', 'Page title is required.'), 'Name'); } if ($FormValues['Body'] == '') { $this->Form->AddError(T('BasicPages.Settings.NewPage.ErrorBody', 'Page body is required.'), 'Body'); } // Format Name $FormValues['Name'] = Gdn_Format::Text($FormValues['Name']); // Validate UrlCode. if ($FormValues['UrlCode'] == '') { $FormValues['UrlCode'] = $FormValues['Name']; } // Format the UrlCode. $FormValues['UrlCode'] = Gdn_Format::Url($FormValues['UrlCode']); $this->Form->SetFormValue('UrlCode', $FormValues['UrlCode']); $SQL = Gdn::Database()->SQL(); // Make sure that the UrlCode is unique among pages. $SQL->Select('p.PageID')->From('Page p')->Where('p.UrlCode', $FormValues['UrlCode']); if (isset($Page)) { $SQL->Where('p.PageID <>', $Page->PageID); } $UrlCodeExists = isset($SQL->Get()->FirstRow()->PageID); if ($UrlCodeExists) { $this->Form->AddError(T('BasicPages.Settings.NewPage.ErrorUrlCode', 'The specified URL code is already in use by another page.'), 'UrlCode'); } // Make sure sort is set if new page. if (!$Page) { $LastSort = $this->PageModel->GetLastSort(); $FormValues['Sort'] = $LastSort + 1; } // Send CurrentFormat value to the page to be used for // setting the selected value of the formats drop-down. $this->AddDefinition('CurrentFormat', $FormValues['Format']); // Explicitly cast these values to an integer data type in case // they are equal to '' to be valid with MySQL strict mode, etc. $FormValues['SiteMenuLink'] = (int) $FormValues['SiteMenuLink']; // If all form values are validated. if ($this->Form->ErrorCount() == 0) { $PageID = $this->PageModel->Save($FormValues); $ValidationResults = $this->PageModel->ValidationResults(); $this->Form->SetValidationResults($ValidationResults); // Create and clean up routes for UrlCode. if ($Page->UrlCode != $FormValues['UrlCode']) { if (Gdn::Router()->MatchRoute($Page->UrlCode . $this->PageModel->RouteExpressionSuffix)) { Gdn::Router()->DeleteRoute($Page->UrlCode . $this->PageModel->RouteExpressionSuffix); } } if ($FormValues['HidePageFromURL'] == '1' && !Gdn::Router()->MatchRoute($FormValues['UrlCode'] . $this->PageModel->RouteExpressionSuffix)) { Gdn::Router()->SetRoute($FormValues['UrlCode'] . $this->PageModel->RouteExpressionSuffix, 'page/' . $FormValues['UrlCode'] . $this->PageModel->RouteTargetSuffix, 'Internal'); } elseif ($FormValues['HidePageFromURL'] == '0' && Gdn::Router()->MatchRoute($FormValues['UrlCode'] . $this->PageModel->RouteExpressionSuffix)) { Gdn::Router()->DeleteRoute($FormValues['UrlCode'] . $this->PageModel->RouteExpressionSuffix); } // Set up a custom view permission. // The UrlCode must be unique and validated before this code. $ViewPermissionName = 'BasicPages.' . $FormValues['UrlCode'] . '.View'; $PermissionTable = Gdn::Database()->Structure()->Table('Permission'); $PermissionModel = Gdn::PermissionModel(); // If a page is being edited, then check if UrlCode was changed by the user // and rename the custom view permission column for the page if it exists accordingly, // to keep the permission table clean. if (isset($Page) && $Page->UrlCode != $FormValues['UrlCode']) { $OldViewPermissionName = 'BasicPages.' . $Page->UrlCode . '.View'; $PermissionModel->Undefine($OldViewPermissionName); // The column must be dropped for now, because the RenameColumn method // has a bug, which has been reported. //$PermissionTable->RenameColumn($OldViewPermissionName, $ViewPermissionName); } $ViewPermissionExists = $PermissionTable->ColumnExists($ViewPermissionName); // Check if the user checked the setting to enable the custom view permission. if ((bool) $FormValues['ViewPermission']) { // Check if the permission does not exist. if (!$ViewPermissionExists) { // Create the custom view permission. $PermissionModel->Define($ViewPermissionName); // Set initial permission for the Administrator role. $PermissionModel->Save(array('Role' => 'Administrator', $ViewPermissionName => 1)); } } elseif ($ViewPermissionExists) { // Delete the custom view permission if it exists. $PermissionTable->DropColumn($ViewPermissionName); } if ($this->DeliveryType() == DELIVERY_TYPE_ALL) { if (strtolower($this->RequestMethod) == 'newpage') { Redirect('pagessettings/allpages#Page_' . $PageID); } $this->InformMessage('<span class="InformSprite Check"></span>' . T('BasicPages.Settings.NewPage.Saved', 'The page has been saved successfully. <br />Go back to ') . Anchor(T('BasicPages.Settings.AllPages', 'all pages'), 'pagessettings/allpages') . T('BasicPages.Settings.NewPage.Saved2', ' or ') . Anchor(T('BasicPages.Settings.NewPage.ViewPage', 'view the page'), PageModel::PageUrl($FormValues['UrlCode'])) . '.', 'Dismissable AutoDismiss HasSprite'); } } } // Setup head. if ($this->Data('Title')) { $this->AddSideMenu(); $this->Title($this->Data('Title')); } else { $this->AddSideMenu('pagessettings/newpage'); $this->Title(T('BasicPages.Settings.NewPage', 'New Page')); } $this->Render(); }