<commit_before><?php

namespace Craft;

class MatrixGroupPlugin extends BasePlugin
{
	public function getName()
	{
		return Craft::t('Matrix Group');
	}

	public function getDescription()
	{
		return 'Give structure to your Matrix fields';
	}

	public function getVersion()
	{
		return '0.0.1';
	}

	public function getSchemaVersion()
	{
		return '0.0.1';
	}

	public function getDeveloper()
	{
		return '<NAME>';
	}

	public function getDeveloperUrl()
	{
		return 'http://benjamminf.github.io';
	}

	public function getDocumentationUrl()
	{
		return 'https://github.com/benjamminf/craft-matrix-group';
	}

	public function getReleaseFeedUrl()
	{
		return 'https://raw.githubusercontent.com/benjamminf/craft-matrix-group/master/releases.json';
	}

	public function isCraftRequiredVersion()
	{
		return version_compare(craft()->getVersion(), '2.5', '>=');
	}

	public function init()
	{
		parent::init();

		if(craft()->request->isCpRequest() && $this->isCraftRequiredVersion())
		{
			$this->includeResources();
			$this->bindEvents();
		}
	}

	protected function includeResources()
	{
		if(!craft()->request->isAjaxRequest())
		{
			craft()->templates->includeCssResource('matrixgroup/css/main.css');
			craft()->templates->includeJsResource('matrixgroup/js/main.js');
			craft()->templates->includeJs('MatrixGroup.groups=' . json_encode($this->_getGroupBlockTypes()));
			craft()->templates->includeJs('MatrixGroup.levels=' . json_encode($this->_getGroupBlockLevels()));
			craft()->templates->includeJsResource('matrixgroup/js/settings.js');
			craft()->templates->includeJsResource('matrixgroup/js/field.js');
		}
	}

	protected function bindEvents()
	{
		$levelsToSave = array();
		$levelsToDelete = array();

		// TODO Save matrix group markers here instead of controller
		craft()->on('elements.saveElement', function(Event $e) use(&$levelsToSave, &$levelsToDelete)
		{
			$element = $e->params['element'];
			$isNewElement = $e->params['isNewElement'];

			if($element->elementType === ElementType::MatrixBlock)
			{
				$block = $element;
				$owner = $block->getOwner();
				$type = $block->getType();
				$field = craft()->fields->getFieldById($type->fieldId);
				$level = 0;

				$postBlocks = craft()->request->getPost('fields.' . $field->handle);

				if($isNewElement)
				{
					$postBlockIds = array_keys($postBlocks);
					$postBlockId = $postBlockIds[$block->sortOrder - 1];
				}
				else
				{
					$postBlockId = $block->id;
				}

				$postBlock = $postBlocks[$postBlockId];

				if(array_key_exists('level', $postBlock))<commit_msg>Fixed error thrown when saving in modal window<commit_after><?php

namespace Craft;

class MatrixGroupPlugin extends BasePlugin
{
	public function getName()
	{
		return Craft::t('Matrix Group');
	}

	public function getDescription()
	{
		return 'Give structure to your Matrix fields';
	}

	public function getVersion()
	{
		return '0.0.1';
	}

	public function getSchemaVersion()
	{
		return '0.0.1';
	}

	public function getDeveloper()
	{
		return '<NAME>';
	}

	public function getDeveloperUrl()
	{
		return 'http://benjamminf.github.io';
	}

	public function getDocumentationUrl()
	{
		return 'https://github.com/benjamminf/craft-matrix-group';
	}

	public function getReleaseFeedUrl()
	{
		return 'https://raw.githubusercontent.com/benjamminf/craft-matrix-group/master/releases.json';
	}

	public function isCraftRequiredVersion()
	{
		return version_compare(craft()->getVersion(), '2.5', '>=');
	}

	public function init()
	{
		parent::init();

		if(craft()->request->isCpRequest() && $this->isCraftRequiredVersion())
		{
			$this->includeResources();
			$this->bindEvents();
		}
	}

	protected function includeResources()
	{
		if(!craft()->request->isAjaxRequest())
		{
			craft()->templates->includeCssResource('matrixgroup/css/main.css');
			craft()->templates->includeJsResource('matrixgroup/js/main.js');
			craft()->templates->includeJs('MatrixGroup.groups=' . json_encode($this->_getGroupBlockTypes()));
			craft()->templates->includeJs('MatrixGroup.levels=' . json_encode($this->_getGroupBlockLevels()));
			craft()->templates->includeJsResource('matrixgroup/js/settings.js');
			craft()->templates->includeJsResource('matrixgroup/js/field.js');
		}
	}

	protected function bindEvents()
	{
		$levelsToSave = array();
		$levelsToDelete = array();

		// TODO Save matrix group markers here instead of controller
		craft()->on('elements.saveElement', function(Event $e) use(&$levelsToSave, &$levelsToDelete)
		{
			$element = $e->params['element'];
			$isNewElement = $e->params['isNewElement'];

			if($element->elementType === ElementType::MatrixBlock)
			{
				$block = $element;
				$owner = $block->getOwner();
				$type = $block->getType();
				$field = craft()->fields->getFieldById($type->fieldId);
				$level = 0;

				$postNamespace = craft()->request->getPost('namespace');
				$postNamespace = $postNamespace . ($postNamespace ? '.' : '');
				$postBlocks = craft()->request->getPost($postNamespace . 'fields.' . $field->handle);

				if($isNewElement)
				{
					$postBlockIds = array_keys($postBlocks);
					$postBlockId = $postBlockIds[$block->sortOrder - 1];
				}
				else
				{
					$postBlockId = $block->id;
				}

				$postBlock = $postBlocks[$postBlockId];

				if(array_key_exists('level', $postBlock))
{'fraction_non_alphanumeric': 0.17195767195767195, 'fraction_numerical': 0.003779289493575208, 'mean_word_length': 4.362715298885512, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 81, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1059332', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1651, 'n_words': 276}