<?php

/**
 * CodeIgniter library
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Michael Pfeilstöcker
 * @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 a whatiswhere item
 *
 * @property    My_Controller           $CI                     The main CI object
 */
class Whatiswhere_racks_values extends Value_object {

    protected $rack;
    protected $parentid;

    /**
     * Init the whatiswhere values from $_POST
     *
     * @return  object  An Whatiswhere_racks_values object with all values out of the post array
     */
    public static function init_from_post() {

        $obj = new Whatiswhere_racks_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $obj->CI->input->post('whatiswhere_roratrfi_edit');
        $obj->rack = $obj->CI->input->post('whatiswhere_roratrfi_title');
        $obj->parentid = $obj->CI->input->post('whatiswhere_roratrfi_parentid');

        return $obj;
    }

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

        $obj->add_or_id = $values->id;
        $obj->rack = $values->rack;
        $obj->parentid = $values->parent_id;

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

    /**
     * Get task to save
     *
     * @return  array
     */
    public function get_racks_items_to_save() {

        $data = array(
            'title' => $this->rack,
            'room_id' => $this->parentid,
        );

        return $data;
    }
}

