summaryrefslogtreecommitdiff
path: root/plugins/YammerImport/lib/yammerprogressform.php
blob: add8d9ab2f39005be942db6b6153da10cc74000e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php

class YammerProgressForm extends Form
{
    /**
     * ID of the form
     *
     * @return string ID of the form
     */
    function id()
    {
        return 'yammer-progress-form';
    }

    /**
     * class of the form
     *
     * @return string class of the form
     */
    function formClass()
    {
        return 'form_settings';
    }

    /**
     * Action of the form
     *
     * @return string URL of the action
     */
    function action()
    {
        return common_local_url('yammeradminpanel');
    }

    /**
     * Data elements of the form
     *
     * @return void
     */
    function formData()
    {
        $this->out->hidden('subaction', 'progress');

        $runner = YammerRunner::init();

        $error = $runner->lastError();
        $userCount = $runner->countUsers();
        $groupCount = $runner->countGroups();
        $fetchedCount = $runner->countFetchedNotices();
        $savedCount = $runner->countSavedNotices();

        $labels = array(
            'init' => array(
                'label' => _m("Initialize"),
                'progress' => _m('No import running'),
                'complete' => _m('Initiated Yammer server connection...'),
            ),
            'requesting-auth' => array(
                'label' => _m('Connect to Yammer'),
                'progress' => _m('Awaiting authorization...'),
                'complete' => _m('Connected.'),
            ),
            'import-users' => array(
                'label' => _m('Import user accounts'),
                'progress' => sprintf(_m("Importing %d user...", "Importing %d users...", $userCount), $userCount),
                'complete' => sprintf(_m("Imported %d user.", "Imported %d users.", $userCount), $userCount),
            ),
            'import-groups' => array(
                'label' => _m('Import user groups'),
                'progress' => sprintf(_m("Importing %d group...", "Importing %d groups...", $groupCount), $groupCount),
                'complete' => sprintf(_m("Imported %d group.", "Imported %d groups.", $groupCount), $groupCount),
            ),
            'fetch-messages' => array(
                'label' => _m('Prepare public notices for import'),
                'progress' => sprintf(_m("Preparing %d notice...", "Preparing %d notices...", $fetchedCount), $fetchedCount),
                'complete' => sprintf(_m("Prepared %d notice.", "Prepared %d notices.", $fetchedCount), $fetchedCount),
            ),
            'save-messages' => array(
                'label' => _m('Import public notices'),
                'progress' => sprintf(_m("Importing %d notice...", "Importing %d notices...", $savedCount), $savedCount),
                'complete' => sprintf(_m("Imported %d notice.", "Imported %d notices.", $savedCount), $savedCount),
            ),
            'done' => array(
                'label' => _m('Done'),
                'progress' => sprintf(_m("Import is complete!")),
                'complete' => sprintf(_m("Import is complete!")),
            )
        );
        $steps = array_keys($labels);
        $currentStep = array_search($runner->state(), $steps);

        $classes = array('yammer-import');
        if ($error) {
            $classes[] = 'yammer-error';
        } else {
            $classes[] = 'yammer-running';
        }
        $this->out->elementStart('fieldset', array('class' => implode(' ', $classes)));
        $this->out->element('legend', array(), _m('Import status'));
        foreach ($steps as $step => $state) {
            if ($state == 'init') {
                // Don't show 'init', it's boring.
                continue;
            }
            if ($step < $currentStep) {
                // This step is done
                $this->progressBar($state,
                                   'complete',
                                   $labels[$state]['label'],
                                   $labels[$state]['complete']);
            } else if ($step == $currentStep) {
                // This step is in progress
                $this->progressBar($state,
                                   'progress',
                                   $labels[$state]['label'],
                                   $labels[$state]['progress'],
                                   $error);
            } else {
                // This step has not yet been done.
                $this->progressBar($state,
                                   'waiting',
                                   $labels[$state]['label'],
                                   _m("Waiting..."));
            }
        }
        $this->out->elementEnd('fieldset');
    }

    private function progressBar($state, $class, $label, $status, $error=null)
    {
        // @fixme prettify ;)
        $this->out->elementStart('div', array('class' => "import-step import-step-$state $class"));
        $this->out->element('div', array('class' => 'import-label'), $label);
        $this->out->element('div', array('class' => 'import-status'), $status);
        if ($class == 'progress') {
            if ($state == 'done') {
                $this->out->submit('abort-import', _m('Reset import state'));
            } else {
                if ($error) {
                    $this->errorBox($error);
                } else {
                    $this->out->submit('pause-import', _m('Pause import'));
                }
            }
        }
        $this->out->elementEnd('div');
    }

    private function errorBox($msg)
    {
        $errline = sprintf(_m('Encountered error "%s"'), $msg);
        $this->out->elementStart('fieldset', array('class' => 'import-error'));
        $this->out->element('legend', array(), _m('Paused'));
        $this->out->element('p', array(), $errline);
        $this->out->submit('continue-import', _m('Continue'));
        $this->out->submit('abort-import', _m('Abort import'));
        $this->out->elementEnd('fieldset');
    }
}