function Dates() { Requirements::themedCSS('archivewidget'); $results = new DataObjectSet(); $container = BlogTree::current(); $ids = $container->BlogHolderIDs(); $stage = Versioned::current_stage(); $suffix = !$stage || $stage == 'Stage' ? "" : "_{$stage}"; $monthclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%m') : 'MONTH("Date")'; $yearclause = method_exists(DB::getConn(), 'formattedDatetimeClause') ? DB::getConn()->formattedDatetimeClause('"Date"', '%Y') : 'YEAR("Date")'; $sqlResults = DB::query("\n\t\t\tSELECT DISTINCT CAST({$monthclause} AS " . DB::getConn()->dbDataType('unsigned integer') . ") AS \"Month\", {$yearclause} AS \"Year\"\n\t\t\tFROM \"SiteTree{$suffix}\" INNER JOIN \"BlogEntry{$suffix}\" ON \"SiteTree{$suffix}\".\"ID\" = \"BlogEntry{$suffix}\".\"ID\"\n\t\t\tWHERE \"ParentID\" IN (" . implode(', ', $ids) . ")\n\t\t\tORDER BY \"Year\" DESC, \"Month\" DESC;"); if ($this->ShowLastYears == 0) { $cutOffYear = 0; } else { $cutOffYear = (int) date("Y") - $this->ShowLastYears; } $years = array(); if (Director::get_current_page()->ClassName == 'BlogHolder') { $urlParams = Director::urlParams(); $yearParam = $urlParams['ID']; $monthParam = $urlParams['OtherID']; } else { $date = new DateTime(Director::get_current_page()->Date); $yearParam = $date->format("Y"); $monthParam = $date->format("m"); } if ($sqlResults) { foreach ($sqlResults as $sqlResult) { $isMonthDisplay = true; $year = $sqlResult['Year'] ? (int) $sqlResult['Year'] : date('Y'); $isMonthDisplay = $year > $cutOffYear; // $dateFormat = 'Month'; else $dateFormat = 'Year'; $monthVal = isset($sqlResult['Month']) ? (int) $sqlResult['Month'] : 1; $month = $isMonthDisplay ? $monthVal : 1; $date = DBField::create('Date', array('Day' => 1, 'Month' => $month, 'Year' => $year)); if ($isMonthDisplay) { $link = $container->Link('date') . '/' . $sqlResult['Year'] . '/' . sprintf("%'02d", $monthVal); } else { $link = $container->Link('date') . '/' . $sqlResult['Year']; } if ($isMonthDisplay || !$isMonthDisplay && !in_array($year, $years)) { $years[] = $year; $current = false; $children = new DataObjectSet(); $LinkingMode = "link"; if ($isMonthDisplay && $yearParam == $year && $monthParam == $month || !$isMonthDisplay && $yearParam == $year) { $LinkingMode = "current"; $current = true; if ($this->ShowChildren && $isMonthDisplay) { $filter = $yearclause . ' = ' . $year . ' AND ' . $monthclause . ' = ' . $month; $children = DataObject::get('BlogEntry', $filter, "Date DESC"); } } $results->push(new ArrayData(array('Date' => $date, 'Year' => $year, 'Link' => $link, 'NoMonth' => !$isMonthDisplay, 'LinkingMode' => $LinkingMode, 'Children' => $children))); unset($children); } } } return $results; }
/** * Set url parameters (should only be called internally by RequestHandler->handleRequest()). * * @param array $params */ public static function setUrlParams($params) { Director::$urlParams = $params; }
function index($request) { // For 2.3 and 2.4 compatibility $bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; BasicAuth::enable(); BasicAuth::requireLogin("CMS RSS feed access. Use your CMS login", "CMS_ACCESS_CMSMain"); $member = $this->getBasicAuthMember(); // Due to a bug in 2.3.0 we can't get the information that we need from $request $params = Director::urlParams(); // Default value if (!isset($params['Data']) || !$params['Data']) { $params['Data'] = 'all'; } switch ($params['Data']) { case 'all': $changes = $this->changes(); break; case 'page': if ((int) $params['PageID']) { $changes = $this->changes("{$bt}SiteTree{$bt}.{$bt}ID{$bt} = " . (int) $params['PageID']); } else { return new HTTPResponse("<h1>Bad Page ID</h1><p>Bad page ID when getting RSS feed of changes to a page.</p>", 400); } break; default: user_error("CMSChangeTracker Data param value '{$params['Data']}' not implemented; this is probably due to a bad URL rule.", E_USER_ERROR); } $processedChanges = new DataObjectSet(); foreach ($changes as $change) { if ($change->canEdit($member)) { $author = DataObject::get_by_id("Member", $change->AuthorID); $verbed = $change->Version == 1 ? "created" : "edited"; if ($author) { $changeTitle = "'{$change->Title}' {$verbed} by {$author->FirstName} {$author->Surname}"; $changeAuthor = "{$author->FirstName} {$author->Surname}"; $firstParagraph = "{$author->FirstName} {$author->Surname} (<a href=\"mailto:{$author->Email}\">{$author->Email}</a>) has {$verbed} the '{$change->Title}' page."; } else { $changeTitle = "'{$change->Title}' {$verbed}"; $changeAuthor = ""; $firstParagraph = "The '{$change->Title}' page has been {$verbed}."; } $actionLinks = ""; $cmsLink = Director::absoluteURL("admin/show/{$change->ID}"); $actionLinks .= "<li><a href=\"{$cmsLink}\">Edit in CMS</a></li>\n"; $page = DataObject::get_by_id('SiteTree', $change->ID); if ($page) { $link = $page->AbsoluteLink(); $actionLinks .= "<li><a href=\"{$link}\">See the page on site</a></li>\n"; } if ($change->Version > 1) { $prevVersion = $change->Version - 1; $diffLink = Director::absoluteURL("admin/compareversions/{$change->ID}/?From={$prevVersion}&To={$change->Version}"); $actionLinks .= "<li><a href=\"{$diffLink}\">See the changes in CMS</a></li>\n"; } $changeDescription = <<<HTML <p>{$firstParagraph}</p> <h3>Actions and links</h3> <ul> \t{$actionLinks} </ul> HTML; $processedChange = new CMSChangeTracker_Change(array("ChangeTitle" => $changeTitle, "Author" => $changeAuthor, "Content" => $changeDescription, "Link" => $change->Link() . "version/{$change->Version}")); $processedChanges->push($processedChange); } } $feed = new RSSFeed($processedChanges, Director::absoluteURL("admin/"), "SilverStripe Content Changes", "", "ChangeTitle"); return $feed->outputToBrowser(); }
public function testURLParams() { // 2.4 only $originalDeprecation = Deprecation::dump_settings(); Deprecation::notification_version('2.4'); Director::test('DirectorTestRule/myaction/myid/myotherid'); // TODO Works on the assumption that urlParam() is not unset after a test run, which is dodgy $this->assertEquals(Director::urlParams(), array('Controller' => 'DirectorTestRequest_Controller', 'Action' => 'myaction', 'ID' => 'myid', 'OtherID' => 'myotherid')); Deprecation::restore_settings($originalDeprecation); }
function testURLParams() { Director::test('DirectorTestRule/myaction/myid/myotherid'); // TODO Works on the assumption that urlParam() is not unset after a test run, which is dodgy $this->assertEquals(Director::urlParams(), array('Controller' => 'DirectorTestRequest_Controller', 'Action' => 'myaction', 'ID' => 'myid', 'OtherID' => 'myotherid')); }
public static function getDateFromURL() { $params = Director::urlParams(); if (isset($params['Action'])) { return CalendarUtil::getDateFromString($params['Action']); } else { return false; } }
public function ics() { $this->urlParams = Director::urlParams(); $feed = false; $announcement = false; if (stristr($this->urlParams['ID'], "feed") !== false) { $id = str_replace("feed", "", $this->urlParams['ID']); $feed = true; } else { if (stristr($this->urlParams['ID'], "announcement-") !== false) { $id = str_replace("announcement-", "", $this->urlParams['ID']); $announcement = true; } else { $id = $this->urlParams['ID']; $announcement = false; } } if (is_numeric($id) && isset($this->urlParams['OtherID'])) { if (!$feed) { $event = DataObject::get_by_id($announcement ? $this->getModel()->getEventDateTimeClass() : $this->getModel()->getEventClass(), $id); $FILENAME = $announcement ? preg_replace("/[^a-zA-Z0-9s]/", "", $event->Title) : $event->URLSegment; } else { $FILENAME = preg_replace("/[^a-zA-Z0-9s]/", "", urldecode($_REQUEST['title'])); } $FILENAME .= ".ics"; $HOST = $_SERVER['HTTP_HOST']; $TIMEZONE = Calendar::$timezone; $LANGUAGE = Calendar::$language; $CALSCALE = Calendar::$calScale; $parts = explode('-', $this->urlParams['OtherID']); $START_TIMESTAMP = $parts[0]; $END_TIMESTAMP = $parts[1]; if (!$feed) { $URL = $announcement ? $event->Calendar()->Link() : $event->Link(); } else { $URL = ""; } $TITLE = $feed ? $_REQUEST['title'] : $event->Title; header("Cache-Control: private"); header("Content-Description: File Transfer"); header("Content-Type: text/calendar"); header("Content-Transfer-Encoding: binary"); if (stristr($_SERVER['HTTP_USER_AGENT'], "MSIE")) { header("Content-disposition: filename=" . $FILENAME . "; attachment;"); } else { header("Content-disposition: attachment; filename=" . $FILENAME); } // pull out the html comments return trim(strip_tags($this->customise(array('HOST' => $HOST, 'LANGUAGE' => $LANGUAGE, 'TIMEZONE' => $TIMEZONE, 'CALSCALE' => $CALSCALE, 'START_TIMESTAMP' => $START_TIMESTAMP, 'END_TIMESTAMP' => $END_TIMESTAMP, 'URL' => $URL, 'TITLE' => $TITLE))->renderWith(array('ics')))); } else { Director::redirectBack(); } }
/** * Handle an HTTP request, defined with a HTTPRequest object. * * @return HTTPResponse|string */ protected static function handleRequest(HTTPRequest $request, Session $session) { krsort(Director::$rules); if (isset($_REQUEST['debug'])) { Debug::show(Director::$rules); } foreach (Director::$rules as $priority => $rules) { foreach ($rules as $pattern => $controllerOptions) { if (is_string($controllerOptions)) { if (substr($controllerOptions, 0, 2) == '->') { $controllerOptions = array('Redirect' => substr($controllerOptions, 2)); } else { $controllerOptions = array('Controller' => $controllerOptions); } } if (($arguments = $request->match($pattern, true)) !== false) { // controllerOptions provide some default arguments $arguments = array_merge($controllerOptions, $arguments); // Find the controller name if (isset($arguments['Controller'])) { $controller = $arguments['Controller']; } // Pop additional tokens from the tokeniser if necessary if (isset($controllerOptions['_PopTokeniser'])) { $request->shift($controllerOptions['_PopTokeniser']); } // Handle redirections if (isset($arguments['Redirect'])) { return "redirect:" . Director::absoluteURL($arguments['Redirect'], true); } else { /* if(isset($arguments['Action'])) { $arguments['Action'] = str_replace('-','',$arguments['Action']); } if(isset($arguments['Action']) && ClassInfo::exists($controller.'_'.$arguments['Action'])) $controller = $controller.'_'.$arguments['Action']; */ if (isset($arguments['URLSegment'])) { self::$urlSegment = $arguments['URLSegment'] . "/"; } Director::$urlParams = $arguments; $controllerObj = new $controller(); $controllerObj->setSession($session); return $controllerObj->handleRequest($request); } } } } }
/** * For the RSS feed */ public function Link() { if ($this->Announcement()) { return $this->Calendar()->Link(); } else { $date = date('Y-m-d', strtotime($this->StartDate)); $filters = Calendar::buildFilterString(); $params = Director::urlParams(); if (isset($params['Action'])) { $calendar_params = "CalendarStart=" . $params['Action']; if (isset($params['ID'])) { $calendar_params .= "&CalendarEnd=" . $params['ID']; } } else { $calendar_params = null; } if ($filters || $calendar_params) { $query_string = !empty($filters) ? "?" . $filters . "&" . $calendar_params : "?" . $calendar_params; } else { $query_string = ""; } return $this->Event()->Link($date . $query_string); } }
public function getNestedController() { $url = Director::urlParams(); $hasUrlSegment = false; $parent = null; $counter = 1; foreach ($this->urlParams as $urlSegment) { $hasUrlSegment = true; $SQL_URLSegment = Convert::raw2sql($urlSegment); $filter = "URLSegment = '{$SQL_URLSegment}'"; if (isset($child)) { $filter .= "AND ParentID = {$child->ID}"; } else { $filter .= "AND ParentID = 0"; } $child = DataObject::get_one('SiteTree', $filter); if (!$child) { $key = $counter - 2; if ($key < 0) { $key = 0; } $this->mapParams($key); $c = new ModelAsController(); $this->request->setParams($this->urlParams); return $c->handleRequest($this->request); } if (isset($child->UrlParentID) && $child->UrlParentID != 0) { $parent = $child->UrlParent(); } else { if (isset($child->ParentID) && $child->ParentID != 0) { $parent = $child->Parent(); } else { $parent = null; } } if ($parent && $parent->URLSegment != 'home') { $keyint = $counter - 1; $key = "url" . $keyint; if (!isset($this->urlParams[$key]) || $parent->URLSegment != $this->urlParams[$key]) { $child = $this->get404Page(); break; } } $counter++; } if ($hasUrlSegment) { if (!$child) { if ($child = $this->findOldPage($SQL_URLSegment)) { $url = Controller::join_links(Director::baseURL(), $child->URLSegment, isset($this->urlParams['Action']) ? $this->urlParams['Action'] : null, isset($this->urlParams['ID']) ? $this->urlParams['ID'] : null, isset($this->urlParams['OtherID']) ? $this->urlParams['OtherID'] : null); $response = new HTTPResponse(); $response->redirect($url, 301); return $response; } $child = $this->get404Page(); } if ($child) { $this->mapParams(); if (isset($_REQUEST['debug'])) { Debug::message("Using record #{$child->ID} of type {$child->class} with URL {$this->urlParams['URLSegment']}"); } // set language if ($child->Locale) { Translatable::set_current_locale($child->Locale); } $controllerClass = "{$child->class}_Controller"; if ($this->urlParams['Action'] && ClassInfo::exists($controllerClass . '_' . $this->urlParams['Action'])) { $controllerClass = $controllerClass . '_' . $this->urlParams['Action']; } if (ClassInfo::exists($controllerClass)) { $controller = new $controllerClass($child); } else { $controller = $child; } return $controller; } else { return new HTTPResponse("The requested page couldn't be found.", 404); } } else { user_error("NestedUrlController not geting a URLSegment. It looks like the site isn't redirecting to home", E_USER_ERROR); } }
function BlogEntries($limit = 10) { $start = isset($_GET['start']) ? (int) $_GET['start'] : 0; $tag = ''; $date = ''; if(Director::urlParams()) { if(Director::urlParam('Action') == 'tag') { $tag = Director::urlParam('ID'); } else { $year = Director::urlParam('Action'); $month = Director::urlParam('ID'); if($month && is_numeric($month) && $month >= 1 && $month <= 12 && is_numeric($year)) { $date = "$year-$month"; } else if(is_numeric($year)) { $date = $year; } } } return $this->Entries("$start,$limit", $tag, $date); }
static function getControllerForURL($url) { if (isset($_GET['debug_profile'])) { Profiler::mark("Director", "getControllerForURL"); } $url = preg_replace(array('/\\/+/', '/^\\//', '/\\/$/'), array('/', '', ''), $url); $urlParts = split('/+', $url); krsort(Director::$rules); if (isset($_REQUEST['debug'])) { Debug::show(Director::$rules); } foreach (Director::$rules as $priority => $rules) { foreach ($rules as $pattern => $controller) { $patternParts = explode('/', $pattern); $matched = true; $arguments = array(); foreach ($patternParts as $i => $part) { $part = trim($part); if (isset($part[0]) && $part[0] == '$') { $arguments[substr($part, 1)] = isset($urlParts[$i]) ? $urlParts[$i] : null; if ($part == '$Controller' && !class_exists($arguments['Controller'])) { $matched = false; break; } } else { if (!isset($urlParts[$i]) || $urlParts[$i] != $part) { $matched = false; break; } } } if ($matched) { if (substr($controller, 0, 2) == '->') { if ($_REQUEST['debug'] == 1) { Debug::message("Redirecting to {$controller}"); } if (isset($_GET['debug_profile'])) { Profiler::unmark("Director", "getControllerForURL"); } return "redirect:" . Director::absoluteURL(substr($controller, 2), true); } else { if (isset($arguments['Controller']) && $controller == "*") { $controller = $arguments['Controller']; } if (isset($_REQUEST['debug'])) { Debug::message("Using controller {$controller}"); } if (isset($arguments['Action'])) { $arguments['Action'] = str_replace('-', '', $arguments['Action']); } if (isset($arguments['Action']) && ClassInfo::exists($controller . '_' . $arguments['Action'])) { $controller = $controller . '_' . $arguments['Action']; } Director::$urlParams = $arguments; $controllerObj = new $controller(); $controllerObj->setURLParams($arguments); if (isset($arguments['URLSegment'])) { self::$urlSegment = $arguments['URLSegment'] . "/"; } if (isset($_GET['debug_profile'])) { Profiler::unmark("Director", "getControllerForURL"); } return $controllerObj; } } } } }