target_contact.php 8.6 KB

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