<commit_before><?php
/**
 * radium: lithium application framework
 *
 * @copyright     Copyright 2013, brünsicke.com GmbH (http://bruensicke.com)
 * @license       http://opensource.org/licenses/BSD-3-Clause The BSD License
 */

namespace radium\extensions\helper;

use lithium\util\Set;
use lithium\core\Libraries;
use lithium\template\TemplateException;

/**
 * Scaffold helper allows easy rendering of CRUD functionality
 *
 * use it in views, like this:
 *
 * echo $this->scaffold->render('index'); // lists a table of all objects
 * echo $this->scaffold->render('view'); // shows current object details
 * echo $this->scaffold->render('form'); // shows form for current object
 */
class Scaffold extends \lithium\template\Helper {

	protected $_data = array();

	protected $_scaffold = array();

	/**
	 * initialize and check for li3_mustache library
	 *
	 */
	protected function _init() {
		parent::_init();
		$this->mustache = (bool) Libraries::get('li3_mustache');
		$this->_data = $this->_context->data();
		if (isset($this->_data['scaffold'])) {
			$this->_scaffold = $this->_data['scaffold'];
		}
		if (isset($this->_data[$this->_scaffold['singular']])) {
			$this->_scaffold['object'] = $this->_data[$this->_scaffold['singular']];
		}
		if (isset($this->_data[$this->_scaffold['plural']])) {
			$this->_scaffold['objects'] = $this->_data[$this->_scaffold['plural']];
		}
	}

	/**
	 * render scaffold-templates
	 *
	 * @param string $name name of template to render, possible options are:
	 *        `index`, `form`, `form.meta`, `form.config`, `errors`, `view`
	 * @param array $data additional data to be passed into template
	 * @param array $options additional options
	 *        - `mustache`: set to false to disable mustache-rendering, defaults to true
	 * @return string rendered html of template
	 */
	public function render($name, array $data = array(), array $options = array()) {
		$defaults = array('mustache' => true);
		$options += $defaults;
		$data = $this->_data($data);
		$scaffold = $data['scaffold'];

		if ($this->mustache && $options['mustache']) {
			switch ($name) {
				case 'index':
					$data['objects'] = (isset($data[$scaffold['plural']])
										&& is_callable(array($data[$scaffold['plural']], 'data')))
						? array_values($data[$scaffold['plural']]->data())
						: array();
				break;
				case 'form':
				case 'form.meta':
				case 'form.fields':
				case 'form.config':
					$options['mustache'] = false;
				break;
				case 'errors':
					$data['errors'] = (isset($data['errors']))
						? $this->data($data['errors'], array('flatten' => false))
						: array();
				break;
			}
		}
		try {
			$template = sprintf('%s/%s', $scaffold['plural'], $name);
			$method = ($this->mustache && $options['mustache']) ? '_mustache' : '_element';
			return $this->$method($template, $data, $options);
		} catch (TemplateException $e) {
			// $options['library'] = 'radium';
			if ($this->mustache && $options['mustache']) {
				if ($name === 'view') {
					$data['data'] = $this->data($data[$scaffold['singular']]->data());
				}
				try {
					return $this->_mustache($name, $data, $options);
				} catch (TemplateException $e) {

				}
			}
			$element = sprintf('scaffold/%s', $name);
			$options['library'] = 'radium';
			return $this->_element($element, $data, $options);
		}
	}

	public function action($action = 'view', array $args = array()) {
		if (isset($this->_scaffold['object'])) {
			$args += array('id' => $this->_scaffold['object']->id());<commit_msg>return on empty errors (no rendering necessary)<commit_after><?php
/**
 * radium: lithium application framework
 *
 * @copyright     Copyright 2013, brünsicke.<EMAIL> GmbH (http://bruensicke.com)
 * @license       http://opensource.org/licenses/BSD-3-Clause The BSD License
 */

namespace radium\extensions\helper;

use lithium\util\Set;
use lithium\core\Libraries;
use lithium\template\TemplateException;

/**
 * Scaffold helper allows easy rendering of CRUD functionality
 *
 * use it in views, like this:
 *
 * echo $this->scaffold->render('index'); // lists a table of all objects
 * echo $this->scaffold->render('view'); // shows current object details
 * echo $this->scaffold->render('form'); // shows form for current object
 */
class Scaffold extends \lithium\template\Helper {

	protected $_data = array();

	protected $_scaffold = array();

	/**
	 * initialize and check for li3_mustache library
	 *
	 */
	protected function _init() {
		parent::_init();
		$this->mustache = (bool) Libraries::get('li3_mustache');
		$this->_data = $this->_context->data();
		if (isset($this->_data['scaffold'])) {
			$this->_scaffold = $this->_data['scaffold'];
		}
		if (isset($this->_data[$this->_scaffold['singular']])) {
			$this->_scaffold['object'] = $this->_data[$this->_scaffold['singular']];
		}
		if (isset($this->_data[$this->_scaffold['plural']])) {
			$this->_scaffold['objects'] = $this->_data[$this->_scaffold['plural']];
		}
	}

	/**
	 * render scaffold-templates
	 *
	 * @param string $name name of template to render, possible options are:
	 *        `index`, `form`, `form.meta`, `form.config`, `errors`, `view`
	 * @param array $data additional data to be passed into template
	 * @param array $options additional options
	 *        - `mustache`: set to false to disable mustache-rendering, defaults to true
	 * @return string rendered html of template
	 */
	public function render($name, array $data = array(), array $options = array()) {
		$defaults = array('mustache' => true);
		$options += $defaults;
		$data = $this->_data($data);
		$scaffold = $data['scaffold'];

		if ($this->mustache && $options['mustache']) {
			switch ($name) {
				case 'index':
					$data['objects'] = (isset($data[$scaffold['plural']])
										&& is_callable(array($data[$scaffold['plural']], 'data')))
						? array_values($data[$scaffold['plural']]->data())
						: array();
				break;
				case 'form':
				case 'form.meta':
				case 'form.fields':
				case 'form.config':
					$options['mustache'] = false;
				break;
				case 'errors':
					$data['errors'] = (isset($data['errors']))
						? $this->data($data['errors'], array('flatten' => false))
						: array();
					if (empty($data['errors'])) {
						return;
					}
				break;
			}
		}
		try {
			$template = sprintf('%s/%s', $scaffold['plural'], $name);
			$method = ($this->mustache && $options['mustache']) ? '_mustache' : '_element';
			return $this->$method($template, $data, $options);
		} catch (TemplateException $e) {
			// $options['library'] = 'radium';
			if ($this->mustache && $options['mustache']) {
				if ($name === 'view') {
					$data['data'] = $this->data($data[$scaffold['singular']]->data());
				}
				try {
					return $this->_mustache($name, $data, $options);
				} catch (TemplateException $e) {

				}
			}
			$element = sprintf('scaffold/%s', $name);
			$options['library'] = 'radium';
			return $this->_element($element, $data, $options);
		}
	}

	public function action($action = 'view', array $args = array()) {
		if (isset($this->_scaffold['object'])) {
			$args += array('id' => $this->_scaffold['object']->id());
{'fraction_non_alphanumeric': 0.1989867717421897, 'fraction_numerical': 0.0019701660568533634, 'mean_word_length': 3.8545081967213113, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 84, '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': '1530438', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2257, 'n_words': 516}