card.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. /* Copyright (C) 2013-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Marcos García <marcosgdf@gmail.com>
  4. * Copyright (C) 2018-2023 Frédéric France <frederic.france@netlogic.fr>
  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/opensurvey/card.php
  21. * \ingroup opensurvey
  22. * \brief Page to edit survey
  23. */
  24. // Load Dolibarr environment
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT."/core/class/doleditor.class.php";
  27. require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
  28. require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
  29. require_once DOL_DOCUMENT_ROOT."/opensurvey/class/opensurveysondage.class.php";
  30. require_once DOL_DOCUMENT_ROOT."/opensurvey/lib/opensurvey.lib.php";
  31. // Security check
  32. if (!$user->hasRight('opensurvey', 'read')) {
  33. accessforbidden();
  34. }
  35. // Initialize Variables
  36. $action = GETPOST('action', 'aZ09');
  37. $cancel = GETPOST('cancel', 'alpha');
  38. $numsondage = '';
  39. if (GETPOST('id')) {
  40. $numsondage = (string) GETPOST('id', 'alpha');
  41. }
  42. // Initialize objects
  43. $object = new Opensurveysondage($db);
  44. $result = $object->fetch(0, $numsondage);
  45. if ($result <= 0) {
  46. dol_print_error($db, $object->error);
  47. exit;
  48. }
  49. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  50. $hookmanager->initHooks(array('surveycard', 'globalcard'));
  51. $expiredate = dol_mktime(0, 0, 0, GETPOST('expiremonth'), GETPOST('expireday'), GETPOST('expireyear'));
  52. $permissiontoread = $user->rights->opensurvey->read;
  53. $permissiontoadd = $user->rights->opensurvey->write;
  54. // permission delete doesn't exists
  55. $permissiontodelete = $user->rights->opensurvey->write;
  56. /*
  57. * Actions
  58. */
  59. $parameters = array('id' => $numsondage);
  60. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  61. if ($reshook < 0) {
  62. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  63. }
  64. if (empty($reshook)) {
  65. if ($cancel) {
  66. $action = '';
  67. }
  68. // Delete
  69. if ($action == 'delete_confirm') {
  70. // Security check
  71. if (!$user->hasRight('opensurvey', 'write')) {
  72. accessforbidden();
  73. }
  74. $result = $object->delete($user, '', $numsondage);
  75. header('Location: '.dol_buildpath('/opensurvey/list.php', 1));
  76. exit();
  77. }
  78. // Close
  79. if ($action == 'close') {
  80. $object->status = Opensurveysondage::STATUS_CLOSED;
  81. $object->update($user);
  82. }
  83. // Valid or Reopend
  84. if ($action == 'reopen' || $action == 'validate') {
  85. $object->status = Opensurveysondage::STATUS_VALIDATED;
  86. $object->update($user);
  87. }
  88. // Update
  89. if ($action == 'update') {
  90. // Security check
  91. if (!$user->hasRight('opensurvey', 'write')) {
  92. accessforbidden();
  93. }
  94. $error = 0;
  95. if (!GETPOST('nouveautitre')) {
  96. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Title")), null, 'errors');
  97. $error++;
  98. $action = 'edit';
  99. }
  100. if (!$error) {
  101. $object->title = (string) GETPOST('nouveautitre', 'alphanohtml');
  102. $object->description = (string) GETPOST('nouveauxcommentaires', 'restricthtml');
  103. $object->mail_admin = (string) GETPOST('nouvelleadresse', 'alpha');
  104. $object->date_fin = $expiredate;
  105. $object->allow_comments = GETPOST('cancomment', 'aZ09') == 'on' ? 1 : 0;
  106. $object->allow_spy = GETPOST('canseeothersvote', 'aZ09') == 'on' ? 1 : 0;
  107. $object->mailsonde = GETPOST('mailsonde', 'aZ09') == 'on' ? 1 : 0;
  108. $res = $object->update($user);
  109. if ($res < 0) {
  110. setEventMessages($object->error, $object->errors, 'errors');
  111. $action = 'edit';
  112. }
  113. }
  114. }
  115. // Add comment
  116. if (GETPOST('ajoutcomment')) {
  117. $error = 0;
  118. if (!GETPOST('comment', "alphanohtml")) {
  119. $error++;
  120. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Comment")), null, 'errors');
  121. }
  122. if (!GETPOST('commentuser', "alphanohtml")) {
  123. $error++;
  124. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("User")), null, 'errors');
  125. }
  126. if (!$error) {
  127. $comment = (string) GETPOST("comment", "alphanohtml");
  128. $comment_user = (string) GETPOST('commentuser', "alphanohtml");
  129. $resql = $object->addComment($comment, $comment_user);
  130. if (!$resql) {
  131. setEventMessages($langs->trans('ErrorInsertingComment'), null, 'errors');
  132. }
  133. }
  134. }
  135. // Delete comment
  136. if ($action == 'deletecomment') {
  137. $idcomment = GETPOST('idcomment', 'int');
  138. if ($idcomment > 0) {
  139. // Security check
  140. if (!$user->hasRight('opensurvey', 'write')) {
  141. accessforbidden();
  142. }
  143. $resql = $object->deleteComment($idcomment);
  144. }
  145. }
  146. if ($action == 'edit') {
  147. // Security check
  148. if (!$user->hasRight('opensurvey', 'write')) {
  149. accessforbidden();
  150. }
  151. }
  152. }
  153. /*
  154. * View
  155. */
  156. $form = new Form($db);
  157. if ($object->fk_user_creat) {
  158. $userstatic = new User($db);
  159. $userstatic->fetch($object->fk_user_creat);
  160. }
  161. $title = $object->title." - ".$langs->trans('Card');
  162. $helpurl = '';
  163. $arrayofjs = array();
  164. $arrayofcss = array('/opensurvey/css/style.css');
  165. llxHeader('', $title, $helpurl, 0, 0, 0, $arrayofjs, $arrayofcss);
  166. // Define format of choices
  167. $toutsujet = explode(",", $object->sujet);
  168. $listofanswers = array();
  169. foreach ($toutsujet as $value) {
  170. $tmp = explode('@', $value);
  171. $listofanswers[] = array('label'=>$tmp[0], 'format'=>(!empty($tmp[1]) ? $tmp[1] : 'checkbox'));
  172. }
  173. $toutsujet = str_replace("@", "<br>", $toutsujet);
  174. $toutsujet = str_replace("°", "'", $toutsujet);
  175. print '<form name="updatesurvey" action="'.$_SERVER["PHP_SELF"].'?id='.$numsondage.'" method="POST">'."\n";
  176. print '<input type="hidden" name="token" value="'.newToken().'">';
  177. print '<input type="hidden" name="action" value="update">';
  178. $head = opensurvey_prepare_head($object);
  179. print dol_get_fiche_head($head, 'general', $langs->trans("Survey"), -1, 'poll');
  180. $morehtmlref = '';
  181. $linkback = '<a href="'.DOL_URL_ROOT.'/opensurvey/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  182. dol_banner_tab($object, 'id', $linkback, 1, 'id_sondage', 'id_sondage', $morehtmlref);
  183. print '<div class="fichecenter">';
  184. print '<div class="fichehalfleft">';
  185. print '<div class="underbanner clearboth"></div>';
  186. print '<table class="border tableforfield centpercent">';
  187. // Type
  188. $type = ($object->format == "A") ? 'classic' : 'date';
  189. print '<tr><td class="titlefieldmax45">'.$langs->trans("Type").'</td><td>';
  190. print img_picto('', dol_buildpath('/opensurvey/img/'.($type == 'classic' ? 'chart-32.png' : 'calendar-32.png'), 1), 'width="16"', 1);
  191. print ' '.$langs->trans($type == 'classic' ? "TypeClassic" : "TypeDate").'</td></tr>';
  192. // Title
  193. print '<tr><td>';
  194. $adresseadmin = $object->mail_admin;
  195. print $langs->trans("Title").'</td><td>';
  196. if ($action == 'edit') {
  197. print '<input type="text" name="nouveautitre" style="width: 95%" value="'.dol_escape_htmltag(dol_htmlentities($object->title)).'">';
  198. } else {
  199. print dol_htmlentities($object->title);
  200. }
  201. print '</td></tr>';
  202. // Description
  203. print '<tr><td class="tdtop">'.$langs->trans("Description").'</td><td class="wordbreak">';
  204. if ($action == 'edit') {
  205. $doleditor = new DolEditor('nouveauxcommentaires', $object->description, '', 120, 'dolibarr_notes', 'In', 1, 1, 1, ROWS_7, '90%');
  206. $doleditor->Create(0, '');
  207. } else {
  208. print(dol_textishtml($object->description) ? $object->description : dol_nl2br($object->description, 1, true));
  209. }
  210. print '</td></tr>';
  211. // Receive an email with each vote
  212. print '<tr><td>'.$langs->trans('ToReceiveEMailForEachVote').'</td><td>';
  213. if ($action == 'edit') {
  214. print '<input type="checkbox" name="mailsonde" '.($object->mailsonde ? 'checked="checked"' : '').'">';
  215. } else {
  216. print yn($object->mailsonde);
  217. //If option is active and linked user does not have an email, we show a warning
  218. if ($object->fk_user_creat && $object->mailsonde) {
  219. if (!$userstatic->email) {
  220. print ' '.img_warning($langs->trans('NoEMail'));
  221. }
  222. }
  223. }
  224. print '</td></tr>';
  225. // Users can comment
  226. print '<tr><td>'.$langs->trans('CanComment').'</td><td>';
  227. if ($action == 'edit') {
  228. print '<input type="checkbox" name="cancomment" '.($object->allow_comments ? 'checked="checked"' : '').'">';
  229. } else {
  230. print yn($object->allow_comments);
  231. }
  232. print '</td></tr>';
  233. // Users can see others vote
  234. print '<tr><td>'.$langs->trans('CanSeeOthersVote').'</td><td>';
  235. if ($action == 'edit') {
  236. print '<input type="checkbox" name="canseeothersvote" '.($object->allow_spy ? 'checked="checked"' : '').'">';
  237. } else {
  238. print yn($object->allow_spy);
  239. }
  240. print '</td></tr>';
  241. print '</table>';
  242. print '</div>';
  243. print '<div class="fichehalfright">';
  244. print '<div class="underbanner clearboth"></div>';
  245. print '<table class="border tableforfield centpercent">';
  246. // Expire date
  247. print '<tr><td>'.$langs->trans('ExpireDate').'</td><td>';
  248. if ($action == 'edit') {
  249. print $form->selectDate($expiredate ? $expiredate : $object->date_fin, 'expire', 0, 0, 0, '', 1, 0);
  250. } else {
  251. print dol_print_date($object->date_fin, 'day');
  252. if ($object->date_fin && $object->date_fin < dol_now() && $object->status == Opensurveysondage::STATUS_VALIDATED) {
  253. print img_warning($langs->trans("Expired"));
  254. }
  255. }
  256. print '</td></tr>';
  257. // Author
  258. print '<tr><td>';
  259. print $langs->trans("Author").'</td><td>';
  260. if ($object->fk_user_creat > 0) {
  261. print $userstatic->getLoginUrl(-1);
  262. } else {
  263. if ($action == 'edit') {
  264. print '<input type="text" name="nouvelleadresse" class="minwith200" value="'.$object->mail_admin.'">';
  265. } else {
  266. print dol_print_email($object->mail_admin, 0, 0, 1, 0, 1, 1);
  267. }
  268. }
  269. print '</td></tr>';
  270. // Link
  271. print '<tr><td>'.$langs->trans("UrlForSurvey", '').'</td><td>';
  272. // Define $urlwithroot
  273. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  274. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  275. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  276. $url = $urlwithroot.'/public/opensurvey/studs.php?sondage='.$object->id_sondage;
  277. print '<input type="text" class="quatrevingtpercent" '.($action == 'edit' ? 'disabled' : '').' id="opensurveyurl" name="opensurveyurl" value="'.$url.'">';
  278. if ($action != 'edit') {
  279. print ajax_autoselect("opensurveyurl", $url, 'image');
  280. }
  281. print '</td></tr>';
  282. // Other attributes
  283. $parameters = array();
  284. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
  285. print $hookmanager->resPrint;
  286. print '</table>';
  287. print '</div>';
  288. print '</div>';
  289. print '<div class="clearboth"></div>';
  290. print dol_get_fiche_end();
  291. if ($action == 'edit') {
  292. print $form->buttonsSaveCancel();
  293. }
  294. print '</form>'."\n";
  295. // Action bar
  296. print '<div class="tabsAction">';
  297. if ($action != 'edit' && $user->hasRight('opensurvey', 'write')) {
  298. // Modify button
  299. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().'&id='.urlencode($numsondage).'">'.$langs->trans("Modify").'</a>';
  300. if ($object->status == Opensurveysondage::STATUS_DRAFT) {
  301. // Validate button
  302. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=validate&token='.newToken().'&id='.urlencode($numsondage).'">'.$langs->trans("Valid").'</a>';
  303. }
  304. if ($object->status == Opensurveysondage::STATUS_VALIDATED) {
  305. // Close button
  306. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=close&token='.newToken().'&id='.urlencode($numsondage).'">'.$langs->trans("Close").'</a>';
  307. }
  308. if ($object->status == Opensurveysondage::STATUS_CLOSED) {
  309. // Re-Open
  310. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=reopen&token='.newToken().'&id='.urlencode($numsondage).'">'.$langs->trans("ReOpen").'</a>';
  311. }
  312. // Delete
  313. print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?suppressionsondage=1&id='.urlencode($numsondage).'&action=delete&token='.newToken(), 'delete', $permissiontodelete);
  314. }
  315. print '</div>';
  316. if ($action == 'delete') {
  317. print $form->formconfirm($_SERVER["PHP_SELF"].'?&id='.urlencode($numsondage), $langs->trans("RemovePoll"), $langs->trans("ConfirmRemovalOfPoll", $id), 'delete_confirm', '', '', 1);
  318. }
  319. print '<form name="formulaire5" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
  320. print '<input type="hidden" name="token" value="'.newToken().'">';
  321. print '<input type="hidden" name="action" value="addcomment">';
  322. print '<input type="hidden" name="id" value="'.urlencode($numsondage).'">';
  323. print '<input type="hidden" name="page_y" value="">';
  324. print load_fiche_titre($langs->trans("CommentsOfVoters"), '', '');
  325. // Comment list
  326. $comments = $object->getComments();
  327. if (!empty($comments)) {
  328. foreach ($comments as $comment) {
  329. if ($user->hasRight('opensurvey', 'write')) {
  330. print '<a class="reposition" href="'.DOL_URL_ROOT.'/opensurvey/card.php?action=deletecomment&token='.newToken().'&idcomment='.((int) $comment->id_comment).'&id='.urlencode($numsondage).'"> '.img_picto('', 'delete.png', '', false, 0, 0, '', '', 0).'</a> ';
  331. }
  332. print dol_htmlentities($comment->usercomment).': '.dol_nl2br(dol_htmlentities($comment->comment))." <br>";
  333. }
  334. } else {
  335. print '<span class="opacitymedium">'.$langs->trans("NoCommentYet").'</span><br>';
  336. }
  337. print '<br>';
  338. // Add comment
  339. if ($object->allow_comments) {
  340. print $langs->trans("AddACommentForPoll").'<br>';
  341. print '<textarea name="comment" rows="2" class="quatrevingtpercent"></textarea><br>'."\n";
  342. print $langs->trans("Name").': <input type="text" class="minwidth300" name="commentuser" value="'.dol_escape_htmltag($user->getFullName($langs)).'"> '."\n";
  343. print '<input type="submit" class="button reposition smallpaddingimp" name="ajoutcomment" value="'.dol_escape_htmltag($langs->trans("AddComment")).'"><br>'."\n";
  344. }
  345. print '</form>';
  346. // End of page
  347. llxFooter();
  348. $db->close();