card.php 13 KB

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