new_promoted_message.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $cidReset = true;
  4. require_once __DIR__.'/../inc/global.inc.php';
  5. api_protect_admin_script();
  6. if (api_get_setting('allow_social_tool') !== 'true') {
  7. api_not_allowed(true);
  8. }
  9. $logInfo = [
  10. 'tool' => 'Messages',
  11. 'action' => 'add_new_promoted_message',
  12. ];
  13. Event::registerLog($logInfo);
  14. $allowSocial = api_get_setting('allow_social_tool') === 'true';
  15. $nameTools = api_xml_http_response_encode(get_lang('Messages'));
  16. $htmlHeadXtra[] = '<script>
  17. var counter_image = 1;
  18. function add_image_form() {
  19. // Multiple filepaths for image form
  20. var filepaths = document.getElementById("file_uploads");
  21. if (document.getElementById("filepath_"+counter_image)) {
  22. counter_image = counter_image + 1;
  23. } else {
  24. counter_image = counter_image;
  25. }
  26. var elem1 = document.createElement("div");
  27. elem1.setAttribute("id","filepath_"+counter_image);
  28. filepaths.appendChild(elem1);
  29. id_elem1 = "filepath_"+counter_image;
  30. id_elem1 = "\'"+id_elem1+"\'";
  31. document.getElementById("filepath_"+counter_image).innerHTML = "<div class=\"form-group\" ><label class=\"col-sm-4\">'.get_lang('FilesAttachment').'</label><input class=\"col-sm-8\" type=\"file\" name=\"attach_"+counter_image+"\" /></div><div class=\"form-group\" ><label class=\"col-sm-4\">'.get_lang('Description').'</label><div class=\"col-sm-8\"><input style=\"width:100%\" type=\"text\" name=\"legend[]\" /></div></div>";
  32. if (filepaths.childNodes.length == 6) {
  33. var link_attach = document.getElementById("link-more-attach");
  34. if (link_attach) {
  35. link_attach.innerHTML="";
  36. }
  37. }
  38. }
  39. </script>';
  40. $nameTools = get_lang('ComposeMessage');
  41. $tpl = new Template(get_lang('ComposeMessage'));
  42. /**
  43. * Shows the compose area + a list of users to select from.
  44. */
  45. function show_compose_to_any($tpl)
  46. {
  47. $default['user_list'] = 0;
  48. $html = manageForm($default, null, null, $tpl);
  49. return $html;
  50. }
  51. function show_compose_reply_to_message($message_id, $receiver_id, $tpl)
  52. {
  53. $table = Database::get_main_table(TABLE_MESSAGE);
  54. $receiver_id = (int) $receiver_id;
  55. $message_id = (int) $message_id;
  56. $query = "SELECT user_sender_id
  57. FROM $table
  58. WHERE user_receiver_id = ".$receiver_id." AND id = ".$message_id;
  59. $result = Database::query($query);
  60. $row = Database::fetch_array($result, 'ASSOC');
  61. $userInfo = api_get_user_info($row['user_sender_id']);
  62. if (empty($row['user_sender_id']) || empty($userInfo)) {
  63. $html = get_lang('InvalidMessageId');
  64. return $html;
  65. }
  66. $default['users'] = [$row['user_sender_id']];
  67. $html = manageForm($default, null, $userInfo['complete_name_with_username'], $tpl);
  68. return $html;
  69. }
  70. function show_compose_to_user($receiver_id, $tpl)
  71. {
  72. $userInfo = api_get_user_info($receiver_id);
  73. $html = get_lang('To').':&nbsp;<strong>'.$userInfo['complete_name'].'</strong>';
  74. $default['title'] = api_xml_http_response_encode(get_lang('EnterTitle'));
  75. $default['users'] = [$receiver_id];
  76. $html .= manageForm($default, null, '', $tpl);
  77. return $html;
  78. }
  79. /**
  80. * @param $default
  81. * @param null $select_from_user_list
  82. * @param string $sent_to
  83. * @param Template $tpl
  84. *
  85. * @return string
  86. */
  87. function manageForm($default, $select_from_user_list = null, $sent_to = '', $tpl = null)
  88. {
  89. $form = new FormValidator(
  90. 'compose_message',
  91. null,
  92. api_get_self(),
  93. null,
  94. ['enctype' => 'multipart/form-data']
  95. );
  96. $form->addText('title', get_lang('Subject'));
  97. $form->addHtmlEditor(
  98. 'content',
  99. get_lang('Message'),
  100. false,
  101. false,
  102. ['ToolbarSet' => 'Messages', 'Width' => '100%', 'Height' => '250', 'style' => true]
  103. );
  104. $form->addLabel(
  105. '',
  106. '<div id="file_uploads"><div id="filepath_1">
  107. <div id="filepaths" class="form-horizontal">
  108. <div id="paths-file" class="form-group">
  109. <label class="col-sm-4">'.get_lang('FilesAttachment').'</label>
  110. <input class="col-sm-8" type="file" name="attach_1"/>
  111. </div>
  112. </div>
  113. <div id="paths-description" class="form-group">
  114. <label class="col-sm-4">'.get_lang('Description').'</label>
  115. <div class="col-sm-8">
  116. <input id="file-descrtiption" class="form-control" type="text" name="legend[]" />
  117. </div>
  118. </div>
  119. </div>
  120. </div>'
  121. );
  122. $form->addLabel(
  123. '',
  124. '<span id="link-more-attach"><a class="btn btn-default" href="javascript://" onclick="return add_image_form()">'.
  125. get_lang('AddOneMoreFile').'</a></span>&nbsp;('.
  126. sprintf(
  127. get_lang('MaximunFileSizeX'),
  128. format_file_size(api_get_setting('message_max_upload_filesize'))
  129. ).')'
  130. );
  131. $form->addButtonSend(get_lang('SendMessage'), 'compose');
  132. $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>');
  133. $form->setDefaults($default);
  134. $html = '';
  135. if ($form->validate()) {
  136. $check = true;
  137. if ($check) {
  138. $file_comments = $_POST['legend'];
  139. $title = $default['title'];
  140. $content = $default['content'];
  141. $res = MessageManager::send_message(
  142. api_get_user_id(),
  143. $title,
  144. $content,
  145. $_FILES,
  146. $file_comments,
  147. 0,
  148. 0,
  149. 0,
  150. 0,
  151. null,
  152. false,
  153. 0,
  154. [],
  155. true,
  156. null,
  157. MESSAGE_STATUS_PROMOTED
  158. );
  159. if ($res) {
  160. Display::addFlash(Display::return_message(
  161. get_lang('MessageSent'),
  162. 'confirmation',
  163. false
  164. ));
  165. }
  166. MessageManager::cleanAudioMessage();
  167. }
  168. Security::clear_token();
  169. header('Location: '.api_get_path(WEB_PATH).'main/social/promoted_messages.php');
  170. exit;
  171. } else {
  172. $token = Security::get_token();
  173. $form->addElement('hidden', 'sec_token');
  174. $form->setConstants(['sec_token' => $token]);
  175. $html .= $form->returnForm();
  176. }
  177. return $html;
  178. }
  179. $this_section = SECTION_SOCIAL;
  180. $interbreadcrumb[] = [
  181. 'url' => api_get_path(WEB_PATH).'main/social/home.php',
  182. 'name' => get_lang('SocialNetwork'),
  183. ];
  184. $interbreadcrumb[] = [
  185. 'url' => api_get_path(WEB_PATH).'main/messages/inbox.php',
  186. 'name' => get_lang('Messages'),
  187. ];
  188. $social_right_content = null;
  189. // LEFT COLUMN
  190. $social_left_content = '';
  191. // Block Social Menu
  192. $social_menu_block = SocialManager::show_social_menu('messages');
  193. $social_right_content .= '<div class="row">';
  194. $social_right_content .= '<div class="col-md-12">';
  195. $social_right_content .= '<div class="actions">';
  196. $social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.
  197. Display::return_icon('back.png', get_lang('Back'), [], 32).'</a>';
  198. $social_right_content .= '</div>';
  199. $social_right_content .= '</div>';
  200. $social_right_content .= '<div class="col-md-12">';
  201. // MAIN CONTENT
  202. if (!isset($_POST['compose'])) {
  203. if (isset($_GET['re_id'])) {
  204. $social_right_content .= show_compose_reply_to_message(
  205. $_GET['re_id'],
  206. api_get_user_id(),
  207. $tpl
  208. );
  209. } elseif (isset($_GET['send_to_user'])) {
  210. $social_right_content .= show_compose_to_user($_GET['send_to_user'], $tpl);
  211. } else {
  212. $social_right_content .= show_compose_to_any($tpl);
  213. }
  214. } else {
  215. $default['title'] = $_POST['title'];
  216. $default['content'] = $_POST['content'];
  217. $social_right_content .= manageForm($default, null, null, $tpl);
  218. }
  219. $social_right_content .= '</div>';
  220. $social_right_content .= '</div>';
  221. // Block Social Avatar
  222. SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'messages');
  223. MessageManager::cleanAudioMessage();
  224. $tpl->assign('social_menu_block', $social_menu_block);
  225. $tpl->assign('social_right_content', $social_right_content);
  226. $social_layout = $tpl->get_template('social/inbox.tpl');
  227. $tpl->display($social_layout);