<commit_before><?php
/**
 * @copyright	Copyright 2006-2013, <NAME> - http://milesj.me
 * @license		http://opensource.org/licenses/mit-license.php - Licensed under the MIT License
 * @link		http://milesj.me/code/cakephp/admin
 */

App::uses('Admin', 'Admin.Lib');

class AdminHelper extends AppHelper {

	/**
	 * Helpers.
	 *
	 * @type array
	 */
	public $helpers = array('Html', 'Session', 'Utility.Breadcrumb');

	/**
	 * Filter down fields if the association has a whitelist.
	 *
	 * @param Model $model
	 * @param array $filter
	 * @return array|mixed
	 */
	public function filterFields(Model $model, $filter = array()) {
		if (empty($filter) || $filter === '*') {
			return $model->fields;
		}

		$fields = array();

		foreach ($filter as $field) {
			list($table, $field) = pluginSplit($field);

			$fields[$field] = $model->fields[$field];
		}

		return $fields;
	}

	/**
	 * Return a list of valid callbacks for the model + behaviors and logged in user.
	 *
	 * @param Model $model
	 * @return array
	 */
	public function getBehaviorCallbacks(Model $model) {
		$callbacks = array();
		$behaviors = Configure::read('Admin.behaviorCallbacks');

		foreach ($behaviors as $behavior => $methods) {
			if (!$model->Behaviors->loaded($behavior)) {
				continue;
			}

			foreach ($methods as $method => $options) {
				if (is_string($options)) {
					$options = array('title' => $options, 'access' => 'update');
				} else {
					$options = $options += array('access' => 'update');
				}

				if ($this->hasAccess($model->qualifiedName, $options['access'])) {
					$callbacks[$method] = array(
						'title' => __d('admin', $options['title'], $model->singularName),
						'behavior' => Inflector::underscore($behavior),
						'method' => $method
					);
				}
			}
		}

		return $callbacks;
	}

	/**
	 * Generate a nested list of dependencies by looping and drilling down through all the model associations.
	 *
	 * @param Model $model
	 * @param int $depth
	 * @return array
	 */
	public function getDependencies(Model $model, $depth = 0) {
		$dependencies = array();

		if ($depth >= 5) {
			return $dependencies;
		}

		foreach (array($model->hasOne, $model->hasMany, $model->hasAndBelongsToMany) as $assocGroup) {
			foreach ($assocGroup as $alias => $assoc) {
				// hasOne, hasMany
				if (isset($assoc['dependent']) && $assoc['dependent']) {
					$class = $assoc['className'];

				// hasAndBelongsToMany
				} else if (isset($assoc['joinTable'])) {
					$class = $assoc['with'];

				} else {
					continue;
				}

				$dependencies[] = array(
					'alias' => $alias,
					'model' => $class,
					'dependencies' => $this->getDependencies($this->introspect($class), ($depth + 1))
				);
			}
		}

		return $dependencies;
	}

	/**
	 * Return the display field. If field does not exist, use the ID.
	 *
	 * @param Model $model
	 * @param array $result
	 * @return string
	 */
	public function getDisplayField(Model $model, $result) {
		$displayField = $result[$model->alias][$model->displayField];

		if (!$displayField) {
			$displayField = $this->Html->tag('span', '(' . __d('admin', 'No Title') . ')', array(
				'class' => 'text-warning'
			));
		}

		return $displayField;
	}

	/**
	 * Return a list of valid callbacks for the model and logged in user.
	 *
	 * @param Model $model
	 * @param string $scope
	 * @return array
	 */
	public function getModelCallbacks(Model $model, $scope = '*') {
		$callbacks = array();
		$models = Configure::read('Admin.modelCallbacks');

		if (isset($models[$model->qualifiedName])) {
			foreach ($models[$model->qualifiedName] as $method => $options) {
				if (is_string($options)) {
					$options = array('title' => $options, 'access' => 'update', 'scope' => '*');
				} else {
					$options = $options += array('access' => 'update', 'scope' => '*');
				}

				if ($options['scope'] !== $scope) {
					continue;
				}

				if ($this->hasAccess($model->qualifiedName, $options['access'])) {
					$callbacks[$method] = __d('admin', $options['title'], $model->singularName);
				}
			}
		}

		return $callbacks;
	}

	/**
	 * Return a list of valid links for a record
	 *
	 * @param Model $model
	 * @param string $scope
	 * @return array
	 */
	public function getModelLinks(Model $model) {
		$models = Configure::read('Admin.modelLinks');

		if (isset($models[$model->qualifiedName])) {
			return $models[$model->qualifiedName];
		}

		return array();
	}

