<?php


/**
 * Тип за добавяне за качване на няколко файла едновременно
 *
 * @category  bgerp
 * @package   fileman
 *
 * @author    Yusein Yuseinov <yyuseinov@gmail.com>
 * @copyright 2006 - 2017 Experta OOD
 * @license   GPL 3
 *
 * @since     v 0.1
 */
class fileman_type_Files extends type_Keylist
{
    /**
     * Инициализиране на обекта
     */
    public function init($params = array())
    {
        $params['params']['mvc'] = 'fileman_Files';
        
        setIfNot($params['params']['select'], 'name');
        
        parent::init($params);
    }
    
    
    /**
     * Конвертира стойността от вербална към (int)
     *
     * @param mixed $value
     *
     * @see type_Keylist::fromVerbal_()
     *
     * @return mixed
     */
    public function fromVerbal_($value)
    {
        $nVal = arr::make($value);
        
        $rVal = array();
        
        foreach ($nVal as $fileHnd) {
            $fId = fileman_Files::fetchByFh($fileHnd, 'id');
            
            if (!$fId) {
                continue;
            }
            
            $rVal[$fId] = $fId;
        }
        
        try {
            $res = self::fromArray($rVal, false);
        } catch (core_exception_Expect $e) {
            $this->error = $e->getMessage();
            $res = false;
        }

        return $res;
    }
    
    
    /**
     * Обръща стойността в хидън
     *
     * @param string $value
     *
     * @return mixed stdClass, array, string, ...
     */
    public function toHidden($value)
    {
        if(!empty($value) && keylist::isKeylist($value)){
            $valueArr = $this->toArray($value);
            $newValueArr = array();
            foreach ($valueArr as $id) {
                $newValueArr[] = fileman_Files::fetchField($id, 'fileHnd');
            }
            
            return implode(',', $newValueArr);
        }
        
        return parent::toHidden($value);
    }
    
    
    /**
     * @todo Чака за документация...
     */
    public function toVerbal($fhList)
    {
        if (fileman::isFileHnd($fhList)) {
            $fhList = '|' . fileman::fhToId($fhList) . '|';
        }
        
        $fhArr = $this->toArray($fhList);
        
        $res = '';
        
        foreach ($fhArr as $id) {
            $fh = fileman_Files::fetchField($id, 'fileHnd');
            if (isset($fh)) {
                if ($this->params['showButton']) {
                    $fh = fileman_Files::fetchField($id, 'fileHnd');
                    if (isset($fh)) {
                        $fRec = fileman::fetchByFh($fh);
                        $ext = fileman_Files::getExt($fRec->name);
                        $icon = "fileman/icons/16/{$ext}.png";
                        if (!is_file(getFullPath($icon))) {
                            $icon = 'fileman/icons/16/default.png';
                        }
                        $url = array();
                        if (fileman_Files::haveRightFor('single', $fRec)) {
                            $url = array('fileman_Files', 'single', $fh);
                        }

                        $res .= ht::createBtn('|*' . core_String::limitLen($fRec->name, $this->params['showButton'], 3), $url, false, false, array('ef_icon' => $icon));
                    }
                } else {
                    $res .= fileman_Files::getLink($fh);
                }
            }
        }
        if (!$res) {
            
            return '';
        }
        
        $align = $this->params['align'] ? $this->params['align'] : 'horizontal';
        $align = 'align_' . $align;
        $res = "<span class='{$align}' style='padding-bottom:5px'>" . $res . '</span>';
        
        return $res;
    }
    
    
    /**
     *
     *
     * @param string $name
     * @param string $value
     * @param array  $attr
     *
     * @see type_Keylist::renderInput_()
     *
     * @return core_ET
     */
    public function renderInput_($name, $value = '', &$attr = array())
    {
        $Files = cls::get('fileman_Files');
        
        unset($attr['ondblclick']);
        
        $attrInp = $attr;
        $attrInp['id'] = $name . '_files_name_id';
        $align = $this->params['align'] ? $this->params['align'] : 'horizontal';
        $attrInp['class'] .= $attr['class'] . ' input_align_' . $align;
        
        $valueFhArr = array();
        if(is_array($value)) {
            $value = self::fromArray($value, false);
        }
        if (fileman::isFileHnd($value)) {
            $value = '|' . fileman::fhToId($value) . '|';
        }
        if ($value && (strpos($value, '|') !== false)) {
            $valueArr = $this->toArray($value);
            
            foreach ($valueArr as $vId) {
                $fRec = fileman_Files::fetch($vId);
                
                $valueFhArr[$fRec->fileHnd] = $fRec->fileHnd;
                
                $crossImg = '<img src=' . sbf('img/16/delete.png') . ' align="absmiddle" title="' . tr('Премахване на файла') . '" alt="">';
                $html .= "<span class='{$name}_{$fRec->fileHnd} multipleFiles'>" . $this->toVerbal($vId) . "&nbsp;<a class=\"remove-file-link\" href=\"javascript:void(0)\" onclick=\"unsetOneFile('" . $name . "', '" . $fRec->fileHnd . "')\">" . $crossImg . '</a></span>';
            }
        }
        
        $tpl = ht::createElement('span', $attrInp, $html, true);
        
        $valueStr = implode(',', $valueFhArr);
        
        $tpl->append("<input name='{$name}' value='{$valueStr}' id='{$name}_id' type='hidden'>");
        
        $bucket = $this->params['bucket'];
        
        $bucketId = fileman_Buckets::fetchByName($bucket);
        
        expect($bucketId, 'Очаква се валидна кофа', $bucket);
        
        $tpl->prepend($Files->makeBtnToAddFile('+', $bucketId, 'placeFile_setInputFile' . $name, array('class' => 'noicon ' . $attrInp['class'] . '_btn', 'title' => 'Добавяне или промяна на файлове')));
        
        $this->addJavascript($tpl, $name);
        
        return $tpl;
    }
    
    
    /**
     * @todo Чака за документация...
     */
    public function addJavascript($tpl, $name)
    {
        $tpl->appendOnce("
            function placeFile_setInputFile(name, fh, fName) {

                var inputFileHnd = document.getElementById(name + '_id');
                
                if (inputFileHnd.value.search(fh) == -1) {
                    inputFileHnd.value += inputFileHnd.value ? ',' : '';
                    inputFileHnd.value += fh;
                    
                    var divFileName = document.getElementById(name + '_files_name_id');
                    var crossImg = '<img src=" . sbf('img/16/delete.png') . " align=\"absmiddle\" alt=\"\">';
                    divFileName.innerHTML += '<span class=\"' + name + '_' + fh + ' multipleFiles\">' + getDownloadLink(fName, fh) + 
                    '&nbsp;<a class=\"remove-file-link-new\" href=\"javascript:void(0)\" onclick=\"unsetOneFile(\'' + name + '\', \'' + fh + '\')\">' + crossImg + '</a></span>';
                }
                
                return true;
            }

            function getDownloadLink(fName, fh) {
                var url = '" . toUrl(array('fileman_Download', 'Download', 'fh' => '1')) . "';
                url = url.replace('1', fh);
                var link = '<a href=\"' + url + '\" target=\"_blank\">' + fName + '</a>';

                return link;
            }
        ", 'SCRIPTS');
        
        $tpl->appendOnce("
            function unsetOneFile(name, fh) {
                var spanFileName = document.getElementsByClassName(name + '_' + fh);
                for(var i=0; i < spanFileName.length; i++) {
                    spanFileName[i].innerHTML = '';
                }
                
                var inputFileHnd = document.getElementById(name + '_id');
                var inputVal = inputFileHnd.value;
                inputVal = inputVal.replace(',' + fh, '');
                inputVal = inputVal.replace(fh + ',', '');
                inputVal = inputVal.replace(fh, '');
                inputFileHnd.value = inputVal;
            }
        ", 'SCRIPTS');
        
        $tpl->appendOnce("
            function placeFile_setInputFile{$name}(fh, fName)
            {
                return placeFile_setInputFile('{$name}', fh, fName);
            }
        ", 'SCRIPTS');
    }
    
    
    /**
     *
     *
     * @param string $value
     *
     * @see core_Type::isValid()
     */
    public function isValid($value)
    {
        $res = parent::isValid($value);
        
        if ($this->params['allowedExtensions'] && $value) {
            $vArr = $this->toArray($value);
            
            setIfNot($res, array());
            
            $eArr = explode('|', strtolower($this->params['allowedExtensions']));
            
            foreach ($vArr as $vId) {
                $fRec = fileman::fetch($vId);
                
                $eArr = arr::make($eArr, true);
                
                $ext = fileman::getExt(strtolower($fRec->name));
                
                if (!$eArr[$ext]) {
                    $res['error'] = "Разширението на файла не е в допустимите|*: " . implode(', ', $eArr);
                    
                    if ($res['error']) {
                        $this->error = $res['error'];
                    }
                    
                    break;
                }
            }
        }
        
        if ($value !== null) {
            
            return $res;
        }
    }
}
