Qual: Try to uniformize code for product canvas to wokr like canvas for third parties. This also reduce seriously the "cyclomatic complexity" reported by quality tools on product/fiche.php file.

This commit is contained in:
Laurent Destailleur 2011-08-27 17:25:09 +00:00
parent 2419ec0118
commit f20fb6e6b4
2 changed files with 102 additions and 109 deletions

View File

@ -56,7 +56,7 @@ $object = new Product($db);
$extrafields = new ExtraFields($db); $extrafields = new ExtraFields($db);
// Get object canvas (By default, this is not defined, so standard usage of dolibarr) // Get object canvas (By default, this is not defined, so standard usage of dolibarr)
if ($id) $object->getCanvas($id); if ($id || $ref) $object->getCanvas($id,$ref);
$canvas = $object->canvas?$object->canvas:GETPOST("canvas"); $canvas = $object->canvas?$object->canvas:GETPOST("canvas");
if (! empty($canvas)) if (! empty($canvas))
{ {
@ -70,6 +70,10 @@ if (isset($id) || isset($ref)) $value = isset($id)?$id:(isset($ref)?$ref:'');
$type = isset($ref)?'ref':'rowid'; $type = isset($ref)?'ref':'rowid';
$result=restrictedArea($user,'produit|service',$value,'product','','',$type); $result=restrictedArea($user,'produit|service',$value,'product','','',$type);
// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
include_once(DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php');
$hookmanager=new HookManager($db);
$hookmanager->callHooks(array('product'));
@ -77,6 +81,10 @@ $result=restrictedArea($user,'produit|service',$value,'product','','',$type);
* Actions * Actions
*/ */
$parameters=array('socid'=>$socid);
$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
$error=$hookmanager->error; $errors=$hookmanager->errors;
if ($action == 'setproductaccountancycodebuy') if ($action == 'setproductaccountancycodebuy')
{ {
$product = new Product($db); $product = new Product($db);
@ -139,13 +147,10 @@ if ($action == 'add' && ($user->rights->produit->creer || $user->rights->service
$product=new Product($db); $product=new Product($db);
$usecanvas=$_POST["canvas"]; if (! empty($canvas)) // Overwrite product here
if (empty($conf->global->MAIN_USE_CANVAS)) $usecanvas=0;
if (! empty($usecanvas)) // Overwrite product here
{ {
$canvas = new Canvas($db,$user); $objcanvas = new Canvas($db,$user);
$product = $canvas->load_canvas('product',$_POST["canvas"]); $product = $objcanvas->load_canvas('product',$canvas);
} }
if (! $error) if (! $error)
@ -230,13 +235,10 @@ if ($action == 'update' && ($user->rights->produit->creer || $user->rights->serv
{ {
$product=new Product($db); $product=new Product($db);
$usecanvas=$_POST["canvas"]; if (! empty($canvas)) // Overwrite product here
if (empty($conf->global->MAIN_USE_CANVAS)) $usecanvas=0;
if (! empty($usecanvas)) // Overwrite product here
{ {
$canvas = new Canvas($db,$user); $objcanvas = new Canvas($db,$user);
$product = $canvas->load_canvas('product',$_POST["canvas"]); $product = $objcanvas->load_canvas('product',$_POST["canvas"]);
} }
if ($product->fetch($id)) if ($product->fetch($id))
@ -642,10 +644,49 @@ if ($_POST["cancel"] == $langs->trans("Cancel"))
$html = new Form($db); $html = new Form($db);
$formproduct = new FormProduct($db); $formproduct = new FormProduct($db);
// TODO Mutualize this part of code (same than societe/soc.php)
/* if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action))
* Fiche creation du produit {
*/ // -----------------------------------------
// When used with CANVAS
// -----------------------------------------
if ($action == 'create')
{
$objcanvas->assign_post(); // TODO: Put code of assign_post into assign_values to keep only assign_values
$objcanvas->assign_values($action); // Set value for templates
$objcanvas->display_canvas($action,0); // Show template
}
elseif ($action == 'edit')
{
$objcanvas->control->object=$objcanvas->getObject($socid); // TODO: Getting and storing object should be done into assign_values (for template with no code) or into tpl
if (empty($objcanvas->control->object))
{
$object = new Societe($db);
$object->fetch($socid);
$objcanvas->control->object=$object;
}
$objcanvas->assign_post(); // TODO: Put code of assign_post into assign_values to keep only assign_values
$objcanvas->assign_values($action); // Set value for templates
$objcanvas->display_canvas($action); // Show template
}
else
{
$objcanvas->control->object=$objcanvas->getObject($socid); // TODO: Getting and storing object should be done into assign_values (for template with no code) or into tpl
if (empty($objcanvas->control->object))
{
$object = new Societe($db);
$object->fetch($socid);
$objcanvas->control->object=$object;
}
$objcanvas->assign_values('view');
$objcanvas->display_canvas('view'); // Show template
}
}
else
{
// -----------------------------------------
// When used in standard mode
// -----------------------------------------
if ($action == 'create' && ($user->rights->produit->creer || $user->rights->service->creer)) if ($action == 'create' && ($user->rights->produit->creer || $user->rights->service->creer))
{ {
$helpurl=''; $helpurl='';
@ -654,11 +695,6 @@ if ($action == 'create' && ($user->rights->produit->creer || $user->rights->serv
llxHeader('',$langs->trans("CardProduct".$_GET["type"]),$helpurl); llxHeader('',$langs->trans("CardProduct".$_GET["type"]),$helpurl);
$usecanvas=$_GET["canvas"];
if (empty($conf->global->MAIN_USE_CANVAS)) $usecanvas=0;
if (empty($usecanvas))
{
print '<form action="fiche.php" method="post">'; print '<form action="fiche.php" method="post">';
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">'; print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<input type="hidden" name="action" value="add">'; print '<input type="hidden" name="action" value="add">';
@ -814,15 +850,6 @@ if ($action == 'create' && ($user->rights->produit->creer || $user->rights->serv
print '</form>'; print '</form>';
} }
else
{
$canvas = new Canvas($db,$user);
$product = $canvas->load_canvas('product',$_GET["canvas"]);
$canvas->assign_values('create');
$canvas->display_canvas();
}
}
/** /**
* Product card * Product card
@ -831,26 +858,7 @@ if ($action == 'create' && ($user->rights->produit->creer || $user->rights->serv
if ($id || $ref) if ($id || $ref)
{ {
$product=new Product($db); $product=new Product($db);
// TODO en attendant d'inclure le nom du canvas dans les liens
$productstatic = new Product($db);
$result = $productstatic->getCanvas($id,$ref);
$usecanvas=$productstatic->canvas;
if (empty($conf->global->MAIN_USE_CANVAS)) $usecanvas=0;
if (empty($usecanvas))
{
$product->fetch($id,$ref); $product->fetch($id,$ref);
}
else // Gestion des produits specifiques
{
$canvas = new Canvas($db,$user);
$product = $canvas->load_canvas('product',$productstatic->canvas);
if (! $product) dol_print_error('','Faled to load canvas product-'.$productstatic->canvas);
$canvas->fetch($productstatic->id,'',$action);
}
llxHeader('',$langs->trans("CardProduct".$product->type)); llxHeader('',$langs->trans("CardProduct".$product->type));
@ -858,8 +866,6 @@ if ($id || $ref)
* Fiche en mode edition * Fiche en mode edition
*/ */
if ($action == 'edit' && ($user->rights->produit->creer || $user->rights->service->creer)) if ($action == 'edit' && ($user->rights->produit->creer || $user->rights->service->creer))
{
if (empty($usecanvas))
{ {
$type = $langs->trans('Product'); $type = $langs->trans('Product');
if ($product->isservice()) $type = $langs->trans('Service'); if ($product->isservice()) $type = $langs->trans('Service');
@ -1006,12 +1012,6 @@ if ($id || $ref)
print '</form>'; print '</form>';
} }
else
{
$canvas->assign_values('edit');
$canvas->display_canvas();
}
}
/* /*
* Fiche en mode visu * Fiche en mode visu
*/ */
@ -1029,8 +1029,6 @@ if ($id || $ref)
if ($ret == 'html') print '<br>'; if ($ret == 'html') print '<br>';
} }
if (empty($usecanvas))
{
$isphoto=$product->is_photo_available($conf->product->dir_output); $isphoto=$product->is_photo_available($conf->product->dir_output);
// En mode visu // En mode visu
@ -1167,12 +1165,6 @@ if ($id || $ref)
print '<tr><td valign="top">'.$langs->trans("Note").'</td><td colspan="2">'.(dol_textishtml($product->note)?$product->note:dol_nl2br($product->note,1,true)).'</td></tr>'; print '<tr><td valign="top">'.$langs->trans("Note").'</td><td colspan="2">'.(dol_textishtml($product->note)?$product->note:dol_nl2br($product->note,1,true)).'</td></tr>';
print "</table>\n"; print "</table>\n";
}
else
{
$canvas->assign_values('view');
$canvas->display_canvas();
}
dol_fiche_end(); dol_fiche_end();
} }
@ -1183,7 +1175,7 @@ else if ($action != 'create')
Header("Location: index.php"); Header("Location: index.php");
exit; exit;
} }
}
// Clone confirmation // Clone confirmation

View File

@ -498,6 +498,7 @@ $formcompany = new FormCompany($db);
$countrynotdefined=$langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')'; $countrynotdefined=$langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
// TODO Mutualize this part of code (same than product/fiche.php)
if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action)) if (is_object($objcanvas) && $objcanvas->displayCanvasExists($action))
{ {
// ----------------------------------------- // -----------------------------------------