31

Page layout

Create site21 by copying site20.

  1. /cms
    1. ...
    2. site20
    3. site21

In this chapter, we are going to create a direct access to a node in a default thread with a specific page layout.

To test the result online, enter http://www.frasq.org/cms/site21/en in the address bar of your navigator. Click on the link Legal information in the footer of the home page. The site displays the content of node 1 of thread 1 with a simple page layout.

Create the action page by adding the file page.php in the folder actions with the following content:

  1. /cms/site21
    1. actions
      1. page.php
  1. require_once 'models/thread.inc';
  2.  
  3. function page($lang, $arglist=false) {
  4.     $page=false;
  5.  
  6.     if (is_array($arglist)) {
  7.         if (isset($arglist[0])) {
  8.             $page=$arglist[0];
  9.         }
  10.     }
  11.  
  12.     if (!$page) {
  13.         return run('error/notfound', $lang);
  14.     }
  15.  
  16.     $thread_id = 1;
  17.  
  18.     $page_id = thread_node_id($thread_id, $page);
  19.     if (!$page_id) {
  20.         return run('error/notfound', $lang);
  21.     }
  22.  
  23.     $r = thread_get_node($lang, $thread_id, $page_id);
  24.     if (!$r) {
  25.         return run('error/notfound', $lang);
  26.     }
  27.     extract($r); /* node_name node_title node_abstract node_cloud node_nocomment */
  28.  
  29.     $page_name=$node_name;
  30.     $page_title=$node_title;
  31.     $page_abstract=$node_abstract;
  32.     $page_cloud=$node_cloud;
  33.  
  34.     $page_contents = build('nodecontent', $lang, $page_id);
  35.  
  36.     head('title', $page_title);
  37.     head('description', $page_abstract);
  38.     head('keywords', $page_cloud);
  39.  
  40.     $validate='/' . $lang . '/'. $page_name;
  41.     $banner = build('banner', $lang, compact('validate'));
  42.  
  43.     $content = view('page', false, compact('page_name', 'page_title', 'page_contents'));
  44.  
  45.     $output = layout('standard', compact('banner', 'content'));
  46.  
  47.     return $output;
  48. }

Notice that the page action looks for nodes in the thread 1.

Give access to the page action by adding an alias for each language in the file includes/aliases.inc:

  1.         'page'                  => 'page',
  1.         'page'                  => 'page',

Since the view doesn't depend on the language, put it directly in the folder views:

  1. /cms/site21
    1. views
      1. page.phtml
  1. <h3><?php echo htmlspecialchars($page_title, ENT_COMPAT, 'UTF-8'); ?></h3>
  2. <?php
  3. if ($page_contents) {
  4.     echo $page_contents;
  5. }
  6. ?>

Add the function thread_get_node in the file models/thread.inc:

  1. function thread_get_node($lang, $thread_id, $node_id) {
  2.     $sqllang=db_sql_arg($lang, false);
  3.  
  4.     $tabthreadnode=db_prefix_table('thread_node');
  5.     $tabnode=db_prefix_table('node');
  6.     $tabnodelocale=db_prefix_table('node_locale');
  7.  
  8.     $sql="SELECT tn.number AS node_number, nl.name AS node_name, nl.title AS node_title, nl.abstract AS node_abstract, nl.cloud AS node_cloud FROM $tabthreadnode tn JOIN $tabnode n ON n.node_id=tn.node_id JOIN $tabnodelocale nl ON nl.node_id=tn.node_id AND nl.locale=$sqllang WHERE tn.thread_id=$thread_id AND tn.node_id=$node_id LIMIT 1";
  9.  
  10.     $r = db_query($sql);
  11.  
  12.     return $r ? $r[0] : false;
  13. }

thread_get_node returns all the attributes for the language $lang of the node whose identifier is $node_id which is in the thread whose identifier is $thread_id, or false in case of error.

Enter http://local.frasq.org/cms/site21/en/page/legal-information in the address bar of your navigator. Try the version in French at the address http://localhost/cms/site21/fr/page/informations-legales.

Modify how a URL is analyzed in the route function defined in the file library/engine.php so the page action become the default action:

  1.     return array('page', $args);

The last step consists in adding a link to the footer.

  1. /cms/site21
    1. views
      1. en
        1. footer.phtml
      2. fr
        1. footer.phtml
<p><a href="<?php echo $contact_page; ?>">Contact</a>&nbsp;|&nbsp;<a href="<?php echo $base_path; ?>/en/legal-information">Legal information</a>&nbsp;|&nbsp;<img src="<?php echo $base_path; ?>/images/ubuntu.png" alt="" width="16" height="16"/>&nbsp;<a href="http://www.ubuntu.com" target="_blank">Ubuntu</a></p>
<p><a href="<?php echo $contact_page; ?>">Contact</a>&nbsp;|&nbsp;<a href="<?php echo $base_path; ?>/fr/informations-legales">Informations légales</a>&nbsp;|&nbsp;<img src="<?php echo $base_path; ?>/images/ubuntu.png" alt="" width="16" height="16" />&nbsp;<a href="http://www.ubuntu.com" target="_blank">Ubuntu</a></p>

Enter http://localhost/cms/site21/en in the address bar of your navigator. Click on the link Legal information in the footer. Check the version in French.

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].