]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/YammerImport/actions/yammeradminpanel.php
Fix mising require_once (now required here because of rearranging)
[quix0rs-gnu-social.git] / plugins / YammerImport / actions / yammeradminpanel.php
index 12df3c20222903ee377a66ba48221c2000f5d255..4714154290781bb6df6c64aa5f7f8b7e7197b879 100644 (file)
@@ -33,6 +33,8 @@ if (!defined('STATUSNET')) {
 
 class YammeradminpanelAction extends AdminPanelAction
 {
+    private $runner;
+
     /**
      * Returns the page title
      *
@@ -50,172 +52,136 @@ class YammeradminpanelAction extends AdminPanelAction
      */
     function getInstructions()
     {
-        return _m('Yammer import tool');
+        return _m('This Yammer import tool is still undergoing testing, ' .
+                  'and is incomplete in some areas. ' .
+                'Currently user subscriptions and group memberships are not ' .
+                'transferred; in the future this may be supported for ' .
+                'imports done by verified administrators on the Yammer side.');
     }
 
-    /**
-     * Show the Yammer admin panel form
-     *
-     * @return void
-     */
-    function showForm()
+    function prepare($args)
     {
-        $form = new YammerAdminPanelForm($this);
-        $form->show();
-        return;
-    }
+        $ok = parent::prepare($args);
 
-    function showStylesheets()
-    {
-        parent::showStylesheets();
-        $this->cssLink('plugins/YammerImport/css/admin.css', null, 'screen, projection, tv');
+        $this->subaction = $this->trimmed('subaction');
+        $this->runner = YammerRunner::init();
+
+        return $ok;
     }
-}
 
-class YammerAdminPanelForm extends AdminForm
-{
-    /**
-     * ID of the form
-     *
-     * @return string ID of the form
-     */
-    function id()
+    function handle($args)
     {
-        return 'yammeradminpanel';
+        // @fixme move this to saveSettings and friends?
+        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+            StatusNet::setApi(true); // short error pages :P
+            $this->checkSessionToken();
+            if ($this->subaction == 'change-apikey') {
+                $form = new YammerApiKeyForm($this);
+            } else if ($this->subaction == 'apikey') {
+                if ($this->saveKeys()) {
+                    $form = new YammerAuthInitForm($this, $this->runner);
+                } else {
+                    $form = new YammerApiKeyForm($this);
+                }
+            } else if ($this->subaction == 'authinit') {
+                // hack
+                if ($this->arg('change-apikey')) {
+                    $form = new YammerApiKeyForm($this);
+                } else {
+                    $url = $this->runner->requestAuth();
+                    $form = new YammerAuthVerifyForm($this, $this->runner);
+                }
+            } else if ($this->subaction == 'authverify') {
+                $this->runner->saveAuthToken($this->trimmed('verify_token'));
+
+                // Haho! Now we can make THE FUN HAPPEN
+                $this->runner->startBackgroundImport();
+
+                $form = new YammerProgressForm($this, $this->runner);
+            } else if ($this->subaction == 'pause-import') {
+                $this->runner->recordError(_m('Paused from admin panel.'));
+                $form = $this->statusForm();
+            } else if ($this->subaction == 'continue-import') {
+                $this->runner->clearError();
+                $this->runner->startBackgroundImport();
+                $form = $this->statusForm();
+            } else if ($this->subaction == 'abort-import') {
+                $this->runner->reset();
+                $form = $this->statusForm();
+            } else if ($this->subaction == 'progress') {
+                $form = $this->statusForm();
+            } else {
+                throw new ClientException('Invalid POST');
+            }
+            return $this->showAjaxForm($form);
+        }
+        return parent::handle($args);
     }
 
-    /**
-     * class of the form
-     *
-     * @return string class of the form
-     */
-    function formClass()
+    function saveKeys()
     {
-        return 'form_settings';
+        $key = $this->trimmed('consumer_key');
+        $secret = $this->trimmed('consumer_secret');
+        Config::save('yammer', 'consumer_key', $key);
+        Config::save('yammer', 'consumer_secret', $secret);
+
+        return !empty($key) && !empty($secret);
     }
 
-    /**
-     * Action of the form
-     *
-     * @return string URL of the action
-     */
-    function action()
+    function showAjaxForm($form)
     {
-        return common_local_url('yammeradminpanel');
+        $this->startHTML('text/xml;charset=utf-8');
+        $this->elementStart('head');
+        $this->element('title', null, _m('Yammer import'));
+        $this->elementEnd('head');
+        $this->elementStart('body');
+        $form->show();
+        $this->elementEnd('body');
+        $this->elementEnd('html');
     }
 
     /**
-     * Data elements of the form
-     *
-     * @return void
+     * Fetch the appropriate form for our current state.
+     * @return Form
      */
-    function formData()
+    function statusForm()
     {
-        $runner = YammerRunner::init();
-
-        switch($runner->state())
+        if (!(common_config('yammer', 'consumer_key'))
+            || !(common_config('yammer', 'consumer_secret'))) {
+            return new YammerApiKeyForm($this);
+        }
+        switch($this->runner->state())
         {
             case 'init':
+                return new YammerAuthInitForm($this, $this->runner);
             case 'requesting-auth':
-                $this->showAuthForm();
+                return new YammerAuthVerifyForm($this, $this->runner);
             default:
+                return new YammerProgressForm($this, $this->runner);
         }
-        $this->showImportState($runner);
     }
 
-    private function showAuthForm()
-    {
-        $this->out->element('p', array(), 'show an auth form');
-    }
-
-    private function showImportState(YammerRunner $runner)
+    /**
+     * Show the Yammer admin panel form
+     *
+     * @return void
+     */
+    function showForm()
     {
-        $userCount = $runner->countUsers();
-        $groupCount = $runner->countGroups();
-        $fetchedCount = $runner->countFetchedNotices();
-        $savedCount = $runner->countSavedNotices();
-
-        $labels = array(
-            'init' => array(
-                'label' => _m("Initialize"),
-                'progress' => _m('No import running'),
-                'complete' => _m('Initiated Yammer server connection...'),
-            ),
-            'requesting-auth' => array(
-                'label' => _m('Connect to Yammer'),
-                'progress' => _m('Awaiting authorization...'),
-                'complete' => _m('Connected.'),
-            ),
-            'import-users' => array(
-                'label' => _m('Import user accounts'),
-                'progress' => sprintf(_m("Importing %d user...", "Importing %d users...", $userCount), $userCount),
-                'complete' => sprintf(_m("Imported %d user.", "Imported %d users.", $userCount), $userCount),
-            ),
-            'import-groups' => array(
-                'label' => _m('Import user groups'),
-                'progress' => sprintf(_m("Importing %d group...", "Importing %d groups...", $groupCount), $groupCount),
-                'complete' => sprintf(_m("Imported %d group.", "Imported %d groups.", $groupCount), $groupCount),
-            ),
-            'fetch-messages' => array(
-                'label' => _m('Prepare public notices for import'),
-                'progress' => sprintf(_m("Preparing %d notice...", "Preparing %d notices...", $fetchedCount), $fetchedCount),
-                'complete' => sprintf(_m("Prepared %d notice.", "Prepared %d notices.", $fetchedCount), $fetchedCount),
-            ),
-            'save-messages' => array(
-                'label' => _m('Import public notices'),
-                'progress' => sprintf(_m("Importing %d notice...", "Importing %d notices...", $savedCount), $savedCount),
-                'complete' => sprintf(_m("Imported %d notice.", "Imported %d notices.", $savedCount), $savedCount),
-            ),
-            'done' => array(
-                'label' => _m('Done'),
-                'progress' => sprintf(_m("Import is complete!")),
-                'complete' => sprintf(_m("Import is complete!")),
-            )
-        );
-        $steps = array_keys($labels);
-        $currentStep = array_search($runner->state(), $steps);
-
-        $this->out->elementStart('div', array('class' => 'yammer-import'));
-        foreach ($steps as $step => $state) {
-            if ($step < $currentStep) {
-                // This step is done
-                $this->progressBar($state,
-                                   'complete',
-                                   $labels[$state]['label'],
-                                   $labels[$state]['complete']);
-            } else if ($step == $currentStep) {
-                // This step is in progress
-                $this->progressBar($state,
-                                   'progress',
-                                   $labels[$state]['label'],
-                                   $labels[$state]['progress']);
-            } else {
-                // This step has not yet been done.
-                $this->progressBar($state,
-                                   'waiting',
-                                   $labels[$state]['label'],
-                                   _m("Waiting..."));
-            }
-        }
-        $this->out->elementEnd('div');
+        $this->elementStart('fieldset');
+        $this->statusForm()->show();
+        $this->elementEnd('fieldset');
     }
 
-    private function progressBar($state, $class, $label, $status)
+    function showStylesheets()
     {
-        // @fixme prettify ;)
-        $this->out->elementStart('div', array('class' => "import-step import-step-$state $class"));
-        $this->out->element('div', array('class' => 'import-label'), $label);
-        $this->out->element('div', array('class' => 'import-status'), $status);
-        $this->out->elementEnd('div');
+        parent::showStylesheets();
+        $this->cssLink(Plugin::staticPath('YammerImport', 'css/admin.css'), null, 'screen, projection, tv');
     }
 
-    /**
-     * Action elements
-     *
-     * @return void
-     */
-    function formActions()
+    function showScripts()
     {
-        // No submit buttons needed at bottom
+        parent::showScripts();
+        $this->script(Plugin::staticPath('YammerImport', 'js/yammer-admin.js'));
     }
 }