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