card.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2022 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.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. /**
  20. * \file htdocs/bookmarks/card.php
  21. * \ingroup bookmark
  22. * \brief Page display/creation of bookmarks
  23. */
  24. // Load Dolibarr environment
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/bookmarks/class/bookmark.class.php';
  27. // Load translation files required by the page
  28. $langs->loadLangs(array('bookmarks', 'other'));
  29. // Get Parameters
  30. $id = GETPOST("id", 'int');
  31. $action = GETPOST("action", "alpha");
  32. $title = (string) GETPOST("title", "alpha");
  33. $url = (string) GETPOST("url", "alpha");
  34. $urlsource = GETPOST("urlsource", "alpha");
  35. $target = GETPOST("target", "int");
  36. $userid = GETPOST("userid", "int");
  37. $position = GETPOST("position", "int");
  38. $backtopage = GETPOST('backtopage', 'alpha');
  39. // Initialize Objects
  40. $object = new Bookmark($db);
  41. if ($id > 0) {
  42. $object->fetch($id);
  43. }
  44. // Security check
  45. restrictedArea($user, 'bookmark', $object);
  46. $permissiontoread = $user->hasRight('bookmark', 'lire');
  47. $permissiontoadd = $user->hasRight('bookmark', 'creer');
  48. $permissiontodelete = $user->hasRight('bookmark', 'supprimer');
  49. /*
  50. * Actions
  51. */
  52. if ($action == 'add' || $action == 'addproduct' || $action == 'update') {
  53. if ($action == 'update') {
  54. $invertedaction = 'edit';
  55. } else {
  56. $invertedaction = 'create';
  57. }
  58. $error = 0;
  59. if (GETPOST('cancel', 'alpha')) {
  60. if (empty($backtopage)) {
  61. $backtopage = ($urlsource ? $urlsource : ((!empty($url) && !preg_match('/^http/i', $url)) ? $url : DOL_URL_ROOT.'/bookmarks/list.php'));
  62. }
  63. header("Location: ".$backtopage);
  64. exit;
  65. }
  66. if ($action == 'update') {
  67. $object->fetch(GETPOST("id", 'int'));
  68. }
  69. // Check if null because user not admin can't set an user and send empty value here.
  70. if (!empty($userid)) {
  71. $object->fk_user = $userid;
  72. }
  73. $object->title = $title;
  74. $object->url = $url;
  75. $object->target = $target;
  76. $object->position = $position;
  77. if (!$title) {
  78. $error++;
  79. setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->trans("BookmarkTitle")), null, 'errors');
  80. }
  81. if (!$url) {
  82. $error++;
  83. setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->trans("UrlOrLink")), null, 'errors');
  84. }
  85. if (!$error) {
  86. $object->favicon = 'none';
  87. if ($action == 'update') {
  88. $res = $object->update();
  89. } else {
  90. $res = $object->create();
  91. }
  92. if ($res > 0) {
  93. if (empty($backtopage)) {
  94. $backtopage = ($urlsource ? $urlsource : ((!empty($url) && !preg_match('/^http/i', $url)) ? $url : DOL_URL_ROOT.'/bookmarks/list.php'));
  95. }
  96. header("Location: ".$backtopage);
  97. exit;
  98. } else {
  99. if ($object->errno == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  100. $langs->load("errors");
  101. setEventMessages($langs->transnoentities("WarningBookmarkAlreadyExists"), null, 'warnings');
  102. } else {
  103. setEventMessages($object->error, $object->errors, 'errors');
  104. }
  105. $action = $invertedaction;
  106. }
  107. } else {
  108. $action = $invertedaction;
  109. }
  110. }
  111. /*
  112. * View
  113. */
  114. llxHeader();
  115. $form = new Form($db);
  116. $head = array();
  117. $h = 1;
  118. $head[$h][0] = $_SERVER["PHP_SELF"].($object->id ? '?id='.$object->id : '');
  119. $head[$h][1] = $langs->trans("Bookmark");
  120. $head[$h][2] = 'card';
  121. $h++;
  122. $hselected = 'card';
  123. if ($action == 'create') {
  124. /*
  125. * Fact bookmark creation mode
  126. */
  127. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST" enctype="multipart/form-data">'."\n";
  128. print '<input type="hidden" name="token" value="'.newToken().'">';
  129. print '<input type="hidden" name="action" value="add">';
  130. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  131. print load_fiche_titre($langs->trans("NewBookmark"), '', 'bookmark');
  132. print dol_get_fiche_head(null, 'bookmark', '', 0, '');
  133. print '<table class="border centpercent tableforfieldcreate">';
  134. print '<tr><td class="titlefieldcreate fieldrequired">'.$langs->trans("BookmarkTitle").'</td><td><input id="titlebookmark" class="flat minwidth250" name="title" value="'.dol_escape_htmltag($title).'"></td><td class="hideonsmartphone"><span class="opacitymedium">'.$langs->trans("SetHereATitleForLink").'</span></td></tr>';
  135. dol_set_focus('#titlebookmark');
  136. // Url
  137. print '<tr><td class="fieldrequired">'.$langs->trans("UrlOrLink").'</td><td><input class="flat quatrevingtpercent minwidth500" name="url" value="'.dol_escape_htmltag($url).'"></td><td class="hideonsmartphone"><span class="opacitymedium">'.$langs->trans("UseAnExternalHttpLinkOrRelativeDolibarrLink").'</span></td></tr>';
  138. // Target
  139. print '<tr><td>'.$langs->trans("BehaviourOnClick").'</td><td>';
  140. $liste = array(0=>$langs->trans("ReplaceWindow"), 1=>$langs->trans("OpenANewWindow"));
  141. $defaulttarget = 1;
  142. if ($url && !preg_match('/^http/i', $url)) {
  143. $defaulttarget = 0;
  144. }
  145. print $form->selectarray('target', $liste, GETPOSTISSET('target') ? GETPOST('target', 'int') : $defaulttarget, 0, 0, 0, '', 0, 0, 0, '', 'maxwidth300');
  146. print '</td><td class="hideonsmartphone"><span class="opacitymedium">'.$langs->trans("ChooseIfANewWindowMustBeOpenedOnClickOnBookmark").'</span></td></tr>';
  147. // Visibility / Owner
  148. print '<tr><td>'.$langs->trans("Visibility").'</td><td>';
  149. print img_picto('', 'user', 'class="pictofixedwidth"');
  150. print $form->select_dolusers(GETPOSTISSET('userid') ? GETPOST('userid', 'int') : $user->id, 'userid', 0, '', 0, ($user->admin ? '' : array($user->id)), '', 0, 0, 0, '', ($user->admin) ? 1 : 0, '', 'maxwidth300 widthcentpercentminusx');
  151. print '</td><td class="hideonsmartphone"></td></tr>';
  152. // Position
  153. print '<tr><td>'.$langs->trans("Position").'</td><td>';
  154. print '<input class="flat width50" name="position" value="'.(GETPOSTISSET("position") ? GETPOST("position", 'int') : $object->position).'">';
  155. print '</td><td class="hideonsmartphone"></td></tr>';
  156. print '</table>';
  157. print dol_get_fiche_end();
  158. print $form->buttonsSaveCancel("CreateBookmark");
  159. print '</form>';
  160. }
  161. if ($id > 0 && !preg_match('/^add/i', $action)) {
  162. if ($action == 'edit') {
  163. print '<form name="edit" method="POST" action="'.$_SERVER["PHP_SELF"].'" enctype="multipart/form-data">';
  164. print '<input type="hidden" name="token" value="'.newToken().'">';
  165. print '<input type="hidden" name="action" value="update">';
  166. print '<input type="hidden" name="id" value="'.$object->id.'">';
  167. print '<input type="hidden" name="urlsource" value="'.DOL_URL_ROOT.'/bookmarks/card.php?id='.$object->id.'">';
  168. print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
  169. }
  170. print dol_get_fiche_head($head, $hselected, $langs->trans("Bookmark"), -1, 'bookmark');
  171. $linkback = '<a href="'.DOL_URL_ROOT.'/bookmarks/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  172. dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', '', '', 0, '', '', 0);
  173. print '<div class="fichecenter">';
  174. print '<div class="underbanner clearboth"></div>';
  175. print '<table class="border centpercent tableforfield">';
  176. print '<tr><td class="titlefield">';
  177. if ($action == 'edit') {
  178. print '<span class="fieldrequired">';
  179. }
  180. print $langs->trans("BookmarkTitle");
  181. if ($action == 'edit') {
  182. print '</span>';
  183. }
  184. print '</td><td>';
  185. if ($action == 'edit') {
  186. print '<input class="flat minwidth250" name="title" value="'.(GETPOSTISSET("title") ? GETPOST("title", '', 2) : $object->title).'">';
  187. } else {
  188. print dol_escape_htmltag($object->title);
  189. }
  190. print '</td></tr>';
  191. print '<tr><td>';
  192. if ($action == 'edit') {
  193. print '<span class="fieldrequired">';
  194. }
  195. print $langs->trans("UrlOrLink");
  196. if ($action == 'edit') {
  197. print '</span>';
  198. }
  199. print '</td><td>';
  200. if ($action == 'edit') {
  201. print '<input class="flat minwidth500 quatrevingtpercent" name="url" value="'.(GETPOSTISSET("url") ? GETPOST("url") : $object->url).'">';
  202. } else {
  203. print '<a href="'.(preg_match('/^http/i', $object->url) ? $object->url : DOL_URL_ROOT.$object->url).'"'.($object->target ? ' target="_blank" rel="noopener noreferrer"' : '').'>';
  204. print img_picto('', 'globe', 'class="paddingright"');
  205. print $object->url;
  206. print '</a>';
  207. }
  208. print '</td></tr>';
  209. print '<tr><td>'.$langs->trans("BehaviourOnClick").'</td><td>';
  210. if ($action == 'edit') {
  211. $liste = array(1=>$langs->trans("OpenANewWindow"), 0=>$langs->trans("ReplaceWindow"));
  212. print $form->selectarray('target', $liste, GETPOSTISSET("target") ? GETPOST("target") : $object->target);
  213. } else {
  214. if ($object->target == 0) {
  215. print $langs->trans("ReplaceWindow");
  216. }
  217. if ($object->target == 1) {
  218. print $langs->trans("OpenANewWindow");
  219. }
  220. }
  221. print '</td></tr>';
  222. // Visibility / owner
  223. print '<tr><td>'.$langs->trans("Visibility").'</td><td>';
  224. if ($action == 'edit' && $user->admin) {
  225. print img_picto('', 'user', 'class="pictofixedwidth"');
  226. print $form->select_dolusers(GETPOSTISSET('userid') ? GETPOST('userid', 'int') : ($object->fk_user ? $object->fk_user : ''), 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300 widthcentpercentminusx');
  227. } else {
  228. if ($object->fk_user > 0) {
  229. $fuser = new User($db);
  230. $fuser->fetch($object->fk_user);
  231. print $fuser->getNomUrl(-1);
  232. } else {
  233. print '<span class="opacitymedium">'.$langs->trans("Everybody").'</span>';
  234. }
  235. }
  236. print '</td></tr>';
  237. // Position
  238. print '<tr><td>'.$langs->trans("Position").'</td><td>';
  239. if ($action == 'edit') {
  240. print '<input class="flat" name="position" size="5" value="'.(GETPOSTISSET("position") ? GETPOST("position", 'int') : $object->position).'">';
  241. } else {
  242. print $object->position;
  243. }
  244. print '</td></tr>';
  245. // Date creation
  246. print '<tr><td>'.$langs->trans("DateCreation").'</td><td>'.dol_print_date($object->datec, 'dayhour').'</td></tr>';
  247. print '</table>';
  248. print '</div>';
  249. print dol_get_fiche_end();
  250. if ($action == 'edit') {
  251. print $form->buttonsSaveCancel();
  252. print '</form>';
  253. }
  254. // Buttons
  255. print "<div class=\"tabsAction\">\n";
  256. // Edit
  257. if ($permissiontoadd && $action != 'edit') {
  258. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=edit&token='.newToken().'">'.$langs->trans("Edit").'</a>'."\n";
  259. }
  260. // Remove
  261. if ($permissiontodelete && $action != 'edit') {
  262. print '<a class="butActionDelete" href="list.php?id='.$object->id.'&action=delete&token='.newToken().'">'.$langs->trans("Delete").'</a>'."\n";
  263. }
  264. print '</div>';
  265. }
  266. // End of page
  267. llxFooter();
  268. $db->close();