<commit_before><?php
/**
 * nPress - opensource cms
 *
 * @copyright  (c) 2012 <NAME> (<EMAIL>)
 * @link       http://npress.info/
 * @package    nPress
 */


//Page meta .fileds = {"date":{"label":"datum"}} --> all child pages have these form fileds
class FieldsPlugin extends Control{
	static $events = array(
					'filterPageEditForm_create',
					'filterPageEditForm_render',
					'filterPageEditForm_values',
					'filterPageEditForm_defaults'
			);

	private $page;
	private $fields = array();

	public function __construct(){
		parent::__construct();
		$this->monitor('Nette\Application\UI\Presenter');
	}


	protected function attached($presenter){
		parent::attached($presenter);
		if (!($presenter instanceof /*Nette\Application\UI\*/Presenter))
			return;

		//připojen presenter
		if(!isset($presenter->page))
						throw new InvalidStateException('FieldsPlugin attached to uncompatible Presenter');
		$this->page = $presenter->page;
		$this->template->page = $presenter->page;
		$this->template->lang = $presenter->lang;
		$this->template->setTranslator(new TranslationsModel($presenter->lang));

		//parse fields
		$json = $this->page->getParent()->getMeta('.fields');
		$fields = array();
		try {
			if($json)
				$fields = Neon::decode($json);
		} catch (Exception $e){}

		foreach($fields as $k=>$f){
			if(substr($k,-1) == '_') $k .= $this->page->lang;
			$this->fields[$k] = $f;
		}
	}

	function filterPageEditForm_create(AppForm $form){
		foreach($this->fields as $k=>$v){
			$label = (is_array($v) && isset($v['label'])) ? $v['label'] : $k;

			if(isset($v['type']) AND $v['type'] == "textarea")
				$form->addTextarea("fields_$k", $label);
			elseif(isset($v['type']) AND $v['type'] == "checkbox")
				$form->addCheckbox("fields_$k", $label);
			else
				$form->addText("fields_$k", $label);
		}

		return $form;
	}

	function filterPageEditForm_defaults(AppForm $form){
		foreach($this->fields as $k=>$v){
			$form["fields_$k"]->setValue($this->page->getMeta($k));
		}

		return $form;
	}
	
	function filterPageEditForm_render(AppForm $_form){
		foreach($this->fields as $k=>$v){
				?>
				<div class="control-group">
				<?php if ($_label = $_form["fields_$k"]->getLabel()) echo $_label->addAttributes(array("class"=>"control-label" )) ?>
				<div class="controls"><?php echo $_form["fields_$k"]->getControl(); ?>
					<span class="help-inline"><?php echo @ $v['desc'] ?></span>
					</div></div><?php
		}
		return $_form;
	}

	function filterPageEditForm_values($values){
		foreach($this->fields as $k=>$v){
			$val = $values["fields_$k"];
			unset($values["fields_$k"]);

			if(isset($v['type']) AND $v['type'] == 'link' AND !preg_match('~^[a-z]+://~i', $val)){
				$val = "http://$val";
			}

			$this->page->addMeta($k, $val);
		}<commit_msg>FieldsPlugin - .sectionFields for subpages + .fields for single page<commit_after><?php
/**
 * nPress - opensource cms
 *
 * @copyright  (c) 2012 <NAME> (<EMAIL>)
 * @link       http://npress.info/
 * @package    nPress
 */


//Page meta .fileds = {"date":{"label":"datum"}} --> all child pages have these form fileds
class FieldsPlugin extends Control{
	static $events = array(
					'filterPageEditForm_create',
					'filterPageEditForm_render',
					'filterPageEditForm_values',
					'filterPageEditForm_defaults'
			);

	private $page;
	private $fields = array();
	private $error = "";

	public function __construct(){
		parent::__construct();
		$this->monitor('Nette\Application\UI\Presenter');
	}


	protected function attached($presenter){
		parent::attached($presenter);
		if (!($presenter instanceof /*Nette\Application\UI\*/Presenter))
			return;

		//připojen presenter
		if(!isset($presenter->page))
						throw new InvalidStateException('FieldsPlugin attached to uncompatible Presenter');
		$this->page = $presenter->page;
		$this->template->page = $presenter->page;
		$this->template->lang = $presenter->lang;
		$this->template->setTranslator(new TranslationsModel($presenter->lang));

		//parse fields
		$json = $this->page->getParent()->getMeta('.sectionFields');
		$sectionFields = array();
		try {
			if($json)
				$fields = Neon::decode($json);
		} catch (Exception $e){
			$m = htmlspecialchars($e->getMessage());
			$this->error .= "<div class='control-group' title=\"$m\">.sectionFields not valid</div>";
		}

		$fields = array();
		$json = $this->page->getMeta('.fields');
		try {
			if($json)
				$fields = Neon::decode($json);
		} catch (Exception $e){
			$m = htmlspecialchars($e->getMessage()."\"");
			$this->error .= "<div class='control-group' title=\"$m\">.fields not valid</div>";
		}


		$fields = array_merge($fields, $sectionFields);
		foreach($fields as $k=>$f){
			if(substr($k,-1) == '_') $k .= $this->page->lang;
			$this->fields[$k] = $f;
		}
	}

	function filterPageEditForm_create(AppForm $form){
		foreach($this->fields as $k=>$v){
			$label = (is_array($v) && isset($v['label'])) ? $v['label'] : $k;

			if(isset($v['type']) AND $v['type'] == "textarea")
				$form->addTextarea("fields_$k", $label);
			elseif(isset($v['type']) AND $v['type'] == "checkbox")
				$form->addCheckbox("fields_$k", $label);
			else
				$form->addText("fields_$k", $label);
		}

		return $form;
	}

	function filterPageEditForm_defaults(AppForm $form){
		foreach($this->fields as $k=>$v){
			$form["fields_$k"]->setValue($this->page->getMeta($k));
		}

		return $form;
	}

	function filterPageEditForm_render(AppForm $_form){
		echo $this->error;
		foreach($this->fields as $k=>$v){
				?>
				<div class="control-group">
				<?php if ($_label = $_form["fields_$k"]->getLabel()) echo $_label->addAttributes(array("class"=>"control-label" )) ?>
				<div class="controls"><?php echo $_form["fields_$k"]->getControl(); ?>
					<span class="help-inline"><?php echo @ $v['desc'] ?></span>
					</div></div><?php
		}
		return $_form;
	}

	function filterPageEditForm_values($values){
		foreach($this->fields as $k=>$v){
			$val = $values["fields_$k"];
			unset($values["fields_$k"]);

			if(isset($v['type']) AND $v['type'] == 'link' AND !preg_match('~^[a-z]+://~i', $val)){
				$val = "http://$val";
			}

			$this->page->addMeta($k, $val);
		}
{'fraction_non_alphanumeric': 0.21689497716894976, 'fraction_numerical': 0.0016307893020221786, 'mean_word_length': 4.296200345423143, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 120, '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': '6702365', 'n_tokens_mistral': 2393, 'n_tokens_neox': 2108, 'n_words': 391}