\n";
if ($label != null)
echo "
$label | \n";
if ($selected == null)
$selected = (!isset($_POST[$name]) ? "orders" : $_POST[$name]);
echo " | \n";
echo "\n";
}
//---------------------------------------------------------------------------------------------
function check_data()
{
return true;
}
/**
* @return Returns the array sorted as required
* @param $aryData Array containing data to sort
* @param $strIndex name of column to use as an index
* @param $strSortBy Column to sort the array by
* @param $strSortType String containing either asc or desc [default to asc]
* @desc Naturally sorts an array using by the column $strSortBy
*/
function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
{
// if the parameters are invalid
if (!is_array($aryData) || !$strIndex || !$strSortBy)
// return the array
return $aryData;
// create our temporary arrays
$arySort = $aryResult = array();
// loop through the array
foreach ($aryData as $aryRow)
// set up the value in the array
$arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
// apply the natural sort
natsort($arySort);
// if the sort type is descending
if ($strSortType=="desc")
// reverse the array
arsort($arySort);
// loop through the sorted and original data
foreach ($arySort as $arySortKey => $arySorted)
foreach ($aryData as $aryOriginal)
// if the key matches
if ($aryOriginal[$strIndex]==$arySortKey)
// add it to the output array
array_push($aryResult, $aryOriginal);
// return the return
return $aryResult;
}
function write_modules()
{
global $path_to_root, $installed_modules;
$mods = array_natsort($installed_modules, 'tab', 'tab');
$installed_modules = $mods;
//reset($installed_languages);
$n = count($installed_modules);
$msg = " ";
for ($i = 0; $i < $n; $i++)
{
if ($i > 0)
$msg .= "\t\tarray ";
else
$msg .= "array ";
$msg .= "('tab' => '" . $installed_modules[$i]['tab'] . "', ";
$msg .= "'name' => '" . $installed_modules[$i]['name'] . "', ";
$msg .= "'path' => '" . $installed_modules[$i]['path'] . "', ";
$msg .= "'filename' => '" . $installed_modules[$i]['filename'] . "'";
$msg .= "),\n";
}
$msg .= "\t);\n?>";
$filename = $path_to_root . "/modules/installed_modules.php";
// Check if the file exists and is writable first.
if (file_exists($filename) && is_writable($filename))
{
if (!$zp = fopen($filename, 'w'))
{
display_error(_("Cannot open the modules file - ") . $filename);
return false;
}
else
{
if (!fwrite($zp, $msg))
{
display_error(_("Cannot write to the modules file - ") . $filename);
fclose($zp);
return false;
}
// Close file
fclose($zp);
}
}
else
{
display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
return false;
}
return true;
}
//---------------------------------------------------------------------------------------------
function handle_submit()
{
global $path_to_root, $installed_modules, $db_connections;
if (!check_data())
return false;
$id = $_GET['id'];
$installed_modules[$id]['tab'] = $_POST['tab'];
$installed_modules[$id]['name'] = $_POST['name'];
$installed_modules[$id]['path'] = $_POST['path'];
$directory = $path_to_root . "/modules/" . $_POST['path'];
if (!file_exists($directory))
{
mkdir($directory);
}
if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
{
$installed_modules[$id]['filename'] = $_FILES['uploadfile']['name'];
$file1 = $_FILES['uploadfile']['tmp_name'];
$file2 = $directory . "/".$_FILES['uploadfile']['name'];
if (file_exists($file2))
unlink($file2);
move_uploaded_file($file1, $file2);
}
else
$installed_modules[$id]['filename'] = $_POST['filename'];
if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
{
$file1 = $_FILES['uploadfile2']['tmp_name'];
$file2 = $directory . "/".$_FILES['uploadfile2']['name'];
if (file_exists($file2))
unlink($file2);
move_uploaded_file($file1, $file2);
$db_name = $_SESSION["wa_current_user"]->company;
db_import($file2, $db_connections[$db_name]);
}
if (!write_modules())
return false;
return true;
}
//---------------------------------------------------------------------------------------------
function handle_delete()
{
global $path_to_root, $installed_modules;
$id = $_GET['id'];
$path = $installed_modules[$id]['path'];
$filename = "$path_to_root/modules/$path";
if ($h = opendir($filename))
{
while (($file = readdir($h)) !== false)
{
if (is_file("$filename/$file"))
unlink("$filename/$file");
}
closedir($h);
}
rmdir($filename);
unset($installed_modules[$id]);
$mods = array_values($installed_modules);
$installed_modules = $mods;
if (!write_modules())
return;
meta_forward($_SERVER['PHP_SELF']);
}
//---------------------------------------------------------------------------------------------
function display_modules()
{
global $table_style, $installed_modules;
echo "
";
start_table($table_style);
$th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), "", "");
table_header($th);
$k = 0;
$mods = $installed_modules;
$n = count($mods);
for ($i = 0; $i < $n; $i++)
{
alt_table_row_color($k);
label_cell(get_tab_title($mods[$i]['tab']));
label_cell($mods[$i]['name']);
label_cell($mods[$i]['path']);
label_cell($mods[$i]['filename']);
edit_link_cell("selected_id=" . $i);
label_cell("" . _("Delete") . "");
end_row();
}
end_table();
}
//---------------------------------------------------------------------------------------------
function display_module_edit($selected_id)
{
global $installed_modules, $table_style2;
if ($selected_id != -1)
$n = $selected_id;
else
$n = count($installed_modules);
start_form(true, true);
echo "
";
start_table($table_style2);
if ($selected_id != -1)
{
$mod = $installed_modules[$selected_id];
$_POST['tab'] = $mod['tab'];
$_POST['name'] = $mod['name'];
$_POST['path'] = $mod['path'];
$_POST['filename'] = $mod['filename'];
hidden('selected_id', $selected_id);
hidden('filename', $_POST['filename']);
}
tab_list_row(_("Menu Tab"), 'tab', null);
text_row_ex(_("Name"), 'name', 30);
text_row_ex(_("Folder"), 'path', 20);
label_row(_("Module File"), "");
label_row(_("SQL File"), "");
end_table(0);
display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
echo "";
end_form();
}
//---------------------------------------------------------------------------------------------
if (isset($_GET['c']))
{
if ($_GET['c'] == 'df')
{
handle_delete();
}
if ($_GET['c'] == 'u')
{
if (handle_submit())
{
//meta_forward($_SERVER['PHP_SELF']);
}
}
}
//---------------------------------------------------------------------------------------------
display_modules();
hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
display_module_edit($selected_id);
//---------------------------------------------------------------------------------------------
end_page();
?>