]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/YammerImport/actions/yammeradminpanel.php
Made YammerImport more robust against errors; can now pause/resume/reset the import...
[quix0rs-gnu-social.git] / plugins / YammerImport / actions / yammeradminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Yammer import administration panel
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 class YammeradminpanelAction extends AdminPanelAction
35 {
36     private $runner;
37
38     /**
39      * Returns the page title
40      *
41      * @return string page title
42      */
43     function title()
44     {
45         return _m('Yammer Import');
46     }
47
48     /**
49      * Instructions for using this form.
50      *
51      * @return string instructions
52      */
53     function getInstructions()
54     {
55         return _m('This Yammer import tool is still undergoing testing, ' .
56                   'and is incomplete in some areas. ' .
57                 'Currently user subscriptions and group memberships are not ' .
58                 'transferred; in the future this may be supported for ' .
59                 'imports done by verified administrators on the Yammer side.');
60     }
61
62     function prepare($args)
63     {
64         $ok = parent::prepare($args);
65
66         $this->subaction = $this->trimmed('subaction');
67         $this->runner = YammerRunner::init();
68
69         return $ok;
70     }
71
72     function handle($args)
73     {
74         // @fixme move this to saveSettings and friends?
75         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
76             StatusNet::setApi(true); // short error pages :P
77             $this->checkSessionToken();
78             if ($this->subaction == 'change-apikey') {
79                 $form = new YammerApiKeyForm($this);
80             } else if ($this->subaction == 'apikey') {
81                 if ($this->saveKeys()) {
82                     $form = new YammerAuthInitForm($this, $this->runner);
83                 } else {
84                     $form = new YammerApiKeyForm($this);
85                 }
86             } else if ($this->subaction == 'authinit') {
87                 // hack
88                 if ($this->arg('change-apikey')) {
89                     $form = new YammerApiKeyForm($this);
90                 } else {
91                     $url = $this->runner->requestAuth();
92                     $form = new YammerAuthVerifyForm($this, $this->runner);
93                 }
94             } else if ($this->subaction == 'authverify') {
95                 $this->runner->saveAuthToken($this->trimmed('verify_token'));
96
97                 // Haho! Now we can make THE FUN HAPPEN
98                 $this->runner->startBackgroundImport();
99
100                 $form = new YammerProgressForm($this, $this->runner);
101             } else if ($this->subaction == 'pause-import') {
102                 $this->runner->recordError(_m('Paused from admin panel.'));
103                 $form = $this->statusForm();
104             } else if ($this->subaction == 'continue-import') {
105                 $this->runner->clearError();
106                 $this->runner->startBackgroundImport();
107                 $form = $this->statusForm();
108             } else if ($this->subaction == 'abort-import') {
109                 $this->runner->reset();
110                 $form = $this->statusForm();
111             } else if ($this->subaction == 'progress') {
112                 $form = $this->statusForm();
113             } else {
114                 throw new ClientException('Invalid POST');
115             }
116             return $this->showAjaxForm($form);
117         }
118         return parent::handle($args);
119     }
120
121     function saveKeys()
122     {
123         $key = $this->trimmed('consumer_key');
124         $secret = $this->trimmed('consumer_secret');
125         Config::save('yammer', 'consumer_key', $key);
126         Config::save('yammer', 'consumer_secret', $secret);
127
128         return !empty($key) && !empty($secret);
129     }
130
131     function showAjaxForm($form)
132     {
133         $this->startHTML('text/xml;charset=utf-8');
134         $this->elementStart('head');
135         $this->element('title', null, _m('Yammer import'));
136         $this->elementEnd('head');
137         $this->elementStart('body');
138         $form->show();
139         $this->elementEnd('body');
140         $this->elementEnd('html');
141     }
142
143     /**
144      * Fetch the appropriate form for our current state.
145      * @return Form
146      */
147     function statusForm()
148     {
149         if (!(common_config('yammer', 'consumer_key'))
150             || !(common_config('yammer', 'consumer_secret'))) {
151             return new YammerApiKeyForm($this);
152         }
153         switch($this->runner->state())
154         {
155             case 'init':
156                 return new YammerAuthInitForm($this, $this->runner);
157             case 'requesting-auth':
158                 return new YammerAuthVerifyForm($this, $this->runner);
159             default:
160                 return new YammerProgressForm($this, $this->runner);
161         }
162     }
163
164     /**
165      * Show the Yammer admin panel form
166      *
167      * @return void
168      */
169     function showForm()
170     {
171         $this->elementStart('fieldset');
172         $this->statusForm()->show();
173         $this->elementEnd('fieldset');
174     }
175
176     function showStylesheets()
177     {
178         parent::showStylesheets();
179         $this->cssLink('plugins/YammerImport/css/admin.css', null, 'screen, projection, tv');
180     }
181
182     function showScripts()
183     {
184         parent::showScripts();
185         $this->script('plugins/YammerImport/js/yammer-admin.js');
186     }
187 }