]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/YammerImport/lib/yammerprogressform.php
0b73472a4d86eaa7c284e2b0908b61fd1a9de94e
[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                 // TRANS: Field label for a Yammer import initialise step.
64                 'label' => _m('Initialize'),
65                 // TRANS: "In progress" description.
66                 'progress' => _m('No import running'),
67                 // TRANS: "Complete" description for initialize state.
68                 'complete' => _m('Initiated Yammer server connection...'),
69             ),
70             'requesting-auth' => array(
71                 // TRANS: Field label for a Yammer import connect step.
72                 'label' => _m('Connect to Yammer'),
73                 // TRANS: "In progress" description.
74                 'progress' => _m('Awaiting authorization...'),
75                 // TRANS: "Complete" description for connect state.
76                 'complete' => _m('Connected.'),
77             ),
78             'import-users' => array(
79                 // TRANS: Field label for a Yammer user import users step.
80                 'label' => _m('Import user accounts'),
81                 // TRANS: "In progress" description.
82                 // TRANS: %d is the number of users to be imported.
83                 'progress' => sprintf(_m('Importing %d user...',
84                                          'Importing %d users...',
85                                          $userCount),
86                                       $userCount),
87                 // TRANS: "Complete" description for step.
88                 // TRANS: %d is the number of users imported.
89                 'complete' => sprintf(_m('Imported %d user.',
90                                          'Imported %d users.',
91                                          $userCount),
92                                       $userCount),
93             ),
94             'import-groups' => array(
95                 // TRANS: Field label for a Yammer group import step.
96                 'label' => _m('Import user groups'),
97                 // TRANS: "In progress" description.
98                 // TRANS: %d is the number of groups to be imported.
99                 'progress' => sprintf(_m('Importing %d group...',
100                                          'Importing %d groups...',
101                                          $groupCount),
102                                       $groupCount),
103                 // TRANS: "Complete" description for step.
104                 // TRANS: %d is the number of groups imported.
105                 'complete' => sprintf(_m('Imported %d group.',
106                                          'Imported %d groups.',
107                                          $groupCount),
108                                       $groupCount),
109             ),
110             'fetch-messages' => array(
111                 // TRANS: Field label for a Yammer import prepare notices step.
112                 'label' => _m('Prepare public notices for import'),
113                 // TRANS: "In progress" description.
114                 // TRANS: %d is the number of notices to be prepared for import.
115                 'progress' => sprintf(_m('Preparing %d notice...',
116                                          'Preparing %d notices...',
117                                          $fetchedCount),
118                                       $fetchedCount),
119                 // TRANS: "Complete" description for step.
120                 // TRANS: %d is the number of notices prepared for import.
121                 'complete' => sprintf(_m('Prepared %d notice.',
122                                          'Prepared %d notices.',
123                                          $fetchedCount),
124                                       $fetchedCount),
125             ),
126             'save-messages' => array(
127                 // TRANS: Field label for a Yammer import notices step.
128                 'label' => _m('Import public notices'),
129                 // TRANS: "In progress" description.
130                 // TRANS: %d is the number of notices to be imported.
131                 'progress' => sprintf(_m('Importing %d notice...',
132                                          'Importing %d notices...',
133                                          $savedCount),
134                                       $savedCount),
135                 // TRANS: "Complete" description for step.
136                 // TRANS: %d is the number of notices imported.
137                 'complete' => sprintf(_m('Imported %d notice.',
138                                          'Imported %d notices.',
139                                          $savedCount),
140                                       $savedCount),
141             ),
142             'done' => array(
143                 // TRANS: Field label for a Yammer import done step.
144                 'label' => _m('Done'),
145                 // TRANS: "In progress" description for done step.
146                 'progress' => sprintf(_m('Import is complete!')),
147                 // TRANS: "Complete" description for done step.
148                 'complete' => sprintf(_m('Import is complete!')),
149             )
150         );
151         $steps = array_keys($labels);
152         $currentStep = array_search($runner->state(), $steps);
153
154         $classes = array('yammer-import');
155         if ($error) {
156             $classes[] = 'yammer-error';
157         } else {
158             $classes[] = 'yammer-running';
159         }
160         $this->out->elementStart('fieldset', array('class' => implode(' ', $classes)));
161         // TRANS: Fieldset legend.
162         $this->out->element('legend', array(), _m('Import status'));
163         foreach ($steps as $step => $state) {
164             if ($state == 'init') {
165                 // Don't show 'init', it's boring.
166                 continue;
167             }
168             if ($step < $currentStep) {
169                 // This step is done
170                 $this->progressBar($state,
171                                    'complete',
172                                    $labels[$state]['label'],
173                                    $labels[$state]['complete']);
174             } else if ($step == $currentStep) {
175                 // This step is in progress
176                 $this->progressBar($state,
177                                    'progress',
178                                    $labels[$state]['label'],
179                                    $labels[$state]['progress'],
180                                    $error);
181             } else {
182                 // This step has not yet been done.
183                 $this->progressBar($state,
184                                    'waiting',
185                                    $labels[$state]['label'],
186                                    // TRANS: Progress bar status.
187                                    _m('Waiting...'));
188             }
189         }
190         $this->out->elementEnd('fieldset');
191     }
192
193     private function progressBar($state, $class, $label, $status, $error=null)
194     {
195         // @fixme prettify ;)
196         $this->out->elementStart('div', array('class' => "import-step import-step-$state $class"));
197         $this->out->element('div', array('class' => 'import-label'), $label);
198         $this->out->element('div', array('class' => 'import-status'), $status);
199         if ($class == 'progress') {
200             if ($state == 'done') {
201                 // TRANS: Button text for resetting the import state.
202                 $this->out->submit('abort-import', _m('Reset import state'));
203             } else {
204                 if ($error) {
205                     $this->errorBox($error);
206                 } else {
207                     // TRANS: Button text for pausing an import.
208                     $this->out->submit('pause-import', _m('Pause import'));
209                 }
210             }
211         }
212         $this->out->elementEnd('div');
213     }
214
215     private function errorBox($msg)
216     {
217         // TRANS: Error message. %s are the error details.
218         $errline = sprintf(_m('Encountered error "%s".'), $msg);
219         $this->out->elementStart('fieldset', array('class' => 'import-error'));
220         // TRANS: Fieldset legend for a paused import.
221         $this->out->element('legend', array(), _m('Paused'));
222         $this->out->element('p', array(), $errline);
223         // TRANS: Button text for continuing a paused import.
224         $this->out->submit('continue-import', _m('Continue'));
225         // TRANS: Button text for aborting a paused import.
226         $this->out->submit('abort-import', _m('Abort import'));
227         $this->out->elementEnd('fieldset');
228     }
229 }