<?php
class Newthread extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('parsedown');
$this->load->helper('string');
$this->load->model('catalog/forum/newthread_model');
$this->load->model('catalog/forum/preview_newthread_model');
}
public function index($forum_id = NULL) {
$code = random_string('alpha', 32);
$data['forum_id'] = $forum_id;
$this->form_validation->set_rules('subject', 'subject', 'trim|required');
$this->form_validation->set_rules('message', 'message', 'trim|required');
if ($this->form_validation->run() == TRUE) {
if ($this->input->post('newthread_preview')) {
if ($this->preview_newthread_model->check_if_code_set($this->input->post('code'))) {
$this->preview_newthread_model->update_post();
} else {
$this->preview_newthread_model->insert_post();
}
}
if ($this->input->post('newthread')) {
if ($this->newthread_model->check_if_code_set($this->input->post('code'))) {
$this->newthread_model->update_post();
redirect(base_url('post') .'/'. $this->input->post('post_id'));
} else {
$new_id = $this->newthread_model->insert_post();
redirect(base_url('post') .'/'. $new_id);
}
}
if ($this->input->post('newthread_delete')) {
$this->newthread_model->delete_post();
redirect(base_url('forum'));
}
}
$post_data = $this->newthread_model->get_newthread();
if ($this->input->post('post_id')) {
$data['post_id'] = $this->input->post('post_id');
} elseif (!empty($post_data)) {
$data['post_id'] = $post_data['post_id'];
} else {
$data['post_id'] = '';
}
if ($this->input->post('code')) {
$data['code'] = $this->input->post('code');
} elseif (!empty($post_data)) {
$data['code'] = $post_data['code'];
} else {
$data['code'] = $code;
}
if ($this->input->post('subject')) {
$data['subject'] = $this->input->post('subject');
} elseif (!empty($post_data)) {
$data['subject'] = $post_data['subject'];
} else {
$data['subject'] = '';
}
if ($this->input->post('message')) {
$data['message'] = $this->input->post('message');
} elseif (!empty($post_data)) {
$data['message'] = $post_data['message'];
} else {
$data['message'] = '';
}
$data['header'] = Modules::run('catalog/common/header/index');
$data['footer'] = Modules::run('catalog/common/footer/index');
$data['preview'] = Modules::run('catalog/forum/newthread_preview/index', $post_data);
$this->load->view('template/forum/newthread_view', $data);
}
}