1<?php
2/**
3 * DokuWiki Plugin structtasks (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Chris MacMackin <cmacmackin@gmail.com>
7 */
8
9use dokuwiki\plugin\structtasks\meta\Utilities;
10use dokuwiki\plugin\structtasks\meta\AssignedNotifier;
11use dokuwiki\plugin\structtasks\meta\ClosedStatusNotifier;
12use dokuwiki\plugin\structtasks\meta\DateNotifier;
13use dokuwiki\plugin\structtasks\meta\DeletedNotifier;
14use dokuwiki\plugin\structtasks\meta\OpenStatusNotifier;
15use dokuwiki\plugin\structtasks\meta\RemovedNotifier;
16use dokuwiki\plugin\structtasks\meta\SelfRemovalNotifier;
17
18class action_plugin_structtasks extends \dokuwiki\Extension\ActionPlugin
19{
20
21    public $notifiers;
22
23    private $util;
24
25    /** @inheritDoc */
26    public function register(Doku_Event_Handler $controller): void
27    {
28        // This must run AFTER the Struct plugin has updated the metadata
29        $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'AFTER', $this, 'handle_common_wikipage_save', null, 3999);
30
31    }
32
33    /**
34     * Set up notifier objects, if they haven't been set already.
35     */
36    private function initNotifiers(): void {
37        if (isset($this->notifiers)) return;
38        $getConf = [$this, 'getConf'];
39        $getLang = [$this, 'getLang'];
40        $this->notifiers = [
41            new AssignedNotifier($getConf, $getLang),
42            new ClosedStatusNotifier($getConf, $getLang),
43            new DateNotifier($getConf, $getLang),
44            new DeletedNotifier($getConf, $getLang),
45            new OpenStatusNotifier($getConf, $getLang),
46            new RemovedNotifier($getConf, $getLang),
47            new SelfRemovalNotifier($getConf, $getLang),
48        ];
49    }
50
51    /**
52     * Event handler for notifying task assignees of task changes or creation
53     *
54     * @param Doku_Event $event  event object by reference
55     * @param mixed      $param  optional parameter passed when event was registered
56     * @return void
57     */
58    public function handle_common_wikipage_save(Doku_Event $event, $param)
59    {
60        global $conf;
61        $schema = $conf['plugin']['structtasks']['schema'];
62        if ($schema == '') return false;
63        $struct = $this->loadHelper('struct', true);
64        if (is_null($struct)) return;
65        $util = new Utilities($struct);
66
67        $id = $event->data['id'];
68        list($old_data, $new_data, $valid) = $util->getMetadata(
69            $id, $schema, $event->data['oldRevision'], $event->data['newRevision']
70        );
71        if (!$valid) return;
72
73        global $INPUT;
74        global $auth;
75        $this->initNotifiers();
76        $title = p_get_first_heading($id, METADATA_RENDER_USING_SIMPLE_CACHE);
77        $editor_id = $INPUT->server->str('REMOTE_USER');
78        $userData = $auth->getUserData($editor_id, false);
79        $editor_name = ($userData !== false and $userData['name'] !== '') ?
80                     $userData['name'] : $editor_id;
81        $editor_email = $util->getUserEmail($editor_id);
82        $new_data['content'] = $event->data['newContent'];
83        $old_data['content'] = $event->data['oldContent'];
84
85        foreach ($this->notifiers as $notifier) {
86            $notifier->sendMessage($id, $title, $editor_name, $editor_email, $new_data, $old_data);
87        }
88    }
89}
90