[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/phpbb/event/ -> rst_exporter.php (source)

   1  <?php
   2  /**
   3   *
   4   * This file is part of the phpBB Forum Software package.
   5   *
   6   * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7   * @license GNU General Public License, version 2 (GPL-2.0)
   8   *
   9   * For full copyright and license information, please see
  10   * the docs/CREDITS.txt file.
  11   *
  12   */
  13  
  14  namespace phpbb\event;
  15  
  16  class rst_exporter
  17  {
  18      /** @var array Column keys */
  19      private $columns = [];
  20  
  21      /** @var array Column headers map */
  22      private $column_headers = [];
  23  
  24      /** @var array Maximum lengths of columns */
  25      private $max_lengths = [];
  26  
  27      /** @var string rst data */
  28      private $rst_data = '';
  29  
  30      /**
  31       * Set columns with array where key is column name and value is title of column in table
  32       *
  33       * @param array $column_data
  34       */
  35  	public function set_columns(array $column_data): void
  36      {
  37          foreach ($column_data as $column_key => $column_header)
  38          {
  39              $this->columns[] = $column_key;
  40              $this->column_headers[$column_key] = $column_header;
  41          }
  42      }
  43  
  44      /**
  45       * Add header to rst page
  46       *
  47       * @param string $type Type of header; allowed are h2, h3, h4 corresponding to HTML
  48       * @param string $header_text Text of header
  49       */
  50  	public function add_section_header(string $type, string $header_text): void
  51      {
  52          $this->rst_data .= $header_text . "\n";
  53  
  54          switch ($type)
  55          {
  56              case 'h2':
  57                  $header_character = '=';
  58              break;
  59  
  60              default:
  61              case 'h3':
  62                  $header_character = '-';
  63              break;
  64  
  65              case 'h4':
  66                  $header_character = '~';
  67              break;
  68          }
  69  
  70          $this->rst_data .= str_repeat($header_character, strlen($header_text)) . "\n\n";
  71      }
  72  
  73      /**
  74       * Fill table with event data
  75       *
  76       * @param array $event_data
  77       */
  78  	public function generate_events_table(array $event_data): void
  79      {
  80          $this->rst_data .= ".. table::\n";
  81          $this->rst_data .= "    :class: events-list\n\n";
  82  
  83          $this->set_max_lengths($event_data);
  84  
  85          // Create table header
  86          $this->rst_data .= $this->get_separator_line();
  87          $this->rst_data .= "    |";
  88          foreach ($this->columns as $column)
  89          {
  90              $this->rst_data .= $this->get_column($column, $this->column_headers[$column]);
  91          }
  92  
  93          $this->rst_data .= "\n" . $this->get_separator_line('=');
  94  
  95          foreach ($event_data as $event)
  96          {
  97              $event_data = [];
  98              $max_column_rows = 1;
  99              foreach ($event as $key => $value)
 100              {
 101                  $column_rows = !is_array($value) ? substr_count($value, '<br>') + 1 : 1;
 102                  $max_column_rows = max($max_column_rows, $column_rows);
 103                  $event_data[$key] = $column_rows > 1 ? explode('<br>', $value) : [is_array($value) ? implode(', ', $value) : $value];
 104              }
 105  
 106              for ($i = 0; $i < $max_column_rows; $i++)
 107              {
 108                  $this->rst_data .= '    |';
 109  
 110                  foreach ($this->columns as $column)
 111                  {
 112                      $this->rst_data .= $this->get_column($column, $event_data[$column][$i] ?? '');
 113                  }
 114                  $this->rst_data .= "\n";
 115              }
 116              $this->rst_data .= $this->get_separator_line();
 117          }
 118      }
 119  
 120      /**
 121       * Get rst output
 122       *
 123       * @return string
 124       */
 125  	public function get_rst_output(): string
 126      {
 127          return $this->rst_data;
 128      }
 129  
 130      /**
 131       * Set maximum lengths array
 132       *
 133       * @param array $event_data
 134       */
 135  	private function set_max_lengths(array $event_data): void
 136      {
 137          $this->max_lengths = [];
 138  
 139          foreach ($this->columns as $column)
 140          {
 141              $this->max_lengths[$column] = strlen($this->column_headers[$column]);
 142          }
 143  
 144          foreach ($event_data as $event)
 145          {
 146              foreach ($this->columns as $column)
 147              {
 148                  $event_column = is_array($event[$column]) ? implode(', ', $event[$column]) : $event[$column];
 149                  $this->max_lengths[$column] = max($this->max_lengths[$column], strlen($event_column));
 150              }
 151          }
 152      }
 153  
 154      /**
 155       * Get separator line
 156       *
 157       * @param string $separator_character
 158       * @return string
 159       */
 160  	private function get_separator_line(string $separator_character = '-'): string
 161      {
 162          $line = "    +";
 163  
 164          foreach ($this->columns as $column)
 165          {
 166              $line .= str_repeat($separator_character, $this->max_lengths[$column] + 2) . '+';
 167          }
 168  
 169          return $line . "\n";
 170      }
 171  
 172      /**
 173       * Get table data column
 174       *
 175       * @param string $type Column type
 176       * @param string $content Column content
 177       * @return string
 178       */
 179  	private function get_column(string $type, string $content): string
 180      {
 181          $content = rtrim($content);
 182          return ' ' . $content . str_repeat(' ' , $this->max_lengths[$type] - strlen($content) + 1) . '|';
 183      }
 184  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1