Issues (2091)

public/plugin/TopLinks/admin.php (1 issue)

Labels
Severity
1
<?php
2
3
/* For license terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\Course as CourseEntity;
6
use Chamilo\CoreBundle\Framework\Container;
7
use Chamilo\PluginBundle\TopLinks\Entity\TopLink;
8
use Chamilo\PluginBundle\TopLinks\Entity\TopLinkRelTool;
9
use Chamilo\PluginBundle\TopLinks\Form\LinkForm as TopLinkForm;
10
use Symfony\Component\Filesystem\Filesystem;
11
12
$cidReset = true;
13
14
require_once __DIR__.'/../../main/inc/global.inc.php';
15
16
api_protect_admin_script();
17
18
$plugin = TopLinksPlugin::create();
19
$httpRequest = Container::getRequest();
20
21
$pageBaseUrl = api_get_self();
22
$em = Database::getManager();
23
$linkRepo = $em->getRepository(TopLink::class);
24
25
$pageTitle = $plugin->get_title();
26
$pageActions = Display::url(
27
    Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
28
    $pageBaseUrl
29
);
30
$pageContent = '';
31
32
$interbreadcrumb[] = [
33
    'name' => get_lang('Administration'),
34
    'url' => api_get_path(WEB_CODE_PATH).'admin/index.php',
35
];
36
$interbreadcrumb[] = ['name' => $plugin->get_title(), 'url' => $pageBaseUrl];
37
38
switch ($httpRequest->query->getAlpha('action', 'list')) {
39
    case 'list':
40
    default:
41
        array_pop($interbreadcrumb); // Only show link to administration in breadcrumb
42
43
        $pageActions = Display::url(
44
            Display::return_icon('add.png', get_lang('AddLink'), [], ICON_SIZE_MEDIUM),
45
            "$pageBaseUrl?".http_build_query(['action' => 'add'])
46
        );
47
48
        $table = new SortableTable(
49
            'toplinks',
50
            function () use ($linkRepo) {
51
                return $linkRepo->count([]);
52
            },
53
            function ($offset, $limit, $column, $direction) use ($linkRepo) {
54
                $links = $linkRepo->all($offset, $limit, $column, $direction);
55
56
                return array_map(
57
                    function (TopLink $link) {
58
                        return [
59
                            [$link->getTitle(), $link->getUrl()],
60
                            $link->getId(),
61
                        ];
62
                    },
63
                    $links
64
                );
65
            },
66
            0
67
        );
68
        $table->set_header(0, get_lang('LinkName'));
69
        $table->set_header(1, get_lang('Actions'), false, ['class' => 'th-head text-right'], ['class' => 'text-right']);
70
        $table->set_column_filter(
71
            0,
72
            function (array $params) {
73
                [$title, $url] = $params;
74
75
                return "$title<br>".Display::url($url, $url);
76
            }
77
        );
78
        $table->set_column_filter(
79
            1,
80
            function (int $id) use ($pageBaseUrl, $em, $plugin) {
81
                $missingCourses = $em->getRepository(TopLinkRelTool::class)->getMissingCoursesForTool($id);
82
                $countMissingCourses = count($missingCourses);
83
84
                $actions = [];
85
                $actions[] = Display::url(
86
                    Display::return_icon('edit.png', get_lang('Edit')),
87
                    "$pageBaseUrl?".http_build_query(['action' => 'edit', 'link' => $id])
88
                );
89
90
                if (count($missingCourses) > 0) {
91
                    $actions[] = Display::url(
92
                        Display::return_icon(
93
                            'view_tree.png',
94
                            sprintf($plugin->get_lang('ReplicateInXMissingCourses'), $countMissingCourses)
95
                        ),
96
                        "$pageBaseUrl?".http_build_query(['action' => 'replicate', 'link' => $id])
97
                    );
98
                } else {
99
                    $actions[] = Display::return_icon(
100
                        'view_tree_na.png',
101
                        $plugin->get_lang('AlreadyReplicatedInAllCourses')
102
                    );
103
                }
104
105
                $actions[] = Display::url(
106
                    Display::return_icon('delete.png', get_lang('Delete')),
107
                    "$pageBaseUrl?".http_build_query(['action' => 'delete', 'link' => $id])
108
                );
109
110
                return implode(PHP_EOL, $actions);
111
            }
112
        );
113
114
        if ($table->total_number_of_items) {
115
            $pageContent = $table->return_table();
116
        } else {
117
            $pageContent = Display::return_message(
118
                get_lang('NoData'),
119
                'info'
120
            );
121
        }
122
    break;
123
    case 'add':
124
        $pageTitle = get_lang('LinkAdd');
125
126
        $form = new TopLinkForm();
127
        $form->createElements();
128
129
        if ($form->validate()) {
130
            $values = $form->exportValues();
131
132
            $link = new TopLink();
133
            $link
134
                ->setTitle($values['title'])
135
                ->setUrl($values['url'])
136
                ->setTarget($values['target']);
137
138
            $em->persist($link);
139
            $em->flush();
140
141
            $iconPath = $form
142
                ->setLink($link)
143
                ->saveImage();
144
145
            $link->setIcon($iconPath);
146
147
            $em->flush();
148
149
            Display::addFlash(
150
                Display::return_message(get_lang('LinkAdded'), 'success')
151
            );
152
153
            header("Location: $pageBaseUrl");
154
            exit;
155
        }
156
157
        $pageContent = $form->returnForm();
158
        break;
159
    case 'edit':
160
        $pageTitle = get_lang('LinkMod');
161
162
        $link = $em->find(TopLink::class, $httpRequest->query->getInt('link'));
163
164
        if (null === $link) {
165
            Display::addFlash(
166
                Display::return_message(get_lang('Resource not found'), 'error')
167
            );
168
169
            header("Location: $pageBaseUrl");
170
            exit;
171
        }
172
173
        $form = new TopLinkForm($link);
174
        $form->createElements();
175
176
        if ($form->validate()) {
177
            $values = $form->exportValues();
178
179
            $iconPath = $form->saveImage();
180
181
            $link
182
                ->setTitle($values['title'])
183
                ->setUrl($values['url'])
184
                ->setIcon($iconPath)
185
                ->setTarget($values['target']);
186
187
            $em->flush();
188
189
            $em->getRepository(TopLinkRelTool::class)->updateTools($link);
190
191
            Display::addFlash(
192
                Display::return_message(get_lang('Item updated'), 'success')
193
            );
194
195
            header("Location: $pageBaseUrl");
196
            exit;
197
        }
198
199
        $pageContent = $form->returnForm();
200
        break;
201
    case 'delete':
202
        $link = $em->find(TopLink::class, $httpRequest->query->getInt('link'));
203
204
        if (null === $link) {
205
            Display::addFlash(
206
                Display::return_message(get_lang('Resource not found'), 'error')
207
            );
208
209
            header("Location: $pageBaseUrl");
210
            exit;
211
        }
212
213
        if ($link->getIcon()) {
214
            $fullIconPath = api_get_path(SYS_UPLOAD_PATH).'plugins/TopLinks/'.$link->getIcon();
0 ignored issues
show
The constant SYS_UPLOAD_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
215
216
            $fs = new Filesystem();
217
            $fs->remove($fullIconPath);
218
        }
219
220
        $em->remove($link);
221
        $em->flush();
222
223
        Display::addFlash(
224
            Display::return_message(get_lang('Item deleted'), 'success')
225
        );
226
227
        header("Location: $pageBaseUrl");
228
        exit;
229
    case 'replicate':
230
        $link = $em->find(TopLink::class, $httpRequest->query->getInt('link'));
231
232
        if (null === $link) {
233
            Display::addFlash(
234
                Display::return_message(get_lang('Resource not found'), 'error')
235
            );
236
237
            header("Location: $pageBaseUrl");
238
            exit;
239
        }
240
241
        $missingCourses = $em->getRepository(TopLinkRelTool::class)->getMissingCoursesForTool($link->getId());
242
243
        /** @var CourseEntity $missingCourse */
244
        foreach ($missingCourses as $missingCourse) {
245
            $plugin->addToolInCourse($missingCourse->getId(), $link);
246
        }
247
248
        Display::addFlash(
249
            Display::return_message($plugin->get_lang('LinkReplicated'), 'success')
250
        );
251
252
        header("Location: $pageBaseUrl");
253
        exit;
254
}
255
256
$view = new Template($plugin->get_title());
257
$view->assign('header', $pageTitle);
258
$view->assign('actions', Display::toolbarAction('xapi_actions', [$pageActions]));
259
$view->assign('content', $pageContent);
260
$view->display_one_col_template();
261