<?php
/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Wengg
 * @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 projects budget plan values
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Budget_plan_values extends Value_object
{
    private $_field_suffixes = array('start_date', 'end_date', 'last_changed_date', 'planned_costs', 'planned_hours', 'proportion', 'threshold');

    protected $project_id;
    protected $is_threshold;
    protected $is_budget_not_bound;

    /**
     * Init the budget plan values from $_POST
     *
     * @return  object  An Budget_plan_values object with all values out of the post array
     */
    public static function init_from_post()
    {
        $obj = new Budget_plan_values();
        $obj->CI = CI_Controller::get_instance();

        $obj->project_id = intval($obj->CI->input->post('budget_plan_project_id'));
        $obj->is_threshold = $obj->CI->input->post('budget_plan_is_threshold') ? true : false;
        $obj->is_budget_not_bound = $obj->CI->input->post('budget_plan_is_budget_not_bound') ? true : false;
        $obj->budget_plan_vars = $obj->_get_budget_plan_vars_post();

        return $obj;
    }

    /**
     * Get an array with the budget plan fields values
     *
     * This function loads the values form $_POST
     *
     * @return  array   Values
     */
    private function _get_budget_plan_vars_post()
    {
        $vars = array();
        $post_keys = preg_grep("@^budget_plan_(".implode('|', $this->_field_suffixes).")_[0-9]+@", array_keys($_POST));
        foreach ($post_keys as $key) {
            if(strpos($key, '_date_') !== false){
                $vars[$key] = new UsDate($this->CI->input->post($key), null, true, null);
            } else {
                $vars[$key] = $this->CI->input->post($key);
            }
        }
        return $vars;
    }

    /**
     * Validate and ensure all values are appropriate
     *
     * @return  mixed   false on success, else the error message
     *
     * @todo    implement!!
     */
    public function validate()
    {
        if (!is_integer($this->project_id)) {
            return 'Project ID has to be an ID';
        }
        return false;
    }

    /**
     * Get project id
     *
     * @return  array
     */
    public function get_project_id()
    {
        return intval($this->project_id);
    }

    /**
     * Get budget plan vars to save
     *
     * @return  array
     */
    public function get_budget_plan_vars_to_save()
    {
        $data = array();
        $keys = preg_grep("@^budget_plan_(".implode('|', $this->_field_suffixes).")_[0-9]+@", array_keys($this->budget_plan_vars));
        foreach ($keys as $key) {
            $a_key = explode('_', $key);
            $work_area_id = intval($a_key[count($a_key)-1]);

            unset($a_key[count($a_key)-1]);
            unset($a_key[0]);
            unset($a_key[1]);
            $field = implode('_', $a_key);

            if (strpos($key, '_date_') !== false) {
                $vars[$key] = new UsDate($this->CI->input->post($key), null, true, null);
                $val = $this->budget_plan_vars[$key]->mysql();
            } elseif ($field === 'proportion' || $field === 'threshold' ) {
                $val = usnumber_to_float($this->budget_plan_vars[$key])/100;
            } else {
                $val = usnumber_to_float($this->budget_plan_vars[$key]);
            }
    
            $data[$work_area_id][$field] = $val;
        }

        foreach ($data as &$val) {
            $val['project_id'] = intval($this->project_id);
        }
        return $data;
    }


    /**
     * Is threshold or not
     *
     * @return  boolean
     */
    public function is_threshold()
    {
        return (boolean) $this->is_threshold;
    }

    /**
     * Is budget not bound or not
     *
     * @return  boolean
     */
    public function is_budget_not_bound()
    {
        return (boolean) $this->is_budget_not_bound;
    }

} // END class Budget_plan_values
