Return false and show message

1

Again asking for help, I leave part of the code:

Driver

$strProyecto = $conex->getIndCol('id', sqlProyectos());//proyectos válidos.
if ($opcion3 == 'save') { 
    $proyecto_id = filter_input(INPUT_POST, 'proyecto_id', FILTER_VALIDATE_INT);//id del proyecto seleccionado
    if ($proyecto_id > 0) {
        $aProyecto = array("proyecto_id"=>$proyecto_id);//id del proyecto seleccionado convertido en array para que sea parámetro de getTareasPendientes
        $aTarea = array();
        $tareasP = Tarea::getTareasPendientes($conex, '', $aProyecto);//datos de tareas pendientes del proyecto seleccionado
        foreach ($tareasP as $ids) {
            $tarea_id = $ids['id'];
            $_Tarea = new Tarea($tarea_id, $conex);
            $cancelar = $_Tarea->cancelarTarea($sys_Usuario, $proyecto_id);//método para cancelar tareas pendientes
            if (!$cancelar == true) {//resultado de cancelarTarea, interrumpe el ciclo y muestra el id de la tarea en la que fue interrumpido
                $error = $tarea_id;
//----------AQUÍ ES DONDE QUIERO LLAMAR EL $msg
                break;
            }
            array_push($aTarea, $tarea_id);//guarda el id de la tarea que fue cancelada, repitiendo el ciclo hasta que se interrumpa
        }
        $resultnum = count($aTarea);
        $resultid = json_encode($aTarea);
        if (empty($error)) $error = null;
        showAviso('Resultado', 'Tareas canceladas: '.$resultnum.' <br>
            ID de tareas canceladas: '.$resultid.' <br>
            No se puede cancelar la tarea ID: '.$error.' <br>
            '.$msg.'', true);
    } 

Model

public function cancelarTarea(Usuario $_Usuario, $proyecto_id) {
    $tarea_id = $this->getId();
    $conex = $_Usuario->getConexBD();
    $idUsuario = $_Usuario->getIdUsuario();
    $_Proyecto = new Proyecto($conex, $proyecto_id);
    $asesor = $_Proyecto->getResponACliente();//array de objetis tipo Usuario
    if(!empty($asesor) && array_key_exists($idUsuario, $asesor)) {
        $conex->query("UPDATE Tareas SET estatus = 'Cancelada' WHERE id = '$tarea_id'");
        regHistorico($conex, 4, 'Tareas', 'estatus -- Cancelada', $tarea_id);
        $result = true;
    }
    else {
        $result = false;
//VARIABLE $msg QUE QUIERO MANDAR AL CONTROLADOR
        $msg = "Usuario no asesor del proyecto";
    }
    return $result;
}

What I want is that within the model there is a variable $msg that shows a message and within the controller to be able to call that variable where I placed the comment //----------AQUÍ ES DONDE QUIERO LLAMAR EL $msg to then use it within showAviso .

I hope you understand and thank you in advance

    
asked by Paloma Alvarado 16.08.2017 в 18:17
source

1 answer

1

A. It would be necessary to modify the model in the following way:

  • Create a variable $msg="" at the beginning which will take the value of the message in case $result is false .
  • Make cancelarTarea return an array, which I called $arrResult . It will have two keys, one could be called msg , which will contain the text of the message, and the other could be called result , which will contain the Boolean value of $result

    public function cancelarTarea(Usuario $_Usuario, $proyecto_id) {
    //Definimos $msg, para no tener un undefined index en caso de que $result sea true
    $msg="";
    
    $tarea_id = $this->getId();
    $conex = $_Usuario->getConexBD();
    $idUsuario = $_Usuario->getIdUsuario();
    $_Proyecto = new Proyecto($conex, $proyecto_id);
    $asesor = $_Proyecto->getResponACliente();//array de objetis tipo Usuario
    if(!empty($asesor) && array_key_exists($idUsuario, $asesor)) {
        $conex->query("UPDATE Tareas SET estatus = 'Cancelada' WHERE id = '$tarea_id'");
        regHistorico($conex, 4, 'Tareas', 'estatus -- Cancelada', $tarea_id);
        $result = true;
    }
    else {
        $result = false;
    // VARIABLE $msg QUE QUIERO MANDAR AL CONTROLADOR
        $msg = "Usuario no asesor del proyecto";
    }
    $arrResult=array("msg"=>$msg, "result"=>$result);
    return $arrResult;
    }
    

B. Then, in the driver

In the variable $cancelar you will have the array returned. And you can obtain and use the value of each key simply with this: $cancelar['result'] to get the value of the key result and $cancelar['msg'] to get the text of the message:

...


            $cancelar = $_Tarea->cancelarTarea($sys_Usuario, $proyecto_id);//método para cancelar tareas pendientes
            if (!$cancelar['result'] == true) {//resultado de cancelarTarea, interrumpe el ciclo y muestra el id de la tarea en la que fue interrumpido
                $error = $tarea_id;
//----------AQUÍ ES DONDE QUIERO LLAMAR EL $msg
                echo $cancelar['msg'];
                break;
            }
    
answered by 16.08.2017 / 18:48
source