	/**
	 * Return a list of models grouped by plugin, to use in the navigation menu.
	 *
	 * @return array
	 */
	public function getNavigation() {
		$plugins = Admin::getModels();
		$navigation = array();

		foreach ($plugins as $plugin) {
			$models = array();

			foreach ($plugin['models'] as $model) {
				if ($model['installed']) {<commit_msg>phpdoc<commit_after><?php
/**
 * @copyright	Copyright 2006-2013, <NAME> - http://milesj.me
 * @license		http://opensource.org/licenses/mit-license.php - Licensed under the MIT License
 * @link		http://milesj.me/code/cakephp/admin
 */

App::uses('Admin', 'Admin.Lib');

class AdminHelper extends AppHelper {

	/**
	 * Helpers.
	 *
	 * @type array
	 */
	public $helpers = array('Html', 'Session', 'Utility.Breadcrumb');

	/**
	 * Filter down fields if the association has a whitelist.
	 *
	 * @param Model $model
	 * @param array $filter
	 * @return array|mixed
	 */
	public function filterFields(Model $model, $filter = array()) {
		if (empty($filter) || $filter === '*') {
			return $model->fields;
		}

		$fields = array();

		foreach ($filter as $field) {
			list($table, $field) = pluginSplit($field);

			$fields[$field] = $model->fields[$field];
		}

		return $fields;
	}

	/**
	 * Return a list of valid callbacks for the model + behaviors and logged in user.
	 *
	 * @param Model $model
	 * @return array
	 */
	public function getBehaviorCallbacks(Model $model) {
		$callbacks = array();
		$behaviors = Configure::read('Admin.behaviorCallbacks');

		foreach ($behaviors as $behavior => $methods) {
			if (!$model->Behaviors->loaded($behavior)) {
				continue;
			}

			foreach ($methods as $method => $options) {
				if (is_string($options)) {
					$options = array('title' => $options, 'access' => 'update');
				} else {
					$options = $options += array('access' => 'update');
				}

				if ($this->hasAccess($model->qualifiedName, $options['access'])) {
					$callbacks[$method] = array(
						'title' => __d('admin', $options['title'], $model->singularName),
						'behavior' => Inflector::underscore($behavior),
						'method' => $method
					);
				}
			}
		}

		return $callbacks;
	}

	/**
	 * Generate a nested list of dependencies by looping and drilling down through all the model associations.
	 *
	 * @param Model $model
	 * @param int $depth
	 * @return array
	 */
	public function getDependencies(Model $model, $depth = 0) {
		$dependencies = array();

		if ($depth >= 5) {
			return $dependencies;
		}

		foreach (array($model->hasOne, $model->hasMany, $model->hasAndBelongsToMany) as $assocGroup) {
			foreach ($assocGroup as $alias => $assoc) {
				// hasOne, hasMany
				if (isset($assoc['dependent']) && $assoc['dependent']) {
					$class = $assoc['className'];

				// hasAndBelongsToMany
				} else if (isset($assoc['joinTable'])) {
					$class = $assoc['with'];

				} else {
					continue;
				}

				$dependencies[] = array(
					'alias' => $alias,
					'model' => $class,
					'dependencies' => $this->getDependencies($this->introspect($class), ($depth + 1))
				);
			}
		}

		return $dependencies;
	}

	/**
	 * Return the display field. If field does not exist, use the ID.
	 *
	 * @param Model $model
	 * @param array $result
	 * @return string
	 */
	public function getDisplayField(Model $model, $result) {
		$displayField = $result[$model->alias][$model->displayField];

		if (!$displayField) {
			$displayField = $this->Html->tag('span', '(' . __d('admin', 'No Title') . ')', array(
				'class' => 'text-warning'
			));
		}

		return $displayField;
	}

	/**
	 * Return a list of valid callbacks for the model and logged in user.
	 *
	 * @param Model $model
	 * @param string $scope
	 * @return array
	 */
	public function getModelCallbacks(Model $model, $scope = '*') {
		$callbacks = array();
		$models = Configure::read('Admin.modelCallbacks');

		if (isset($models[$model->qualifiedName])) {
			foreach ($models[$model->qualifiedName] as $method => $options) {
				if (is_string($options)) {
					$options = array('title' => $options, 'access' => 'update', 'scope' => '*');
				} else {
					$options = $options += array('access' => 'update', 'scope' => '*');
				}

				if ($options['scope'] !== $scope) {
					continue;
				}

				if ($this->hasAccess($model->qualifiedName, $options['access'])) {
					$callbacks[$method] = __d('admin', $options['title'], $model->singularName);
				}
			}
		}

		return $callbacks;
	}

	/**
	 * Return a list of valid links for a record
	 *
	 * @param Model $model
	 * @return array
	 */
	public function getModelLinks(Model $model) {
		$models = Configure::read('Admin.modelLinks');

		if (isset($models[$model->qualifiedName])) {
			return $models[$model->qualifiedName];
		}

		return array();
	}

	/**
	 * Return a list of models grouped by plugin, to use in the navigation menu.
	 *
	 * @return array
	 */
	public function getNavigation() {
		$plugins = Admin::getModels();
		$navigation = array();

		foreach ($plugins as $plugin) {
			$models = array();

			foreach ($plugin['models'] as $model) {
				if ($model['installed']) {
{'fraction_non_alphanumeric': 0.18023255813953487, 'fraction_numerical': 0.002325581395348837, 'mean_word_length': 3.349885057471264, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 91, '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': '5366047', 'n_tokens_mistral': 3570, 'n_tokens_neox': 2938, 'n_words': 755}