dolibarr  20.0.0-beta
card.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
25 // Load Dolibarr environment
26 require '../../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT . '/core/lib/asset.lib.php';
28 require_once DOL_DOCUMENT_ROOT . '/asset/class/assetmodel.class.php';
29 require_once DOL_DOCUMENT_ROOT . '/core/class/html.formcompany.class.php';
30 require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php';
31 require_once DOL_DOCUMENT_ROOT . '/asset/class/assetdepreciationoptions.class.php';
32 require_once DOL_DOCUMENT_ROOT . '/asset/class/assetaccountancycodes.class.php';
33 
34 // Load translation files required by the page
35 $langs->loadLangs(array("assets", "other"));
36 
37 // Get parameters
38 $id = GETPOSTINT('id');
39 $ref = GETPOST('ref', 'alpha');
40 $action = GETPOST('action', 'aZ09');
41 $confirm = GETPOST('confirm', 'alpha');
42 $cancel = GETPOST('cancel', 'aZ09');
43 $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'assetmodelcard'; // To manage different context of search
44 $backtopage = GETPOST('backtopage', 'alpha');
45 $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');
46 
47 // Initialize technical objects
48 $object = new AssetModel($db);
49 $assetdepreciationoptions = new AssetDepreciationOptions($db);
50 $assetaccountancycodes = new AssetAccountancyCodes($db);
51 $extrafields = new ExtraFields($db);
52 $diroutputmassaction = $conf->asset->dir_output . '/temp/massgeneration/' . $user->id;
53 $hookmanager->initHooks(array('assetmodelcard', 'globalcard')); // Note that conf->hooks_modules contains array
54 
55 // Fetch optionals attributes and labels
56 $extrafields->fetch_name_optionals_label($object->table_element);
57 
58 $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
59 
60 // Initialize array of search criteria
61 $search_all = GETPOST("search_all", 'alpha');
62 $search = array();
63 foreach ($object->fields as $key => $val) {
64  if (GETPOST('search_' . $key, 'alpha')) {
65  $search[$key] = GETPOST('search_' . $key, 'alpha');
66  }
67 }
68 
69 if (empty($action) && empty($id) && empty($ref)) {
70  $action = 'view';
71 }
72 
73 // Load object
74 include DOL_DOCUMENT_ROOT . '/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
75 
76 $permissiontoread = ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'read')) || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'model_advance', 'read')));
77 $permissiontoadd = ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'write')) || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'model_advance', 'write'))); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
78 $permissiontodelete = ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'delete')) || (getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && $user->hasRight('asset', 'model_advance', 'delete'))) || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
79 $permissionnote = $permissiontoadd; // Used by the include of actions_setnotes.inc.php
80 $permissiondellink = $permissiontoadd; // Used by the include of actions_dellink.inc.php
81 $upload_dir = $conf->asset->multidir_output[isset($object->entity) ? $object->entity : 1];
82 
83 // Security check (enable the most restrictive one)
84 if ($user->socid > 0) {
86 }
87 if ($user->socid > 0) {
88  $socid = $user->socid;
89 }
90 $isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
91 restrictedArea($user, 'asset', $object->id, $object->table_element, '', 'fk_soc', 'rowid', $isdraft);
92 if (!isModEnabled('asset')) {
94 }
95 if (!$permissiontoread) {
97 }
98 
99 // Model depreciation options and accountancy codes
100 $object->asset_depreciation_options = &$assetdepreciationoptions;
101 $object->asset_accountancy_codes = &$assetaccountancycodes;
102 if (!empty($id)) {
103  $depreciationoptionserrors = $assetdepreciationoptions->fetchDeprecationOptions(0, $object->id);
104  $accountancycodeserrors = $assetaccountancycodes->fetchAccountancyCodes(0, $object->id);
105 
106  if ($depreciationoptionserrors < 0) {
107  setEventMessages($assetdepreciationoptions->error, $assetdepreciationoptions->errors, 'errors');
108  }
109  if ($accountancycodeserrors < 0) {
110  setEventMessages($assetaccountancycodes->error, $assetaccountancycodes->errors, 'errors');
111  }
112 }
113 
114 /*
115  * Actions
116  */
117 
118 $parameters = array();
119 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
120 if ($reshook < 0) {
121  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
122 }
123 
124 if (empty($reshook)) {
125  $error = 0;
126 
127  $backurlforlist = DOL_URL_ROOT . '/asset/model/list.php';
128 
129  if (empty($backtopage) || ($cancel && empty($id))) {
130  if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
131  if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
132  $backtopage = $backurlforlist;
133  } else {
134  $backtopage = DOL_URL_ROOT . '/asset/model/card.php?id=' . ((!empty($id) && $id > 0) ? $id : '__ID__');
135  }
136  }
137  }
138 
139  $triggermodname = 'ASSETMODEL_MODIFY'; // Name of trigger action code to execute when we modify record
140 
141  if (($action == 'edit' && !($permissiontoadd && $object->status == $object::STATUS_DRAFT)) ||
142  ($action == 'confirm_setdraft' && !($permissiontoadd && $object->status != $object::STATUS_DRAFT)) ||
143  ($action == 'confirm_validate' && !($permissiontoadd && $object->status != $object::STATUS_VALIDATED)) ||
144  ($action == 'confirm_close' && !($permissiontoadd && $object->status != $object::STATUS_CANCELED))
145  ) {
146  $action = "";
147  }
148 
149  // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
150  include DOL_DOCUMENT_ROOT . '/core/actions_addupdatedelete.inc.php';
151 }
152 
153 
154 /*
155  * View
156  *
157  * Put here all code to build page
158  */
159 
160 $form = new Form($db);
161 $formfile = new FormFile($db);
162 
163 $title = $langs->trans("AssetModel") . ' - ' . $langs->trans("Card");
164 $help_url = '';
165 llxHeader('', $title, $help_url, '', 0, 0, '', '', '', 'mod-asset page-model-card');
166 
167 // Part to create
168 if ($action == 'create') {
169  print load_fiche_titre($langs->trans("NewObject", $langs->transnoentitiesnoconv("AssetModel")), '', 'object_' . $object->picto);
170 
171  print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
172  print '<input type="hidden" name="token" value="' . newToken() . '">';
173  print '<input type="hidden" name="action" value="add">';
174  if ($backtopage) {
175  print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
176  }
177  if ($backtopageforcancel) {
178  print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
179  }
180 
181  print dol_get_fiche_head(array(), '');
182 
183 
184  print '<table class="border centpercent tableforfieldcreate">' . "\n";
185 
186  // Common attributes
187  include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_add.tpl.php';
188 
189  // Other attributes
190  include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_add.tpl.php';
191 
192  // Depreciation options
193  include DOL_DOCUMENT_ROOT . '/asset/tpl/depreciation_options_edit.tpl.php';
194 
195  // Accountancy codes
196  print '<div class="clearboth"></div>';
197  print '<hr>';
198  include DOL_DOCUMENT_ROOT . '/asset/tpl/accountancy_codes_edit.tpl.php';
199 
200  print '</table>' . "\n";
201 
202  print dol_get_fiche_end();
203 
204  print $form->buttonsSaveCancel("Create");
205 
206  print '</form>';
207 
208  //dol_set_focus('input[name="ref"]');
209 }
210 
211 // Part to edit record
212 if (($id || $ref) && $action == 'edit') {
213  print load_fiche_titre($langs->trans("AssetModel"), '', 'object_' . $object->picto);
214 
215  print '<form method="POST" action="' . $_SERVER["PHP_SELF"] . '">';
216  print '<input type="hidden" name="token" value="' . newToken() . '">';
217  print '<input type="hidden" name="action" value="update">';
218  print '<input type="hidden" name="id" value="' . $object->id . '">';
219  if ($backtopage) {
220  print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
221  }
222  if ($backtopageforcancel) {
223  print '<input type="hidden" name="backtopageforcancel" value="' . $backtopageforcancel . '">';
224  }
225 
226  print dol_get_fiche_head();
227 
228  print '<table class="border centpercent tableforfieldedit">' . "\n";
229 
230  // Common attributes
231  include DOL_DOCUMENT_ROOT . '/core/tpl/commonfields_edit.tpl.php';
232 
233  // Other attributes
234  include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_edit.tpl.php';
235 
236  // Depreciation options
237  include DOL_DOCUMENT_ROOT . '/asset/tpl/depreciation_options_edit.tpl.php';
238 
239  // Accountancy codes
240  print '<div class="clearboth"></div>';
241  print '<hr>';
242  include DOL_DOCUMENT_ROOT . '/asset/tpl/accountancy_codes_edit.tpl.php';
243 
244  print '</table>';
245 
246  print dol_get_fiche_end();
247 
248  print $form->buttonsSaveCancel();
249 
250  print '</form>';
251 }
252 
253 // Part to show record
254 if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
255  $res = $object->fetch_optionals();
256 
258  print dol_get_fiche_head($head, 'card', $langs->trans("AssetModel"), -1, $object->picto);
259 
260  $formconfirm = '';
261 
262  // Confirmation to delete
263  if ($action == 'delete') {
264  $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('DeleteAssetModel'), $langs->trans('ConfirmDeleteObject'), 'confirm_delete', '', 0, 1);
265  } elseif ($action == 'clone') {
266  // Clone confirmation
267  // Create an array for form
268  $formquestion = array();
269  $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneAsk', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
270  }
271 
272  // Call Hook formConfirm
273  $parameters = array('formConfirm' => $formconfirm);
274  $reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
275  if (empty($reshook)) {
276  $formconfirm .= $hookmanager->resPrint;
277  } elseif ($reshook > 0) {
278  $formconfirm = $hookmanager->resPrint;
279  }
280 
281  // Print form confirm
282  print $formconfirm;
283 
284 
285  // Object card
286  // ------------------------------------------------------------
287  $linkback = '<a href="' . DOL_URL_ROOT . '/asset/model/list.php?restore_lastsearch_values=1' . (!empty($socid) ? '&socid=' . $socid : '') . '">' . $langs->trans("BackToList") . '</a>';
288 
289  $morehtmlref = '<div class="refidno">';
290  $morehtmlref .= '</div>';
291 
292 
293  dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
294 
295 
296  print '<div class="fichecenter">';
297  print '<div class="underbanner clearboth"></div>';
298  print '<table class="border centpercent tableforfield">' . "\n";
299 
300  // Common attributes
301  $object->fields = dol_sort_array($object->fields, 'position');
302 
303  foreach ($object->fields as $key => $val) {
304  if (!empty($keyforbreak) && $key == $keyforbreak) {
305  break; // key used for break on second column
306  }
307 
308  // Discard if extrafield is a hidden field on form
309  if (abs($val['visible']) != 1 && abs($val['visible']) != 3 && abs($val['visible']) != 4 && abs($val['visible']) != 5) {
310  continue;
311  }
312 
313  if (array_key_exists('enabled', $val) && isset($val['enabled']) && !verifCond($val['enabled'])) {
314  continue; // We don't want this field
315  }
316  if (in_array($key, array('ref', 'status'))) {
317  continue; // Ref and status are already in dol_banner
318  }
319 
320  $value = $object->$key;
321 
322  print '<tr class="field_'.$key.'"><td';
323  print ' class="'.(empty($val['tdcss']) ? 'titlefield' : $val['tdcss']).' fieldname_'.$key;
324  //if ($val['notnull'] > 0) print ' fieldrequired'; // No fieldrequired on the view output
325  if ($val['type'] == 'text' || $val['type'] == 'html') {
326  print ' tdtop';
327  }
328  print '">';
329 
330  $labeltoshow = '';
331  if (!empty($val['help'])) {
332  $labeltoshow .= $form->textwithpicto($langs->trans($val['label']), $langs->trans($val['help']));
333  } else {
334  if (isset($val['copytoclipboard']) && $val['copytoclipboard'] == 1) {
335  $labeltoshow .= showValueWithClipboardCPButton($value, 0, $langs->transnoentitiesnoconv($val['label']));
336  } else {
337  $labeltoshow .= $langs->trans($val['label']);
338  }
339  }
340  if (empty($val['alwayseditable'])) {
341  print $labeltoshow;
342  } else {
343  print $form->editfieldkey($labeltoshow, $key, $value, $object, 1, $val['type']);
344  }
345 
346  print '</td>';
347  print '<td class="valuefield fieldname_'.$key;
348  if ($val['type'] == 'text') {
349  print ' wordbreak';
350  }
351  if (!empty($val['cssview'])) {
352  print ' '.$val['cssview'];
353  }
354  print '">';
355  if (empty($val['alwayseditable'])) {
356  if (preg_match('/^(text|html)/', $val['type'])) {
357  print '<div class="longmessagecut">';
358  }
359  if ($key == 'lang') {
360  $langs->load("languages");
361  $labellang = ($value ? $langs->trans('Language_'.$value) : '');
362  print picto_from_langcode($value, 'class="paddingrightonly saturatemedium opacitylow"');
363  print $labellang;
364  } else {
365  if (isset($val['copytoclipboard']) && $val['copytoclipboard'] == 2) {
366  $out = $object->showOutputField($val, $key, $value, '', '', '', 0);
367  print showValueWithClipboardCPButton($out, 0, $out);
368  } else {
369  print $object->showOutputField($val, $key, $value, '', '', '', 0);
370  }
371  }
372  //print dol_escape_htmltag($object->$key, 1, 1);
373  if (preg_match('/^(text|html)/', $val['type'])) {
374  print '</div>';
375  }
376  } else {
377  print $form->editfieldval($labeltoshow, $key, $value, $object, 1, $val['type']);
378  }
379  print '</td>';
380  print '</tr>';
381  }
382 
383  // Other attributes. Fields from hook formObjectOptions and Extrafields.
384  include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
385  print '</table>';
386 
387  // Depreciation options attributes
388  print '<div class="fichehalfleft">';
389  print '<table class="border centpercent tableforfield">';
390  include DOL_DOCUMENT_ROOT . '/asset/tpl/depreciation_options_view.tpl.php';
391  print '</table>';
392  print '</div>';
393 
394  // Accountancy codes attributes
395  print '<div class="fichehalfright">';
396  print '<table class="border centpercent tableforfield">';
397  include DOL_DOCUMENT_ROOT . '/asset/tpl/accountancy_codes_view.tpl.php';
398  print '</table>';
399  print '</div>';
400 
401  print '</div>';
402  print '<div class="clearboth"></div>';
403 
404  print dol_get_fiche_end();
405 
406 
407  // Buttons for actions
408  if ($action != 'editline') {
409  print '<div class="tabsAction">' . "\n";
410  $parameters = array();
411  $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
412  if ($reshook < 0) {
413  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
414  }
415 
416  if (empty($reshook)) {
417  if ($object->status == $object::STATUS_DRAFT) {
418  print dolGetButtonAction($langs->trans('Modify'), '', 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=edit&token=' . newToken(), '', $permissiontoadd);
419  }
420 
421  // Back to draft
422  if ($object->status != $object::STATUS_DRAFT) {
423  print dolGetButtonAction($langs->trans('SetToDraft'), '', 'default', $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=confirm_setdraft&confirm=yes&token=' . newToken(), '', $permissiontoadd);
424  }
425 
426  if ($object->status != $object::STATUS_VALIDATED) {
427  print dolGetButtonAction($langs->trans('Enable'), '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=confirm_validate&confirm=yes&token=' . newToken(), '', $permissiontoadd);
428  }
429 
430  if ($object->status != $object::STATUS_CANCELED) {
431  print dolGetButtonAction($langs->trans('Disable'), '', 'default', $_SERVER['PHP_SELF'].'?id='.$object->id.'&action=confirm_close&confirm=yes&token='.newToken(), '', $permissiontoadd);
432  }
433 
434  // Clone
435  print dolGetButtonAction($langs->trans('ToClone'), '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . (!empty($socid) ? '&socid=' . $socid : '') . '&action=clone&token=' . newToken(), '', $permissiontoadd);
436 
437  // Delete (need delete permission, or if draft, just need create/modify permission)
438  print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=delete&token=' . newToken(), '', $permissiontodelete || ($object->status == $object::STATUS_DRAFT && $permissiontoadd));
439  }
440  print '</div>' . "\n";
441  }
442 
443  print '<div class="fichecenter"><div class="fichehalfleft">';
444  print '<a name="builddoc"></a>'; // ancre
445 
446  print '</div><div class="fichehalfright">';
447 
448  // $MAXEVENT = 10;
449  //
450  // $morehtmlcenter = dolGetButtonTitle($langs->trans('SeeAll'), '', 'fa fa-bars imgforviewmode', DOL_URL_ROOT . '/asset/model/agenda.php?id=' . $object->id);
451  //
452  // // List of actions on element
453  // include_once DOL_DOCUMENT_ROOT . '/core/class/html.formactions.class.php';
454  // $formactions = new FormActions($db);
455  // $somethingshown = $formactions->showactions($object, $object->element.'@'.$object->module, 0, 1, '', $MAXEVENT, '', $morehtmlright);
456 
457  print '</div></div>';
458 }
459 
460 // End of page
461 llxFooter();
462 $db->close();
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
assetModelPrepareHead($object)
Prepare array of tabs for AssetModel.
Definition: asset.lib.php:183
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:55
llxFooter()
Empty footer.
Definition: wrapper.php:69
Class for AssetAccountancyCodes.
Class for AssetDepreciationOptions.
Class for AssetModel.
Class to manage standard extra fields.
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
$parameters
Actions.
Definition: card.php:84
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
verifCond($strToEvaluate, $onlysimplestring='1')
Verify if condition in string is ok or not.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
picto_from_langcode($codelang, $moreatt='', $notitlealt=0)
Return img flag of country for a language code or country code.
showValueWithClipboardCPButton($valuetocopy, $showonlyonhover=1, $texttoshow='')
Create a button to copy $valuetocopy in the clipboard (for copy and paste feature).
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by the value of a given key, which produces ascending (default) or descending out...
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dolGetButtonAction($label, $text='', $actionType='default', $url='', $id='', $userRight=1, $params=array())
Function dolGetButtonAction.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
$formconfirm
if ($action == 'delbookkeepingyear') {
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.