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
19/**
20 *  \file       htdocs/asset/card.php
21 *  \ingroup    asset
22 *  \brief      Page to create/edit/view asset
23 */
24
25require '../main.inc.php';
26require_once DOL_DOCUMENT_ROOT.'/core/lib/asset.lib.php';
27require_once DOL_DOCUMENT_ROOT.'/asset/class/asset.class.php';
28require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
30
31// Load translation files required by the page
32$langs->loadLangs(array("asset"));
33
34// Get parameters
35$id         = GETPOST('id', 'int');
36$ref        = GETPOST('ref', 'alpha');
37$action     = GETPOST('action', 'aZ09');
38$confirm    = GETPOST('confirm', 'alpha');
39$cancel     = GETPOST('cancel', 'aZ09');
40$contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'assetcard'; // To manage different context of search
41$backtopage = GETPOST('backtopage', 'alpha');
42$backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');
43
44// Initialize technical objects
45$object = new Asset($db);
46$extrafields = new ExtraFields($db);
47$diroutputmassaction = $conf->asset->dir_output.'/temp/massgeneration/'.$user->id;
48$hookmanager->initHooks(array('assetcard', 'globalcard')); // Note that conf->hooks_modules contains array
49
50// Fetch optionals attributes and labels
51$extrafields->fetch_name_optionals_label($object->table_element);
52
53$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
54
55// Initialize array of search criterias
56$search_all = GETPOST("search_all", 'alpha');
57$search = array();
58foreach ($object->fields as $key => $val) {
59	if (GETPOST('search_'.$key, 'alpha')) {
60		$search[$key] = GETPOST('search_'.$key, 'alpha');
61	}
62}
63
64if (empty($action) && empty($id) && empty($ref)) {
65	$action = 'view';
66}
67
68// Load object
69include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
70
71// Security check
72if (!empty($user->socid)) {
73	$socid = $user->socid;
74}
75$result = restrictedArea($user, 'asset', $id);
76
77$permissiontoread = $user->rights->asset->read;
78$permissiontoadd = $user->rights->asset->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
79$permissiontodelete = $user->rights->asset->delete || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
80$permissionnote = $user->rights->asset->write; // Used by the include of actions_setnotes.inc.php
81$permissiondellink = $user->rights->asset->write; // Used by the include of actions_dellink.inc.php
82$upload_dir = $conf->asset->multidir_output[isset($object->entity) ? $object->entity : 1];
83
84
85/*
86 * Actions
87 */
88
89$parameters = array();
90$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
91if ($reshook < 0) {
92	setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
93}
94
95if (empty($reshook)) {
96	$error = 0;
97
98	$backurlforlist = dol_buildpath('/asset/list.php', 1);
99
100	// Actions cancel, add, update or delete
101	include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
102
103	// Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
104	include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
105
106	// Actions when linking object each other
107	include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php';
108
109	// Actions when printing a doc from card
110	include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
111
112	// Action to move up and down lines of object
113	//include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
114
115	// Action to build doc
116	include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
117
118	if ($action == 'set_thirdparty' && $permissiontoadd) {
119		$object->setValueFrom('fk_soc', GETPOST('fk_soc', 'int'), '', '', 'date', '', $user, 'MYOBJECT_MODIFY');
120	}
121	if ($action == 'classin' && $permissiontoadd) {
122		$object->setProject(GETPOST('projectid', 'int'));
123	}
124
125	// Actions to send emails
126	$triggersendname = 'ASSET_SENTBYMAIL';
127	$autocopy = 'MAIN_MAIL_AUTOCOPY_ASSET_TO';
128	$trackid = 'asset'.$object->id;
129	include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
130}
131
132/*
133 * View
134 *
135 */
136
137$form = new Form($db);
138$formfile = new FormFile($db);
139
140$title = $langs->trans("Asset").' - '.$langs->trans("Card");
141$help_url = '';
142llxHeader('', $title, $help_url);
143
144// Part to create
145if ($action == 'create') {
146	print load_fiche_titre($langs->trans("NewAsset"), '', $object->picto);
147
148	print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
149	print '<input type="hidden" name="token" value="'.newToken().'">';
150	print '<input type="hidden" name="action" value="add">';
151	if ($backtopage) {
152		print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
153	}
154	if ($backtopageforcancel) {
155		print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
156	}
157
158	print dol_get_fiche_head(array(), '');
159
160	print '<table class="border centpercent tableforfieldcreate">'."\n";
161
162	// Common attributes
163	include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_add.tpl.php';
164
165	// Other attributes
166	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
167
168	print '</table>'."\n";
169
170	print dol_get_fiche_end();
171
172	print '<div class="center">';
173	print '<input type="submit" class="button" name="add" value="'.dol_escape_htmltag($langs->trans("Create")).'">';
174	print '&nbsp; ';
175	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
176	print '</div>';
177
178	print '</form>';
179
180	//dol_set_focus('input[name="ref"]');
181}
182
183// Part to edit record
184if (($id || $ref) && $action == 'edit') {
185	print load_fiche_titre($langs->trans("Assets"));
186
187	print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
188	print '<input type="hidden" name="token" value="'.newToken().'">';
189	print '<input type="hidden" name="action" value="update">';
190	print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
191	print '<input type="hidden" name="id" value="'.$object->id.'">';
192	if ($backtopage) {
193		print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
194	}
195	if ($backtopageforcancel) {
196		print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
197	}
198
199	print dol_get_fiche_head();
200
201	print '<table class="border centpercent tableforfieldedit">'."\n";
202
203	// Common attributes
204	include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_edit.tpl.php';
205
206	// Other attributes
207	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_edit.tpl.php';
208
209	print '</table>';
210
211	print dol_get_fiche_end();
212
213	print '<div class="center"><input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
214	print ' &nbsp; <input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
215	print '</div>';
216
217	print '</form>';
218}
219
220// Part to show record
221if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
222	$res = $object->fetch_optionals();
223
224	$head = asset_prepare_head($object);
225	print dol_get_fiche_head($head, 'card', $langs->trans("Asset"), -1, $object->picto);
226
227	$formconfirm = '';
228
229	// Confirmation to delete
230	if ($action == 'delete') {
231		$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteAssets'), $langs->trans('ConfirmDeleteAsset'), 'confirm_delete', '', 0, 1);
232	}
233
234	// Call Hook formConfirm
235	$parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
236	$reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
237	if (empty($reshook)) {
238		$formconfirm .= $hookmanager->resPrint;
239	} elseif ($reshook > 0) {
240		$formconfirm = $hookmanager->resPrint;
241	}
242
243	// Print form confirm
244	print $formconfirm;
245
246
247	// Object card
248	// ------------------------------------------------------------
249	$linkback = '<a href="'.dol_buildpath('/asset/list.php', 1).'?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
250
251	$morehtmlref = '<div class="refidno">';
252	/*
253	// Ref bis
254	$morehtmlref.=$form->editfieldkey("RefBis", 'ref_client', $object->ref_client, $object, $user->rights->asset->creer, 'string', '', 0, 1);
255	$morehtmlref.=$form->editfieldval("RefBis", 'ref_client', $object->ref_client, $object, $user->rights->asset->creer, 'string', '', null, null, '', 1);
256	// Thirdparty
257	$morehtmlref.='<br>'.$langs->trans('ThirdParty') . ' : ' . $soc->getNomUrl(1);
258	*/
259	$morehtmlref .= '</div>';
260
261	dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
262
263	print '<div class="fichecenter">';
264	print '<div class="fichehalfleft">';
265	print '<div class="underbanner clearboth"></div>';
266	print '<table class="border centpercent">'."\n";
267
268	// Common attributes
269	//$keyforbreak='fieldkeytoswitchonsecondcolumn';	// We change column just after this field
270	//unset($object->fields['fk_project']);				// Hide field already shown in banner
271	//unset($object->fields['fk_soc']);					// Hide field already shown in banner
272	include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
273
274	// Other attributes. Fields from hook formObjectOptions and Extrafields.
275	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
276
277	print '</table>';
278	print '</div>';
279
280	print '<div class="clearboth"></div>';
281
282	print dol_get_fiche_end();
283
284
285	/*
286	 * Buttons
287	 */
288	if ($user->socid == 0) {
289		print '<div class="tabsAction">';
290
291		$parameters = array();
292		$reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
293
294		if (empty($reshook)) {
295			if ($user->rights->asset->write) {
296				print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=edit">'.$langs->trans("Modify").'</a>'."\n";
297			} else {
298				print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">'.$langs->trans('Modify').'</a>'."\n";
299			}
300
301			if ($user->rights->asset->delete) {
302				print '<a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete&token='.newToken().'">'.$langs->trans('Delete').'</a>'."\n";
303			} else {
304				print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">'.$langs->trans('Delete').'</a>'."\n";
305			}
306		}
307		print "</div>";
308	}
309
310	if ($action != 'presend') {
311		print '<div class="fichecenter"><div class="fichehalfleft">';
312		print '<a name="builddoc"></a>'; // ancre
313
314		// Documents
315		$filename = dol_sanitizeFileName($object->ref);
316		$filedir = $conf->contrat->dir_output."/".dol_sanitizeFileName($object->ref);
317		$urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
318		$genallowed = $user->rights->asset->read;	// If you can read, you can build the PDF to read content
319		$delallowed = $user->rights->asset->write;	// If you can create/edit, you can remove a file on card
320
321		print $formfile->showdocuments('asset', $filename, $filedir, $urlsource, 0, $delallowed, $object->model_pdf, 1, 0, 0, 28, 0, '', '', '', $soc->default_lang);
322
323		// Show links to link elements
324		$linktoelem = $form->showLinkToObjectBlock($object, null, array('asset'));
325		$somethingshown = $form->showLinkedObjectBlock($object, $linktoelem);
326
327		print '</div><div class="fichehalfright"><div class="ficheaddleft">';
328
329		$MAXEVENT = 10;
330
331		$morehtmlcenter = dolGetButtonTitle($langs->trans('SeeAll'), '', 'fa fa-list-alt imgforviewmode', DOL_URL_ROOT.'/asset/info.php?id='.$object->id);
332
333		// List of actions on element
334		include_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
335		$formactions = new FormActions($db);
336		$somethingshown = $formactions->showactions($object, 'asset', $socid, 1, '', $MAXEVENT, '', $morehtmlright);
337
338		print '</div></div></div>';
339	}
340}
341
342
343// End of page
344llxFooter();
345$db->close();
346