<?php
class ClassPlay extends ripcord {
  public function __construct(){}
  public function __destruct(){}
  
  private function _getStageId($models, $db, $uid, $password, $stage){
    return $models->execute_kw($db, $uid, $password, 'project.task.type', 'search', array(array(array('name', '=', $stage))))[0];
  }
  
  public function codePushed($models, $db, $uid, $password, $task_id){
    # Check if upload have no error, then encode to base64.
    if ($_FILES['input_file_code_form']['error'] == UPLOAD_ERR_OK
        && is_uploaded_file($_FILES['input_file_code_form']['tmp_name'])) {
      $file_content = file_get_contents($_FILES['input_file_code_form']['tmp_name']);
      $file_base64 = base64_encode($file_content);
    }
    else{
      $res['status'] = 'warning';
      $res['message'] = 'Something went wrong with the file you try to upload. Please try again. If problem persist, <a href="mailto:contact@jmdn-solutions.com">contact us</a>';
      return $res;
    }
    //assign task to user in ERP
    $stage_id = $this->_getStageId($models, $db, $uid, $password, "Approval");
    $res['erp'] = $models->execute_kw(
      $db, $uid, $password, 'project.task', 'write', array(
        array((integer) $task_id),
        array(
          'stage_id'=>$stage_id,
          'file_base64'=>$file_base64,
          'file_name'=>$_FILES['input_file_code_form']['name'],
        )
      )); //the (integer) casting before $task_id is required
    $res['status'] = 'success';
    $res['message'] = 'Well done and thank you for your work. We will review it as soon as possible and let you know if it is approved or if a part of your work needs to be fixed.';
    return $res;
  }

  public function getUnassignedTask($models, $db, $uid, $password, $task_id){
    //assign task to user in ERP
    $res = $models->execute_kw($db, $uid, $password, 'project.task', 'get_task', array(array((integer) $task_id))); //the (integer) casting before $task_id is required
    return $res;
  }
  
  public function listMyCurrentTasks($models, $db, $uid, $password){
    $res = $models->execute_kw($db, $uid, $password, 'project.task', 'search_read', array(array(array('user_id', '=', $uid))), array('fields'=>array()));
    return $res;
  }
  
  public function getMyCurrentTask($models, $db, $uid, $password){
    $stage_id = $this->_getStageId($models, $db, $uid, $password, "Current");
    $res = $models->execute_kw($db, $uid, $password, 'project.task', 'search_read', array(array(array('user_id', '=', $uid), array('stage_id', '=', $stage_id))), array('fields'=>array()));
    return $res;
  }
  
  public function listMyTasksOnApproval($models, $db, $uid, $password){
    $stage_id = $this->_getStageId($models, $db, $uid, $password, "Approval");
    $res = $models->execute_kw($db, $uid, $password, 'project.task', 'search_read', array(array(array('user_id', '=', $uid), array('stage_id', '=', $stage_id))), array('fields'=>array()));
    return $res;
  }
  
  public function listTasksToFix($models, $db, $uid, $password){
    $stage_id = $this->_getStageId($models, $db, $uid, $password, "To Fix");
    $res = $models->execute_kw($db, $uid, $password, 'project.task', 'search_read', array(array(array('user_id', '=', $uid), array('stage_id', '=', $stage_id))), array('fields'=>array()));
    return $res;
  }
  
  public function listMyTasksDone($models, $db, $uid, $password){
    $stage_id = $this->_getStageId($models, $db, $uid, $password, "Done");
    $res = $models->execute_kw($db, $uid, $password, 'project.task', 'search_read', array(array(array('user_id', '=', $uid), array('stage_id', '=', $stage_id))), array('fields'=>array()));
    return $res;
  }
  
  
  public function listUnassignedTasks($models, $db, $uid, $password){
    $res = $models->execute_kw($db, $uid, $password, 'project.task', 'search_read', array(array(array('user_id', '=', false),array('published', '=', true))), array('fields'=>array()));
    return $res;
  }

  public function listFixTasks($models, $db, $uid, $password){
    $res = $models->execute_kw($db, $uid, $password, 'project.task', 'list_to_fix_tasks', array());
    return $res;
  }
  
  public function taskGaveUp($models, $db, $uid, $password, $task_id){
    //unassign task from user in ERP
    $stage_id = $this->_getStageId($models, $db, $uid, $password, "Current");
    $res = $models->execute_kw($db, $uid, $password, 'project.task', 'write', array(array((integer) $task_id), array('stage_id'=>$stage_id, 'user_id'=>false))); //the (integer) casting before $task_id is required
    return $res;
  }

  public function taskGetAttachment($models, $db, $uid, $password, $attachment_id){
    $res = $models->execute_kw($db, $uid, $password, 'ir.attachment', 'read', array((integer) $attachment_id), array('fields'=>["datas", "name"])); //the (integer) casting before $attachment_id is required
    return $res;
  }
  
}