<?php
class Add extends MY_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('countries');
$this->load->library('upload');
$this->load->model('admin/user/add_user_model');
}
public function index() {
$data['timezones'] = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('firstname', 'Firstname', 'trim|required');
$this->form_validation->set_rules('lastname', 'Lastname', 'trim');
$this->form_validation->set_rules('email', 'Email', 'trim|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|matches[password]');
$this->form_validation->set_rules('status', 'Status', 'trim|required|callback_status[status]');
$this->form_validation->set_rules('country', 'Country', 'trim|required|callback_country[country]');
$this->form_validation->set_rules('timezone', 'Time Zone', 'trim|required|callback_timezone[timezone]');
$this->form_validation->set_message('required', '<b>{field}</b> must be set!');
if ($this->form_validation->run($this)) {
if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0) {
if (!is_dir(FCPATH . 'uploads/users/' . $this->input->post('username') . '/')) {
mkdir(FCPATH . 'uploads/users/' . $this->input->post('username') . '/');
}
$config['upload_path'] = './uploads/users/' . $this->input->post('username') . '/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 3000;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$this->error['warning'] = $this->upload->display_errors();
}
$upload_data = $this->upload->data();
}
$this->add_user_model->insert_user($upload_data['file_name']);
}
if (validation_errors() != false) {
$this->error['warning'] = validation_errors('<div class="alert alert-danger">', '</div>');
}
if (isset($this->error['warning'])) {
$data['warning_error'] = $this->error['warning'];
} else {
$data['warning_error'] = '';
}
$data['countries'] = $this->countries->get();
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->load->view('user/add_view', $data);
}
public function country($str) {
if ($str) {
return true;
} else {
$this->form_validation->set_message('country', '<b>{field}</b> must be set!');
return false;
}
}
public function timezone($str) {
if ($str) {
return true;
} else {
$this->form_validation->set_message('timezone', '<b>{field}</b> must be set!');
return false;
}
}
public function status($str) {
if ($str) {
return true;
} else {
$this->form_validation->set_message('status', '<b>{field}</b> must be set!');
return false;
}
}
}