File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/admin/php/mp_multipage.php
<?php
/*
DEENSE KROON
mp_multipage.php
part of the multipage system
Mike Ontwerpt 2012
www.mikeontwerpt.nl
*/
function initMultipage($linkname){
/* access the global sql object */
global $mysqli;
/* language */
$siteLabels = getLanguage();
/* check for maxitems of this page */
if(!$result = $mysqli->query('SELECT id, label, maxItems FROM mp_pages WHERE linkname = "'.$linkname.'" LIMIT 1')){
//handle errors
$errors[] = $siteLabels['fm-somethingwentwrong'];
displayErrors($errors);
}
else{
$record = $result->fetch_assoc();
$maxItems = $record['maxItems'];
$pageId = $record['id'];
$label = $record['label'];
/* if (max items == 1, and the page contains an item){ we want to show the item } else { the dashboard }, so check for items */
if($maxItems == 1){
//does this page contain items ?
$result = $mysqli->query('SELECT p.id FROM '.$linkname.' AS p, content_status as s WHERE s.active = 1 AND s.itemId = p.id AND s.linkname = "'.$linkname.'" ORDER BY id ASC LIMIT 1');
if($result->num_rows > 0){
$record = $result->fetch_assoc();
$firstId = $record['id'];
$nopages = false;
}
else{
//if no items on page
$nopages = true;
}
}
/* require the form generator */
require_once './php/mp_generate_form.php';
/* get the form array form the database */
$formArr = generateMpArray($linkname,$label);
/* DISPATCHER */
$output = '';
if(isset($_POST['back'])){
if(isset($_POST['location'])){
$location = $_POST['location'];
header('location: '.$location);
}
else{
unset($_SESSION[$formArr['form-config']['linkname']]);
$output .= showDashboard($pageId);
}
}
else if(isset($_POST['sbm-add'])){
$output .= validateForm($formArr, 'add');
}
else if(isset($_POST['sbm-edit'])){
$output .= validateForm($formArr, 'edit', URL_SUB2);
}
else if(isset($_POST['do_submit'])){
$output .= sbmChangeOrder($pageId);
}
else if(isset($_POST['askMoveToTrash'])){
$output .= askMoveToTrash($pageId);
}
else if(isset($_POST['moveToTrash'])){
$output .= moveToTrash($pageId);
}
else if(defined('URL_SUB')){
$action = URL_SUB;
switch($action){
case 'add-item' :
$output .= generateForm($formArr, 'add');
break;
case 'edit-item' :
$output .= generateForm($formArr, 'edit', URL_SUB2);
break;
case 'order-items' :
$output .= showChangeOrder($pageId);
break;
case 'update-published' :
$output .= updatePublished();
break;
}
}
else{
//if($maxItems != 1 || $nopages){
unset($_SESSION[$formArr['form-config']['linkname']]);
$output .= showDashboard($pageId);
/*}
else{
$output .= showItem($pageId, $firstId);
}*/
}
return $output;
}
}
function showDashboard($pageId){
/* access the global sql object */
global $mysqli;
$output = '';
/* language */
$siteLabels = getLanguage();
/* get info */
if(!$result = $mysqli->query('SELECT linkname,label,maxitems FROM mp_pages WHERE id = "'.$pageId.'" LIMIT 1')){
//handle errors
$errors[] = $siteLabels['fm-somethingwentwrong'];
displayErrors($errors);
}
else{
$record = $result->fetch_assoc();
$linkname = $record['linkname'];
$label = $record['label'];
$maxitems = $record['maxitems'];
/* get info */
$query = 'SELECT p.id, p.title, p.timest, p.published ';
if($linkname == 'banners') $query .= ',type ';
$query .='FROM '.$linkname.' as p, content_status as s
WHERE s.active = 1
AND s.itemId = p.id
AND s.linkname = "'.$linkname.'"
ORDER BY p.itemOrder DESC';
if($result = $mysqli->query($query)){
$numitems = $result->num_rows;
/* title */
$output .= '<h1>'.$siteLabels['mm-pages'].' › '.$label.'</h1>';
//start with form for checkboxes
$output .= '<form action="" name="chkForm" method="post">';
/* buttons */
$output .= '<div class="submenu">';
//buttons
if($numitems < $maxitems || $maxitems == 0) $output .= '<a href="/admin/'.$linkname.'/add-item/" class="btn184 proceed">'.$siteLabels['mp-additem'].'</a>';
if($maxitems != 1) $output .= '<a href="/admin/'.$linkname.'/order-items/" class="btn184">'.$siteLabels['mp-orderitems'].'</a>';
$output .= '<input type="submit" name="askMoveToTrash" value="'.$siteLabels['mp-removeitems'].'" class="btn184"/>';
$output .= '<div class="clear"></div>';
$output .= '</div>';
/* top row */
$output .= '<div class="itemlist-row itemlist-heading">';
$output .= '<div class="itemlist-single-column"> </div>';
$output .= '<div class="itemlist-double-column"><input type="checkbox" name="checkCtrl" value="checkAll" onClick="check_all(this);" class="itemlist-cbox">title</div>';
$output .= '<div class="itemlist-single-column"> </div>';
$output .= '<div class="itemlist-single-column">toegevoegd op</div>';
$output .= '<div class="itemlist-single-column">';
if($linkname == 'banners') $output .= 'type banner';
$output .= '</div>';
$output .= '<div class="itemlist-single-column"> </div>';
$output .= '<div class="itemlist-single-column"> </div>';
$output .= '<div class="itemlist-single-column itemlist-edit">publish / edit</div>';
$output .= '</div>';
$fakeId = 1;
while($record = $result->fetch_assoc()){
//create productid
$itemId = $record['id'];
$published = $record['published'];
//title
$title = fromDatabase($record['title']);
if(strlen($title) >= 20){ $title = substr($title,0,32).'..'; }
$dateAdded = date('d / m / y',$record['timest']);
if($linkname == 'banners'){
$bannertype = $record['type'];
switch($bannertype){
case 1:
$bannertype = 'voorpagina';
break;
case 2:
$bannertype = 'middel';
break;
case 3:
$bannertype = 'klein';
break;
case 4:
$bannertype = 'tekstbanner';
break;
}
}
$output .= '<div class="itemlist-row">';
/* product id */
$output .= '<div class="itemlist-single-column">';
$displayId = str_pad($fakeId,2,"0",STR_PAD_LEFT);
($linkname == 'merken') ? $output .= 'id: ' . $itemId . '' : $output .= '#'.$displayId;
$output .= '</div>';
/* title */
$output .= '<div class="itemlist-double-column"><input type="checkbox" name="option[]" id="option" value="'.$itemId.'" class="itemlist-cbox"/>'.$title;
$output .= '</div>';
/* second (empty) */
$output .= '<div class="itemlist-single-column"> </div>';
/* third (empty) */
$output .= '<div class="itemlist-single-column">'.$dateAdded.'</div>';
/* fourth (empty) */
$output .= '<div class="itemlist-single-column">';
if($linkname == 'banners') $output .= $bannertype;
$output .= '</div>';
/* fifth (empty) */
$output .= '<div class="itemlist-single-column"> </div>';
/* sixth (empty) */
$output .= '<div class="itemlist-single-column"> </div>';
/* edit */
$output .= '<div class="itemlist-single-column itemlist-edit">';
$output .= '<a href="/admin/'.$linkname.'/update-published/'.$itemId.'/"><span class="itemlist-btn';
if($published == 1) $output .= ' active';
$output .= '" id="itemlist-btn-publish"></span></a>';
$output .= '<a href="/admin/'.$linkname.'/edit-item/'.$itemId.'/" class="itemlist-btn" id="itemlist-btn-edit"></a>';
$output .= '</div>';
$output .= '<div class="clear"></div>';
$output .= '</div>';
$fakeId++;
}
}
$output .= '</form>';
return $output;
}
}
function showItem($pageId){
$output = 'item';
return $output;
}
/* CHANGE ORDER */
function showChangeOrder($pageId){
/* access the global sql object */
global $mysqli;
/* language */
$siteLabels = getLanguage();
/* get info */
if(!$result = $mysqli->query('SELECT linkname,label FROM mp_pages WHERE id = "'.$pageId.'" LIMIT 1')){
//handle errors
$errors[] = $siteLabels['fm-somethingwentwrong'];
displayErrors($errors);
}
else{
$record = $result->fetch_assoc();
$linkname = $record['linkname'];
$label = $record['label'];
$output = '';
/* title */
$output .= '<h1>'.$label.' › '.$siteLabels['mp-changeorder'].'</h1>';
$output .= '<div class="double-column">';
//$output .= $siteLabels['changeOrderHelp'];
if(!isset($message)) $message = '';
//$output .= '<div id="message-box">'.$message.' Waiting for sortation submission...</div>';
$output .= '<form action="'.$_SERVER['REQUEST_URI'].'" id="dd-form" name="dd-form" method="post">';
/* $output .= '<p><input type="checkbox" value="1" name="auto_submit" id="auto_submit"';
if(isset($_POST['auto_submit'])) {
if($_POST['auto_submit']) { $output .= 'checked="checked"'; }
}
$output .= '/> <label for="auto_submit">Automatically submit on drop event</label></p>';*/
$output .= '<ul id="sortable-list">';
$order = array();
$count = 1;
//Get Info for the list
$query = 'SELECT p.id, p.title, p.itemOrder
FROM '.$linkname.' as p, content_status as s
WHERE s.active = 1
AND s.itemId = p.id
AND s.linkname = "'.$linkname.'"
ORDER BY p.itemOrder DESC';
$result = $mysqli->query($query);
while($item = $result->fetch_assoc())
{
//create productid
$itemId = $item['id'];
$output .= '<li title="'.$itemId.'">'.$item['title'].'</li>';
$order[] = $item['itemOrder'];
}
$output .= '</ul>';
$output .= '<br />';
$output .= '<input type="hidden" name="sort_order" id="sort_order" value="'.implode(',',$order).'" />';
$output .= '<input type="submit" name="do_submit" value="'.$siteLabels['form-save'].'" class="btn176 proceed" />';
$output .= '<input type="hidden" name="location" value="../"/>';
$output .= '<input type="submit" name="back" value="'.$siteLabels['form-back'].'" class="btn176" />';
$output .= '</form>';
$output .= '</div>';
$output .= '<script type="text/javascript"> initSortableList(); </script>';
return $output;
}
}
function sbmChangeOrder($pageId){
/* access the global sql object */
global $mysqli;
/* language */
$siteLabels = getLanguage();
/* get info */
if(!$result = $mysqli->query('SELECT linkname,label FROM mp_pages WHERE id = "'.$pageId.'" LIMIT 1')){
//handle errors
$errors[] = $siteLabels['fm-somethingwentwrong'];
displayErrors($errors);
}
else{
$record = $result->fetch_assoc();
$linkname = $record['linkname'];
$label = $record['label'];
/* split the value of the sortation */
$ids = explode(',',$_POST['sort_order']);
/* run the update query for each id */
foreach($ids as $index=>$id) {
$id = (int) $id;
if($id != '') {
$queries[] = 'UPDATE '.$linkname.' SET itemOrder = '.($index + 1).' WHERE id = '.$id.' LIMIT 1';
}
}
// Set autocommit to FALSE
$mysqli->autocommit(FALSE);
foreach($queries as $query)
{
if(!$mysqli->query($query))
{
$errors[] = $siteLabels['fm-somethingwentwrong'];
}
}
// ROLLBACK of COMMIT
if(isset($errors))
{
$mysqli->rollback();
displayErrors($errors);
//return showDashboard();
header('location: ../');
}
else
{
$mysqli->commit();
$_SESSION['feed-msg'] = '<span class="fm-suc"><span class="fm-suc-v">√</span>'.$siteLabels['fm-changeordersucces'].'</span>';
// return showDashboard();
header('location: ../');
}
}
}
/** Manage move to trash **/
function askMoveToTrash($pageId){
/* access the global sql object */
global $mysqli;
/* language */
$siteLabels = getLanguage();
if(isset($_POST['option'])){
/* get info */
if(!$result = $mysqli->query('SELECT linkname,label FROM mp_pages WHERE id = "'.$pageId.'" LIMIT 1')){
//handle errors
$errors[] = $siteLabels['fm-somethingwentwrong'];
displayErrors($errors);
}
else{
$record = $result->fetch_assoc();
$linkname = $record['linkname'];
$label = $record['label'];
$output = '';
//ask the question
$output .= $siteLabels['mp-movetotrash-areyousure'].'<br /><br />';
foreach($_POST['option'] as $itemId){
//get info
/* get info */
$query = 'SELECT p.title, p.timest
FROM '.$linkname.' as p
WHERE p.id = '.$itemId.'
LIMIT 1';
if($result = $mysqli->query($query)){
$record = $result->fetch_assoc();
//create productid
//title
$title = fromDatabase($record['title']);
$dateAdded = date('d / m / y',$record['timest']);
$output .= '<div class="itemlist-row">';
/* product id */
$output .= '<div class="itemlist-single-column"></div>';
/* title */
$output .= '<div class="itemlist-double-column">'.$title.'</div>';
/* second (empty) */
$output .= '<div class="itemlist-single-column"> </div>';
/* third (empty) */
$output .= '<div class="itemlist-single-column">'.$dateAdded.'</div>';
/* fourth (empty) */
$output .= '<div class="itemlist-single-column"> </div>';
/* fifth (empty) */
$output .= '<div class="itemlist-single-column"> </div>';
/* sixth (empty) */
$output .= '<div class="itemlist-single-column"> </div>';
/* edit */
$output .= '<div class="itemlist-single-column itemlist-edit"> </div>';
$output .= '<div class="clear"></div>';
$output .= '</div>';
}
}
$output .= '<div class="clear"></div>';
$output .= '<br /><br />';
$output .= '<form action="" name="moveToTrash" method="post" enctype="multipart/form-data">';
foreach($_POST['option'] as $itemId){
$output .= '<input type="hidden" name="items[]" value="'.$itemId.'" />';
}
$output .= '<input type="submit" name="moveToTrash" value="'.$siteLabels['form-yes'].'" class="btn176"/>';
$output .= '<input type="hidden" name="location" value="../"/>';
$output .= '<input type="submit" name="back" value="'.$siteLabels['form-no'].'" class="btn176 proceed"/>';
$output .= '</form>';
return $output;
}
}
else{
$errors[] = $siteLabels['fm-nothingselected'];
displayErrors($errors);
header('location: ./');
}
}
function moveToTrash($pageId){
/* access the global sql object */
global $mysqli;
/* language */
$siteLabels = getLanguage();
if(isset($_POST['items'])){
/* get info */
if(!$result = $mysqli->query('SELECT linkname,label FROM mp_pages WHERE id = "'.$pageId.'" LIMIT 1')){
//handle errors
$errors[] = $siteLabels['fm-somethingwentwrong'];
displayErrors($errors);
}
else{
$record = $result->fetch_assoc();
$linkname = $record['linkname'];
$label = $record['label'];
$output = '';
foreach($_POST['items'] as $itemId){
/* For each product; change content status to 0 */
$queries[] = 'UPDATE content_status SET active = 0 WHERE itemId ='.$itemId.' AND linkname = "'.$linkname.'"';
}
// Set autocommit to FALSE
$mysqli->autocommit(FALSE);
foreach($queries as $query)
{
if(!$mysqli->query($query))
{
$errors[] = $siteLabels['fm-somethingwentwrong'];
}
}
// ROLLBACK of COMMIT
if(isset($errors))
{
$mysqli->rollback();
displayErrors($errors);
}
else
{
$mysqli->commit();
$msg = str_replace('[numitems]', count($_POST['items']), $siteLabels['fm-itemsmovedtotrash']);
$_SESSION['feed-msg'] = '<span class="fm-suc"><span class="fm-suc-v">√</span>'.$msg.'</span>';
//header('location: ./');
return showDashboard($pageId);
}
}
}
}
function updatePublished(){
global $mysqli;
if(defined('URL_SUB2')){
$location = '../../';
$itemId = URL_SUB2;
$linkname = URL_PAGE;
if($result = $mysqli->query('SELECT published FROM '.$linkname.' WHERE id = '.$itemId.' LIMIT 1')){
$record = $result->fetch_assoc();
$published = $record['published'];
$published == 1 ? $newpublished = 0 : $newpublished = 1;
$mysqli->query('UPDATE '.$linkname.' SET published = '.$newpublished.' WHERE id = '.$itemId.' LIMIT 1');
}
}
else{
$location = '../';
}
header('location:'.$location);
}
?>