card.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. // Title
  177. print '<tr><td class="titlefield">';
  178. if ($action == 'edit') {
  179. print '<span class="fieldrequired">';
  180. }
  181. print $langs->trans("BookmarkTitle");
  182. if ($action == 'edit') {
  183. print '</span>';
  184. }
  185. print '</td><td>';
  186. if ($action == 'edit') {
  187. print '<input class="flat minwidth250" name="title" value="'.(GETPOSTISSET("title") ? GETPOST("title", '', 2) : $object->title).'">';
  188. } else {
  189. print dol_escape_htmltag($object->title);
  190. }
  191. print '</td></tr>';
  192. // URL
  193. print '<tr><td>';
  194. if ($action == 'edit') {
  195. print '<span class="fieldrequired">';
  196. }
  197. print $langs->trans("UrlOrLink");
  198. if ($action == 'edit') {
  199. print '</span>';
  200. }
  201. print '</td><td class="tdoverflowmax500">';
  202. if ($action == 'edit') {
  203. print '<input class="flat minwidth500 quatrevingtpercent" name="url" value="'.(GETPOSTISSET("url") ? GETPOST("url") : $object->url).'">';
  204. } else {
  205. print '<a href="'.(preg_match('/^http/i', $object->url) ? $object->url : DOL_URL_ROOT.$object->url).'"'.($object->target ? ' target="_blank" rel="noopener noreferrer"' : '').'>';
  206. print img_picto('', 'globe', 'class="paddingright"');
  207. print $object->url;
  208. print '</a>';
  209. }
  210. print '</td></tr>';
  211. print '<tr><td>'.$langs->trans("BehaviourOnClick").'</td><td>';
  212. if ($action == 'edit') {
  213. $liste = array(1=>$langs->trans("OpenANewWindow"), 0=>$langs->trans("ReplaceWindow"));
  214. print $form->selectarray('target', $liste, GETPOSTISSET("target") ? GETPOST("target") : $object->target);
  215. } else {
  216. if ($object->target == 0) {
  217. print $langs->trans("ReplaceWindow");
  218. }
  219. if ($object->target == 1) {
  220. print $langs->trans("OpenANewWindow");
  221. }
  222. }
  223. print '</td></tr>';
  224. // Visibility / owner
  225. print '<tr><td>'.$langs->trans("Visibility").'</td><td>';
  226. if ($action == 'edit' && $user->admin) {
  227. print img_picto('', 'user', 'class="pictofixedwidth"');
  228. print $form->select_dolusers(GETPOSTISSET('userid') ? GETPOST('userid', 'int') : ($object->fk_user ? $object->fk_user : ''), 'userid', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth300 widthcentpercentminusx');
  229. } else {
  230. if ($object->fk_user > 0) {
  231. $fuser = new User($db);
  232. $fuser->fetch($object->fk_user);
  233. print $fuser->getNomUrl(-1);
  234. } else {
  235. print '<span class="opacitymedium">'.$langs->trans("Everybody").'</span>';
  236. }
  237. }
  238. print '</td></tr>';
  239. // Position
  240. print '<tr><td>'.$langs->trans("Position").'</td><td>';
  241. if ($action == 'edit') {
  242. print '<input class="flat" name="position" size="5" value="'.(GETPOSTISSET("position") ? GETPOST("position", 'int') : $object->position).'">';
  243. } else {
  244. print $object->position;
  245. }
  246. print '</td></tr>';
  247. // Date creation
  248. print '<tr><td>'.$langs->trans("DateCreation").'</td><td>'.dol_print_date($object->datec, 'dayhour').'</td></tr>';
  249. print '</table>';
  250. print '</div>';
  251. print dol_get_fiche_end();
  252. if ($action == 'edit') {
  253. print $form->buttonsSaveCancel();
  254. print '</form>';
  255. }
  256. // Buttons
  257. print "<div class=\"tabsAction\">\n";
  258. // Edit
  259. if ($permissiontoadd && $action != 'edit') {
  260. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=edit&token='.newToken().'">'.$langs->trans("Edit").'</a>'."\n";
  261. }
  262. // Remove
  263. if ($permissiontodelete && $action != 'edit') {
  264. print '<a class="butActionDelete" href="list.php?id='.$object->id.'&action=delete&token='.newToken().'">'.$langs->trans("Delete").'</a>'."\n";
  265. }
  266. print '</div>';
  267. }
  268. // End of page
  269. llxFooter();
  270. $db->close();