1<?php
2
3namespace dokuwiki\template\sprintdoc;
4
5/**
6 * Class tpl
7 *
8 * Provides additional template functions for the dokuwiki template
9 * @package dokuwiki\tpl\dokuwiki
10 */
11
12class tpl {
13  static $icons = array(
14              'default' => '00-default_checkbox-blank-circle-outline.svg',
15              'edit' => '01-edit_pencil.svg',
16              'create' => '02-create_pencil.svg',
17              'draft' => '03-draft_android-studio.svg',
18              'show' => '04-show_file-document.svg',
19              'source' => '05-source_file-xml.svg',
20              'revert' => '06-revert_replay.svg',
21              'revs' => '07-revisions_history.svg',
22              'backlink' => '08-backlink_link-variant.svg',
23              'subscribe' => '09-subscribe_email-outline.svg',
24              'top' => '10-top_arrow-up.svg',
25              'mediaManager' => '11-mediamanager_folder-image.svg',
26              'img_backto' => '12-back_arrow-left.svg',
27          );
28
29/**
30   * Return the HTML for one of the default actions
31   *
32   * Reimplements parts of tpl_actionlink
33   *
34   * @param string $action
35   * @return string
36   */
37  static public function pageToolAction($action) {
38              $data = tpl_get_action($action);
39              if(!is_array($data)) return '';
40      global $lang;
41
42      if($data['id'][0] == '#') {
43                      $linktarget = $data['id'];
44                  } else {
45                      $linktarget = wl($data['id'], $data['params'], false, '&');
46                  }
47      $caption = $lang['btn_' . $data['type']];
48      if(strpos($caption, '%s') !== false) {
49                      $caption = sprintf($caption, $data['replacement']);
50                  }
51
52      $svg = __DIR__ . '/images/tools/' . self::$icons[$data['type']];
53
54      return self::pageToolItem(
55                      $linktarget,
56                      $caption,
57                      $svg,
58          array('accesskey' => $data['accesskey'])
59              );
60  }
61
62/**
63   * Return the HTML for a page action
64   *
65   * Plugins may use this in TEMPLATE_PAGETOOLS_DISPLAY
66   *
67   * @param string $link The link
68   * @param string $caption The label of the action
69   * @param string $svg The icon to show
70   * @param string[] $args HTML attributes for the item
71   * @return string
72   */
73  static public function pageToolItem($link, $caption, $svg, $args = array()) {
74              if(blank($args['title'])) {
75                      $args['title'] = $caption;
76                  }
77
78      if(!blank($args['accesskey'])) {
79                      $args['title'] .= ' [' . strtoupper($args['accesskey']) . ']';
80                  }
81
82      if(blank($args['rel'])) {
83                      $args['rel'] = 'nofollow';
84                  }
85
86      $args['href'] = $link ?: '#';
87
88      $svg = inlineSVG($svg);
89      if(!$svg) $svg = inlineSVG(__DIR__ . '/images/tools/' . self::$icons['default']);
90
91      $attributes = buildAttributes($args, true);
92
93      $out = "<a $attributes>";
94      $out .= '<span>' . hsc($caption) . '</span>';
95      $out .= $svg;
96      $out .= '</a>';
97
98      return $out;
99  }
100
101    /**
102     * Assemble the tools for the current page
103     *
104     * It also includes the tools for some plugins, if they are installed and enabled. This does currently not trigger
105     * any events, but should be adjusted to the standard dokuwiki template, once that has svg-functionality implemented.
106     *
107     * @return array
108     */
109    static public function assemblePageTools() {
110      $data = array(
111          'view'  => 'main-svg',
112          'items' => array(
113              'edit'      => static::pageToolAction('edit'),
114              'revert'    => static::pageToolAction('revert'),
115              'revisions' => static::pageToolAction('revisions'),
116              'backlink'  => static::pageToolAction('backlink'),
117              'subscribe' => static::pageToolAction('subscribe'),
118          )
119      );
120
121      $data['items'] = array_map(function ($elem) {
122          return '<li>' . $elem . '</li>';
123      },array_filter($data['items']));
124
125      /**
126       * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
127       * Begin shims as a temporary solution until the svg-approach is mainlined and the plugins have adapted
128       * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
129       */
130      global $ACT;
131      if (act_clean($ACT) === 'show') {
132          /** @var \action_plugin_move_rename $move */
133          $move = plugin_load('action', 'move_rename');
134          if ($move && $move->getConf('pagetools_integration')) {
135              $attr = array(
136                  'style' => 'background-image: none;',
137              );
138              $item = \dokuwiki\template\sprintdoc\tpl::pageToolItem('', $move->getLang('renamepage'), __DIR__ . '/images/tools/41-format-paint.svg', $attr);
139              $data['items'][] = '<li class="plugin_move_page">' . $item . '</li>';
140          }
141
142          /** @var \action_plugin_odt_export $odt */
143          $odt = plugin_load('action', 'odt_export');
144          if ($odt && $odt->getConf('showexportbutton')) {
145              global $ID, $REV;
146              $params = array('do' => 'export_odt');
147              if ($REV) {
148                  $params['rev'] = $REV;
149              }
150              $attr = array(
151                  'class' => 'action export_pdf',
152                  'style' => 'background-image: none;',
153              );
154              $svg = __DIR__ . '/images/tools/43-file-delimeted.svg';
155              $item = \dokuwiki\template\sprintdoc\tpl::pageToolItem(wl($ID, $params, false, '&'), $odt->getLang('export_odt_button'), $svg, $attr);
156              $data['items'][] = '<li>' . $item . '</li>';
157          }
158
159          /** @var \action_plugin_dw2pdf $dw2pdf */
160          $dw2pdf = plugin_load('action', 'dw2pdf');
161          if ($dw2pdf && $dw2pdf->getConf('showexportbutton')) {
162              global $ID, $REV;
163
164              $params = array('do' => 'export_pdf');
165              if ($REV) {
166                  $params['rev'] = $REV;
167              }
168              $attr = array(
169                  'class' => 'action export_pdf',
170                  'style' => 'background-image: none;',
171              );
172              $svg = __DIR__ . '/images/tools/40-pdf-file.svg';
173              $item = \dokuwiki\template\sprintdoc\tpl::pageToolItem(wl($ID, $params, false, '&'), $dw2pdf->getLang('export_pdf_button'), $svg, $attr);
174              $data['items'][] = '<li>' . $item . '</li>';
175          }
176      }
177      /**
178       * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
179       * End of shims
180       * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
181       */
182
183      $data['items']['top'] = '<li>' . static::pageToolAction('top') . '</li>';
184      return $data;
185  }
186}
187