<?php

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

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

function campaignsummary($lang, $campaign) {
	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);
	}

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

	$r = thread_get($lang, $campaign_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 */

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

	$campaign_name = $thread_name;
	$campaign_title = $thread_title;
	$campaign_abstract = $thread_abstract;
	$campaign_cloud = $thread_cloud;
	$campaign_modified= $thread_modified;
	$campaign_nocloud = $thread_nocloud;
	$campaign_nosearch = $thread_nosearch;

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

	$campaign_contents = array();
	$r = thread_get_contents($lang, $campaign_id);
	if ($r) {
		$campaign_url = url('campaign', $lang);
		foreach ($r as $c) {
			extract($c);	/* node_id node_name node_title node_number */
			$page_id = $node_id;
			$page_title = $node_title;
			$page_url = $campaign_url . '/' . $campaign_id . '/' . $node_id;
			$campaign_contents[] = compact('page_id', 'page_title', 'page_url');
		}
	}

	$content = view('campaignsummary', false, compact('campaign_id', 'campaign_title', 'campaign_abstract', 'campaign_contents'));

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

	$cloud=false;
	if (!$campaign_nocloud) {
		$cloud_url= url('search', $lang, $campaign_id);
		$byname=$bycount=$index=true;
		$cloud = build('cloud', $lang, $cloud_url, $campaign_id, false, 30, compact('byname', 'bycount', 'index'));
	}

	$headline_text=	translate('campaignsummary:title', $lang);
	$headline_url=user_has_role('administrator') ? url('campaign', $lang) : false;
	$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) . '/'. $campaign_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('sharebar', 'toolbar', 'banner', 'sidebar', 'content'));

	return $output;
}

