Created
December 1, 2011 15:35
-
-
Save markjames/1417615 to your computer and use it in GitHub Desktop.
Silverstripe Snippets
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 | |
// Set default cache length in seconds | |
HTTP::set_cache_age( 60 ); | |
// Disable cache per page | |
HTTP::set_cache_age( 0 ); | |
// Set default cache lifetime in seconds | |
SS_Cache::set_cache_lifetime('all', 600, 1); | |
// Set template partial caching in seconds | |
SS_Cache::set_cache_lifetime('cacheblock', 120, 2); | |
// Change order of comment listings | |
PageCommentInterface::$order_comments_by = '"Created" ASC'; | |
// Put in site config | |
Security::set_login_recording(true); |
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 | |
function SearchTest() { | |
// Search for all news posts that have the announcements or reviews category | |
$criteria = array( | |
'Categories.URLSegment' => array('announcements','reviews'), | |
); | |
$search = new SearchContext( | |
'NewsPost', | |
array(), | |
array( | |
'Categories.URLSegment' => new ExactMatchFilter('Categories.URLSegment'), | |
) | |
); | |
return $search->getResults($criteria); | |
// Basic search on Title | |
$criteria = array( | |
'Title' => $_GET['title'], | |
); | |
$search = new SearchContext( | |
'NewsPost', | |
array(), | |
array( | |
'Title' => new ExactMatchFilter('Title') | |
) | |
); | |
return $search->getResults($criteria); | |
} |
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 | |
// A new page type | |
class Page extends SiteTree { | |
static $icon = array('/site/icons/page.png?','file'); | |
// Or if you have icons in multiple states: | |
// static $icon = '/site/icons/page'; | |
// Allowed child types | |
// static $allowed_children = array('MyPage'); | |
// Default child type | |
static $default_child = "Page"; | |
// Can exist at the root of the site? | |
static $can_be_root = true; | |
// Extensions | |
static $extensions = array(); | |
/* | |
static $many_many = array( | |
'FeaturePanels' => 'FeatureLinkPanel' | |
); | |
*/ | |
/* | |
static $db = array( | |
'NewField' =>'Boolean' | |
); | |
static $defaults = array( | |
'NewField' => '1' | |
); | |
*/ | |
public function canCreate() { | |
// Only allow one page of this type | |
// return !DataObject::get_one($this->class); | |
return parent::canCreate(); | |
} | |
public function canDelete() { | |
// Don't allow deletion | |
// return false; | |
return parent::canDelete(); | |
} | |
/** | |
* Customise the CMS field | |
* | |
* @return FieldsSet | |
*/ | |
public function getCMSFields(){ | |
$fields = parent::getCMSFields(); | |
return $fields; | |
} | |
/** | |
* Return classes for the body | |
* | |
* @return string The HTML metatags | |
*/ | |
public function BodyClasses() { | |
$classes = array(); | |
// Get each of the page types | |
$className = get_class($this); | |
$classes []= 'type'. strtolower(preg_replace('~([A-Z])~','-$1',$className)); | |
while( ($className = get_parent_class($className)) && $className != 'SiteTree' ) { | |
$classes []= 'subtype'. strtolower(preg_replace('~([A-Z])~','-$1',$className)); | |
} | |
// Add a page ID | |
$page = $this; | |
$classes []= 'page-id-'.$page->ID; | |
// Find the section by looping through the parents | |
while( true ) { | |
$newpage = $page->Parent; | |
if( $newpage ) { | |
$page = $newpage; | |
} else { | |
$classes []= 'section-'.$page->URLSegment; | |
break; | |
} | |
} | |
return implode(' ',$classes); | |
} | |
/** | |
* Return the title | |
* | |
* @return string The HTML metatags | |
*/ | |
function MetaTitle(){ | |
if($this->MetaTitle && $this->MetaTitle != $this->Title) { | |
return $this->MetaTitle; | |
} else { | |
return $this->getSiteConfig()->Title . ' - ' . $this->Title; | |
} | |
} | |
/** | |
* Return the title, description, keywords and language metatags. | |
* | |
* @todo Move <title> tag in separate getter for easier customization and more obvious usage | |
* | |
* @param boolean|string $includeTitle Show default <title>-tag, set to false for custom templating | |
* @param boolean $includeTitle Show default <title>-tag, set to false for | |
* custom templating | |
* @return string The HTML metatags | |
*/ | |
public function MetaTags($includeTitle = false, $indent="\t" ) { | |
$tags = array(); | |
if($includeTitle === true || $includeTitle == 'true') { | |
$tags []= "<title>" . $this->MetaTitle() . "</title>"; | |
} | |
// Keywords | |
if( $this->MetaKeywords ) { | |
$tags []= '<meta name="keywords" content="' . Convert::raw2att($this->MetaKeywords) . '">'; | |
} | |
// Description | |
if( $this->MetaKeywords ) { | |
$tags []= '<meta name="description" content="' . Convert::raw2att($this->MetaDescription) . '">'; | |
} | |
if( $this->ExtraMeta ) { | |
$tags []= $this->ExtraMeta; | |
} | |
$tags = implode("\n".$indent,$tags); | |
$this->extend('MetaTags', $tags); | |
return $tags; | |
} | |
/** | |
* Return an array of opengraph metadata elements | |
* | |
* @return string The HTML metatags | |
*/ | |
protected function OpenGraphMetaTagElements() { | |
$tags = array(); | |
$tags['site_name'] = $this->getSiteConfig()->Title; | |
$tags['title'] = $this->MetaTitle(); | |
$tags['description'] = $this->MetaDescription ? $this->MetaDescription : $this->getSiteConfig()->SiteDescription; | |
$tags['type'] = 'article'; | |
// Get default image from Site Config | |
if( $siteImage = $this->getSiteConfig()->SiteImage() ) { | |
$siteImageUrl = $siteImage->URL('opengraph'); | |
$tags['image'] = $siteImageUrl; | |
} | |
if( $this->has_one('Image') ) { | |
$iconsize = 'opengraph'; | |
if($iconsize && $img = $this->Image()) { | |
$tags['image'] = $img->URL($iconsize); | |
} | |
} | |
$tags['url'] = $this->AbsoluteLink(); | |
return $tags; | |
} | |
/** | |
* Return the open graph tags | |
* | |
* @return string The HTML metatags | |
*/ | |
public function OpenGraphMetaTags( $indent="\t" ) { | |
$tags = $this->OpenGraphMetaTagElements(); | |
if( !is_array($tags) ) { | |
return ''; | |
} | |
$output = array(); | |
foreach( $tags as $key => $val ) { | |
$output []= '<meta property="og:'.$key.'" content="' . Convert::raw2att($val) . '">'; | |
} | |
$tags = implode("\n".$indent,$output); | |
return $tags; | |
} | |
/** | |
* Adds first/last/start/end class to a list item. | |
* | |
* Sometimes you need to: | |
* Lay out a load of thumbnails in a grid. | |
* Style only the first or last elements in a vertical/horizontal list of items | |
* | |
* You can do this using nth-child pseudoselectors, but it is dangerous. | |
* | |
* Instead, on any list of elements: | |
* | |
* add a _first_ class to the very first one | |
* add a _last_ class to the very last one | |
* add a _start_ class to the first item on a row (e.g. every 4th item, starting with the first one) | |
* add an _end_ class to the last item on a row (e.g. every 4th item, starting with the fourth one) | |
* | |
* @param $rowLength length of each row | |
* @return string list class | |
*/ | |
public function ListClass( $rowLength ) { | |
$classes = array(); | |
// first | |
if ($this->iteratorPos == 0) { | |
$classes[] = 'first'; | |
} | |
// last | |
if($this->iteratorPos == $this->iteratorTotalItems - 1) { | |
$classes[] = 'last'; | |
} | |
// start | |
if ($this->iteratorPos % $rowLength == 0) { | |
$classes[] = 'start'; | |
} | |
// end | |
if ($this->iteratorPos % $rowLength == $rowLength - 1) { | |
$classes[] = 'end'; | |
} | |
$html = join(' ', $classes); | |
return $html; | |
} | |
/** | |
* Return the value of the current environment | |
*/ | |
public function Environment() { | |
$val = Director::get_environment_type() or $val = 'live'; | |
return $val; | |
} | |
} |
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 | |
// Put in Site_Controller, call to flip between SSL and non-SSL pages | |
protected function _checkSSL() { | |
$needSSL = $inSSL = $destURL = false; | |
$inSSL = ( isset($_SERVER['SSL']) || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ) ? true : false; | |
// Get static $ssl_actions and see if we need to switch in or out of SSL | |
if($all_ssl_actions = Object::combined_static($this, 'ssl_actions') and is_array($all_ssl_actions) ) { | |
$action = $this->getRequest()->latestParam('Action'); // $this->getAction() always empty?? | |
if( in_array($action,$all_ssl_actions) or | |
(in_array('index',$all_ssl_actions) and is_null($action) ) ) { | |
$needSSL = true; | |
} | |
} | |
if( $needSSL and !$inSSL ){ | |
$destURL = str_replace('http:','https:', Director::absoluteURL($_SERVER['REQUEST_URI'])); | |
} elseif( !$needSSL and $inSSL ) { | |
$destURL = str_replace('https:','http:', Director::absoluteURL($_SERVER['REQUEST_URI'])); | |
} | |
if( $destURL ) { | |
header("Location: $destURL", true, 301); | |
die('<h1>Your browser is not accepting header redirects</h1><p>Please <a href="'.$destURL.'">click here</a>'); | |
} | |
} |
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
// Caching lists | |
<% cached 'child-pages', Aggregate(Children).Max(LastEdited) %> | |
<% cached unless CurrentUser %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment