Merge pull request #7849 from IonAgorria/resources-card

NEW: Merge resource/add.php to resource/card.php
This commit is contained in:
Laurent Destailleur
2017-11-22 11:33:56 +01:00
committed by GitHub
3 changed files with 86 additions and 246 deletions

View File

@@ -227,7 +227,7 @@ class modResource extends DolibarrModules
'titre'=> 'MenuResourceAdd',
'mainmenu'=> 'tools',
'leftmenu'=> 'resource_add',
'url'=> '/resource/add.php',
'url'=> '/resource/card.php?action=create',
'langs'=> 'resource',
'position'=> 101,
'enabled'=> '1',

View File

@@ -1,211 +0,0 @@
<?php
/* Copyright (C) 2013 Jean-François Ferry <jfefe@aternatik.fr>
* Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
* Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file resource/add.php
* \ingroup resource
* \brief Page to manage resource object
*/
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/resource/class/dolresource.class.php';
require_once DOL_DOCUMENT_ROOT.'/resource/class/html.formresource.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
// Load traductions files required by page
$langs->load("resource");
$langs->load("companies");
$langs->load("other");
$langs->load("resource");
// Get parameters
$id = GETPOST('id','int');
$action = GETPOST('action','alpha');
$cancel = GETPOST('cancel','alpha');
if (empty($sortorder)) $sortorder="DESC";
if (empty($sortfield)) $sortfield="t.rowid";
if (empty($arch)) $arch = 0;
if ($page == -1) {
$page = 0 ;
}
$limit = $conf->global->limit;
$offset = $limit * $page ;
$pageprev = $page - 1;
$pagenext = $page + 1;
// Protection if external user
if ($user->societe_id > 0)
{
accessforbidden();
}
$object = new DolResource($db);
$extrafields = new ExtraFields($db);
// fetch optionals attributes and labels
$extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
$hookmanager->initHooks(array('resource_card_add','globalcard'));
$parameters=array();
$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
if (empty($reshook))
{
if ($action == 'confirm_add_resource')
{
if (! $cancel)
{
$error='';
$ref=GETPOST('ref','alpha');
$description=GETPOST('description','alpha');
$fk_code_type_resource=GETPOST('fk_code_type_resource','alpha');
if (empty($ref))
{
$mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Ref"));
setEventMessages($mesg, null, 'errors');
$error++;
}
if (! $error)
{
$object=new Dolresource($db);
$object->ref=$ref;
$object->description=$description;
$object->fk_code_type_resource=$fk_code_type_resource;
// Fill array 'array_options' with data from add form
$ret = $extrafields->setOptionalsFromPost($extralabels, $object);
if ($ret < 0) {
$error ++;
}
$result=$object->create($user);
if ($result > 0)
{
// Creation OK
$db->commit();
setEventMessages($langs->trans('ResourceCreatedWithSuccess'), null, 'mesgs');
Header("Location: card.php?id=" . $object->id);
return;
}
else
{
// Creation KO
setEventMessages($object->error, $object->errors, 'errors');
$action = '';
}
}
else
{
$action = '';
}
}
else
{
Header("Location: list.php");
}
}
}
/*
* View
*/
$form=new Form($db);
$formresource = new FormResource($db);
if (! $action)
{
$pagetitle=$langs->trans('AddResource');
llxHeader('',$pagetitle,'');
print load_fiche_titre($pagetitle,'','title_generic');
print '<form method="post" action="'.$_SERVER['PHP_SELF'].'" name="add_resource">';
print '<input type="hidden" name="action" value="confirm_add_resource" />';
dol_fiche_head('');
print '<table class="border" width="100%">';
// Ref / label
$field = 'ref';
print '<tr>';
print '<td class="titlefieldcreate fieldrequired">';
print $langs->trans('ResourceFormLabel_'.$field);
print '</td>';
print '<td>';
print '<input class="flat maxwidthonsmartphone" type="text" name="'.$field.'" value="'.$$field.'" />';
print '</td>';
print '</tr>';
// Type
print '<tr><td>'.$langs->trans("ResourceType").'</td>';
print '<td>';
$ret = $formresource->select_types_resource($object->fk_code_type_resource, 'fk_code_type_resource', '', 2, 1);
print '</td></tr>';
// Description
$field = 'description';
print '<tr>';
print '<td class="tdtop">';
print $langs->trans('ResourceFormLabel_'.$field);
print '</td>';
print '<td>';
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
$doleditor = new DolEditor('description', $description, '', '200', 'dolibarr_notes', false);
$doleditor->Create();
print '</td>';
print '</tr>';
// Other attributes
$parameters=array('objectsrc' => $objectsrc);
$reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
print $hookmanager->resPrint;
if (empty($reshook) && ! empty($extrafields->attribute_label))
{
print $object->showOptionals($extrafields,'edit');
}
print '</table>';
dol_fiche_end('');
print '<div class="center">';
print '<input type="submit" class="button" value="' . $langs->trans("Save") . '">';
print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
print '<input type="button" class="button" value="' . $langs->trans("Cancel") . '">';
print '</div>';
print '</form>';
}
// End of page
llxFooter();
$db->close();

