1<?php
2/* Copyright (C) 2017 Laurent Destailleur  <eldy@users.sourceforge.net>
3 * Copyright (C) ---Put here your own copyright and developer email---
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       knowledgerecord_card.php
21 *		\ingroup    knowledgemanagement
22 *		\brief      Page to create/edit/view knowledgerecord
23 */
24
25// Load Dolibarr environment
26require '../main.inc.php';
27
28require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
31require_once DOL_DOCUMENT_ROOT.'/knowledgemanagement/class/knowledgerecord.class.php';
32require_once DOL_DOCUMENT_ROOT.'/knowledgemanagement/lib/knowledgemanagement_knowledgerecord.lib.php';
33
34// Load translation files required by the page
35$langs->loadLangs(array("knowledgemanagement", "ticket", "other"));
36
37// Get parameters
38$id = GETPOST('id', 'int');
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') : 'knowledgerecordcard'; // To manage different context of search
44$backtopage = GETPOST('backtopage', 'alpha');
45$backtopageforcancel = GETPOST('backtopageforcancel', 'alpha');
46//$lineid   = GETPOST('lineid', 'int');
47
48// Initialize technical objects
49$object = new KnowledgeRecord($db);
50$extrafields = new ExtraFields($db);
51$diroutputmassaction = $conf->knowledgemanagement->dir_output.'/temp/massgeneration/'.$user->id;
52$hookmanager->initHooks(array('knowledgerecordcard', 'globalcard')); // Note that conf->hooks_modules contains array
53
54// Fetch optionals attributes and labels
55$extrafields->fetch_name_optionals_label($object->table_element);
56
57$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
58
59// Initialize array of search criterias
60$search_all = GETPOST("search_all", 'alpha');
61$search = array();
62foreach ($object->fields as $key => $val) {
63	if (GETPOST('search_'.$key, 'alpha')) {
64		$search[$key] = GETPOST('search_'.$key, 'alpha');
65	}
66}
67
68if (empty($action) && empty($id) && empty($ref)) {
69	$action = 'view';
70}
71
72// Load object
73include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
74
75
76$permissiontoread = $user->rights->knowledgemanagement->knowledgerecord->read;
77$permissiontovalidate = $user->rights->knowledgemanagement->knowledgerecord->write;
78$permissiontoadd = $user->rights->knowledgemanagement->knowledgerecord->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
79$permissiontodelete = $user->rights->knowledgemanagement->knowledgerecord->delete || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT);
80$permissionnote = $user->rights->knowledgemanagement->knowledgerecord->write; // Used by the include of actions_setnotes.inc.php
81$permissiondellink = $user->rights->knowledgemanagement->knowledgerecord->write; // Used by the include of actions_dellink.inc.php
82$upload_dir = $conf->knowledgemanagement->multidir_output[isset($object->entity) ? $object->entity : 1];
83
84// Security check - Protection if external user
85//if ($user->socid > 0) accessforbidden();
86//if ($user->socid > 0) $socid = $user->socid;
87//$isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
88//restrictedArea($user, $object->element, $object->id, '', '', 'fk_soc', 'rowid', $isdraft);
89//if (empty($conf->knowledgemanagement->enabled)) accessforbidden();
90//if (empty($permissiontoread)) accessforbidden();
91
92
93/*
94 * Actions
95 */
96
97$parameters = array();
98$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
99if ($reshook < 0) {
100	setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
101}
102
103if (empty($reshook)) {
104	$error = 0;
105
106	$backurlforlist = dol_buildpath('/knowledgemanagement/knowledgerecord_list.php', 1);
107
108	if (empty($backtopage) || ($cancel && empty($id))) {
109		if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
110			if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) {
111				$backtopage = $backurlforlist;
112			} else {
113				$backtopage = dol_buildpath('/knowledgemanagement/knowledgerecord_card.php', 1).'?id='.($id > 0 ? $id : '__ID__');
114			}
115		}
116	}
117
118	$triggermodname = 'KNOWLEDGEMANAGEMENT_KNOWLEDGERECORD_MODIFY'; // Name of trigger action code to execute when we modify record
119
120	// Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen
121	include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
122
123	// Actions when linking object each other
124	include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php';
125
126	// Actions when printing a doc from card
127	include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
128
129	// Action to move up and down lines of object
130	//include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php';
131
132	// Action to build doc
133	include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
134
135	if ($action == 'set_thirdparty' && $permissiontoadd) {
136		$object->setValueFrom('fk_soc', GETPOST('fk_soc', 'int'), '', '', 'date', '', $user, $triggermodname);
137	}
138	if ($action == 'classin' && $permissiontoadd) {
139		$object->setProject(GETPOST('projectid', 'int'));
140	}
141
142	// Actions to send emails
143	$triggersendname = 'KNOWLEDGEMANAGEMENT_KNOWLEDGERECORD_SENTBYMAIL';
144	$autocopy = 'MAIN_MAIL_AUTOCOPY_KNOWLEDGERECORD_TO';
145	$trackid = 'knowledgerecord'.$object->id;
146	include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
147}
148if ($action == 'confirm_validate') {
149	$action = 'edit';
150}
151
152/*
153 * View
154 */
155
156$form = new Form($db);
157$formfile = new FormFile($db);
158$formproject = new FormProjets($db);
159
160$title = $langs->trans("KnowledgeRecord");
161$help_url = '';
162llxHeader('', $title, $help_url);
163
164// Part to create
165if ($action == 'create') {
166	print load_fiche_titre($langs->trans("NewKnowledgeRecord"), '', 'object_'.$object->picto);
167
168	print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
169	print '<input type="hidden" name="token" value="'.newToken().'">';
170	print '<input type="hidden" name="action" value="add">';
171	if ($backtopage) {
172		print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
173	}
174	if ($backtopageforcancel) {
175		print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
176	}
177
178	print dol_get_fiche_head(array(), '');
179
180	// Set some default values
181	//if (! GETPOSTISSET('fieldname')) $_POST['fieldname'] = 'myvalue';
182
183	print '<table class="border centpercent tableforfieldcreate">'."\n";
184
185	// Common attributes
186	include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_add.tpl.php';
187
188	// Other attributes
189	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_add.tpl.php';
190
191	print '</table>'."\n";
192
193	print dol_get_fiche_end();
194
195	print '<div class="center">';
196	print '<input type="submit" class="button" name="add" value="'.dol_escape_htmltag($langs->trans("Create")).'">';
197	print '&nbsp; ';
198	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
199	print '</div>';
200
201	print '</form>';
202
203	//dol_set_focus('input[name="ref"]');
204}
205
206// Part to edit record
207if (($id || $ref) && $action == 'edit') {
208	print load_fiche_titre($langs->trans("KnowledgeRecord"), '', 'object_'.$object->picto);
209
210	print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
211	print '<input type="hidden" name="token" value="'.newToken().'">';
212	print '<input type="hidden" name="action" value="update">';
213	print '<input type="hidden" name="id" value="'.$object->id.'">';
214	if ($backtopage) {
215		print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
216	}
217	if ($backtopageforcancel) {
218		print '<input type="hidden" name="backtopageforcancel" value="'.$backtopageforcancel.'">';
219	}
220
221	print dol_get_fiche_head();
222
223	print '<table class="border centpercent tableforfieldedit">'."\n";
224
225	// Common attributes
226	include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_edit.tpl.php';
227
228	// Other attributes
229	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_edit.tpl.php';
230
231	print '</table>';
232
233	print dol_get_fiche_end();
234
235	print '<div class="center"><input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
236	print ' &nbsp; <input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
237	print '</div>';
238
239	print '</form>';
240}
241
242// Part to show record
243if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
244	$res = $object->fetch_optionals();
245
246	$head = knowledgerecordPrepareHead($object);
247	print dol_get_fiche_head($head, 'card', $langs->trans("KnowledgeRecord"), -1, $object->picto);
248
249	$formconfirm = '';
250
251	// Confirmation to delete
252	if ($action == 'delete') {
253		$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteKnowledgeRecord'), $langs->trans('ConfirmDeleteObject'), 'confirm_delete', '', 0, 1);
254	}
255	// Confirmation to delete line
256	if ($action == 'deleteline') {
257		$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid, $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_deleteline', '', 0, 1);
258	}
259	// Clone confirmation
260	if ($action == 'clone') {
261		// Create an array for form
262		$formquestion = array();
263		$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneAsk', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
264	}
265
266	// Call Hook formConfirm
267	$parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
268	$reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
269	if (empty($reshook)) {
270		$formconfirm .= $hookmanager->resPrint;
271	} elseif ($reshook > 0) {
272		$formconfirm = $hookmanager->resPrint;
273	}
274
275	// Print form confirm
276	print $formconfirm;
277
278
279	// Object card
280	// ------------------------------------------------------------
281	$linkback = '<a href="'.dol_buildpath('/knowledgemanagement/knowledgerecord_list.php', 1).'?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
282
283	$morehtmlref = '<div class="refidno">';
284	/*
285	 // Ref customer
286	 $morehtmlref.=$form->editfieldkey("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', 0, 1);
287	 $morehtmlref.=$form->editfieldval("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', null, null, '', 1);
288	 // Thirdparty
289	 $morehtmlref.='<br>'.$langs->trans('ThirdParty') . ' : ' . (is_object($object->thirdparty) ? $object->thirdparty->getNomUrl(1) : '');
290	 // Project
291	 if (! empty($conf->projet->enabled)) {
292	 $langs->load("projects");
293	 $morehtmlref .= '<br>'.$langs->trans('Project') . ' ';
294	 if ($permissiontoadd) {
295	 //if ($action != 'classify') $morehtmlref.='<a class="editfielda" href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> ';
296	 $morehtmlref .= ' : ';
297	 if ($action == 'classify') {
298	 //$morehtmlref .= $form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
299	 $morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
300	 $morehtmlref .= '<input type="hidden" name="action" value="classin">';
301	 $morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
302	 $morehtmlref .= $formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
303	 $morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
304	 $morehtmlref .= '</form>';
305	 } else {
306	 $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
307	 }
308	 } else {
309	 if (! empty($object->fk_project)) {
310	 $proj = new Project($db);
311	 $proj->fetch($object->fk_project);
312	 $morehtmlref .= ': '.$proj->getNomUrl();
313	 } else {
314	 $morehtmlref .= '';
315	 }
316	 }
317	 }*/
318	$morehtmlref .= '</div>';
319
320
321	dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
322
323
324	print '<div class="fichecenter">';
325	print '<div class="fichehalfleft">';
326	print '<div class="underbanner clearboth"></div>';
327	print '<table class="border centpercent tableforfield">'."\n";
328
329	// Common attributes
330	$keyforbreak='fk_c_ticket_category';	// We change column just before this field
331	//unset($object->fields['fk_project']);				// Hide field already shown in banner
332	//unset($object->fields['fk_soc']);					// Hide field already shown in banner
333	include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
334
335	// Other attributes. Fields from hook formObjectOptions and Extrafields.
336	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
337
338	print '</table>';
339	print '</div>';
340	print '</div>';
341
342	print '<div class="clearboth"></div>';
343
344	print dol_get_fiche_end();
345
346
347	// Buttons for actions
348
349	if ($action != 'presend' && $action != 'editline') {
350		print '<div class="tabsAction">'."\n";
351		$parameters = array();
352		$reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
353		if ($reshook < 0) {
354			setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
355		}
356
357		if (empty($reshook)) {
358			// Send
359			if (empty($user->socid)) {
360				//print dolGetButtonAction($langs->trans('SendMail'), '', 'default', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=presend&mode=init#formmailbeforetitle');
361			}
362
363			// Back to draft
364			if ($object->status == $object::STATUS_VALIDATED) {
365				print dolGetButtonAction($langs->trans('SetToDraft'), '', 'default', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=confirm_setdraft&confirm=yes', '', $permissiontoadd);
366			}
367			if ($object->status == $object::STATUS_VALIDATED && $permissiontovalidate) {
368				print dolGetButtonAction($langs->trans('Modify'), '', 'default', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=edit', '', $permissiontoadd);
369			}
370			// Validate
371			if ($object->status == $object::STATUS_DRAFT) {
372				if ((empty($object->table_element_line) || (is_array($object->lines) && count($object->lines) > 0)) && $permissiontovalidate) {
373					print dolGetButtonAction($langs->trans('ValidateReply'), '', 'default', $_SERVER['PHP_SELF'].'?id='.$object->id.'&action=confirm_validate&confirm=yes', '', $permissiontoadd);
374				} else {
375					$langs->load("errors");
376					//print dolGetButtonAction($langs->trans('Validate'), '', 'default', $_SERVER['PHP_SELF'].'?id='.$object->id.'&action=confirm_validate&confirm=yes', '', 0);
377					print '<a class="butActionRefused" href="" title="'.$langs->trans("ErrorAddAtLeastOneLineFirst").'">'.$langs->trans("Reply").'</a>';
378				}
379			}
380
381			// Clone
382			print dolGetButtonAction($langs->trans('ToClone'), '', 'default', $_SERVER['PHP_SELF'].'?id='.$object->id.'&socid='.$object->socid.'&action=clone&object=scrumsprint', '', $permissiontoadd);
383
384			/*
385			if ($permissiontoadd) {
386				if ($object->status == $object::STATUS_ENABLED) {
387					print '<a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=disable&token='.newToken().'">'.$langs->trans("Disable").'</a>'."\n";
388				} else {
389					print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=enable&token='.newToken().'">'.$langs->trans("Enable").'</a>'."\n";
390				}
391			}
392			if ($permissiontoadd) {
393				if ($object->status == $object::STATUS_VALIDATED) {
394					print '<a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=close&token='.newToken().'">'.$langs->trans("Cancel").'</a>'."\n";
395				} else {
396					print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=reopen&token='.newToken().'">'.$langs->trans("Re-Open").'</a>'."\n";
397				}
398			}
399			*/
400
401			// Delete (need delete permission, or if draft, just need create/modify permission)
402			print dolGetButtonAction($langs->trans('Delete'), '', 'delete', $_SERVER['PHP_SELF'].'?id='.$object->id.'&action=delete', '', $permissiontodelete || ($object->status == $object::STATUS_DRAFT && $permissiontoadd));
403		}
404		print '</div>'."\n";
405	}
406
407
408	// Select mail models is same action as presend
409	if (GETPOST('modelselected')) {
410		$action = 'presend';
411	}
412
413	if ($action != 'presend') {
414		print '<div class="fichecenter"><div class="fichehalfleft">';
415		print '<a name="builddoc"></a>'; // ancre
416
417		$includedocgeneration = 0;
418
419		// Documents
420		if ($includedocgeneration) {
421			$objref = dol_sanitizeFileName($object->ref);
422			$relativepath = $objref.'/'.$objref.'.pdf';
423			$filedir = $conf->knowledgemanagement->dir_output.'/'.$object->element.'/'.$objref;
424			$urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
425			$genallowed = $user->rights->knowledgemanagement->knowledgerecord->read; // If you can read, you can build the PDF to read content
426			$delallowed = $user->rights->knowledgemanagement->knowledgerecord->write; // If you can create/edit, you can remove a file on card
427			print $formfile->showdocuments('knowledgemanagement:KnowledgeRecord', $object->element.'/'.$objref, $filedir, $urlsource, $genallowed, $delallowed, $object->model_pdf, 1, 0, 0, 28, 0, '', '', '', $langs->defaultlang);
428		}
429
430		// Show links to link elements
431		$linktoelem = $form->showLinkToObjectBlock($object, null, array('knowledgerecord'));
432		$somethingshown = $form->showLinkedObjectBlock($object, $linktoelem);
433
434
435		print '</div><div class="fichehalfright"><div class="ficheaddleft">';
436
437		$MAXEVENT = 10;
438
439		$morehtmlright = '<a href="'.dol_buildpath('/knowledgemanagement/knowledgerecord_agenda.php', 1).'?id='.$object->id.'">';
440		$morehtmlright .= $langs->trans("SeeAll");
441		$morehtmlright .= '</a>';
442
443		// List of actions on element
444		include_once DOL_DOCUMENT_ROOT.'/core/class/html.formactions.class.php';
445		$formactions = new FormActions($db);
446		$somethingshown = $formactions->showactions($object, $object->element.'@'.$object->module, (is_object($object->thirdparty) ? $object->thirdparty->id : 0), 1, '', $MAXEVENT, '', $morehtmlright);
447
448		print '</div></div></div>';
449	}
450
451	//Select mail models is same action as presend
452	if (GETPOST('modelselected')) {
453		$action = 'presend';
454	}
455
456	// Presend form
457	$modelmail = 'knowledgerecord';
458	$defaulttopic = 'InformationMessage';
459	$diroutput = $conf->knowledgemanagement->dir_output;
460	$trackid = 'knowledgerecord'.$object->id;
461
462	include DOL_DOCUMENT_ROOT.'/core/tpl/card_presend.tpl.php';
463}
464
465// End of page
466llxFooter();
467$db->close();
468