<?php
session_start();
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
include_once('../inc/config.php');
include_once('../inc/util.inc');
include_once('../inc/widget_class.php');
mysql_connect($db_server, $db_username, $db_password);
mysql_select_db($db_database);
$widget = new Widget();
//------------------------------------------------------------------------------
// Setup Theme
//------------------------------------------------------------------------------
$THEME = array(
'site_name' => getSiteName(),
'path' => '../',
'page' => 'widgets',
'page_title' => _('Administration'),
'theme_path' => getTheme(),
'modules' => getAdminModules(),
);
$THEME['javascript'] = '
<script type="text/javascript">
//<![CDATA[
Event.observe(window, \'load\', function() {
initHideControls();
initHighlight();
});
function initHideControls() {
if (!$$(\'.controls\')) { return; }
$$(\'.controls\').each(function(item) {
item.addClassName(\'hidecontrols\');
});
}
function initHighlight() {
if (!$$(\'#layout .clearfix div\')) { return; }
$$(\'#layout .clearfix div\').each(function(item) {
var ctrl = item.down().next(\'.controls\');
item.observe(\'mouseover\', function() {
if (ctrl)
ctrl.removeClassName(\'hidecontrols\');
});
item.observe(\'mouseout\', function() {
if (ctrl)
ctrl.addClassName(\'hidecontrols\');
});
});
}
//]]>
</script>';
//------------------------------------------------------------------------------
// Control Structure
//------------------------------------------------------------------------------
include $THEME['path'].$THEME['theme_path'].'adminheader.php';
if (!isLoggedInAdmin()) {
handleBadLogin();
return;
}
if (isset($_GET['up']) && isset($_GET['to'])) {
moveWidgetUp($_GET['up'], $_GET['to']);
$widget->displayCurrentWidgets();
} elseif (isset($_GET['down']) && isset($_GET['to'])) {
moveWidgetDown($_GET['down'], $_GET['to']);
$widget->displayCurrentWidgets();
} elseif (isset($_GET['right']) && isset($_GET['row']) && isset($_GET['pos'])) {
moveWidgetRight($_GET['right'], $_GET['row'], $_GET['pos']);
$widget->displayCurrentWidgets();
} elseif (isset($_GET['left']) && isset($_GET['row']) && isset($_GET['pos'])) {
moveWidgetLeft($_GET['left'], $_GET['row'], $_GET['pos']);
$widget->displayCurrentWidgets();
} elseif (isset($_GET['expand']) && isset($_GET['row']) && isset($_GET['pos'])) {
expandWidget($_GET['expand'], $_GET['row'], $_GET['pos']);
$widget->displayCurrentWidgets();
} elseif (isset($_GET['shrink'])) {
shrinkWidget($_GET['shrink']);
$widget->displayCurrentWidgets();
} elseif (isset($_GET['delete']) && isset($_GET['type']) && isset($_GET['row'])) {
deleteWidget($_GET['delete'], $_GET['type'], $_GET['row']);
$widget->displayCurrentWidgets();
} elseif (isset($_GET['edit']) && isset($_GET['type'])) {
$widget->displayEditWidgetForm($_GET['type'], $_GET['edit']);
} elseif (isset($_POST['add-widget'])) {
$widget->displayEditWidgetForm($_POST['type']);
} elseif (isset($_POST['edit-widget'])) {
handleEdit($_POST);
} else {
$widget->displayCurrentWidgets();
}
include $THEME['path'].$THEME['theme_path'].'footer.php';
return;
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
function handleBadLogin ()
{
echo '
<div class="error-alert">'._('You must logged in to view this page.').'</div>';
displayLogin();
include $THEME['path'].$THEME['theme_path'].'footer.php';
}
function moveWidgetUp ($up, $to)
{
global $widget;
$up = (int)$up;
$to = (int)$to;
$sql = "SELECT `position`
FROM `sam_layout`
WHERE `row` = $to
ORDER BY `row`, `position`";
$result = mysql_query($sql);
if (!$result) {
$error = _('Could not get layout information.');
displayError(__FILE__, __LINE__, mysql_error(), $sql);
return;
}
$positions = array();
if (mysql_num_rows($result) > 0) {
while ($r = mysql_fetch_assoc($result)) {
$positions[] = $r['position'];
}
}
$newPosition = $widget->validatePositionMove($positions);
$positions = '';
if ($newPosition) {
$sql = "UPDATE `sam_layout`
SET `row` = $to,
`position` = $newPosition
WHERE `id` = $up";
if (!mysql_query($sql)) {
$error = _('Could not move widget.');
displayError(__FILE__, __LINE__, mysql_error(), $sql);
return;
}
} else {
echo '
<div class="error-alert">'.sprintf(_('Row %s is full. Please make room and try again.'), $to).'</div>';
}
}
function moveWidgetDown ($down, $to)
{
global $widget;
$to = (int)$to;
$down = (int)$down;
$curRow = $to - 1;
$sql = "(
SELECT `position`, 'cur' AS 'type'
FROM `sam_layout`
WHERE `row` = $curRow ORDER BY `row`, `position`
)
UNION
(
SELECT `position`, 'to' AS 'type'
FROM `sam_layout`
WHERE `row` = $to ORDER BY `row`, `position`
)";
$result = mysql_query($sql);
if (!$result) {
$error = _('Could not get layout information');
displayError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
$positions = '';
$current = 0;
if (mysql_num_rows($result) > 0) {
while ($r = mysql_fetch_assoc($result)) {
if ($r['type'] == 'to') {
$positions[] = $r['position'];
} else {
$current++;
}
}
}
if ($current == 1) {
echo '
<div class="error-alert">'.sprintf(_('Cannot move down. Row %s will be empty.'), $curRow).'</div>';
} else {
$newPosition = $widget->validatePositionMove($positions);
if ($newPosition) {
$sql = "UPDATE `sam_layout`
SET `row` = $to,
`position` = $newPosition
WHERE `id` = $down";
if (!mysql_query($sql)) {
$error = _('Could not move widget.');
displayError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
} else {
echo '
<div class="error-alert">'.sprintf(_('Row %s is full. Please make room and try again.'), $to).'</div>';
}
}
}
function moveWidgetRight ($right, $row, $pos)
{
global $widget;
$right = (int)$right;
$row = (int)$row;
$pos = (int)$pos;
$sql = "SELECT `position`
FROM `sam_layout`
WHERE `row` = $row
ORDER BY `row`, `position`";
$result = mysql_query($sql);
if (!$result) {
$error = _('Could not get layout information.');
displayError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
$positions = '';
if (mysql_num_rows($result) > 0) {
while ($r = mysql_fetch_assoc($result)) {
$positions[] = $r['position'];
}
}
$newPosition = $widget->validatePositionMoveRight($pos, $positions);
if ($newPosition) {
$sql = "UPDATE `sam_layout`
SET `position` = $newPosition
WHERE `id` = $right";
if (!mysql_query($sql)) {
$error = _('Could not move widget.');
displayError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
} else {
echo '
<div class="error-alert">'._('No room to move widget to. Please make room and try again.').'</div>';
}
}
function moveWidgetLeft ($left, $row, $pos)
{
global $widget;
$left = (int)$left;
$row = (int)$row;
$pos = (int)$pos;
$sql = "SELECT `position`
FROM `sam_layout`
WHERE `row` = $row
ORDER BY `row`, `position`";
$result = mysql_query($sql);
if (!$result) {
$error = _('Could not get layout information.');
display($error, __FILE__, __LINE__, mysql_error(), sql);
return;
}
$positions = '';
if (mysql_num_rows($result) > 0) {
while ($r = mysql_fetch_assoc($result)) {
$positions[] = $r['position'];
}
}
$newPosition = $widget->validatePositionMoveLeft($pos, $positions);
if ($newPosition) {
$sql = "UPDATE `sam_layout`
SET `position` = $newPosition
WHERE `id` = $left";
if (!mysql_query($sql)) {
$error = _('Could not move widget.');
displayError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
} else {
echo '
<div class="error-alert">'._('No room to move widget to. Please make room and try again.').'</div>';
}
}
function expandWidget ($expand, $row, $pos)
{
global $widget;
$expand = (int)$expand;
$row = (int)$row;
$pos = (int)$pos;
$sql = "SELECT `position`
FROM `sam_layout`
WHERE `row` = $row
ORDER BY `row`, `position`";
$result = mysql_query($sql);
if (!$result) {
$error = _('Could not get layout information.');
displayError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
$positions = '';
if (mysql_num_rows($result) > 0) {
while ($r = mysql_fetch_assoc($result)) {
$positions[] = $r['position'];
}
}
$newPosition = $widget->validatePositionExpand($pos, $positions);
if ($newPosition) {
$sql = "UPDATE `sam_layout`
SET `position` = $newPosition
WHERE `id` = $expand";
if (!mysql_query($sql)) {
$error = _('Could not move widget.');
displayError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
} else {
echo '
<div class="error-alert">'._('No room to expand widget. Please make room and try again.').'</div>';
}
}
function shrinkWidget ($shrink)
{
global $widget;
$shrink = (int)$shrink;
$sql = "SELECT `position`
FROM `sam_layout`
WHERE `id` = $shrink
LIMIT 1";
$result = mysql_query($sql);
if (!$result) {
$error = _('Could not get layout information.');
displayError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
$r = mysql_fetch_assoc($result);
$newPosition = $widget->validatePositionShrink($r['position']);
if ($newPosition) {
$sql = "UPDATE `sam_layout`
SET `position` = $newPosition
WHERE `id` = $shrink";
if (!mysql_query($sql)) {
$error = _('Could not move widget.');
displayError($error, __FILE__, __LINE__, mysql_error(), $sql);
return;
}
} else {
echo '
<div class="error-alert">'._('Cannot shrink widget any smaller.').'</div>';
}
}
function deleteWidget ($delete, $type, $row)
{
$delete = (int)$delete;
$row = (int)$row;
// Get all the data
$count = 0;
$data = '';
$max = 0;
$sql = "SELECT `data`, 0 AS 'position', 0 AS 'max', 'data' AS 'type'
FROM `sam_layout`
WHERE `id` = $delete
UNION
(
SELECT 0, `position`, 0, 'pos' AS 'type'
FROM `sam_layout`
WHERE `row` = $row
ORDER BY `row`, `position`
)
UNION
SELECT 0, 0, MAX(`row`), 'max' AS 'type'
FROM `sam_layout`";
$result = mysql_query($sql) or die('ERROR ' . __FILE__ . ' [' . __LINE__ . ']<br/><br/>' . mysql_error() . "<br/><br/>$sql");
if (mysql_num_rows($result) > 0) {
while ($r = mysql_fetch_assoc($result)) {
if ($r['type'] == 'pos') $count++;
if ($r['type'] == 'max') $max = $r['max'];
if ($r['type'] == 'data') $data = $r['data'];
}
}
// If a middle row only had 1 widget and it was deleted, need to move all rows up 1
if ($count == 1 && $row != $max) {
// TODO
// write a function to do this
}
// Delete the image from the server
if ($type == 'image') {
unlink('../uploads/'.$data);
}
// Delete the record from the DB
$sql = "DELETE FROM `sam_layout` WHERE `id` = $delete LIMIT 1";
mysql_query($sql) or die('ERROR ' . __FILE__ . ' [' . __LINE__ . ']<br/><br/>' . mysql_error() . "<br/><br/>$sql");
}
function handleEdit ($params)
{
global $widget;
if ($params['id'] > 0) {
$ok = $widget->editExistingWidget($params);
} else {
$ok = $widget->addNewWidget($params);
}
if (!$ok) {
$widget->displayEditWidgetForm($params['type']);
return;
}
$widget->displayCurrentWidgets();
}