Share Pastebin
Guest
Public paste!

babel - New action page_id / page_alias

By: a guest | Mar 18th, 2010 | Syntax: Diff | Size: 17.95 KB | Hits: 78 | Expires: Never
Copy text to clipboard
  1. Index: babel.module.php
  2. ===================================================================
  3. --- babel.module.php    (revision 8)
  4. +++ babel.module.php    (working copy)
  5. @@ -203,6 +203,10 @@
  6.                 $this->SetParameterType("assign",CLEAN_STRING);
  7.                 $this->SetParameterType("newlang",CLEAN_STRING);
  8.                 $this->SetParameterType("newurl",CLEAN_STRING);
  9. +
  10. +               //Parameters for page_id / page_alias
  11. +               $this->SetParameterType("page",CLEAN_STRING);
  12. +               $this->SetParameterType("lang",CLEAN_STRING);
  13.         }
  14.  
  15.         function HandlesEvents(){
  16. @@ -378,6 +382,11 @@
  17.                                 $this->Redirect($id, "defaultadmin", $returnid, $newparams);
  18.                                 break;
  19.  
  20. +                       case "page_alias":
  21. +                       case "page_id":
  22. +                               require "action.page.php";
  23. +                               break;
  24. +
  25.                         case "default":
  26.                         default:
  27.                                 if(isset($params["show"])){
  28. @@ -642,20 +651,41 @@
  29.                 }
  30.         }
  31.  
  32. -       function get_urls($desired_lang=false){
  33. +       function get_urls($desired_lang=false, $page_id_or_alias = false){
  34. +               global $gCms;
  35. +               $hierarchy = $gCms->variables['position'];
  36. +               $other_languages = $gCms->smarty->get_template_vars('other_languages');
  37. +               if( $page_id_or_alias !== false ) {
  38. +                       $manager =& $gCms->GetHierarchyManager();
  39. +                       $node =& $manager->sureGetNodeByAlias($page_id_or_alias);
  40. +                       if (isset($node)) {
  41. +                               $content =& $node->GetContent( false, false, true );
  42. +                               if ($content !== FALSE && is_object($content))
  43. +                               {
  44. +                                       if( $content->HasProperty( "Other_languages" ) ) {
  45. +                                               $other_languages = $content->GetPropertyValue( "Other_languages" );
  46. +                                       }
  47. +                                       $friendly_hierarchy = $content->Hierarchy();
  48. +                                       if( isset( $friendly_hierarchy ) ) {
  49. +                                       $contentops =& $gCms->GetContentOperations();
  50. +                                       $hierarchy = $contentops->CreateUnfriendlyHierarchyPosition($friendly_hierarchy);
  51. +                                       }
  52. +                               }
  53. +                       }
  54. +               }
  55. +
  56.                 if($this->GetPreference('hierarchy_redirect',true)){
  57. -                       return $this->hierarchy_get_links($desired_lang);
  58. +                       return $this->hierarchy_get_links($desired_lang, $hierarchy);
  59.                 }else{
  60. -                       return $this->manual_get_links($desired_lang);
  61. +                       return $this->manual_get_links($desired_lang, $other_languages);
  62.                 }
  63.         }
  64.  
  65. -       function hierarchy_get_links($desired_lang=false){
  66. +       function hierarchy_get_links($desired_lang=false, $hierarchy){
  67.                 // retrieves the url to the page in the desired language(s) equivalent to the current page,
  68.                 // based on hierarchy
  69.                 // if $desired_lang is false, links for all active languages will be returned
  70.                 if(!$this->available_languages || count($this->available_languages) == 0)       return false;
  71. -               global $gCms;
  72.                 $db = $this->GetDb();
  73.                 $base_url = $this->get_base_url();
  74.                 $default_hide = $this->GetPreference('default_hide', false);
  75. @@ -666,7 +696,7 @@
  76.                 }
  77.                 $module_links = $this->module_get_url();
  78.                 if(count($module_links) == count($langs) && isset($module_links[$lang[0]['langcode']])) return $module_links;
  79. -               $current_position = explode('.', $gCms->variables['position']);
  80. +               $current_position = explode('.', $hierarchy );
  81.                 $output = array();
  82.                 $rootids = array();
  83.                 $hierarchies = array();
  84. @@ -679,7 +709,7 @@
  85.                         $where .= ($where == ""?"":" OR ")."content_id=?";
  86.                         array_push($query_values, $onelang['root_id']);
  87.                 }
  88. -               $query = "SELECT content_name, menu_text, content_id, hierarchy, hierarchy_path FROM " . cms_db_prefix()."content WHERE (".$where.")";
  89. +               $query = "SELECT content_name, menu_text, content_id, hierarchy, hierarchy_path, content_alias FROM " . cms_db_prefix()."content WHERE (".$where.")";
  90.                 $dbresult = $db->Execute($query, $query_values);
  91.                 $query_values = array();
  92.                 $where = "";
  93. @@ -692,17 +722,19 @@
  94.                         $hierarchies[$rootids[$row['content_id']]] = $new_hierarchy;
  95.                         array_push($query_values,$new_hierarchy);
  96.                         $newpaths[$new_hierarchy] = $rootids[$row['content_id']];
  97. -                       $default_paths[$rootids[$row['content_id']]] = array("url"=>$base_url.$row["hierarchy_path"], "name"=>$row["content_name"], "menu_text"=>$row["menu_text"]);
  98. +                       $default_paths[$rootids[$row['content_id']]] = array("url"=>$base_url.$row["hierarchy_path"], "name"=>$row["content_name"], "menu_text"=>$row["menu_text"], "alias"=>$row["hierarchy_path"], "id"=>$row["content_id"]);
  99.                         $where .= ($where == ""?"":" OR ")."hierarchy=?";
  100.                 }
  101. -               $query = "SELECT content_id, content_name, menu_text, hierarchy, hierarchy_path FROM ".cms_db_prefix()."content WHERE (".$where.")";
  102. +               $query = "SELECT content_id, content_name, menu_text, hierarchy, hierarchy_path, content_alias FROM ".cms_db_prefix()."content WHERE (".$where.")";
  103.                 $dbresult = $db->Execute($query, $query_values);
  104. -               while($dbresult && $row = $dbresult->FetchRow())        $output[$newpaths[$row["hierarchy"]]] = array("url"=>$base_url.$row["hierarchy_path"], "name"=>$row["content_name"], "menu_text"=>$row["menu_text"]);
  105. +               while($dbresult && $row = $dbresult->FetchRow())        $output[$newpaths[$row["hierarchy"]]] = array("url"=>$base_url.$row["hierarchy_path"], "name"=>$row["content_name"], "menu_text"=>$row["menu_text"], "alias"=>$row["content_alias"], "id"=>$row["content_id"]);
  106.                 $final = array();
  107.                 foreach($output as $lang=>$value){
  108.                         if(isset($module_links[$lang])){
  109.                                 $value["url"] = $module_links[$lang]["url"];
  110.                                 $value["name"] = $module_links[$lang]["name"];
  111. +                               $value["alias"] = $module_links[$lang]["alias"];
  112. +                               $value["id"] = $module_links[$lang]["id"];
  113.                         }
  114.                         if($value["url"] == "" && $default_hide){
  115.                         }elseif($value["url"] == ""){
  116. @@ -715,12 +747,11 @@
  117.         }
  118.  
  119.  
  120. -       function manual_get_links($desired_lang=false){
  121. +       function manual_get_links($desired_lang=false, $other_languages){
  122.                 // retrieves the url to the page in the desired language(s) equivalent to the current page,
  123.                 // based on Marcos Cruz manual procedure (see help)
  124.                 // if $desired_lang is false, links for all active languages will be returned
  125.                 if(!$this->available_languages || count($this->available_languages) == 0)       return false;
  126. -               global $gCms;
  127.                 $db = $this->GetDb();
  128.                 $base_url = $this->get_base_url();
  129.                 if($desired_lang && isset($this->available_languages[$desired_lang])){
  130. @@ -737,22 +768,23 @@
  131.                 $btn_text = $this->GetPreference("use_page_title",false)?"":$newlang['menu_name'];
  132.  
  133.                 // we retrieve the alternatives from the content block
  134. -               $other_languages = $gCms->smarty->get_template_vars('other_languages');
  135.                 $alternatives = array();
  136.                 $wherevalues = array();
  137.                 $where = "";
  138.                 foreach ( explode(';', $other_languages) as $onelang ){
  139.                         $parts = explode('=',$onelang);
  140.                         if(count($parts)==2){
  141. -                               $where .= ($where==""?"":" OR ")."content_alias=?";
  142. -                               $alternatives[$parts[1]] = $parts[0];
  143. -                               array_push($wherevalues, $parts[1]);
  144. +                               if( array_key_exists( $parts[0], $langs ) ) {
  145. +                                       $where .= ($where==""?"":" OR ")."content_alias=?";
  146. +                                       $alternatives[$parts[1]] = $parts[0];
  147. +                                       array_push($wherevalues, $parts[1]);
  148. +                               }
  149.                         }
  150.                 }
  151. -               $query = "SELECT menu_text, content_name, content_alias, hierarchy_path FROM ".cms_db_prefix()."content WHERE (".$where.")";
  152. +               $query = "SELECT menu_text, content_name, content_alias, hierarchy_path, content_alias FROM ".cms_db_prefix()."content WHERE (".$where.")";
  153.                 $dbresult = $db->Execute($query, $wherevalues);
  154.                 while($dbresult && $row = $dbresult->FetchRow()){
  155. -                       $output[$alternatives[$row["content_alias"]]] = array("url"=>$base_url.$row["hierarchy_path"], "name"=>$row["content_name"], "menu_text"=>$row["menu_text"]);
  156. +                       $output[$alternatives[$row["content_alias"]]] = array("url"=>$base_url.$row["hierarchy_path"], "name"=>$row["content_name"], "menu_text"=>$row["menu_text"], "alias"=>$row["content_alias"], "id"=>$row["content_id"]);
  157.                 }
  158.                 if($this->GetPreference('default_hide', false)) return $output;
  159.  
  160. @@ -769,10 +801,10 @@
  161.                 }
  162.                 if($where == "")        return $output;
  163.  
  164. -               $query = "SELECT menu_text, content_name, content_id, hierarchy, hierarchy_path FROM " . cms_db_prefix()."content WHERE (".$where.")";
  165. +               $query = "SELECT menu_text, content_name, content_id, hierarchy, hierarchy_path, content_alias FROM " . cms_db_prefix()."content WHERE (".$where.")";
  166.                 $dbresult = $db->Execute($query, $wherevalues);
  167.                 while($dbresult && $row = $dbresult->FetchRow()){
  168. -                       $output[$idtolang[$row["content_id"]]] = array("url"=>$base_url.$row["hierarchy_path"], "name"=>$row["content_name"], "menu_text"=>$row["menu_text"]);
  169. +                       $output[$idtolang[$row["content_id"]]] = array("url"=>$base_url.$row["hierarchy_path"], "name"=>$row["content_name"], "menu_text"=>$row["menu_text"], "alias"=>$row["content_alias"], "id"=>$row["content_id"]);
  170.                 }
  171.                 return $output;
  172.         }
  173. Index: lang/en_US.php
  174. ===================================================================
  175. --- lang/en_US.php      (revision 8)
  176. +++ lang/en_US.php      (working copy)
  177. @@ -102,6 +102,14 @@
  178.                                         <p>Root language pages (1. and 2.) should be section headers. You can name them (alias and all) any way you want.</p>
  179.                                         <p>In order to display menu correctly (that is, not to display language header pages and pages belonging to other languages), all you have to do is add the start_level=\"2\" to your menu tags:<br/>
  180.                                         {menu start_level=\"2\"}</p>
  181. +                                       <p>Sometime, you cannot always use menus for navigation. If you have some \"hardcoded\" links in your pages, templates or global content blocks (that is, if you use {cms_selflink page=\".....\"}), all you have to do is get the page id (or alias) in a smarty variable and use it :<br/><br/>
  182. +                                       <b>Before :</b> {cms_selflink page=\"index\"}<br/><br/>
  183. +                                       <b>After :</b> {babel action=\"page_id\" page=\"index\" assign=\"id_of_my_page_index\"}{cms_selflink page=\$id_of_my_page_index}<br/>
  184. +                                       or<br/>
  185. +                                       {babel action=\"page_alias\" page=\"index\" assign=\"alias_of_my_page_index\"}{cms_selflink page=\$alias_of_my_page_index}<br/>
  186. +                                       <br/>
  187. +                                       ( You can also force the target language with the parameter lang=\"....\" : {babel lang=\"fr_FR\" action=\"page_alias\" page=\"index\" assign=\"alias_of_my_page_index_in_french\"}{cms_selflink page=\$alias_of_my_page_index_in_french} )<br/>
  188. +</p>
  189.                                 <br/><h4>Part 2: setting up the languages</h4>
  190.                                         <p>Next, go to Extensions-&gt;Babel: Multilingual site. For each language, click Add Language and enter the appropriate informations. The \"Root page\" should be the section headers you've created for each language (1.English, 2.Fran&ccedil;ais). The \"Default page\" should be the home page for each language. As of the language code, you could use any 5 characters code, but in order to provide good compatibility with the modules, you should use the same as the cms (you may use the drop down list on the top right to partially fill the form).</p>
  191.                                         <p>( *When ready, make sur you activate each language... )</p>
  192. Index: lang/ext/fr_FR.php
  193. ===================================================================
  194. --- lang/ext/fr_FR.php  (revision 8)
  195. +++ lang/ext/fr_FR.php  (working copy)
  196. @@ -95,6 +95,14 @@
  197.                                         <p>Les pages racine (1. and 2.) devrait &ecirc;tre des en-t&ecirc;tes de section. Vous pouvez leur donner n'importe quel nom ou alias.</p>
  198.                                         <p>Pour afficher vos menus correctement (c'est-&agrave;-dire, pour ne pas afficher les pages racines et les pages d'une autre langue), il vous suffira d'ajouter start_level=\"2\" &agrave; vos balises de menu:<br/>
  199.                                         {menu start_level=\"2\"}</p>
  200. +                                       <p>Il n'est pas toujours possible d'utiliser des menus pour la navigation. Si vous avez de liens \"en dur\" dans vos pages, gabarits ou blocs de contenu global (c'est-à-dire, si vous utilisez le tag {cms_selflink page=\".....\"}), il suffit de récupérer l'id (ou l'alias) de la page concernée dans une variable smarty et de l'utiliser :<br/><br/>
  201. +                                       <b>Avant :</b> {cms_selflink page=\"index\"}<br/><br/>
  202. +                                       <b>Après :</b> {babel action=\"page_id\" page=\"index\" assign=\"id_de_ma_page_index\"}{cms_selflink page=\$id_de_ma_page_index}<br/>
  203. +                                       ou<br/>
  204. +                                       {babel action=\"page_alias\" page=\"index\" assign=\"alias_de_ma_page_index\"}{cms_selflink page=\$alias_de_ma_page_index}<br/>
  205. +                                       <br/>
  206. +                                       ( Il est également possible de forcer la langue souhaitée, avec le paramètre lang=\"....\" : {babel lang=\"fr_FR\" action=\"page_alias\" page=\"index\" assign=\"alias_de_ma_page_index_en_francais\"}{cms_selflink page=\$alias_de_ma_page_index_en_francais} )<br/>
  207. +</p>
  208.                                 <br/><h3>&Eacute;tape 2: cr&eacute;ation des langues</h3>
  209.                                         <p>Ensuite, rendez-vous dans Extensions-&gt;Babel: Site multilingue. Pour chaque langue, cliquez Ajouter une langue et entrez les informations appropri&eacute;es. La \"Page racine\" devrait &ecirc;tre l'en-t&ecirc;te de section que vous avez cr&eacute;&eacute; pour chaque langue (1.English, 2.Fran&ccedil;ais). La \"Page par d&eacute;faut\" devrait être la page d'accueil pour chaque langue. En ce qui concerne le code de langue, vous pourriez utiliser n'importe quel code de 5 caract&egrave;res, mais pour une bonne compatibilit&eacute; avec les autres modules, il est pr&eacute;f&eacute;rable d'utiliser les m&ecirc;mes que le cms (vous pouvez utiliser la liste d&eacute;roulante en haut &agrave; droite pour remplir partiellement le formulaire).</p>
  210.                                         <p>( *Lorsque les langues sont pr&ecirc;tes &agrave; l'utilisation, n'oubliez pas de les activer...)</p>
  211. Index: action.page.php
  212. ===================================================================
  213. --- action.page.php     (revision 0)
  214. +++ action.page.php     (revision 0)
  215. @@ -0,0 +1,36 @@
  216. +<?php
  217. +if (!isset($gCms)) exit;
  218. +if(!$this->current_langcode)   $this->assign_page_lang();
  219. +if(!$this->current_langcode)   return false;   // no page lang assigned... maybe we aren't in a page that has a particular language?
  220. +$current_language = $this->current_langcode;
  221. +
  222. +$result = "";
  223. +
  224. +//Parameter : $lang
  225. +if( isset( $params["lang"] ) && ($params["lang"]!="") ) {
  226. +       $current_language = $params["lang"];
  227. +}
  228. +
  229. +//Parameter : $page
  230. +if( isset( $params["page"] ) ) {
  231. +
  232. +       // Get an array of information on the page corresponding to this Page-or-ID, in the current language
  233. +       $links = $this->get_urls( $current_language, $params["page"] );
  234. +
  235. +       // If the result is valid, set $page to it.
  236. +       if( isset( $links[ $current_language ] ) ) {
  237. +               $newlink = $links[ $current_language ];
  238. +               if( $action == "page_id" ) {
  239. +                       if( isset( $newlink[ "id" ] ) ) {
  240. +                               $result = $newlink[ "id" ];
  241. +                       }
  242. +               } else {
  243. +                       if( isset( $newlink[ "alias" ] ) ) {
  244. +                               $result = $newlink[ "alias" ];
  245. +                       }
  246. +               }
  247. +       }
  248. +}
  249. +
  250. +echo $result;
  251. +?>
  252.  
  253. Property changes on: action.page.php
  254. ___________________________________________________________________
  255. Added: svn:mime-type
  256.    + text/plain
  257. Added: svn:eol-style
  258.    + native