Files
friendshiptours.org/admin/linksandnumbers.php
2015-11-12 19:58:50 -06:00

188 lines
5.7 KiB
PHP

<?php
include '../php/classes/page.php';
include 'functions/func_xml.php';
$page = new Page();
$page->init_user();
# make sure the user is logged in before he has access to the
# admin section
if(!$page->user()->check_role('admin'))
header('Location: login.php?message=1');
$page->attr('title','Administrate FTI: Links and Numbers');
// to make jquery be loaded first
$page->useJSFile('jquery.min.js');
$page->addJSFile('admin.js');
$page->useCSSFile('admin.css');
$page->content('admin/linksandnumbers.php');
$page->layout('admin_layout.php');
$page->menu('admin/menu.php');
# get news posts
$linksandnumbers = xml_get_from_file($FILES['linksandnumbers']);
$items = $linksandnumbers->xpath('item');
$page->data('linksandnumbers',$items);
if(isset($_REQUEST['do']))
{
if($_REQUEST['do'] == 'add' || $_REQUEST['do'] == 'edit')
{
$page->addJSFile('admin/textboxbuttons.js');
// this may be changed below, if the item is not found
$page->content('admin/linksandnumbers.add.edit.php');
if($_GET['do'] == 'edit')
{
$item = FindItemById($items,$_GET['id']);
if(!is_null($item))
{
$page->data('item',$item);
$page->data('editing',true);
}
else
{
$page->addError('Links and numbers item does not exist. Please try again.');
$page->content('admin/linksandnumbers.php');
}
}
}
else if($_REQUEST['do'] == 'delete')
{
$item = FindItemById($items,$_GET['id']);
if(!is_null($item))
{
$page->content('admin/linksandnumbers.delete.php');
$page->data('item',$item);
}
else
$page->addError('Links and items number does not exist. Please try again.');
}
else if($_REQUEST['do'] == 'reallyadd')
{
$newItem = $linksandnumbers->addChild('item');
$newItem->addChild('title',stripslashes($_POST['title']));
$newItem->addChild('subtitle',stripslashes($_POST['subtitle']));
$newItem->addChild('type',$_POST['type']);
$newItem->addChild('text',"<![CDATA[".stripslashes($_POST['text'])."]]>");
if(!empty($_POST['title']) && !empty($_POST['text']))
{
$nextId = GetNextId($items);
$newItem->addAttribute('id',$nextId);
if(file_put_contents($FILES['linksandnumbers'],xml_fix_cdata($linksandnumbers->asXML())))
{
$page->addMessage('Links and numbers item added.');
$page->data('linksandnumbers',$linksandnumbers->xpath('item'));
}
else
{
$page->addError('Could not add links and numbers item. Please try again later.');
$page->data('item',$newItem);
$page->addJSFile('admin/textboxbuttons.js');
$page->content('admin/linksandnumbers.add.edit.php');
}
}
else
{
$page->addError('Please enter a title and text.');
$page->data('item',$newItem);
$page->addJSFile('admin/textboxbuttons.js');
$page->content('admin/linksandnumbers.add.edit.php');
}
}
else if($_REQUEST['do'] == 'reallyedit')
{
$itemIdx = FindItemById($items,$_POST['id'],true);
$page->data('editing',true);
if(!is_null($itemIdx))
{
$linksandnumbers->item[$itemIdx]->title = stripslashes($_POST['title']);
$linksandnumbers->item[$itemIdx]->subtitle = stripslashes($_POST['subtitle']);
$linksandnumbers->item[$itemIdx]->type = $_POST['type'];
$linksandnumbers->item[$itemIdx]->text = "<![CDATA[".stripslashes($_POST['text'])."]]>";
if(!empty($_POST['title']) && !empty($_POST['text']))
{
if(file_put_contents($FILES['linksandnumbers'],xml_fix_cdata($linksandnumbers->asXML())))
$page->addMessage('Links and numbers item saved.');
else
{
$page->addError('Could not save links and numbers item. Please try again later.');
$page->data('item',$linksandnumbers->item[$itemIdx]);
$page->addJSFile('admin/textboxbuttons.js');
$page->content('admin/linksandnumbers.add.edit.php');
}
}
else
{
$page->addError('Please enter a title and text.');
$page->data('item',$linksandnumbers->item[$itemIdx]);
$page->addJSFile('admin/textboxbuttons.js');
$page->content('admin/linksandnumbers.add.edit.php');
}
}
else
$page->addError('Links and numbers item does not exit. Please try again.');
}
else if($_REQUEST['do'] == 'reallydelete')
{
$itemIdx = FindItemById($items,$_POST['id'],true);
if(!is_null($itemIdx))
{
unset($linksandnumbers->item[$itemIdx]);
if(file_put_contents($FILES['linksandnumbers'],xml_fix_cdata($linksandnumbers->asXML())))
{
$page->addMessage('Links and numbers item deleted.');
$page->data('linksandnumbers',$linksandnumbers->xpath('item'));
}
else
{
$page->addError('Could not delete news item. Please try again later.');
$page->data('item',$linksandnumbers->item[$itemIdx]);
$page->content('admin/linksandnumbers.delete.php');
}
}
else
$page->addError('Links and numbers item does not exist. Please try again.');
}
}
$page->process();
################################################################# FindItemById #
function FindItemById($items, $itemId, $returnIdx = false)
{
for($c = 0; $c < count($items); $c++)
{
if((int)$items[$c]['id'] === (int)$itemId)
if($returnIdx)
return $c;
else
return $items[$c];
}
return NULL;
}
#################################################################### GetNextId #
function GetNextId($items)
{
$largestId = 1;
for($c = 0; $c < count($items); $c++)
{
if($largestId < (int)$items[$c]['id'])
$largestId = (int)$items[$c]['id'];
}
return $largestId + 1;
}
?>