<?php
class ClassWorkflow extends ClassApp {
    public function __construct(){}
    public function __destruct(){}
    
  private function _getWorkflow($workflow){
    $this->_workflow = $workflow;
    $oPDOLink = ClassConfig::databaseConnect();
    $sql = "SELECT * FROM workflow_workflow WHERE name=:name LIMIT 1";
    $execSQL = $oPDOLink->prepare($sql);
    $execSQL->execute(array());
    $row = $execSQL->fetch(PDO::FETCH_ASSOC);
    return $row;
  }
  
  private function _getTransition($workflow, $transition){
    $this->_transition = $transition;
    $this->_workflow = $workflow;
    $oPDOLink = ClassConfig::databaseConnect();
    
    //$wf = $this->_getWorkflow($this->_workflow);
    $sql = "
    SELECT wt.*
    FROM workflow_transition wt
    WHERE code=:transition_code
        AND wf_id=(SELECT id FROM workflow_workflow WHERE name=:workflow_name LIMIT 1)
    LIMIT 1;
    ";
    $execSQL = $oPDOLink->prepare($sql);
    $execSQL->execute(array(':transition_code'=>$this->_transition, ':workflow_name'=>$this->_workflow));
    $row = $execSQL->fetch(PDO::FETCH_ASSOC);
    return $row;
  }
  
  private function _getWorkflowItem($model, $model_id){
    $this->_model = $model;
    $this->_modelId = $model_id;
    
    $oPDOLink = ClassConfig::databaseConnect();
    $sql="
    SELECT *
    FROM workflow_item
    WHERE model=:model
        AND model_id=:model_id
    LIMIT 1
    ";
    $execSQL = $oPDOLink->prepare($sql);
    $execSQL->execute(array(
            ':model'=>$this->_model,
            ':model_id'=>$this->_modelId
    ));
    $row = $execSQL->fetch(PDO::FETCH_ASSOC);
    return $row;
  }
  
    // _createItemHistory($wf_trans, $wf_item, $this->_userId);
    private function _createItemHistory($wf_trans, $wf_item, $user_id){
        $this->_wfTrans = $wf_trans;
        $this->_wfItem = $wf_item;
        $this->_userId = $user_id;		
        $oPDOLink = ClassConfig::databaseConnect();
        
        $sql="
        INSERT INTO workflow_item_history(
            wf_item_id, wf_status_src_id,
            wf_status_dest_id, model, model_id,
            user_id
        ) VALUES (
            :wf_item_id, :wf_status_src_id,
            :wf_status_dest_id,
            :model, :model_id,
            :user_id
        );
        ";
        $execSQL = $oPDOLink->prepare($sql);
        $execSQL->execute(array(
            ':wf_item_id'=>$this->_wfItem['id'],
            ':wf_status_src_id'=>$this->_wfItem['wf_status_id'],
            //':wf_status_src_id'=>$this->_wfTrans['wf_status_src_id'],
            ':wf_status_dest_id'=>$this->_wfTrans['wf_status_dest_id'],
            ':model'=>trim($this->_wfItem['model']),
            ':model_id'=>$this->_wfItem['model_id'],
            ':user_id'=>$this->_userId
        ));
        
    }
    
    public function changeStatus($workflow, $transition, $model, $model_id, $user_id){
        $this->_workflow = $workflow;
        $this->_transition = $transition;
        $this->_model = $model;
        $this->_modelId = $model_id;
        $this->_userId = $user_id;
        $oPDOLink = ClassConfig::databaseConnect() or die();
        
        $wf_trans = ClassWorkflow::_getTransition($this->_workflow, $this->_transition);
        if($wf_trans['create_item'] == '1'){
            $sql="
            INSERT INTO workflow_item(wf_status_id, model, model_id)
            VALUES(:wf_status_id, :model, :model_id);
            ";
            $execSQL = $oPDOLink->prepare($sql);
            if($execSQL->execute(array(
                    ':wf_status_id'=>$wf_trans['wf_status_dest_id'],
                    ':model'=>$this->_model,
                    ':model_id'=>$this->_modelId
            ))){
                $wf_item = ClassWorkflow::_getWorkflowItem($this->_model, $this->_modelId);
                ClassWorkflow::_createItemHistory($wf_trans, $wf_item, $this->_userId);
                
                //update status model/model_id to the new status
                $sql="
                UPDATE ".$this->_model."
                SET wf_status_code=(SELECT code FROM workflow_status WHERE id=:wf_status_dest_id)
                WHERE id=:model_id;
                ";
                $execSQL = $oPDOLink->prepare($sql);
                $execSQL->execute(array(
                    ':wf_status_dest_id'=>$wf_trans['wf_status_dest_id'],
                    ':model_id'=>$this->_modelId
                    ));
                return true;
            }
        } else {
            //TODO finish and test this else instructions
            $wf_item = ClassWorkflow::_getWorkflowItem($this->_model, $this->_modelId);
            $sql="
            UPDATE workflow_item
            SET wf_status_id=:wf_status_id
            WHERE id=:item_id
            ";
            $execSQL = $oPDOLink->prepare($sql);
            if($execSQL->execute(array(
                    ':wf_status_id'=>$wf_trans['wf_status_dest_id'],
                    ':item_id'=>$wf_item['id']))
            ){
                ClassWorkflow::_createItemHistory($wf_trans, $wf_item, $this->_userId);
                
                //update status model/model_id to the new status
                $sql="
                UPDATE ".$this->_model."
                SET wf_status_code=(SELECT code FROM workflow_status WHERE id=:wf_status_dest_id)
                WHERE id=:model_id;
                ";
                $execSQL = $oPDOLink->prepare($sql);
                $execSQL->execute(array(
                    ':wf_status_dest_id'=>$wf_trans['wf_status_dest_id'],
                    ':model_id'=>$this->_modelId
                    ));
                
                return true;
            } else {
                return false;
            }
        }
    }
    
    public function getStatusByItemModelId($model, $wf_item_model_id){
        $this->_model = $model;
        $this->_wfItemModelId = $wf_item_model_id;
        $oPDOLink = ClassConfig::databaseConnect();
        $sql="
        SELECT ws.code, (SELECT code FROM workflow_translation WHERE id=ws.wf_translation_id) AS name
        FROM workflow_status ws
        WHERE ws.id=(SELECT wf_status_id FROM workflow_item WHERE model=:model AND model_id=:wf_item_model_id)
        ";
        $execSQL = $oPDOLink->prepare($sql);
        $execSQL->execute(array(':model'=>$this->_model, ':wf_item_model_id'=>$this->_wfItemModelId));
        $row = $execSQL->fetch(PDO::FETCH_ASSOC);
        return $row;
    }

}
