SproutCMS

This is the code documentation for the SproutCMS project

source of /modules/HarborAndSprout/Controllers/HarborController.php

  1. <?php
  2. namespace SproutModules\Karmabunny\HarborAndSprout\Controllers;
  3.  
  4. use Exception;
  5. use LogicException;
  6. use SproutModules\Karmabunny\HarborAndSprout\Helpers\GoogleDriveApi;
  7. use SproutModules\Karmabunny\HarborAndSprout\Helpers\ShopifyApi;
  8. use Sprout\Controllers\Controller;
  9. use Sprout\Helpers\AdminAuth;
  10. use Sprout\Helpers\Notification;
  11. use Sprout\Helpers\Url;
  12. use Sprout\Helpers\View;
  13.  
  14.  
  15. class HarborController extends Controller
  16. {
  17. /**
  18.   * Render Shopify API test form
  19.   * Viewable from within dbtools
  20.   *
  21.   * @return void Echos HTML directly
  22.   */
  23. public function shopifyApiForm()
  24. {
  25. $view = new View('modules/HarborAndSprout/admin/shopify_api_form');
  26. $view->methods = ShopifyApi::$methods;
  27. $view->output = '';
  28.  
  29. if (!empty($_POST)) {
  30. $params = !empty($_POST['extra']) ? json_decode($_POST['extra'], true) : [];
  31. $view->output = print_r(json_decode(ShopifyApi::request($_POST['method'], $_POST['endpoint'], $params), true), true);
  32. }
  33.  
  34. echo $view->render();
  35. }
  36.  
  37.  
  38. /**
  39.   * Render Google Drive API test form
  40.   * Viewable from within dbtools
  41.   *
  42.   * @return void Echos HTML directly
  43.   */
  44. public function driveApiForm()
  45. {
  46. $view = new View('modules/HarborAndSprout/admin/drive_api_form');
  47. $view->output = '';
  48. $view->connected = (bool) GoogleDriveApi::getToken();
  49.  
  50. if (!empty($_POST)) {
  51. try
  52. {
  53. $params = !empty($_POST['extra']) ? json_decode($_POST['extra'], true) : [];
  54.  
  55. $resp = GoogleDriveApi::apiRequest($_POST['method'], GoogleDriveApi::$domain . $_POST['endpoint'], $params);
  56. $view->output = print_r($resp, true);
  57. }
  58. catch (Exception $ex)
  59. {
  60. $view->output = $ex->getMessage();
  61. }
  62. }
  63.  
  64. if (empty($_POST['endpoint'])) $_POST['endpoint'] = '/files';
  65.  
  66. echo $view->render();
  67. }
  68.  
  69.  
  70. /**
  71.   * Render Google OAuth form
  72.   *
  73.   * @return void Redirects
  74.   */
  75. public function googleAuthConnect()
  76. {
  77. AdminAuth::checkLogin();
  78. GoogleDriveApi::requestOauthCode();
  79. }
  80.  
  81.  
  82. /**
  83.   * Handle Oauth2 callback. Saves Oauth2 token
  84.   *
  85.   * @return void Redirects
  86.   */
  87. public function googleAuthAction()
  88. {
  89. AdminAuth::checkLogin();
  90.  
  91. if (isset($_GET['error'])) {
  92. Notification::error(sprintf('Authentication error: %s', $_GET['error']));
  93. } else {
  94. Notification::confirm('Connected');
  95. GoogleDriveApi::saveOauthToken($_GET['code']);
  96. }
  97.  
  98. Url::redirect('dbtools');
  99. }
  100. }
  101.