<?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 a Tabel configuration
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Tableconfig_values extends Value_object
{

    protected $view_table;
    protected $order;
    protected $savevalues;

    /**
     * Init the tableconfig 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 Tableconfig_values();
        $obj->CI = CI_Controller::get_instance();

        $obj->view_table = $obj->CI->input->post('view_table');
        $obj->order = $obj->CI->input->post('order');
        $obj->savevalues = $obj->CI->input->post('savevalues');
        return $obj;
    }


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

        $obj->view_table = $values->view_table;
        $obj->order =  $values->order;
        $obj->savevalues = $values->savevalues;
        return $obj;
    }

    /**
     * Validate and ensure all values are appropriate
     *
     * @return  mixed   false on success, else the error message
     */
    public function validate()
    {
        if (!is_string($this->view_table)) {
            return 'View Table is invalid';
        }

        if (!is_array($this->order) || count($this->order) == 0) {
            return 'Order is no array or an empty array';
        }
        foreach ($this->order as $key => $val) {
            if(!is_integer($key)){
                return 'Order key is not an integer value';
            }
            if(!is_string($val)){
                return 'Order value is not an string value';
            }
        }

        if (!is_array($this->savevalues) || count($this->savevalues) == 0) {
            return 'Savevalues is no array or an empty array';
        }
        foreach ($this->savevalues as $key => $val) {
            $arr_key = explode('_', $key);
            if(substr($arr_key[count($arr_key)-1], 0, 7) == 'visible'){
                if($val != 'on'){
                    return "Value '$val' for an visible or visible mobil is not valid";
                }
            } elseif (substr($arr_key[count($arr_key)-1], 0, 5) == 'width') {
                if(!is_numeric(usnumber_to_float($val))){
                    return "Value '$val' for an width or width mobil is not nummeric";
                }
            }
        }
        return false;
    }

    /**
     * Get tableconfiguration to save
     *
     * @return  array
     */
    public function get_tableconfig_to_save()
    {
        $data = array();
        $i = 0;
        foreach ($this->order as $field) {
            $data[$field] = array('order' => $i, 'visible' => false, 'visiblemob' => false, 'width' => null, 'maxchars' => null, 'widthmob' => null, 'maxcharsmob' => null);
            $i++;
        }

        foreach ($this->savevalues as $fieldkey => $value) {
            $arr = explode('_', $fieldkey);
            if(count($arr) < 2){
                basic_error('Syntax of savevalues Key is invalid', null, __CLASS__.'::'.__FUNCTION__);
            }
           
            $key = $arr[count($arr)-1];
            unset($arr[count($arr)-1]);
            $field = implode('_', $arr);
            if($key == 'visible' || $key == 'visiblemob'){
                $value = ($value) ? true : false;
            } else {
                $value = usnumber_to_float($value);
            }
            $data[$field][$key] = $value;
        }
        return $data;
    }
} // END class Tableconfig_values
