Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

rst_exporter.php

Zuletzt modifiziert: 02.04.2025, 15:02 - Dateigröße: 4.09 KiB


001  <?php
002  /**
003   *
004   * This file is part of the phpBB Forum Software package.
005   *
006   * @copyright (c) phpBB Limited <https://www.phpbb.com>
007   * @license GNU General Public License, version 2 (GPL-2.0)
008   *
009   * For full copyright and license information, please see
010   * the docs/CREDITS.txt file.
011   *
012   */
013   
014  namespace phpbb\event;
015   
016  class rst_exporter
017  {
018      /** @var array Column keys */
019      private $columns = [];
020   
021      /** @var array Column headers map */
022      private $column_headers = [];
023   
024      /** @var array Maximum lengths of columns */
025      private $max_lengths = [];
026   
027      /** @var string rst data */
028      private $rst_data = '';
029   
030      /**
031       * Set columns with array where key is column name and value is title of column in table
032       *
033       * @param array $column_data
034       */
035      public function set_columns(array $column_data): void
036      {
037          foreach ($column_data as $column_key => $column_header)
038          {
039              $this->columns[] = $column_key;
040              $this->column_headers[$column_key] = $column_header;
041          }
042      }
043   
044      /**
045       * Add header to rst page
046       *
047       * @param string $type Type of header; allowed are h2, h3, h4 corresponding to HTML
048       * @param string $header_text Text of header
049       */
050      public function add_section_header(string $type, string $header_text): void
051      {
052          $this->rst_data .= $header_text . "\n";
053   
054          switch ($type)
055          {
056              case 'h2':
057                  $header_character = '=';
058              break;
059   
060              default:
061              case 'h3':
062                  $header_character = '-';
063              break;
064   
065              case 'h4':
066                  $header_character = '~';
067              break;
068          }
069   
070          $this->rst_data .= str_repeat($header_character, strlen($header_text)) . "\n\n";
071      }
072   
073      /**
074       * Fill table with event data
075       *
076       * @param array $event_data
077       */
078      public function generate_events_table(array $event_data): void
079      {
080          $this->rst_data .= ".. table::\n";
081          $this->rst_data .= "    :class: events-list\n\n";
082   
083          $this->set_max_lengths($event_data);
084   
085          // Create table header
086          $this->rst_data .= $this->get_separator_line();
087          $this->rst_data .= "    |";
088          foreach ($this->columns as $column)
089          {
090              $this->rst_data .= $this->get_column($column, $this->column_headers[$column]);
091          }
092   
093          $this->rst_data .= "\n" . $this->get_separator_line('=');
094   
095          foreach ($event_data as $event)
096          {
097              $event_data = [];
098              $max_column_rows = 1;
099              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  }
185