1<?php
2/**
3 * Ajax interface for attachments.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public License,
6 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
7 * obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * @package phpMyFAQ
10 * @author Anatoliy Belsky <ab@php.net>
11 * @copyright 2010-2020 phpMyFAQ Team
12 * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
13 * @link https://www.phpmyfaq.de
14 * @since 2010-12-13
15 */
16
17use phpMyFAQ\Attachment\AttachmentCollection;
18use phpMyFAQ\Filter;
19use phpMyFAQ\Pagination;
20
21if (!defined('IS_VALID_PHPMYFAQ')) {
22    http_response_code(400);
23    exit();
24}
25
26$page = Filter::filterInput(INPUT_GET, 'page', FILTER_VALIDATE_INT);
27$page = 1 > $page ? 1 : $page;
28
29$attachmentCollection = new AttachmentCollection($faqConfig);
30$itemsPerPage = 24;
31$allCrumbs = $attachmentCollection->getBreadcrumbs();
32
33$crumbs = array_slice($allCrumbs, ($page - 1) * $itemsPerPage, $itemsPerPage);
34
35$pagination = new Pagination(
36    $faqConfig,
37    [
38        'baseUrl' => $faqConfig->getDefaultUrl() . 'admin/?' . str_replace('&', '&amp;', $_SERVER['QUERY_STRING']),
39        'total' => count($allCrumbs),
40        'perPage' => $itemsPerPage,
41    ]
42);
43?>
44<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
45  <h1 class="h2">
46    <i aria-hidden="true" class="fa fa-paperclip"></i>
47      <?= $PMF_LANG['ad_menu_attachment_admin'] ?>
48  </h1>
49</div>
50
51<div class="row">
52  <div class="col-lg-12">
53    <table class="table table-striped">
54      <thead>
55      <tr>
56        <th>#</th>
57        <th><?= $PMF_LANG['msgAttachmentsFilename'] ?></th>
58        <th><?= $PMF_LANG['msgTransToolLanguage'] ?></th>
59        <th><?= $PMF_LANG['msgAttachmentsFilesize'] ?></th>
60        <th colspan="3"><?= $PMF_LANG['msgAttachmentsMimeType'] ?></th>
61      </tr>
62      </thead>
63      <tbody>
64      <?php foreach ($crumbs as $item): ?>
65        <tr class="att_<?= $item->id ?>" title="<?= $item->thema ?>">
66          <td><?= $item->id ?></td>
67          <td><?= $item->filename ?></td>
68          <td><?= $item->record_lang ?></td>
69          <td><?= $item->filesize ?></td>
70          <td><?= $item->mime_type ?></td>
71          <td>
72            <a href="deleteAttachment(<?= $item->id ?>, '<?= $user->getCsrfTokenFromSession() ?>'); void(0);"
73               class="btn btn-danger" title="<?= $PMF_LANG['ad_gen_delete'] ?>">
74              <i aria-hidden="true" class="fa fa-trash"></i>
75            </a>
76          </td>
77          <td>
78            <a title="<?= $PMF_LANG['ad_entry_faq_record'] ?>" class="btn btn-info"
79               href="../index.php?action=faq&id=<?= $item->record_id ?>&lang=<?= $item->record_lang ?>">
80              <i aria-hidden="true" class="fa fa-link"></i>
81            </a>
82          </td>
83        </tr>
84      <?php endforeach; ?>
85      </tbody>
86      <tfoot>
87      <tr>
88        <td colspan="5"><?= $pagination->render(); ?></td>
89      </tr>
90      </tfoot>
91    </table>
92  </div>
93</div>
94
95<script>
96  /**
97   * Ajax call for deleting attachments
98   *
99   * @param attachmentId Attachment id
100   * @apram csrf CSRF token
101   */
102  function deleteAttachment(attachmentId, csrf) {
103    if (confirm('<?= $PMF_LANG['msgAttachmentsWannaDelete'] ?>')) {
104      $('#pmf-admin-saving-data-indicator').html('<i class="fa fa-cog fa-spin fa-fw"></i><span class="sr-only">Deleting ...</span>');
105      $.ajax({
106        type: "GET",
107        url: "index.php?action=ajax&ajax=att&ajaxaction=delete",
108        data: {attId: attachmentId, csrf: csrf},
109        success: function (msg) {
110          $('.att_' + attachmentId).fadeOut('slow');
111          $('#pmf-admin-saving-data-indicator').html('<p class="alert alert-success">' + msg + '</p>');
112        }
113      });
114    }
115  }
116</script>
117