<commit_before><?php
/*"******************************************************************************************************
*   (c) 2007-2015 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
********************************************************************************************************/

/**
 * A yes-no field renders a dropdown containing a list of entries.
 * Make sure to pass the list of possible entries before rendering the form.
 *
 * @author <EMAIL>
 * @since 4.0
 * @package module_formgenerator
 */
class class_formentry_multiselect extends class_formentry_dropdown {

    protected $arrKeyValues = array();

    /**
     * Renders the field itself.
     * In most cases, based on the current toolkit.
     *
     * @return string
     */
    public function renderField() {
        $objToolkit = class_carrier::getInstance()->getObjToolkit("admin");
        $strReturn = "";
        if($this->getStrHint() != null) {
            $strReturn .= $objToolkit->formTextRow($this->getStrHint());
        }
        $strReturn .= $objToolkit->formInputMultiselect($this->getStrEntryName(), $this->arrKeyValues, $this->getStrLabel(), explode(",", $this->getStrValue()));
        return $strReturn;
    }

    public function setStrValue($strValue) {
        if(is_array($strValue))
            $strValue = implode(",", $strValue);

        return parent::setStrValue($strValue);
    }


    public function validateValue() {
        foreach(explode(",", $this->getStrValue()) as $strOneSelect) {
            if(!in_array($strOneSelect, array_keys($this->arrKeyValues))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns a textual representation of the formentries' value.
     * May contain html, but should be stripped down to text-only.
     *
     * @return string
     */
    public function getValueAsText() {
        $arrSelected = $this->getStrValue();
        if(!is_array($arrSelected))
            $arrSelected = explode(",", $this->getStrValue());

        array_walk($arrSelected, function(&$strValue) {
            $strValue = $this->arrKeyValues[$strValue];
        });

        return implode(", ", $arrSelected);
    }

    /**
     * @param $arrKeyValues
     *
     * @return class_formentry_dropdown
     */
    public function setArrKeyValues($arrKeyValues) {
        $this->arrKeyValues = $arrKeyValues;
        return $this;
    }<commit_msg>FIX: module_system | class_formentry_multiselect > return empty string when strValue is empty because otherwise we get an array with one empty element which in the end produces an notice if the key is not available in arrKeyValues<commit_after><?php
/*"******************************************************************************************************
*   (c) 2007-2015 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
********************************************************************************************************/

/**
 * A yes-no field renders a dropdown containing a list of entries.
 * Make sure to pass the list of possible entries before rendering the form.
 *
 * @author <EMAIL>
 * @since 4.0
 * @package module_formgenerator
 */
class class_formentry_multiselect extends class_formentry_dropdown {

    protected $arrKeyValues = array();

    /**
     * Renders the field itself.
     * In most cases, based on the current toolkit.
     *
     * @return string
     */
    public function renderField() {
        $objToolkit = class_carrier::getInstance()->getObjToolkit("admin");
        $strReturn = "";
        if($this->getStrHint() != null) {
            $strReturn .= $objToolkit->formTextRow($this->getStrHint());
        }
        $strReturn .= $objToolkit->formInputMultiselect($this->getStrEntryName(), $this->arrKeyValues, $this->getStrLabel(), explode(",", $this->getStrValue()));
        return $strReturn;
    }

    public function setStrValue($strValue) {
        if(is_array($strValue))
            $strValue = implode(",", $strValue);

        return parent::setStrValue($strValue);
    }


    public function validateValue() {
        foreach(explode(",", $this->getStrValue()) as $strOneSelect) {
            if(!in_array($strOneSelect, array_keys($this->arrKeyValues))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns a textual representation of the formentries' value.
     * May contain html, but should be stripped down to text-only.
     *
     * @return string
     */
    public function getValueAsText() {
        $arrSelected = $this->getStrValue();
        if(empty($arrSelected))
            return "";

        if(!is_array($arrSelected))
            $arrSelected = explode(",", $this->getStrValue());

        array_walk($arrSelected, function(&$strValue) {
            $strValue = $this->arrKeyValues[$strValue];
        });

        return implode(", ", $arrSelected);
    }

    /**
     * @param $arrKeyValues
     *
     * @return class_formentry_dropdown
     */
    public function setArrKeyValues($arrKeyValues) {
        $this->arrKeyValues = $arrKeyValues;
        return $this;
    }
{'fraction_non_alphanumeric': 0.19153633854645813, 'fraction_numerical': 0.0044158233670653175, 'mean_word_length': 2.555264879005886, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6035585', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1410, 'n_words': 340}