1
24

File insertion

Create site23 by copying site22.

  1. /cms
    1. ...
    2. site22
    3. site23

In this chapter, we are going to insert a content from an external file.

To test the result online, enter http://www.frasq.org/cms/site23 in the address bar of your navigator. Click on the ladybug in the bamboo. The page displays the information returned by PHP about the host system.

Add the file sysinfo.php in the folder files with following content:

  1. /cms/site23
    1. files
      1. sysinfo.php
  1. <p>PHP <?php echo phpversion(); ?><br />
  2. MySQL <?php echo mysql_get_server_info(); ?><br />
  3. <?php echo php_uname('s'); ?>&nbsp;<?php echo php_uname('r'); ?></p>

Test the program:

$ php -a
php> require_once 'includes/db.inc';
php> require_once 'library/db.php';
php> db_connect($db_url);
php> include 'files/sysinfo.php';
<p>PHP 5.3.2-1ubuntu4.7<br />
MySQL 5.1.41-3ubuntu12.10<br />
Linux&nbsp;2.6.32-30-generic</p>
php> quit

Create the table content_infile:

CREATE TABLE content_infile (
  content_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  locale enum('fr','en') NOT NULL DEFAULT 'fr',
  `path` VARCHAR(200) DEFAULT NULL,
  PRIMARY KEY (content_id,locale)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

Add a content in English and in French:

INSERT INTO content_infile (content_id, locale, `path`) VALUES
(1, 'fr', 'files/sysinfo.php'),
(1, 'en', 'files/sysinfo.php');

Add a new node:

INSERT INTO node (node_id, user_id, created, modified) VALUES
(2, 1, NOW(), NOW());

Complete the node with its version in English and in French in node_locale:

INSERT INTO node_locale (node_id, locale, `name`, title, abstract, cloud) VALUES
('2', 'fr', 'informations-systeme', 'Informations système', 'version PHP MySQL Linux', 'Versions de PHP, de MySQL et du système.'),
('2', 'en', 'system-information', 'System information', 'version PHP MySQL Linux', 'PHP, MySQL and system versions.');

Add node 2 to thread 1:

INSERT INTO thread_node (thread_id, node_id, `number`) VALUES
('1', '2', '1');

Add the content type infile to the node_content table:

ALTER TABLE node_content CHANGE content_type content_type ENUM( 'text', 'infile' )
CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL DEFAULT 'text';

Add content_infile 1 to node 2:

INSERT INTO node_content (node_id, content_id, content_type, `number`)
VALUES ('2', '1', 'infile', '1');

Try a few requests on the DB:

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 thread_node tn
JOIN node n ON n.node_id=tn.node_id
JOIN node_locale nl ON nl.node_id=tn.node_id AND nl.locale='en'
WHERE tn.thread_id=1 AND tn.node_id=2
LIMIT 1;

Returns node 2 in English.

SELECT nc.content_id AS content_id, nc.content_type AS content_type, nc.number AS content_number, ct.text AS content_text, ct.eval AS content_eval, cin.path AS content_infile
FROM node_content nc
LEFT JOIN content_text ct ON nc.content_type = 'text'
AND ct.content_id = nc.content_id
AND ct.locale = 'fr'
LEFT JOIN content_infile cin ON nc.content_type = 'infile'
AND cin.content_id = nc.content_id
AND cin.locale = 'fr'
WHERE nc.node_id =2
ORDER BY nc.`number`;

Returns all the contents of node 2 in French.

Modify the file models/node.inc to add reading the table content_infile in the function node_get_contents:

  1. function node_get_contents($lang, $node_id) {
  2.     $sqllang=db_sql_arg($lang, false);
  3.  
  4.     $tabnodecontent=db_prefix_table('node_content');
  5.     $tabcontenttext=db_prefix_table('content_text');
  6.     $tabcontentinfile=db_prefix_table('content_infile');
  7.  
  8.     $sql="SELECT nc.content_id AS content_id, nc.content_type AS content_type, nc.number AS content_number, ct.text AS content_text, ct.eval AS content_eval, cin.path AS content_infile FROM $tabnodecontent nc LEFT JOIN $tabcontenttext ct ON nc.content_type='text' AND ct.content_id=nc.content_id AND ct.locale=$sqllang LEFT JOIN $tabcontentinfile cin ON nc.content_type='infile' AND cin.content_id=nc.content_id AND cin.locale=$sqllang WHERE nc.node_id=$node_id ORDER BY nc.number";
  9.  
  10.     $r = db_query($sql);
  11.  
  12.     return $r;
  13. }

Joins the content of the content_file table and adds the field content_infile to the returned array.

  1.                 case 'infile':
  2.                     $infile=$c['content_infile'];
  3.                     if ($infile) {
  4.                         $contents[] = compact('type', 'infile');
  5.                     }
  6.                     break;

Simply passes the name of the file to the view.

Add the treatment of a content of the type infile by the view in the file views/nodecontent.phtml:

  1. foreach ($contents as $c) {
  2.     switch($c['type']) {
  3.         case 'text':
  4.             $s = $c['text'];
  5.             echo $s, PHP_EOL;
  6.             break;
  7.         case 'infile':
  8.             include $c['infile'];
  9.             break;
  10.         default:
  11.             break;
  12.     }
  13. }

Inserts the file with the include function of PHP. IMPORTANT: The content of the file is evaluated by PHP.

Add the view which builds the board in the folders views/en for the English version and views/fr for the French version:

  1. /cms/site23
    1. views
      1. en
        1. board.phtml
      2. fr
        1. board.phtml
  1. <div id="board">
  2. <a id="bug" href="<?php echo $base_path; ?>/en/system-information"></a>
  3. </div>
  1. <div id="board">
  2. <a id="bug" href="<?php echo $base_path; ?>/fr/informations-systeme"></a>
  3. </div>

These views don't display anything. The images are placed by the style sheet. Add the following content to the file css/theme.css:

  1. #board {height:200px;margin-bottom:0.5em;background:transparent url(../images/bamboo.png) no-repeat center center;}
  2. #board #bug {float:right;width:64px;height:64px;position:relative;background:transparent url(../images/ladybug.png) no-repeat;cursor:default;}
  3. #board #bug {top:156px;right:210px;}
  4. #board #bug:hover {top:158px;right:213px;}

Add the images for the background and the ladybug in the folder images:

  1. /cms/site23
    1. images
      1. bamboo.png
      2. ladybug.png

Modify the home:

  1.     $board = view('board', $lang);

Builds the board.

  1.     $output = layout('home', compact('footer', 'banner', 'board', 'content'));

Builds the home page with a particular document model.

Add the document model home.phtml which is speficic to the home page in the folder layouts:

  1. /cms/site23
    1. layouts
      1. home.phtml
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <?php echo $head; ?>
  5. </head>
  6. <body>
  7. <?php echo $banner; ?>
  8. <?php echo $board; ?>
  9. <div id="content">
  10. <?php echo $content; ?>
  11. </div>
  12. <?php if (isset($footer)): ?>
  13. <?php echo $footer; ?>
  14. <?php endif; ?>
  15. </body>
  16. </html>

Places the header of the document then the banner, the board, the content and the footer of the body.

Enter http://www.frasq.org/cms/site23/en in the address bar of your navigator. Move the pointer of the mouse over the ladybug. It should move. Click. The site displays the page with the system information. Check the French version.

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].