feedback.admin.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * @file
  4. * Administrative functionality for Feedback module.
  5. */
  6. /**
  7. * Build a (sortable) form containing a checkbox for each feedback entry.
  8. */
  9. function feedback_admin_view_form($form, &$form_state) {
  10. $form['#attached']['js'][] = drupal_get_path('module', 'feedback') . '/feedback.admin.js';
  11. $status_headings = array(
  12. FEEDBACK_OPEN => t('Open feedback messages'),
  13. FEEDBACK_PROCESSED => t('Processed feedback messages'),
  14. );
  15. $form['#feedback_header'] = array(
  16. array(),
  17. array('data' => t('Location'), 'field' => 'f.location_masked', 'sort' => 'asc'),
  18. array('data' => t('Date'), 'field' => 'f.timestamp'),
  19. array('data' => t('User'), 'field' => 'u.name'),
  20. t('Message'),
  21. t('Operations'),
  22. );
  23. // Hack to prevent pager_query() from issuing PHP notices.
  24. if (!isset($_GET['page'])) {
  25. $_GET['page'] = '';
  26. }
  27. if (count(explode(',', $_GET['page'])) < 2) {
  28. $_GET['page'] .= ',0';
  29. }
  30. $form['feedback-messages'] = array('#tree' => TRUE);
  31. $query = db_select('feedback', 'f')->extend('PagerDefault')->extend('TableSort');
  32. $query->join('users', 'u', 'f.uid = u.uid');
  33. $query->fields('f')
  34. ->limit(50);
  35. foreach (array(FEEDBACK_OPEN, FEEDBACK_PROCESSED) as $status) {
  36. $status_query = clone $query;
  37. $fids = $status_query->element($status)
  38. ->condition('f.status', $status)
  39. ->execute()->fetchCol();
  40. $form['feedback-messages'][$status] = array(
  41. '#type' => 'fieldset',
  42. '#title' => $status_headings[$status],
  43. '#collapsible' => TRUE,
  44. '#collapsed' => $status,
  45. '#attributes' => array('class' => array('feedback-messages')),
  46. );
  47. if (!empty($fids)) {
  48. $entries = feedback_load_multiple($fids);
  49. foreach ($entries as $fid => $entry) {
  50. $form['feedback-messages'][$status][$fid] = array(
  51. '#type' => 'checkbox',
  52. '#return_value' => FEEDBACK_PROCESSED,
  53. '#default_value' => FALSE,
  54. );
  55. $form['feedback-messages'][$status][$fid]['location'] = array('#markup' => l(truncate_utf8($entry->location, 32, FALSE, TRUE), $entry->url));
  56. $form['feedback-messages'][$status][$fid]['date'] = array('#markup' => format_date($entry->timestamp, 'small'));
  57. $form['feedback-messages'][$status][$fid]['user'] = array('#markup' => check_plain(format_username($entry)));
  58. $form['feedback-messages'][$status][$fid]['message'] = feedback_build_content($entry, 'teaser');
  59. $form['feedback-messages'][$status][$fid]['operations'] = array(
  60. '#theme' => 'links',
  61. '#links' => array(
  62. 'edit' => array(
  63. 'title' => t('edit'),
  64. 'href' => "admin/reports/feedback/$fid/edit"
  65. ),
  66. 'delete' => array(
  67. 'title' => t('delete'),
  68. 'href' => "admin/reports/feedback/$fid/delete"
  69. ),
  70. ),
  71. '#attributes' => array(),
  72. );
  73. }
  74. }
  75. }
  76. $form['submit'] = array(
  77. '#type' => 'submit',
  78. '#value' => t('Update'),
  79. // Hide the submit button, if there are no entries at all.
  80. '#access' => !empty($entries),
  81. );
  82. return $form;
  83. }
  84. /**
  85. * Output a sortable table containing all feedback entries.
  86. */
  87. function theme_feedback_admin_view_form($variables) {
  88. $form = $variables['form'];
  89. $output = '';
  90. foreach (element_children($form['feedback-messages']) as $status) {
  91. $item = &$form['feedback-messages'][$status];
  92. if (!isset($item['#type']) || $item['#type'] != 'fieldset') {
  93. continue;
  94. }
  95. // Build the table.
  96. $rows = array();
  97. foreach (element_children($item) as $element_entry) {
  98. $entry = &$item[$element_entry];
  99. // Render the data first.
  100. $rows[] = array(
  101. 0,
  102. drupal_render($entry['location']),
  103. drupal_render($entry['date']),
  104. drupal_render($entry['user']),
  105. drupal_render($entry['message']),
  106. drupal_render($entry['operations']),
  107. );
  108. // Render the checkbox.
  109. $rows[count($rows) - 1][0] = drupal_render($entry);
  110. }
  111. if (empty($rows)) {
  112. $rows[] = array(array('data' => t('No feedback entries available.'), 'colspan' => 6));
  113. }
  114. // Inject the table.
  115. $item['messages'] = array(
  116. '#markup' => theme('table', array('header' => $form['#feedback_header'], 'rows' => $rows)),
  117. '#suffix' => theme('pager', array('element' => $status)),
  118. '#weight' => -1,
  119. );
  120. // Render the fieldset.
  121. $output .= drupal_render($item);
  122. }
  123. // Render internal FAPI and potential extra form elements.
  124. $output .= drupal_render_children($form);
  125. return $output;
  126. }
  127. /**
  128. * Form submit callback for admin view form.
  129. */
  130. function feedback_admin_view_form_submit($form, &$form_state) {
  131. $update = array();
  132. // Determine feedback entries to update.
  133. foreach ($form_state['values']['feedback-messages'] as $status => $values) {
  134. $values = array_filter($values);
  135. if (!empty($values)) {
  136. $entries = feedback_load_multiple(array_keys($values));
  137. foreach ($entries as $fid => $entry) {
  138. $entry->status = ($status == FEEDBACK_OPEN ? FEEDBACK_PROCESSED : FEEDBACK_OPEN);
  139. feedback_save($entry);
  140. }
  141. }
  142. }
  143. }
  144. /**
  145. * Form builder; Feedback entry edit form.
  146. *
  147. * @ingroup forms
  148. */
  149. function feedback_entry_form($form, &$form_state, $entry) {
  150. $form['#fid'] = $entry->fid;
  151. $form['location'] = array(
  152. '#type' => 'textfield',
  153. '#title' => t('Location'),
  154. '#required' => TRUE,
  155. '#default_value' => $entry->location,
  156. );
  157. $account = user_load($entry->uid);
  158. $form['user'] = array(
  159. '#title' => t('User'),
  160. '#type' => 'item',
  161. '#markup' => theme('username', array('account' => $account)),
  162. );
  163. $form['status'] = array(
  164. '#title' => t('Processed'),
  165. '#type' => 'radios',
  166. '#options' => array(
  167. FEEDBACK_OPEN => 'Open',
  168. FEEDBACK_PROCESSED => 'Processed',
  169. ),
  170. '#default_value' => $entry->status,
  171. );
  172. $form['message'] = array(
  173. '#type' => 'textarea',
  174. '#title' => t('Message'),
  175. '#required' => TRUE,
  176. '#wysiwyg' => FALSE,
  177. '#default_value' => $entry->message,
  178. );
  179. field_attach_form('feedback', $entry, $form, $form_state);
  180. $form['actions'] = array(
  181. '#type' => 'actions',
  182. );
  183. $form['actions']['submit'] = array(
  184. '#type' => 'submit',
  185. '#value' => t('Submit'),
  186. );
  187. $form['actions']['delete'] = array(
  188. '#type' => 'submit',
  189. '#value' => t('Delete'),
  190. '#submit' => array('feedback_entry_form_delete_submit'),
  191. );
  192. return $form;
  193. }
  194. /**
  195. * Form submit callback for entry edit form.
  196. *
  197. * @todo Duplicates feedback_form_submit(). Add all default entity properties
  198. * as #type 'value' to feedback_form() and merge the two submit handlers.
  199. */
  200. function feedback_entry_form_submit(&$form, &$form_state) {
  201. $entry = feedback_load($form['#fid']);
  202. entity_form_submit_build_entity('feedback', $entry, $form, $form_state);
  203. $entry->message = $form_state['values']['message'];
  204. $entry->location = $form_state['values']['location'];
  205. $entry->location_masked = feedback_mask_path($entry->location);
  206. $entry->url = url($entry->location, array('absolute' => TRUE));
  207. $entry->status = $form_state['values']['status'];
  208. feedback_save($entry);
  209. drupal_set_message(t('The entry has been updated.'));
  210. }
  211. /**
  212. * Button submit function: handle the 'Delete' button on the feedback entry edit form.
  213. */
  214. function feedback_entry_form_delete_submit($form, &$form_state) {
  215. $destination = array();
  216. if (isset($_GET['destination'])) {
  217. $destination = drupal_get_destination();
  218. unset($_GET['destination']);
  219. }
  220. $fid = $form['#fid'];
  221. $form_state['redirect'] = array('admin/reports/feedback/' . $fid . '/delete', array('query' => $destination));
  222. }
  223. /**
  224. * Form builder; The general feedback settings form.
  225. *
  226. * @ingroup forms
  227. */
  228. function feedback_admin_settings_form($form, &$form_state) {
  229. $form['feedback_excluded_paths'] = array(
  230. '#type' => 'textarea',
  231. '#title' => t('Paths to exclude from feedback display'),
  232. '#default_value' => variable_get('feedback_excluded_paths', 'admin/reports/feedback'),
  233. '#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
  234. );
  235. return system_settings_form($form);
  236. }
  237. /**
  238. * Generate a render array for viewing a feedback entry.
  239. *
  240. * @param $entry
  241. * A feedback entry object.
  242. * @param $view_mode
  243. * View mode, e.g. 'full', 'teaser'...
  244. * @param $langcode
  245. * (optional) A language code to use for rendering. Defaults to the global
  246. * content language of the current request.
  247. *
  248. * @return
  249. * An array as expected by drupal_render().
  250. *
  251. * @todo This is an API function; move into feedback.module.
  252. */
  253. function feedback_view($entry, $view_mode = 'full', $langcode = NULL) {
  254. if (!isset($langcode)) {
  255. $langcode = $GLOBALS['language_content']->language;
  256. }
  257. // Populate $entry->content with a render() array.
  258. feedback_build_content($entry, $view_mode, $langcode);
  259. $build = $entry->content;
  260. unset($entry->content);
  261. $build += array(
  262. '#theme' => 'feedback_entry',
  263. '#feedback' => $entry,
  264. '#view_mode' => $view_mode,
  265. '#language' => $langcode,
  266. );
  267. // Allow modules to modify the structured entry.
  268. $type = 'feedback';
  269. drupal_alter(array('feedback_view', 'entity_view'), $build, $type);
  270. return $build;
  271. }
  272. /**
  273. * Builds a structured array representing the feedback entry's content.
  274. *
  275. * @param $entry
  276. * A feedback entry object.
  277. * @param $view_mode
  278. * View mode, e.g. 'full', 'teaser'...
  279. * @param $langcode
  280. * (optional) A language code to use for rendering. Defaults to the global
  281. * content language of the current request.
  282. *
  283. * @todo This is an API function; move into feedback.module.
  284. */
  285. function feedback_build_content($entry, $view_mode = 'full', $langcode = NULL) {
  286. if (!isset($langcode)) {
  287. $langcode = $GLOBALS['language_content']->language;
  288. }
  289. // Remove previously built content, if exists.
  290. $entry->content = array();
  291. // Allow modules to change the view mode.
  292. $context = array(
  293. 'entity_type' => 'feedback',
  294. 'entity' => $entry,
  295. 'langcode' => $langcode,
  296. );
  297. drupal_alter('entity_view_mode', $view_mode, $context);
  298. $entry->content['message'] = array(
  299. '#markup' => check_plain($entry->message),
  300. );
  301. if (!empty($entry->useragent)) {
  302. $entry->content['browser'] = array(
  303. '#theme' => 'container',
  304. '#attributes' => array('class' => array('browserinfo', 'description')),
  305. );
  306. if (module_exists('browscap')) {
  307. $browserinfo = browscap_get_browser($entry->useragent);
  308. // Browscap returns useragent but not always parent info.
  309. $browser = (isset($browserinfo['parent']) ? $browserinfo['parent'] . ' / ' . $browserinfo['platform'] : $browserinfo['useragent']);
  310. $entry->content['browser']['#markup'] = check_plain($browser);
  311. }
  312. else {
  313. $entry->content['browser']['#markup'] = check_plain($entry->useragent);
  314. }
  315. }
  316. // Build fields content.
  317. field_attach_prepare_view('feedback', array($entry->fid => $entry), $view_mode, $langcode);
  318. entity_prepare_view('feedback', array($entry->fid => $entry), $langcode);
  319. $entry->content += field_attach_view('feedback', $entry, $view_mode, $langcode);
  320. $entry->content['links'] = array(
  321. '#theme' => 'links__feedback',
  322. '#pre_render' => array('drupal_pre_render_links'),
  323. '#attributes' => array('class' => array('links', 'inline')),
  324. );
  325. $uri = entity_uri('feedback', $entry);
  326. if ($uri['path'] != $_GET['q'] && arg(0) != 'admin') {
  327. $entry->content['links']['#links']['view'] = array('title' => t('view'), 'href' => $uri['path']);
  328. }
  329. // Allow modules to make their own additions to the entry.
  330. module_invoke_all('feedback_view', $entry, $view_mode, $langcode);
  331. module_invoke_all('entity_view', $entry, 'feedback', $view_mode, $langcode);
  332. }
  333. /**
  334. * Process variables for feedback-entry.tpl.php.
  335. *
  336. * The $variables array contains the following arguments:
  337. * - $entry
  338. *
  339. * @see feedback-entry.tpl.php
  340. */
  341. function template_preprocess_feedback_entry(&$variables) {
  342. foreach (element_children($variables['elements']) as $key) {
  343. $variables['content'][$key] = $variables['elements'][$key];
  344. }
  345. $entry = $variables['elements']['#feedback'];
  346. // Preprocess fields.
  347. field_attach_preprocess('feedback', $entry, $variables['elements'], $variables);
  348. $variables['location'] = l($entry->location, $entry->url);
  349. $variables['date'] = format_date($entry->timestamp, 'small');
  350. $variables['account'] = check_plain(format_username($entry));
  351. }
  352. /**
  353. * Form constructor for the feedback delete confirmation form.
  354. *
  355. * @param $entry
  356. * The feedback entry to delete.
  357. *
  358. * @see feedback_delete_confirm_submit()
  359. * @ingroup forms
  360. */
  361. function feedback_delete_confirm($form, &$form_state, $entry) {
  362. $form['fid'] = array('#type' => 'value', '#value' => $entry->fid);
  363. $output = confirm_form($form,
  364. t('Are you sure you want to delete the feedback entry?'),
  365. 'admin/reports/feedback',
  366. NULL,
  367. t('Delete'));
  368. return $output;
  369. }
  370. /**
  371. * Process feedback_delete_confirm() form submissions.
  372. */
  373. function feedback_delete_confirm_submit($form, &$form_state) {
  374. feedback_delete($form_state['values']['fid']);
  375. drupal_set_message(t('The feedback entry was deleted.'));
  376. $form_state['redirect'] = 'admin/reports/feedback';
  377. }