<commit_before><?php
/**
 * News page and controller.
 * 
 * NewsHolderPage
 *
 * @package News/blog module
 * @author Simon 'Sphere'
 * @method News Newsitems() linked to this page (for Translatable)
 * @todo WHOAH! This thing is fat. Slim it down boy!
 * @todo besides the general getters, the news-functions should be in the model
 */
class NewsHolderPage extends Page {

	/** @var string $description PageType's description */
	private static $description = 'HolderPage for newsitems';
	
	/** @var array $many_many many-to-many relationships */
	private static $many_many = array(
		'Newsitems' => 'News'
	);
	
	/** @var array $allowed_children Allowed Children */
	private static $allowed_children = array(
		'News',
	);

	/**
	 * Create a default NewsHolderPage. This prevents error500 because of a missing page.
	 * @todo optional creation? I'm afraid here's a big potential bug at extending stuff!
	 */
	public function requireDefaultRecords()	{
		parent::requireDefaultRecords();
		if(NewsHolderPage::get()->count() == 0){
			$page = NewsHolderPage::create();
			$page->Title = _t('NewsHolderPage.DEFAULTPAGETITLE', 'Newspage');
			$page->Content = '';
			$page->URLSegment = 'news';
			$page->Sort = 1;
			$page->Status = 'Published';
			$page->write();
			$page->publish('Stage','Live');
			$page->flushCache();
			/** 
			 * This is to make sure we don't create any orphans by upgrading.
			 * It shouldn't be necessary, but we prefer to be safe over being sorry.
			 */
			$newsItems = News::get()->filter(array('NewsHolderPageID' => 0));
			if($newsItems->count()){
				foreach($newsItems as $newsItem){
					$newsItem->NewsHolderPageID = $this->ID;
					$newsItem->write();
				}
			}
			DB::alteration_message('Newsholder Page created', 'created');
		}
		/** @todo fix backward compatibility for Author-method */
		/** Backwards compatibility for upgraders. Update the PublishFrom field */
		$sql = "UPDATE `News` SET `PublishFrom` = `Created` WHERE `PublishFrom` IS NULL";
		DB::query($sql);
	}
	
	/**
	 * Support for children.
	 * Just call <% loop Children.Limit(x) %>$Title<% end_loop %> from your template to get the news-children.
	 * @return DataObjectSet NewsItems Items belonging to this page
	 */
	public function Children(){
		return $this->Newsitems()
			->filter(array('Live' => true))
			->exclude(array('PublishFrom:GreaterThan' => 'NOW()'));
	}

}

class NewsHolderPage_Controller extends Page_Controller {

	/**
	 * We allow a lot, right?
	 * @var array $allowed_actions, again.
	 */
	private static $allowed_actions = array(
		'show',
		'tag',
		'tags',
		'rss',
		'archive',
		'author',
		'CommentForm',
		'migrate',
	);
	
	private static $url_handlers = array();
	
	protected $current_item;
	
	protected $current_tag;
	
	protected $current_siteconfig;

	/**
	 * Include the tagcloud scripts. Configure in newsmodule.js!
	 */
	public function init() {
		parent::init();
		$this->needsRedirect();
		$this->setNews();
		// I would advice to put these in a combined file, but it works this way too.
		Requirements::javascript('silverstripe-newsmodule/javascript/jquery.tagcloud.js');
		Requirements::javascript('silverstripe-newsmodule/javascript/newsmodule.js');
	}

	/**
	 * We escape the tags here, otherwise things bug out with the meta-tags.
	 * @return News $news Current newsitem selected.
	 */
	public function setNews(){
		$Params = $this->getURLParams();
		$exclude = array(
			'PublishFrom:GreaterThan' => date('Y-m-d H:i:s'), 
		);
		/** @var array $segmentFilter Array containing the filter for current or any item */
		$segmentFilter = $this->setupFilter($Params);
		$news = $this->Newsitems()->filter($segmentFilter)->exclude($exclude)->first();
		$this->current_item = $news;
	}
	
