1<?php
2/*
3 * Yurii's Gantt Plugin
4 *
5 * Copyright (C) 2020 Yurii K.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see http://www.gnu.org/licenses
19 */
20/**
21 * https://docs.dhtmlx.com/gantt/api__gantt_autosize_config.html
22 *
23 * @var array $database
24 * @var string $pluginName
25 * @var string $lang
26 * @var $baseUrl
27 */
28
29use \dokuwiki\plugin\yuriigantt\src\Driver\Embedded as EmbeddedDriver;
30
31$withTranslation = function () use ($pluginName, $lang, $baseUrl) {
32    $langMap = [
33        'uk' => 'ua',
34    ];
35    $lang = preg_replace("/[^a-z]+/", "", $lang);
36
37    if (in_array($lang, array_keys($langMap))) {
38        $lang = $langMap[$lang];
39    }
40
41    $langFile = dirname(__DIR__, 2) . "/3rd/dhtmlxgantt/locale/locale_$lang.js";
42    $langUrl = $baseUrl . "lib/plugins/{$pluginName}/3rd/dhtmlxgantt/locale/locale_$lang.js?v=6.3.5";
43
44    if (!file_exists($langFile)) {
45        return;
46    }
47
48    echo "<script src=\"$langUrl\"></script>";
49};
50?>
51<link rel="stylesheet" href="<?= $baseUrl ?>lib/plugins/<?= $pluginName; ?>/3rd/dhtmlxgantt/dhtmlxgantt.css?v=6.3.5">
52<style>
53    .gantt-fullscreen {
54        position: absolute;
55        bottom: 10px;
56        right: 10px;
57        padding: 2px;
58        background: transparent;
59        cursor: pointer;
60        opacity: 0.5;
61        -webkit-transition: background-color 0.5s, opacity 0.5s;
62        transition: background-color 0.5s, opacity 0.5s;
63    }
64    .gantt-fullscreen:hover {
65        opacity: 1;
66    }
67</style>
68<script src="<?= $baseUrl ?>lib/plugins/<?= $pluginName; ?>/3rd/dhtmlxgantt/dhtmlxgantt.js?v=6.3.5"></script>
69<script src="<?= $baseUrl ?>lib/plugins/<?= $pluginName; ?>/3rd/dhtmlxgantt/ext/dhtmlxgantt_fullscreen.js?v=6.3.5"></script>
70<?php $withTranslation(); ?>
71<div id="<?= $pluginName; ?>"></div>
72<script>
73    let database = <?= json_encode($database); ?>;
74
75    gantt.config.autosize = "y"
76    gantt.config.date_format = "%d-%m-%Y %H:%i"
77    gantt.config.order_branch = true
78    gantt.config.order_branch_free = true
79
80    // fullscreen -->
81    gantt.attachEvent("onTemplatesReady", function () {
82        var toggleIcon = document.createElement("img");
83        toggleIcon.src = '<?= $baseUrl ?>lib/plugins/<?= $pluginName; ?>/3rd/fontawesome/expand-solid.svg';
84        toggleIcon.className = 'gantt-fullscreen'
85        toggleIcon.style.width = '20px'
86        toggleIcon.style.height = '20px'
87        gantt.toggleIcon = toggleIcon;
88        gantt.$container.appendChild(toggleIcon);
89        console.log(toggleIcon)
90        toggleIcon.onclick = function() {
91            gantt.ext.fullscreen.toggle();
92        };
93    });
94    gantt.attachEvent("onExpand", function () {
95        var toggleIcon = gantt.toggleIcon;
96        //console.log(toggleIcon)
97        if (toggleIcon) {
98            toggleIcon.src = '<?= $baseUrl ?>lib/plugins/<?= $pluginName; ?>/3rd/fontawesome/compress-solid.svg';
99        }
100
101    });
102    gantt.attachEvent("onCollapse", function () {
103        var toggleIcon = gantt.toggleIcon;
104        console.log(toggleIcon)
105        if (toggleIcon) {
106            toggleIcon.src = '<?= $baseUrl ?>lib/plugins/<?= $pluginName; ?>/3rd/fontawesome/expand-solid.svg';
107        }
108    });
109    // <---
110
111    gantt.init('<?=$pluginName;?>')
112
113    if (database.dsn === '<?= EmbeddedDriver::DSN ?>') {
114        gantt.config.cascade_delete = false; // optimization
115        gantt.parse(database.gantt)
116    } else {
117        throw new Error('NOT SUPPORTED DSN!')
118        //gantt.load('..URL..')
119    }
120
121    let dp = gantt.createDataProcessor({
122        task: {
123            create: function (data) {
124                restCall('create', 'task', data)
125            },
126            update: function (data, id) {
127                restCall('update', 'task', data, id)
128            },
129            delete: function (id) {
130                restCall('delete', 'task', null, id)
131            }
132        },
133        link: {
134            create: function (data) {
135                restCall('create', 'link', data)
136            },
137            update: function (data, id) {
138                restCall('update', 'link', data, id)
139            },
140            delete: function (id) {
141                restCall('delete', 'link', null, id)
142            }
143        }
144    });
145    dp.attachEvent("onAfterUpdate", function(id, action, tid, response){
146        if(action === 'error'){
147            console.warn('ERROR', response)
148        }
149    });
150
151    function restCall(action, entity, data, id) {
152        gantt.ajax.post('<?= $baseUrl . 'lib/exe/ajax.php'; ?>', {
153            call: 'plugin_<?=$pluginName;?>',
154            csrf: '<?= getSecurityToken() ; ?>',
155            payload: {
156                pageId: database.pageId,
157                version: database.version,
158                action: action,
159                entity: entity,
160                data: data,
161                id: id,
162                test: true
163            }
164        }).then(function(response){
165            var res = JSON.parse(response.responseText);
166            console.log(res)
167            if (res && res.status == "ok") {
168                // response is ok
169                console.log(res)
170            }
171        })
172    }
173</script>
174