1<?php
2/* Copyright (C) 2012      Regis Houssin        <regis.houssin@inodbox.com>
3 * Copyright (C) 2013-2015 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2015-2016 Charlie BENKE 	<charlie@patas-monkey.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * This template needs:
20 * $object
21 * $withproject (if we are on task contact)
22 *
23 * $preselectedtypeofcontact may be defined or not
24 */
25
26// Protection to avoid direct call of template
27if (empty($object) || !is_object($object))
28{
29	print "Error, template page can't be called as URL";
30	exit;
31}
32
33if (empty($preselectedtypeofcontact)) {
34	$preselectedtypeofcontact = 0;
35}
36
37require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
38require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
39
40$module = $object->element;
41
42// Special cases
43if ($module == 'propal') {
44	$permission = $user->rights->propale->creer;
45} elseif ($module == 'fichinter') {
46	$permission = $user->rights->ficheinter->creer;
47} elseif ($module == 'order_supplier') {
48	$permission = $user->rights->fournisseur->commande->creer;
49} elseif ($module == 'invoice_supplier') {
50	$permission = $user->rights->fournisseur->facture->creer;
51} elseif ($module == 'project') {
52	$permission = $user->rights->projet->creer;
53} elseif ($module == 'action') {
54	$permission = $user->rights->agenda->myactions->create;
55} elseif ($module == 'shipping') {
56	$permission = $user->rights->expedition->creer;
57} elseif ($module == 'reception') {
58	$permission = $user->rights->reception->creer;
59} elseif ($module == 'project_task') {
60	$permission = $user->rights->projet->creer;
61} elseif (!isset($permission) && isset($user->rights->$module->creer)) {
62	$permission = $user->rights->$module->creer;
63} elseif (!isset($permission) && isset($user->rights->$module->write)) {
64	$permission = $user->rights->$module->write;
65}
66
67$formcompany = new FormCompany($db);
68$companystatic = new Societe($db);
69$contactstatic = new Contact($db);
70$userstatic = new User($db);
71
72?>
73
74<!-- BEGIN PHP TEMPLATE CONTACTS -->
75<?php
76if ($permission)
77{
78	print '<div class="underbanner clearboth"></div>'."\n";
79
80	print '<div class="div-table-responsive-no-min">'."\n";
81	print '<div class="tagtable tableforcontact centpercent noborder nobordertop allwidth">'."\n";
82
83	?>
84	<form class="tagtr liste_titre">
85		<div class="tagtd liste_titre"><?php echo $langs->trans("NatureOfContact"); ?></div>
86		<div class="tagtd liste_titre"><?php echo $langs->trans("ThirdParty"); ?></div>
87		<div class="tagtd liste_titre"><?php echo $langs->trans("Users").'/'.$langs->trans("Contacts"); ?></div>
88		<div class="tagtd liste_titre"><?php echo $langs->trans("ContactType"); ?></div>
89		<div class="tagtd liste_titre">&nbsp;</div>
90		<div class="tagtd liste_titre">&nbsp;</div>
91	</form>
92
93	<?php
94
95	if (empty($hideaddcontactforuser))
96	{
97		?>
98	<form class="tagtr impair nohover" action="<?php echo $_SERVER["PHP_SELF"].'?id='.$object->id; ?>" method="POST">
99	<input type="hidden" name="token" value="<?php echo newToken(); ?>" />
100	<input type="hidden" name="id" value="<?php echo $object->id; ?>" />
101	<input type="hidden" name="action" value="addcontact" />
102	<input type="hidden" name="source" value="internal" />
103	    <?php if ($withproject) print '<input type="hidden" name="withproject" value="'.$withproject.'">'; ?>
104		<div class="nowrap tagtd"><?php echo img_object('', 'user').' '.$langs->trans("Users"); ?></div>
105		<div class="tagtd"><?php echo $conf->global->MAIN_INFO_SOCIETE_NOM; ?></div>
106		<div class="tagtd maxwidthonsmartphone"><?php echo $form->select_dolusers($user->id, 'userid', 0, (!empty($userAlreadySelected) ? $userAlreadySelected : null), 0, null, null, 0, 56, '', 0, '', 'minwidth200imp'); ?></div>
107		<div class="tagtd maxwidthonsmartphone">
108		<?php
109		$tmpobject = $object;
110		if (($object->element == 'shipping' || $object->element == 'reception') && is_object($objectsrc)) $tmpobject = $objectsrc;
111		$formcompany->selectTypeContact($tmpobject, '', 'type', 'internal');
112		?></div>
113		<div class="tagtd">&nbsp;</div>
114		<div class="tagtd center"><input type="submit" class="button" value="<?php echo $langs->trans("Add"); ?>"></div>
115	</form>
116
117	    <?php
118	}
119
120	if (empty($hideaddcontactforthirdparty)) {
121		?>
122
123	<form class="tagtr pair nohover" action="<?php echo $_SERVER["PHP_SELF"].'?id='.$object->id; ?>" method="POST">
124	<input type="hidden" name="token" value="<?php echo newToken(); ?>" />
125	<input type="hidden" name="id" value="<?php echo $object->id; ?>" />
126	<input type="hidden" name="action" value="addcontact" />
127	<input type="hidden" name="source" value="external" />
128	    <?php if ($withproject) print '<input type="hidden" name="withproject" value="'.$withproject.'">'; ?>
129		<div class="tagtd nowrap noborderbottom"><?php echo img_object('', 'contact').' '.$langs->trans("ThirdPartyContacts"); ?></div>
130		<div class="tagtd nowrap maxwidthonsmartphone noborderbottom">
131			<?php $selectedCompany = isset($_GET["newcompany"]) ? $_GET["newcompany"] : $object->socid; ?>
132			<?php
133			// add company icon before select list
134			if ($selectedCompany)
135			{
136				echo img_object('', 'company', 'class="hideonsmartphone"');
137			}
138			?>
139			<?php $selectedCompany = $formcompany->selectCompaniesForNewContact($object, 'id', $selectedCompany, 'newcompany', '', 0, '', 'minwidth300imp'); ?>
140		</div>
141		<div class="tagtd maxwidthonsmartphone noborderbottom">
142			<?php
143			print $form->selectcontacts(($selectedCompany > 0 ? $selectedCompany : -1), '', 'contactid', 3, '', '', 1, 'minwidth100imp');
144			$nbofcontacts = $form->num;
145
146			$newcardbutton = '';
147			if (!empty($object->socid) && $object->socid > 1 && $user->rights->societe->creer)
148			{
149				$newcardbutton .= '<a href="'.DOL_URL_ROOT.'/contact/card.php?socid='.$selectedCompany.'&action=create&backtopage='.urlencode($_SERVER["PHP_SELF"].'?id='.$object->id).'" title="'.$langs->trans('NewContact').'"><span class="fa fa-plus-circle valignmiddle paddingleft"></span></a>';
150			}
151			print $newcardbutton;
152			?>
153		</div>
154		<div class="tagtd maxwidthonsmartphone noborderbottom">
155			<?php
156			$tmpobject = $object;
157			if (($object->element == 'shipping' || $object->element == 'reception') && is_object($objectsrc)) $tmpobject = $objectsrc;
158			$formcompany->selectTypeContact($tmpobject, $preselectedtypeofcontact, 'typecontact', 'external', 'position', 0, 'minwidth100imp');
159			?>
160		</div>
161		<div class="tagtd noborderbottom">&nbsp;</div>
162		<div class="tagtd center noborderbottom">
163			<input type="submit" id="add-customer-contact" class="button" value="<?php echo $langs->trans("Add"); ?>"<?php if (!$nbofcontacts) echo ' disabled'; ?>>
164		</div>
165	</form>
166
167        <?php
168	}
169
170	print "</div>";
171	print "</div>";
172
173	print '<br>';
174}
175
176
177// Prepare list
178
179// TODO: replace this with direct SQL string to use $db->sort($sortfield, $sortorder)
180$list = array();
181foreach (array('internal', 'external') as $source)
182{
183	if (($object->element == 'shipping' || $object->element == 'reception') && is_object($objectsrc))
184	{
185		$contactlist = $objectsrc->liste_contact(-1, $source);
186	} else {
187		$contactlist = $object->liste_contact(-1, $source);
188	}
189
190	foreach ($contactlist as $contact)
191	{
192		$entry = new stdClass();
193		$entry->id   = $contact['rowid'];
194		$entry->type = $contact['libelle'];
195		$entry->nature = "";
196		$entry->thirdparty_html = "";
197		$entry->thirdparty_name = "";
198		$entry->contact_html = "";
199		$entry->contact_name = "";
200		$entry->status = "";
201
202		if ($contact['source'] == 'internal')
203		{
204			$entry->nature = $langs->trans("User");
205		} elseif ($contact['source'] == 'external') {
206			$entry->nature = $langs->trans("ThirdPartyContact");
207		}
208
209		if ($contact['socid'] > 0)
210		{
211			$companystatic->fetch($contact['socid']);
212			$entry->thirdparty_html = $companystatic->getNomUrl(1);
213			$entry->thirdparty_name = strtolower($companystatic->getFullName($langs));
214		} elseif ($contact['socid'] < 0) {
215			$entry->thirdparty_html = $conf->global->MAIN_INFO_SOCIETE_NOM;
216			$entry->thirdparty_name = strtolower($conf->global->MAIN_INFO_SOCIETE_NOM);
217		}
218
219		if ($contact['source'] == 'internal')
220		{
221			$userstatic->fetch($contact['id']);
222			$entry->contact_html = $userstatic->getNomUrl(-1, '', 0, 0, 0, 0, '', 'valignmiddle');
223			$entry->contact_name = strtolower($userstatic->getFullName($langs));
224		} elseif ($contact['source'] == 'external') {
225			$contactstatic->fetch($contact['id']);
226			$entry->contact_html = $contactstatic->getNomUrl(1, '', 0, '', 0, 0);
227			$entry->contact_name = strtolower($contactstatic->getFullName($langs));
228		}
229
230		if ($contact['source'] == 'internal')
231		{
232			$entry->status = $userstatic->LibStatut($contact['statuscontact'], 3);
233		} elseif ($contact['source'] == 'external') {
234			$entry->status = $contactstatic->LibStatut($contact['statuscontact'], 3);
235		}
236
237		$list[] = $entry;
238	}
239}
240
241
242$sortfield = GETPOST("sortfield", "aZ09comma");
243$sortorder = GETPOST("sortorder", 'aZ09comma');
244
245if (!$sortfield) $sortfield = "nature";
246if (!$sortorder) $sortorder = "asc";
247
248// Re-sort list
249$list = dol_sort_array($list, $sortfield, $sortorder, 1, 0, 1);
250
251$arrayfields = array(
252	'rowid' 		=> array('label'=>$langs->trans("Id"), 'checked'=>1),
253	'nature' 		=> array('label'=>$langs->trans("NatureOfContact"), 'checked'=>1),
254	'thirdparty' 	=> array('label'=>$langs->trans("ThirdParty"), 'checked'=>1),
255	'contact' 		=> array('label'=>$langs->trans("Users").'/'.$langs->trans("Contacts"), 'checked'=>1),
256	'type' 			=> array('label'=>$langs->trans("ContactType"), 'checked'=>1),
257	'status' 		=> array('label'=>$langs->trans("Status"), 'checked'=>1),
258	'link' 			=> array('label'=>$langs->trans("Link"), 'checked'=>1),
259);
260
261$param = 'id='.$object->id.'&mainmenu=home';
262
263/**
264 * Show list
265 */
266print '<form method="POST" id="searchFormList" action="'.$_SERVER["PHP_SELF"].'">';
267print '<input type="hidden" name="token" value="'.newToken().'">';
268print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
269print '<input type="hidden" name="action" value="list">';
270print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
271print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
272
273print '<div class="div-table-responsive-no-min">'."\n";
274print '<table class="tagtable nobottomiftotal liste">';
275
276//print '<tr class="liste_titre_filter">';
277//print '</tr>';
278
279print '<tr class="liste_titre">';
280print_liste_field_titre($arrayfields['nature']['label'], $_SERVER["PHP_SELF"], "nature", "", $param, "", $sortfield, $sortorder);
281print_liste_field_titre($arrayfields['thirdparty']['label'], $_SERVER["PHP_SELF"], "thirdparty_name", "", $param, "", $sortfield, $sortorder);
282print_liste_field_titre($arrayfields['contact']['label'], $_SERVER["PHP_SELF"], "contact_name", "", $param, "", $sortfield, $sortorder);
283print_liste_field_titre($arrayfields['type']['label'], $_SERVER["PHP_SELF"], "type", "", $param, "", $sortfield, $sortorder);
284print_liste_field_titre($arrayfields['status']['label'], $_SERVER["PHP_SELF"], "statut", "", $param, "", $sortfield, $sortorder, 'center ');
285print_liste_field_titre('', $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder, 'center maxwidthsearch ');
286print "</tr>";
287
288foreach ($list as $entry)
289{
290	print '<tr class="oddeven">';
291
292	print '<td class="nowrap">'.$entry->nature.'</td>';
293	print '<td class="tdoverflowmax200">'.$entry->thirdparty_html.'</td>';
294	print '<td class="tdoverflowmax200">'.$entry->contact_html.'</td>';
295	print '<td class="tdoverflowmax200">'.$entry->type.'</td>';
296	print '<td class="tdoverflowmax200 center">'.$entry->status.'</td>';
297
298	if ($permission)
299	{
300		$href = $_SERVER["PHP_SELF"];
301		$href .= '?id='.$object->id;
302		$href .= '&action=deletecontact&token='.newToken();
303		$href .= '&lineid='.$entry->id;
304
305		print "<td class='center'>";
306		print "<a href='$href'>";
307		print img_picto($langs->trans("Unlink"), "unlink");
308		print "</a>";
309		print "</td>";
310	}
311
312	print "</tr>";
313}
314
315print "</table>";
316print '</div>';
317
318print "</form>";
319
320print "<!-- TEMPLATE CONTACTS HOOK BEGIN HERE -->\n";
321if (is_object($hookmanager)) {
322	$hookmanager->initHooks(array('contacttpl'));
323	$parameters = array();
324	$reshook = $hookmanager->executeHooks('formContactTpl', $parameters, $object, $action);
325}
326print "<!-- END PHP TEMPLATE CONTACTS -->\n";
327