<?php defined('KOBLOG') or die('Koblog CMS.');

// ============================================================================
// Check role
// ============================================================================

checkRole(array('admin', 'editor', 'author'));
global $pagetypes;

// ============================================================================
// Functions
// ============================================================================

// Returns the content belongs to the current user if the user has the role Editor
function filterContentOwner($list) {
	global $login;
	global $pages;
	$tmp = array();
	foreach ($list as $pageKey) {
		if ($pages->db[$pageKey]['username']==$login->username()) {
			array_push($tmp, $pageKey);
		}
	}
	return $tmp;
}

// ============================================================================
// Main before POST
// ============================================================================

// ============================================================================
// POST Method
// ============================================================================

// ============================================================================
// Main after POST
// ============================================================================

$contentTypeKey = $layout['parameters'];
if (!$pagetypes->exists($contentTypeKey)) {
	Alert::set($L->g('Content type do not exists'));
	Redirect::page('dashboard');
}

try {
	$contentType = new PageType($contentTypeKey);
} catch (Exception $e) {
	Alert::set($L->g('Content type do not exists'));
	Redirect::page('dashboard');
}

$published = $pages->getList($url->pageNumber(), ITEMS_PER_PAGE_ADMIN, [$contentTypeKey], ['published'], true, true);
$drafts = $pages->getDraftDB([$contentTypeKey], true);
$scheduled = $pages->getScheduledDB([$contentTypeKey],true);
$autosave = $pages->getAutosaveDB([$contentTypeKey],true);

// If the user is an Author filter the content he/she can edit
if (checkRole(array('author'), false)) {
	$published 	= filterContentOwner($published);
	$drafts 	= filterContentOwner($drafts);
	$scheduled 	= filterContentOwner($scheduled);
	$static 	= filterContentOwner($static);
}

// Check if out of range the pageNumber
if (empty($published) && $url->pageNumber()>1) {
	Redirect::page('content');
}

// Title of the page
$layout['title'] .= ' - '.$L->g('Manage content');