Add and use objectManager to display and edit vendors and distributions. Based on a patch by

Chris Morgan
This commit is contained in:
Alexander Nicolaysen Sørnes
2007-02-03 19:03:42 +00:00
committed by WineHQ
parent 00729ac39d
commit 0ad148f69f
9 changed files with 496 additions and 99 deletions

View File

@@ -1,42 +0,0 @@
<?php
require("path.php");
require(BASE."include/incl.php");
require_once(BASE."include/distribution.php");
if(!$_SESSION['current']->hasPriv("admin"))
util_show_error_page_and_exit("Insufficient privileges.");
$oDistribution = new distribution($aClean['iDistributionId']);
if($aClean['sSubmit'])
{
$oDistribution->GetOutputEditorValues($aClean);
if($oDistribution->iDistributionId)
$oDistribution->update();
else
{
$oDistribution->create();
}
util_redirect_and_exit(apidb_fullurl("distributionView.php"));
}
else
{
if ($oDistribution->iDistributionId)
apidb_header("Edit Distribution");
else
apidb_header("Add Distribution");
echo '<form name="sQform" action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data">',"\n";
$oDistribution->outputEditor();
echo '<tr valign=top><td class=color3 align=center colspan=2>',"\n";
echo '<input name="sSubmit" type="submit" value="Submit" class="button" >&nbsp',"\n";
echo '</td></tr>',"\n";
echo "</form>";
echo html_frame_end("&nbsp;");
apidb_footer();
}
?>

View File

@@ -1,37 +0,0 @@
<?php
require("path.php");
require(BASE."include/incl.php");
require_once(BASE."include/vendor.php");
if(!$_SESSION['current']->hasPriv("admin"))
util_show_error_page_and_exit();
$oVendor = new Vendor($aClean['iVendorId']);
if($aClean['sSubmit'])
{
$oVendor->getOutputEditorValues($aClean);
$oVendor->update();
util_redirect_and_exit(apidb_fullurl("vendorview.php"));
}
else
{
if($oVendor->iVendorId)
apidb_header("Edit Vendor");
else
apidb_header("Add Vendor");
// Show the form
echo '<form name="sQform" action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data">',"\n";
$oVendor->outputEditor();
echo '<tr valign=top><td class=color3 align=center colspan=2>',"\n";
echo '<input name="sSubmit" type="submit" value="Submit" class="button" >&nbsp',"\n";
echo '</td></tr>',"\n";
echo "</form>";
echo html_frame_end("&nbsp;");
apidb_footer();
}
?>

View File

