Sep 07, 2008, 01:55 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
modxcms.com web
  MODxCMS.com   Forums   Help Login Register  
News:Donate to MODx: Donations
Pages: [1]   Go Down
  Print  
Author Topic: [function] Smart ModX Url function (for snippet developers)  (Read 5399 times)
0 Members and 1 Guest are viewing this topic.
bS
Coding Team
*
Posts: 397



WWW
« on: Jun 21, 2006, 07:51 AM »

I don't know if it's really usefull but I needed this functionality for pagination I include in my snippets.
The function will keep the original url+querystring and keep all values that are not changed intact. This way the link it produces works in every situation.

For example if your snippet is called together with another snippet that uses some querystring value you can't possibly now what that value will be unless you preserve the url. When you create a link to a certain ability of your own snippet it will not break the other snippet call.

you will give either a docid or a docalias of the link and put the extra querystring values in an array.

function:
Code:
function smartModxUrl($docid, $docalias, $array_values) {
global $modx;
$array_url = $_GET;
$urlstring = array();

unset($array_url["id"]);
unset($array_url["q"]);

$array_url = array_merge($array_url,$array_values);

foreach ($array_url as $name => $value) {
if (!is_null($value)) {
  $urlstring[] = $name . '=' . urlencode($value);
}
}

return $modx->makeUrl($docid, $docalias, join('&',$urlstring));
}

example usage:
Code:
$link['page'] = 3;
$link['aname'] = 'avalue';
$link['another'] = 'one';

echo smartModxUrl($modx->documentObject["id"],NULL, $link);
« Last Edit: Jul 12, 2006, 03:12 PM by PaulGregory » Logged

Armand Pondman
MODx Coding Team
:: Jot :: PHx
doze
Coding Team
*
Posts: 3,046


....Boom!


« Reply #1 on: Jun 21, 2006, 08:02 AM »

Thanks, gonna implement this to the maxigallery pagination too.. don't know that is it needed in any case, but I can't think of all ways that the gallery snippet could be used, so I'll add this just for kicks..

But I'm a bit worried about this issue with makeUrl api call  Undecided
Logged

MODxWiki || Please, list wiki worthy material here!
bS
Coding Team
*
Posts: 397



WWW
« Reply #2 on: Jun 21, 2006, 08:37 AM »

Thanks, gonna implement this to the maxigallery pagination too.. don't know that is it needed in any case, but I can't think of all ways that the gallery snippet could be used, so I'll add this just for kicks..

But I'm a bit worried about this issue with makeUrl api call  Undecided

well to be exact.. the reason i wrote this was  because of the maxigallery+replix combination Smiley so in reverse if someone adds comments to a gallery.. and you have pagination enabled there will be two pagination abilities (one in maxigallery and one in replix) if you switch to antoher page in the gallery, the comments will switch back to page 1 (that is if you don't preserve the querystring)
Logged

Armand Pondman
MODx Coding Team
:: Jot :: PHx
Mark
Coding Team
*
Posts: 3,247


Ditto Developer


WWW
« Reply #3 on: Jun 21, 2006, 09:06 AM »

What is the purpose of this line:

Code:
foreach ($array_values as $name => $value) {
$array_url[$name] = $value;
}
Logged

bS
Coding Team
*
Posts: 397



WWW
« Reply #4 on: Jun 21, 2006, 09:11 AM »

unnecessary lines of code  lol
can be replaced with

Code:
$array_url = $array_url + $array_values;

the lines fuses the $_GET array and the new values array (thus overwriting any previous value)

update: changed in original post

I also want to note that to completely remove a value from teh querystring you have to set it to NULL (not '')

like this:  $array['name'] = NULL;
« Last Edit: Jun 21, 2006, 09:14 AM by bS » Logged

Armand Pondman
MODx Coding Team
:: Jot :: PHx
bS
Coding Team
*
Posts: 397



WWW
« Reply #5 on: Jun 23, 2006, 06:49 AM »

again same line of code replaced with:
Code:
$array_url = array_merge($array_url,$array_values);

because of strange results in some situations.
Logged

Armand Pondman
MODx Coding Team
:: Jot :: PHx
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP

Copyright © 2005-2008 MODxCMS, All rights reserved. Contact Us
Styles by ziworks.com

Powered by SMF 1.1.4 | SMF © 2005, Simple Machines LLC

Valid XHTML 1.0! Valid CSS!