developer.jelix.org is not used any more and exists only for history. Post new tickets on the Github account.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.

Opened 10 years ago

Closed 10 years ago

#1186 closed new feature (fixed)

jUrl::getFull

Reported by: hadrien Owned by:
Priority: normal Milestone: Jelix 1.2RC1
Component: jelix:core Version: 1.2b1
Severity: normal Keywords:
Cc: Blocked By:
Blocking: Documentation needed: no
Hosting Provider: Php version:

Description

Un ptit patch pour avoir des urls absolues avec un domaine prédéfini !

diff --git a/lib/jelix/core-modules/jelix/locales/fr_FR/errors.ISO-8859-1.properties b/lib/jelix/core-modules/jelix/locales/fr_FR/errors.ISO-8859-1.properties
index 7ec7d5d..6ad4e71 100644
--- a/lib/jelix/core-modules/jelix/locales/fr_FR/errors.ISO-8859-1.properties
+++ b/lib/jelix/core-modules/jelix/locales/fr_FR/errors.ISO-8859-1.properties
@@ -5,61 +5,62 @@ unknown=(1)erreur inconnue
tech=(2)Il y a une erreur technique sur le site. Essayer de vous connecter plus tard.

#---- urls
urls.engine.notfound=(30)Le moteur d'url %s est introuvable
+urls.domain.void=(31)Domaine introuvable. Veuillez le renseigner dans votre fichier de configuration

diff --git a/lib/jelix/core-modules/jelix/locales/fr_FR/errors.ISO-8859-15.properties b/lib/jelix/core-modules/jelix/locales/fr_FR/errors.ISO-8859-15.properties
index 7ec7d5d..53f3232 100644
--- a/lib/jelix/core-modules/jelix/locales/fr_FR/errors.ISO-8859-15.properties
+++ b/lib/jelix/core-modules/jelix/locales/fr_FR/errors.ISO-8859-15.properties

#---- urls
urls.engine.notfound=(30)Le moteur d'url %s est introuvable
+urls.domain.void=(31)Domaine introuvable. Veuillez le renseigner dans votre fichier de configuration

