]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/YammerImport/lib/yammerprogressform.php
YammerImport admin UI now auto-refreshes during running BG progress, whee
[quix0rs-gnu-social.git] / plugins / YammerImport / lib / yammerprogressform.php
1 <?php
2
3 class YammerProgressForm extends Form
4 {
5     /**
6      * ID of the form
7      *
8      * @return string ID of the form
9      */
10     function id()
11     {
12         return 'yammer-progress-form';
13     }
14
15     /**
16      * class of the form
17      *
18      * @return string class of the form
19      */
20     function formClass()
21     {
22         $classes = array('form_settings');
23         $runner = YammerRunner::init();
24         if ($runner->lastError()) {
25             $classes[] = 'import-error';
26         } else if ($runner->state() == 'done') {
27             $classes[] = 'import-done';
28         } else {
29             $classes[] = 'import-progress';
30         }
31         return implode(' ', $classes);
32     }
33
34     /**
35      * Action of the form
36      *
37      * @return string URL of the action
38      */
39     function action()
40     {
41         return common_local_url('yammeradminpanel');
42     }
43
44     /**
45      * Data elements of the form
46      *
47      * @return void
48      */
49     function formData()
50     {
51         $this->out->hidden('subaction', 'progress');
52
53         $runner = YammerRunner::init();
54
55         $error = $runner->lastError();
56         $userCount = $runner->countUsers();
57         $groupCount = $runner->countGroups();
58         $fetchedCount = $runner->countFetchedNotices();
59         $savedCount = $runner->countSavedNotices();
60
61         $labels = array(
62             'init' => array(
63                 'label' => _m("Initialize"),
64                 'progress' => _m('No import running'),
65                 'complete' => _m('Initiated Yammer server connection...'),
66             ),
67             'requesting-auth' => array(
68                 'label' => _m('Connect to Yammer'),
69                 'progress' => _m('Awaiting authorization...'),
70                 'complete' => _m('Connected.'),
71             ),
72             'import-users' => array(
73                 'label' => _m('Import user accounts'),
74                 'progress' => sprintf(_m("Importing %d user...", "Importing %d users...", $userCount), $userCount),
75                 'complete' => sprintf(_m("Imported %d user.", "Imported %d users.", $userCount), $userCount),
76             ),
77             'import-groups' => array(
78                 'label' => _m('Import user groups'),
79                 'progress' => sprintf(_m("Importing %d group...", "Importing %d groups...", $groupCount), $groupCount),
80                 'complete' => sprintf(_m("Imported %d group.", "Imported %d groups.", $groupCount), $groupCount),
81             ),
82             'fetch-messages' => array(
83                 'label' => _m('Prepare public notices for import'),
84                 'progress' => sprintf(_m("Preparing %d notice...", "Preparing %d notices...", $fetchedCount), $fetchedCount),
85                 'complete' => sprintf(_m("Prepared %d notice.", "Prepared %d notices.", $fetchedCount), $fetchedCount),
86             ),
87             'save-messages' => array(
88                 'label' => _m('Import public notices'),
89                 'progress' => sprintf(_m("Importing %d notice...", "Importing %d notices...", $savedCount), $savedCount),
90                 'complete' => sprintf(_m("Imported %d notice.", "Imported %d notices.", $savedCount), $savedCount),
91             ),
92             'done' => array(
93                 'label' => _m('Done'),
94                 'progress' => sprintf(_m("Import is complete!")),
95                 'complete' => sprintf(_m("Import is complete!")),
96             )
97         );
98         $steps = array_keys($labels);
99         $currentStep = array_search($runner->state(), $steps);
100
101         $classes = array('yammer-import');
102         if ($error) {
103             $classes[] = 'yammer-error';
104         } else {
105             $classes[] = 'yammer-running';
106         }
107         $this->out->elementStart('fieldset', array('class' => implode(' ', $classes)));
108         $this->out->element('legend', array(), _m('Import status'));
109         foreach ($steps as $step => $state) {
110             if ($state == 'init') {
111                 // Don't show 'init', it's boring.
112                 continue;
113             }
114             if ($step < $currentStep) {
115                 // This step is done
116                 $this->progressBar($state,
117                                    'complete',
118                                    $labels[$state]['label'],
119                                    $labels[$state]['complete']);
120             } else if ($step == $currentStep) {
121                 // This step is in progress
122                 $this->progressBar($state,
123                                    'progress',
124                                    $labels[$state]['label'],
125                                    $labels[$state]['progress'],
126                                    $error);
127             } else {
128                 // This step has not yet been done.
129                 $this->progressBar($state,
130                                    'waiting',
131                                    $labels[$state]['label'],
132                                    _m("Waiting..."));
133             }
134         }
135         $this->out->elementEnd('fieldset');
136     }
137
138     private function progressBar($state, $class, $label, $status, $error=null)
139     {
140         // @fixme prettify ;)
141         $this->out->elementStart('div', array('class' => "import-step import-step-$state $class"));
142         $this->out->element('div', array('class' => 'import-label'), $label);
143         $this->out->element('div', array('class' => 'import-status'), $status);
144         if ($class == 'progress') {
145             if ($state == 'done') {
146                 $this->out->submit('abort-import', _m('Reset import state'));
147             } else {
148                 if ($error) {
149                     $this->errorBox($error);
150                 } else {
151                     $this->out->submit('pause-import', _m('Pause import'));
152                 }
153             }
154         }
155         $this->out->elementEnd('div');
156     }
157
158     private function errorBox($msg)
159     {
160         $errline = sprintf(_m('Encountered error "%s"'), $msg);
161         $this->out->elementStart('fieldset', array('class' => 'import-error'));
162         $this->out->element('legend', array(), _m('Paused'));
163         $this->out->element('p', array(), $errline);
164         $this->out->submit('continue-import', _m('Continue'));
165         $this->out->submit('abort-import', _m('Abort import'));
166         $this->out->elementEnd('fieldset');
167     }
168 }