You are here

public function InstapageCmsPluginSubaccountModel::setSubAccountsStatus in Instapage plugin 8.3

Same name and namespace in other branches
  1. 7.3 core/models/InstapageCmsPluginSubaccountModel.php \InstapageCmsPluginSubaccountModel::setSubAccountsStatus()

Sets the status of subaccount in Instapage app. Subaccount can be connected to or disconnected from a CMS.

Parameters

string $status Status to be set. Default: 'connect'. 'disconnect' is another option.:

array $tokens List of tokens, that are meant to be connected of disconnected.:

bool $silent Do you want a message to appear?:

1 call to InstapageCmsPluginSubaccountModel::setSubAccountsStatus()
InstapageCmsPluginSubaccountModel::disconnectAccountBoundSubaccounts in core/models/InstapageCmsPluginSubaccountModel.php
Disconnects all subaccount bound to an account. User has to be looged in via email and password.

File

core/models/InstapageCmsPluginSubaccountModel.php, line 104

Class

InstapageCmsPluginSubaccountModel
Class responsible for managing the subaccounts in Instapage app.

Code

public function setSubAccountsStatus($status = 'connect', $tokens = null, $silent = false) {
  $api = InstapageCmsPluginAPIModel::getInstance();
  $subaccount = InstapageCmsPluginSubaccountModel::getInstance();
  $post = InstapageCmsPluginHelper::getPostData();
  if ($tokens !== null) {
    $selectedSubaccounts = $tokens;
  }
  else {
    $selectedSubaccounts = isset($post->data->tokens) ? $post->data->tokens : array();
  }
  if (count($selectedSubaccounts)) {
    $tokens = $subaccount
      ->getAllTokens();
    $headers = array(
      'accountkeys' => InstapageCmsPluginHelper::getAuthHeader($tokens),
    );
    $data = array(
      'accountkeys' => base64_encode(json_encode($selectedSubaccounts)),
      'status' => $status,
      'domain' => InstapageCmsPluginConnector::getHomeURL(false),
    );
    $response = json_decode($api
      ->apiCall('page/connection-status', $data, $headers));
    if ($silent) {
      return;
    }
    if (!InstapageCmsPluginHelper::checkResponse($response, null, false) || !$response->success || !isset($response->data->changed) || $response->data->changed != count($selectedSubaccounts)) {
      $action = array();
      $action[0] = $status == 'connect' ? 'connected to' : 'disconnected from';
      $action[1] = $status == 'connect' ? 'connect' : 'disconnect';
      if (count($selectedSubaccounts) > 1) {
        $message = isset($response->message) ? $response->message : InstapageCmsPluginConnector::lang('There was an error, selected workspaces are not properly %s app. Try to %s workspaces again.', $action[0], $action[1]);
      }
      else {
        $message = isset($response->message) ? $response->message : InstapageCmsPluginConnector::lang('There was an error, selected workspace is not properly %s app. Try to %s workspaces again.', $action[0], $action[1]);
      }
      echo InstapageCmsPluginHelper::formatJsonMessage($message, 'ERROR');
    }
    else {
      $action = array();
      $action[0] = $status == 'connect' ? 'Selected workspaces' : 'Workspaces bound to your account';
      $action[1] = $status == 'connect' ? 'connected' : 'disconnected';
      if (count($selectedSubaccounts) > 1) {
        $message = isset($response->message) ? $response->message : InstapageCmsPluginConnector::lang('%s are %s.', $action[0], $action[1]);
      }
      else {
        $message = isset($response->message) ? $response->message : InstapageCmsPluginConnector::lang('Selected workspace is %s.', $action[1]);
      }
      echo InstapageCmsPluginHelper::formatJsonMessage($message);
    }
  }
  else {
    echo InstapageCmsPluginHelper::formatJsonMessage(InstapageCmsPluginConnector::lang('No workspaces were connected.'));
  }
}