]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/YammerImport/actions/yammeradminpanel.php
Merge remote-tracking branch 'upstream/master' into social-master
[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         // TRANS: Page title for Yammer import administration panel.
46         return _m('Yammer Import');
47     }
48
49     /**
50      * Instructions for using this form.
51      *
52      * @return string instructions
53      */
54     function getInstructions()
55     {
56         // TRANS: Instructions for Yammer import administration panel.
57         return _m('This Yammer import tool is still undergoing testing, ' .
58                   'and is incomplete in some areas. ' .
59                 'Currently user subscriptions and group memberships are not ' .
60                 'transferred; in the future this may be supported for ' .
61                 'imports done by verified administrators on the Yammer side.');
62     }
63
64     function prepare(array $args=array())
65     {
66         $ok = parent::prepare($args);
67
68         $this->subaction = $this->trimmed('subaction');
69         $this->runner = YammerRunner::init();
70
71         return $ok;
72     }
73
74     function handle(array $args=array())
75     {
76         // @fixme move this to saveSettings and friends?
77         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
78             StatusNet::setApi(true); // short error pages :P
79             $this->checkSessionToken();
80             if ($this->subaction == 'change-apikey') {
81                 $form = new YammerApiKeyForm($this);
82             } else if ($this->subaction == 'apikey') {
83                 if ($this->saveKeys()) {
84                     $form = new YammerAuthInitForm($this, $this->runner);
85                 } else {
86                     $form = new YammerApiKeyForm($this);
87                 }
88             } else if ($this->subaction == 'authinit') {
89                 // hack
90                 if ($this->arg('change-apikey')) {
91                     $form = new YammerApiKeyForm($this);
92                 } else {
93                     $url = $this->runner->requestAuth();
94                     $form = new YammerAuthVerifyForm($this, $this->runner);
95                 }
96             } else if ($this->subaction == 'authverify') {
97                 $this->runner->saveAuthToken($this->trimmed('verify_token'));
98
99                 // Haho! Now we can make THE FUN HAPPEN
100                 $this->runner->startBackgroundImport();
101
102                 $form = new YammerProgressForm($this, $this->runner);
103             } else if ($this->subaction == 'pause-import') {
104                 // TRANS: Error message about an import job being paused from the admin panel.
105                 $this->runner->recordError(_m('Paused from admin panel.'));
106                 $form = $this->statusForm();
107             } else if ($this->subaction == 'continue-import') {
108                 $this->runner->clearError();
109                 $this->runner->startBackgroundImport();
110                 $form = $this->statusForm();
111             } else if ($this->subaction == 'abort-import') {
112                 $this->runner->reset();
113                 $form = $this->statusForm();
114             } else if ($this->subaction == 'progress') {
115                 $form = $this->statusForm();
116             } else {
117                 // TRANS: Client exception thrown when encountering an unhandled sub action.
118                 throw new ClientException(_m('Invalid POST'));
119             }
120             return $this->showAjaxForm($form);
121         }
122         return parent::handle($args);
123     }
124
125     function saveKeys()
126     {
127         $key = $this->trimmed('consumer_key');
128         $secret = $this->trimmed('consumer_secret');
129         Config::save('yammer', 'consumer_key', $key);
130         Config::save('yammer', 'consumer_secret', $secret);
131
132         return !empty($key) && !empty($secret);
133     }
134
135     function showAjaxForm($form)
136     {
137         $this->startHTML('text/xml;charset=utf-8');
138         $this->elementStart('head');
139         // TRANS: Page title for Yammer import administration panel.
140         $this->element('title', null, _m('Yammer import'));
141         $this->elementEnd('head');
142         $this->elementStart('body');
143         $form->show();
144         $this->elementEnd('body');
145         $this->endHTML();
146     }
147
148     /**
149      * Fetch the appropriate form for our current state.
150      * @return Form
151      */
152     function statusForm()
153     {
154         if (!(common_config('yammer', 'consumer_key'))
155             || !(common_config('yammer', 'consumer_secret'))) {
156             return new YammerApiKeyForm($this);
157         }
158         switch($this->runner->state())
159         {
160             case 'init':
161                 return new YammerAuthInitForm($this, $this->runner);
162             case 'requesting-auth':
163                 return new YammerAuthVerifyForm($this, $this->runner);
164             default:
165                 return new YammerProgressForm($this, $this->runner);
166         }
167     }
168
169     /**
170      * Show the Yammer admin panel form
171      *
172      * @return void
173      */
174     function showForm()
175     {
176         $this->elementStart('fieldset');
177         $this->statusForm()->show();
178         $this->elementEnd('fieldset');
179     }
180
181     function showStylesheets()
182     {
183         parent::showStylesheets();
184         $this->cssLink(Plugin::staticPath('YammerImport', 'css/admin.css'), null, 'screen, projection, tv');
185     }
186
187     function showScripts()
188     {
189         parent::showScripts();
190         $this->script(Plugin::staticPath('YammerImport', 'js/yammer-admin.js'));
191     }
192 }