dukt /
facebook
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @link https://dukt.net/facebook/ |
||
| 4 | * @copyright Copyright (c) Dukt |
||
| 5 | * @license https://github.com/dukt/facebook/blob/master/LICENSE.md |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace dukt\facebook\controllers; |
||
| 9 | |||
| 10 | use Craft; |
||
| 11 | use craft\web\Controller; |
||
| 12 | use dukt\facebook\Plugin as Facebook; |
||
| 13 | use yii\web\Response; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Class SettingsController |
||
| 17 | * |
||
| 18 | * @author Dukt <[email protected]> |
||
| 19 | * @since 2.0 |
||
| 20 | */ |
||
| 21 | class SettingsController extends Controller |
||
| 22 | { |
||
| 23 | // Public Methods |
||
| 24 | // ========================================================================= |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Settings index. |
||
| 28 | * |
||
| 29 | * @return Response |
||
| 30 | */ |
||
| 31 | public function actionIndex(): Response |
||
| 32 | { |
||
| 33 | try { |
||
| 34 | $token = Facebook::$plugin->getOauth()->getToken(); |
||
| 35 | |||
| 36 | if ($token !== null) { |
||
| 37 | $account = Facebook::$plugin->getCache()->get(['getResourceOwner', $token]); |
||
| 38 | |||
| 39 | if (!$account) { |
||
| 40 | $provider = Facebook::$plugin->getOauth()->getOauthProvider(); |
||
| 41 | $account = $provider->getResourceOwner($token); |
||
| 42 | Facebook::$plugin->getCache()->set(['getResourceOwner', $token], $account); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | } catch (\Exception $exception) { |
||
| 46 | Craft::info("Couldn't get account\r\n".$exception->getMessage().'\r\n'.$exception->getTraceAsString(), __METHOD__); |
||
| 47 | |||
| 48 | if (method_exists($exception, 'getResponse')) { |
||
| 49 | Craft::info("GuzzleErrorResponse\r\n".$exception->getResponse(), __METHOD__); |
||
| 50 | } |
||
| 51 | |||
| 52 | $error = $exception->getMessage(); |
||
| 53 | } |
||
| 54 | |||
| 55 | $plugin = Craft::$app->getPlugins()->getPlugin('facebook'); |
||
| 56 | |||
| 57 | return $this->renderTemplate('facebook/settings/index', [ |
||
| 58 | 'account' => ($account ?? null), |
||
| 59 | 'token' => ($token ?? null), |
||
| 60 | 'error' => ($error ?? null), |
||
| 61 | 'settings' => $plugin->getSettings(), |
||
| 62 | 'redirectUri' => Facebook::$plugin->getOauth()->getRedirectUri(), |
||
| 63 | ]); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * OAuth settings. |
||
| 68 | * |
||
| 69 | * @return Response |
||
| 70 | */ |
||
| 71 | public function actionOauth(): Response |
||
| 72 | { |
||
| 73 | $plugin = Craft::$app->getPlugins()->getPlugin('facebook'); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 74 | |||
| 75 | return $this->renderTemplate('facebook/settings/oauth', [ |
||
| 76 | 'redirectUri' => Facebook::$plugin->getOauth()->getRedirectUri(), |
||
| 77 | 'oauthClientId' => Facebook::$plugin->getClientId(false), |
||
| 78 | 'oauthClientSecret' => Facebook::$plugin->getClientSecret(false), |
||
| 79 | ]); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Save OAuth settings. |
||
| 84 | * |
||
| 85 | * @return Response |
||
| 86 | * @throws \yii\web\BadRequestHttpException |
||
| 87 | */ |
||
| 88 | public function actionSaveOauthSettings(): Response |
||
| 89 | { |
||
| 90 | $plugin = Craft::$app->getPlugins()->getPlugin('facebook'); |
||
| 91 | $settings = $plugin->getSettings(); |
||
| 92 | |||
| 93 | $postSettings = Craft::$app->getRequest()->getBodyParam('settings'); |
||
| 94 | |||
| 95 | $settings['oauthClientId'] = $postSettings['oauthClientId']; |
||
| 96 | $settings['oauthClientSecret'] = $postSettings['oauthClientSecret']; |
||
| 97 | |||
| 98 | Craft::$app->getPlugins()->savePluginSettings($plugin, $settings->getAttributes()); |
||
| 99 | |||
| 100 | Craft::$app->getSession()->setNotice(Craft::t('facebook', 'OAuth settings saved.')); |
||
| 101 | |||
| 102 | return $this->redirectToPostedUrl(); |
||
| 103 | } |
||
| 104 | } |
||
| 105 |