<commit_before><?php

if (!defined('__IN_SYMPHONY__')) {
    die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
}

require_once FACE . '/interface.exportablefield.php';
require_once FACE . '/interface.importablefield.php';

class FieldAssociation extends Field implements ExportableField, ImportableField
{
    private static $cache = array();

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

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

        // Default settings
        $this->set('show_column', 'no');
        $this->set('show_association', 'yes');
        $this->set('hide_when_prepopulated', 'no');
        $this->set('required', 'yes');
        $this->set('limit', 20);
        $this->set('related_field_id', array());
    }

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

    public function canFilter()
    {
        return true;
    }

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

    public function allowDatasourceParamOutput()
    {
        return true;
    }

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

    public function fetchSuggestionTypes()
    {
        return array('association');
    }

/*-------------------------------------------------------------------------
    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,
                `relation_id` int(11) unsigned DEFAULT NULL,
                PRIMARY KEY (`id`),
                KEY `entry_id` (`entry_id`),
                KEY `relation_id` (`relation_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
        );
    }

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

    public function set($field, $value)
    {
        if ($field == 'related_field_id' && !is_array($value)) {
            $value = explode(',', $value);
        }
        $this->_settings[$field] = $value;
    }

    public function findOptions(array $selected_ids = array(), $entry_id = null)
    {
        $values = array();
        $limit = $this->get('limit');

        if (!is_array($this->get('related_field_id'))) {
            return $values;
        }

        // find the sections of the related fields
        $sections = Symphony::Database()->fetch(
            "SELECT DISTINCT (s.id), f.id as `field_id`
             FROM `tbl_sections` AS `s`
             LEFT JOIN `tbl_fields` AS `f` ON `s`.id = `f`.parent_section
             WHERE `f`.id IN ('" . implode("','", $this->get('related_field_id')) . "')
             ORDER BY s.sortorder ASC"
        );

        if (is_array($sections) && !empty($sections)) {
            foreach ($sections as $_section) {
                $results = array();

                $section = SectionManager::fetch($_section['id']);
                $group = array(
                    'name' => $section->get('name'),
                    'section' => $section->get('id'),
                    'values' => array()
                );

                if ($limit > 0) {
                    EntryManager::setFetchSorting($section->getSortingField(), $section->getSortingOrder());
                    $entries = EntryManager::fetch(null, $section->get('id'), $limit, 0, null, null, false, false);
                    foreach ($entries as $entry) {
                        $results[] = (int) $entry['id'];
                    }
                }

                // If a value is already selected, exclude it from the list
                $results = array_diff($results, $selected_ids);

                if (is_array($results) && !empty($results)) {
                    $related_values = $this->findRelatedValues($results);
                    foreach ($related_values as $value) {<commit_msg>add sortfield in distinct select<commit_after><?php

if (!defined('__IN_SYMPHONY__')) {
    die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
}

require_once FACE . '/interface.exportablefield.php';
require_once FACE . '/interface.importablefield.php';

class FieldAssociation extends Field implements ExportableField, ImportableField
{
    private static $cache = array();

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

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

        // Default settings
        $this->set('show_column', 'no');
        $this->set('show_association', 'yes');
        $this->set('hide_when_prepopulated', 'no');
        $this->set('required', 'yes');
        $this->set('limit', 20);
        $this->set('related_field_id', array());
    }

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

    public function canFilter()
    {
        return true;
    }

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

    public function allowDatasourceParamOutput()
    {
        return true;
    }

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

    public function fetchSuggestionTypes()
    {
        return array('association');
    }

/*-------------------------------------------------------------------------
    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,
                `relation_id` int(11) unsigned DEFAULT NULL,
                PRIMARY KEY (`id`),
                KEY `entry_id` (`entry_id`),
                KEY `relation_id` (`relation_id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
        );
    }

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

    public function set($field, $value)
    {
        if ($field == 'related_field_id' && !is_array($value)) {
            $value = explode(',', $value);
        }
        $this->_settings[$field] = $value;
    }

    public function findOptions(array $selected_ids = array(), $entry_id = null)
    {
        $values = array();
        $limit = $this->get('limit');

        if (!is_array($this->get('related_field_id'))) {
            return $values;
        }

        // find the sections of the related fields
        $sections = Symphony::Database()->fetch(
            "SELECT DISTINCT (s.id), f.id as `field_id`, s.sortorder 
             FROM `tbl_sections` AS `s`
             LEFT JOIN `tbl_fields` AS `f` ON `s`.id = `f`.parent_section
             WHERE `f`.id IN ('" . implode("','", $this->get('related_field_id')) . "')
             ORDER BY s.sortorder ASC"
        );

        if (is_array($sections) && !empty($sections)) {
            foreach ($sections as $_section) {
                $results = array();

                $section = SectionManager::fetch($_section['id']);
                $group = array(
                    'name' => $section->get('name'),
                    'section' => $section->get('id'),
                    'values' => array()
                );

                if ($limit > 0) {
                    EntryManager::setFetchSorting($section->getSortingField(), $section->getSortingOrder());
                    $entries = EntryManager::fetch(null, $section->get('id'), $limit, 0, null, null, false, false);
                    foreach ($entries as $entry) {
                        $results[] = (int) $entry['id'];
                    }
                }

                // If a value is already selected, exclude it from the list
                $results = array_diff($results, $selected_ids);

                if (is_array($results) && !empty($results)) {
                    $related_values = $this->findRelatedValues($results);
                    foreach ($related_values as $value) {
{'fraction_non_alphanumeric': 0.22883097220720233, 'fraction_numerical': 0.003028009084027252, 'mean_word_length': 1.5406593406593407, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 69, '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': '4983601', 'n_tokens_mistral': 2461, 'n_tokens_neox': 2317, 'n_words': 529}