<?php
function _usuario_form_other_validate($arr, $return_bool = false, $new_client = false){
    $ret = array();

    if (isset($arr['CIUDAD']) || $new_client){
        $arr['CIUDAD'] = isset($arr['CIUDAD']) ? $arr['CIUDAD'] : "";
        $ret[]= array(
            "FIELD"=>'CIUDAD',
            "VALIDATE"=> validate($arr['CIUDAD'],
            array(
                    array("ALPHANUMERIC" => true,"MESSAGE" => "ERROR"),
            ))
        );
    }

    if (isset($arr['PAIS'])  || $new_client){
        $arr['PAIS'] = isset($arr['PAIS']) ? $arr['PAIS'] : "";
        $ret[] = array(
            "FIELD"=>'PAIS',
            "VALIDATE"=>validate($arr['PAIS'],
                array(
                    array("ALPHANUMERIC" => true,"MESSAGE" => "ERROR"),
                )
            )
        );
    }

    if (isset($arr['PROVINCIA'])  || $new_client){
        $arr['PROVINCIA'] = isset($arr['PROVINCIA']) ? $arr['PROVINCIA'] : "";
        $ret[]= array(
            "FIELD"=>'PROVINCIA',
            "VALIDATE" =>
                validate($arr['PROVINCIA'],
                    array(
                        array("ALPHANUMERIC" => true,"MESSAGE" => "ERROR"),
                    )
                )

        );
    }

    if (isset($arr['DOMICILIO'])  || $new_client){
        $arr['DOMICILIO'] = isset($arr['DOMICILIO']) ? $arr['DOMICILIO'] : "";
        $ret[]= array(
            "FIELD"=>'DOMICILIO',
            "VALIDATE" =>
                validate($arr['DOMICILIO'],
                    array(
                        array("ALPHANUMERIC" => true,"MESSAGE" => "ERROR"),
                    )
            )

        );
    }

    if (isset($arr['CUIT'])  || $new_client){
        $arr['CUIT'] = isset($arr['CUIT']) ? $arr['CUIT'] : "";
        $ret[]= array(
            "FIELD"=>'CUIT',
            "VALIDATE" =>
                validate($arr['CUIT'],
                    array(
                        array("ALPHANUMERIC" => true,"MESSAGE" => "ERROR"),
                    )
                )

        );
    }
    if (isset($arr['CCPP'])  || $new_client){
        $arr['CCPP'] = isset($arr['CCPP']) ? $arr['CCPP'] : "";
        $ret[]= array(
            "FIELD"=>'CCPP',
            "VALIDATE" =>
                validate($arr['CCPP'],
                    array(
                        array("ALPHANUMERIC" => true,"MESSAGE" => "ERROR"),
                    )
                )

        );
    }

    if ($return_bool){
        $brtn = true;
        foreach($ret as $field){
            foreach($field['VALIDATE'] as $validate){
                if ($validate['STATUS']==0){
                    $brtn = false;
                    break;
                }
            }
        }

        $ret['_GLOBAL'] = $brtn;

        return $ret;
    }
    else{
       return $ret ;
    }
}
function _usuario_form_main_validate($arr, $return_bool = false, $new_client = false){
    $ret = array();

    if (isset($arr['EMAIL']) || $new_client){
        $arr['EMAIL'] = isset($arr['EMAIL']) ? $arr['EMAIL'] : "";
        $ret[]= array(
            "FIELD"=>'EMAIL',
            "VALIDATE"=> validate($arr['EMAIL'],
            array(
                array("EMAIL" => true, "MESSAGE"=>"FORMATO DE MAIL INVALIDO"),
                array("EMAIL-EXIST" => $new_client, "MESSAGE"=>"MAIL YA EXISTENTE"),
            ))
        );
    }

    if (isset($arr['NOMBRE'])  || $new_client){
        $arr['NOMBRE'] = isset($arr['NOMBRE']) ? $arr['NOMBRE'] : "";
        $ret[] = array(
            "FIELD"=>'NOMBRE',
            "VALIDATE"=>validate($arr['NOMBRE'],
                array(
                    array("ALPHANUMERIC" => true,"MESSAGE" => "ERROR"),
                    array("MIN_LENGTH"=>5,"MESSAGE" => "DEMASIADO CORTO"),
                    array("MAX_LENGTH"=>10 ,"MESSAGE" => "DEMASIADO LARGO"),
                )
            )
        );
    }

    if (isset($arr['APELLIDO'])  || $new_client){
        $arr['APELLIDO'] = isset($arr['APELLIDO']) ? $arr['APELLIDO'] : "";
        $ret[]= array(
            "FIELD"=>'APELLIDO',
            "VALIDATE" =>
                validate($arr['APELLIDO'],
                    array(
                        array("ALPHANUMERIC" => true,"MESSAGE" => "ERROR"),
                        array("MIN_LENGTH"=>5,"MESSAGE" => "DEMASIADO CORTO"),
                        array("MAX_LENGTH"=>20 ,"MESSAGE" => "DEMASIADO LARGO"),
                    )
                        )

        );
    }

    if ($return_bool){
        $brtn = true;
        foreach($ret as $field){
            foreach($field['VALIDATE'] as $validate){
                if ($validate['STATUS']==0){
                    $brtn = false;
                    break;
                }
            }
        }

        $ret['_GLOBAL'] = $brtn;

        return $ret;
    }
    else{
       return $ret ;
    }
}



