<?php
class Thread extends MX_Controller {
private $pre_message;
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('parsedown');
$this->load->model('catalog/qna/thread_model');
$this->load->model('catalog/qna/category_model');
$this->load->model('catalog/qna/forum_model');
}
public function add($forum_id) {
$this->form_validation->set_rules('subject', 'subject', 'required|min_length[5]|max_length[120]');
$this->form_validation->set_message('required', 'This thread {field} is required!');
if ($this->form_validation->run() == true) {
if ($this->input->post('preview')) {
$post_tmp_info = $this->thread_model->get_tempory_post($this->input->post('my_post_key'), $forum_id);
if ($this->input->post('my_post_key') == $post_tmp_info['posting_id']) {
$data = array(
'forum_id' => $forum_id,
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message')
);
$this->db->where('posting_id', $this->input->post('my_post_key'));
$this->db->update('post_tmp', $data);
} else {
$data = array(
'posting_id' => $this->input->post('my_post_key'),
'forum_id' => $forum_id,
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message')
);
$this->db->insert('post_tmp', $data);
}
}
}
$this->get_form($forum_id);
}
public function edit($forum_id) {
$this->get_form($forum_id);
}
public function index() {
}
public function get_form($forum_id) {
if (form_error('subject')) {
$data['error_subject'] = form_error('subject');
} else {
$data['error_subject'] = '';
}
$thread_info = '';
if ($this->uri->segment(1) == 'newthread') {
$data['action'] = 'newthread/' . $forum_id;
$data['is_edit'] = false;
}
if ($this->uri->segment(1) == 'editpost') {
$data['action'] = 'editpost/';
$data['is_edit'] = true;
$thread_info = $this->thread_model->get_post($this->uri->segment(2));
}
$post_tmp_info = $this->thread_model->get_tempory_post($this->input->post('my_post_key'), $forum_id);
$this->parsedown->setLiteralBreaks(true);
if ($this->input->post('preview')) {
$data['my_post_key'] = $this->input->post('my_post_key');
$data['pre_message'] = $this->parsedown->text($post_tmp_info['message']);
} else {
$data['my_post_key'] = $this->generateRandomString(15);
$data['pre_message'] = '';
}
if ($this->input->post('message')) {
$data['message'] = $this->input->post('message');
} else if (!empty($thread_info)) {
$data['message'] = $thread_info['message'];
} else {
$data['message'] = '';
}
$data['header'] = Modules::run('catalog/common/header/index');
$data['footer'] = Modules::run('catalog/common/footer/index');
$this->load->view('template/qna/thread_form', $data);
}
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}