timelist.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * @category Event
  20. * @package StatusNet
  21. * @author Zach Copley <zach@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  24. * @link http://status.net/
  25. */
  26. if (!defined('STATUSNET')) {
  27. exit(1);
  28. }
  29. /**
  30. * Callback handler to populate end time dropdown
  31. */
  32. class TimelistAction extends Action {
  33. private $start;
  34. private $duration;
  35. /**
  36. * Get ready
  37. *
  38. * @param array $args misc. arguments
  39. *
  40. * @return boolean true
  41. */
  42. function prepare(array $args = array()) {
  43. parent::prepare($args);
  44. $this->start = $this->arg('start');
  45. $this->duration = $this->boolean('duration', false);
  46. return true;
  47. }
  48. /**
  49. * Handle input and ouput something
  50. *
  51. * @param array $args $_REQUEST arguments
  52. *
  53. * @return void
  54. */
  55. function handle()
  56. {
  57. parent::handle();
  58. if (!common_logged_in()) {
  59. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  60. $this->clientError(_m('Not logged in.'));
  61. }
  62. if (!empty($this->start)) {
  63. $times = EventTimeList::getTimes($this->start, $this->duration);
  64. } else {
  65. // TRANS: Client error when submitting a form with unexpected information.
  66. $this->clientError(_m('Unexpected form submission.'));
  67. }
  68. if ($this->boolean('ajax')) {
  69. header('Content-Type: application/json; charset=utf-8');
  70. print json_encode($times);
  71. } else {
  72. // TRANS: Client error displayed when using an action in a non-AJAX way.
  73. $this->clientError(_m('This action is AJAX only.'));
  74. }
  75. }
  76. /**
  77. * Override the regular error handler to show something more
  78. * ajaxy
  79. *
  80. * @param string $msg error message
  81. * @param int $code error code
  82. */
  83. function clientError($msg, $code = 400) {
  84. if ($this->boolean('ajax')) {
  85. header('Content-Type: application/json; charset=utf-8');
  86. print json_encode(
  87. array(
  88. 'success' => false,
  89. 'code' => $code,
  90. 'message' => $msg
  91. )
  92. );
  93. } else {
  94. parent::clientError($msg, $code);
  95. }
  96. }
  97. }