@@ -363,7 +363,6 @@ class distribution {
function outputEditor()
{
echo html_frame_start("Distribution Form", "90%", "", 0);
echo "<table width='100%' border=0 cellpadding=2 cellspacing=0>\n";
// Name
@@ -376,7 +375,6 @@ class distribution {
echo '<input type="hidden" name="iDistributionId" value="'.$this->iDistributionId.'">',"\n";
echo "</table>\n";
echo html_frame_end();
}
/* retrieves values from $_REQUEST that were output by outputEditor() */
@@ -459,11 +457,11 @@ class distribution {
return new distribution($oRow->distributionId, $oRow);
}
function objectOutputTableRow($sClass = "")
function objectOutputTableRow($oManager, $sClass)
{
$aCells = array(
"<a href=\"".BASE."distributionView.php?iDistributionId=".
$this->iDistributionId."\">$this->sName.</a>",
"<a href=\"".$oManager->makeUrl("view", $this->iDistributionId,
"View distribution")."\">$this->sName.</a>",
"<a href=\"$this->sUrl\">$this->sUrl</a>",
array(sizeof($this->aTestingIds), "align=\"right\""));
@@ -471,12 +469,12 @@ class distribution {
{
if(!sizeof($this->aTestingIds))
{
$sDelete = " &nbsp; [<a href='".$_SERVER['PHP_SELF']."?sSub=delete&".
"iDistributionId=$this->iDistributionId'>delete</a>]";
$sDelete = " &nbsp; [<a href='".$oManager->makeUrl("delete",
$this->iDistributionId)."'>delete</a>]";
}
$aCells[3] = array(
"[<a href='".BASE."admin/editDistribution.php?iDistributionId=".
$this->iDistributionId."'>edit</a>]$sDelete",
"[<a href='".$oManager->makeUrl("edit", $this->iDistributionId,
"Edit Distribution")."'>edit</a>]$sDelete",
"align=\"center\"");
}

288
include/objectManager.php Normal file
View File

@@ -0,0 +1,288 @@
<?php
/* class for managing objects */
/* - handles processing of queued objects */
/* - handles the display and editing of unqueued objects */
class ObjectManager
{
var $sClass;
var $bIsQueue;
var $sTitle;
var $iId;
function ObjectManager($sClass, $sTitle = "list", $iId = false)
{
$this->sClass = $sClass;
$this->sTitle = $sTitle;
$this->iId = $iId;
}
/* Check whether the associated class has the given method */
function checkMethod($sMethodName, $bEnableOutput)
{
if(!method_exists($this->sClass, $sMethodName))
{
if($bEnableOutput) echo "class '".$this->sClass."' lacks method '".$sMethodName."'\n";
return false;
}
return true;
}
/* Check whether the specified methods are valid */
function checkMethods($aMethods, $bExit = true)
{
foreach($aMethods as $sMethod)
{
if(!$this->checkMethod($sMethod, false))
{
echo "Selected class does not support this operation ".
"(missing '$sMethod()')\n";
if($bExit)
exit;
else
return FALSE;
}
}
return TRUE;
}
/* displays the list of entries */
function display_table()
{
$this->checkMethods(array("ObjectGetEntries", "ObjectOutputHeader",
"ObjectGetInstanceFromRow", "ObjectOutputTableRow"));
/* query the class for its entries */
/* We pass in $this->bIsQueue to tell the object */
/* if we are requesting a list of its queued objects or */
/* all of its objects */
$hResult = call_user_func(array($this->sClass,
"objectGetEntries"), $this->bIsQueue);
/* did we get any entries? */
if(mysql_num_rows($hResult) == 0)
{
$sIsQueue = $this->bIsQueue ? "true" : "false";
if($this->bIsQueue)
echo "<center>The queue for '$this->sClass' is empty</center>";
else
echo "<center>No entries of '$this->sClass' are present</center>";
echo "<br /><center><a href=\"".$_SERVER['PHP_SELF']."?sClass=".
"$this->sClass&bIsQueue=$sIsQueue&sTitle=".
urlencode($this->sTitle)."&sAction=add\">Add an entry?</a></center>";
return;
}
/* output the header */
echo '<table width="100%" border="0" cellpadding="3" cellspacing="0">';
call_user_func(array($this->sClass,
"objectOutputHeader"), "color4");
/* output each entry */
for($iCount = 0; $oRow = mysql_fetch_object($hResult); $iCount++)
{
$oObject = call_user_func(array($this->sClass,
"objectGetInstanceFromRow"), $oRow);
$oObject->objectOutputTableRow($this,
($iCount % 2) ? "color0" : "color1");
}
echo "</table>";
$oObject = new $this->sClass();
if($oObject->canEdit())
{
echo "<br /><br /><a href=\"".$_SERVER['PHP_SELF']."?sClass=".
"$this->sClass&sAction=add&sTitle=Add\">".
"Add entry</a>\n";
}
}
/* display the entry for editing */
function display_entry_for_editing($sBackLink)
{
$this->checkMethods(array("outputEditor", "getOutputEditorValues",
"update", "create"));
// link back to the previous page
echo html_back_link(1, $sBackLink);
echo '<form name="sQform" action="'.BASE.'objectManager.php?sClass='.
$this->sClass."&bIsQueue=".($this->bIsQueue ? "true" : "false").
' method="post" enctype="multipart/form-data">',"\n";
echo '<input type="hidden" name="sClass" value="'.$this->sClass.'" />';
echo '<input type="hidden" name="sTitle" value="'.$this->sTitle.'" />';
echo '<input type="hidden" name="iId" value="'.$this->iId.'" />';
echo '<input type="hidden" name="bIsQueue" '.
'value='.($this->bIsQueue ? "true" : "false").'>';
$oObject = new $this->sClass($this->iId);
$oObject->outputEditor();
/* if this is a queue add a dialog for replying to the submitter of the
queued entry */
if($this->bIsQueue)
{
echo html_frame_start("Reply text", "90%", "", 0);
echo "<table width='100%' border=0 cellpadding=2 cellspacing=0>\n";
echo '<tr valign=top><td class="color0"><b>email Text</b></td>',"\n";
echo '<td><textarea name="sReplyText" style="width: 100%" cols="80" '.
'rows="10"></textarea></td></tr>',"\n";
/* buttons for operations we can perform on this entry */
echo '<tr valign=top><td class=color3 align=center colspan=2>' ,"\n";
echo '<input name="sSub" type="submit" value="Submit" class="button" '.
'/>',"\n";
echo '<input name="sSub" type="submit" value="Delete" class="button" '.
'/>',"\n";
echo '<input name="sSub" type="submit" value="Reject" class="button" '.
'/>',"\n";
echo '<input name="sSub" type="submit" value="Cancel" class="button" '.
'/>',"\n";
echo '</td></tr>',"\n";
echo '</table>';
echo html_frame_end();
} else
{
echo '<tr valign=top><td class=color3 align=center colspan=2>',"\n";
echo '<input name="sSubmit" type="submit" value="Save" class="button">'.
'&nbsp',"\n";
echo "</td></tr>\n";
}
echo '</form>';
}
/* Display help for queue processing */
function display_queue_processing_help()
{
/* No help text defined, so do nothing */
if(!method_exists($this->sClass, "ObjectDisplayQueueProcessingHelp"))
return FALSE;
call_user_func(array($this->sClass,
"ObjectDisplayQueueProcessingHelp"));
}
/* Delete the object associated with the given id */
function delete_entry()
{
$this->checkMethods(array("delete", "canEdit"));
$oObject = new $this->sClass($this->iId);
if(!$oObject->canEdit())
return FALSE;
if($oObject->delete())
util_redirect_and_exit($this->makeUrl("view", false));
else
echo "Failure.\n";
}
/* Display screen for submitting a new entry of given type */
function add_entry($sBackLink)
{
$this->checkMethods(array("outputEditor", "getOutputEditorValues",
"update", "create"));
$oObject = new $this->sClass();
echo "<form method=\"post\">\n";
$oObject->outputEditor();
echo "<input type=\"hidden\" name=\"sClass=\"distribution\" />\n";
echo "<input type=\"hidden\" name=\"sTitle\" value=\"$this->sTitle\" />\n";
echo "<div align=\"center\">";
echo "<input type=\"submit\" class=\"button\" value=\"Submit\" ".
"name=\"sSubmit\" />\n";
echo "</div></form>\n";
echo html_back_link(1, $sBackLink);
}
/* View an entry */
function view($sBackLink)
{
$this->checkMethods(array("display"));
$oObject = new $this->sClass($this->iId);
$oObject->display();
echo html_back_link(1, $sBackLink);
}
/* Process form data generated by adding or updating an entry */
function processForm($aClean)
{
if(!$aClean['sSubmit'])
return;
$this->checkMethods(array("getOutputEditorValues", "update", "create",
"canEdit"));
$this->iId = $this->getIdFromInput($aClean);
$oObject = new $this->sClass($this->iId);
$oObject->getOutputEditorValues($aClean);
if($this->iId)
{
if(!$oObject->canEdit())
return FALSE;
$oObject->update();
}
else
$oObject->create();
$sIsQueue = $tihs->bIsQueue ? "true" : "false";
util_redirect_and_exit($this->makeUrl("view", false, "$this->sClass list"));
}
/* Make an objectManager URL based on the object and optional parameters */
function makeUrl($sAction = false, $iId = false, $sTitle = false)
{
if($iId)
$sId = "&iId=$iId";
if($sAction)
$sAction = "&sAction=$sAction";
$sIsQueue = $this->bIsQueue ? "true" : "false";
if(!$sTitle)
$sTitle = $this->sTitle;
$sTitle = urlencode($sTitle);
return $_SERVER['PHP_SELF']."?bIsQueue=$sIsQueue&sClass=$this->sClass".
"&sTitle=$sTitle$sId$sAction";
}
/* Get id from form data */
function getIdFromInput($aClean)
{
$sId = "i".ucfirst($this->sClass)."Id";
return $aClean[$sId];
}
}
?>

View File

@@ -28,9 +28,8 @@ function global_sidebar_menu()
$g->add("Submit Application", BASE."appsubmit.php?sSub=view&sAppType=application");
$g->add("Help &amp; Documentation", BASE."help/");
$g->add("AppDB Stats", BASE."appdbStats.php");
$g->add("View Distributions (".distribution::getNumberOfDistributions(false).")", BASE."distributionView.php");
$g->add("View Vendors (".getNumberOfvendors().")", BASE."vendorview.php");
$g->add("View Distributions (".distribution::getNumberOfDistributions(false).")", BASE."objectManager.php?sClass=distribution&bIsQueue=false&sTitle=View%20Distributions");
$g->add("View Vendors (".getNumberOfvendors().")", BASE."objectManager.php?sClass=vendor&bIsQueue=false&sTitle=View%20Vendors");
$g->add("Email your suggestions for improving the AppDB", "mailto:appdb@winehq.org");
$g->done();

View File

@@ -61,6 +61,9 @@ class Vendor {
*/
function create()
{
if(!$this->canEdit())
return FALSE;
$hResult = query_parameters("INSERT INTO vendor (vendorName, vendorURL) ".
"VALUES ('?', '?')",
$this->sName, $this->sWebpage);
@@ -182,23 +185,23 @@ class Vendor {
return new vendor($oRow->vendorId, $oRow);
}
function objectOutputTableRow($sClass = "")
function objectOutputTableRow($oObject, $sClass = "")
{
$aCells = array(
"<a href=\"".BASE."vendorview.php?iVendorId=$this->iVendorId\">".
"$this->sName</a>",
"<a href=\"".$oObject->makeUrl("view", $this->iVendorId,
"View Vendor")."\">$this->sName</a>",
"<a href=\"$this->sWebpage\">$this->sWebpage</a>",
array(sizeof($this->aApplicationsIds), "align=\"right\""));
if($this->canEdit())
{
if(!sizeof($this->aApplicationsIds))
$sDelete = " &nbsp; [<a href=\"".BASE."vendorview.php?sSub=delete&".
"iVendorId=$this->iVendorId\">".
$sDelete = " &nbsp; [<a href=\"".$oObject->makeUrl("delete",
$this->iVendorId, "View Vendors")."\">".
"delete</a>]";
$aCells[sizeof($aCells)] = "[<a href=\"".BASE."admin/editVendor.php?".
"iVendorId=$this->iVendorId\">edit</a>]$sDelete";
$aCells[sizeof($aCells)] = "[<a href=\"".$oObject->makeUrl("edit",
$this->iVendorId, "Edit Vendor")."\">edit</a>]$sDelete";
}
echo html_tr($aCells, $sClass);
@@ -223,7 +226,8 @@ class Vendor {
echo 'Vendor Name: '.$this->sName,"\n";
if($this->canEdit())
{
echo "[<a href=\"".BASE."admin/editVendor.php?iVendorId=$this->iVendorId\">edit</a>]";
echo "[<a href=\"".$_SERVER['PHP_SELF']."?sClass=vendor&sAction=edit&".
"iId=$this->iVendorId&sTitle=Edit%20Vendor\">edit</a>]";
}
echo '<br />',"\n";

70
objectManager.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
/* code for displaying and processing objects that have object management
methods */
require('path.php');
require(BASE.'include/incl.php');
require_once(BASE.'include/objectManager.php');
/* require_once(BASE.'include/application_queue.php');
require_once(BASE.'include/version_queue.php'); */
/* if we have no valid class name we should abort */
if(!$aClean['sClass'])
{
echo "No class defined.\n";
exit;
}
/* make sure the class is defined */
if(!class_exists($aClean['sClass']))
{
echo "Class ".$aClean['sClass']." doesn't exist";
exit;
}
$oObject = new objectManager($aClean['sClass'], $aClean['sTitle'], $aClean['iId']);
if($aClean['bIsQueue'] == 'true') $oObject->bIsQueue = true;
else $oObject->bIsQueue = false;
$oOtherObject = new $oObject->sClass($oObject->iId);
/* Certain actions must be performed before the header is set */
$oObject->processForm($aClean);
if($oObject->iId && $aClean['sAction'] == "delete")
$oObject->delete_entry();
apidb_header($oObject->sTitle);
/* display a particular element */
if($oObject->iId)
{
switch($aClean['sAction'])
{
case "cancel":
$oObject->display_table(); /* go back to the queue */
break;
case "edit":
$oObject->display_entry_for_editing($REQUEST_URI);
break;
default:
$oObject->view($REQUEST_URI);
break;
}
} else if ($aClean['sAction'] == "add")
$oObject->add_entry($REQUEST_URI);
else
{
// if displaying a queue display the help for the given queue
if($oObject->bIsQueue)
$oObject->display_queue_processing_help();
$oObject->display_table();
}
?>

View File

@@ -24,4 +24,6 @@ echo "\n";
include_once("test_filter.php");
echo "\n";
include_once("test_url.php");
echo "\n";
include_once("test_om_objects.php");
?>

View File

@@ -0,0 +1,115 @@
<?php
/* unit tests to make sure objects we want to use with the object manager are valid */
require_once("path.php");
require_once("test_common.php");
//require_once(BASE."include/incl.php");
require_once(BASE.'include/objectManager.php');
require_once(BASE.'include/application.php');
//require_once(BASE.'include/application_queue.php');
require_once(BASE.'include/maintainer.php');
//require_once(BASE.'include/version_queue.php');
/* internal function */
function test_class($sClassName)
{
$oOMM = new objectManager($sClassName);
if(!$oOMM->hasValidMethods(true))
{
return false;
}
return true;
}
function test_object_methods()
{
test_start(__FUNCTION__);
/* $sClassName = 'application';
if(!test_class($sClassName))
{
echo $sClassName." class does not have valid methods for use with the object manager\n";
return false;
} else
{
echo "PASSED:\t\t".$sClassName."\n";
}
$sClassName = 'application_queue';
if(!test_class($sClassName))
{
echo $sClassName." class does not have valid methods for use with the object manager\n";
return false;
} else
{
echo "PASSED:\t\t".$sClassName."\n";
}
$sClassName = 'version';
if(!test_class($sClassName))
{
echo $sClassName." class does not have valid methods for use with the object manager\n";
return false;
} else
{
echo "PASSED:\t\t".$sClassName."\n";
}
$sClassName = 'version_queue';
if(!test_class($sClassName))
{
echo $sClassName." class does not have valid methods for use with the object manager\n";
return false;
} else
{
echo "PASSED:\t\t".$sClassName."\n";
}
$sClassName = 'maintainer';
if(!test_class($sClassName))
{
echo $sClassName." class does not have valid methods for use with the object manager\n";
return false;
} else
{
echo "PASSED:\t\t".$sClassName."\n";
} */
$aTestMethods = array("objectOutputHeader", "objectOutputTableRow",
"objectGetEntries", "display",
"objectGetInstanceFromRow", "outputEditor", "canEdit");
$oObject = new ObjectManager("");
$oObject->sClass = 'distribution';
if(!$oObject->checkMethods($aTestMethods, false))
{
echo $oObject->sClass." class does not have valid methods for use with".
" the object manager\n";
return false;
} else
{
echo "PASSED:\t\t".$oObject->sClass."\n";
}
$oObject->sClass = 'vendor';
if(!$oObject->checkMethods($aTestMethods, false))
{
echo $oObject->sClass." class does not have valid methods for use with".
" the object manager\n";
return false;
} else
{
echo "PASSED:\t\t".$oObject->sClass."\n";
}
return true;
}
if(!test_object_methods())
echo "test_object_methods() failed!\n";
else
echo "test_object_methods() passed\n";
?>