redis_logger.admin.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. use Redis\Logger\Reader;
  3. /**
  4. * @file
  5. * Administrative controllers for the Redis Logger module.
  6. */
  7. function redis_logger_form_overview($form, $form_state) {
  8. $redis = Redis_Client::getClient();
  9. $reader = new Reader($redis);
  10. $severity_levels = $reader->getSettings()->getSeverityLevels();
  11. $base_form = $form;
  12. $form['redis_logger_filter_form'] = _redis_logger_filter_form($base_form, $reader);
  13. $form['redis_logger_clear_log_form'] = _redis_logger_clear_log_form($base_form);
  14. $header = array(
  15. t('Type'),
  16. t('Level'),
  17. t('Message template'),
  18. t('Occurrences'),
  19. );
  20. $templates = $reader->getTemplates();
  21. dsm($templates);
  22. $regex = $reader->getSettings()->getSplittingRegex();
  23. $rows = array();
  24. foreach ($templates as $template) {
  25. preg_match($regex, $template, $matches);
  26. list(, $channel, $severity, $template) = $matches;
  27. $occurrences = l(t('Occurrences'), 'admin/reports/redis/logger/' . $template);
  28. $rows[] = array(
  29. $channel,
  30. $severity_levels[$severity],
  31. $template,
  32. $occurrences,
  33. );
  34. }
  35. $form['redis_logger_table'] = array(
  36. '#theme' => 'table',
  37. '#header' => $header,
  38. '#rows' => $rows,
  39. '#attributes' => array('id' => 'admin-dblog'),
  40. '#empty' => t('No log messages available.'),
  41. );
  42. $build['dblog_pager'] = array('#theme' => 'pager');
  43. return $form;
  44. }
  45. /**
  46. * TODO implement. Beware of XSS.
  47. *
  48. * @param string $template
  49. *
  50. * @return string
  51. */
  52. function redis_logger_page_event($template) {
  53. return check_plain($template);
  54. }
  55. /**
  56. * Form constructor for the form that clears out the log.
  57. *
  58. * TODO implement.
  59. *
  60. * @see dblog_clear_log_submit()
  61. * @ingroup forms
  62. */
  63. function _redis_logger_clear_log_form($form) {
  64. $form['redis_logger_clear'] = array(
  65. '#type' => 'fieldset',
  66. '#title' => t('Clear log messages'),
  67. '#description' => t('NOT IMPLEMENTED YET. This will permanently remove the log messages from the Redis store.'),
  68. '#collapsible' => TRUE,
  69. '#collapsed' => TRUE,
  70. );
  71. $form['redis_logger_clear']['clear'] = array(
  72. '#type' => 'submit',
  73. '#value' => t('Clear log messages'),
  74. '#submit' => array('redis_logger_clear_log_submit'),
  75. );
  76. return $form;
  77. }
  78. /**
  79. * Helper for the Redis logger filter form builder.
  80. *
  81. * @param \Redis\Logger\Reader $reader
  82. *
  83. * @return array
  84. */
  85. function _redis_logger_filters(Reader $reader) {
  86. $filters = array();
  87. $filters['type'] = array(
  88. 'title' => t('Type'),
  89. 'options' => $reader->getChannels(),
  90. );
  91. $filters['severity'] = array(
  92. 'title' => t('Severity'),
  93. 'options' => $reader->getSettings()->getSeverityLevels(),
  94. );
  95. return $filters;
  96. }
  97. /**
  98. * Build the filter form. Shares session status with DBLog.
  99. *
  100. * @param array $form
  101. * @param \Redis\Logger\Reader $reader
  102. *
  103. * @return mixed
  104. */
  105. function _redis_logger_filter_form($form, Reader $reader) {
  106. $filters = _redis_logger_filters($reader);
  107. $form['filters'] = array(
  108. '#type' => 'fieldset',
  109. '#title' => t('Filter log messages'),
  110. '#collapsible' => TRUE,
  111. '#collapsed' => empty($_SESSION['dblog_overview_filter']),
  112. );
  113. foreach ($filters as $key => $filter) {
  114. $form['filters']['status'][$key] = array(
  115. '#title' => $filter['title'],
  116. '#type' => 'select',
  117. '#multiple' => TRUE,
  118. '#size' => 8,
  119. '#options' => $filter['options'],
  120. );
  121. if (!empty($_SESSION['dblog_overview_filter'][$key])) {
  122. $form['filters']['status'][$key]['#default_value'] = $_SESSION['dblog_overview_filter'][$key];
  123. }
  124. }
  125. $form['filters']['actions'] = array(
  126. '#type' => 'actions',
  127. '#attributes' => array('class' => array('container-inline')),
  128. );
  129. $form['filters']['actions']['submit'] = array(
  130. '#type' => 'submit',
  131. '#value' => t('Filter'),
  132. );
  133. if (!empty($_SESSION['dblog_overview_filter'])) {
  134. $form['filters']['actions']['reset'] = array(
  135. '#type' => 'submit',
  136. '#value' => t('Reset')
  137. );
  138. }
  139. return $form;
  140. }