<commit_before><?php

class CmsBehavior extends ModelBehavior {

	var $cmsConfigDefault = array(
		'names' => array(
			'section' => false,
			'plural' => false,
			'singular' => false,
		),

		'paginate' => array(
			'limit' => 20,
			'order' => '{model}.id desc',
			'fields' => array()
		),

		'index' => array(
			'home' => true,
			'menu' => true
		),

		'fields' => array(
			'no_add' => array(),
			'no_edit' => array(),
			'hide' => array('lft', 'rght'),
			'export' => array(),
			'no_export' => array(),
			'search' => array(),
			'no_search' => array(),
		),

		'actions' => array(
			'add' => true,
			'edit' => true,
			'add_images' => true,
			'edit_image' => true,
			'edit_file' => true,
			'delete' => true,
			'export' => false,
			'import' => false,
			'search' => false,
			'print' => false,
			'empty' => false,
			'url_view' => array(),
		),

		'fields_rename' => array(),

		'images' => array(),

		'files' => array(),

		'default' => array(),

	);

	var $cmsConfigDefaultImage = array(
		'name_category' => 'Images',
		'sizes' => array(),
		'index' => false,
		'description' => true,
	);

	var $cmsConfigDefaultFile = array(
		'name_category' => 'Files',
		'index' => false,
		'description' => true,
	);


	function setup($Model, $config = array()) {
		$this->cmsConfigInit($Model);
		//pr($Model->brownieCmsConfig);
	}


	function afterFind($Model, $results, $primary) {
		//if ($Model->)
		$results = $this->_addImagePaths($results, $Model);
		$results = $this->_addFilePaths($results, $Model);
		$results = $this->_addUrlView($results, $Model);
		return $results;
	}

	/*
	function beforeSave($Model) {
		pr($Model->data);
		if (!empty($Model->data['BrownieImage'])) {
			$data['BrwImage'] = $Model->data['BrownieImage'];
			$Model->BrwImage->save($data);
		}
	}*/

	function cmsConfigInit($Model) {
		//echo $Model->name;
		//pr($Model->brownieCmsConfig);

		if (empty($Model->brownieCmsConfig['paginate']['fields'])) {
			$Model->brownieCmsConfig['paginate']['fields'] = $this->listableFields($Model);
		}

		if (empty($Model->brownieCmsConfig['paginate']['order']) and !empty($Model->order)) {
			$Model->brownieCmsConfig['paginate']['order'] = $Model->order;
		}

		if (!empty($Model->brownieCmsConfig)) {
			$config = Set::merge($this->cmsConfigDefault, $Model->brownieCmsConfig);
		} else {
			$config = $this->cmsConfig;
		}
		$config['paginate']['order'] = str_replace('{model}', $Model->alias, $config['paginate']['order']);

		if (empty($Model->order)) {
			$Model->order = $config['paginate']['order'];
		}



		$config['names'] = $this->cmsConfigNames($config['names'], $Model);

		if ($config['images']) {
			$Model->bindModel(array('hasMany' => array('BrwImage' => array(
				'foreignKey' => 'record_id',
				'conditions' => array('BrwImage.model' => $Model->name)
			))));
			foreach($config['images'] as $key => $value) {
				$config['images'][$key] = Set::merge($this->cmsConfigDefaultImage, $value);
			}
		}

		if ($config['files']) {
			$Model->bindModel(array('hasMany' => array('BrwFile' => array(
				'foreignKey' => 'record_id',
				'conditions' => array('BrwFile.model' => $Model->name)
			))));
			foreach($config['files'] as $key => $value) {
				$config['files'][$key] = Set::merge($this->cmsConfigDefaultFile, $value);
			}
		}

		$Model->brownieCmsConfig = $config;

		//pr($config);
		//pr($this->cmsConfig);
	}

	function listableFields($Model) {
		$fields = array();
		$i = 0;
		$schema = (array)$Model->_schema;
		foreach($schema as $key => $values) {
			if ($this->isListable($values['type'])) {
				$fields[] = $key;
			}
			if ($i++ > 5) {
				return $fields;
			}
		}
		return $fields;
	}


	function isListable($fieldType) {
		$listableTypes = array('integer', 'float', 'string', 'boolean', 'date', 'datetime', 'time', 'timestamp');
		return in_array($fieldType, $listableTypes);
	}


	function cmsConfigNames($names, $Model) {
		$modelName = Inflector::underscore($Model->name);
		if (empty($names['singular']) or !$names['singular']) {
			$names['singular'] = Inflector::humanize($modelName);
		}
		if (empty($names['plural']) or !$names['plural']) {
			$names['plural'] = Inflector::humanize(Inflector::pluralize($modelName));
		}
		if (empty($names['section']) or !$names['section']) {
			$names['section'] = $names['plural'];
		}
		//pr($names);
		return $names;
	}


