array(), 'css' => array(), 'themedcss' => array(), ); /** * @param Member $member * * @return boolean */ function canView($member = null) { if(!$member && $member !== FALSE) { $member = Member::currentUser(); } // cms menus only for logged-in members if(!$member) return false; // alternative extended checks if($this->hasMethod('alternateAccessCheck')) { $alternateAllowed = $this->alternateAccessCheck(); if($alternateAllowed === FALSE) return false; } // Default security check for LeftAndMain sub-class permissions if(!Permission::checkMember($member, "CMS_ACCESS_$this->class") && !Permission::checkMember($member, "CMS_ACCESS_LeftAndMain")) { return false; } return true; } /** * @uses LeftAndMainExtension->init() * @uses LeftAndMainExtension->accessedCMS() * @uses CMSMenu */ function init() { parent::init(); SSViewer::setOption('rewriteHashlinks', false); // set language $member = Member::currentUser(); if(!empty($member->Locale)) i18n::set_locale($member->Locale); if(!empty($member->DateFormat)) i18n::set_date_format($member->DateFormat); if(!empty($member->TimeFormat)) i18n::set_time_format($member->TimeFormat); // can't be done in cms/_config.php as locale is not set yet CMSMenu::add_link( 'Help', _t('LeftAndMain.HELP', 'Help', PR_HIGH, 'Menu title'), self::$help_link ); // Allow customisation of the access check by a extension // Also all the canView() check to execute Director::redirect() if(!$this->canView() && !$this->response->isFinished()) { // When access /admin/, we should try a redirect to another part of the admin rather than be locked out $menu = $this->MainMenu(); foreach($menu as $candidate) { if( $candidate->Link && $candidate->Link != $this->Link() && $candidate->MenuItem->controller && singleton($candidate->MenuItem->controller)->canView() ) { return Director::redirect($candidate->Link); } } if(Member::currentUser()) { Session::set("BackURL", null); } // if no alternate menu items have matched, return a permission error $messageSet = array( 'default' => _t('LeftAndMain.PERMDEFAULT',"Please choose an authentication method and enter your credentials to access the CMS."), 'alreadyLoggedIn' => _t('LeftAndMain.PERMALREADY',"I'm sorry, but you can't access that part of the CMS. If you want to log in as someone else, do so below"), 'logInAgain' => _t('LeftAndMain.PERMAGAIN',"You have been logged out of the CMS. If you would like to log in again, enter a username and password below."), ); return Security::permissionFailure($this, $messageSet); } // Don't continue if there's already been a redirection request. if(Director::redirected_to()) return; // Audit logging hook if(empty($_REQUEST['executeForm']) && !$this->isAjax()) $this->extend('accessedCMS'); // Requirements Requirements::combine_files( 'lib.js', array( THIRDPARTY_DIR . '/prototype/prototype.js', THIRDPARTY_DIR . '/behaviour/behaviour.js', THIRDPARTY_DIR . '/jquery/jquery.js', SAPPHIRE_DIR . '/javascript/jquery_improvements.js', THIRDPARTY_DIR . '/jquery-livequery/jquery.livequery.js', SAPPHIRE_DIR . '/javascript/jquery-ondemand/jquery.ondemand.js', THIRDPARTY_DIR . '/jquery-ui/jquery-ui.js', THIRDPARTY_DIR . '/json-js/json2.js', THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js', THIRDPARTY_DIR . '/jquery-cookie/jquery.cookie.js', THIRDPARTY_DIR . '/jquery-query/jquery.query.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/jquery-notice/jquery.notice.js', THIRDPARTY_DIR . '/jquery-metadata/jquery.metadata.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/jsizes/lib/jquery.sizes.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/jlayout/lib/jlayout.border.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/jlayout/lib/jquery.jlayout.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/history-js/scripts/uncompressed/history.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/history-js/scripts/uncompressed/history.adapter.jquery.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/history-js/scripts/uncompressed/history.html4.js', THIRDPARTY_DIR . '/jstree/jquery.jstree.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/chosen/chosen/chosen.jquery.js', SAPPHIRE_ADMIN_DIR . '/thirdparty/jquery-hoverIntent/jquery.hoverIntent.js', SAPPHIRE_ADMIN_DIR . '/javascript/jquery-changetracker/lib/jquery.changetracker.js', SAPPHIRE_DIR . '/javascript/TreeDropdownField.js', SAPPHIRE_DIR ."/thirdparty/jquery-form/jquery.form.js", SAPPHIRE_DIR . '/javascript/DateField.js', SAPPHIRE_DIR . '/javascript/HtmlEditorField.js', SAPPHIRE_DIR . '/javascript/TabSet.js', SAPPHIRE_DIR . '/javascript/Validator.js', SAPPHIRE_DIR . '/javascript/i18n.js', SAPPHIRE_ADMIN_DIR . '/javascript/ssui.core.js', ) ); Requirements::combine_files( 'leftandmain.js', array( SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.js', SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Panel.js', SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Tree.js', SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Ping.js', SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Content.js', SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.EditForm.js', SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Menu.js', SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.AddForm.js', SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.Preview.js', SAPPHIRE_ADMIN_DIR . '/javascript/LeftAndMain.BatchActions.js', ) ); Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang'); Requirements::add_i18n_javascript(SAPPHIRE_ADMIN_DIR . '/javascript/lang'); Requirements::combine_files( 'lib.css', array( // @todo Load separately so the CSS files can be inlined THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery-ui.css', SAPPHIRE_ADMIN_DIR .'/thirdparty/chosen/chosen/chosen.css', THIRDPARTY_DIR . '/jstree/themes/apple/style.css', SAPPHIRE_DIR . '/css/TreeDropdownField.css' ) ); Requirements::css(SAPPHIRE_ADMIN_DIR . '/css/screen.css'); // Set the members html editor config HtmlEditorConfig::set_active(Member::currentUser()->getHtmlEditorConfigForCMS()); // Set default values in the config if missing. These things can't be defined in the config // file because insufficient information exists when that is being processed $htmlEditorConfig = HtmlEditorConfig::get_active(); $htmlEditorConfig->setOption('language', i18n::get_tinymce_lang()); if(!$htmlEditorConfig->getOption('content_css')) { $cssFiles = array(); $cssFiles[] = 'sapphire/admin/css/editor.css'; // Use theme from the site config if(class_exists('SiteConfig') && ($config = SiteConfig::current_site_config()) && $config->Theme) { $theme = $config->Theme; } elseif(SSViewer::current_theme()) { $theme = SSViewer::current_theme(); } else { $theme = false; } if($theme) $cssFiles[] = THEMES_DIR . "/{$theme}/css/editor.css"; else if(project()) $cssFiles[] = project() . '/css/editor.css'; // Remove files that don't exist foreach($cssFiles as $k => $cssFile) { if(!file_exists(BASE_PATH . '/' . $cssFile)) unset($cssFiles[$k]); } $htmlEditorConfig->setOption('content_css', implode(',', $cssFiles)); } foreach (self::$extra_requirements['javascript'] as $file) { Requirements::javascript($file[0]); } foreach (self::$extra_requirements['css'] as $file) { Requirements::css($file[0], $file[1]); } foreach (self::$extra_requirements['themedcss'] as $file) { Requirements::themedCSS($file[0], $file[1]); } $dummy = null; $this->extend('init', $dummy); // The user's theme shouldn't affect the CMS, if, for example, they have replaced // TableListField.ss or Form.ss. SSViewer::set_theme(null); } function handleRequest($request, DataModel $model) { $title = $this->Title(); $response = parent::handleRequest($request, $model); $response->addHeader('X-Controller', $this->class); $response->addHeader('X-Title', $title); return $response; } function index($request) { return ($this->isAjax()) ? $this->show($request) : $this->getViewer('index')->process($this); } /** * If this is set to true, the "switchView" context in the * template is shown, with links to the staging and publish site. * * @return boolean */ function ShowSwitchView() { return false; } //------------------------------------------------------------------------------------------// // Main controllers /** * You should implement a Link() function in your subclass of LeftAndMain, * to point to the URL of that particular controller. * * @return string */ public function Link($action = null) { // Handle missing url_segments if(!$this->stat('url_segment', true)) self::$url_segment = $this->class; return Controller::join_links( $this->stat('url_base', true), $this->stat('url_segment', true), '/', // trailing slash needed if $action is null! "$action" ); } /** * Returns the menu title for the given LeftAndMain subclass. * Implemented static so that we can get this value without instantiating an object. * Menu title is *not* internationalised. */ static function menu_title_for_class($class) { $title = eval("return $class::\$menu_title;"); if(!$title) $title = preg_replace('/Admin$/', '', $class); return $title; } public function show($request) { // TODO Necessary for TableListField URLs to work properly if($request->param('ID')) $this->setCurrentPageID($request->param('ID')); if($this->isAjax()) { if($request->getVar('cms-view-form')) { $form = $this->getEditForm(); $content = $form->forTemplate(); } else { // Rendering is handled by template, which will call EditForm() eventually $content = $this->renderWith($this->getTemplatesWithSuffix('_Content')); } } else { $content = $this->renderWith($this->getViewer('show')); } return $content; } //------------------------------------------------------------------------------------------// // Main UI components /** * Returns the main menu of the CMS. This is also used by init() * to work out which sections the user has access to. * * @return SS_List */ public function MainMenu() { // Don't accidentally return a menu if you're not logged in - it's used to determine access. if(!Member::currentUser()) return new ArrayList(); // Encode into DO set $menu = new ArrayList(); $menuItems = CMSMenu::get_viewable_menu_items(); if($menuItems) { foreach($menuItems as $code => $menuItem) { // alternate permission checks (in addition to LeftAndMain->canView()) if( isset($menuItem->controller) && $this->hasMethod('alternateMenuDisplayCheck') && !$this->alternateMenuDisplayCheck($menuItem->controller) ) { continue; } $linkingmode = "link"; if($menuItem->controller && get_class($this) == $menuItem->controller) { $linkingmode = "current"; } else if(strpos($this->Link(), $menuItem->url) !== false) { if($this->Link() == $menuItem->url) { $linkingmode = "current"; // default menu is the one with a blank {@link url_segment} } else if(singleton($menuItem->controller)->stat('url_segment') == '') { if($this->Link() == $this->stat('url_base').'/') { $linkingmode = "current"; } } else { $linkingmode = "current"; } } // already set in CMSMenu::populate_menu(), but from a static pre-controller // context, so doesn't respect the current user locale in _t() calls - as a workaround, // we simply call LeftAndMain::menu_title_for_class() again // if we're dealing with a controller if($menuItem->controller) { $defaultTitle = LeftAndMain::menu_title_for_class($menuItem->controller); $title = _t("{$menuItem->controller}.MENUTITLE", $defaultTitle); } else { $title = $menuItem->title; } $menu->push(new ArrayData(array( "MenuItem" => $menuItem, "Title" => Convert::raw2xml($title), "Code" => DBField::create('Text', $code), "Link" => $menuItem->url, "LinkingMode" => $linkingmode ))); } } // if no current item is found, assume that first item is shown //if(!isset($foundCurrent)) return $menu; } public function Menu() { return $this->renderWith($this->getTemplatesWithSuffix('_Menu')); } /** * Return a list of appropriate templates for this class, with the given suffix */ protected function getTemplatesWithSuffix($suffix) { $classes = array_reverse(ClassInfo::ancestry($this->class)); foreach($classes as $class) { $templates[] = $class . $suffix; if($class == 'LeftAndMain') break; } return $templates; } public function Content() { return $this->renderWith($this->getTemplatesWithSuffix('_Content')); } public function getRecord($id) { $className = $this->stat('tree_class'); if($className && $id instanceof $className) { return $id; } else if($id == 'root') { return singleton($className); } else if(is_numeric($id)) { return DataObject::get_by_id($className, $id); } else { return false; } } /** * @return String HTML */ public function SiteTreeAsUL() { return $this->getSiteTreeFor($this->stat('tree_class')); } /** * Get a site tree HTML listing which displays the nodes under the given criteria. * * @param $className The class of the root object * @param $rootID The ID of the root object. If this is null then a complete tree will be * shown * @param $childrenMethod The method to call to get the children of the tree. For example, * Children, AllChildrenIncludingDeleted, or AllHistoricalChildren * @return String Nested unordered list with links to each page */ function getSiteTreeFor($className, $rootID = null, $childrenMethod = null, $numChildrenMethod = null, $filterFunction = null, $minNodeCount = 30) { // Default childrenMethod and numChildrenMethod if (!$childrenMethod) $childrenMethod = 'AllChildrenIncludingDeleted'; if (!$numChildrenMethod) $numChildrenMethod = 'numChildren'; // Get the tree root $record = ($rootID) ? $this->getRecord($rootID) : null; $obj = $record ? $record : singleton($className); // Mark the nodes of the tree to return if ($filterFunction) $obj->setMarkingFilterFunction($filterFunction); $obj->markPartialTree($minNodeCount, $this, $childrenMethod, $numChildrenMethod); // Ensure current page is exposed if($p = $this->currentPage()) $obj->markToExpose($p); // NOTE: SiteTree/CMSMain coupling :-( if(class_exists('SiteTree')) { SiteTree::prepopuplate_permission_cache('CanEditType', $obj->markedNodeIDs(), 'SiteTree::can_edit_multiple'); } // getChildrenAsUL is a flexible and complex way of traversing the tree $titleEval = ' "
%s %s. %s
', // _t('LeftAndMain_right.ss.WELCOMETO','Welcome to'), // $this->getApplicationName(), // _t('CHOOSEPAGE','Please choose an item from the left.') // ) // ) ), new FieldList() ); $form->unsetValidator(); $form->addExtraClass('cms-edit-form'); $form->addExtraClass('root-form'); $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); return $form; } /** * @return Form */ function AddForm() { $class = $this->stat('tree_class'); $typeMap = array($class => singleton($class)->i18n_singular_name()); $form = new Form( $this, 'AddForm', new FieldList( new HiddenField('ParentID') ), new FieldList( $addAction = new FormAction('doAdd', _t('AssetAdmin_left.ss.GO','Go')) ) ); $addAction->addExtraClass('ss-ui-action-constructive'); $form->addExtraClass('add-form'); return $form; } /** * Add a new group and return its details suitable for ajax. */ public function doAdd($data, $form) { $class = $this->stat('tree_class'); // check create permissions if(!singleton($class)->canCreate()) return Security::permissionFailure($this); // check addchildren permissions if( singleton($class)->hasDatabaseField('Hierarchy') && isset($data['ParentID']) && is_numeric($data['ParentID']) ) { $parentRecord = DataObject::get_by_id($class, $data['ParentID']); if( $parentRecord->hasMethod('canAddChildren') && !$parentRecord->canAddChildren() ) return Security::permissionFailure($this); } $record = Object::create($class); $form->saveInto($record); $record->write(); // Used in TinyMCE inline folder creation if(isset($data['returnID'])) { return $record->ID; } else if($this->isAjax()) { $form = $this->getEditForm($record->ID); return $form->forTemplate(); } else { return $this->redirect(Controller::join_links($this->Link('show'), $record->ID)); } } /** * Batch Actions Handler */ function batchactions() { return new CMSBatchActionHandler($this, 'batchactions', $this->stat('tree_class')); } /** * @return Form */ function BatchActionsForm() { $actions = $this->batchactions()->batchActionList(); $actionsMap = array('-1' => _t('LeftAndMain.DropdownBatchActionsDefault', 'Actions')); foreach($actions as $action) $actionsMap[$action->Link] = $action->Title; $form = new Form( $this, 'BatchActionsForm', new FieldList( new HiddenField('csvIDs'), new DropdownField( 'Action', false, $actionsMap ) ), new FieldList( // TODO i18n new FormAction('submit', "Go") ) ); $form->addExtraClass('cms-batch-actions nostyle'); $form->unsetValidator(); return $form; } public function myprofile() { $form = $this->Member_ProfileForm(); return $this->customise(array( 'Content' => ' ', 'Form' => $form ))->renderWith('BlankPage'); } public function Member_ProfileForm() { return new Member_ProfileForm($this, 'Member_ProfileForm', Member::currentUser()); } public function printable() { $form = $this->getEditForm($this->currentPageID()); if(!$form) return false; $form->transform(new PrintableTransformation()); $form->setActions(null); Requirements::clear(); Requirements::css(SAPPHIRE_ADMIN_DIR . '/css/LeftAndMain_printable.css'); return array( "PrintForm" => $form ); } /** * Used for preview controls, mainly links which switch between different states of the page. * * @return ArrayData */ function getSilverStripeNavigator() { $page = $this->currentPage(); if($page) { $navigator = new SilverStripeNavigator($page); return $navigator->renderWith($this->getTemplatesWithSuffix('_SilverStripeNavigator')); } else { return false; } } /** * Identifier for the currently shown record, * in most cases a database ID. Inspects the following * sources (in this order): * - GET/POST parameter named 'ID' * - URL parameter named 'ID' * - Session value namespaced by classname, e.g. "CMSMain.currentPage" * * @return int */ public function currentPageID() { if($this->request->requestVar('ID')) { return $this->request->requestVar('ID'); } elseif ($this->request->param('ID') && is_numeric($this->request->param('ID'))) { return $this->request->param('ID'); } elseif(Session::get("{$this->class}.currentPage")) { return Session::get("{$this->class}.currentPage"); } else { return null; } } /** * Forces the current page to be set in session, * which can be retrieved later through {@link currentPageID()}. * Keep in mind that setting an ID through GET/POST or * as a URL parameter will overrule this value. * * @param int $id */ public function setCurrentPageID($id) { Session::set("{$this->class}.currentPage", $id); } /** * Uses {@link getRecord()} and {@link currentPageID()} * to get the currently selected record. * * @return DataObject */ public function currentPage() { return $this->getRecord($this->currentPageID()); } /** * Compares a given record to the currently selected one (if any). * Used for marking the current tree node. * * @return boolean */ public function isCurrentPage(DataObject $record) { return ($record->ID == $this->currentPageID()); } /** * URL to a previewable record which is shown through this controller. * The controller might not have any previewable content, in which case * this method returns FALSE. * * @return String|boolean */ public function PreviewLink() { $record = $this->getRecord($this->currentPageID()); $baseLink = ($record && $record instanceof Page) ? $record->Link('?stage=Stage') : Director::absoluteBaseURL(); return $baseLink; } /** * Return the version number of this application. * Uses the subversion path information in