1 | <?php |
||
2 | /** |
||
3 | * Socializer plugin for Craft CMS 3.x |
||
4 | * |
||
5 | * @link https://enupal.com/ |
||
6 | * @copyright Copyright (c) 2019 Enupal LLC |
||
7 | */ |
||
8 | |||
9 | |||
10 | namespace enupal\socializer\controllers; |
||
11 | |||
12 | use Craft; |
||
13 | use craft\elements\Asset; |
||
14 | use craft\helpers\UrlHelper; |
||
15 | use craft\web\Controller as BaseController; |
||
16 | use enupal\socializer\elements\Provider as ProviderElement; |
||
17 | use enupal\socializer\Socializer; |
||
18 | use Hybridauth\Provider\Apple; |
||
19 | use Hybridauth\Provider\Telegram; |
||
20 | use yii\web\NotFoundHttpException; |
||
21 | |||
22 | class ProvidersController extends BaseController |
||
23 | { |
||
24 | /** |
||
25 | * Save a Provider |
||
26 | * |
||
27 | * @return null|\yii\web\Response |
||
28 | * @throws \Exception |
||
29 | * @throws \Throwable |
||
30 | * @throws \yii\web\BadRequestHttpException |
||
31 | */ |
||
32 | public function actionSaveProvider() |
||
33 | { |
||
34 | $this->requirePostRequest(); |
||
35 | |||
36 | $request = Craft::$app->getRequest(); |
||
37 | $provider = new ProviderElement; |
||
38 | |||
39 | $providerId = $request->getBodyParam('providerId'); |
||
40 | |||
41 | if ($providerId) { |
||
42 | $provider = Socializer::$app->providers->getProviderById($providerId); |
||
43 | } |
||
44 | |||
45 | $provider = Socializer::$app->providers->populateProviderFromPost($provider); |
||
46 | |||
47 | // Save it |
||
48 | if (!Socializer::$app->providers->saveProvider($provider)) { |
||
49 | Craft::$app->getSession()->setError(Craft::t('enupal-socializer','Couldn’t save provider')); |
||
50 | |||
51 | Craft::$app->getUrlManager()->setRouteParams([ |
||
52 | 'provider' => $provider |
||
53 | ] |
||
54 | ); |
||
55 | |||
56 | return null; |
||
57 | } |
||
58 | |||
59 | Craft::$app->getSession()->setNotice(Craft::t('enupal-socializer','Provider saved.')); |
||
60 | |||
61 | return $this->redirectToPostedUrl($provider); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Edit a Provider |
||
66 | * |
||
67 | * @param int|null $providerId The button's ID, if editing an existing button. |
||
68 | * @param ProviderElement|null $provider The provider send back by setRouteParams if any errors on saveProvider |
||
69 | * |
||
70 | * @return \yii\web\Response |
||
71 | * @throws NotFoundHttpException |
||
72 | * @throws \Exception |
||
73 | * @throws \Throwable |
||
74 | */ |
||
75 | public function actionEditProvider(int $providerId = null, ProviderElement $provider = null) |
||
76 | { |
||
77 | // Immediately create a new Provider |
||
78 | if ($providerId === null) { |
||
79 | $request = Craft::$app->getRequest(); |
||
80 | $providerType = $request->getRequiredBodyParam("providerType"); |
||
81 | $provider = Socializer::$app->providers->getProviderByType($providerType); |
||
82 | if ($provider){ |
||
83 | throw new \Exception(Craft::t('enupal-socializer','Provider '.$provider->name.' already exists')); |
||
84 | } |
||
85 | $providerName = Socializer::$app->providers->getClassNameFromNamespace($providerType); |
||
86 | $providerHandle = lcfirst($providerName); |
||
87 | $provider = Socializer::$app->providers->createNewProvider($providerName, $providerHandle, $providerType); |
||
88 | |||
89 | if ($provider->id) { |
||
90 | $url = UrlHelper::cpUrl('enupal-socializer/providers/edit/'.$provider->id); |
||
91 | return $this->redirect($url); |
||
92 | } else { |
||
93 | $errors = $provider->getErrors(); |
||
94 | throw new \Exception(Craft::t('enupal-socializer','Error creating the Provider'.json_encode($errors))); |
||
95 | } |
||
96 | } else { |
||
97 | if ($providerId !== null) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
98 | if ($provider === null) { |
||
99 | // Get the provider |
||
100 | $provider = Socializer::$app->providers->getProviderById($providerId); |
||
101 | |||
102 | if (!$provider) { |
||
0 ignored issues
–
show
|
|||
103 | throw new NotFoundHttpException(Craft::t('enupal-socializer','Provider not found')); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if (is_string($provider->fieldMapping)){ |
||
0 ignored issues
–
show
|
|||
110 | $provider->fieldMapping = json_decode($provider->fieldMapping, true); |
||
111 | } |
||
112 | |||
113 | $variables['providerId'] = $providerId; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
114 | $variables['provider'] = $provider; |
||
115 | |||
116 | // Set the "Continue Editing" URL |
||
117 | $variables['continueEditingUrl'] = 'enupal-socializer/providers/edit/{id}'; |
||
118 | $settings = Socializer::$app->settings->getSettings(); |
||
119 | $variables['settings'] = $settings; |
||
120 | $variables['apiDocumentation'] = $this->getApiDocumentation($provider->type); |
||
121 | $variables['callbackUrl'] = Socializer::$app->settings->getCallbackUrl(); |
||
122 | $variables['supportedUserFields'] = Socializer::$app->providers->getUserFieldsAsOptions(); |
||
123 | $variables['userProfileFields'] = Socializer::$app->providers->getUserProfileFieldsAsOptions(); |
||
124 | $variables['fieldMapping'] = $provider->fieldMapping ?? $settings->fieldMapping ?? Socializer::$app->providers->getDefaultFieldMapping(); |
||
125 | $userProfileFields = Socializer::$app->providers->getUserProfileFieldsAsOptions(); |
||
126 | $variables['userProfileFields'] = $userProfileFields; |
||
127 | |||
128 | return $this->renderTemplate('enupal-socializer/providers/_edit', $variables); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Delete a Provider. |
||
133 | * |
||
134 | * @return \yii\web\Response |
||
135 | * @throws \Throwable |
||
136 | * @throws \craft\errors\MissingComponentException |
||
137 | * @throws \yii\web\BadRequestHttpException |
||
138 | */ |
||
139 | public function actionDeleteProvider() |
||
140 | { |
||
141 | $this->requirePostRequest(); |
||
142 | |||
143 | $request = Craft::$app->getRequest(); |
||
144 | |||
145 | $providerId = $request->getRequiredBodyParam('providerId'); |
||
146 | $provider = Socializer::$app->providers->getProviderById($providerId); |
||
147 | |||
148 | // @TODO - handle errors |
||
149 | Socializer::$app->providers->deleteProvider($provider); |
||
150 | |||
151 | Craft::$app->getSession()->setNotice(Craft::t('enupal-socializer','Provider deleted.')); |
||
152 | |||
153 | return $this->redirectToPostedUrl($provider); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param $providerType |
||
158 | * @return mixed |
||
159 | * @throws \ReflectionException |
||
160 | */ |
||
161 | private function getApiDocumentation($providerType) |
||
162 | { |
||
163 | if ($providerType === Apple::class) { |
||
164 | return 'https://developer.apple.com/documentation/sign_in_with_apple'; |
||
165 | } |
||
166 | |||
167 | if ($providerType === Telegram::class) { |
||
168 | return 'https://core.telegram.org/bots'; |
||
169 | } |
||
170 | |||
171 | try { |
||
172 | $reflection = new \ReflectionClass($providerType); |
||
173 | $property = $reflection->getProperty('apiDocumentation'); |
||
174 | $property->setAccessible(true); |
||
175 | $obj = new $providerType(['callback' => 'https://example.com/path/to/script.php',"keys"=>["key" => "ads", "secret"=>"test"]]); |
||
176 | |||
177 | return $property->getValue($obj); |
||
178 | } catch (\Exception $e) { |
||
179 | Craft::error("Error trying to get api documentation: ".$e->getMessage() , __METHOD__); |
||
180 | } |
||
181 | |||
182 | return "No Docs available for {$providerType}"; |
||
183 | } |
||
184 | } |
||
185 |