Skip to content

Commit

Permalink
A 'redirect' function that automatically redirects to a template if b…
Browse files Browse the repository at this point in the history
…rowser check conditions are not met.
  • Loading branch information
Mark Notton committed Jun 8, 2018
1 parent 4178a8e commit 6efa4f7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 38 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.7 - 2018-07-08
### Added
- A 'redirect' function that automatically redirects to a template if browser check conditions are not met.

## 1.0.6 - 2018-07-07
### Fixed
- Resolved errors on older browsers where data-attributes didn't exsit.
- Resolved errors on older browsers where data-attributes didn't exsit.

## 1.0.5 - 2018-05-18
### Added
Expand Down
34 changes: 26 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Query the server-side information from the users agent data.

- [Credit](#dependencies)
- [Installation](#installation)
- [Is](#is)
- [Check](#check)
- [Redirect](#redirect)
- [Data](#data)
- [Full](#full)
- [Browser/platform version](#browserplatform-version)
Expand Down Expand Up @@ -46,38 +47,55 @@ Or manually in your compsoer.json:
}
```

## Is
## Check

Perform a number of checks to determine wether the users browser type is a match. Returns ```boolean```.

#### Example 1:
Returns true if current browser is either 'IE, Edge, or Firefox'
```
{{ craft.agent.is('ie edge firefox') }}
{{ craft.agent.check('ie edge firefox') }}
```

#### Example 2:
Exactly the same as example one, but demonstrates you can pass in as many arguments as you like. Each argument is handled as an "or" not an "and".
```
{{ craft.agent.is('ie', 'edge', 'firefox') }}
{{ craft.agent.check('ie', 'edge', 'firefox') }}
```

#### Example 3:
Returns true if current browser is greater than IE 9
```
{{ craft.agent.is('ie 9 >') }}
{{ craft.agent.check('ie 9 >') }}
```

#### Example 4:
Returns true if current browser is greater or equal to IE 9
```
{{ craft.agent.is('ie => 9') }}
{{ craft.agent.check('ie => 9') }}
```

#### Example 5:
Returns true if current browser is either, IE version 9 or 10, Chrome version 50 or above, or Firefox any version
Returns true if current browser is either, IE version 9, Chrome version 50 or above, or Firefox any version
```
{{ craft.agent.is('ie 9 10', 'chrome > 49', 'firefox') }}
{{ craft.agent.check('ie 9', 'chrome > 49', 'firefox') }}
```

## Redirect
Redirect users if your browser conditions are not met. Following the same syntax as the 'check' function,
this will redirect users to a specific template. You can also pass in a status code too.

```
{% set criteria = [
'ie < 11',
'chrome <= 55',
'firefox <= 44',
'safari <= 7',
'edge <= 15',
'opera <= 50'
] %}
{{ craft.agent.redirect(criteria, 'no-support.twig', 302) }}
```

## Data
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "marknotton/agent",
"description": "Query the server-side information from the users agent data.",
"type": "craft-plugin",
"version": "1.0.6",
"version": "1.0.7",
"keywords": [
"craft",
"cms",
Expand All @@ -22,7 +22,7 @@
}
],
"require": {
"craftcms/cms": "^3.0.0-RC15",
"craftcms/cms": "^3.0.0.*",
"jenssegers/agent": "^2.6"
},
"autoload": {
Expand All @@ -33,7 +33,7 @@
"extra": {
"name": "Agent",
"handle": "agent",
"schemaVersion": "1.0.6",
"schemaVersion": "1.0.7",
"hasCpSettings": false,
"hasCpSection": false,
"changelogUrl": "https://github.com/marknotton/craft-plugin-agent/blob/master/CHANGELOG.md",
Expand Down
37 changes: 11 additions & 26 deletions src/services/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function data() {
* Checks for browser types and versions
* @return boolean
*/
public function is() {
public function check() {

// Atleast one browser sting arugment should be passed
if ( func_num_args() < 1 ){
Expand All @@ -66,20 +66,18 @@ public function is() {

$arguments = func_get_args();

foreach ($arguments as &$settings) {
if (is_array($arguments[0])) {
$arguments = $arguments[0];
}

foreach ($arguments as &$argument) {

$agents = [];
$versions = [];
$condition = null;

if (gettype($settings) == 'string') {
$explodeSettings = explode(' ', $settings);
} else {
$explodeSettings = $settings;
}

// Check all the given settings
foreach ($explodeSettings as &$setting) {
foreach (explode(' ', $argument) as &$setting) {

if (preg_match('[<|>|=>|<=]', $setting)) {
// If a greater or less than condition is passed
Expand All @@ -89,26 +87,12 @@ public function is() {
} else if (ctype_digit($setting)) {
// If number, add as version
array_push($versions, $setting);
} else {
} else if ($setting !== '='){
// Anything else is assumed to be the agents name
array_push($agents, $setting);
}
}

// If mutliple versions and a condition are used at the same time
// Recreate the version array with only the more relivant version number.
if (!is_null($condition) && count($versions) >= 2) {
switch ($condition) {
case ( $condition == ">" || $condition == "=>" ):
$versions = array(max($versions));
break;
case ( $condition == "<" || $condition == "=<" ):
$versions = array(min($versions));
break;
}
}


if (!empty($agents)) {

$checkVersion = null;
Expand All @@ -118,9 +102,10 @@ public function is() {
if (!empty($versions)) {
// Validate any of the given versions
foreach ($versions as &$version) {
// Only update check variable is it is null or true. Once it's false, it stays false
// Only update check variable if it is null or true. Once it's false, it stays false
if (is_null($checkVersion) || $checkVersion != false) {
if (isset($condition)){

// If there is a condition to validate
switch ($condition) {
case ">=":
Expand Down Expand Up @@ -183,7 +168,7 @@ public function is() {
* @param integer $statuscode Amend the status code
*/
public function redirect($criteria, $redirect = 'browser', $statuscode = 302) {
if ( $this->is($criteria) ) {
if ( $this->check($criteria) ) {
$url = UrlHelper::url($redirect);
Craft::$app->getResponse()->redirect($url, $statuscode);
}
Expand Down

0 comments on commit 6efa4f7

Please sign in to comment.