<?php
    
class attr_mobile_phone extends attr {

    function getDBType() {
    	return "bigint(10)";
    }
    
    function lPost() {
        
        if ( defined('_ADMINKA_') ) {
            $val = preg_replace('/[^0-9]/','', $_POST[$this->attr_name]);
           	$this->obj->set($this->attr_name, $val);
        } else {
            $val = preg_replace('/[^0-9]/','', $_POST[$this->attr_name]);
            if ($val) {
           	    $this->obj->set($this->attr_name, _PHONE_PREFIX . substr($val,-_PHONE_LEN));
            } else {
           	    $this->obj->set($this->attr_name, '');
            }
        }
    }  

    function render_text( $crop = false ) {
	    	$p = $this->getCurVal();
            // Форматиирование телефонов
            switch( strlen($p) ) {
                case 11: // RU + KZ
        	    	return '+' . substr($p,0,1) . '('.substr($p,1,3).')'.substr($p,-(strlen($p) - 4 ));
                break;
                case 12: // BY
        	    	return '+' . substr($p,0,3) . '('.substr($p,3,2).')'.substr($p,-(strlen($p) - 5 ));
                break;
                case 3:
        	    	return $p;
                break;
                case 1:
                    if (!$p) {
                        return '';
                    } else {
                        return $p;
                    }
                default:
        	    	return  '('.substr($p,0,3).')'.substr($p,-(strlen($p) - 3 ));
                break;   
            }
            
    }

    function validate() {
        if ( defined('_ADMINKA_') ) {
            if ( !preg_match('/\d{10,}/', $this->getCurVal() ) ) {
                return 'Введите телефон в формате<br> +[код страны][код города][телефон]';
            }
            return;   
        } else {
            $fulllen = _PHONE_LEN + strlen(_PHONE_PREFIX);
            if ( !preg_match('/\d{'.$fulllen.'}/', $this->getCurVal() ) ) {
                return 'Введите телефон в формате<br> +' . _PHONE_PREFIX . str_repeat('X', _PHONE_LEN);
            }
        }
    }

    function render_input() {
		if (defined('_PHONE_PLACEHOLDER')) {
           $placeholder = _PHONE_PLACEHOLDER;
        } else {
           $placeholder = '+[код страны][код города][телефон]';
        }
		
		return '<input id="' . $this->attr_name . '" type="text" placeholder="'.$placeholder.'" class="form-control" name="' . $this->attr_name . '" value="' .$this->getCurVal() . '" '.($this->obj->attrs[$this->attr_name]['mand']?'required':'').'>';    	
    }

  function getFilter( $sql_where = '' ) {
        
        $fname = $this->obj->obj_name . '_' . $this->attr_name;
        $html = '<input class="form-control form-control-sm" id="_f_'.$fname.'" name="'.$fname.'" value="'.htmlentities($_GET[$fname]).'" onkeypress="int13_filter_'.$fname.'();" onKeyUp>
            <script>
                function int13_filter_'.$fname.'() { 
                    if (window.event.keyCode == 13 || window.event.keyCode == 9) { location.href="'._getCurl(array( $fname, 'curpage' )).'&'.$fname.'=" + $("#_f_'.$fname.'").val(); return true; }
            }
            </script>';
            
        $sql = '';
        if ( $_GET[$fname] ) {
            $sql = ' AND `' . $this->attr_name . '` LIKE "%' . db_escape_string($_GET[$fname]) . '%" ';
        }
        
        return array('html'=> $html, 'sql'=> $sql);
   }


}

?>