	/**
	 * 
	 * @return News The current newsitem
	 */
	public function getNews() {
		return $this->current_item;
	}
	/**
	 * Set the current SiteConfig
	 */
	private function setCurrentSiteConfig() {
		$this->current_siteconfig = SiteConfig::current_site_config();
	}
	
	/**
	 * Get the current SiteConfig
	 * @return SiteConfig
	 */
	private function getCurrentSiteConfig() {
		if(!$this->current_siteconfig) {
			$this->setCurrentSiteConfig();
		}
		return $this->current_siteconfig;
	}
	
	/**
	 * Setup the allowed actions to work with the SiteConfig settings.
	 * @param type $limitToClass
	 * @return array
	 */
	public function allowedActions($limitToClass = null){
		$actions = parent::allowedActions($limitToClass);
		$defaultMapping = self::$allowed_actions;
		$siteConfig = $this->getCurrentSiteConfig();
		foreach($defaultMapping as $map) {
			$key = ucfirst($map.'Action');
			if($siteConfig->$key) {
				self::$allowed_actions[] = $siteConfig->$key;
			}
		}
		return array_merge($actions, self::$allowed_actions);
	}
	
	public function handleAction($request, $action) {
		parent::handleAction($request, $action);
		$defaultMapping = array(
			/** URL Mapping */
			'tag',
			'tags',
			'show',
			'author',
			'archive',
		);
		$siteConfig = $this->getCurrentSiteConfig();
		foreach($defaultMapping as $key) {
			$map = ucfirst($key.'Action');
			if($siteConfig->$map && $siteConfig->$map == $action) {
				return parent::handleAction($request, $key);
			}
		}
		return parent::handleAction($request, $action);
	}

