<?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 an target_hours entry
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Target_hours_values extends Value_object
{

    protected $from_date;
    protected $to_date;
    protected $norm_work_mon;
    protected $norm_work_tue;
    protected $norm_work_wed;
    protected $norm_work_thu;
    protected $norm_work_fri;
    protected $norm_work_sat;
    protected $norm_work_sun;
    protected $description;

    /**
     * Init the target_hours values from $_POST
     *
     * @return  object  An Target_hours_values object with all values out of the post array
     */
    public static function init_from_post()
    {
        $obj = new Target_hours_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $obj->CI->input->post('staffadmin_nwh_edit');
        $obj->from_date = new UsDate($obj->CI->input->post('staffadmin_nwh_from_date'), null, true, null);
        $obj->to_date = new UsDate($obj->CI->input->post('staffadmin_nwh_to_date'), null, true, null);
        $obj->norm_work_mon = $obj->CI->input->post('staffadmin_nwh_norm_work_mon');
        $obj->norm_work_tue = $obj->CI->input->post('staffadmin_nwh_norm_work_tue');
        $obj->norm_work_wed = $obj->CI->input->post('staffadmin_nwh_norm_work_wed');
        $obj->norm_work_thu = $obj->CI->input->post('staffadmin_nwh_norm_work_thu');
        $obj->norm_work_fri = $obj->CI->input->post('staffadmin_nwh_norm_work_fri');
        $obj->norm_work_sat = $obj->CI->input->post('staffadmin_nwh_norm_work_sat');
        $obj->norm_work_sun = $obj->CI->input->post('staffadmin_nwh_norm_work_sun');
        $obj->description = $obj->CI->input->post('staffadmin_nwh_description');
        return $obj;
    }

     /**
     * Init an Target_hours_values object with all values from a given stdClass object
     *
     * @param   object  $values stdClass object with fee values to initialize a valid Target_hours_values object out of it
     *
     * @return  object  An Target_hours_values object with all values out of the given stdClass object
     */
    public static function init_from_values(stdClass $values)
    {
        $obj = new Target_hours_values();
        $obj->CI = CI_Controller::get_instance();

        $obj->add_or_id = $values->id;
        $obj->from_date = $values->from_date;
        $obj->to_date = $values->to_date;
        $obj->norm_work_mon = float_to_usnumber($values->norm_work_mon);
        $obj->norm_work_tue = float_to_usnumber($values->norm_work_tue);
        $obj->norm_work_wed = float_to_usnumber($values->norm_work_wed);
        $obj->norm_work_thu = float_to_usnumber($values->norm_work_thu);
        $obj->norm_work_fri = float_to_usnumber($values->norm_work_fri);
        $obj->norm_work_sat = float_to_usnumber($values->norm_work_sat);
        $obj->norm_work_sun = float_to_usnumber($values->norm_work_sun);
        if (isset($values->description)) {
            $obj->description = $values->description;
        } else {
            $obj->description = '';
        }
        return $obj;
    }

    /**
     * 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 'Target_hour ID has to be "add" or an ID';
        }
        return false;
    }

    /**
     * Get target_hours values to save
     *
     * @return  array
     */
    public function get_target_hours_to_save()
    {
        $data = array(
            'fr_om_date'         =>  $this->from_date->mysql(),
            'to_date'            =>  $this->to_date ? $this->to_date->mysql() : null,
            'norm_work_mon'      =>  usnumber_to_float($this->norm_work_mon),
            'norm_work_tue'      =>  usnumber_to_float($this->norm_work_tue),
            'norm_work_wed'      =>  usnumber_to_float($this->norm_work_wed),
            'norm_work_thu'      =>  usnumber_to_float($this->norm_work_thu),
            'norm_work_fri'      =>  usnumber_to_float($this->norm_work_fri),
            'norm_work_sat'      =>  usnumber_to_float($this->norm_work_sat),
            'norm_work_sun'      =>  usnumber_to_float($this->norm_work_sun),
            'description'        => (!$this->is_really_empty($this->description)) ? $this->description : '',
        );
        return $data;
    }

} // END class Target_hours_values
