<?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 (work areas) level of completion values
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Level_of_completion_values extends Value_object
{

    protected $project_id;

    /**
     * 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()
    {
        $obj = new Level_of_completion_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->project_id = $obj->CI->input->post('loc_project_id');

        $obj->loc_vars = $obj->_get_loc_vars_post();

        return $obj;
    }

    /**
     * Get an array with the level of completion fields values
     *
     * This function loads the value form $_POST
     *
     * @return  array   Values
     */
    private function _get_loc_vars_post()
    {
        $obj = new Level_of_completion_values();
        $obj->CI = CI_Controller::get_instance();

        $vars = array();
        $post_keys = preg_grep("@^loc_level_[0-9]+@", array_keys($_POST));
        foreach ($post_keys as $key) {
            $vars[$key] = intval($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_numeric($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 level of completion vars to save
     *
     * @return  array
     */
    public function get_level_of_completion_vars_to_save()
    {
        $data = array();

        $keys = preg_grep("@^loc_(level)_[0-9]+@", array_keys($this->loc_vars));
        foreach ($keys as $key) {
            $a_key = explode('_', $key);
            $work_package_id = intval($a_key[count($a_key)-1]);
            $field = $a_key[count($a_key)-2];

            switch($field) {
                case 'level':
                    $val = intval($this->loc_vars[$key])/100;
                    $field = 'completion_level';
                    break;
                default:
                    break;
            }

            $data[$work_package_id][$field] = $val;
        }

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

} // END class Level_of_completion_values
