File insertion
Create site23 by copying site22.
- /cms
- ...
- site22
- 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:
- /cms/site23
- files
- sysinfo.php
- files
- <p>PHP <?php echo phpversion(); ?><br />
- MySQL <?php echo mysql_get_server_info(); ?><br />
- <?php echo php_uname('s'); ?> <?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 2.6.32-30-generic</p>
php> quit
Create the 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:
(1, 'fr', 'files/sysinfo.php'),
(1, 'en', 'files/sysinfo.php');
Add a new node
:
(2, 1, NOW(), NOW());
Complete the node
with its version in English and in French in node_locale
:
('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:
('1', '2', '1');
Add the content type infile
to the node_content
table:
CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL DEFAULT 'text';
Add content_infile
1 to node
2:
VALUES ('2', '1', 'infile', '1');
Try a few requests on the DB:
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.
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
:
- function node_get_contents($lang, $node_id) {
- $sqllang=db_sql_arg($lang, false);
- $tabnodecontent=db_prefix_table('node_content');
- $tabcontenttext=db_prefix_table('content_text');
- $tabcontentinfile=db_prefix_table('content_infile');
- $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";
- $r = db_query($sql);
- return $r;
- }
Joins the content of the content_file
table and adds the field content_infile
to the returned array.
- case 'infile':
- $infile=$c['content_infile'];
- if ($infile) {
- $contents[] = compact('type', 'infile');
- }
- 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:
- foreach ($contents as $c) {
- switch($c['type']) {
- case 'text':
- $s = $c['text'];
- echo $s, PHP_EOL;
- break;
- case 'infile':
- include $c['infile'];
- break;
- default:
- break;
- }
- }
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:
- /cms/site23
- views
- en
- board.phtml
- fr
- board.phtml
- en
- views
- <div id="board">
- <a id="bug" href="<?php echo $base_path; ?>/en/system-information"></a>
- </div>
- <div id="board">
- <a id="bug" href="<?php echo $base_path; ?>/fr/informations-systeme"></a>
- </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:
- #board {height:200px;margin-bottom:0.5em;background:transparent url(../images/bamboo.png) no-repeat center center;}
- #board #bug {float:right;width:64px;height:64px;position:relative;background:transparent url(../images/ladybug.png) no-repeat;cursor:default;}
- #board #bug {top:156px;right:210px;}
- #board #bug:hover {top:158px;right:213px;}
Add the images for the background and the ladybug in the folder images:
- /cms/site23
- images
- bamboo.png
- ladybug.png
- images
Modify the home:
- $board = view('board', $lang);
Builds the board.
- $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:
- /cms/site23
- layouts
- home.phtml
- layouts
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <?php echo $head; ?>
- </head>
- <body>
- <?php echo $banner; ?>
- <?php echo $board; ?>
- <div id="content">
- <?php echo $content; ?>
- </div>
- <?php if (isset($footer)): ?>
- <?php echo $footer; ?>
- <?php endif; ?>
- </body>
- </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