<?php

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

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

function campaignpage($lang, $thread, $node) {
	global $system_languages, $with_toolbar;

	$user_id=user_profile('id');

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

	$slang=false;
	if (isset($_GET['slang'])) {
		$slang = $_GET['slang'];
	}
	else {
		$slang=$lang;
	}
	if (!in_array($slang, $system_languages)) {
		return run('error/notfound', $lang);
	}

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

	$node_id = thread_node_id($thread_id, $node, $lang);
	if (!$node_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_name thread_title thread_abstract thread_cloud thread_nocloud thread_nosearch thread_nocomment thread_nomorecomment */

	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);
	}

	$r = thread_get_node($lang, $thread_id, $node_id);
	if (!$r) {
		return run('error/notfound', $lang);
	}
	extract($r); /* node_number node_ignored node_name node_title node_abstract node_cloud node_image node_user_id node_visits node_nocomment node_nomorecomment node_novote node_nomorevote node_ilike node_tweet node_plusone node_linkedin */

	$sms_user_id=$node_user_id;
	$sms_name=$node_name;
	$sms_title=$node_title;
	$sms_subject=$node_abstract;
	$sms_cloud=$node_cloud;
	$sms_image=$node_image;
	$sms_number=$node_number;
	$sms_modified=$node_modified;

	$smscontents = build('nodecontent', $lang, $node_id);

	if ($sms_title) {
		head('title', $sms_title);
	}
	else if ($thread_title) {
		head('title', $thread_title);
	}
	else {
		head('title', translate('campaignpage:title', $lang));
	}
	head('description', false);
	head('keywords', false);
	head('robots', 'noindex, nofollow');

	$prev_node_label=$prev_node_url=false;
	$r=thread_node_prev($lang, $thread_id, $node_id);
	if ($r) {
		extract($r);	/* prev_node_id prev_node_name prev_node_title prev_node_number */
		$prev_node_label = $prev_node_title ? $prev_node_title : $prev_node_number;
		$prev_node_url=url('campaign', $lang) .  '/' . $thread_id . '/'. $prev_node_id;
	}

	$next_node_label=$next_node_url=false;
	$r=thread_node_next($lang, $thread_id, $node_id);
	if ($r) {
		extract($r);	/* next_node_id next_node_name next_node_title next_node_number */
		$next_node_label = $next_node_title ? $next_node_title : $next_node_number;
		$next_node_url=url('campaign', $lang) . '/' . $thread_id . '/'. $next_node_id;
	}

	$content = view('campaignpage', $lang, compact('node_id', 'sms_title', 'sms_cloud', 'sms_modified', 'sms_subject', 'smscontents', 'prev_node_url', 'prev_node_label', 'next_node_url', 'next_node_label'));

	$search=false;
	if (!$thread_nosearch) {
		$search_text='';
		$search_url= url('search', $lang, $thread_id);
		$suggest_url= url('suggest', $lang, $thread_id);
		$search=view('searchinput', $lang, compact('search_url', 'search_text', 'suggest_url'));
	}

	$cloud=false;
	if (!$thread_nocloud) {
		$cloud_url= url('search', $lang, $thread_id);
		$byname=$bycount=$index=true;
		$cloud = build('cloud', $lang, $cloud_url, $thread_id, false, 15, compact('byname', 'bycount', 'index'));
	}

	$headline_text=translate('campaignsummary:title', $lang);
	$headline_url=url('campaign', $lang) . '/' . $thread_id;
	$headline = compact('headline_text', 'headline_url');
	$title = view('headline', false, $headline);

	$sidebar = view('sidebar', false, compact('search', 'cloud', 'title'));

	$search=!$thread_nosearch ? compact('search_url', 'search_text', 'suggest_url') : false;
	$editsms=url('campaignedit', $slang) . '/' . $thread_id . '/' . $node_id . '?' . 'clang=' . $lang;

	$banner = build('banner', $lang, $with_toolbar ? compact('headline', 'search') : compact('headline', 'search', 'editsms'));
	$toolbar = $with_toolbar ? build('toolbar', $lang, compact('editsms')) : false;

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

	return $output;
}

