ajax_controller.class.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Definition of the AddManySessionToCategoryFunctions class
  5. * @package chamilo.library
  6. */
  7. /**
  8. * Ajax controller. Dispatch request and perform required action.
  9. *
  10. *
  11. * Usage:
  12. *
  13. * $controller = AjaxController::instance();
  14. * $controller->run();
  15. *
  16. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
  17. * @license /license.txt
  18. */
  19. class AjaxController extends \Controller
  20. {
  21. /**
  22. * Returns an HTML error message for forbidden access
  23. * @return bool|void
  24. * @assert () === null
  25. */
  26. function forbidden()
  27. {
  28. $this->response(false, get_lang('YouAreNotAuthorized'));
  29. }
  30. /**
  31. * Returns an HTML error message for unknown action
  32. * @return bool|void
  33. * @assert () === null
  34. */
  35. public function unknown()
  36. {
  37. $this->response(false, get_lang('UnknownAction'));
  38. }
  39. /**
  40. * Action exists but implementation is missing.
  41. * @return bool|void
  42. * @assert () === null
  43. */
  44. public function missing()
  45. {
  46. $this->response(false, get_lang('NoImplementation'));
  47. }
  48. /**
  49. * Display a standard json responce.
  50. *
  51. * @param bool $success
  52. * @param string $message
  53. * @param object $data
  54. * @return bool|void
  55. * @assert () === null
  56. */
  57. public function response($success = false, $message = '', $data = null)
  58. {
  59. $message = trim($message);
  60. $response = (object) array();
  61. $response->success = $success;
  62. if ($message) {
  63. $response->message = Display::return_message($message, $success ? 'normal' : 'error');
  64. } else {
  65. $response->message = '';
  66. }
  67. $response->data = $data;
  68. $this->render_json($response);
  69. }
  70. }