<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Florian Götz
 * @copyright 2009 untermStrich
 * @package us_libraries
 * @version $Id$
*/

if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

require_once APPPATH.'us_libraries/Value_object.php';

/**
 * Used to save a work package
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Work_packages_values extends Value_object
{
    protected $parent_id=null;
    protected $name=null;
    protected $number=null;
    protected $tag_id=null;
    protected $is_special=null;
    protected $is_special_phase=null;
    protected $is_extern=null;
    protected $cost_centre_id=null;
    protected $description=null;
    
    protected $is_purpose_required=null;
    protected $is_fee_area=null;
    protected $active_calcexp=null;
    protected $budget_start=null;
    protected $budget_end=null;
    protected $status=null;
    protected $budget_threshold=null;
    protected $completion_level=null;
    protected $budget_proportion=null;
    protected $budget_planned_costs=null;
    protected $budget_planned_hours=null;
    protected $fee_costs=null;
    protected $fee_hours=null;
    protected $fee_proportion=null;    
    protected $from_loc=null;    
    protected $category_rate=null;
    protected $is_custom_category_rate=null;
    protected $single_node=null;
    protected $loc_estimation_date=null;    

    /**
     * Init from values
     * 
     * @param   integer     $add_or_id              The item ID or 'add' for adding
     * @param   integer     $parent_id              Parent ID (Used for 'add' only)
     * @param   string      $name                   Name
     * @param   integer     $tag_id                 Optional tag_id
     * @param   boolean     $is_purpose_required    Optional is_purpose_required
     * @param   boolean     $is_fee_area            Optional is_fee_area
     * @param   float       $budget_threshold       Optional budget_threshold
     * @param   integer     $is_special             Optional Special ID
     * @param   integer     $is_special_phase       Optional Special ID Phase (ca. 1-9)
     * @param   integer     $is_extern              Optional Is wp extern
     * @param   integer     $cost_centre_id         Optional Cost centre ID
     * @param   string      $description            Optional The description
     * @param   string      $number                 Optional Number
     * @param   mixed       $status                 Optional Status
     * @return  object
     */
    public static function init_from_values(
        $add_or_id,
        $parent_id,
        $name,
        $tag_id=null,
        $is_purpose_required=null,
        $is_fee_area=null,
        $budget_threshold=null,
        $is_special=0,
        $is_special_phase=null,
        $is_extern=null,
        $cost_centre_id=null,
        $description=null,
        $number=null,
        $status=null
    ) {
        $obj = new Work_packages_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $add_or_id;
        $obj->parent_id = $parent_id;
        $obj->name = $name;
        $obj->number = $number;
        $obj->description = $description;

        if ($cost_centre_id !== null && is_numeric($cost_centre_id)) {
            $obj->cost_centre_id = intval($cost_centre_id);
        } else {
            $obj->cost_centre_id = null;
        }
        if ($tag_id !== null && is_numeric($tag_id)) {
            $obj->tag_id = intval($tag_id);
        } else {
            $obj->tag_id = null;
        }

        if ($is_purpose_required !== null) {
            $obj->is_purpose_required = $is_purpose_required;
        }
        if ($is_fee_area !== null) {
            $obj->is_fee_area = (boolean) $is_fee_area;
        }
        if ($budget_threshold !== null) {
            $obj->budget_threshold = floatval($budget_threshold);
        }
        if ($is_special !== null) {
            $obj->is_special = intval($is_special);
        }
        if ($is_special_phase !== null) {
            $obj->is_special_phase = intval($is_special_phase);
        }
        if ($is_extern !== null) {
            $obj->is_extern = $is_extern;
        }
        $obj->category_rate = null;
        $obj->single_node = true;
        $obj->status = $status;
        return $obj;
    }
    
    /**
     * Init the work package values from $_POST
     *
     * @return  object  A Work_package_values object with all values out of the post array
     */
    public static function init_from_post_rows()
    {
        #echo "<pre>";print_r($_POST);
        $CI = CI_Controller::get_instance();
        
        $rows = array();
        foreach ($CI->input->post('val1') AS $key => $value) {
            $id = substr($key, strripos($key, '_')+1);
            $field_key = substr($key, 0, strripos($key, '_')); //column in DB
            $field_name = substr($field_key, 13);
            
            if (!isset($rows[$id])) {
                $rows[$id] = array();
            }
            $rows[$id][$field_name] = $value;
        }
        
        $reply = array();
        foreach ($rows as $id => $vars) {
            $obj = new Work_packages_values();
            $obj->CI = $CI;
            $obj->add_or_id = $id;
            
            $obj->name = isset($vars['name']) ? $vars['name'] : null;
            $obj->number = isset($vars['number']) ? $vars['number'] : null;
            $obj->description = isset($vars['description']) ? $vars['description'] : null;
            $obj->tag_id = null;
            $obj->is_purpose_required = isset($vars['is_purpose_required']) ? true : false;
            $obj->cost_centre_id = isset($vars['cost_centre']) ? $vars['cost_centre'] : null;
            $obj->is_fee_area = isset($vars['is_fee_area']) ? true : false;
            $obj->active_calcexp = isset($vars['active_calcexp']) ? true : false;
            $obj->budget_start = new UsDate(isset($vars['budget_start']) ? $vars['budget_start'] : false, null, true, null);
            $obj->budget_end = new UsDate(isset($vars['budget_end']) ? $vars['budget_end'] : false, null, true, null);
            $obj->loc_estimation_date = new UsDate(isset($vars['loc_estimation_date']) ? $vars['loc_estimation_date'] : false, null, true, null);
            $obj->status = intval(isset($vars['status']) ? $vars['status'] : false);
            $obj->budget_threshold = isset($vars['budget_threshold']) ? usnumber_to_float($vars['budget_threshold'])/100 : false;
            $obj->completion_level = isset($vars['completion_level']) ? usnumber_to_float($vars['completion_level'])/100 : false;
            $obj->budget_proportion = isset($vars['budget_proportion']) ? usnumber_to_float($vars['budget_proportion'])/100 : false;
            $obj->budget_planned_costs = isset($vars['budget_planned_costs']) ? usnumber_to_float($vars['budget_planned_costs']) : false;
            $obj->budget_planned_hours = isset($vars['budget_planned_hours']) ? usnumber_to_float($vars['budget_planned_hours']) : false;
            $obj->fee_costs = isset($vars['fee_costs']) ? usnumber_to_float($vars['fee_costs']) : false;
            $obj->fee_hours = isset($vars['fee_hours']) ? usnumber_to_float($vars['fee_hours']) : false;
            $obj->fee_proportion = isset($vars['fee_proportion']) ? usnumber_to_float($vars['fee_proportion'])/100 : false;
            $obj->category_rate = isset($vars['category_rate']) ? usnumber_to_float($vars['category_rate']) : false;
            $obj->is_custom_category_rate = isset($vars['is_custom_category_rate']) ? true : false;
            $obj->is_extern = isset($vars['is_extern']) ? true : false;
            
            array_push($reply, $obj);
        }
        
        return $reply;
    }
    
    /**
     * Init the level of completion values from $_POST
     *
     * @return  object  An Level_of_completion_values object with all values out of the post array
     */
    public static function init_from_post_loc()
    {
        $reply = array();

        $post_keys = preg_grep("@^loc_level_[0-9]+@", array_keys($_POST));
        #echo"<pre>";print_r($_POST);die();
        foreach ($post_keys as $key) {
            $id = substr($key, 10);
            $obj = new Work_packages_values();
            $obj->CI = CI_Controller::get_instance();
            $obj->project_id = $obj->CI->input->post('loc_project_id');
            $obj->from_loc = true;
            $obj->add_or_id = substr($key, 10);
            $obj->completion_level = usnumber_to_float($obj->CI->input->post($key))/100;
            if ($obj->completion_level <= 0) {
                $obj->completion_level = false;
            }

            if (!$obj->CI->input->post('loc_date_'.$id)) {
                $obj->loc_estimation_date = null;
            } else {
                $obj->loc_estimation_date = new UsDate($obj->CI->input->post('loc_date_'.$id), null, true);
            }

            array_push($reply, $obj);
        } 
        return $reply;
    }
    
    /**
     * Validate and ensure all values are appropriate
     *
     * @return  mixed   false on success, else the error message
     *
     * @todo    implement!!
     */
    public function validate()
    {
        if ($this->add_or_id!=='add' && !is_numeric($this->add_or_id)) {
            return 'Work packages ID has to be "add" or an ID';
        }
        if ($this->add_or_id==='add') {
            if (!is_numeric($this->parent_id)) {
                return 'Work packages parent ID has to be an ID on "add"';
            }
            if (!isset($this->name) || $this->name==='') {
                return 'Work packages name missing on "add"';
            }
            if (!isset($this->is_purpose_required)) {
                return 'Work packages is_purpose_required missing on "add"';
            }
            if (!isset($this->is_special)) {
                return 'Work packages is_special missing on "add"';
            }
            if (!isset($this->is_fee_area)) {
                return 'Work packages is_fee_area missing on "add"';
            }
            if (!isset($this->budget_threshold)) {
                return 'Work packages budget_threshold missing on "add"';
            }
        }
        
        return false;
    }

    /**
     * Get package to save
     *
     * @return  array
     */
    public function get_values_to_save()
    {
        $data = array();
        
        if (isset($this->name)) {
            $data['name'] = $this->rem_newlines($this->name);
        }
        if (isset($this->number)) {
            $data['wp_number'] = $this->rem_newlines($this->number);
        }
        if (isset($this->description)) {
            $data['description'] = $this->description;
        }
        if (isset($this->tag_id)) {
            $data['tag_id'] = $this->tag_id;
        }
        if (isset($this->cost_centre_id)) {
            $data['cost_centre_id'] = $this->cost_centre_id;
        }
        if (isset($this->is_special)) {
            $data['is_special'] = $this->is_special;
        }
        if (isset($this->is_special_phase)) {
            $data['is_special_phase'] = $this->is_special_phase;
        }
        if (isset($this->is_extern)) {
            $data['is_extern'] = $this->is_extern;
        }
        if (isset($this->active_calcexp)) {
            $data['active_calcexp'] = $this->active_calcexp;
        }
        
        if (isset($this->is_purpose_required)) {
            $data['is_purpose_required'] = $this->is_purpose_required;
        }
        if (isset($this->is_fee_area)) {
            $data['is_fee_area'] = $this->is_fee_area;
        }
        if (isset($this->budget_start)) {
            $data['budget_start'] = $this->budget_start->mysql();
        }
        if (isset($this->budget_end)) {
            $data['budget_end'] = $this->budget_end->mysql();
        }
        if (isset($this->status)) {
            $data['status'] = $this->status;
        }
        if (isset($this->budget_threshold)) {
            $data['budget_threshold'] = $this->budget_threshold;
        }
        if (isset($this->completion_level)) {
            $data['completion_level'] = $this->completion_level;
        }
        if (isset($this->budget_proportion)) {
            $data['budget_proportion'] = $this->budget_proportion;
        }
        if (isset($this->budget_planned_costs)) {
            $data['budget_planned_costs'] = $this->budget_planned_costs;
        }
        if (isset($this->budget_planned_hours)) {
            $data['budget_planned_hours'] = $this->budget_planned_hours;
        }
        if (isset($this->fee_costs)) {
            $data['fee_costs'] = $this->fee_costs;
        }
        if (isset($this->fee_hours)) {
            $data['fee_hours'] = $this->fee_hours;
        }
        if (isset($this->fee_proportion)) {
            $data['fee_proportion'] = $this->fee_proportion;
        }
        if (isset($this->from_loc)) {
            $data['from_loc'] = $this->from_loc;
        }
        if (isset($this->category_rate)) {
            $data['category_rate'] = $this->category_rate;
        }
        if (isset($this->is_custom_category_rate)) {
            $data['is_custom_category_rate'] = $this->is_custom_category_rate;
        }
        if (isset($this->loc_estimation_date)) {
            $data['loc_estimation_date'] = $this->loc_estimation_date;
        }
        if (isset($this->single_node)) {
            $data['single_node'] = $this->single_node;
        } else {
            $data['single_node'] = false; 
        }
        
        return $data;
    }
    
    /**
     * Get values for add mode only
     * 
     * @return  array
     */
    public function get_values_to_add_only()
    {
        $reply = array($this->parent_id, $this->name, $this->tag_id, $this->is_purpose_required, $this->is_special, $this->is_special_phase, $this->is_fee_area, $this->budget_threshold, $this->is_extern, $this->cost_centre_id, $this->description, $this->number ?? '', $this->status ?? Work_packages_model::S_ACTIVE);
        
        if ($reply[4] < 0) {
            $reply[6] = true;
        }
        
        return $reply;
    }
    
    /**
     * Get name
     * 
     * @return string
     */
    public function get_name()
    {
        return $this->name;
    }

    /**
     * Is fee area
     * 
     * @return  boolean
     */
    public function get_is_fee_area()
    {
        return $this->is_fee_area;
    }

    /**
     * Get parent ID
     * 
     * @return  integer
     */
    public function get_parent_id()
    {
        if ($this->add_or_id!=='add') {
            basic_error('get_parent_id is only valid for "add"', null, __CLASS__.'::'.__FUNCTION__);
        }
        return $this->parent_id;
    }
    
} // END class Opentasks_values