View File

@@ -43,32 +43,32 @@ $langs->load("main");
// Get parameters
$id = GETPOST('id','int');
$action = GETPOST('action','alpha');
$cancel = GETPOST('cancel','alpha');
$ref = GETPOST('ref','alpha');
$description = GETPOST('description');
$confirm = GETPOST('confirm');
$fk_code_type_resource = GETPOST('fk_code_type_resource','alpha');
// Protection if external user
if ($user->societe_id > 0)
if ($user->socid > 0)
{
accessforbidden();
}
if (! $user->rights->resource->read)
if( ! $user->rights->resource->read)
{
accessforbidden();
}
$object = new Dolresource($db);
$result = $object->fetch($id, $ref);
if (! ($result > 0)) dol_print_error($db, $object->error);
$extrafields = new ExtraFields($db);
// fetch optionals attributes and labels
$extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
$hookmanager->initHooks(array('resource_card','globalcard'));
$parameters=array('resource_id'=>$object->id);
$hookmanager->initHooks(array('resource', 'resource_card','globalcard'));
$parameters=array('resource_id'=>$id);
$reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
@@ -77,15 +77,58 @@ if (empty($reshook))
/*******************************************************************
* ACTIONS
********************************************************************/
if ($action == 'add' && $user->rights->resource->write)
{
if (! $cancel)
{
$error='';
if ($action == 'update' && ! $_POST["cancel"] && $user->rights->resource->write)
if (empty($ref))
{
setEventMessages($langs->trans("ErrorFieldRequired",$langs->transnoentities("Ref")), null, 'errors');
$action = 'create';
}
else
{
$object->ref = $ref;
$object->description = $description;
$object->fk_code_type_resource = $fk_code_type_resource;
// Fill array 'array_options' with data from add form
$ret = $extrafields->setOptionalsFromPost($extralabels,$object);
if ($ret < 0) $error++;
$result=$object->create($user);
if ($result > 0)
{
// Creation OK
setEventMessages($langs->trans('ResourceCreatedWithSuccess'), null, 'mesgs');
Header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
exit;
}
else
{
// Creation KO
setEventMessages($object->error, $object->errors, 'errors');
$action = 'create';
}
}
}
else
{
Header("Location: list.php");
exit;
}
}
if ($action == 'update' && ! $cancel && $user->rights->resource->write)
{
$error=0;
if (empty($ref))
{
$error++;
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
$error++;
}
if (! $error)
@@ -112,17 +155,18 @@ if (empty($reshook))
else
{
setEventMessages($object->error, $object->errors, 'errors');
$action='edit';
$error++;
}
}
else
{
setEventMessages($object->error, $object->errors, 'errors');
$action='edit';
$error++;
}
}
else
if ($error)
{
$action='edit';
}
@@ -159,37 +203,42 @@ if (empty($reshook))
*
* Put here all code to build page
****************************************************/
$pagetitle = $langs->trans('ResourceCard');
llxHeader('',$pagetitle,'');
$title = $langs->trans($action == 'create' ? 'AddResource' : 'ResourceCard');
llxHeader('',$title,'');
$form = new Form($db);
$formresource = new FormResource($db);
if ($object->id > 0)
if ($action == 'create' || $object->fetch($id) > 0)
{
$head=resource_prepare_head($object);
if ($action == 'create')
{
print load_fiche_titre($title,'','title_generic');
dol_fiche_head('');
}
else
{
$head = resource_prepare_head($object);
dol_fiche_head($head, 'resource', $title, -1, 'resource');
}
if ($action == 'edit' )
if ($action == 'create' || $action == 'edit')
{
if ( ! $user->rights->resource->write )
accessforbidden('',0);
/*---------------------------------------
* Edit object
* Create/Edit object
*/
print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">';
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
print '<input type="hidden" name="action" value="update">';
print '<input type="hidden" name="id" value="'.$object->id.'">';
dol_fiche_head($head, 'resource', $langs->trans("ResourceSingular"),0,'resource');
print '<input type="hidden" name="action" value="'.($action == "create"?"add":"update").'">';
print '<table class="border" width="100%">';
// Ref
print '<tr><td class="titlefieldcreate fieldrequired">'.$langs->trans("ResourceFormLabel_ref").'</td>';
print '<td><input class="minwidth200" name="ref" value="'.(GETPOST('ref') ? GETPOST('ref') : $object->ref).'"></td></tr>';
print '<td><input class="minwidth200" name="ref" value="'.($ref ? $ref : $object->ref).'"></td></tr>';
// Type
print '<tr><td>'.$langs->trans("ResourceType").'</td>';
@@ -200,7 +249,9 @@ if ($object->id > 0)
// Description
print '<tr><td class="tdtop">'.$langs->trans("Description").'</td>';
print '<td>';
print '<textarea name="description" class="quatrevingtpercent" rows="'.ROWS_3.'">'.($_POST['description'] ? GETPOST('description','alpha') : $object->description).'</textarea>';
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
$doleditor = new DolEditor('description', ($description ? $description : $object->description), '', '200', 'dolibarr_notes', false);
$doleditor->Create();
print '</td></tr>';
// Other attributes
@@ -217,16 +268,16 @@ if ($object->id > 0)
dol_fiche_end();
print '<div class="center">';
print '<input name="update" class="button" type="submit" value="'.$langs->trans("Modify").'"> &nbsp; ';
print '<input type="submit" class="button" name="cancel" Value="'.$langs->trans("Cancel").'"></td></tr>';
print '<input type="submit" class="button" value="' . $langs->trans($action == "create"?"Create":"Modify") . '">';
print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
print '<input type="submit" class="button" value="' . $langs->trans("Cancel") . '">';
print '</div>';
print '</div>';
print '</form>';
}
else
{
dol_fiche_head($head, 'resource', $langs->trans("ResourceSingular"), -1, 'resource');
$formconfirm = '';
// Confirm deleting resource line
@@ -296,23 +347,23 @@ if ($object->id > 0)
// modified by hook
if (empty($reshook))
{
if ($action != "edit" )
if ($action != "create" && $action != "edit" )
{
// Edit resource
if($user->rights->resource->write)
{
print '<div class="inline-block divButAction">';
print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=edit" class="butAction">'.$langs->trans('Modify').'</a>';
print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;action=edit" class="butAction">'.$langs->trans('Modify').'</a>';
print '</div>';
}
}
if ($action != "delete" && $action != "edit")
if ($action != "delete" && $action != "create" && $action != "edit")
{
// Delete resource
if($user->rights->resource->delete)
{
print '<div class="inline-block divButAction">';
print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;action=delete" class="butActionDelete">'.$langs->trans('Delete').'</a>';
print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;action=delete" class="butActionDelete">'.$langs->trans('Delete').'</a>';
print '</div>';
}
}