ajax_controller.class.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }