<?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_addi.php';

/**
 * Used to save an project participant
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Project_participant_values extends Value_object_addi
{

    protected $project_id;
    protected $address_id;
    protected $address_category_id;
    protected $auto_plan_approv;
    protected $purpose;

    /**
     * Init the project participant values from $_POST
     *
     * @return  object  An Opentasks_values object with all values out of the post array
     */
    public static function init_from_post()
    {
        $obj = new Project_participant_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $obj->CI->input->post('projectadmin_participants_edit');
        $obj->project_id = $obj->CI->input->post('projectadmin_participants_project_id');
        $obj->address_id = $obj->CI->input->post('projectadmin_participants_address_id');
        $obj->address_category_id = $obj->CI->input->post('projectadmin_participants_address_category_id');
        $obj->auto_plan_approv = $obj->CI->input->post('projectadmin_participants_auto_plan_approv') ? true : false;
        $obj->purpose = $obj->CI->input->post('projectadmin_participants_purpose');

        $obj->addi_vars = $obj->CI->project_participants->get_addi_vars_post();
        return $obj;
    }
    
    /**
     * Init the project participant values from a given stdClass object
     * 
     * @param   object  $values stdClass object with appointment values to initialize a valid Personalapp_values object out of it
     *
     * @return  object  An Project_participant_values object with all values out of the given stdClass object
     */
    public static function init_from_values(stdClass $values)
    {
        $obj = new Project_participant_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $values->id;
        $obj->project_id = $values->project_id;
        $obj->address_id = $values->address_id;
        $obj->address_category_id = $values->address_category_id;
        $obj->auto_plan_approv = ($values->auto_plan_approv) ? true : false;
        
        if (isset($values->purpose)) {
            $obj->purpose = $values->purpose;
        } else {
            $obj->purpose = false;
        }

        if (!isset($values->addi_fields)) {
            $obj->CI->load->model('Project_participants_model', 'project_participants');
            $values->addi_fields = $obj->CI->project_participants->get_addi_list();
        }
        self::init_addi_vars_from_values($obj, $values);
        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 'Participant ID has to be "add" or an ID';
        }

        return false;
    }

    /**
     * Get task to save
     *
     * @return  array
     */
    public function get_project_participants_to_save()
    {
        $data = array(
            'auto_plan_approv'          =>  intval($this->auto_plan_approv),
            'purpose'                   =>  (!$this->is_really_empty($this->purpose)) ? $this->purpose : '',
        );
        
        if ($this->add_or_id ==='add') {
            $data['project_id']             =   intval($this->project_id);
            $data['address_id']             =   intval($this->address_id);
            $data['address_category_id']    =   intval($this->address_category_id);
        }
        
        return $data;
    }
    
    /**
     * Get the project id
     *
     * @return  array
     */
    public function get_project_id()
    {
        return intval($this->project_id);
    }

} // END class Project_participant_values
