<?php
/**
* @copyright (C) 2013 iJoomla, Inc. - All rights reserved.
* @license GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html)
* @author iJoomla.com <webmaster@ijoomla.com>
* @url https://www.jomsocial.com/license-agreement
* The PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript *are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0
* More info at https://www.jomsocial.com/license-agreement
*/

use Joomla\CMS\Language\Text;

// no direct access
defined('_JEXEC') or die('Restricted access');
require_once (COMMUNITY_COM_PATH.'/libraries/fields/profilefield.php');
class CFieldsList extends CProfileField
{
    public function _translateValue( &$string )
    {
        $string = Text::_( $string );
    }

    /**
     * Method to format the specified value for text type
     **/
    public function getFieldData( $field , $delimiter = '<br/>')
    {
        $value = $field['value'];

        // Since multiple select has values separated by commas, we need to replace it with <br />.
        $fieldArray = explode ( ',' , $value );

        array_walk($fieldArray, array('CFieldsList', '_translateValue'));

        $fieldValue = implode($delimiter, array_filter($fieldArray));
        return $fieldValue;
    }

    public function getFieldHTML( $field , $required )
    {
        $required = ($field->required == 1) ? ' data-required="true"' : '';

        //a fix for wrong data
        $field->value= CStringHelper::trim($field->value);
        if(CStringHelper::strrpos($field->value,',') == (CStringHelper::strlen($field->value) - 1)) {
            $field->value = CStringHelper::substr($field->value,0,-1);
        }

        $lists   = explode(',', $field->value);
        //CFactory::load( 'helpers' , 'string' );
        $html   = '<select id="field'.$field->id.'" name="field' . $field->id . '[]" type="select-multiple" multiple="multiple" class="joms-select joms-select--multiple" title="' . CStringHelper::escape( Text::_( $field->tips ) ) . '" ' . $required . '>';

        $elementSelected    = 0;

        foreach( $field->options as $option )
        {   
            $selected = (in_array( JString::trim( $option ) , $lists ) || in_array( htmlspecialchars((JString::trim( $option ))) , $lists )) ? ' selected="selected"' : '';

            if( empty($selected) )
            {
                $elementSelected++;
            }
            $html   .= '<option value="' . $option . '"' . $selected . '>' . Text::_( $option ) . '</option>';
        }

        if($elementSelected == 0)
        {
            //if nothing is selected, we default the 1st option to be selected.
            $elementName = 'field'.$field->id;
            $html .=<<< HTML

                   <script type='text/javascript'>
                       var slt = document.getElementById('$elementName');
                       if(slt != null){
                          slt.options[0].selected = true;
                       }
                   </script>
HTML;
        }
        $html   .= '</select>';

        return $html;
    }

    public function isValid( $value , $required )
    {
        if( $required && empty($value))
        {
            return false;
        }
        return true;
    }

    public function formatdata( $value )
    {
        $finalvalue = '';
        if(!empty($value))
        {
            foreach($value as $listValue){
                $finalvalue .= $listValue . ',';
            }
        }
        return $finalvalue;
    }
}
