<?php
/**
 * Block Reinsurance Custom Prestashop module
 * 
 * @author    Prestaddons <contact@prestaddons.fr>
 * @copyright 2014 Prestaddons
 * @license
 * @link      http://www.prestaddons.fr
 */

class ReinsuranceCustomClass extends ObjectModel
{
	/** @var integer reinsurance id*/
	public $id;

	/** @var integer reinsurance id shop*/
	public $id_shop;

	/** @var string reinsurance file name icon*/
	public $icon_name;

	/** @var string reinsurance text*/
	public $text;
	
	/** @var string reinsurance link*/
	public $link;
	
	/** @var boolean reinsurance new wwindow link*/
	public $new_window;

	/**
	 * @see ObjectModel::$definition
	 */
	public static $definition = array(
		'table' => 'reinsurance_custom',
		'primary' => 'id_reinsurance_custom',
		'multilang' => true,
		'fields' => array(
			'id_shop' =>				array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt', 'required' => true),
			'icon_name' =>				array('type' => self::TYPE_STRING),
			'new_window' =>				array('type' => self::TYPE_BOOL),
			/** Lang fields */
			'text' =>					array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName'),
			'link' =>					array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isAbsoluteUrl'),
		)
	);

	public function copyFromPost()
	{
		/* Classical fields */
		foreach ($_POST as $key => $value)
			if (array_key_exists($key, $this) && $key != 'id_'.$this->table)
				$this->{$key} = $value;

		/* Multilingual fields */
		if (count($this->fieldsValidateLang))
		{
			$languages = Language::getLanguages(false);
			foreach ($languages as $language)
				foreach ($this->fieldsValidateLang as $field => $validation)
					if (Tools::getIsset($field.'_'.(int)$language['id_lang']))
						$this->{$field}[(int)$language['id_lang']] = Tools::getValue($field.'_'.(int)$language['id_lang']);
		}
	}
}