1<?php
2/**
3 * DokuWiki Plugin farmsync (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <grosse@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12use dokuwiki\Form\Form;
13use dokuwiki\plugin\farmsync\meta\FarmSyncUtil;
14
15class admin_plugin_farmsync extends DokuWiki_Admin_Plugin {
16
17    /**
18     * @return int sort number in admin menu
19     */
20    public function getMenuSort() {
21        return 43; // One behind the Farmer Entry
22    }
23
24    /**
25     * @return bool true if only access for superuser, false is for superusers and moderators
26     */
27    public function forAdminOnly() {
28        return true;
29    }
30
31    public $farm_util;
32
33    /** @var \helper_plugin_struct_imexport $struct */
34    private $struct;
35
36    function __construct() {
37        $this->farm_util = new FarmSyncUtil();
38    }
39
40    /**
41     * Should carry out any processing required by the plugin.
42     */
43    public function handle() {
44    }
45
46    /**
47     * Render HTML output, e.g. helpful text and a form
48     */
49    public function html() {
50        $farmer = plugin_load('helper', 'farmer');
51        if (!$farmer) {
52            msg('The farmsync plugin requires the farmer plugin to work. Please install it', -1);
53            return;
54        }
55
56
57        global $INPUT;
58        if (!($INPUT->has('farmsync-animals') && $INPUT->has('farmsync'))) {
59            echo "<div id=\"plugin__farmsync\">";
60            echo '<h1>' . $this->getLang('heading:Update animals') . '</h1>';
61            $animals = $this->farm_util->getAllAnimals();
62            $form = new Form();
63            $form->addFieldsetOpen($this->getLang('legend:choose source'));
64            $form->addDropdown('farmsync[source]', $animals, $this->getLang('label:source'))->addClass('make_chosen');
65            $form->addFieldsetClose();
66            $form->addFieldsetOpen($this->getLang('legend:choose documents'));
67            $form->addTextarea('farmsync[pages]', $this->getLang('label:PageEntry'));
68            $form->addHTML("<br>");
69            $form->addTextarea('farmsync[media]', $this->getLang('label:MediaEntry'));
70            $form->addHTML("<br>");
71            if (plugin_load('helper', 'struct_imexport')) {
72                $form->addCheckbox('farmsync[struct]', $this->getLang('label:struct synchronisation'));
73                $form->addTagOpen('div')->addClass('structsync')->attr('style','display: none;');
74                $form->addTagClose('div');
75            } elseif (plugin_load('helper', 'struct_imexport', false, true)) {
76                echo '<div style="color: grey;">' . $this->getLang('notice:struct disabled') . '</div>';
77            }
78            $form->addFieldsetClose();
79            $form->addFieldsetOpen($this->getLang('legend:choose animals'));
80            foreach ($animals as $animal) {
81                $form->addCheckbox('farmsync-animals[' . $animal . ']', $animal);
82            }
83            $form->addFieldsetClose();
84            $form->addButton('submit', $this->getLang('button:submit'));
85
86            echo $form->toHTML();
87            echo $this->locale_xhtml('update');
88            echo "</div>";
89            return;
90        } else {
91            set_time_limit(0);
92            $targets = array_keys($INPUT->arr('farmsync-animals'));
93            $options = $INPUT->arr('farmsync');
94            $textare_linebreak = "\r\n";
95            $pages = array_filter(explode($textare_linebreak, $options['pages']));
96            $media = array_filter(explode($textare_linebreak, $options['media']));
97            $struct = array_keys($INPUT->arr('farmsync_struct'));
98            $source = $options['source']; // ToDo: validate thath source exists
99
100            /** @var \dokuwiki\plugin\farmsync\meta\EntityUpdates[] $updaters */
101            $updaters = array();
102            if (!empty($pages)) {
103                $updaters[] = new \dokuwiki\plugin\farmsync\meta\PageUpdates($source, $targets, $pages);
104                $updaters[] = new \dokuwiki\plugin\farmsync\meta\TemplateUpdates($source, $targets, $pages);
105            }
106            if (!empty($media)) {
107                $updaters[] = new \dokuwiki\plugin\farmsync\meta\MediaUpdates($source, $targets, $media);
108            }
109            if (!empty($struct)) {
110                $updaters[] = new \dokuwiki\plugin\farmsync\meta\StructUpdates($source, $targets, $struct);
111            }
112            echo "<div id=\"plugin__farmsync\"><div id=\"results\" data-source='$options[source]'>";
113            echo "<span class='progress'>Progress and Errors</span>";
114            echo "<div class='progress'>";
115            foreach ($updaters as $updater) {
116                $updater->updateEntities();
117            }
118
119            echo "</div>";
120            echo "<h1>" . $this->getLang('heading:Update done') . "</h1>";
121            foreach ($targets as $target) {
122                $conflicts = 0;
123                foreach ($updaters as $updater) {
124                    $conflicts += $updater->getNumberOfAnimalConflicts($target);
125                }
126                if ($conflicts == 0) {
127                    $class = 'noconflicts';
128                    $heading = sprintf($this->getLang('heading:animal noconflict'), $target);
129                } else {
130                    $class = 'withconflicts';
131                    $heading = sprintf($this->getLang('heading:animal conflict'), $target, $conflicts);
132                }
133                echo "<div class='result $class'><h2>" . $heading . "</h2>";
134                echo "<div>";
135
136                foreach ($updaters as $updater) {
137                    $updater->printAnimalResultHTML($target);
138                }
139                echo "</div>";
140                echo "</div>";
141            }
142            echo "</div></div>";
143        }
144    }
145}
146
147// vim:ts=4:sw=4:et:
148