<?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 delete projects, staff, addresses (models which uses the deletion tools)
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Deletion_tools_values extends Value_object
{
    protected $sections = null;
    
    /**
     * Constructor
     */
    protected function __construct()
    {
        $this->sections = new stdClass();
        $this->move_to = null;
        parent::__construct();
    }
    
    /**
     * Init the deletion tool values from $_POST
     *
     * @return  object  An Deletion_tools_values object with all values out of the post array
     */
    public static function init_from_post()
    {
        $obj = new Deletion_tools_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $obj->CI->input->post('val1');
        $delete_params = $obj->CI->input->post('val2');
        $obj->sections = new stdClass();
        
        foreach ($delete_params as $key => $val) {
            $arr = explode('radio_del_', $key);
            if(count($arr) > 1) {
                $section = trim($arr[1], '_');
                if (!isset($obj->sections->$section)) {
                    $obj->sections->$section = new stdClass();
                }                
                $obj->sections->$section->action = $val;
                continue;
            }
            
            if ($key === 'move_to') {
                $obj->move_to = intval($val);
            }
            
            #$arr = explode('wp_move_', $key);
            #if(count($arr) === 2) {
            #    $obj->move_to_wp_mapping[intval($arr[1])] = intval($val);
            #}
        }
        
        return $obj;
    }
    
    /**
     * Init the deletion tool from values (Always delete, no move!)
     *
     * @return  object  An Deletion_tools_values object
     */
    public static function init_from_values($item)
    {
        $obj = new Deletion_tools_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = intval($item);
        
        $obj->CI->load->library('Project_deletion_tools', null, 'project_deletion_tools');
        $sections = $obj->CI->project_deletion_tools->get_sections();
        
        $obj->sections = new stdClass();
        foreach ($sections as $section) {
            $obj->sections->$section = new stdClass();
            $obj->sections->$section->action = Deletion_tools::ACTION_DELETE;
        }
        
        return $obj;
    }

    /**
     * Validate and ensure all values are appropriate
     *
     * @return  mixed   false on success, else the error message
     */
    public function validate()
    {
        if (!is_numeric($this->add_or_id) || $this->add_or_id <= 0) {
            return 'Wrong ID of main record';
        }

        return false;
    }

    /**
     * Get the section values to delete
     *
     * @return  array
     */
    public function get_section_values_to_delete()
    {

        return $this->sections;
    }
    
    /**
     * Get the global move_to ID
     *
     * @return  mixed   Either an integer or null
     */
    public function get_move_to_id()
    {
        return $this->move_to;
    }    

} // END class Deletion_tools_values
