<?php

/**
 *
 * @copyright  2010-2011 frasq.org
 * @version    1
 * @link       http://frasq.org
 */

require_once 'models/thread.inc';

require_once 'userhasrole.php';
require_once 'readarg.php';
require_once 'strtofname.php';

function threadeditsummary($lang, $clang, $thread) {
	global $supported_threads;

	if (!user_has_role('writer')) {
		return run('error/unauthorized', $lang);
	}

	$thread_id = thread_id($thread);
	if (!$thread_id) {
		return run('error/notfound', $lang);
	}

	$action='init';
	if (isset($_POST['thread_edit'])) {
		$action='edit';
	}

	$thread_type=$thread_name=$thread_title=$thread_abstract=$thread_cloud=false;

	switch($action) {
		case 'init':
		case 'reset':
			$r = thread_get($clang, $thread_id, false);
			if ($r) {
				extract($r); /* thread_type thread_name thread_title thread_abstract thread_cloud */
			}

			break;
		case 'edit':
			if (isset($_POST['thread_type'])) {
				$thread_type=readarg($_POST['thread_type']);
			}
			if (isset($_POST['thread_title'])) {
				$thread_title=readarg($_POST['thread_title']);
			}
			if (isset($_POST['thread_name'])) {
				$thread_name=strtofname(readarg($_POST['thread_name']));
			}
			if (empty($thread_name) and !empty($thread_title)) {
				$thread_name = strtofname($thread_title);
			}
			if (isset($_POST['thread_abstract'])) {
				$thread_abstract=readarg($_POST['thread_abstract']);
			}
			if (isset($_POST['thread_cloud'])) {
				$thread_cloud=readarg($_POST['thread_cloud'], true, false);	// trim but DON'T strip!
				preg_match_all('/(\S+)/', $thread_cloud, $r);
				$thread_cloud=implode(' ', array_unique($r[0]));
			}
			break;
		default:
			break;
	}

	$missing_thread_title=false;
	$missing_thread_name=false;
	$bad_thread_name=false;
	$missing_thread_type=false;
	$bad_thread_type=false;

	switch($action) {
		case 'edit':
			if (empty($thread_title)) {
				$missing_thread_title = true;
			}
			if (empty($thread_name)) {
				$missing_thread_name = true;
			}
			else if (!preg_match('#^[\w-]{3,}$#', $thread_name)) {
				$bad_thread_name = true;
			}
			if (empty($thread_type)) {
				$missing_thread_type = true;
			}
			else if (!in_array($thread_type, $supported_threads)) {
				$bad_thread_type = true;
			}
			break;

		default:
			break;
	}

	switch($action) {
		case 'edit':
			if ($missing_thread_name or $bad_thread_name or $missing_thread_title or $missing_thread_type or $bad_thread_type) {
				break;
			}

			$r = thread_set($clang, $thread_id, $thread_name, $thread_title, $thread_type, $thread_abstract, $thread_cloud);

			if (!$r) {
				break;
			}

			break;

		default:
			break;
	}

	head('title', $thread_title);
	head('description', false);
	head('keywords', false);
	head('robots', 'noindex, nofollow');

	$view=url($thread_type, $clang) . '/'. $thread_id;
	$validate=url($thread_type, $clang) . '/'. $thread_id;
	$banner = build('banner', $lang, compact('view', 'validate'));

	$errors = compact('missing_thread_name', 'bad_thread_name', 'missing_thread_title', 'missing_thread_type', 'bad_thread_type');

	$content = view('editing/threadeditsummary', $lang, compact('clang', 'thread_type', 'thread_name', 'thread_title', 'thread_abstract', 'thread_cloud', 'errors'));

	$output = layout('editing', compact('banner', 'content'));

	return $output;
}

