| 1 | <?php |
||
| 2 | |||
| 3 | namespace Poptin\Silverstripe; |
||
| 4 | |||
| 5 | use SilverStripe\Admin\LeftAndMain; |
||
| 6 | use SilverStripe\View\Requirements; |
||
| 7 | use Silverstripe\SiteConfig\SiteConfig; |
||
| 8 | use SilverStripe\Control\HTTPRequest; |
||
| 9 | use SilverStripe\Security\Security; |
||
| 10 | |||
| 11 | class Poptin extends LeftAndMain |
||
| 12 | { |
||
| 13 | private static $allowed_actions = array('setConfig', 'deleteConfig', 'redirectToDashboard'); |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 14 | private static $menu_icon_class = "icon menu-icon poptin"; |
||
|
0 ignored issues
–
show
|
|||
| 15 | private static $url_segment = "poptin"; |
||
|
0 ignored issues
–
show
|
|||
| 16 | private static $menu_title = "Poptin"; |
||
|
0 ignored issues
–
show
|
|||
| 17 | |||
| 18 | public function init() |
||
| 19 | { |
||
| 20 | parent::init(); |
||
| 21 | Requirements::css('poptin/silverstripe-popups-forms:client/dist/css/bootstrap.min.css'); |
||
| 22 | Requirements::css('poptin/silverstripe-popups-forms:client/dist/css/poptin-admin.css'); |
||
| 23 | Requirements::javascript('https://code.jquery.com/jquery-3.5.1.min.js'); |
||
| 24 | Requirements::javascript('poptin/silverstripe-popups-forms:client/dist/js/bootstrap.min.js'); |
||
| 25 | Requirements::javascript('poptin/silverstripe-popups-forms:client/dist/js/poptin-admin.js'); |
||
| 26 | } |
||
| 27 | |||
| 28 | public function csrf_token() |
||
| 29 | { |
||
| 30 | return hash('sha512', 'poptin-fe-login'); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function currentUserEmail() |
||
| 34 | { |
||
| 35 | $member = Security::getCurrentUser(); |
||
| 36 | return $member->Email; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function poptinidcheck() |
||
| 40 | { |
||
| 41 | $config = SiteConfig::current_site_config(); |
||
| 42 | $poptinConfig = json_decode($config->PoptinConfig, true); |
||
| 43 | |||
| 44 | if ($poptinConfig['client_id']) { |
||
| 45 | return true; |
||
| 46 | } |
||
| 47 | |||
| 48 | return false; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function setConfig(HTTPRequest $request) |
||
| 52 | { |
||
| 53 | $config = SiteConfig::current_site_config(); |
||
| 54 | |||
| 55 | $body = $request->requestVars(); |
||
| 56 | |||
| 57 | $poptinConfig = json_decode($config->PoptinConfig, true); |
||
| 58 | |||
| 59 | if (array_key_exists('client_id', $body)) { |
||
| 60 | $poptinConfig['client_id'] = $body['client_id']; |
||
| 61 | } |
||
| 62 | |||
| 63 | if (array_key_exists('user_id', $body)) { |
||
| 64 | $poptinConfig['user_id'] = $body['user_id']; |
||
| 65 | } |
||
| 66 | |||
| 67 | if (array_key_exists('token', $body)) { |
||
| 68 | $poptinConfig['token'] = $body['token']; |
||
| 69 | } |
||
| 70 | |||
| 71 | $config->PoptinConfig = json_encode($poptinConfig); |
||
| 72 | $config->write(); |
||
| 73 | |||
| 74 | return json_encode(['success' => true, 'config' => json_decode($config->PoptinConfig)]); |
||
| 75 | } |
||
| 76 | |||
| 77 | public function deleteConfig(HTTPRequest $request) |
||
| 78 | { |
||
| 79 | $config = SiteConfig::current_site_config(); |
||
| 80 | |||
| 81 | $config->PoptinConfig = json_encode([ |
||
| 82 | 'client_id' => '', |
||
| 83 | 'user_id' => '', |
||
| 84 | 'token' => '' |
||
| 85 | ]); |
||
| 86 | $config->write(); |
||
| 87 | |||
| 88 | return json_encode(['success' => true, 'config' => json_decode($config->PoptinConfig)]); |
||
| 89 | } |
||
| 90 | |||
| 91 | public function redirectToDashboard() |
||
| 92 | { |
||
| 93 | $config = SiteConfig::current_site_config(); |
||
| 94 | |||
| 95 | $poptinConfig = json_decode($config->PoptinConfig, true); |
||
| 96 | |||
| 97 | $body = [ |
||
| 98 | "user_id" => $poptinConfig['user_id'], |
||
| 99 | "token" => $poptinConfig['token'], |
||
| 100 | ]; |
||
| 101 | |||
| 102 | $client = new \GuzzleHttp\Client(); |
||
| 103 | try { |
||
| 104 | $response = $client->request("POST", 'https://app.popt.in/api/marketplace/auth', [ |
||
| 105 | "form_params" => ($body) |
||
| 106 | ]); |
||
| 107 | |||
| 108 | if ($response->getStatusCode() == 200) { |
||
| 109 | return $this->redirect(json_decode($response->getBody()->getContents())->login_url . '&utm_source=silverstripe'); |
||
| 110 | } |
||
| 111 | } catch (\Exception $e) { |
||
| 112 | return $this->redirect('https://app.popt.in'); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 |