yammeradminpanel.php 6.4 KB

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