<?php

    class WorkFlow {

        private $con;
        private $esquema_tabla_wf_esquemas;
        private $concepto_tabla_wf_esquemas;
        private $etiqueta_tabla_wf_esquemas;
        private $workflow_tabla_wf_estados;
        private $inicial_tabla_wf_estados;
        private $activo;


        public function __construct($con) {
            $this->con = $con;
        }

        public function set_esquema_tabla_wf_esquemas($esquema_tabla_wf_esquemas) {
            $this->esquema_tabla_wf_esquemas = $esquema_tabla_wf_esquemas;
            return $this;
        }
        public function get_esquema_tabla_wf_esquemas() {
            return $this->esquema_tabla_wf_esquemas;
        }
        public function set_concepto_tabla_wf_esquemas($concepto_tabla_wf_esquemas) {
            $this->concepto_tabla_wf_esquemas = $concepto_tabla_wf_esquemas;
            return $this;
        }
        public function get_concepto_tabla_wf_esquemas() {
            return $this->concepto_tabla_wf_esquemas;
        }
        public function set_etiqueta_tabla_wf_esquemas($etiqueta_tabla_wf_esquemas) {
            $this->etiqueta_tabla_wf_esquemas = $etiqueta_tabla_wf_esquemas;
            return $this;
        }
        public function get_etiqueta_tabla_wf_esquemas() {
            return $this->etiqueta_tabla_wf_esquemas;
        }
        public function set_workflow_tabla_wf_estados($workflow_tabla_wf_estados) {
            $this->workflow_tabla_wf_estados = $workflow_tabla_wf_estados;
            return $this;
        }
        public function get_workflow_tabla_wf_estados() {
            return $this->workflow_tabla_wf_estados;
        }
        public function set_inicial_tabla_wf_estados($inicial_tabla_wf_estados) {
            $this->inicial_tabla_wf_estados = $inicial_tabla_wf_estados;
            return $this;
        }
        public function get_inicial_tabla_wf_estados() {
            return $this->inicial_tabla_wf_estados;
        }
        public function set_activo($activo) {
            $this->activo = $activo;
            return $this;
        }
        public function get_activo() {
            return $this->activo;
        }



        public function select_wf_esquemas() {
            $Schema = new Schema($this->con);
            $val = 0;

            $condiciones = [$this->get_esquema_tabla_wf_esquemas(), $this->get_concepto_tabla_wf_esquemas(), $this->get_etiqueta_tabla_wf_esquemas()];

            //Check if all conditions are not empty
            foreach($condiciones as $key => $value) {
                if(!empty($value)) {
                    $val += 1;
                }
            }

            $row = $Schema->executeQuery('SELECT * FROM WF_ESQUEMAS WHERE ESQUEMA = ? AND CONCEPTO = ? AND ETIQUETA1 = ?', 'iii', $condiciones)->fetch_array();

            return $val < count($condiciones) ? FALSE : $row;
        }


        public function select_wf_estados() {
            $Schema = new Schema($this->con);
            $val = 0;

            $condiciones = [$this->get_workflow_tabla_wf_estados(), $this->get_inicial_tabla_wf_estados(), $this->get_activo()];
            foreach($condiciones as $key => $value) {
                if(!empty($value)) {
                    $val += 1;
                }
            }

            $row = $Schema->executeQuery('SELECT * FROM WF_ESTADOS WHERE WORKFLOW = ? AND INICIAL = ? AND ACTIVO = ?', 'sii', $condiciones)->fetch_array();


            return $val < count($condiciones) ? FALSE : $row;
        }


        private $wf_tabla_wf_historial;
        private $documento_tabla_wf_historial;
        private $estado_tabla_wf_historial;
        private $accion_tabla_wf_historial;
        private $usuario_session_tabla_wf_historial;
        private $actual_tabla_wf_historial;
        private $descripcion_tabla_wf_historial;


        public function set_wf_tabla_wf_historial($wf_tabla_wf_historial) {
            $this->wf_tabla_wf_historial = $wf_tabla_wf_historial;
            return $this;
        }
        public function get_wf_tabla_wf_historial() {
            return $this->wf_tabla_wf_historial;
        }
        public function set_documento_tabla_wf_historial($documento_tabla_wf_historial) {
            $this->documento_tabla_wf_historial = $documento_tabla_wf_historial;
            return $this;
        }
        public function get_documento_tabla_wf_historial() {
            return $this->documento_tabla_wf_historial;
        }
        public function set_estado_tabla_wf_historial($estado_tabla_wf_historial) {
            $this->estado_tabla_wf_historial = $estado_tabla_wf_historial;
            return $this;
        }
        public function get_estado_tabla_wf_historial() {
            return $this->estado_tabla_wf_historial;
        }
        public function set_accion_tabla_wf_historial($accion_tabla_wf_historial) {
            $this->accion_tabla_wf_historial = $accion_tabla_wf_historial;
            return $this;
        }
        public function get_accion_tabla_wf_historial() {
            return $this->accion_tabla_wf_historial;
        }
        public function set_usuario_session_tabla_wf_historial($usuario_session_tabla_wf_historial) {
            $this->usuario_session_tabla_wf_historial = $usuario_session_tabla_wf_historial;
            return $this;
        }
        public function get_usuario_session_tabla_wf_historial() {
            return $this->usuario_session_tabla_wf_historial;
        }
        public function set_actual_tabla_wf_historial($actual_tabla_wf_historial) {
            $this->actual_tabla_wf_historial = $actual_tabla_wf_historial;
            return $this;
        }
        public function get_actual_tabla_wf_historial() {
            return $this->actual_tabla_wf_historial;
        }
        public function set_descripcion_tabla_wf_historial($descripcion_tabla_wf_historial) {
            $this->descripcion_tabla_wf_historial = $descripcion_tabla_wf_historial;
            return $this;
        }
        public function get_descripcion_tabla_wf_historial() {
            return $this->descripcion_tabla_wf_historial;
        }


        public function insert_wf_historial() {
            $Schema = new Schema($this->con);
            $val = 0;

            $condiciones = [
                $this->get_wf_tabla_wf_historial(), 
                $this->get_documento_tabla_wf_historial(), 
                $this->get_estado_tabla_wf_historial(),
                date('Y-m-d H:i:s'),
                $this->get_accion_tabla_wf_historial(),
                $this->get_usuario_session_tabla_wf_historial(),
                $this->get_actual_tabla_wf_historial(),
                gethostname() . "(WEB)",
                $this->get_descripcion_tabla_wf_historial()
            ];
            foreach($condiciones as $key => $value) {
                if(!empty($value)) {
                    $val += 1;
                }
            }

            if($val < count($condiciones)) {
                return FALSE;
            } else {
                $Schema->executeQuery('INSERT INTO WF_HISTORIAL(WORKFLOW, DOCUMENTO, ESTADO, FECHA, ACCION, USUARIO, ACTUAL, ORDENADOR, DESCRIPCION) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', 'ssssssiss',  $condiciones);
                return TRUE;
            }


        }
       
    }

?>