<commit_before><?php
namespace Craft;

/**
 * Class RelabelPlugin
 *
 * Thank you for using Craft Relabel!
 * @see https://github.com/benjamminf/craft-relabel
 * @package Craft
 */
class RelabelPlugin extends BasePlugin
{
	function getName()
	{
		return Craft::t('Relabel');
	}

	function getVersion()
	{
		return '0.0.1';
	}

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

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

	function getDeveloperUrl()
	{
		return 'http://benf.co';
	}

	public function getDocumentationUrl()
	{
		return 'https://github.com/benjamminf/craft-relabel/blob/master/README.md';
	}

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

		if($this->isCraftRequiredVersion())
		{
			$this->includeResources();
			$this->bindEvent();
		}
	}

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

	protected function includeResources()
	{
		if(craft()->request->isCpRequest() && !craft()->request->isAjaxRequest() && craft()->userSession->isAdmin())
		{
			craft()->templates->includeCssResource('relabel/css/main.css');

			craft()->templates->includeJsResource('relabel/js/Relabel.js');
			craft()->templates->includeJsResource('relabel/js/Editor.js');
			craft()->templates->includeJsResource('relabel/js/EditorModal.js');

			craft()->templates->includeJs('window.Relabel.fields=' . json_encode($this->_getFields()));
			craft()->templates->includeJs('window.Relabel.labels=' . json_encode($this->_getLabels()));
			craft()->templates->includeJs('window.Relabel.layouts=' . json_encode($this->_getLayouts()));

			craft()->templates->includeJsResource('relabel/js/main.js');
		}
	}

	protected function bindEvent()
	{
		craft()->on('fields.saveFieldLayout', function(Event $e)
		{
			$layout = $e->params['layout'];
			$relabel = craft()->request->getPost('relabel');

			if($relabel)
			{
				$transaction = craft()->db->getCurrentTransaction() ? false : craft()->db->beginTransaction();
				try
				{
					foreach($relabel as $fieldId => $labelInfo)
					{
						$label = new RelabelModel();
						$label->fieldId = $fieldId;
						$label->fieldLayoutId = $layout->id;
						$label->name = $labelInfo['name'];
						$label->instructions = $labelInfo['instructions'];

						craft()->relabel->saveLabel($label);
					}

					if($transaction)
					{
						$transaction->commit();
					}
				}
				catch(\Exception $e)<commit_msg>Fixed bug where saving failed if only one of the fields were set<commit_after><?php
namespace Craft;

/**
 * Class RelabelPlugin
 *
 * Thank you for using Craft Relabel!
 * @see https://github.com/benjamminf/craft-relabel
 * @package Craft
 */
class RelabelPlugin extends BasePlugin
{
	function getName()
	{
		return Craft::t('Relabel');
	}

	function getVersion()
	{
		return '0.0.1';
	}

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

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

	function getDeveloperUrl()
	{
		return 'http://benf.co';
	}

	public function getDocumentationUrl()
	{
		return 'https://github.com/benjamminf/craft-relabel/blob/master/README.md';
	}

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

		if($this->isCraftRequiredVersion())
		{
			$this->includeResources();
			$this->bindEvent();
		}
	}

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

	protected function includeResources()
	{
		if(craft()->request->isCpRequest() && !craft()->request->isAjaxRequest() && craft()->userSession->isAdmin())
		{
			craft()->templates->includeCssResource('relabel/css/main.css');

			craft()->templates->includeJsResource('relabel/js/Relabel.js');
			craft()->templates->includeJsResource('relabel/js/Editor.js');
			craft()->templates->includeJsResource('relabel/js/EditorModal.js');

			craft()->templates->includeJs('window.Relabel.fields=' . json_encode($this->_getFields()));
			craft()->templates->includeJs('window.Relabel.labels=' . json_encode($this->_getLabels()));
			craft()->templates->includeJs('window.Relabel.layouts=' . json_encode($this->_getLayouts()));

			craft()->templates->includeJsResource('relabel/js/main.js');
		}
	}

	protected function bindEvent()
	{
		craft()->on('fields.saveFieldLayout', function(Event $e)
		{
			$layout = $e->params['layout'];
			$relabel = craft()->request->getPost('relabel');

			if($relabel)
			{
				$transaction = craft()->db->getCurrentTransaction() ? false : craft()->db->beginTransaction();
				try
				{
					foreach($relabel as $fieldId => $labelInfo)
					{
						$label = new RelabelModel();
						$label->fieldId = $fieldId;
						$label->fieldLayoutId = $layout->id;

						if(array_key_exists('name', $labelInfo))
						{
							$label->name = $labelInfo['name'];
						}

						if(array_key_exists('instructions', $labelInfo))
						{
							$label->instructions = $labelInfo['instructions'];
						}

						craft()->relabel->saveLabel($label);
					}

					if($transaction)
					{
						$transaction->commit();
					}
				}
				catch(\Exception $e)
{'fraction_non_alphanumeric': 0.18604651162790697, 'fraction_numerical': 0.003180282250049692, 'mean_word_length': 3.918866080156403, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 99, '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': '4089114', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1596, 'n_words': 237}