From 6614c7446f56c8e77bd9ced65dd4b0d87aa7f9b8 Mon Sep 17 00:00:00 2001 From: Sebastian Buckpesch Date: Fri, 2 Feb 2018 18:15:36 +0100 Subject: [PATCH] Update smartlink to improve the Redirect behaviour corresponding to the channels the app is installed on. The whole smartlink redirect behaviour can be controlled by GET parameters --- docs/smartlink.md | 17 +- .../Models/Entities/AbstractEntity.php | 72 +++-- src/AppArena/Models/Entities/App.php | 132 +++++++- src/AppArena/Models/Environment.php | 4 +- src/AppArena/Models/Environment/Facebook.php | 282 +++++++++--------- src/AppArena/Models/SmartLink.php | 109 ++----- .../views/partials/shareDebug.mustache | 33 +- 7 files changed, 363 insertions(+), 286 deletions(-) diff --git a/docs/smartlink.md b/docs/smartlink.md index f8b3cb1..f353212 100644 --- a/docs/smartlink.md +++ b/docs/smartlink.md @@ -26,14 +26,15 @@ Note You can add all of the listed parameters to the SmartLink Url to modify the Redirect behaviour. -| Parameter | Description | Example | -|:----------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------| -| device | To simulate a different device type (mobile, tablet or desktop), just add a device-GET Parameter to your URL. The SmartLink will automatically use this device type then and respond with it. Allowed values are `mobile`, `tablet` and `desktop`. | Simulate the mobile view of an app: https://www.my-web-app.com/?i_id=1234&device=mobile | -| website | If your app is being embedded in a website via iframe, you should add a GET-Parameter called website containing the URL the app is embedded in to your smartlink.php Url. Your users will then be redirected the Website Url and not directly to your app. This will keep traffic up on your website. :-) | Redirect the user to a certain website the iframe with your app is embedded in e.g. https://www.app-arena.com/fotowettbewerb.html The SmartLink is: | -| fb_page_id | Submit this parameter to redirect to this Facebook fanpage Tab your app is embedded in. You can use this to have the same app installed on several fanpages and to control the redirects -| ref_app_env | Set this parameter to fb to force the redirection to the facebook fanpage the app is installed on. | https://www.my-app.com/?i_id=1234&ref_app_env=fb | -| lang | The language parameter controls the used language of the app | Show your app in french: https://www.my-web-app.com/?i_id=1234&lang=fr_FR -| debug | Add the debug parameter to disable redirects and show Debug info on the smartlink.php page | Show the Debug-Page for the SmartLink: https://www.my-web-app.com/smartlink.php?debug=1 +| Parameter | Description | Example | +|:-----------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------| +| device | To simulate a different device type (mobile, tablet or desktop), just add a device-GET Parameter to your URL. The SmartLink will automatically use this device type then and respond with it. Allowed values are `mobile`, `tablet` and `desktop`. | Simulate the mobile view of an app: https://www.my-web-app.com/?i_id=1234&device=mobile | +| channelId | Add this parameter to prioritize the channel with the submitted ID as redirection target. | | +| website | If your app is being embedded in a website via iframe, you should add a GET-Parameter called website containing the URL the app is embedded in to your smartlink.php Url. Your users will then be redirected the Website Url and not directly to your app. This will keep traffic up on your website. :-) | Redirect the user to a certain website the iframe with your app is embedded in e.g. https://www.app-arena.com/fotowettbewerb.html The SmartLink is: | +| fb_page_id | Submit this parameter to redirect to this Facebook fanpage Tab your app is embedded in. You can use this to have the same app installed on several fanpages and to control the redirects | | +| target | Set this parameter to prioritize `facebook`, `website` or `domain` as Smartlink redirection target. | | +| lang | The language parameter controls the used language of the app | Show your app in french: https://www.my-web-app.com/?i_id=1234&lang=fr_FR | +| debug | Add the debug parameter to disable redirects and show Debug info on the smartlink.php page | Show the Debug-Page for the SmartLink: https://www.my-web-app.com/smartlink.php?debug=1 | ## Embed an App via iframe ($_GET[‘website’]) diff --git a/src/AppArena/Models/Entities/AbstractEntity.php b/src/AppArena/Models/Entities/AbstractEntity.php index bfe5b4f..82ccf28 100644 --- a/src/AppArena/Models/Entities/AbstractEntity.php +++ b/src/AppArena/Models/Entities/AbstractEntity.php @@ -272,7 +272,7 @@ public function getInfos() { if ( isset( $meta['_embedded']['data'] ) && is_array( $meta['_embedded']['data'] ) ) { $values = array_map( function ( $item ) { - if (isset($item['value'])) { + if ( isset( $item['value'] ) ) { return $item['value']; } }, $meta['_embedded']['data'] ); @@ -448,44 +448,47 @@ public function getLang() { if ( ! $this->lang ) { // Try to recover language from Request $lang = false; - if ( isset( $_GET['lang'] ) ) { - $this->setLang($_GET['lang']); - return $this->lang; - } + if ( isset( $_GET['lang'] ) ) { + $this->setLang( $_GET['lang'] ); + + return $this->lang; + } // Try to get lang from Cookie if ( isset( $_COOKIE[ 'aa_' . $this->id . '_lang' ] ) ) { $this->lang = $_COOKIE[ 'aa_' . $this->id . '_lang' ]; + return $this->lang; } // Get the default language from of the entity - if ($languages = $this->getLanguages()) { - foreach ( $languages['activated'] as $language ) { - if ($language['default']) { - $this->lang = $language['lang']; - return $this->lang; - } - } - } + if ( $languages = $this->getLanguages() ) { + foreach ( $languages['activated'] as $language ) { + if ( $language['default'] ) { + $this->lang = $language['lang']; + + return $this->lang; + } + } + } } return $this->lang; } - /** - * @param string $lang - */ - public function setLang( $lang ) { - // Validate language code - $languages = $this->validLanguages; - if ( ! isset( $languages[ $lang ] ) ) { - throw new \InvalidArgumentException( $lang . ' is not a valid language code' ); - } - setcookie('aa_' . $this->id . '_lang', $lang, time() + 172600, '/'); + /** + * @param string $lang + */ + public function setLang( $lang ) { + // Validate language code + $languages = $this->validLanguages; + if ( ! isset( $languages[ $lang ] ) ) { + throw new \InvalidArgumentException( $lang . ' is not a valid language code' ); + } + setcookie( 'aa_' . $this->id . '_lang', $lang, time() + 172600, '/' ); - $this->lang = $lang; - } + $this->lang = $lang; + } /** * @return mixed @@ -502,4 +505,23 @@ public function setName( $name ) { } + /** + * Returns the base url of the entity + */ + protected function getBaseUrl() { + // Initialize the base_url + $base_url = $this->getInfo( 'base_url' ); + if ( isset( $_SERVER['SERVER_NAME'] ) ) { + $base_url = 'http'; + if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) { + $base_url .= 's'; + } + $base_url .= '://'; + $base_url .= $_SERVER['SERVER_NAME']; + if ( substr( $base_url, - 1 ) !== '/' ) { + $base_url .= '/'; + } + } + return $base_url; + } } \ No newline at end of file diff --git a/src/AppArena/Models/Entities/App.php b/src/AppArena/Models/Entities/App.php index f47afbf..e67f2c4 100644 --- a/src/AppArena/Models/Entities/App.php +++ b/src/AppArena/Models/Entities/App.php @@ -1,4 +1,5 @@ type = 'app'; + $this->type = 'app'; $this->versionId = $versionId; // If no App ID available, then try to recover it @@ -33,7 +34,7 @@ public function __construct( $id = null, $versionId ) { * @return integer */ public function getTemplateId() { - return $this->getInfo('templateId'); + return $this->getInfo( 'templateId' ); } /** @@ -97,8 +98,8 @@ public function recoverAppId() { // Set ID to the object and the users session and cookie if ( $id ) { - $_SESSION['current_appId'] = (int)$id; - $this->id = (int)$id; + $_SESSION['current_appId'] = (int) $id; + $this->id = (int) $id; } return $this->id; @@ -106,7 +107,7 @@ public function recoverAppId() { /** - * Returns a list of all channels the app is published on + * Returns a list of all channels the app is published on in prioritized order (highest channel first) * @return array|bool */ public function getChannels() { @@ -115,19 +116,126 @@ public function getChannels() { return $this->channels; } + // 1. Initialize the default channel (Base Url with direct access) + $channels = [ + [ + 'channelId' => 0, + 'priority' => 100, + 'type' => 'domain', + 'name' => 'Default domain', + 'url' => $this->getBaseUrl() + ] + ]; + + // 2. Add a channel which might be added (e.g. added via GET param) + if ( isset( $_GET['fb_page_id'] ) && $this->getInfo( 'fb_app_id' ) ) { + // Add channel with high priority as it is explicitly defined in GET parameter + $channels[] = [ + 'channelId' => 0, + 'priority' => 9999, + 'type' => 'facebook', + 'name' => 'Facebook Page added via GET parameter fb_page_id', + 'url' => 'https://www.facebook.com/' . $_GET['fb_page_id'] . '/app/' . $this->getInfo( 'fb_app_id' ), + ]; + } + + if ( isset( $_GET['website'] ) ) { + // Add channel with high priority as it is explicitly defined in GET parameter + $channels[] = [ + 'channelId' => 0, + 'priority' => 9999, + 'type' => 'website', + 'name' => 'Website added via GET parameter website', + 'url' => $_GET['website'], + ]; + } // App infos is a merged array of basic app information and additional app meta data - $channels = $this->api->get( 'apps/' . $this->id . '/channels' ); + $installedChannels = $this->api->get( 'apps/' . $this->id . '/channels' ); + + if ( isset( $installedChannels['_embedded']['data'] ) && is_array( $installedChannels['_embedded']['data'] ) ) { + $installedChannels = $installedChannels['_embedded']['data']; - if ( isset( $channels['_embedded']['data'] ) && is_array( $channels['_embedded']['data'] ) ) { - $this->channels = $channels['_embedded']['data']; + // Prepare data + $installedChannels = array_map(function($channel){ + if ($channel['type'] === 'facebook') { + // If a target GET parameter is defined and set to 'facebook', then Facebook channels will get higher prio + $priority = $channel['priority'] ?? 0; + if (isset($_GET['target']) && $_GET['target'] === 'facebook'){ + $priority = 8888; // Not as high as directly called channels + } + + return [ + 'channelId' => $channel['channelId'], + 'priority' => $priority, + 'type' => 'facebook', + 'name' => $channel['name'] ?? 'Channel ID ' . $channel['channelId'], + 'url' => 'https://www.facebook.com/' . $channel['value'] . '/app/' . $this->getInfo( 'fb_app_id' ), + ]; + } + if ($channel['type'] === 'website') { + // If a target GET parameter is defined and set to 'facebook', then Facebook channels will get higher prio + $priority = $channel['priority'] ?? 0; + if (isset($_GET['target']) && $_GET['target'] === 'website'){ + $priority = 8888; // Not as high as directly called channels + } + + return [ + 'channelId' => $channel['channelId'], + 'priority' => $priority, + 'type' => 'website', + 'name' => $channel['name'] ?? 'Channel ID ' . $channel['channelId'], + 'url' => $channel['value'], + ]; + } + if ($channel['type'] === 'domain') { + // If a target GET parameter is defined and set to 'facebook', then Facebook channels will get higher prio + $priority = $channel['priority'] ?? 0; + if (isset($_GET['target']) && $_GET['target'] === 'domain'){ + $priority = 8888; // Not as high as directly called channels + } + + return [ + 'channelId' => $channel['channelId'], + 'priority' => $priority, + 'type' => 'domain', + 'name' => $channel['name'] ?? 'Channel ID ' . $channel['channelId'], + 'url' => $channel['value'], + ]; + } + + + }, $installedChannels); + + // Merge default channels and channels the customer has installed the app on + $channels = array_merge_recursive( $channels, $installedChannels ); + $this->channels = $channels; } else { - return false; + $this->channels = $defaultChannels; } if ( ! $this->channels ) { return false; } + // Order all channel by priority + $priority = []; + foreach ( $channels as $key => $row ) { + // If the current channel is exoplicitly prioritized via GET param, then give it a high priority + $channelId = $row['channelId'] ?? 0; + if ( isset( $_GET['channelId'] ) && $_GET['channelId'] == $channelId ) { + $row['priority'] = 9999; + $channels[$key]['priority'] = 9999; + } + + if ( ! isset( $row['priority'] ) ) { + $row['priority'] = 0; + } + + $priority[ $key ] = $row['priority']; + } + array_multisort( $priority, SORT_DESC, $channels ); + $this->channels = $channels; + return $this->channels; } @@ -155,11 +263,11 @@ private function getIdFromFBRequest() { $request_url = "https://manager.app-arena.com/api/v1/env/fb/pages/" . $fb_page_id . "/instances.json?projectId=" . $this->versionId . "&active=true"; // If the facebook App ID is submitted, then it will be added to the request - if (isset($_GET['fb_app_id']) && strlen($_GET['fb_app_id']) > 10) { + if ( isset( $_GET['fb_app_id'] ) && strlen( $_GET['fb_app_id'] ) > 10 ) { $request_url .= '&fb_app_id=' . $_GET['fb_app_id']; } - $instances = json_decode( file_get_contents( $request_url ), true ); + $instances = json_decode( file_get_contents( $request_url ), true ); foreach ( $instances['data'] as $instance ) { if ( $instance['activate'] == 1 ) { $appId = $instance['i_id']; diff --git a/src/AppArena/Models/Environment.php b/src/AppArena/Models/Environment.php index dd73814..34f1221 100644 --- a/src/AppArena/Models/Environment.php +++ b/src/AppArena/Models/Environment.php @@ -53,7 +53,7 @@ public function __construct( AbstractEntity $entity ) { * * @return AbstractEnvironment */ - public function getPrimaryEnvironment() { + /*public function getPrimaryEnvironment() { if ($this->website->getUrl()) { return $this->website; @@ -64,7 +64,7 @@ public function getPrimaryEnvironment() { } return $this->domain; - } + }*/ /** diff --git a/src/AppArena/Models/Environment/Facebook.php b/src/AppArena/Models/Environment/Facebook.php index a0b450a..766f339 100644 --- a/src/AppArena/Models/Environment/Facebook.php +++ b/src/AppArena/Models/Environment/Facebook.php @@ -12,147 +12,147 @@ class Facebook extends AbstractEnvironment { - private $appId; - private $pageId; - private $pageUrl; - private $pageTab; - private $signedRequest; - - /** - * Facebook constructor. - * - * @param AbstractEntity $entity - */ - public function __construct(AbstractEntity $entity) - { - - parent::__construct($entity); - $this->type = 'facebook'; - $this->priority = 10; - - // Get Facebook page tab parameters and write them to GET parameters - if (isset($_REQUEST['signed_request'])) { - $this->signedRequest = $_REQUEST['signed_request']; - list($encoded_sig, $payload) = explode('.', $this->signedRequest, 2); - $fb_signed_request = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); - - // So use the current Facebook page for sharing, if no fb_page_id is defined via GET - if (isset($fb_signed_request['page']['id']) && !isset($_GET['fb_page_id'])) { - $_GET['fb_page_id'] = $fb_signed_request['page']['id']; - } - // And the parameter-passthrough mechanism, will reset all GET parameters from app_data - if (isset($fb_signed_request['app_data'])) { - $params = json_decode(urldecode($fb_signed_request['app_data']), true); - foreach ($params as $key => $value) { - if (!isset($_GET[$key])) { - $_GET[$key] = $value; - } - } - } - } else { - $this->signedRequest = false; - } - - // Initialize Facebook Information ... (and check if the SmartLink should redirect to Facebook) - $fb_page_id = false; - $fb_app_id = $this->entity->getInfo('fb_app_id'); - if (isset($_GET['fb_app_id']) && $_GET['fb_app_id']) { - $fb_app_id = $_GET['fb_app_id']; - } - if ($fb_app_id) { - if (isset($_GET['fb_page_id'])) { - // ... from GET-Parameter - $this->appId = $fb_app_id; - $this->pageId = $_GET['fb_page_id']; - $this->pageUrl = 'https://www.facebook.com/' . $this->pageId; - $this->pageTab = $this->pageUrl . '/app/' . $this->appId; - } else { - $facebook = $this->getCookieValue("facebook"); - if (isset($facebook['page_id'])) { - // ... from COOKIE-Parameter - $this->appId = $fb_app_id; - $this->pageId = $facebook['page_id']; - $this->pageUrl = 'https://www.facebook.com/' . $this->pageId; - $this->pageTab = $this->pageUrl . '/app/' . $this->appId; - } else { - // ... from the App Channels - $channels = $this->entity->getChannels(); - if (is_array($channels)) { - foreach ($channels as $channel) { - if ($channel['type'] === 'facebook' && isset($channel['meta'])) { - $this->appId = $fb_app_id; - $this->pageId = $channel['value']; - $this->pageUrl = 'https://www.facebook.com/' . $this->pageId; - $this->pageTab = $this->pageUrl . '/app/' . $this->appId; - break; - } - } - } - } - } - } - - // Initializes Facebook canvas information - /*if ( $fb_app_id && $this->entity->getInfo( 'fb_app_namespace' ) ) { - $this->facebook['app_namespace'] = $this->entity->getInfo( 'fb_app_namespace' ); - $this->appId = $fb_app_id; - $canvas_url = 'https://apps.facebook.com/' . $this->facebook['app_namespace'] . '/?entityId=' . $this->entityId; - $this->facebook['canvas_url'] = $canvas_url; - }*/ - - } - - /** - * @return array|bool|String - */ - public function getAppId() - { - return $this->appId; - } - - /** - * @return string - */ - public function getPageUrl() - { - return $this->pageUrl; - } - - /** - * Returns all relevant Environment information as array - */ - public function toArray() - { - return [ - 'priority' => $this->getPriority(), - 'type' => $this->getType(), - 'pageId' => $this->getPageId() - ]; - } - - /** - * @return mixed - */ - public function getPageId() - { - return $this->pageId; - } - - /** - * @return string - */ - public function getPageTab() - { - return $this->pageTab; - } - - /** - * @return string - */ - public function getSignedRequest() - { - return $this->signedRequest; - } + private $appId; + private $pageId; + private $pageUrl; + private $pageTab; + private $signedRequest; + + /** + * Facebook constructor. + * + * @param AbstractEntity $entity + */ + public function __construct(AbstractEntity $entity) + { + + parent::__construct($entity); + $this->type = 'facebook'; + $this->priority = 10; + + // Get Facebook page tab parameters and write them to GET parameters + if (isset($_REQUEST['signed_request'])) { + $this->signedRequest = $_REQUEST['signed_request']; + list($encoded_sig, $payload) = explode('.', $this->signedRequest, 2); + $fb_signed_request = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); + + // So use the current Facebook page for sharing, if no fb_page_id is defined via GET + if (isset($fb_signed_request['page']['id']) && !isset($_GET['fb_page_id'])) { + $_GET['fb_page_id'] = $fb_signed_request['page']['id']; + } + // And the parameter-passthrough mechanism, will reset all GET parameters from app_data + if (isset($fb_signed_request['app_data'])) { + $params = json_decode(urldecode($fb_signed_request['app_data']), true); + foreach ($params as $key => $value) { + if (!isset($_GET[$key])) { + $_GET[$key] = $value; + } + } + } + } else { + $this->signedRequest = false; + } + + // Initialize Facebook Information ... (and check if the SmartLink should redirect to Facebook) + $fb_page_id = false; + $fb_app_id = $this->entity->getInfo('fb_app_id'); + if (isset($_GET['fb_app_id']) && $_GET['fb_app_id']) { + $fb_app_id = $_GET['fb_app_id']; + } + if ($fb_app_id) { + if (isset($_GET['fb_page_id'])) { + // ... from GET-Parameter + $this->appId = $fb_app_id; + $this->pageId = $_GET['fb_page_id']; + $this->pageUrl = 'https://www.facebook.com/' . $this->pageId; + $this->pageTab = $this->pageUrl . '/app/' . $this->appId; + } else { + $facebook = $this->getCookieValue("facebook"); + if (isset($facebook['page_id'])) { + // ... from COOKIE-Parameter + $this->appId = $fb_app_id; + $this->pageId = $facebook['page_id']; + $this->pageUrl = 'https://www.facebook.com/' . $this->pageId; + $this->pageTab = $this->pageUrl . '/app/' . $this->appId; + } else { + // ... from the App Channels + $channels = $this->entity->getChannels(); + if (is_array($channels)) { + foreach ($channels as $channel) { + if ($channel['type'] === 'facebook' && isset($channel['meta'])) { + $this->appId = $fb_app_id; + $this->pageId = $channel['value']; + $this->pageUrl = 'https://www.facebook.com/' . $this->pageId; + $this->pageTab = $this->pageUrl . '/app/' . $this->appId; + break; + } + } + } + } + } + } + + // Initializes Facebook canvas information + /*if ( $fb_app_id && $this->entity->getInfo( 'fb_app_namespace' ) ) { + $this->facebook['app_namespace'] = $this->entity->getInfo( 'fb_app_namespace' ); + $this->appId = $fb_app_id; + $canvas_url = 'https://apps.facebook.com/' . $this->facebook['app_namespace'] . '/?entityId=' . $this->entityId; + $this->facebook['canvas_url'] = $canvas_url; + }*/ + + } + + /** + * @return array|bool|String + */ + public function getAppId() + { + return $this->appId; + } + + /** + * @return string + */ + public function getPageUrl() + { + return $this->pageUrl; + } + + /** + * Returns all relevant Environment information as array + */ + public function toArray() + { + return [ + 'priority' => $this->getPriority(), + 'type' => $this->getType(), + 'pageId' => $this->getPageId() + ]; + } + + /** + * @return mixed + */ + public function getPageId() + { + return $this->pageId; + } + + /** + * @return string + */ + public function getPageTab() + { + return $this->pageTab; + } + + /** + * @return string + */ + public function getSignedRequest() + { + return $this->signedRequest; + } } \ No newline at end of file diff --git a/src/AppArena/Models/SmartLink.php b/src/AppArena/Models/SmartLink.php index b649615..1d9ae32 100644 --- a/src/AppArena/Models/SmartLink.php +++ b/src/AppArena/Models/SmartLink.php @@ -91,6 +91,9 @@ public function __construct( AbstractEntity $entity, Environment $environment, C // Initializes the SmartCookie $this->initCookies(); + + // Initialize the SmartLink Url + $this->initUrl(); } /** @@ -343,7 +346,7 @@ public function renderSharePage( $debug = false ) { 'params' => $this->prepareMustacheArray( $this->getParams() ), 'params_expired' => $this->prepareMustacheArray( $this->paramsExpired ), 'reasons' => $this->reasons, - 'target' => $this->getEnvironment()->getPrimaryEnvironment()->getType(), + //'target' => $this->getEnvironment()->getPrimaryEnvironment()->getType(), 'url' => $this->getUrl(), 'url_target' => $this->getUrlTarget() ]; @@ -458,11 +461,14 @@ public function getDevice() { } /** + * Returns the SmartLink Url for the current environment + * * @params bool $shortenLink Make a Short Link? * * @return mixed */ public function getUrl( $shortenLink = false ) { + $this->initUrl(); if ( $shortenLink ) { @@ -473,94 +479,36 @@ public function getUrl( $shortenLink = false ) { } /** - * Analyzes all available data and sets the best suited target environment for the user + * Initialize the Url compatible to the current device * * @return array All available information about the environments */ private function initUrl() { - $url = false; - - // Due to Safari Cookie Blocking policies, redirect Safari Users to the direct page - /*$browser = $this->getBrowser(); - if ($browser['name'] == 'Safari') { - $this->reasons[] = 'BROWSER: Safari is used. Change target to direct'; - $this->setTarget('direct'); - }*/ - - // 1. If a website is defined, use the website as default environment - if ( $this->website->getUrl() ) { - $this->reasons[] = 'ENV: Website is defined'; - - // Validate the Website url - $website_valid = true; - if ( strpos( $this->website->getUrl(), 'www.facebook.com/' ) !== false || strpos( $this->website->getUrl(), - 'static.sk.facebook.com' ) !== false || strpos( $this->website->getUrl(), - '.js' ) !== false - ) { - $this->reasons[] = 'ENV: Website target is not valid, so it cannot be used as target.'; - $website_valid = false; - } - - // Check if another target is defined, then add the website as GET param, but do not use it for redirection - if ( $this->getUrlTarget() && $this->getTarget() !== 'website' ) { - $this->reasons[] = 'ENV: Website valid, but another target is defined'; - $this->addParams( [ 'website' => $this->website->getUrl() ] ); - $website_valid = false; - } - // If Website is valid, then use it - if ( $website_valid ) { - $this->setUrl( $this->website->getUrl() ); + $channels = $this->getEntity()->getChannels(); + $deviceType = $this->getEnvironment()->getDevice()->getDeviceType(); - return; - } - } else { - $this->reasons[] = 'ENV: No website parameter defined'; - } - - // If there is no website defined, check if the device is tablet or mobile. If so, use direct access - if ( in_array( $this->getEnvironment()->getDevice()->getDeviceType(), [ 'mobile', 'tablet' ] ) ) { - $this->reasons[] = 'DEVICE: User is using a ' . $this->getEnvironment()->getDevice()->getDeviceType() . ' device. Direct Access.'; - if ( $this->getBaseUrl() ) { - $this->setUrl( $this->getBaseUrl() ); - } else { - $this->setUrl( $this->entity->getInfo( 'base_url' ) ); + // Check if the channel is compatible with the current device + foreach ( $channels as $channel ) { + // Facebook page tab cannot be accessed by mobile and desktop devices + if ( $channel['type'] == 'facebook' && in_array( $deviceType, [ 'mobile', 'tablet' ] ) ) { + continue; } + $this->setUrl( $channel ); return; } - // So here should be only Desktop devices... So check if facebook page tab information are available... - $this->reasons[] = 'DEVICE: User is using a desktop device.'; - $facebook = $this->getFacebook(); - if ( $facebook->getPageId() && $facebook->getAppId() ) { - $this->reasons[] = 'ENV: Facebook environment data available.'; - - // Check if another target is defined, then add the website as GET param, but do not use it for redirection - $facebook_valid = true; - if ( $this->getUrlTarget() && $this->getTarget() !== 'facebook' ) { - $this->reasons[] = 'ENV: Facebook environment valid, but another target is defined'; - $facebook_valid = false; - } - - // If Facebook Environment is valid and the facebook should be used as target - if ( $facebook_valid ) { - $this->setUrl( $facebook->getPageTab() ); - - return; - } - } - - // If no optimal url is defined yet, then use direct source - $this->reasons[] = 'DEVICE: No website or facebook defined. Choose environment direct'; - if ( $this->getBaseUrl() ) { - $this->setUrl( $this->getBaseUrl() ); - } else { - $this->setUrl( $this->entity->getInfo( 'base_url' ) ); - } + } - return; + /** + * Returns an array of all publication channels in their priority Order + * + * @return Returns an array of channels prioritized by priority + */ + private function getChannels() { + return $this->getEntity()->getChannels(); } /** @@ -599,13 +547,14 @@ public function setTarget( $target ) { /** * Generates the SmartLink from the submitted url * - * @param String $target_url Url to generate the smartlink from - * @param bool $shortenLink Shorten the SmartLink using bit.ly + * @param array $targetChannel Optimal target channel for the current environment + * @param bool $shortenLink Shorten the SmartLink using bit.ly */ - private function setUrl( $target_url, $shortenLink = false ) { + private function setUrl( $targetChannel, $shortenLink = false ) { $share_url = $this->getBaseUrl() . $this->getFilename(); + $target_url = $targetChannel['url']; $target_original = $target_url; - $target_environment = $this->getEnvironment()->getPrimaryEnvironment()->getType(); + $target_environment = $targetChannel['type']; $params = []; diff --git a/src/AppArena/views/partials/shareDebug.mustache b/src/AppArena/views/partials/shareDebug.mustache index 08db0e1..a1c308b 100644 --- a/src/AppArena/views/partials/shareDebug.mustache +++ b/src/AppArena/views/partials/shareDebug.mustache @@ -25,14 +25,14 @@ {{ url_target }} - + Language @@ -64,30 +64,27 @@
-

Channels the app is installed on

- +

Channels

- - - + + + + + - {{#channels}} + {{# channels }} - - - + + + + + - {{/channels}} + {{/ channels }}
TypeNameUrlchannelIdprioritytypenameurl
- {{type}} - - {{name}} - - {{value}} - {{ channelId }}{{ priority }}{{ type }}{{ name }}{{ url }}