##	editblog.php

#	Include
	require_once 'includes/blog.inc.php';

##	blog.inc.php

#	Database
	require_once 'includes/db.php';

#	Variables
	$title=$precis=$article='';
	$id=0;
	$errors='';
	$disabled='';

#	Regex: Replace [variable] with <?= $variable; />
	Find:		\[(.*?)\]
	Replace:	<?= $$$1; ?>

#	POST actions
	if(isset($_POST['add'])) {

	}
	if(isset($_POST['edit'])) {

	}
	if(isset($_POST['remove'])) {

	}
	if(isset($_POST['insert'])) {

	}
	if(isset($_POST['update'])) {

	}
	if(isset($_POST['delete'])) {

	}

#	Reading Data
	$id = intval($_POST['id']);
	$title = trim($_POST['title']);
	$precis = trim($_POST['precis']);
	$article = trim($_POST['article']);

#	checkData()
	function checkData($title,$precis,$article) {
		$errors=[];
		if(!$title) $errors[]='Missing Title';
		if(!$precis)	$errors[]='Missing Precis';
		if(!$article) $errors[]='Missing Article';
		return $errors;
	}

#	Calling checkData()
	$errors=checkData($title,$precis,$article);
	if(!$errors) {

	}
	else $errors=sprintf('<p class="errors">%s</p>',implode('<br>',$errors));

#	Normalising Line Breaks
	function normaliseLineBreak($text) {
	 	$text = str_replace("\r",'',$text);		//	remove CR
		$text = str_replace('\n',"\n",$text);	//	convert '\n'
		return $text;
	}

	require_once 'includes/library.php';

	$article=normaliseLineBreak($article);

#	Manipulating Data
	#	INSERT
		$sql='INSERT INTO blog(title,precis,article,created,updated)
			VALUES(?,?,?,now(),now())';
		$pdoStatement=$pdo->prepare($sql);
		$pdoStatement->execute([$title,$precis,$article);
	#	UPDATE
		$sql='UPDATE blog
			SET title=?,precis=?,article=?,updated=now()
			WHERE id=?';
		$pdoStatement=$pdo->prepare($sql);
		$pdoStatement->execute([$title,$precis,$article,$id);
	#	DELETE
		$sql='DELETE FROM blog WHERE id=?';
		$pdoStatement=$pdo->prepare($sql);
		$pdoStatement->execute([$id]);

#	Redirecting
	$host = $_SERVER['HTTP_HOST'];
	$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http';

	header("Location: $protocol://$host/admin.php");
	exit;

##	config.ini
	[blog-list]
		limit=6

##	bloglist.php
	$CONFIG=parse_ini_file('includes/config.ini',true);
	require_once 'includes/db.php';
	require_once 'includes/library.php';

	$tbody=$paging='';

#	Page Number
	$limit = $CONFIG['blog-list']['limit'];
	$page = intval(@$_GET['page'])
 		or $page = intval(@$_COOKIE['imagelist-page'])
		or $page = 1;
	$pages=ceil($pdo->query('SELECT count(*) FROM blog')->fetchColumn()/$limit);
	$page=min($pages,max(1,$page));
	setcookie('bloglist-page',$page,time()+7*86400);
	$offset=($page-1)*$limit;

#	Generating the List
	$sql="SELECT id,title,created,updated FROM images ORDER BY id LIMIT $limit OFFSET $offset";

	$tr = '<tr><th>%s</th><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>';
	$editbutton='<button name="edit[%s]">Edit</button>';
	$removebutton='<button name="remove[%s]">Delete</button>';

	$tbody = [];
	foreach($pdo->query($sql) as $row) {
		$created = date('d M y g:i a',strtotime($row['created']));
		$updated = date('d M y g:i a',strtotime($row['updated']));
		$edit=sprintf($editbutton,$row['id']);
		$remove=sprintf($removebutton,$row['id']);
		$tbody[] = sprintf($tr, $row['id'],$created, $updated, $row['title'],$edit,$remove);
	}
	$tbody = implode($tbody);

#	Paging
	if($pages>1) $paging=paging($page,$pages);

#	Setting the Heading
	$pagesubtitle='Add Article';
	$pagesubtitle='Edit Article';
	$pagesubtitle='Remove Article';

#	Fetching Data
	$id=intval(key($_POST['edit']));
	$id=intval(key($_POST['remove']));

#	Switching Buttons
	<?php if(isset($_POST['edit'])): ?>
		edit button
	<?php elseif(isset($_POST['remove'])): ?>
		delete button
	<?php else: ?>
		insert button
	<?php endif; ?>

#	Disabling the fieldset
	$disabled='disabled';

##	config.ini
	[blog]
		limit=4

##	blog.php

#	PHP Block
	<?php
		$CONFIG=parse_ini_file('includes/config.ini',true);
		require_once 'includes/db.php';
		require_once 'includes/library.php';
	?>

#	Variables
	$articles=$paging='';
	<?= $articles ?>
	<?= $paging ?>

#	Page
	$limit = $CONFIG['blog']['limit'];
	$pages=ceil($pdo->query('SELECT count(*) FROM blog')->fetchColumn()/$limit);

	$page=intval(@$_GET['page']) or $page=@$_COOKIE['blog-page'] or $page=1;
	$page=min($pages,max(1,$page));
	setcookie('blog-page',$page,time()+3600);

#	Fetching the Data
	$offset=($page-1)*$limit;
	$sql="SELECT id,title,created,updated,precis FROM blog ORDER BY id LIMIT $limit OFFSET $offset";

#	Generating Summaries
	$article='<h3 class="blog"><a href="blogarticle.php?article=%s">%s<span>🦘</span></a></h3><p class="date">Created: %s<br>Updated: %s</p><p class="precis">%s<br></p>';


##	blogarticle.php

	<?php
		$CONFIG=parse_ini_file('includes/config.ini',true);
		require_once 'includes/db.php';
		require_once 'includes/library.php';

		$host = $_SERVER['HTTP_HOST'];
		$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http';

	?>

	variables
		$title=$precis=$created=$updated=$article='';

	$id=intval(@$_GET['article']);

	$sql="SELECT id,title,created,updated FROM blog WHERE id=$id";

		$row=$pdo->query($sql)->fetch();
		if(!$row) {
			header("Location: $protocol://$host/blog.php");
			exit;
		}

		list($title,$precis,$created,$updated,$article)=$row;
		$article=nl2p($article);
$created = date('d M y g:i a',strtotime($row['created']));
$updated = date('d M y g:i a',strtotime($row['updated']));