function _update_usuario_form_main($arr, $token){
    $ret = array();
    $ret['VALIDATE'] = _usuario_form_main_validate($arr, true);
    if ($ret['VALIDATE']['_GLOBAL'] ){
       $data = array(
            "Email"=>$arr['EMAIL'],
            "Nombre"=>$arr['NOMBRE'],
            "Apellido"=>$arr['APELLIDO']
        );


        $sql = new sqldata();
        $res = $sql->get_tabla("users", "ID = ".  decrypt($arr['COD'], $token));
        if ($res){
            $rtn =$sql->update("users", $data, "ID = ".decrypt($arr['COD'], $token) );
            $ret['STATUS'] = 1;
        }
        else{
            $ret['STATUS'] = 0;
        }
        return $ret;
    }
    else{
        $ret['STATUS'] = 0;
        return $ret;
    }
}


function _password_change($cod, $password_anterior, $password_nuevo){
    $sql = new sqldata();
    $user =  $sql->get_tabla("users", "ID = ".decrypt(trim($cod) )." AND Password = '".$password_anterior."'");

    if ($user){
        $sql->update("users", array("password"=>$password_nuevo), "ID = ".decrypt(trim($cod) ));
        return array("STATUS"=>1) ;
    }
    else{
        return array("STATUS"=>1100) ;
    }

}
function _add_usuario_form_main($arr){
    $ret = array();
    $ret['VALIDATE'] = _usuario_form_main_validate($arr, true, true);

    if ($ret['VALIDATE']['_GLOBAL'] ){
       $data = array(
            "Email"=>$arr['EMAIL'],
            "Nombre"=>$arr['NOMBRE'],
            "Apellido"=>$arr['APELLIDO'],
            "Password"=>$arr['PASSWORD'],
        );

        $sql = new sqldata();

        $rtn =$sql->insert("users", $data);
        $ret['STATUS'] = 1;
        return $ret;
    }
    else{
        $ret['STATUS'] = 0;
        return $ret;
    }
}

function _update_user_form_other($arr, $token){
    $ret = array();
    
    $ret['VALIDATE'] = _usuario_form_other_validate($arr, true);
    
    if ($ret['VALIDATE']['_GLOBAL']){
       $data = array(
            "Ciudad"=>$arr['CIUDAD'],
            "ProvinciaID"=>$arr['PROVINCIA'],
            "PaisID"=>$arr['PAIS'],
            "Domicilio"=>$arr['DOMICILIO'],
            "Ccpp"=>$arr['CCPP'],
            "Cuit"=>$arr['CUIT'],
        );

        $sql = new sqldata();
        $res = $sql->get_tabla("users", "ID = ".  decrypt($arr['COD'], $token));
        if ($res){
            $rtn =$sql->update("users", $data, "ID = ".decrypt($arr['COD']) );
            $ret['STATUS'] = 1;
        }
        else{
            $ret['STATUS'] = 0;
        }
        return $ret;
    }
    else{
        $ret['STATUS'] = 0;
        return $ret;
    }
}

?>
