yammerprogress.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. class YammerProgressForm extends Form
  3. {
  4. /**
  5. * ID of the form
  6. *
  7. * @return string ID of the form
  8. */
  9. function id()
  10. {
  11. return 'yammer-progress-form';
  12. }
  13. /**
  14. * class of the form
  15. *
  16. * @return string class of the form
  17. */
  18. function formClass()
  19. {
  20. $classes = array('form_settings');
  21. $runner = YammerRunner::init();
  22. if ($runner->lastError()) {
  23. $classes[] = 'import-error';
  24. } else if ($runner->state() == 'done') {
  25. $classes[] = 'import-done';
  26. } else {
  27. $classes[] = 'import-progress';
  28. }
  29. return implode(' ', $classes);
  30. }
  31. /**
  32. * Action of the form
  33. *
  34. * @return string URL of the action
  35. */
  36. function action()
  37. {
  38. return common_local_url('yammeradminpanel');
  39. }
  40. /**
  41. * Data elements of the form
  42. *
  43. * @return void
  44. */
  45. function formData()
  46. {
  47. $this->out->hidden('subaction', 'progress');
  48. $runner = YammerRunner::init();
  49. $error = $runner->lastError();
  50. $userCount = $runner->countUsers();
  51. $groupCount = $runner->countGroups();
  52. $fetchedCount = $runner->countFetchedNotices();
  53. $savedCount = $runner->countSavedNotices();
  54. $labels = array(
  55. 'init' => array(
  56. // TRANS: Field label for a Yammer import initialise step.
  57. 'label' => _m('Initialize'),
  58. // TRANS: "In progress" description.
  59. 'progress' => _m('No import running'),
  60. // TRANS: "Complete" description for initialize state.
  61. 'complete' => _m('Initiated Yammer server connection...'),
  62. ),
  63. 'requesting-auth' => array(
  64. // TRANS: Field label for a Yammer import connect step.
  65. 'label' => _m('Connect to Yammer'),
  66. // TRANS: "In progress" description.
  67. 'progress' => _m('Awaiting authorization...'),
  68. // TRANS: "Complete" description for connect state.
  69. 'complete' => _m('Connected.'),
  70. ),
  71. 'import-users' => array(
  72. // TRANS: Field label for a Yammer user import users step.
  73. 'label' => _m('Import user accounts'),
  74. // TRANS: "In progress" description.
  75. // TRANS: %d is the number of users to be imported.
  76. 'progress' => sprintf(_m('Importing %d user...',
  77. 'Importing %d users...',
  78. $userCount),
  79. $userCount),
  80. // TRANS: "Complete" description for step.
  81. // TRANS: %d is the number of users imported.
  82. 'complete' => sprintf(_m('Imported %d user.',
  83. 'Imported %d users.',
  84. $userCount),
  85. $userCount),
  86. ),
  87. 'import-groups' => array(
  88. // TRANS: Field label for a Yammer group import step.
  89. 'label' => _m('Import user groups'),
  90. // TRANS: "In progress" description.
  91. // TRANS: %d is the number of groups to be imported.
  92. 'progress' => sprintf(_m('Importing %d group...',
  93. 'Importing %d groups...',
  94. $groupCount),
  95. $groupCount),
  96. // TRANS: "Complete" description for step.
  97. // TRANS: %d is the number of groups imported.
  98. 'complete' => sprintf(_m('Imported %d group.',
  99. 'Imported %d groups.',
  100. $groupCount),
  101. $groupCount),
  102. ),
  103. 'fetch-messages' => array(
  104. // TRANS: Field label for a Yammer import prepare notices step.
  105. 'label' => _m('Prepare public notices for import'),
  106. // TRANS: "In progress" description.
  107. // TRANS: %d is the number of notices to be prepared for import.
  108. 'progress' => sprintf(_m('Preparing %d notice...',
  109. 'Preparing %d notices...',
  110. $fetchedCount),
  111. $fetchedCount),
  112. // TRANS: "Complete" description for step.
  113. // TRANS: %d is the number of notices prepared for import.
  114. 'complete' => sprintf(_m('Prepared %d notice.',
  115. 'Prepared %d notices.',
  116. $fetchedCount),
  117. $fetchedCount),
  118. ),
  119. 'save-messages' => array(
  120. // TRANS: Field label for a Yammer import notices step.
  121. 'label' => _m('Import public notices'),
  122. // TRANS: "In progress" description.
  123. // TRANS: %d is the number of notices to be imported.
  124. 'progress' => sprintf(_m('Importing %d notice...',
  125. 'Importing %d notices...',
  126. $savedCount),
  127. $savedCount),
  128. // TRANS: "Complete" description for step.
  129. // TRANS: %d is the number of notices imported.
  130. 'complete' => sprintf(_m('Imported %d notice.',
  131. 'Imported %d notices.',
  132. $savedCount),
  133. $savedCount),
  134. ),
  135. 'done' => array(
  136. // TRANS: Field label for a Yammer import done step.
  137. 'label' => _m('Done'),
  138. // TRANS: "In progress" description for done step.
  139. 'progress' => sprintf(_m('Import is complete!')),
  140. // TRANS: "Complete" description for done step.
  141. 'complete' => sprintf(_m('Import is complete!')),
  142. )
  143. );
  144. $steps = array_keys($labels);
  145. $currentStep = array_search($runner->state(), $steps);
  146. $classes = array('yammer-import');
  147. if ($error) {
  148. $classes[] = 'yammer-error';
  149. } else {
  150. $classes[] = 'yammer-running';
  151. }
  152. $this->out->elementStart('fieldset', array('class' => implode(' ', $classes)));
  153. // TRANS: Fieldset legend.
  154. $this->out->element('legend', array(), _m('Import status'));
  155. foreach ($steps as $step => $state) {
  156. if ($state == 'init') {
  157. // Don't show 'init', it's boring.
  158. continue;
  159. }
  160. if ($step < $currentStep) {
  161. // This step is done
  162. $this->progressBar($state,
  163. 'complete',
  164. $labels[$state]['label'],
  165. $labels[$state]['complete']);
  166. } else if ($step == $currentStep) {
  167. // This step is in progress
  168. $this->progressBar($state,
  169. 'progress',
  170. $labels[$state]['label'],
  171. $labels[$state]['progress'],
  172. $error);
  173. } else {
  174. // This step has not yet been done.
  175. $this->progressBar($state,
  176. 'waiting',
  177. $labels[$state]['label'],
  178. // TRANS: Progress bar status.
  179. _m('Waiting...'));
  180. }
  181. }
  182. $this->out->elementEnd('fieldset');
  183. }
  184. private function progressBar($state, $class, $label, $status, $error=null)
  185. {
  186. // @fixme prettify ;)
  187. $this->out->elementStart('div', array('class' => "import-step import-step-$state $class"));
  188. $this->out->element('div', array('class' => 'import-label'), $label);
  189. $this->out->element('div', array('class' => 'import-status'), $status);
  190. if ($class == 'progress') {
  191. if ($state == 'done') {
  192. // TRANS: Button text for resetting the import state.
  193. $this->out->submit('abort-import', _m('Reset import state'));
  194. } else {
  195. if ($error) {
  196. $this->errorBox($error);
  197. } else {
  198. // TRANS: Button text for pausing an import.
  199. $this->out->submit('pause-import', _m('Pause import'));
  200. }
  201. }
  202. }
  203. $this->out->elementEnd('div');
  204. }
  205. private function errorBox($msg)
  206. {
  207. // TRANS: Error message. %s are the error details.
  208. $errline = sprintf(_m('Encountered error "%s".'), $msg);
  209. $this->out->elementStart('fieldset', array('class' => 'import-error'));
  210. // TRANS: Fieldset legend for a paused import.
  211. $this->out->element('legend', array(), _m('Paused'));
  212. $this->out->element('p', array(), $errline);
  213. // TRANS: Button text for continuing a paused import.
  214. $this->out->submit('continue-import', _m('Continue'));
  215. // TRANS: Button text for aborting a paused import.
  216. $this->out->submit('abort-import', _m('Abort import'));
  217. $this->out->elementEnd('fieldset');
  218. }
  219. }