diff --git a/lib/jelix/core/jUrl.class.php b/lib/jelix/core/jUrl.class.php
index a61dde6..c6fdcad 100644
--- a/lib/jelix/core/jUrl.class.php
+++ b/lib/jelix/core/jUrl.class.php
@@ -145,7 +145,7 @@ class jUrl extends jUrlBase {

    /**
    * Gets the url corresponding to an action, in the given format
-    * @param string $actSel  action selector. You can use # instead of the module 
+    * @param string $actSel  action selector. You can use # instead of the module
    *                or the action name, to specify the current url.
    * @param array $params associative array with the parameters
    * @param integer $what the format you want : one of the jUrl const,
@@ -169,6 +169,44 @@ class jUrl extends jUrlBase {
    }

    /**
+    * Gets the absolute url corresponding to an action, in the given format with
+    * the domainName in defaultConfig or current
+    * @param string $actSel  action selector. You can use # instead of the module
+    *                or the action name, to specify the current url.
+    * @param array $params associative array with the parameters
+    * @param integer $what the format you want : one of the jUrl const,
+    *                                     STRING XMLSTRING JURL JURLACTION
+    * @param string $domainName Customized domain name
+    * @return mixed a value, depending of the $what parameter
+    */
+    static function getFull ($actSel, $params = array (), $what=0, $domainName = null) {
+        global $gJConfig;
+
+        if (!is_null($domainName))
+        {
+            $domain = $domainName;
+        }
+        if (!is_null($gJConfig->domainName))
+        {
+            $domain = $gJConfig->domainName;
+        }
+        elseif (isset($_SERVER['HTTP_HOST']))
+        {
+            $domain = $_SERVER['HTTP_HOST'];
+        }
+        else
+        {
+            throw new jException('jelix~errors.urls.domain.void');
+        }
+
+        if (!preg_match('/^http/', $domain)) {
+            $domain = $GLOBALS['gJCoord']->request->getProtocol().$domain;
+        }
+
+        return $domain . self::get($actSel, $params, $what);
+    }
+
+    /**
     * Parse a url
     * @param string $scriptNamePath    /path/index.php
     * @param string $pathinfo          the path info of the url.
diff --git a/lib/jelix/plugins/tpl/html/function.jfullurl.php b/lib/jelix/plugins/tpl/html/function.jfullurl.php
index 10f4624..3075d12 100644
--- a/lib/jelix/plugins/tpl/html/function.jfullurl.php
+++ b/lib/jelix/plugins/tpl/html/function.jfullurl.php
@@ -19,17 +19,5 @@
 * @param boolean $escape if true, then escape the string for html
 */
function jtpl_function_html_jfullurl($tpl, $selector, $params=array(), $domain=false, $escape=true) {
-    global $gJConfig;
-
-    if (!$domain) {
-        $domain = $gJConfig->domainName;
-    }
-
-    // Add the http or https if not given
-    if (!preg_match('/^http/', $domain)) {
-        $domain = $GLOBALS['gJCoord']->request->getProtocol().$domain;
-    }
-
-    // echo the full Url
-    echo $domain.jUrl::get($selector, $params, ($escape?1:0));
+    echo jUrl::getFull($selector, $params, ($escape?1:0), $domain);
}
diff --git a/lib/jelix/plugins/tpl/text/function.jfullurl.php b/lib/jelix/plugins/tpl/text/function.jfullurl.php
index a61101c..8c01c42 100644
--- a/lib/jelix/plugins/tpl/text/function.jfullurl.php
+++ b/lib/jelix/plugins/tpl/text/function.jfullurl.php
@@ -18,17 +18,5 @@
 * @param string domain name, false if you want to use the config domain name or the server name
 */
function jtpl_function_text_jfullurl($tpl, $selector, $params=array(), $domain=false) {
-    global $gJConfig;
-
-    if (!$domain) {
-        $domain = $gJConfig->domainName;
-    }
-
-    // Add the http or https if not given
-    if (!preg_match('/^http/', $domain)) {
-        $domain = $GLOBALS['gJCoord']->request->getProtocol().$domain;
-    }
-
-    // echo the full Url
-    echo $domain.jUrl::get($selector, $params, 0);
+    echo jUrl::getFull($selector, $params, ($escape?1:0), $domain);
}
diff --git a/lib/jelix/plugins/tpl/xml/function.jfullurl.php b/lib/jelix/plugins/tpl/xml/function.jfullurl.php
index bd73879..3656b22 100644
--- a/lib/jelix/plugins/tpl/xml/function.jfullurl.php
+++ b/lib/jelix/plugins/tpl/xml/function.jfullurl.php
@@ -19,17 +19,5 @@
 * @param boolean $escape if true, then escape the string for html
 */
function jtpl_function_xul_jfullurl($tpl, $selector, $params=array(), $domain=false, $escape=true) {
-    global $gJConfig;
-
-    if (!$domain) {
-        $domain = $gJConfig->domainName;
-    }
-
-    // Add the http or https if not given
-    if (!preg_match('/^http/', $domain)) {
-        $domain = $GLOBALS['gJCoord']->request->getProtocol().$domain;
-    }
-
-    // echo the full Url
-    echo $domain.jUrl::get($selector, $params, ($escape?1:0));
+    echo jUrl::getFull($selector, $params, ($escape?1:0), $domain);
}
diff --git a/lib/jelix/plugins/tpl/xul/function.jfullurl.php b/lib/jelix/plugins/tpl/xul/function.jfullurl.php
index bd73879..3656b22 100644
--- a/lib/jelix/plugins/tpl/xul/function.jfullurl.php
+++ b/lib/jelix/plugins/tpl/xul/function.jfullurl.php
@@ -19,17 +19,5 @@
 * @param boolean $escape if true, then escape the string for html
 */
function jtpl_function_xul_jfullurl($tpl, $selector, $params=array(), $domain=false, $escape=true) {
-    global $gJConfig;
-
-    if (!$domain) {
-        $domain = $gJConfig->domainName;
-    }
-
-    // Add the http or https if not given
-    if (!preg_match('/^http/', $domain)) {
-        $domain = $GLOBALS['gJCoord']->request->getProtocol().$domain;
-    }
-
-    // echo the full Url
-    echo $domain.jUrl::get($selector, $params, ($escape?1:0));
+    echo jUrl::getFull($selector, $params, ($escape?1:0), $domain);
}

Attachments (1)

jurlgetfull.diff (6.8 KB) - added by hadrien 10 years ago.

Download all attachments as: .zip

Change History (3)

Changed 10 years ago by hadrien

comment:1 Changed 10 years ago by laurentj

  • Milestone set to Jelix 1.2

comment:2 Changed 10 years ago by laurentj

  • Component changed from jelix to jelix:core
  • Resolution set to fixed
  • review changed from review? to review-
  • Status changed from new to closed

+ if (!is_null($domainName)) + {

Sometimes, the value is false. So the test is not good

+ $domain = $domainName; + } + if (!is_null($gJConfig->domainName))

a "else" is missing.

and domainName is never null, but could be equal to ""

+ return $domain . self::get($actSel, $params, $what);

We should filter $what : we don't want values like jUrl::JURL or jUrl::JURLACTION because get() returns an object for them, not a string. And these objects don't support a domain name as a property.

I think we should support only STRING and XMLSTRING.

I fixed these issue and pushed the patch : http://bitbucket.org/jelix/jelix-trunk/changeset/c8c058eba146

Note: See TracTickets for help on using tickets.