Skip to content

Commit

Permalink
Merge pull request #11519 from eileenmcnaughton/hook
Browse files Browse the repository at this point in the history
CRM-21659 - Add hook to CRM_Utils_System::redirect
  • Loading branch information
eileenmcnaughton authored Mar 15, 2018
2 parents 84e209e + ca4ce86 commit 8c6a526
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 4 deletions.
22 changes: 22 additions & 0 deletions CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,28 @@ public static function disable() {
);
}

/**
* Alter redirect.
*
* This hook is called when the browser is being re-directed and allows the url
* to be altered.
*
* @param \Psr\Http\Message\UriInterface $url
* @param array $context
* Additional information about context
* - output - if this is 'json' then it will return json.
*
* @return null
* the return value is ignored
*/
public static function alterRedirect(&$url, &$context) {
return self::singleton()->invoke(array('url', 'context'), $url,
$context, self::$_nullObject,
self::$_nullObject, self::$_nullObject, self::$_nullObject,
'civicrm_alterRedirect'
);
}

/**
* @param $varType
* @param $var
Expand Down
12 changes: 10 additions & 2 deletions CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,17 +433,25 @@ public static function getClassName($object) {
*
* @param string $url
* The URL to provide to the browser via the Location header.
* @param array $context
* Optional additional information for the hook.
*/
public static function redirect($url = NULL) {
public static function redirect($url = NULL, $context = []) {
if (!$url) {
$url = self::url('civicrm/dashboard', 'reset=1');
}
// replace the & characters with &
// this is kinda hackish but not sure how to do it right
$url = str_replace('&', '&', $url);

$context['output'] = CRM_Utils_Array::value('snippet', $_GET);

$parsedUrl = CRM_Utils_Url::parseUrl($url);
CRM_Utils_Hook::alterRedirect($parsedUrl, $context);
$url = CRM_Utils_Url::unparseUrl($parsedUrl);

// If we are in a json context, respond appropriately
if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
if ($context['output'] === 'json') {
CRM_Core_Page_AJAX::returnJsonResponse(array(
'status' => 'redirect',
'userContext' => $url,
Expand Down
55 changes: 55 additions & 0 deletions CRM/Utils/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface;

class CRM_Utils_Url {

/**
* Parse url to a UriInterface.
*
* @param string $url
*
* @return UriInterface
*/
public static function parseUrl($url) {
return new Uri($url);
}

/**
* Unparse url back to a string.
*
* @param UriInterface $parsed
*
* @return string
*/
public static function unparseUrl(UriInterface $parsed) {
return $parsed->__toString();
}

}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"pear/Auth_SASL": "1.1.0",
"pear/Net_SMTP": "1.6.*",
"pear/Net_socket": "1.0.*",
"civicrm/civicrm-setup": "~0.2.0"
"civicrm/civicrm-setup": "~0.2.0",
"guzzlehttp/guzzle": "^6.3"
},
"repositories": [
{
Expand Down
233 changes: 232 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8c6a526

Please sign in to comment.