|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://dukt.net/social/ |
|
4
|
|
|
* @copyright Copyright (c) 2018, Dukt |
|
5
|
|
|
* @license https://github.com/dukt/social/blob/v2/LICENSE.md |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dukt\social\controllers; |
|
9
|
|
|
|
|
10
|
|
|
use Craft; |
|
11
|
|
|
use craft\web\Controller; |
|
12
|
|
|
use dukt\social\Plugin; |
|
13
|
|
|
use yii\web\Response; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The SettingsController class is a controller that handles various settings related tasks. |
|
17
|
|
|
* |
|
18
|
|
|
* Note that all actions in the controller require an authenticated Craft session via [[allowAnonymous]]. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Dukt <[email protected]> |
|
21
|
|
|
* @since 1.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class SettingsController extends Controller |
|
24
|
|
|
{ |
|
25
|
|
|
// Public Methods |
|
26
|
|
|
// ========================================================================= |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* General settings. |
|
30
|
|
|
* |
|
31
|
|
|
* @return Response |
|
32
|
|
|
* @throws \yii\base\InvalidConfigException |
|
33
|
|
|
*/ |
|
34
|
|
|
public function actionSettings(): Response |
|
35
|
|
|
{ |
|
36
|
|
|
if (Craft::$app->getEdition() !== Craft::Pro) { |
|
37
|
|
|
return $this->renderTemplate('social/settings/_pro-requirement'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$plugin = Craft::$app->getPlugins()->getPlugin('social'); |
|
41
|
|
|
$variables['settings'] = $plugin->getSettings(); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
$accounts = Plugin::getInstance()->getLoginAccounts()->getLoginAccounts(); |
|
44
|
|
|
$variables['totalAccounts'] = count($accounts); |
|
45
|
|
|
|
|
46
|
|
|
return $this->renderTemplate('social/settings/settings', $variables); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Saves the settings. |
|
51
|
|
|
* |
|
52
|
|
|
* @return null|Response |
|
53
|
|
|
* @throws \yii\web\BadRequestHttpException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function actionSaveSettings() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->requirePostRequest(); |
|
58
|
|
|
$settings = Craft::$app->getRequest()->getBodyParam('settings', []); |
|
59
|
|
|
$plugin = Craft::$app->getPlugins()->getPlugin('social'); |
|
60
|
|
|
|
|
61
|
|
|
if ($plugin === null) { |
|
62
|
|
|
throw new NotFoundHttpException('Plugin not found'); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!Plugin::getInstance()->savePluginSettings($settings, $plugin)) { |
|
|
|
|
|
|
66
|
|
|
Craft::$app->getSession()->setError(Craft::t('app', 'Couldn’t save plugin settings.')); |
|
67
|
|
|
|
|
68
|
|
|
// Send the plugin back to the template |
|
69
|
|
|
Craft::$app->getUrlManager()->setRouteParams([ |
|
70
|
|
|
'plugin' => $plugin |
|
71
|
|
|
]); |
|
72
|
|
|
|
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('app', 'Plugin settings saved.')); |
|
77
|
|
|
|
|
78
|
|
|
return $this->redirectToPostedUrl(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|