PluginProbe
Instapage Plugin / trunk
Instapage Plugin vtrunk
trunk 3.2.11 3.2.12 3.2.13 3.2.14 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.10 3.5.11 3.5.12 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 All 28 releases
instapage / models / InstapageCmsPluginSubaccountModel.php

InstapageCmsPluginSubaccountModel.php in Instapage Plugin trunk, at models/InstapageCmsPluginSubaccountModel.php

204 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class responsible for managing the subaccounts in Instapage app.
5 */
6 class InstapageCmsPluginSubaccountModel {
7
8 /**
9 * @var object Class instance.
10 */
11 private static $subaccountModel = null;
12
13 /**
14 * @var array List of subaccount tokens.
15 */
16 private $subaccountTokens = null;
17
18 /**
19 * Gets the class instance.
20 *
21 * @return object Class instance.
22 */
23 public static function getInstance() {
24 if (self::$subaccountModel === null) {
25 self::$subaccountModel = new InstapageCmsPluginSubaccountModel();
26 }
27
28 return self::$subaccountModel;
29 }
30
31 /**
32 * Gets all the tokens - stored in plugin's settings and bound to currently used app user account.
33 *
34 * @return array List of tokens.
35 */
36 public function getAllTokens() {
37 if ($this->subaccountTokens === null) {
38 $tokens = InstapageCmsPluginHelper::getTokens();
39 $accountKeys = $this->getAccountBoundTokens();
40 $this->subaccountTokens = array_merge($tokens, $accountKeys);
41 }
42
43 return $this->subaccountTokens;
44 }
45
46 /**
47 * Gets the list of tokens bound to subaccount. User has to be logged in via email and password.
48 *
49 * @return array List of tokens bound to an account.
50 */
51 public function getAccountBoundTokens() {
52 $api = InstapageCmsPluginAPIModel::getInstance();
53 $userToken = InstapageCmsPluginHelper::getOption('plugin_hash');
54 $accountKeys = array();
55
56 if ($userToken) {
57 $headers = array('usertoken' => $userToken);
58 $responseJson = $api->apiCall('page/get-account-keys', null, $headers);
59 $response = json_decode($responseJson);
60
61 if (!is_null($response) && $response->success) {
62 $accountKeys = $response->data->accountkeys;
63 }
64 }
65
66 return $accountKeys;
67 }
68
69 /**
70 * Gets the list of subaccounts of currently logged in user.
71 *
72 * @param string $format Format for the response. Default: 'json'.
73 *
74 * @return (string|array) List of subaccounts as a JSON string or an array.
75 */
76 public function getAccountBoundSubAccounts($format = 'json') {
77 $api = InstapageCmsPluginAPIModel::getInstance();
78 $tokens = $this->getAccountBoundTokens();
79 $subAccounts = array();
80
81 if (is_array($tokens) && count($tokens)) {
82 $headers = array('accountkeys' => InstapageCmsPluginHelper::getAuthHeader($tokens));
83 $response = json_decode($api->apiCall('page/get-sub-accounts-list', null, $headers));
84 $subAccounts = isset($response->data) ? $response->data : null;
85 }
86
87 if ($format == 'json') {
88 echo json_encode((object) array(
89 'status' => 'OK',
90 'data' => $subAccounts
91 ));
92 } else {
93 return $subAccounts;
94 }
95 }
96
97 /**
98 * Sets the status of subaccount in Instapage app. Subaccount can be connected to or disconnected from a CMS.
99 *
100 * @param string $status Status to be set. Default: 'connect'. 'disconnect' is another option.
101 * @param array $tokens List of tokens, that are meant to be connected of disconnected.
102 * @param bool $silent Do you want a message to appear?
103 */
104 public function setSubAccountsStatus($status = 'connect', $tokens = null, $silent = false) {
105 $api = InstapageCmsPluginAPIModel::getInstance();
106 $subaccount = InstapageCmsPluginSubaccountModel::getInstance();
107 $post = InstapageCmsPluginHelper::getPostData();
108
109 if ($tokens !== null) {
110 $selectedSubaccounts = $tokens;
111 } else {
112 $selectedSubaccounts = isset($post->data->tokens) ? $post->data->tokens : array();
113 }
114
115 if (count($selectedSubaccounts)) {
116 $tokens = $subaccount->getAllTokens();
117 $headers = array('accountkeys' => InstapageCmsPluginHelper::getAuthHeader($tokens));
118 $data = array(
119 'accountkeys' => base64_encode(json_encode($selectedSubaccounts)),
120 'status' => $status,
121 'domain' => InstapageCmsPluginConnector::getHomeURL(false)
122 );
123
124 $response = json_decode($api->apiCall('page/connection-status', $data, $headers));
125
126 if ($silent) {
127 return;
128 }
129
130 if (
131 !InstapageCmsPluginHelper::checkResponse($response, null, false) ||
132 !$response->success ||
133 !isset($response->data->changed) ||
134 $response->data->changed != count($selectedSubaccounts)
135 ) {
136 $action = array();
137 $action[0] = $status == 'connect' ? 'connected to' : 'disconnected from';
138 $action[1] = $status == 'connect' ? 'connect' : 'disconnect';
139
140 if (count($selectedSubaccounts) > 1) {
141 $message = isset($response->message)
142 ? $response->message
143 : InstapageCmsPluginConnector::lang(
144 'There was an error, selected workspaces are not properly %s app. Try to %s workspaces again.',
145 $action[0],
146 $action[1]
147 );
148 } else {
149 $message = isset($response->message)
150 ? $response->message
151 : InstapageCmsPluginConnector::lang(
152 'There was an error, selected workspace is not properly %s app. Try to %s workspaces again.',
153 $action[0],
154 $action[1]
155 );
156 }
157
158 echo InstapageCmsPluginHelper::formatJsonMessage($message, 'ERROR');
159 } else {
160 $action = array();
161 $action[0] = $status == 'connect' ? 'Selected workspaces' : 'Workspaces bound to your account';
162 $action[1] = $status == 'connect' ? 'connected' : 'disconnected';
163
164 if (count($selectedSubaccounts) > 1) {
165 $message = isset($response->message)
166 ? $response->message
167 : InstapageCmsPluginConnector::lang('%s are %s.', $action[0], $action[1]);
168 } else {
169 $message = isset($response->message)
170 ? $response->message
171 : InstapageCmsPluginConnector::lang('Selected workspace is %s.', $action[1]);
172 }
173
174 echo InstapageCmsPluginHelper::formatJsonMessage($message);
175 }
176 } else {
177 echo InstapageCmsPluginHelper::formatJsonMessage(InstapageCmsPluginConnector::lang('No workspaces were connected.'));
178 }
179 }
180
181 /**
182 * Disconnects all subaccount bound to an account. User has to be looged in via email and password.
183 *
184 * @param bool $silent Do you want a message to appear? Default: false.
185 */
186 public function disconnectAccountBoundSubaccounts($silent = false) {
187 $subAccounts = $this->getAccountBoundSubAccounts('array');
188
189 if (count($subAccounts)) {
190 $tokens = array();
191
192 foreach ($subAccounts as $item) {
193 $tokens[] = isset($item->accountkey) ? $item->accountkey : '';
194 }
195
196 $this->setSubAccountsStatus('disconnect', $tokens, $silent);
197 } else {
198 if (!$silent) {
199 echo InstapageCmsPluginHelper::formatJsonMessage(InstapageCmsPluginConnector::lang('Workspaces bound to your account are disconnected'));
200 }
201 }
202 }
203 }
204