<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * Form for module profile
 *
 * @author CIEXT skeleton
 */
class profile_form
{

    protected $fields = [];
    protected $error_delimeter_open = '';
    protected $error_delimeter_close = '';
    protected $ci;
    protected $error_messages = [];
    protected $values = [];

    public function __construct()
    {
        $this->ci = & get_instance();
        $this->ci->load->config('user/config');
        $this->error_delimeter_open = $this->ci->config->item('form_error_delimeter_open');
        $this->error_delimeter_close = $this->ci->config->item('form_error_delimeter_close');

        // Set fields
        $this->fields = [
            [
                'field' => 'name',
                'label' => $this->ci->lang->line('Name'),
                'rules' => 'xss_clean|trim|min_length[4]|max_length[32]'
            ],
            [
                'field' => 'surname',
                'label' => $this->ci->lang->line('Surname'),
                'rules' => 'xss_clean|trim|min_length[4]|max_length[32]'
            ],
            [
                'field' => 'sex',
                'label' => $this->ci->lang->line('Sex'),
                'rules' => 'intval'
            ],
            [
                'field' => 'phone',
                'label' => $this->ci->lang->line('Phone'),
                'rules' => 'xss_clean|trim|min_length[3]|max_length[255]'
            ],
            [
                'field' => 'subscribe',
                'label' => $this->ci->lang->line('Subscribe'),
                'rules' => 'xss_clean'
            ],
        ];

        $this->ci->load->library('form_validation');
        $this->ci->form_validation->set_error_delimiters($this->error_delimeter_open, $this->error_delimeter_close);
        // Set rules
        $this->ci->form_validation->set_rules($this->fields);
    }

    /**
     * Validate
     * @return boolean
     */
    public function is_valid()
    {
        return $this->ci->form_validation->run();
    }

    /**
     * Get value by key or all values
     * @param string $key
     * @return mixed
     */
    public function get_value($key = '')
    {
        if (empty($this->values) && $this->is_valid()) {
            foreach ($this->fields as $field) {
                $this->values[$field['field']] = $this->ci->input->post($field['field']);
            }
        }

        // Return all values if empty key
        if (empty($key)) {
            return $this->values;
        }

        return isset($this->values[$key]) ? $this->values[$key] : null;
    }

    /**
     * Get error message
     * @param string $key
     * @return string
     */
    public function get_error_message($key = '')
    {
        $form_error = $this->ci->form_validation->error($key);
        if (!empty($form_error)) {

            return $form_error;
        }

        return !empty($this->error_messages[$key]) ? $this->error_messages[$key] : '';
    }

    /**
     * Set error message
     * @param string $key
     * @param string $message
     * @return self
     */
    public function set_error_message($key, $message = '')
    {
        $this->error_messages[$key] = $this->error_delimeter_open . $message . $this->error_delimeter_close;
        return $this;
    }

}