	function fieldsAdd($Model) {
		return $this->fieldsForForm($Model, 'add');
	}


	function fieldsEdit($Model) {
		return $this->fieldsForForm($Model, 'edit');
	}

	function fieldsForForm($Model, $action) {
		$schema = $Model->_schema;
		$fieldsConfig = $this->getCmsConfig($Model, 'fields');
		$fieldsNotUsed = array_merge(array('created', 'modified'), $fieldsConfig['no_' . $action], $fieldsConfig['hide']);
		foreach($fieldsNotUsed as $field) {
			if (!empty($schema[$field])) {
				unset($schema[$field]);
			}
		}
		return $schema;
	}


	function _addImagePaths($r, $Model) {
		if (!is_array($r)) {
			return $r;
		}
		foreach ($r as $key => $value) {
			if ($key === 'BrwImage') {
				//echo $key.'<br />';					pr($value);
				$r[$key] = $this->_addBrwImagePaths($value, $Model);
			} else {
				//pr($value); echo '<hr>';
				$r[$key] = $this->_addImagePaths($value, $Model);
			}
		}
		return $r;
	}


	function _addFilePaths($r) {
		if (!is_array($r)) {
			return $r;
		}
		foreach($r as $key => $value) {
			if ($key === 'BrwFile') {
				//echo $key.'<br />';					pr($value);
				$r[$key] = $this->_addBrwFilePaths($value, $Model);
			} else {
				//pr($value); echo '<hr>';
				$r[$key] = $this->_addFilePaths($value, $Model);
			}
		}
		return $r;
	}

	/* this funcion expects an array like
	[BrwImage] => Array(
	    [0] => Array(
	    	[id] => 4a6a23a8-837c-4485-834f-0fa816fac25f
			etc...
	    )
	    [1] => Array(
			[id] => 4a6a23a8-cd5c-4ae9-b69b-0fa816fac25f
			etc...<commit_msg>lack $Model variable in params<commit_after><?php

class CmsBehavior extends ModelBehavior {

	var $cmsConfigDefault = array(
		'names' => array(
			'section' => false,
			'plural' => false,
			'singular' => false,
		),

		'paginate' => array(
			'limit' => 20,
			'order' => '{model}.id desc',
			'fields' => array()
		),

		'index' => array(
			'home' => true,
			'menu' => true
		),

		'fields' => array(
			'no_add' => array(),
			'no_edit' => array(),
			'hide' => array('lft', 'rght'),
			'export' => array(),
			'no_export' => array(),
			'search' => array(),
			'no_search' => array(),
		),

		'actions' => array(
			'add' => true,
			'edit' => true,
			'add_images' => true,
			'edit_image' => true,
			'edit_file' => true,
			'delete' => true,
			'export' => false,
			'import' => false,
			'search' => false,
			'print' => false,
			'empty' => false,
			'url_view' => array(),
		),

		'fields_rename' => array(),

		'images' => array(),

		'files' => array(),

		'default' => array(),

	);

	var $cmsConfigDefaultImage = array(
		'name_category' => 'Images',
		'sizes' => array(),
		'index' => false,
		'description' => true,
	);

	var $cmsConfigDefaultFile = array(
		'name_category' => 'Files',
		'index' => false,
		'description' => true,
	);


	function setup($Model, $config = array()) {
		$this->cmsConfigInit($Model);
		//pr($Model->brownieCmsConfig);
	}


	function afterFind($Model, $results, $primary) {
		//if ($Model->)
		$results = $this->_addImagePaths($results, $Model);
		$results = $this->_addFilePaths($results, $Model);
		$results = $this->_addUrlView($results, $Model);
		return $results;
	}

	/*
	function beforeSave($Model) {
		pr($Model->data);
		if (!empty($Model->data['BrownieImage'])) {
			$data['BrwImage'] = $Model->data['BrownieImage'];
			$Model->BrwImage->save($data);
		}
	}*/

