<?php

function _aps_form_main_validate($arr, $return_bool = false, $new_aps = false){
    $ret = array();

    if (isset($arr['DENOMINACION']) || $new_aps){
        $arr['DENOMINACION'] = isset($arr['DENOMINACION']) ? $arr['DENOMINACION'] : "";
        $ret[]= array(
            "FIELD"=>'DENOMINACION',
            "VALIDATE"=> validate($arr['DENOMINACION'],
            array(
                    array("ALPHANUMERIC" => true,"MESSAGE" => "ERROR"),
            ))
        );
    }
    if (isset($arr['DESCRIPCION']) || $new_aps){
        $arr['DESCRIPCION'] = isset($arr['DESCRIPCION']) ? $arr['DESCRIPCION'] : "";
        $ret[]= array(
            "FIELD"=>'DESCRIPCION',
            "VALIDATE"=> validate($arr['DESCRIPCION'],
            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 _add_aps($arr){
    $ret['VALIDATE'] = _aps_form_main_validate($arr, true, true);

    if ($ret['VALIDATE']['_GLOBAL'] ){
       $data = array(
            "Denominacion"=>$arr['DENOMINACION'],
            "Descripcion"=>$arr['DESCRIPCION'],
        );

        $sql = new sqldata();

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


function _update_aps($arr, $cod, $token){
    $ret['VALIDATE'] = _aps_form_main_validate($arr, true);

    if ($ret['VALIDATE']['_GLOBAL'] ){
        _logthis("llega hasta aca");
        $data = array(
            "Denominacion"=>$arr['DENOMINACION'],
            "Descripcion"=>$arr['DESCRIPCION'],
        );

        $sql = new sqldata();

        $sql->update("aps", $data, "ID = ".decrypt($cod, $token) );
        _logthis($sql->last_query());
        $ret = array('STATUS'=> 1, "APS"=> array("EDIT_COD"=>$cod));
        return $ret;
    }
    else{
        $ret['STATUS'] = 0;
        return $ret;
    }
}

function _add_media_file($file, $tipo, $id, $token){
    $sql = new sqldata();

    $aps = $sql->get_tabla("aps", "ID = ".  decrypt($id, $token));
    if ($aps){
        _logthis(print_r(json_decode($file), true) );
        if (is_array(json_decode($file) ) ){
            $file = json_decode($file);
            $arr_return = array();
            foreach($file as $fil){
                $id = $sql->insert("media", array("Path"=>$fil, "Tipo"=>$tipo, "Tabla"=>"aps","TablaID"=>$aps[0]['ID']));
                $arr_return[] = $id;
            }
            return array("STATUS"=>1,"MEDIA"=>array("ID"=>$arr_return) );
        }
        else{
            $id = $sql->insert("media", array("Path"=>$file, "Tipo"=>$tipo, "Tabla"=>"aps","TablaID"=>$aps[0]['ID']));
            return array("STATUS"=>1,"MEDIA"=>array("ID"=>$id) );
        }

        
        
    }
    else{
        return array("STATUS"=>0);
    }

    
    
}


function _link_media_file($media_id, $tabla_id, $tabla_name ){
    $id = $sql->insert("media_all", array("MediaID"=>$media_id, "TablaName"=>$tabla_name, "TablaID"=>$tabla_id));
}

?>
