<?php

/**
 *
 * @copyright  2016-2017 izend.org
 * @version    2
 * @link       http://www.izend.org
 */

require_once 'readarg.php';
require_once 'strtofname.php';
require_once 'userhasrole.php';
require_once 'userprofile.php';
require_once 'models/thread.inc';

function campaigneditsummary($lang, $clang, $thread) {
	global $with_toolbar;

	$user_id=user_profile('id');

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

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

	$r = thread_get($lang, $thread_id);
	if (!$r) {
		return run('error/notfound', $lang);
	}
	extract($r); /* thread_user_id thread_type thread_title */

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

	if ($thread_user_id != $user_id and !user_has_role('administrator')) {
		return run('error/notfound', $lang);
	}

	$confirmed=false;

	$action='init';
	if (isset($_POST['thread_reorder'])) {
		$action='reorder';
	}
	else if (isset($_POST['node_create'])) {
		$action='create';
	}
	else if (isset($_POST['node_copy'])) {
		$action='copy';
	}
	else if (isset($_POST['node_delete'])) {
		$action='delete';
	}
	else if (isset($_POST['node_confirmdelete'])) {
		$action='delete';
		$confirmed=true;
	}

	$new_node_name=$new_node_title=$new_node_number=false;
	$old_node_number=false;

	$p=false;

	switch($action) {
		case 'create':
		case 'delete':
		case 'copy':
		case 'reorder':
			if (isset($_POST['new_node_title'])) {
				$new_node_title=readarg($_POST['new_node_title']);
				$new_node_name = strtofname($new_node_title);
			}
			if (isset($_POST['new_node_number'])) {
				$new_node_number=readarg($_POST['new_node_number']);
			}
			if (isset($_POST['old_node_number'])) {
				$old_node_number=readarg($_POST['old_node_number']);
			}
			if (isset($_POST['p'])) {
				$p=$_POST['p'];	// DON'T readarg!
			}
			break;
		default:
			break;
	}

	$thread_contents = array();

	$r = thread_get_contents($clang, $thread_id);	/* node_id node_number node_ignored node_name node_title node_cloud thread_image */

	if (!$r or count($r) != count($p)) {
		$p = false;
	}

	if ($r) {
		$pos=1;
		$thread_url = url('campaignedit', $lang) . '/' . $thread_id;
		foreach ($r as $c) {
			$c['node_url'] = $thread_url  . '/' . $c['node_id'];
			$c['pos'] = $p ? $p[$pos] : $pos;
			$thread_contents[$pos] = $c;
			$pos++;
		}
	}

	$missing_new_node_title=false;
	$bad_new_node_title=false;
	$bad_new_node_number=false;

	$missing_old_node_number=false;
	$bad_old_node_number=false;

	switch($action) {
		case 'copy':
		case 'create':
			if (!$new_node_title) {
				$missing_new_node_title = true;
			}
			else if (!$new_node_name) {
				$bad_new_node_title = true;
			}
			else if (!preg_match('#^\w+(-\w+)*$#', $new_node_name)) {
				$bad_new_node_title = true;
			}
			if (!$new_node_number) {
				$new_node_number = false;
			}
			else if (!is_numeric($new_node_number)) {
				$bad_new_node_number = true;
			}
			else if ($new_node_number < 1 or $new_node_number > count($thread_contents) + 1) {
				$bad_new_node_number = true;
			}
			break;

		default:
			break;
	}

	switch($action) {
		case 'copy':
		case 'delete':
			if (!$old_node_number) {
				$missing_old_node_number = true;
			}
			else if (!is_numeric($old_node_number)) {
				$bad_old_node_number = true;
			}
			else if ($old_node_number < 1 or $old_node_number > count($thread_contents)) {
				$bad_old_node_number = true;
			}
			break;

		default:
			break;
	}

	$confirm_delete_node=false;

	switch($action) {
		case 'create':
		case 'copy':
			if ($missing_new_node_title or $bad_new_node_title or $bad_new_node_number) {
				break;
			}

			if ($action == 'copy') {
				if ($missing_old_node_number or $bad_old_node_number) {
					break;
				}

				$node_id = $thread_contents[$old_node_number]['node_id'];

				$np = thread_copy_node($clang, $thread_user_id, $thread_id, $node_id, $new_node_name, $new_node_title, $new_node_number);
			}
			else {
				$np = thread_create_node($clang, $thread_user_id, $thread_id, $new_node_name, $new_node_title, $new_node_number);
			}

			if (!$np) {
				break;
			}

			extract($np);	/* node_id node_number */

			if ($action == 'create') {
				node_create_content($clang, $node_id, 'sms');
			}

			$node_ignored = false;
			$node_title = $new_node_title;
			$node_url = url('campaignedit', $lang) . '/' . $thread_id . '/' . $node_id;
			$pos = $node_number;

			if ($thread_contents) {
				foreach ($thread_contents as &$c) {
					if ($c['node_number'] >= $pos) {
						$c['node_number']++;
					}
					if ($c['pos'] >= $pos) {
						$c['pos']++;
					}
				}
				array_splice($thread_contents, $pos-1, 0, array(compact('node_id', 'node_title', 'node_number', 'node_ignored', 'node_url', 'pos')));
			}
			else {
				$pos=1;
				$thread_contents=array($pos => compact('node_id', 'node_title', 'node_number', 'node_ignored', 'node_url', 'pos'));
			}

			$new_node_name=$new_node_title=false;
			$new_node_number=$node_number+1;

			$old_node_number = false;

			break;

		case 'delete':
			if ($missing_old_node_number or $bad_old_node_number) {
				break;
			}

			if (!$confirmed) {
				$confirm_delete_node=true;
				break;
			}

			$node_id = $thread_contents[$old_node_number]['node_id'];

			$r = thread_delete_node($thread_id, $node_id);

			if (!$r) {
				break;
			}

			unset($thread_contents[$old_node_number]);
			$thread_contents = array_values($thread_contents);

			foreach ($thread_contents as &$c) {
				if ($c['node_number'] >= $old_node_number) {
					$c['node_number']--;
				}
				if ($c['pos'] >= $old_node_number) {
					$c['pos']--;
				}
			}

			$old_node_number = false;

			break;

		case 'reorder':
			if (!$p) {
				break;
			}

			$neworder=range(1, count($p));
			array_multisort($p, SORT_NUMERIC, $neworder);

			$number=1;
			$nc=array();
			foreach ($neworder as $i) {
				$c = &$thread_contents[$i];
				if ($c['node_number'] != $number) {
					thread_set_node_number($thread_id, $c['node_id'], $number);
					$c['node_number'] = $number;
				}
				$c['pos']=$number;

				$nc[$number++] = $c;
			}
			$thread_contents = $nc;

			break;

		default:
			break;
	}

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

	$viewsms=$thread_contents ? url('campaign', $clang) . '/'. $thread_id . '?' . 'slang=' . $lang : false;

	$admin=true;
	$banner = build('banner', $lang, $with_toolbar ? false : compact('viewsms', 'admin'));
	$toolbar = $with_toolbar ? build('toolbar', $lang, compact('viewsms', 'admin')) : false;

	$inlanguages=view('inlanguages', false, compact('clang'));

	$errors = compact('missing_new_node_title', 'bad_new_node_title', 'bad_new_node_number', 'missing_old_node_number', 'bad_old_node_number');

	$content = view('campaigneditsummary', $lang, compact('clang', 'inlanguages', 'thread_title', 'thread_id', 'thread_contents', 'new_node_name', 'new_node_title', 'new_node_number', 'old_node_number', 'confirm_delete_node', 'errors'));

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

	return $output;
}

