<commit_before><?php
	
	Class fieldSelect extends Field {
		function __construct(&$parent){
			parent::__construct($parent);
			$this->_name = __('Select Box');
			
			// Set default
			$this->set('show_column', 'no');			
		}

		function canToggle(){
			return ($this->get('allow_multiple_selection') == 'yes' ? false : true);
		}
		
		function allowDatasourceOutputGrouping(){
			## Grouping follows the same rule as toggling.
			return $this->canToggle();
		}
		
		function allowDatasourceParamOutput(){
			return true;
		}		
		
		function canFilter(){
			return true;
		}
		
		public function canImport(){
			return true;
		}
		
		function canPrePopulate(){
			return true;
		}	

		function isSortable(){
			return true;
		}
		
		public function appendFormattedElement(&$wrapper, $data, $encode = false) {
			if (!is_array($data) or empty($data)) return;
			
			$list = new XMLElement($this->get('element_name'));
			
			if (!is_array($data['handle']) and !is_array($data['value'])) {
				$data = array(
					'handle'	=> array($data['handle']),
					'value'		=> array($data['value'])
				);
			}
			
			foreach ($data['value'] as $index => $value) {
				$list->appendChild(new XMLElement(
					'item', General::sanitize($value), array(
						'handle'	=> $data['handle'][$index]
					)
				));
			}

			$wrapper->appendChild($list);
		}		
		
		function fetchAssociatedEntrySearchValue($data){
			if(!is_array($data)) return $data;
			
			return $data['value'];
		}
		
		function fetchAssociatedEntryCount($value){
			return Symphony::Database()->fetchVar('count', 0, "SELECT count(*) AS `count` FROM `tbl_entries_data_".$this->get('id')."` WHERE `value` = '".Symphony::Database()->cleanValue($value)."'");
		}
		
		function fetchAssociatedEntryIDs($value){
			return Symphony::Database()->fetchCol('entry_id', "SELECT `entry_id` FROM `tbl_entries_data_".$this->get('id')."` WHERE `value` = '".Symphony::Database()->cleanValue($value)."'");
		}	
			
		public function getToggleStates() {
			$values = preg_split('/,\s*/i', $this->get('static_options'), -1, PREG_SPLIT_NO_EMPTY);
			
			if ($this->get('dynamic_options') != '') $this->findAndAddDynamicOptions($values);
			
			$values = array_map('trim', $values);
			$states = array();
			
			foreach ($values as $value) {
				$value = General::sanitize($value);
				
				$states[$value] = $value;
			}
			
			return $states;
		}
		
		function toggleFieldData($data, $newState){
			$data['value'] = General::sanitize($newState);
			$data['handle'] = Lang::createHandle($newState);
			return $data;
		}

		function displayPublishPanel(&$wrapper, $data=NULL, $flagWithError=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){
			$states = $this->getToggleStates();
			
			if(!is_array($data['value'])) $data['value'] = array($data['value']);
			
			$options = array();
			
			foreach($states as $handle => $v){
				$options[] = array(General::sanitize($v), in_array($v, $data['value']), $v);
			}
			
			$fieldname = 'fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix;
			if($this->get('allow_multiple_selection') == 'yes') $fieldname .= '[]';
			
			$label = Widget::Label($this->get('label'));
			$label->appendChild(Widget::Select($fieldname, $options, ($this->get('allow_multiple_selection') == 'yes' ? array('multiple' => 'multiple') : NULL)));
			
			if($flagWithError != NULL) $wrapper->appendChild(Widget::wrapFormElementWithError($label, $flagWithError));
			else $wrapper->appendChild($label);		
		}

		function displayDatasourceFilterPanel(&$wrapper, $data=NULL, $errors=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){
			
			parent::displayDatasourceFilterPanel($wrapper, $data, $errors, $fieldnamePrefix, $fieldnamePostfix);
			
			$data = preg_split('/,\s*/i', $data);
			$data = array_map('trim', $data);
			
			$existing_options = $this->getToggleStates();

			if(is_array($existing_options) && !empty($existing_options)){
				$optionlist = new XMLElement('ul');
				$optionlist->setAttribute('class', 'tags');
				
				foreach($existing_options as $option) $optionlist->appendChild(new XMLElement('li', $option));
						
				$wrapper->appendChild($optionlist);
			}
					
		}

		function findAndAddDynamicOptions(&$values){

			if(!is_array($values)) $values = array();

			$sql = "SELECT DISTINCT `value` FROM `tbl_entries_data_".$this->get('dynamic_options')."`
					ORDER BY `value` DESC";
					
			if($results = $this->Database->fetchCol('value', $sql)) $values = array_merge($values, $results);

		}

		function prepareTableValue($data, XMLElement $link=NULL){
			$value = $data['value'];
			
			if(!is_array($value)) $value = array($value);
			
			return parent::prepareTableValue(array('value' => @implode(', ', $value)), $link);
		}

		public function processRawFieldData($data, &$status, $simulate=false, $entry_id=NULL){
			
			$status = self::__OK__;

			if(!is_array($data)) return array('value' => General::sanitize($data), 'handle' => Lang::createHandle($data));

			if(empty($data)) return NULL;
			
			$result = array('value' => array(), 'handle' => array());

			foreach($data as $value){ 
				$result['value'][] = $value;
				$result['handle'][] = Lang::createHandle($value);
			}
			
			return $result;
		}

		public function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation = false) {
			$field_id = $this->get('id');
			
			if (self::isFilterRegex($data[0])) {
				$this->_key++;
				$pattern = str_replace('regexp:', '', $this->cleanValue($data[0]));
				$joins .= "
					LEFT JOIN
						`tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}<commit_msg>Fix ampersands bug whereby selected value is not persisted in the backend. Value was being double-encoded in the backend and DS XML. Fixes #115.<commit_after><?php
	
	Class fieldSelect extends Field {
		function __construct(&$parent){
			parent::__construct($parent);
			$this->_name = __('Select Box');
			
			// Set default
			$this->set('show_column', 'no');			
		}

		function canToggle(){
			return ($this->get('allow_multiple_selection') == 'yes' ? false : true);
		}
		
		function allowDatasourceOutputGrouping(){
			## Grouping follows the same rule as toggling.
			return $this->canToggle();
		}
		
		function allowDatasourceParamOutput(){
			return true;
		}		
		
		function canFilter(){
			return true;
		}
		
		public function canImport(){
			return true;
		}
		
		function canPrePopulate(){
			return true;
		}	

		function isSortable(){
			return true;
		}
		
		public function appendFormattedElement(&$wrapper, $data, $encode = false) {
			if (!is_array($data) or empty($data)) return;
			
			$list = new XMLElement($this->get('element_name'));
			
			if (!is_array($data['handle']) and !is_array($data['value'])) {
				$data = array(
					'handle'	=> array($data['handle']),
					'value'		=> array($data['value'])
				);
			}
			
			foreach ($data['value'] as $index => $value) {
				$list->appendChild(new XMLElement(
					'item',
					General::sanitize($value),
					array(
						'handle'	=> $data['handle'][$index]
					)
				));
			}

			$wrapper->appendChild($list);
		}		
		
		function fetchAssociatedEntrySearchValue($data){
			if(!is_array($data)) return $data;
			
			return $data['value'];
		}
		
		function fetchAssociatedEntryCount($value){
			return Symphony::Database()->fetchVar('count', 0, "SELECT count(*) AS `count` FROM `tbl_entries_data_".$this->get('id')."` WHERE `value` = '".Symphony::Database()->cleanValue($value)."'");
		}
		
		function fetchAssociatedEntryIDs($value){
			return Symphony::Database()->fetchCol('entry_id', "SELECT `entry_id` FROM `tbl_entries_data_".$this->get('id')."` WHERE `value` = '".Symphony::Database()->cleanValue($value)."'");
		}	
			
		public function getToggleStates() {
			$values = preg_split('/,\s*/i', $this->get('static_options'), -1, PREG_SPLIT_NO_EMPTY);
			
			if ($this->get('dynamic_options') != '') $this->findAndAddDynamicOptions($values);
			
			$values = array_map('trim', $values);
			$states = array();
			
			foreach ($values as $value) {
				$value = $value;
				$states[$value] = $value;
			}
			
			return $states;
		}
		
		function toggleFieldData($data, $newState){
			$data['value'] = $newState;
			$data['handle'] = Lang::createHandle($newState);
			return $data;
		}

		function displayPublishPanel(&$wrapper, $data=NULL, $flagWithError=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){
			$states = $this->getToggleStates();
			
			if(!is_array($data['value'])) $data['value'] = array($data['value']);
			
			$options = array();
			
			foreach($states as $handle => $v){
				var_dump($v);
				$options[] = array(General::sanitize($v), in_array($v, $data['value']), $v);
			}
			
			$fieldname = 'fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix;
			if($this->get('allow_multiple_selection') == 'yes') $fieldname .= '[]';
			
			$label = Widget::Label($this->get('label'));
			$label->appendChild(Widget::Select($fieldname, $options, ($this->get('allow_multiple_selection') == 'yes' ? array('multiple' => 'multiple') : NULL)));
			
			if($flagWithError != NULL) $wrapper->appendChild(Widget::wrapFormElementWithError($label, $flagWithError));
			else $wrapper->appendChild($label);		
		}

		function displayDatasourceFilterPanel(&$wrapper, $data=NULL, $errors=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){
			
			parent::displayDatasourceFilterPanel($wrapper, $data, $errors, $fieldnamePrefix, $fieldnamePostfix);
			
			$data = preg_split('/,\s*/i', $data);
			$data = array_map('trim', $data);
			
			$existing_options = $this->getToggleStates();

			if(is_array($existing_options) && !empty($existing_options)){
				$optionlist = new XMLElement('ul');
				$optionlist->setAttribute('class', 'tags');
				
				foreach($existing_options as $option) $optionlist->appendChild(new XMLElement('li', $option));
						
				$wrapper->appendChild($optionlist);
			}
					
		}

		function findAndAddDynamicOptions(&$values){

			if(!is_array($values)) $values = array();

			$sql = "SELECT DISTINCT `value` FROM `tbl_entries_data_".$this->get('dynamic_options')."`
					ORDER BY `value` DESC";
					
			if($results = $this->Database->fetchCol('value', $sql)) $values = array_merge($values, $results);

		}

		function prepareTableValue($data, XMLElement $link=NULL){
			$value = $data['value'];
			
			if(!is_array($value)) $value = array($value);
			
			return parent::prepareTableValue(array('value' => @implode(', ', $value)), $link);
		}

		public function processRawFieldData($data, &$status, $simulate=false, $entry_id=NULL){
			
			$status = self::__OK__;

			if(!is_array($data)) return array('value' => $data, 'handle' => Lang::createHandle($data));

			if(empty($data)) return NULL;
			
			$result = array('value' => array(), 'handle' => array());

			foreach($data as $value){ 
				$result['value'][] = $value;
				$result['handle'][] = Lang::createHandle($value);
			}
			
			return $result;
		}

		public function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation = false) {
			$field_id = $this->get('id');
			
			if (self::isFilterRegex($data[0])) {
				$this->_key++;
				$pattern = str_replace('regexp:', '', $this->cleanValue($data[0]));
				$joins .= "
					LEFT JOIN
						`tbl_entries_data_{$field_id}` AS t{$field_id}_{$this->_key}
{'fraction_non_alphanumeric': 0.1931223965257467, 'fraction_numerical': 0.0009749180182575556, 'mean_word_length': 4.263059701492537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 95, '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': '665194', 'n_tokens_mistral': 4382, 'n_tokens_neox': 3580, 'n_words': 663}