1<?php
2/* Copyright (C) 2007-2017 Laurent Destailleur  <eldy@users.sourceforge.net>
3 * Copyright (C) 2021		NextGestion			<contact@nextgestion.com>
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       partnership_contact.php
21 *  \ingroup    partnership
22 *  \brief      Tab for contacts linked to Partnership
23 */
24
25
26// Load Dolibarr environment
27require '../main.inc.php';
28
29require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
31require_once DOL_DOCUMENT_ROOT.'/partnership/class/partnership.class.php';
32require_once DOL_DOCUMENT_ROOT.'/partnership/lib/partnership.lib.php';
33
34// Load translation files required by the page
35$langs->loadLangs(array("partnership", "companies", "other", "mails"));
36
37$id     = (GETPOST('id') ?GETPOST('id', 'int') : GETPOST('facid', 'int')); // For backward compatibility
38$ref    = GETPOST('ref', 'alpha');
39$lineid = GETPOST('lineid', 'int');
40$socid  = GETPOST('socid', 'int');
41$action = GETPOST('action', 'aZ09');
42
43// Initialize technical objects
44$object = new Partnership($db);
45$extrafields = new ExtraFields($db);
46$diroutputmassaction = $conf->partnership->dir_output.'/temp/massgeneration/'.$user->id;
47$hookmanager->initHooks(array('partnershipcontact', 'globalcard')); // Note that conf->hooks_modules contains array
48// Fetch optionals attributes and labels
49$extrafields->fetch_name_optionals_label($object->table_element);
50
51// Load object
52include 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
53
54// Security check - Protection if external user
55//if ($user->socid > 0) accessforbidden();
56//if ($user->socid > 0) $socid = $user->socid;
57//$result = restrictedArea($user, 'partnership', $object->id);
58
59$permission = $user->rights->partnership->write;
60
61/*
62 * Add a new contact
63 */
64
65if ($action == 'addcontact' && $permission) {
66	$contactid = (GETPOST('userid') ? GETPOST('userid', 'int') : GETPOST('contactid', 'int'));
67	$typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type'));
68	$result = $object->add_contact($contactid, $typeid, GETPOST("source", 'aZ09'));
69
70	if ($result >= 0) {
71		header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
72		exit;
73	} else {
74		if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
75			$langs->load("errors");
76			setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
77		} else {
78			setEventMessages($object->error, $object->errors, 'errors');
79		}
80	}
81} elseif ($action == 'swapstatut' && $permission) {
82	// Toggle the status of a contact
83	$result = $object->swapContactStatus(GETPOST('ligne'));
84} elseif ($action == 'deletecontact' && $permission) {
85	// Deletes a contact
86	$result = $object->delete_contact($lineid);
87
88	if ($result >= 0) {
89		header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
90		exit;
91	} else {
92		dol_print_error($db);
93	}
94}
95
96
97/*
98 * View
99 */
100
101$title = $langs->trans('Partnership')." - ".$langs->trans('ContactsAddresses');
102$help_url = '';
103//$help_url='EN:Module_Third_Parties|FR:Module_Tiers|ES:Empresas';
104llxHeader('', $title, $help_url);
105
106$form = new Form($db);
107$formcompany = new FormCompany($db);
108$contactstatic = new Contact($db);
109$userstatic = new User($db);
110
111
112/* *************************************************************************** */
113/*                                                                             */
114/* View and edit mode                                                         */
115/*                                                                             */
116/* *************************************************************************** */
117
118if ($object->id) {
119	/*
120	 * Show tabs
121	 */
122	$head = partnershipPrepareHead($object);
123
124	print dol_get_fiche_head($head, 'contact', $langs->trans("Partnership"), -1, $object->picto);
125
126	$linkback = '<a href="'.dol_buildpath('/partnership/partnership_list.php', 1).'?restore_lastsearch_values=1'.(!empty($socid) ? '&socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
127
128	$morehtmlref = '<div class="refidno">';
129	/*
130	 // Ref customer
131	 $morehtmlref.=$form->editfieldkey("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', 0, 1);
132	 $morehtmlref.=$form->editfieldval("RefCustomer", 'ref_client', $object->ref_client, $object, 0, 'string', '', null, null, '', 1);
133	 // Thirdparty
134	 $morehtmlref.='<br>'.$langs->trans('ThirdParty') . ' : ' . (is_object($object->thirdparty) ? $object->thirdparty->getNomUrl(1) : '');
135	 // Project
136	 if (! empty($conf->projet->enabled))
137	 {
138	 $langs->load("projects");
139	 $morehtmlref.='<br>'.$langs->trans('Project') . ' ';
140	 if ($permissiontoadd)
141	 {
142	 if ($action != 'classify')
143	 //$morehtmlref.='<a class="editfielda" href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
144	 $morehtmlref.=' : ';
145	 if ($action == 'classify') {
146	 //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
147	 $morehtmlref.='<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
148	 $morehtmlref.='<input type="hidden" name="action" value="classin">';
149	 $morehtmlref.='<input type="hidden" name="token" value="'.newToken().'">';
150	 $morehtmlref.=$formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
151	 $morehtmlref.='<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
152	 $morehtmlref.='</form>';
153	 } else {
154	 $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
155	 }
156	 } else {
157	 if (! empty($object->fk_project)) {
158	 $proj = new Project($db);
159	 $proj->fetch($object->fk_project);
160	 $morehtmlref .= ': '.$proj->getNomUrl();
161	 } else {
162	 $morehtmlref .= '';
163	 }
164	 }
165	 }*/
166	$morehtmlref .= '</div>';
167
168	dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
169
170	print dol_get_fiche_end();
171
172	print '<br>';
173
174	// Contacts lines (modules that overwrite templates must declare this into descriptor)
175	$dirtpls = array_merge($conf->modules_parts['tpl'], array('/core/tpl'));
176	foreach ($dirtpls as $reldir) {
177		$res = @include dol_buildpath($reldir.'/contacts.tpl.php');
178		if ($res) {
179			break;
180		}
181	}
182}
183
184// End of page
185llxFooter();
186$db->close();
187