2016-07-23 16:37:21 +02:00
< ? php
2018-07-25 09:36:34 +02:00
/* Copyright ( C ) 2016 Marcos García < marcosgdf @ gmail . com >
* Copyright ( C ) 2018 Frédéric France < frederic . france @ netlogic . fr >
2022-03-18 15:38:44 +01:00
* Copyright ( C ) 2022 Open - Dsi < support @ open - dsi . fr >
2016-07-23 16:37:21 +02:00
*
* 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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2016-07-23 16:37:21 +02:00
*/
2022-03-18 15:38:44 +01:00
/**
* \file htdocs / variants / card . php
* \ingroup variants
* \brief Page to show product attribute
*/
2016-07-23 16:37:21 +02:00
require '../main.inc.php' ;
require 'class/ProductAttribute.class.php' ;
require 'class/ProductAttributeValue.class.php' ;
2022-03-18 15:38:44 +01:00
require 'lib/variants.lib.php' ;
// Load translation files required by the page
$langs -> loadLangs ( array ( 'products' ));
2016-07-23 16:37:21 +02:00
2019-01-27 11:55:16 +01:00
$id = GETPOST ( 'id' , 'int' );
$ref = GETPOST ( 'ref' , 'alpha' );
2022-03-18 15:38:44 +01:00
$action = GETPOST ( 'action' , 'aZ09' );
2019-01-27 11:55:16 +01:00
$confirm = GETPOST ( 'confirm' , 'alpha' );
$cancel = GETPOST ( 'cancel' , 'alpha' );
2022-03-18 15:38:44 +01:00
$contextpage = GETPOST ( 'contextpage' , 'aZ' ) ? GETPOST ( 'contextpage' , 'aZ' ) : 'productattribute' ; // To manage different context of search
$backtopage = GETPOST ( 'backtopage' , 'alpha' );
$backtopageforcancel = GETPOST ( 'backtopageforcancel' , 'alpha' );
$lineid = GETPOST ( 'lineid' , 'alpha' );
2021-03-20 18:58:34 +01:00
// Security check
if ( empty ( $conf -> variants -> enabled )) {
accessforbidden ( 'Module not enabled' );
}
if ( $user -> socid > 0 ) { // Protection if external user
accessforbidden ();
}
2022-03-18 15:38:44 +01:00
$result = restrictedArea ( $user , 'variants' );
$object = new ProductAttribute ( $db );
// Load object
include DOL_DOCUMENT_ROOT . '/core/actions_fetchobject.inc.php' ; // Must be include, not include_once
// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
$hookmanager -> initHooks ( array ( 'productattributecard' , 'globalcard' ));
2022-03-18 15:43:32 +01:00
$permissiontoread = $user -> rights -> variants -> read ;
$permissiontoadd = $user -> rights -> variants -> write ; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
$permissiontoedit = $user -> rights -> variants -> write ; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
$permissiontodelete = $user -> rights -> variants -> delete ;
2022-03-18 15:38:44 +01:00
$error = 0 ;
2021-03-20 18:58:34 +01:00
2017-02-08 14:07:54 +01:00
2017-02-08 12:37:38 +01:00
/*
* Actions
*/
2022-03-18 15:38:44 +01:00
$parameters = array ();
// Note that $action and $object may be modified by some hooks
$reshook = $hookmanager -> executeHooks ( 'doActions' , $parameters , $object , $action );
if ( $reshook < 0 ) {
setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
2021-02-26 13:10:10 +01:00
}
2017-02-08 12:37:38 +01:00
2022-03-18 15:38:44 +01:00
if ( empty ( $reshook )) {
$error = 0 ;
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
$backurlforlist = dol_buildpath ( '/variants/list.php' , 1 );
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
if ( empty ( $backtopage ) || ( $cancel && empty ( $id ))) {
if ( empty ( $backtopage ) || ( $cancel && strpos ( $backtopage , '__ID__' ))) {
if ( empty ( $id ) && (( $action != 'add' && $action != 'create' ) || $cancel )) {
$backtopage = $backurlforlist ;
} else {
$backtopage = dol_buildpath ( '/variants/card.php' , 1 ) . '?id=' . (( ! empty ( $id ) && $id > 0 ) ? $id : '__ID__' );
2016-07-23 16:37:21 +02:00
}
}
2022-03-18 15:38:44 +01:00
}
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
// Action to move up and down lines of object
include DOL_DOCUMENT_ROOT . '/core/actions_lineupdown.inc.php' ;
if ( $cancel ) {
if ( ! empty ( $backtopage )) {
header ( " Location: " . $backtopage );
exit ;
}
$action = '' ;
2016-07-23 16:37:21 +02:00
}
2022-03-18 15:38:44 +01:00
// Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
include DOL_DOCUMENT_ROOT . '/core/actions_addupdatedelete.inc.php' ;
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
// Action to move up and down lines of object
if ( $action == 'up' && $permissiontoedit ) {
$object -> line_up ( GETPOST ( 'rowid' ), false );
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
header ( 'Location: ' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '#' . GETPOST ( 'rowid' ));
exit ();
} elseif ( $action == 'down' && $permissiontoedit ) {
$object -> line_down ( GETPOST ( 'rowid' ), false );
header ( 'Location: ' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '#' . GETPOST ( 'rowid' ));
exit ();
}
if ( $action == 'addline' && $permissiontoedit ) {
$line_ref = GETPOST ( 'line_ref' , 'alpha' );
$line_value = GETPOST ( 'line_value' , 'alpha' );
$result = $object -> addLine ( $line_ref , $line_value );
if ( $result > 0 ) {
2018-07-27 14:49:33 +02:00
setEventMessages ( $langs -> trans ( 'RecordSaved' ), null , 'mesgs' );
2022-03-18 15:38:44 +01:00
header ( " Location: " . $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id );
exit ();
} else {
setEventMessages ( $object -> error , $object -> errors , 'errors' );
$action = '' ;
2016-07-23 16:37:21 +02:00
}
2022-03-18 15:38:44 +01:00
} elseif ( $action == 'updateline' && $permissiontoedit ) {
$line_ref = GETPOST ( 'line_ref' , 'alpha' );
$line_value = GETPOST ( 'line_value' , 'alpha' );
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
$result = $object -> updateLine ( $lineid , $line_ref , $line_value );
if ( $result > 0 ) {
setEventMessages ( $langs -> trans ( 'RecordSaved' ), null , 'mesgs' );
header ( " Location: " . $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id );
2017-02-08 12:52:31 +01:00
exit ();
2022-03-18 15:38:44 +01:00
} else {
setEventMessages ( $object -> error , $object -> errors , 'errors' );
$action = 'editline' ;
2016-07-23 16:37:21 +02:00
}
}
}
2017-02-08 14:07:54 +01:00
/*
* View
*/
2022-03-18 15:38:44 +01:00
$title = $langs -> trans ( 'ProductAttributeName' , dol_htmlentities ( $object -> label ));
2021-03-18 20:52:24 +01:00
$help_url = 'EN:Module_Products#Variants' ;
2022-03-18 15:38:44 +01:00
llxHeader ( '' , $title , $help_url );
2021-03-18 20:52:24 +01:00
2022-03-18 15:38:44 +01:00
// Part to create
if ( $action == 'create' ) {
print load_fiche_titre ( $langs -> trans ( " NewObject " , $langs -> transnoentitiesnoconv ( " ProductAttribute " )), '' , 'object_' . $object -> picto );
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
print '<form method="POST" action="' . $_SERVER [ " PHP_SELF " ] . '">' ;
print '<input type="hidden" name="token" value="' . newToken () . '">' ;
print '<input type="hidden" name="action" value="add">' ;
if ( $backtopage ) {
print '<input type="hidden" name="backtopage" value="' . $backtopage . '">' ;
}
if ( $backtopageforcancel ) {
print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">' ;
}
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
print dol_get_fiche_head ( array (), '' );
2017-04-12 13:01:41 +02:00
2022-03-18 15:38:44 +01:00
print '<table class="border centpercent tableforfieldcreate">' . " \n " ;
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
// Common attributes
include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_add.tpl.php' ;
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
print '</table>' . " \n " ;
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
print dol_get_fiche_end ();
2017-04-12 13:01:41 +02:00
2022-03-18 15:38:44 +01:00
print '<div class="center">' ;
print '<input type="submit" class="button" name="add" value="' . dol_escape_htmltag ( $langs -> trans ( " Create " )) . '">' ;
print ' ' ;
print '<input type="' . ( $backtopage ? " submit " : " button " ) . '" class="button button-cancel" name="cancel" value="' . dol_escape_htmltag ( $langs -> trans ( " Cancel " )) . '"' . ( $backtopage ? '' : ' onclick="javascript:history.go(-1)"' ) . '>' ; // Cancel for create does not post form if we don't know the backtopage
print '</div>' ;
print '</form>' ;
dol_set_focus ( 'input[name="label"]' );
2022-03-24 14:09:46 +01:00
} elseif (( $id || $ref ) && $action == 'edit' ) {
// Part to edit record
2022-03-18 15:38:44 +01:00
print load_fiche_titre ( $langs -> trans ( " ProductAttribute " ), '' , 'object_' . $object -> picto );
print '<form method="POST" action="' . $_SERVER [ " PHP_SELF " ] . '">' ;
print '<input type="hidden" name="token" value="' . newToken () . '">' ;
print '<input type="hidden" name="action" value="update">' ;
print '<input type="hidden" name="id" value="' . $object -> id . '">' ;
if ( $backtopage ) {
print '<input type="hidden" name="backtopage" value="' . $backtopage . '">' ;
}
if ( $backtopageforcancel ) {
print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">' ;
}
print dol_get_fiche_head ();
print '<table class="border centpercent tableforfieldedit">' . " \n " ;
// Common attributes
include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_edit.tpl.php' ;
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
print '</table>' ;
print dol_get_fiche_end ();
2017-04-12 13:01:41 +02:00
2022-03-18 15:38:44 +01:00
print '<div class="center"><input type="submit" class="button button-save" name="save" value="' . $langs -> trans ( " Save " ) . '">' ;
print ' <input type="submit" class="button button-cancel" name="cancel" value="' . $langs -> trans ( " Cancel " ) . '">' ;
2020-10-31 14:32:18 +01:00
print '</div>' ;
2022-03-18 15:38:44 +01:00
print '</form>' ;
2022-03-24 14:09:46 +01:00
} elseif ( $object -> id > 0 && ( empty ( $action ) || ( $action != 'edit' && $action != 'create' ))) {
// Part to show record
2022-03-18 15:38:44 +01:00
$res = $object -> fetch_optionals ();
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
$head = productAttributePrepareHead ( $object );
print dol_get_fiche_head ( $head , 'card' , $langs -> trans ( " ProductAttribute " ), - 1 , $object -> picto );
$formconfirm = '' ;
// Confirmation to delete
2016-07-23 16:37:21 +02:00
if ( $action == 'delete' ) {
2022-03-18 15:38:44 +01:00
$formconfirm = $form -> formconfirm ( $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id , $langs -> trans ( 'DeleteMyObject' ), $langs -> trans ( 'ProductAttributeDeleteDialog' ), 'confirm_delete' , '' , 0 , 1 );
} elseif ( $action == 'ask_deleteline' ) {
// Confirmation to delete line
$object_value = new ProductAttributeValue ( $db );
if ( $object_value -> fetch ( $lineid ) > 0 ) {
$formconfirm = $form -> formconfirm ( $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&lineid=' . $lineid , $langs -> trans ( 'DeleteLine' ), $langs -> trans ( 'ProductAttributeValueDeleteDialog' , dol_htmlentities ( $object_value -> value ), dol_htmlentities ( $object_value -> ref )), 'confirm_deleteline' , '' , 0 , 1 );
2016-07-23 16:37:21 +02:00
}
}
2022-03-18 15:38:44 +01:00
// Call Hook formConfirm
$parameters = array ( 'formConfirm' => $formconfirm , 'lineid' => $lineid );
$reshook = $hookmanager -> executeHooks ( 'formConfirm' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
if ( empty ( $reshook )) {
$formconfirm .= $hookmanager -> resPrint ;
} elseif ( $reshook > 0 ) {
$formconfirm = $hookmanager -> resPrint ;
}
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
// Print form confirm
print $formconfirm ;
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
// Object card
// ------------------------------------------------------------
$backtolist = ( GETPOST ( 'backtolist' ) ? GETPOST ( 'backtolist' ) : DOL_URL_ROOT . '/variants/list.php?leftmenu=?restore_lastsearch_values=1' );
$linkback = '<a href="' . dol_sanitizeUrl ( $backtolist ) . '">' . $langs -> trans ( " BackToList " ) . '</a>' ;
2017-11-26 10:13:32 +01:00
2022-03-18 15:38:44 +01:00
dol_banner_tab ( $object , 'id' , $linkback );
2017-11-26 10:13:32 +01:00
2022-03-18 15:38:44 +01:00
print '<div class="fichecenter">' ;
print '<div class="fichehalfleft">' ;
print '<div class="underbanner clearboth"></div>' ;
print '<table class="border centpercent tableforfield">' . " \n " ;
2017-11-26 10:13:32 +01:00
2022-03-18 15:38:44 +01:00
// Common attributes
include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_view.tpl.php' ;
2019-10-26 20:53:39 +02:00
print '</table>' ;
2022-03-18 15:38:44 +01:00
print '</div>' ;
print '</div>' ;
print '<div class="clearboth"></div>' ;
print dol_get_fiche_end ();
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
// Buttons for actions
if ( $action != 'editline' ) {
print '<div class="tabsAction">' . " \n " ;
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'addMoreActionsButtons' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
if ( $reshook < 0 ) setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
if ( empty ( $reshook )) {
// Modify
print dolGetButtonAction ( $langs -> trans ( 'Modify' ), '' , 'default' , $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . '&action=edit' , '' , $permissiontoedit );
// Delete (need delete permission, or if draft, just need create/modify permission)
print dolGetButtonAction ( $langs -> trans ( 'Delete' ), '' , 'delete' , $_SERVER [ 'PHP_SELF' ] . '?id=' . $object -> id . '&action=delete' , '' , $permissiontodelete );
}
print '</div>' . " \n " ;
2019-10-26 20:53:39 +02:00
}
2016-07-23 16:37:21 +02:00
2022-03-18 15:38:44 +01:00
/*
* Lines
*/
if ( ! empty ( $object -> table_element_line )) {
// Show object lines
$result = $object -> getLinesArray ();
print load_fiche_titre ( $langs -> trans ( " PossibleValues " ) . ( ! empty ( $object -> lines ) ? ' (' . count ( $object -> lines ) . ')' : '' ));
print ' <form name="addproduct" id="addproduct" action="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $object -> id . (( $action != 'editline' ) ? '' : '#line_' . GETPOST ( 'lineid' , 'int' )) . ' " method= " POST " >
< input type = " hidden " name = " token " value = " ' . newToken() . ' " >
< input type = " hidden " name = " action " value = " ' . (( $action != 'editline') ? 'addline' : 'updateline') . ' " >
< input type = " hidden " name = " mode " value = " " >
< input type = " hidden " name = " page_y " value = " " >
< input type = " hidden " name = " id " value = " ' . $object->id . ' " >
' ;
if ( $backtopage ) {
print '<input type="hidden" name="backtopage" value="' . $backtopage . '">' ;
}
if ( $backtopageforcancel ) {
print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">' ;
}
if ( ! empty ( $conf -> use_javascript_ajax )) {
include DOL_DOCUMENT_ROOT . '/core/tpl/ajaxrow.tpl.php' ;
}
print '<div class="div-table-responsive-no-min">' ;
if ( ! empty ( $object -> lines ) || ( $permissiontoedit && $action != 'selectlines' && $action != 'editline' )) {
print '<table id="tablelines" class="noborder noshadow" width="100%">' ;
}
// Form to add new line
if ( $permissiontoedit && $action != 'selectlines' ) {
if ( $action != 'editline' ) {
// Add products/services form
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'formAddObjectLine' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
if ( $reshook < 0 ) setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
if ( empty ( $reshook ))
$object -> formAddObjectLine ( 1 , $mysoc , $soc );
}
}
if ( ! empty ( $object -> lines )) {
$object -> printObjectLines ( $action , $mysoc , null , GETPOST ( 'lineid' , 'int' ), 1 );
}
if ( ! empty ( $object -> lines ) || ( $permissiontoedit && $action != 'selectlines' && $action != 'editline' )) {
print '</table>' ;
}
print '</div>' ;
print " </form> \n " ;
}
2016-07-23 16:37:21 +02:00
}
2018-08-04 15:58:05 +02:00
// End of page
2017-02-08 12:37:38 +01:00
llxFooter ();
2018-11-26 16:09:05 +01:00
$db -> close ();