Bonjour,
Je suis entrain de progresser dans la façon de concevoir un module Dolibarr et j’essaye d’attaquer la partie concernant l’affichage de la fiche d’un objet que je viens de créer au travers d’un formulaire.
Le principe me semble simple :
Remplissage du formulaire -> validation -> création de l’objet -> récupération de l’ID dans l’URL -> affichage des attributs de l’objets via l’ID récupéré.
Je bloque sur cette dernière partie, c’est-à-dire d’afficher la fiche d’un simple objet créé par mon formulaire.
Voici mon code (card.php) :
[code]
<?php /* Copyright (C) 2017 Laurent Destailleur * Copyright (C) ---Put here your own copyright and developer email--- * * 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 . */ /** * \file htdocs/modultest/card.php * \ingroup moduletest * \brief Page to create my object */ // Load Dolibarr environment $res=0; // Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined) if (! $res && ! empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) $res=@include($_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php"); // Try main.inc.php into web root detected using web root caluclated from SCRIPT_FILENAME $tmp=empty($_SERVER['SCRIPT_FILENAME'])?'':$_SERVER['SCRIPT_FILENAME'];$tmp2=realpath(__FILE__); $i=strlen($tmp)-1; $j=strlen($tmp2)-1; while($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i]==$tmp2[$j]) { $i--; $j--; } if (! $res && $i > 0 && file_exists(substr($tmp, 0, ($i+1))."/main.inc.php")) $res=@include(substr($tmp, 0, ($i+1))."/main.inc.php"); if (! $res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i+1)))."/main.inc.php")) $res=@include(dirname(substr($tmp, 0, ($i+1)))."/main.inc.php"); // Try main.inc.php using relative path if (! $res && file_exists("../main.inc.php")) $res=@include("../main.inc.php"); if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php"); if (! $res && file_exists("../../../main.inc.php")) $res=@include("../../../main.inc.php"); if (! $res) die("Include of main fails"); include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'); //include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'); dol_include_once('/moduletest/class/myobject.class.php'); dol_include_once('/moduletest/lib/myobject.lib.php'); // Load traductions files requiredby by page $langs->loadLangs(array("moduletest@moduletest","other")); // Get parameters $id = GETPOST('id', 'int'); $ref = GETPOST('ref', 'alpha'); $action = GETPOST('action', 'alpha'); $cancel = GETPOST('cancel', 'aZ09'); $backtopage = GETPOST('backtopage', 'alpha'); // Initialize technical objects $object=new MyObject($db); $extrafields = new ExtraFields($db); $diroutputmassaction=$conf->moduletest->dir_output . '/temp/massgeneration/'.$user->id; $hookmanager->initHooks(array('myobjectcard')); // Note that conf->hooks_modules contains array // Fetch optionals attributes and labels $extralabels = $extrafields->fetch_name_optionals_label('myobject'); $search_array_options=$extrafields->getOptionalsFromPost($extralabels,'','search_'); // Initialize array of search criterias $search_all=trim(GETPOST("search_all",'alpha')); $search=array(); foreach($object->fields as $key => $val) { if (GETPOST('search_'.$key,'alpha')) $search[$key]=GETPOST('search_'.$key,'alpha'); } if (empty($action) && empty($id) && empty($ref)) $action='view'; // fetch optionals attributes and labels $extralabels = $extrafields->fetch_name_optionals_label($object->table_element); // Load object include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once // Must be include, not include_once. Include fetch and fetch_thirdparty but not fetch_optionals /* * Actions * * Put here all code to do according to value of "action" parameter */ // If create a request if ($action == 'create') { $db->begin(); $nom = GETPOST('nom'); $prenom = GETPOST('prenom'); $datenaissance = GETPOST('datenaissance'); $adresse = GETPOST('adresse'); $country_id = GETPOST('country_id'); // We set country_id, country_code and country for the selected country $object->country_id=GETPOST('country_id')!=''?GETPOST('country_id'):$object->country_id; if ($object->country_id) { $tmparray=getCountry($object->country_id,'all'); $object->country_code=$tmparray['code']; $object->country=$tmparray['label']; } // If no name if (empty($nom)) { setEventMessages($langs->trans("Pas de nom"), null, 'errors'); $error++; $action='create'; } // If no firstname if (empty($prenom)) { setEventMessages($langs->trans("Pas de prénom"), null, 'errors'); $error++; $action='create'; } // If no datenaissance if (empty($datenaissance)) { setEventMessages($langs->trans("Pas de date de naissance"), null, 'errors'); $error++; $action='create'; } // If no adresse if (empty($adresse)) { setEventMessages($langs->trans("Pas d'adresse"), null, 'errors'); $error++; $action='create'; } // If no fk_country if (empty($country_id)) { setEventMessages($langs->trans("Pas de pays"), null, 'errors'); $error++; $action='create'; } $result = 0; if (! $error) { $object->nom = $nom; $object->prenom = $prenom; $datenaissance = dol_mktime(0, 0, 0, GETPOST('datenaissancemonth'), GETPOST('datenaissanceday'), GETPOST('datenaissanceyear')); $object->datenaissance = $datenaissance; $object->adresse = $adresse; $object->country_id = GETPOST('country_id', 'int'); $result = $object->create($user); //return int if ($result <= 0) { setEventMessages($object->errors, 'errors'); $error++; } } // If no SQL error we redirect to the request card if (! $error) { $db->commit(); $db->close(); header("Location: ".$_SERVER['PHP_SELF'].'?id='.$id); exit; } else { $db->rollback(); } } /* ************************ View *********************** */ llxHeader('', $langs->trans('Formulaire de test')); // Part to create if ($action == 'create' && ($user->rights->moduletest->creer )) { // Formulaire de test print load_fiche_titre($langs->trans('Formulaire de test'), '', 'title_hrm.png'); // Formulaire de test print ''."\n"; print ''."\n"; dol_fiche_head(); print ''.$langs->trans("Nom").' | '; print ''; print ''; print ' |
'.$langs->trans("Prenom").' | '; print ''; print ''; print ' |
'.$langs->trans("Date de Naissance").' | '; print ''; echo $form->select_date(GETPOST('datenaissance'),'datenaissance',0,0,1,'form'.'datenaissance',1,0,1); print ' |
'.$langs->trans("Adresse").' | '; print ''; print ''; print ' |
'.fieldLabel('Country','selectcounty_id').' | '; print $form->select_country((GETPOST('country_id')!=''?GETPOST('country_id'):$object->country_id),'country_id'); if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1); print ' |
';
print '
|