Completed
Push — master ( 395485...bbad3a )
by Julito
43:12
created

tools.php ➔ getCustomWebIconPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
require_once __DIR__.'/../inc/global.inc.php';
5
6
// The section for the tabs
7
$this_section = SECTION_COURSES;
8
9
$sessionId = api_get_session_id();
10
11
if (!empty($sessionId)) {
12
    api_not_allowed();
13
}
14
15
api_protect_course_script(true);
16
17
if (!api_is_allowed_to_edit()) {
18
    api_not_allowed(true);
19
}
20
21
$action = isset($_GET['action']) ? $_GET['action'] : '';
22
$id = isset($_GET['id']) ? intval($_GET['id']) : '';
23
24
$toolName = get_lang('CustomizeIcons');
25
26
switch ($action) {
27
    case 'delete_icon':
28
        $tool = CourseHome::getTool($id);
29
        if (empty($tool)) {
30
            api_not_allowed(true);
31
        }
32
33
        $currentUrl = api_get_self().'?'.api_get_cidreq();
34
        Display::addFlash(Display::return_message(get_lang('Updated')));
35
        CourseHome::deleteIcon($id);
36
        header('Location: '.$currentUrl);
37
        exit;
38
39
        break;
40
    case 'edit_icon':
41
        $tool = CourseHome::getTool($id);
42
        if (empty($tool)) {
43
            api_not_allowed(true);
44
        }
45
46
        $interbreadcrumb[] = array(
47
            'url' => api_get_self().'?'.api_get_cidreq(),
48
            'name' => get_lang('CustomizeIcons'),
49
        );
50
        $toolName = Security::remove_XSS(stripslashes($tool['name']));
51
52
        $currentUrl = api_get_self().'?action=edit_icon&id=' . $id.'&'.api_get_cidreq();
53
54
        $form = new FormValidator('icon_edit', 'post', $currentUrl);
55
        $form->addHeader(get_lang('EditIcon'));
56
        $form->addHtml('<div class="col-md-7">');
57
        $form->addText('name', get_lang('Name'));
58
        $form->addText('link', get_lang('Links'));
59
        $allowed_picture_types = array ('jpg', 'jpeg', 'png');
60
        $form->addFile('icon', get_lang('CustomIcon'));
61
        $form->addRule('icon', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
62
        $form->addSelect(
63
            'target',
64
            get_lang('LinkTarget'),
65
            [
66
                '_self' => get_lang('LinkOpenSelf'),
67
                '_blank' => get_lang('LinkOpenBlank')
68
            ]
69
        );
70
        $form->addSelect(
71
            'visibility',
72
            get_lang('Visibility'),
73
            array(1 => get_lang('Visible'), 0 => get_lang('Invisible'))
74
        );
75
        $form->addTextarea(
76
            'description',
77
            get_lang('Description'),
78
            array('rows' => '3', 'cols' => '40')
79
        );
80
        $form->addButtonUpdate(get_lang('Update'));
81
        $form->addHtml('</div>');
82
        $form->addHtml('<div class="col-md-5">');
83 View Code Duplication
        if (isset($tool['custom_icon']) && !empty($tool['custom_icon'])) {
84
            $form->addLabel(
85
                get_lang('CurrentIcon'),
86
                Display::img(
87
                    CourseHome::getCustomWebIconPath().$tool['custom_icon']
88
                )
89
            );
90
91
            $form->addCheckBox('delete_icon', null, get_lang('DeletePicture'));
92
        }
93
        $form->addHtml('</div>');
94
        $form->setDefaults($tool);
95
        $content = $form->returnForm();
96
97
        if ($form->validate()) {
98
            $data = $form->getSubmitValues();
99
            CourseHome::updateTool($id, $data);
100
            Display::addFlash(Display::return_message(get_lang('Updated')));
101
            if (isset($data['delete_icon'])) {
102
                CourseHome::deleteIcon($id);
103
            }
104
            $currentUrlReturn = api_get_self().'?'.api_get_cidreq();
105
            header('Location: '.$currentUrlReturn);
106
            exit;
107
        }
108
        break;
109
    case 'list':
110
    default:
111
        $toolList = CourseHome::toolsIconsAction(
112
            api_get_course_int_id(),
113
            api_get_session_id()
114
        );
115
        $iconsTools = '<div id="custom-icons">';
116
        $iconsTools .= Display::page_header(get_lang('CustomizeIcons'), null, 'h4');
117
        $iconsTools .= '<div class="row">';
118
        foreach ($toolList as $tool) {
119
            $tool['name'] = Security::remove_XSS(stripslashes($tool['name']));
120
            $toolIconName = 'Tool' . api_underscore_to_camel_case($tool['name']);
121
            $toolIconName = isset($$toolIconName) ? get_lang($toolIconName) : $tool['name'];
122
123
            $iconsTools .= '<div class="col-md-2">';
124
            $iconsTools .= '<div class="items-tools">';
125
126
            if (!empty($tool['custom_icon'])) {
127
                $image = CourseHome::getCustomWebIconPath().$tool['custom_icon'];
128
                $icon = Display::img($image, $toolIconName);
129
            } else {
130
                $image = (substr($tool['image'], 0, strpos($tool['image'], '.'))).'.png';
131
                $icon = Display::return_icon(
132
                    $image,
133
                    $toolIconName,
134
                    array('id' => 'tool_'.$tool['id']),
135
                    ICON_SIZE_BIG,
136
                    false
137
                );
138
            }
139
140
            $delete = (!empty($tool['custom_icon'])) ? "<a class=\"btn btn-default\" onclick=\"javascript:
141
                if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)).
142
                "')) return false;\" href=\"". api_get_self() . '?action=delete_icon&id=' . $tool['iid'] . '&'.api_get_cidreq()."\">
143
            <em class=\"fa fa-trash-o\"></em></a>" : "";
144
            $edit = '<a class="btn btn-default" href="' . api_get_self() . '?action=edit_icon&id=' . $tool['iid'] . '&'.api_get_cidreq().'"><em class="fa fa-pencil"></em></a>';
145
146
            $iconsTools .= '<div class="icon-tools">'. $icon . '</div>';
147
            $iconsTools .= '<div class="name-tools">' . $toolIconName . '</div>';
148
            $iconsTools .= '<div class="toolbar">' . $edit . $delete . '</div>';
149
            $iconsTools .= '</div>';
150
            $iconsTools .= '</div>';
151
        }
152
        $iconsTools .= '</div>';
153
        $iconsTools .= '</div>';
154
        $content = $iconsTools;
155
        break;
156
}
157
158
$tpl = new Template($toolName);
159
$tpl->assign('content', $content);
160
$template = $tpl->get_template('layout/layout_1_col.tpl');
161
$tpl->display($template);
162