<?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 fee allocation values for projects
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Fee_allocation_values extends Value_object
{
    /**
     * Init the fee allocation values from $_POST
     *
     * @return  object  An Fee_allocation_values object with all values out of the post array
     */
    public static function init_from_post()
    {
        $obj = new Fee_allocation_values();
        $obj->CI = CI_Controller::get_instance();

        $obj->edit = $obj->CI->input->post('fee_allocation_edit');
        $obj->fee_allocation_vars = $obj->_get_fee_allocation_vars_post();
        return $obj;
    }

    /**
     * Get an array with the fee allocation fields values
     *
     * This function loads the value form $_POST
     *
     * @return  array   Values
     */
    private function _get_fee_allocation_vars_post()
    {
        $obj = new Fee_allocation_values();
        $obj->CI = CI_Controller::get_instance();

        $vars = array();
        $vars['percent_sum'] = $this->CI->input->post('fee_allocation_percent_sum');
        $vars['costs_sum'] = $this->CI->input->post('fee_allocation_costs_sum');
        $vars['hours_sum'] = $this->CI->input->post('fee_allocation_hours_sum');
        $vars['is_costs_sum'] = $this->CI->input->post('fee_allocation_is_costs_sum') ? true : false;

        $post_keys = preg_grep("@^fee_allocation_(percent|costs|hours|status)_[0-9]+@", array_keys($_POST));
        foreach ($post_keys as $key) {
            $vars[$key] = $this->CI->input->post($key);
        }
        return $vars;
    }

    /**
     * Get projects fee allocations to save
     *
     * @return  array
     */
    public function get_fee_allocation_vars_to_save()
    {
        $data = array();

        $costs_sum = usnumber_to_float($this->fee_allocation_vars['costs_sum']);
        $is_costs_sum = $this->fee_allocation_vars['is_costs_sum'];

        $keys = preg_grep("@^fee_allocation_(percent|costs|hours|status)_[0-9]+@", array_keys($this->fee_allocation_vars));
        foreach ($keys as $key) {
            $a_key = explode('_', $key);
            $fee_area_id = intval($a_key[count($a_key)-1]);
            $field = $a_key[count($a_key)-2];
            $field = str_replace('percent', 'costs_percent', $field);

            $data[$fee_area_id][$field] = usnumber_to_float($this->fee_allocation_vars[$key]);
        }

        foreach ($data as &$val) {
            $val['costs_sum'] = $costs_sum;
            $val['is_costs_sum'] = $is_costs_sum;
        }
        
        return $data;
    }

    /**
     * Validate and ensure all values are appropriate
     *
     * @return  mixed   false on success, else the error message
     */
    public function validate()
    {
        if ($this->edit!=='true' && $this->edit!=='false') {
            return 'Edit has to be "true" or "false"';
        }
        return false;
    }

    /**
     * Is the object new
     *
     * @return  boolean     New?
     */
    public function isNew()
    {
        return ($this->edit==='false') ? true : false;
    }

} // END class Fee_allocation_values