	function cmsConfigInit($Model) {
		//echo $Model->name;
		//pr($Model->brownieCmsConfig);

		if (empty($Model->brownieCmsConfig['paginate']['fields'])) {
			$Model->brownieCmsConfig['paginate']['fields'] = $this->listableFields($Model);
		}

		if (empty($Model->brownieCmsConfig['paginate']['order']) and !empty($Model->order)) {
			$Model->brownieCmsConfig['paginate']['order'] = $Model->order;
		}

		if (!empty($Model->brownieCmsConfig)) {
			$config = Set::merge($this->cmsConfigDefault, $Model->brownieCmsConfig);
		} else {
			$config = $this->cmsConfig;
		}
		$config['paginate']['order'] = str_replace('{model}', $Model->alias, $config['paginate']['order']);

		if (empty($Model->order)) {
			$Model->order = $config['paginate']['order'];
		}



		$config['names'] = $this->cmsConfigNames($config['names'], $Model);

		if ($config['images']) {
			$Model->bindModel(array('hasMany' => array('BrwImage' => array(
				'foreignKey' => 'record_id',
				'conditions' => array('BrwImage.model' => $Model->name)
			))));
			foreach($config['images'] as $key => $value) {
				$config['images'][$key] = Set::merge($this->cmsConfigDefaultImage, $value);
			}
		}

		if ($config['files']) {
			$Model->bindModel(array('hasMany' => array('BrwFile' => array(
				'foreignKey' => 'record_id',
				'conditions' => array('BrwFile.model' => $Model->name)
			))));
			foreach($config['files'] as $key => $value) {
				$config['files'][$key] = Set::merge($this->cmsConfigDefaultFile, $value);
			}
		}

		$Model->brownieCmsConfig = $config;

		//pr($config);
		//pr($this->cmsConfig);
	}

	function listableFields($Model) {
		$fields = array();
		$i = 0;
		$schema = (array)$Model->_schema;
		foreach($schema as $key => $values) {
			if ($this->isListable($values['type'])) {
				$fields[] = $key;
			}
			if ($i++ > 5) {
				return $fields;
			}
		}
		return $fields;
	}


	function isListable($fieldType) {
		$listableTypes = array('integer', 'float', 'string', 'boolean', 'date', 'datetime', 'time', 'timestamp');
		return in_array($fieldType, $listableTypes);
	}


	function cmsConfigNames($names, $Model) {
		$modelName = Inflector::underscore($Model->name);
		if (empty($names['singular']) or !$names['singular']) {
			$names['singular'] = Inflector::humanize($modelName);
		}
		if (empty($names['plural']) or !$names['plural']) {
			$names['plural'] = Inflector::humanize(Inflector::pluralize($modelName));
		}
		if (empty($names['section']) or !$names['section']) {
			$names['section'] = $names['plural'];
		}
		//pr($names);
		return $names;
	}


	function fieldsAdd($Model) {
		return $this->fieldsForForm($Model, 'add');
	}


	function fieldsEdit($Model) {
		return $this->fieldsForForm($Model, 'edit');
	}

	function fieldsForForm($Model, $action) {
		$schema = $Model->_schema;
		$fieldsConfig = $this->getCmsConfig($Model, 'fields');
		$fieldsNotUsed = array_merge(array('created', 'modified'), $fieldsConfig['no_' . $action], $fieldsConfig['hide']);
		foreach($fieldsNotUsed as $field) {
			if (!empty($schema[$field])) {
				unset($schema[$field]);
			}
		}
		return $schema;
	}


	function _addImagePaths($r, $Model) {
		if (!is_array($r)) {
			return $r;
		}
		foreach ($r as $key => $value) {
			if ($key === 'BrwImage') {
				//echo $key.'<br />';					pr($value);
				$r[$key] = $this->_addBrwImagePaths($value, $Model);
			} else {
				//pr($value); echo '<hr>';
				$r[$key] = $this->_addImagePaths($value, $Model);
			}
		}
		return $r;
	}


	function _addFilePaths($r, $Model) {
		if (!is_array($r)) {
			return $r;
		}
		foreach($r as $key => $value) {
			if ($key === 'BrwFile') {
				//echo $key.'<br />';					pr($value);
				$r[$key] = $this->_addBrwFilePaths($value, $Model);
			} else {
				//pr($value); echo '<hr>';
				$r[$key] = $this->_addFilePaths($value, $Model);
			}
		}
		return $r;
	}

	/* this funcion expects an array like
	[BrwImage] => Array(
	    [0] => Array(
	    	[id] => 4a6a23a8-837c-4485-834f-0fa816fac25f
			etc...
	    )
	    [1] => Array(
			[id] => 4a6a23a8-cd5c-4ae9-b69b-0fa816fac25f
			etc...
{'fraction_non_alphanumeric': 0.23015126912229725, 'fraction_numerical': 0.007349799162464746, 'mean_word_length': 3.883973288814691, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 233, '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': '1969860', 'n_tokens_mistral': 4950, 'n_tokens_neox': 4253, 'n_words': 756}