	/**
	 * Meta! This is so Meta! I mean, MetaTitle!
	 */
	public function MetaTitle(){
		$Params = $this->getURLParams();<commit_msg>Resolving issue #114, This allows the admin to setup his/her own url-mapping via the SiteConfig. Only available for admins!<commit_after><?php
/**
 * News page and controller.
 * 
 * NewsHolderPage
 *
 * @package News/blog module
 * @author Simon 'Sphere'
 * @method News Newsitems() linked to this page (for Translatable)
 * @todo WHOAH! This thing is fat. Slim it down boy!
 * @todo besides the general getters, the news-functions should be in the model
 */
class NewsHolderPage extends Page {

	/** @var string $description PageType's description */
	private static $description = 'HolderPage for newsitems';
	
	/** @var array $many_many many-to-many relationships */
	private static $many_many = array(
		'Newsitems' => 'News'
	);
	
	/** @var array $allowed_children Allowed Children */
	private static $allowed_children = array(
		'News',
	);

	/**
	 * Create a default NewsHolderPage. This prevents error500 because of a missing page.
	 * @todo optional creation? I'm afraid here's a big potential bug at extending stuff!
	 */
	public function requireDefaultRecords()	{
		parent::requireDefaultRecords();
		if(NewsHolderPage::get()->count() == 0){
			$page = NewsHolderPage::create();
			$page->Title = _t('NewsHolderPage.DEFAULTPAGETITLE', 'Newspage');
			$page->Content = '';
			$page->URLSegment = 'news';
			$page->Sort = 1;
			$page->Status = 'Published';
			$page->write();
			$page->publish('Stage','Live');
			$page->flushCache();
			/** 
			 * This is to make sure we don't create any orphans by upgrading.
			 * It shouldn't be necessary, but we prefer to be safe over being sorry.
			 */
			$newsItems = News::get()->filter(array('NewsHolderPageID' => 0));
			if($newsItems->count()){
				foreach($newsItems as $newsItem){
					$newsItem->NewsHolderPageID = $this->ID;
					$newsItem->write();
				}
			}
			DB::alteration_message('Newsholder Page created', 'created');
		}
		/** @todo fix backward compatibility for Author-method */
		/** Backwards compatibility for upgraders. Update the PublishFrom field */
		$sql = "UPDATE `News` SET `PublishFrom` = `Created` WHERE `PublishFrom` IS NULL";
		DB::query($sql);
	}
	
	/**
	 * Support for children.
	 * Just call <% loop Children.Limit(x) %>$Title<% end_loop %> from your template to get the news-children.
	 * @return DataObjectSet NewsItems Items belonging to this page
	 */
	public function Children(){
		return $this->Newsitems()
			->filter(array('Live' => true))
			->exclude(array('PublishFrom:GreaterThan' => 'NOW()'));
	}

}

class NewsHolderPage_Controller extends Page_Controller {

	/**
	 * We allow a lot, right?
	 * @var array $allowed_actions, again.
	 */
	private static $allowed_actions = array(
		'show',
		'tag',
		'tags',
		'rss',
		'archive',
		'author',
		'CommentForm',
		'migrate',
	);
	
	private static $url_handlers = array();
	
	protected $current_item;
	
	protected $current_tag;
	
	protected $current_siteconfig;

	/**
	 * Include the tagcloud scripts. Configure in newsmodule.js!
	 */
	public function init() {
		parent::init();
		$this->needsRedirect();
		$this->setNews();
		// I would advice to put these in a combined file, but it works this way too.
		Requirements::javascript('silverstripe-newsmodule/javascript/jquery.tagcloud.js');
		Requirements::javascript('silverstripe-newsmodule/javascript/newsmodule.js');
	}

	/**
	 * We escape the tags here, otherwise things bug out with the meta-tags.
	 * @return News $news Current newsitem selected.
	 */
	public function setNews(){
		$Params = $this->getURLParams();
		$exclude = array(
			'PublishFrom:GreaterThan' => date('Y-m-d H:i:s'), 
		);
		/** @var array $segmentFilter Array containing the filter for current or any item */
		$segmentFilter = $this->setupFilter($Params);
		$news = $this->Newsitems()->filter($segmentFilter)->exclude($exclude)->first();
		$this->current_item = $news;
	}
	
	/**
	 * @return News The current newsitem
	 */
	public function getNews() {
		return $this->current_item;
	}
	/**
	 * Set the current SiteConfig
	 */
	private function setCurrentSiteConfig() {
		$this->current_siteconfig = SiteConfig::current_site_config();
	}
	
	/**
	 * Get the current SiteConfig
	 * @return SiteConfig
	 */
	private function getCurrentSiteConfig() {
		if(!$this->current_siteconfig) {
			$this->setCurrentSiteConfig();
		}
		return $this->current_siteconfig;
	}
	
	/**
	 * Setup the allowed actions to work with the SiteConfig settings.
	 * @param type $limitToClass
	 * @return array
	 */
	public function allowedActions($limitToClass = null){
		$actions = parent::allowedActions($limitToClass);
		$defaultMapping = self::$allowed_actions;
		$siteConfig = $this->getCurrentSiteConfig();
		foreach($defaultMapping as $map) {
			$key = ucfirst($map.'Action');
			if($siteConfig->$key) {
				self::$allowed_actions[] = $siteConfig->$key;
			}
		}
		return array_merge($actions, self::$allowed_actions);
	}
	
	public function handleAction($request, $action) {
		parent::handleAction($request, $action);
		$defaultMapping = self::$allowed_actions;
		$siteConfig = $this->getCurrentSiteConfig();
		foreach($defaultMapping as $key) {
			$map = ucfirst($key.'Action');
			if($siteConfig->$map && $siteConfig->$map == $action) {
				return parent::handleAction($request, $key);
			}
		}
		return parent::handleAction($request, $action);
	}

	/**
	 * Meta! This is so Meta! I mean, MetaTitle!
	 */
	public function MetaTitle(){
		$Params = $this->getURLParams();
{'fraction_non_alphanumeric': 0.14962086184575552, 'fraction_numerical': 0.0013870908082115776, 'mean_word_length': 4.034916201117318, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5033384', 'n_tokens_mistral': 3686, 'n_tokens_neox': 3267, 'n_words': 902}