<commit_before><?php

	/**
	 * @package toolkit
	 */

	/**
	 * A simple Select field that essentially maps to HTML's `<select/>`. The
	 * options for this field can be static, or feed from another field.
	 */

	Class fieldSelect extends Field {

		public function __construct(){
			parent::__construct();
			$this->_name = __('Select Box');
			$this->_required = true;
			$this->_showassociation = true;

			// Set default
			$this->set('show_column', 'yes');
			$this->set('location', 'sidebar');
			$this->set('required', 'no');
		}

	/*-------------------------------------------------------------------------
		Definition:
	-------------------------------------------------------------------------*/

		public function canToggle(){
			return ($this->get('allow_multiple_selection') == 'yes' ? false : true);
		}

		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;
			}

			if($this->get('sort_options') == 'yes') {
				natsort($states);
			}

			return $states;
		}

		public function toggleFieldData(array $data, $newState, $entry_id=null){
			$data['value'] = $newState;
			$data['handle'] = Lang::createHandle($newState);
			return $data;
		}

		public function canFilter(){
			return true;
		}

		public function canImport(){
			return true;
		}

		public function canPrePopulate(){
			return true;
		}

		public function isSortable(){
			return true;
		}

		public function allowDatasourceOutputGrouping(){
			// Grouping follows the same rule as toggling.
			return $this->canToggle();
		}

		public function allowDatasourceParamOutput(){
			return true;
		}

	/*-------------------------------------------------------------------------
		Setup:
	-------------------------------------------------------------------------*/

		public function createTable(){
			return Symphony::Database()->query("
				CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
				  `id` int(11) unsigned NOT NULL auto_increment,
				  `entry_id` int(11) unsigned NOT NULL,
				  `handle` varchar(255) default NULL,
				  `value` varchar(255) default NULL,
				  PRIMARY KEY  (`id`),
				  KEY `entry_id` (`entry_id`),
				  KEY `handle` (`handle`),
				  KEY `value` (`value`)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			");
		}

	/*-------------------------------------------------------------------------
		Utilities:
	-------------------------------------------------------------------------*/

		public 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)."'");
		}

		public 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 fetchAssociatedEntrySearchValue($data){
			if(!is_array($data)) return $data;

			return $data['value'];
		}

		public function findAndAddDynamicOptions(&$values){
			if(!is_array($values)) $values = array();

			$results = false;

			// Ensure that the table has a 'value' column
			if((boolean)Symphony::Database()->fetchVar('Field', 0, sprintf("
					SHOW COLUMNS FROM `tbl_entries_data_%d` LIKE '%s'
				", $this->get('dynamic_options'), 'value'
			))) {
				$results = Symphony::Database()->fetchCol('value', sprintf("
						SELECT DISTINCT `value`
						FROM `tbl_entries_data_%d`
						ORDER BY `value` ASC
					", $this->get('dynamic_options')
				));
			}

			// In the case of a Upload field, use 'file' instead of 'value'
			if(($results == false) && (boolean)Symphony::Database()->fetchVar('Field', 0, sprintf("
					SHOW COLUMNS FROM `tbl_entries_data_%d` LIKE '%s'
				", $this->get('dynamic_options'), 'file'
			))) {
				$results = Symphony::Database()->fetchCol('file', sprintf("
						SELECT DISTINCT `file`
						FROM `tbl_entries_data_%d`
						ORDER BY `file` ASC
					", $this->get('dynamic_options')
				));
			}

			if($results) {
				if($this->get('sort_options') == 'no') {
					natsort($results);
				}

				$values = array_merge($values, $results);
			}
		}

	/*-------------------------------------------------------------------------
		Settings:
	-------------------------------------------------------------------------*/

		public function findDefaults(array &$settings){
			if(!isset($settings['allow_multiple_selection'])) $settings['allow_multiple_selection'] = 'no';
			if(!isset($settings['show_association'])) $settings['show_association'] = 'no';
			if(!isset($settings['sort_options'])) $settings['sort_options'] = 'no';
		}

		public function displaySettingsPanel(XMLElement &$wrapper, $errors = null) {
			parent::displaySettingsPanel($wrapper, $errors);

			$div = new XMLElement('div', NULL, array('class' => 'group'));

			// Predefined Values
			$label = Widget::Label(__('Predefined Values'));
			$label->appendChild(new XMLElement('i', __('Optional')));
			$input = Widget::Input('fields['.$this->get('sortorder').'][static_options]', General::sanitize($this->get('static_options')));
			$label->appendChild($input);
			$div->appendChild($label);

			// Dynamic Values
			$label = Widget::Label(__('Dynamic Values'));
			$label->appendChild(new XMLElement('i', __('Optional')));

			$sections = SectionManager::fetch(NULL, 'ASC', 'name');
			$field_groups = array();

			if(is_array($sections) && !empty($sections))
				foreach($sections as $section) $field_groups[$section->get('id')] = array('fields' => $section->fetchFields(), 'section' => $section);

			$options = array(
				array('', false, __('None')),
			);

			foreach($field_groups as $group){
				if(!is_array($group['fields'])) continue;

				$fields = array();
				foreach($group['fields'] as $f){
					if($f->get('id') != $this->get('id') && $f->canPrePopulate()) $fields[] = array($f->get('id'), ($this->get('dynamic_options') == $f->get('id')), $f->get('label'));
				}

				if(is_array($fields) && !empty($fields)) $options[] = array('label' => $group['section']->get('name'), 'options' => $fields);
			}

			$label->appendChild(Widget::Select('fields['.$this->get('sortorder').'][dynamic_options]', $options));<commit_msg>Use .two.columns for Select Box field. RE: #1230<commit_after><?php

	/**
	 * @package toolkit
	 */

	/**
	 * A simple Select field that essentially maps to HTML's `<select/>`. The
	 * options for this field can be static, or feed from another field.
	 */

	Class fieldSelect extends Field {

		public function __construct(){
			parent::__construct();
			$this->_name = __('Select Box');
			$this->_required = true;
			$this->_showassociation = true;

			// Set default
			$this->set('show_column', 'yes');
			$this->set('location', 'sidebar');
			$this->set('required', 'no');
		}

	/*-------------------------------------------------------------------------
		Definition:
	-------------------------------------------------------------------------*/

		public function canToggle(){
			return ($this->get('allow_multiple_selection') == 'yes' ? false : true);
		}

		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;
			}

			if($this->get('sort_options') == 'yes') {
				natsort($states);
			}

			return $states;
		}

		public function toggleFieldData(array $data, $newState, $entry_id=null){
			$data['value'] = $newState;
			$data['handle'] = Lang::createHandle($newState);
			return $data;
		}

		public function canFilter(){
			return true;
		}

		public function canImport(){
			return true;
		}

		public function canPrePopulate(){
			return true;
		}

		public function isSortable(){
			return true;
		}

		public function allowDatasourceOutputGrouping(){
			// Grouping follows the same rule as toggling.
			return $this->canToggle();
		}

		public function allowDatasourceParamOutput(){
			return true;
		}

	/*-------------------------------------------------------------------------
		Setup:
	-------------------------------------------------------------------------*/

		public function createTable(){
			return Symphony::Database()->query("
				CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
				  `id` int(11) unsigned NOT NULL auto_increment,
				  `entry_id` int(11) unsigned NOT NULL,
				  `handle` varchar(255) default NULL,
				  `value` varchar(255) default NULL,
				  PRIMARY KEY  (`id`),
				  KEY `entry_id` (`entry_id`),
				  KEY `handle` (`handle`),
				  KEY `value` (`value`)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
			");
		}

	/*-------------------------------------------------------------------------
		Utilities:
	-------------------------------------------------------------------------*/

		public 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)."'");
		}

		public 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 fetchAssociatedEntrySearchValue($data){
			if(!is_array($data)) return $data;

			return $data['value'];
		}

		public function findAndAddDynamicOptions(&$values){
			if(!is_array($values)) $values = array();

			$results = false;

			// Ensure that the table has a 'value' column
			if((boolean)Symphony::Database()->fetchVar('Field', 0, sprintf("
					SHOW COLUMNS FROM `tbl_entries_data_%d` LIKE '%s'
				", $this->get('dynamic_options'), 'value'
			))) {
				$results = Symphony::Database()->fetchCol('value', sprintf("
						SELECT DISTINCT `value`
						FROM `tbl_entries_data_%d`
						ORDER BY `value` ASC
					", $this->get('dynamic_options')
				));
			}

			// In the case of a Upload field, use 'file' instead of 'value'
			if(($results == false) && (boolean)Symphony::Database()->fetchVar('Field', 0, sprintf("
					SHOW COLUMNS FROM `tbl_entries_data_%d` LIKE '%s'
				", $this->get('dynamic_options'), 'file'
			))) {
				$results = Symphony::Database()->fetchCol('file', sprintf("
						SELECT DISTINCT `file`
						FROM `tbl_entries_data_%d`
						ORDER BY `file` ASC
					", $this->get('dynamic_options')
				));
			}

			if($results) {
				if($this->get('sort_options') == 'no') {
					natsort($results);
				}

				$values = array_merge($values, $results);
			}
		}

	/*-------------------------------------------------------------------------
		Settings:
	-------------------------------------------------------------------------*/

		public function findDefaults(array &$settings){
			if(!isset($settings['allow_multiple_selection'])) $settings['allow_multiple_selection'] = 'no';
			if(!isset($settings['show_association'])) $settings['show_association'] = 'no';
			if(!isset($settings['sort_options'])) $settings['sort_options'] = 'no';
		}

		public function displaySettingsPanel(XMLElement &$wrapper, $errors = null) {
			parent::displaySettingsPanel($wrapper, $errors);

			$div = new XMLElement('div', NULL, array('class' => 'two columns'));

			// Predefined Values
			$label = Widget::Label(__('Predefined Values'));
			$label->setAttribute('class', 'column');
			$label->appendChild(new XMLElement('i', __('Optional')));
			$input = Widget::Input('fields['.$this->get('sortorder').'][static_options]', General::sanitize($this->get('static_options')));
			$label->appendChild($input);
			$div->appendChild($label);

			// Dynamic Values
			$label = Widget::Label(__('Dynamic Values'));
			$label->setAttribute('class', 'column');
			$label->appendChild(new XMLElement('i', __('Optional')));

			$sections = SectionManager::fetch(NULL, 'ASC', 'name');
			$field_groups = array();

			if(is_array($sections) && !empty($sections))
				foreach($sections as $section) $field_groups[$section->get('id')] = array('fields' => $section->fetchFields(), 'section' => $section);

			$options = array(
				array('', false, __('None')),
			);

			foreach($field_groups as $group){
				if(!is_array($group['fields'])) continue;

				$fields = array();
				foreach($group['fields'] as $f){
					if($f->get('id') != $this->get('id') && $f->canPrePopulate()) $fields[] = array($f->get('id'), ($this->get('dynamic_options') == $f->get('id')), $f->get('label'));
				}

				if(is_array($fields) && !empty($fields)) $options[] = array('label' => $group['section']->get('name'), 'options' => $fields);
			}

			$label->appendChild(Widget::Select('fields['.$this->get('sortorder').'][dynamic_options]', $options));
{'fraction_non_alphanumeric': 0.26511907444970323, 'fraction_numerical': 0.002704530087897228, 'mean_word_length': 2.7947548460661347, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 111, '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': '5441532', 'n_tokens_mistral': 4652, 'n_tokens_neox': 3851, 'n_words': 815}