1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6
7/**
8 * DokuWiki Plugin taskextra (Action Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author Louis Ouellet <louis_ouellet@hotmail.com>
12 */
13class action_plugin_taskextra extends ActionPlugin
14{
15    /** @inheritDoc */
16    public function register(EventHandler $controller)
17    {
18        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
19    }
20
21    /**
22     * Event handler for AJAX requests
23     *
24     * @param Event $event Event object
25     * @param mixed $param optional parameter passed when event was registered
26     * @return void
27     */
28    public function handle_ajax(Event $event, $param)
29    {
30        global $INPUT, $ID, $TEXT;
31
32        switch ($event->data) {
33            case 'taskextra_unassign':
34            case 'taskextra_due':
35            case 'taskextra_priority':
36                break;
37            default:
38                return;
39        }
40
41        $event->preventDefault();
42        $event->stopPropagation();
43
44        switch ($event->data) {
45            case 'taskextra_unassign':
46                $this->handle_unassign($event);
47                break;
48            case 'taskextra_due':
49                $this->handle_due($event);
50                break;
51            case 'taskextra_priority':
52                $this->handle_priority($event);
53                break;
54        }
55        exit;
56    }
57
58    /**
59     * Handle unassigning a task
60     *
61     * @param Event $event Event object
62     * @return void
63     */
64    private function handle_unassign(Event $event)
65    {
66        global $INPUT, $ID, $TEXT;
67
68        // Get the page ID from the AJAX request
69        $id = $INPUT->str('id');
70
71        // Retrieve the content
72        $pageContent = rawWiki($id);
73
74        // Remove the assigned user from the task markup
75        $pageContent = preg_replace('/~~TASK:([^\?!]*?)(\?.*?)?(\!*)~~/', '~~TASK:$2$3~~', $pageContent);
76
77        // Save the updated content
78        saveWikiText($id, $pageContent, 'Unassigned the user');
79        echo json_encode(array('status' => 'success'));
80    }
81
82    /**
83     * Handle updating the due date of a task
84     *
85     * @param Event $event Event object
86     * @return void
87     */
88    private function handle_due(Event $event)
89    {
90        global $INPUT, $ID, $TEXT;
91
92        // Logic to handle updating the due date
93        $dueDate = $INPUT->str('due_date');
94        $id = $INPUT->str('id');
95        if (!$dueDate) {
96            echo json_encode(['error' => 'Due date is required']);
97            return;
98        }
99
100        // Retrieve the content
101        $pageContent = rawWiki($id);
102
103        // Check if the due date already exists and update accordingly
104        if (preg_match('/~~TASK:([^\?]*?)\?([^!]*)(!*)~~/', $pageContent)) {
105            // Update the existing due date
106            $pageContent = preg_replace('/~~TASK:([^\?]*?)\?([^!]*)(!*)~~/', '~~TASK:$1?' . $dueDate . '$3~~', $pageContent);
107        } else {
108            // Add the new due date if none exists
109            $pageContent = preg_replace('/~~TASK:([^\?]*?)(!*)~~/', '~~TASK:$1?' . $dueDate . '$2~~', $pageContent);
110        }
111
112        // Save the updated content
113        saveWikiText($id, $pageContent, 'Updated the due date to: ' . $dueDate);
114        echo json_encode(array('status' => 'success'));
115    }
116
117    /**
118     * Handle updating the priority of a task
119     *
120     * @param Event $event Event object
121     * @return void
122     */
123    private function handle_priority(Event $event)
124    {
125        global $INPUT, $ID, $TEXT;
126
127        // Logic to handle updating the priority
128        $priority = $INPUT->str('priority');
129        $id = $INPUT->str('id');
130        if (!$priority && $priority != 0) {
131            echo json_encode(['error' => 'Priority is required']);
132            return;
133        }
134
135        // Retrieve the content
136        $pageContent = rawWiki($id);
137
138        // Remove existing priority indicators
139        $pageContent = preg_replace('/~~TASK:(.*?)(\?\[.*?\])?(!*)~~/', '~~TASK:$1$2~~', $pageContent);
140
141        // Add the new priority level
142        $priorityIndicators = '';
143        if($priority > 0) {
144          $priorityIndicators = str_repeat('!', $priority);
145        }
146        $pageContent = preg_replace('/~~TASK:(.*?)(\?\[.*?\])?~~/', '~~TASK:$1$2' . $priorityIndicators . '~~', $pageContent);
147
148        // Save the updated content
149        saveWikiText($id, $pageContent, 'Updated the priority to: ' . $priority);
150        echo json_encode(array('status' => 'success'));
151    }
152}
153