<?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_article_values extends Value_object {

    protected $title;
    protected $version;
    protected $author;
    protected $in_date;
    protected $themes = array();
    protected $address_id;
    protected $room_id;
    protected $rack_id;
    protected $tray_id;
    protected $filing_id;

    /**
     * Init the whatiswhere values from $_POST
     *
     * @return  object  An Whatiswhere_article_values object with all values out of the post array
     */
    public static function init_from_post() {
        $obj = new Whatiswhere_article_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $obj->CI->input->post('whatiswhere_article_edit');
        $obj->article_title = $obj->CI->input->post('whatiswhere_article_title');
        $obj->article_version = $obj->CI->input->post('whatiswhere_article_version');
        $obj->article_author = $obj->CI->input->post('whatiswhere_article_author');
        $obj->article_in_date = new UsDate($obj->CI->input->post('whatiswhere_article_in_date'), null, true, null);
        $obj->article_themes = $obj->CI->input->post('whatiswhere_article_themes');
        $obj->article_room_id = $obj->CI->input->post('whatiswhere_article_room');
        $obj->article_rack_id = $obj->CI->input->post('whatiswhere_article_rack');
        $obj->article_tray_id = $obj->CI->input->post('whatiswhere_article_tray');
        $obj->article_filing_id = $obj->CI->input->post('whatiswhere_article_filing');

        return $obj;
    }

    /**
     * Init the whatiswhere values from $_POST
     *
     * @return  object  An Whatiswhere_article_values object with all values out of the post array
     */
    public static function init_from_values($values) {
        $obj = new Whatiswhere_article_values();
        $obj->CI = CI_Controller::get_instance();
        $obj->add_or_id = $values->id;
        $obj->article_title = $values->article_title;
        $obj->article_version = $values->article_version;
        $obj->article_author = $values->article_author;
        $obj->article_in_date = $values->article_in_date;
        $obj->article_themes = $values->article_themes;
        $obj->article_room_id = $values->article_room_id;
        $obj->article_rack_id = $values->article_rack_id;
        $obj->article_tray_id = $values->article_tray_id;
        $obj->article_filing_id = $values->article_filing_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_article ID has to be "add" or an ID';
        }
        return false;
    }

    /**
     * Get task to save
     *
     * @return  array
     */
    public function get_article_items_to_save() {
        $data = array(
            'article_title' => $this->article_title,
            'version' => $this->article_version,
            'article_author' => $this->article_author,
            'in_date' => $this->article_in_date->mysql(),
            'room_id' => intval($this->article_room_id),
            'rack_id' => intval($this->article_rack_id),
            'tray_id' => intval($this->article_tray_id),
            'filing_id' => intval($this->article_filing_id),
        );

        return $data;
    }

    /**
     * Get themes to save
     *
     * @return  array
     */
    public function get_article_themes_to_save() {

        $data = array();
        if (!empty($this->article_themes)) {
            foreach ($this->article_themes as $theme) {
                array_push($data, $theme);
            }
        }
        return $data;
    }
}

