Created a new folder in _templates to house site icons. Put the new RSS standard...
[elgg.git] / lib / templates.php
blob2a1dd95e416d663a8000240fe570987aca0911d0
1 <?php
3 /*** NZVLE TODO
4 ***
5 *** + Break the html/css bits of the default template into separate files
6 *** they mess up the code layout and indentation, and generally don't
7 *** belong here.
8 *** + Clean up and document the calling conventions -- get rid of $parameter
9 *** + the callbacks to template_variables_substitute are *evil* -- rework
10 ***
11 ***/
13 function default_template () {
15 global $CFG;
16 global $template;
17 global $template_definition;
18 $sitename = $CFG->sitename;
20 $run_result = '';
22 $template_definition[] = array(
23 'id' => 'pageshell',
24 'name' => gettext("Page Shell"),
25 'description' => gettext("The main page shell, including headers and footers."),
26 'glossary' => array(
27 '{{metatags}}' => gettext("Page metatags (mandatory) - must be in the 'head' portion of the page"),
28 '{{title}}' => gettext("Page title"),
29 '{{menu}}' => gettext("Menu"),
30 '{{topmenu}}' => gettext("Status menu"),
31 '{{mainbody}}' => gettext("Main body"),
32 '{{sidebar}}' => gettext("Sidebar")
36 $welcome = gettext("Welcome"); // gettext variable
38 $template['pageshell'] = <<< END
40 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
41 <html xmlns="http://www.w3.org/1999/xhtml">
42 <head>
43 <title>{{title}}</title>
44 {{metatags}}
45 </head>
46 <body>
47 <!-- elgg banner and logo -->
48 <div id="container"><!-- start container -->
49 <div id="statusbar"><!-- start statusbar -->
50 <div id="welcome"><!-- start welcome -->
51 <p>$welcome {{userfullname}}</p>
52 </div><!-- end welcome -->
53 {{topmenu}}
54 </div><!-- end statusbar -->
55 <div id="header"><!-- start header -->
56 <h1>$sitename</h1>
57 <h2>Community learning space</h2>
58 <ul id="navigation">
59 {{menu}}
60 </ul>
61 </div><!-- end header -->
62 <div id="content_holder"><!-- start contentholder -->
63 <div id="maincontent_container"><!-- start main content -->
64 {{messageshell}}
65 {{mainbody}}
66 </div><!-- end main content -->
67 <div id="sidebar_container">
68 <div id="sidebar"><!-- start sidebar -->
69 <ul><!-- open sidebar lists -->
70 {{sidebar}}
71 </ul>
72 </div><!-- end sidebar -->
73 </div><!-- end sidebar_container -->
74 </div><!-- end contentholder -->
75 <div class="clearall" />
76 <div id="footer"><!-- start footer -->
77 <a href="http://elgg.net"><img src="{$url}_templates/elgg_powered.png" alt="Powered by Elgg" title="Powered by Elgg" border="0" /></a>
78 </div><!-- end footer -->
79 </div><!-- end container -->
80 </body>
81 </html>
83 END;
85 // REMOVED stylesheet (was old version and should not have been here)
86 // TODO: extract all default template stuff from lib/templates.php
88 $template_definition[] = array(
89 'id' => 'contentholder',
90 'name' => gettext("Content holder"),
91 'description' => gettext("Contains the main content for a page (as opposed to the sidebar or the title)."),
92 'glossary' => array(
93 '{{title}}' => gettext("The title"),
94 '{{submenu}}' => gettext("The page submenu"),
95 '{{body}}' => gettext("The body of the page")
97 );
99 $template['contentholder'] = <<< END
101 <div class="SectionContent">
103 <h1>{{title}}</h1>
104 {{submenu}}
105 </div>
106 {{body}}
108 END;
110 $template_definition[] = array(
111 'id' => 'ownerbox',
112 'name' => gettext("Owner box"),
113 'description' => gettext("A box containing a description of the owner of the current profile."),
114 'glossary' => array(
115 '{{name}}' => gettext("The user's name"),
116 '{{profileurl}}' => gettext("The URL of the user's profile page, including terminating slash"),
117 '{{usericon}}' => gettext("The user's icon, if it exists"),
118 '{{tagline}}' => gettext("A short blurb about the user"),
119 '{{usermenu}}' => gettext("Links to friend / unfriend a user"),
120 '{{lmshosts}}' => gettext("Links to any lms hosts the user is attached to"),
124 $tags = gettext("Tags");
125 $resources = gettext("Resources");
126 $template['ownerbox'] = <<< END
128 <div class="me">
129 <div style="float: left; width: 70px"><a href="{{profileurl}}">{{usericon}}</a></div>
130 <div style="margin-left: 75px; margin-top: 0px; padding-top: 0px; text-align: left" ><p>
131 <span class="userdetails">{{name}}<br /><a href="{{profileurl}}rss/">RSS</a> | <a href="{{profileurl}}tags/">$tags</a> | <a href="{{profileurl}}newsclient/">$resources</a></span></p>
132 <p>{{tagline}}</p>
133 <p>{{lmshosts}}</p>
134 <p style="margin-bottom: 3px" class="usermenu">{{usermenu}}</p>
135 </div>
136 </div>
138 END;
140 $template_definition[] = array(
141 'id' => 'infobox',
142 'name' => gettext("Information Box"),
143 'description' => gettext("A box containing a caption and some text, used extensively throughout the site. For example, the 'friends' box and most page bodies are info boxes. Of course, you can alter this template however you wish - it doesn't need to be an actual box."),
144 'glossary' => array(
145 '{{name}}' => gettext("The title"),
146 '{{contents}}' => gettext("The contents of the box")
149 $template['infobox'] = <<< END
151 <table class="infobox" width="100%">
152 <caption align="top">
153 {{name}}
154 </caption>
155 <tr>
156 <td>
157 {{contents}}
158 </td>
159 </tr>
160 </table><br />
161 END;
163 $template_definition[] = array(
164 'id' => 'messageshell',
165 'name' => gettext("System message shell"),
166 'description' => gettext("A list of system messages will be placed within the message shell."),
167 'glossary' => array(
168 '{{messages}}' => gettext("The messages")
172 $template['messageshell'] = <<< END
174 <div id="js">{{messages}}</div><br />
176 END;
178 $template_definition[] = array(
179 'id' => 'messages',
180 'name' => gettext("Individual system messages"),
181 'description' => gettext("Each individual system message."),
182 'glossary' => array(
183 '{{message}}' => gettext("The system message")
187 $template['messages'] = <<< END
190 {{message}}
191 </p>
193 END;
196 $template_definition[] = array(
197 'id' => 'menu',
198 'name' => gettext("Main menu shell"),
199 'description' => gettext("A list of main menu items will be placed within the menubar shell."),
200 'glossary' => array(
201 '{{menuitems}}' => gettext("The menu items")
205 $template['menu'] = <<< END
207 {{menuitems}}
208 END;
210 $template_definition[] = array(
211 'id' => 'menuitem',
212 'name' => gettext("Individual main menu item"),
213 'description' => gettext("This is the template for each individual main menu item. A series of these is placed within the menubar shell template."),
214 'glossary' => array(
215 '{{location}}' => gettext("The URL of the menu item"),
216 '{{name}}' => gettext("The menu item's name")
220 $template['menuitem'] = <<< END
222 <li><a href="{{location}}">{{name}}</a></li>
224 END;
226 $template_definition[] = array(
227 'id' => 'selectedmenuitem',
228 'name' => gettext("Selected individual main menu item"),
229 'description' => gettext("This is the template for an individual main menu item if it is selected."),
230 'glossary' => array(
231 '{{location}}' => gettext("The URL of the menu item"),
232 '{{name}}' => gettext("The menu item's name")
236 $template['selectedmenuitem'] = <<< END
238 <li><a class="current" href="{{location}}">{{name}}</a></li>
240 END;
242 $template_definition[] = array(
243 'id' => 'submenu',
244 'name' => gettext("Sub-menubar shell"),
245 'description' => gettext("A list of sub-menu items will be placed within the menubar shell."),
246 'glossary' => array(
247 '{{submenuitems}}' => gettext("The menu items")
251 $template['submenu'] = <<< END
253 <h3>
254 {{submenuitems}}
255 </h3>
256 END;
258 $template_definition[] = array(
259 'id' => 'submenuitem',
260 'name' => gettext("Individual sub-menu item"),
261 'description' => gettext("This is the template for each individual sub-menu item. A series of these is placed within the sub-menubar shell template."),
262 'glossary' => array(
263 '{{location}}' => gettext("The URL of the menu item"),
264 '{{menu}}' => gettext("The menu item's name")
268 $template['submenuitem'] = <<< END
270 <a href="{{location}}">{{name}}</a>&nbsp;|
272 END;
274 $template_definition[] = array(
275 'id' => 'topmenu',
276 'name' => gettext("Status menubar shell"),
277 'description' => gettext("A list of statusbar menu items will be placed within the status menubar shell."),
278 'glossary' => array(
279 '{{topmenuitems}}' => gettext("The menu items")
283 $template['topmenu'] = <<< END
285 <div id="StatusRight">
286 {{topmenuitems}}
287 </div>
289 END;
291 $template_definition[] = array(
292 'id' => 'topmenuitem',
293 'name' => gettext("Individual statusbar menu item"),
294 'description' => gettext("This is the template for each individual statusbar menu item. A series of these is placed within the status menubar shell template."),
295 'glossary' => array(
296 '{{location}}' => gettext("The URL of the menu item"),
297 '{{menu}}' => gettext("The menu item's name")
301 $template['topmenuitem'] = <<< END
303 [<a href="{{location}}">{{name}}</a>]&nbsp;
305 END;
307 $template_definition[] = array(
308 'id' => 'databox',
309 'name' => gettext("Data input box (two columns)"),
310 'description' => gettext("This is mostly used whenever some input is taken from the user. For example, each of the fields in the profile edit screen is a data input box."),
311 'glossary' => array(
312 '{{name}}' => gettext("The name for the data we're inputting"),
313 '{{column1}}' => gettext("The first item of data"),
314 '{{column2}}' => gettext("The second item of data")
318 $template['databox'] = <<< END
320 <div class="infobox">
321 <table width="95%" class="profiletable" align="center" style="margin-bottom: 3px">
322 <tr>
324 <td width="20%" class="fieldname" valign="top">
325 <p><b>{{name}}</b></p>
326 </td>
327 <td width="50%" valign="top">
328 <p>{{column1}}</p>
329 </td>
330 <td width="30%" valign="top">
331 <p>{{column2}}</p>
332 </td>
333 </tr>
334 </table>
335 </div>
337 END;
339 $template_definition[] = array(
340 'id' => 'databox1',
341 'name' => gettext("Data input box (one column)"),
342 'description' => gettext("A single-column version of the data box."),
343 'glossary' => array(
344 '{{name}}' => gettext("The name of the data we're inputting"),
345 '{{column1}}' => gettext("The data itself")
349 $template['databox1'] = <<< END
351 <div class="infobox">
352 <table width="95%" class="profiletable" align="center" style="margin-bottom: 3px">
353 <tr>
355 <td width="20%" class="fieldname" valign="top">
356 <p><b>{{name}}</b></p>
357 </td>
358 <td width="80%" valign="top">
359 <p>{{column1}}</p>
360 </td>
361 </tr>
362 </table>
363 </div>
365 END;
367 $template_definition[] = array(
368 'id' => 'databoxvertical',
369 'name' => gettext("Data input box (vertical)"),
370 'description' => gettext("A slightly different version of the data box, used on this edit page amongst other places."),
371 'glossary' => array(
372 '{{name}}' => gettext("Name of the data we\'re inputting"),
373 '{{contents}}' => gettext("The data itself")
377 $template['databoxvertical'] = <<< END
378 <div class="infobox">
379 <table width="95%" class="fileTable" align="center" style="margin-bottom: 3px">
380 <tr>
381 <td class="fieldname">
382 <p><b>{{name}}</b></p>
383 </td>
384 </tr>
385 <tr>
386 <td>
387 <p>{{contents}}</p>
388 </td>
389 </tr>
390 </table>
391 </div>
393 END;
394 return $run_result;
397 function templates_main () {
399 global $PAGE;
401 $run_result = '';
404 * Templates unit
407 // Load default values
408 $function['init'][] = path . "units/templates/default_template.php";
410 // Actions
411 $function['templates:init'][] = path . "units/templates/template_actions.php";
413 // Draw template (returns HTML as opposed to echoing it straight to the screen)
414 $function['templates:draw'][] = path . "units/templates/template_draw.php";
416 // Function to substitute variables within a template, used in templates:draw
417 $function['templates:variables:substitute'][] = path . "units/templates/variables_substitute.php";
419 // Function to draw the page, once supplied with a main body and title
420 $function['templates:draw:page'][] = path . "units/templates/page_draw.php";
422 // Function to display a list of templates
423 $function['templates:view'][] = path . "units/templates/templates_view.php";
424 $function['templates:preview'][] = path . "units/templates/templates_preview.php";
426 // Function to display input fields for template editing
427 $function['templates:edit'][] = path . "units/templates/templates_edit.php";
429 // Function to allow the user to create a new template
430 $function['templates:add'][] = path . "units/templates/templates_add.php";
432 if ($context == "account") {
433 $PAGE->menu_sub[] = array( 'name' => 'template:edit',
434 'html' => templates_draw(array( 'context' => 'submenuitem',
435 'name' => gettext("Change theme"),
436 'location' => url . '_templates/')));
439 return $run_result;
442 function templates_page_setup (){
444 global $PAGE;
445 global $CFG;
447 if (!empty($PAGE->setupdone)) {
448 return false; // don't run twice
451 $PAGE->setupdone = true; // leave your mark
454 // Populate $PAGE with links for non-module core code
457 if (isadmin()) {
458 $PAGE->menu_top [] = array( 'name' => 'admin',
459 //'html' => a_hrefg("{$CFG->wwwroot}_admin/",
460 // "Administration"));
461 'html' => "<li><a href=\"" . $CFG->wwwroot . "_admin/\">" . gettext("Administration") . "</a></li>");
464 $PAGE->menu_top[] = array(
465 'name' => 'userdetails',
466 //'html' => a_hrefg("{$CFG->wwwroot}_userdetails/",
467 // "Account settings"));
468 'html' => "<li><a href=\"" . $CFG->wwwroot . "_userdetails/\">" . gettext("Account settings") . "</a></li>");
470 $PAGE->menu_top[] = array(
471 'name' => 'logoff',
472 //'html' => a_hrefg("{$CFG->wwwroot}login/logout.php",
473 // "Log off"));
474 'html' => "<li><a href=\"" . $CFG->wwwroot . "login/logout.php\">" . gettext("Log off") . "</a></li>");
476 if (context == "account") {
477 $PAGE->menu_sub[] = array(
478 'name' => 'user:edit',
479 'html' => a_hrefg("{$CFG->wwwroot}_userdetails/",
480 "Edit user details"));
481 $PAGE->menu_sub[] = array(
482 'name' => 'user:icon',
483 'html' => a_hrefg("{$CFG->wwwroot}_icons/",
484 "Your site picture"));
487 if (context == "admin" && logged_on && run("users:flags:get",array("admin", $_SESSION['userid']))) {
488 $PAGE->menu_sub[] = array(
489 'name' => 'admin',
490 'html' => a_hrefg("{$CFG->wwwroot}_admin/",
491 "Main"));
493 $PAGE->menu_sub[] = array(
494 'name' => 'admin:useradd',
495 'html' => a_hrefg("{$CFG->wwwroot}_admin/users_add.php",
496 "Add users"));
498 $PAGE->menu_sub[] = array(
499 'name' => 'admin:users',
500 'html' => a_hrefg("{$CFG->wwwroot}_admin/users.php",
501 "Manage users"));
503 $PAGE->menu_sub[] = array(
504 'name' => 'admin:flaggedcontent',
505 'html' => a_hrefg("{$CFG->wwwroot}_admin/flags.php",
506 "Manage flagged content"));
508 $PAGE->menu_sub[] = array(
509 'name' => 'admin:spam',
510 'html' => a_hrefg("{$CFG->wwwroot}_admin/antispam.php",
511 "Spam control"));
517 // Give a chance to all registered modules
519 if ($allmods = get_list_of_plugins('mod') ) {
520 foreach ($allmods as $mod) {
521 $mod_pagesetup = $mod . '_pagesetup';
522 if (function_exists($mod_pagesetup)) {
523 $mod_pagesetup();
524 } else {
525 notify("Function $mod_pagesetup doesn't exist!");
531 function templates_page_draw ($param) {
532 // Draws the page, given a title and a main body (parameters[0] and [1]).
533 $title = $param[0];
534 $mainbody = $param[1];
536 $run_result = '';
538 global $messages;
540 ////
541 //// Prepare things for the module run
542 //// populating $PAGE as required
543 ////
544 if (empty($PAGE->setupdone)) {
545 templates_page_setup();
548 $messageshell = "";
549 if (isset($messages) && sizeof($messages) > 0) {
550 foreach($messages as $message) {
551 $messageshell .=templates_draw(array(
552 'context' => 'messages',
553 'message' => $message
557 $messageshell =templates_draw(array(
558 'context' => 'messageshell',
559 'messages' => $messageshell
564 // If $parameter[2] is set, we'll substitute it for the
565 // sidebar
566 if (isset($param[2])) {
567 $sidebarhtml = $param[2];
568 } else {
569 $sidebarhtml = run("display:sidebar");
572 $run_result .= templates_draw(array(
573 'context' => 'pageshell',
574 'title' => $title,
575 'menu' => displaymenu(),
576 'submenu' => displaymenu_sub(),
577 'top' => displaymenu_top(),
578 'sidebar' => $sidebarhtml,
579 'mainbody' => $mainbody,
580 'messageshell' => $messageshell
583 return $run_result;
586 function templates_actions () {
588 global $CFG,$USER,$db;
590 // Actions
592 global $template;
593 global $messages;
595 $action = optional_param('action');
596 if (!logged_on) {
597 return false;
600 $run_result = '';
602 switch ($action) {
603 case "templates:select":
604 $id = optional_param('selected_template',0,PARAM_INT);
605 if ($id == -1) {
606 $exists = 1;
607 } else {
608 $exists = record_exists_sql('SELECT ident FROM '.$CFG->prefix.'templates WHERE ident = '.$id.' AND (owner = '.$USER->ident ." OR public='yes')");
610 if ($exists) {
611 $t = new StdClass;
612 $t->template_id = $id;
613 $t->ident = $USER->ident;
614 update_record('users',$t);
616 $messages[] = gettext("Your current template has been changed.");
618 break;
620 case "templates:save":
621 $template = optional_param('template');
622 $id = optional_param('save_template_id',0,PARAM_INT);
623 $templatetitle = optional_param('templatetitle');
624 if (!empty($template) && !empty($id) && !empty($templatetitle)) {
625 unset($_SESSION['template_element_cache'][$id]);
626 $exists = record_exists('templates','ident',$id,'owner',$USER->ident);
627 if ($exists) {
628 $t = new StdClass;
629 $t->name = $templatetitle;
630 $t->ident = $id;
631 update_record('templates',$t);
632 delete_records('template_elements','template_id',$id);
633 $contents = optional_param('template');
634 foreach ($contents as $name => $content) {
635 //TODO Fix this with PARAM_CLEANHTML or similar
636 $slashname = addslashes($name);
637 $slashcontent = addslashes($content);
638 if ($content != "" && $content != $template[$name]) {
639 $te = new StdClass;
640 $te->name = $slashname;
641 $te->content = $slashcontent;
642 $te->template_id = $id;
643 insert_record('template_elements',$te);
646 $messages[] = gettext("Your template has been updated.");
649 break;
650 case "deletetemplate":
651 $id = optional_param('delete_template_id',0,PARAM_INT);
652 unset($_SESSION['template_element_cache'][$id]);
653 $exists = record_exists('templates','ident',$id,'owner',$USER->ident);
654 if ($exists) {
655 $db->execute('UPDATE '.$CFG->prefix.'users SET template_id = -1 WHERE template_id = '.$id);
656 delete_records('template_elements','template_id',$id);
657 delete_records('template','ident',$id);
658 $messages[] = gettext("Your template was deleted.");
660 break;
661 case "templates:create":
662 $based_on = optional_param('template_based_on',0,PARAM_INT);
663 $name = optional_param('new_template_name');
664 if (!empty($name)) { // $based_on can be empty, surely? -Penny
665 $t = new StdClass;
666 $t->name = $name;
667 $t->public = 'no';
668 $t->owner = $USER->ident;
669 $new_template_id = insert_record('templates',$t);
670 if ($based_on != -1) {
671 $exists = record_exists_sql('SELECT ident FROM '.$CFG->prefix.'templates WHERE ident = ? AND (owner = ? OR public = ?)',array($based_on,$USER->ident,'yes'));
672 if ($exists) {
673 if ($elements = get_records('template_elements','template_id',$based_on)) {
674 foreach($elements as $element) {
675 $element->template_id = $new_template_id;
676 insert_record('template_elements',$element);
682 break;
684 return $run_result;
687 /// NOTE: this function takes a named array as single parameter
688 function templates_draw ($parameter) {
690 // Draw a page element using a specified template (or, if the template is -1, the default)
691 // $parameter['template'] = the template ID, $parameter['element'] = the template element,
692 // all other $parameter[n] = template elements
694 // Initialise global template variable, which contains the default template
695 global $template;
697 // Initialise global template ID variable, which contains the template ID we're using
698 global $template_id;
699 global $page_owner;
701 global $page_template_cache;
703 $run_result = '';
705 if ($parameter['context'] === 'topmenuitem') {
706 // error_log("templates_draw pcontext " . print_r($parameter,1));
708 // Get template details
709 if (!isset($template_id)) {
710 if (!isset($page_owner) || $page_owner == -1) {
711 $template_id = -1;
712 } else {
713 // if (!isset($_SESSION['template_id_cache'][$page_owner])) {
714 if (!$template_id = get_field('users','template_id','ident',$page_owner)) {
715 $template_id = -1;
717 // }
718 // $template_id = $_SESSION['template_id_cache'][$page_owner];
722 // Template ID override
723 $t = optional_param('template_preview',0,PARAM_INT);
724 if (!empty($t)) {
725 $template_id = $t;
728 // Grab the template content
729 if ($template_id == -1 || ($parameter['context'] != "css" && $parameter['context'] != "pageshell")) {
730 $template_element = $template[$parameter['context']];
731 } else {
732 $template_context = addslashes($parameter['context']);
733 if (!isset($page_template_cache[$parameter['context']])) {
734 $result = get_template_element($template_id, $template_context);
735 $page_template_cache[$parameter['context']] = $result;
736 } else {
737 $result = $page_template_cache[$parameter['context']];
739 if (!empty($result)) {
740 $template_element = stripslashes($result->content);
741 } else {
742 $template_element = $template[$parameter['context']];
746 if ($parameter['context'] === 'topmenuitem') {
747 // error_log("templates_draw pcontext " . print_r($template_element));
750 // Substitute elements
752 $functionbody = "
753 \$passed = array(".var_export($parameter,true).",\$matches[1], " . $template_id . ");
754 return templates_variables_substitute(\$passed);
757 // $template_element = templates_variables_substitute(array($parameter,$template_element));
758 $body = preg_replace_callback("/\{\{([A-Za-z_0-9]*)\}\}/i",create_function('$matches',$functionbody),$template_element);
760 $run_result = $body;
761 return $run_result;
764 function templates_add () {
765 global $USER;
767 // Create a new template
768 $header = gettext("Create theme"); // gettext variable
769 $desc = gettext("Here you can create your own themes based on one of the existing public themes. Just select which public theme you would like to alter and then create your own. You will now have edit privilages."); // gettext variable
771 $panel = <<< END
773 <h2>$header</h2>
774 <p>$desc</p>
775 <form action="index.php" method="post">
777 END;
779 $panel .= <<< END
781 END;
783 $panel .=templates_draw(array(
784 'context' => 'databox1',
785 'name' => gettext("Theme name"),
786 'column1' => display_input_field(array("new_template_name","","text"))
790 $default = gettext("Default Theme"); // gettext variable
791 $column1 = <<< END
793 <select name="template_based_on">
794 <option value="-1">$default</option>
795 END;
797 if ($templates = get_records_select('templates','owner = '.$USER->ident." OR public = 'yes'",'public')) {
798 foreach($templates as $template) {
799 $column1 .= "<option value=\"".$template->ident."\">".stripslashes($template->name) . "</option>";
803 $column1 .= <<< END
804 </select>
805 END;
807 $panel .=templates_draw(array(
808 'context' => 'databox1',
809 'name' => gettext("Based on"),
810 'column1' => $column1
814 $buttonValue = gettext("Create Theme"); // gettext variable
815 $panel .= <<< END
818 <input type="hidden" name="action" value="templates:create" />
819 <input type="submit" value="$buttonValue" />
820 </p>
822 </form>
824 END;
826 $run_result .= $panel;
827 return $run_result;
830 function templates_edit () {
833 global $template;
834 global $template_definition;
835 global $USER;
837 if (!isset($parameter)) {
838 // Get template details
839 if (!$template_id = get_field('users','template_id','ident',$USER->ident)) {
840 $template_id = -1;
842 } else {
843 if (!is_array($parameter)) {
844 $template_id = (int) $parameter;
845 } else {
846 $template_id = -1;
850 // Grab title, see if we can edit the template
851 $editable = 0;
852 if ($template_id == -1) {
853 $templatetitle = gettext("Default Theme");
854 } else {
855 $templatestuff = get_record('templates','ident',$template_id);
856 $templatetitle = stripslashes($templatestuff->name);
857 if ($templatestuff->owner == $USER->ident) {
858 $editable = 1;
860 if (($templatestuff->owner != $USER->ident) && ($templatestuff->public != 'yes')) {
861 $template_id = -1;
865 // Grab the template content
866 if ($template_id == -1) {
867 $current_template = $template;
868 } else {
869 if ($elements = get_records('template_elements','template_id',$template_id)) {
870 foreach($result as $element) {
871 $current_template[stripslashes($element->name)] = stripslashes($element->content);
873 } else {
874 $current_template = $template;
878 $run_result .= <<< END
880 <form action="" method="post">
882 END;
884 $run_result .= templates_draw(array(
885 'context' => 'databoxvertical',
886 'name' => gettext("Theme Name"),
887 'contents' => display_input_field(array("templatetitle",$templatetitle,"text"))
891 foreach($template_definition as $element) {
893 $name = "<b>" . $element['name'] . "</b><br /><i>" . $element['description'] . "</i>";
894 $glossary = gettext("Glossary"); // gettext variable
896 if (is_array($element['glossary']) && sizeof($element['glossary']) > 0) {
897 $column1 = "<b>$glossary</b><br />";
898 foreach($element['glossary'] as $gloss_id => $gloss_descr) {
899 $column1 .= $gloss_id . " -- " . $gloss_descr . "<br />";
901 } else {
902 $column1 = "";
905 if ($current_template[$element['id']] == "" || !isset($current_template[$element['id']])) {
906 $current_template[$element['id']] = $template[$element['id']];
909 $column2 = display_input_field(array("template[" . $element['id'] . "]",$current_template[$element['id']],"longtext"));
911 $run_result .=templates_draw(array(
912 'context' => 'databox',
913 'name' => $name,
914 'column2' => $column1,
915 'column1' => $column2
919 $run_result .=templates_draw(array(
920 'context' => 'databoxvertical',
921 'name' => $name,
922 'contents' => $column1 . "<br />" . $column2
929 if ($editable) {
930 $save = gettext("Save"); // gettext variable
931 $run_result .= <<< END
933 <p align="center">
934 <input type="hidden" name="action" value="templates:save" />
935 <input type="hidden" name="save_template_id" value="$template_id" />
936 <input type="submit" value="$save" />
937 </p>
939 END;
940 } else {
941 $noEdit = gettext("You may not edit this theme. To create a new, editable theme based on the default, go to <a href=\"index.php\">the main themes page</a>."); // gettext variable
942 $run_result .= <<< END
945 $noEdit
946 </p>
948 END;
950 $run_result .= <<< END
952 </form>
954 END;
955 return $run_result;
958 function templates_preview () {
960 global $CFG;
962 // Preview template
964 // Basic page elements
966 $name = "Basic page elements";
967 $heading1 = gettext("Heading one"); // gettext variable
968 $heading2 = gettext("Heading two"); // gettext variable
969 $bulletList = gettext("A bullet list"); // gettext variable
970 $heading3 = gettext("Heading three"); // gettext variable
971 $numberedList = gettext("A numbered list"); // gettext variable
972 $body = <<< END
974 <img src="{$CFG->wwwroot}_templates/leaves.jpg" width="300" height="225" alt="A test image" align="right" />
975 <h1>$heading1</h1>
976 <p>Paragraph text</p>
977 <h2>$heading2</h2>
978 <ul>
979 <li>$bulletList</li>
980 </ul>
981 <h3>$heading3</h3>
982 <ol>
983 <li>$numberedList</li>
984 </ol>
986 END;
988 $run_result .=templates_draw(array(
989 'context' => 'contentholder',
990 'title' => $name,
991 'body' => $body
995 // Form elements
997 $name = "Data input";
999 $body =templates_draw(array(
1000 'context' => 'databox',
1001 'name' => gettext("Some text input"),
1002 'column1' => display_input_field(array("blank","","text")),
1003 'column2' => run("display:access_level_select",array("blank","PUBLIC"))
1006 $body .=templates_draw(array(
1007 'context' => 'databox1',
1008 'name' => gettext("Some longer text input"),
1009 'column1' => display_input_field(array("blank","","longtext"))
1012 $body .=templates_draw(array(
1013 'context' => 'databoxvertical',
1014 'name' => gettext("Further text input"),
1015 'contents' => display_input_field(array("blank","","longtext")) . "<br />" . display_input_field(array("blank","","text")) . "<br /><input type='button' value='Button' />"
1019 $run_result .=templates_draw(array(
1020 'context' => 'contentholder',
1021 'title' => $name,
1022 'body' => $body,
1023 'submenu' => ''
1026 return $run_result;
1029 function templates_view () {
1030 global $USER;
1032 $user_template = get_field('users','template_id','ident',$USER->ident);
1033 $sitename = sitename;
1034 $title = gettext("Select / Create / Edit Themes"); // gettext variable
1035 $header = gettext("Public Themes"); // gettext variable
1036 $desc = sprintf(gettext("The following are public themes that you can use to change the way your %s looks - these do not change the content only the appearance. Check the preview and then select the one you want. If you wish you can adapt one of these using the 'create theme' option below."), $sitename); // gettext variable
1037 $panel = <<< END
1039 <h2>$title</h2>
1040 <form action="" method="post">
1041 <h3>
1042 $header
1043 </h3>
1045 $desc
1046 </p>
1048 END;
1050 $template_list[] = array(
1051 'name' => gettext("Default Theme"),
1052 'id' => -1
1054 if ($templates = get_records('templates','public','yes')) {
1055 foreach($templates as $template) {
1056 $template_list[] = array(
1057 'name' => stripslashes($template->name),
1058 'id' => stripslashes($template->ident)
1062 foreach($template_list as $template) {
1063 $name = "<input type='radio' name='selected_template' value='".$template['id']."' ";
1064 if ($template['id'] == $user_template) {
1065 $name .= "checked=\"checked\"";
1067 $name .=" /> ";
1068 $column1 = "<b>" . $template['name'] . "</b>";
1069 $column2 = "<a href=\"".url."_templates/preview.php?template_preview=".$template['id']."\" target=\"preview\">" . gettext("preview") . "</a>";
1070 $panel .=templates_draw(array(
1071 'context' => 'databox',
1072 'name' => $name,
1073 'column1' => $column1,
1074 'column2' => $column2
1079 $templates = get_records('templates','owner',$USER->ident);
1080 $header2 = gettext("Personal themes"); // gettext variable
1081 $desc2 = gettext("These are themes that you have created. You can edit and delete these. These theme(s) only control actual look and feel - you cannot change any content here. To change any of your content you need to use the other menu options such as: edit profile, update weblog etc."); // gettext variable
1083 if (is_array($templates) && sizeof($templates) > 0) {
1084 $panel .= <<< END
1085 <h3>
1086 $header2
1087 </h3>
1089 $desc2
1090 </p>
1092 END;
1094 foreach($templates as $template) {
1095 $name = "<input type='radio' name='selected_template' value='".$template->ident."'";
1096 if ($template->ident == $user_template) {
1097 $name .= "checked=\"checked\"";
1099 $name .=" /> ";
1100 $column1 = "<b>" . stripslashes($template->name) . "</b>";
1101 $column2 = "<a href=\"".url."_templates/preview.php?template_preview=".$template->ident."\" target=\"preview\">" . gettext("preview") . "</a>";
1103 $column2 .= " | <a href=\"".url."_templates/edit.php?id=".$template->ident."\" >". gettext("Edit") ."</a>";
1104 $column2 .= " | <a href=\"".url."_templates/?action=deletetemplate&amp;delete_template_id=".$template->ident."\" onclick=\"return confirm('" . gettext("Are you sure you want to permanently remove this template?") . "')\">" . gettext("Delete") . "</a>";
1105 $panel .=templates_draw(array(
1106 'context' => 'databox',
1107 'name' => $name,
1108 'column1' => $column1,
1109 'column2' => $column2
1115 $submitValue = gettext("Select new theme"); // gettext variable
1116 $panel .= <<< END
1119 <input type="submit" value=$submitValue />
1120 <input type="hidden" name="action" value="templates:select" />
1121 </p>
1123 </form>
1125 END;
1127 $run_result .= $panel;
1128 return $run_result;
1131 function templates_variables_substitute ($param) {
1133 global $CFG;
1135 $variables = $param[0];
1136 $template_variable = $param[1];
1138 $run_result = '';
1140 // Substitute variables in templates:
1141 // where {{variablename}} is found in the template, this function is passed
1142 // "variablename" and returns the proper variable
1144 global $menubar;
1145 global $submenubar;
1146 global $metatags;
1147 global $PAGE;
1148 global $template_id;
1150 //error_log("tvs " . print_r($template_variable,1));
1152 if (isset($variables[$template_variable])) {
1153 return $variables[$template_variable];
1154 } else {
1155 switch($template_variable) {
1157 case "username":
1158 if (logged_on) {
1159 return $_SESSION['username'];
1160 } else {
1161 return gettext("Guest");
1163 break;
1165 case "userfullname":
1166 if (logged_on) {
1167 return $_SESSION['name'];
1168 } else {
1169 return gettext("Guest")
1170 . " [<a href=\"".url."login/index.php\">" . gettext("Log in") . "</a>]";
1172 break;
1173 case "menu":
1174 if (logged_on) {
1175 return templates_draw(array(
1176 'menuitems' => menu_join('', $PAGE->menu),
1177 'context' => 'menu'
1180 break;
1182 case "submenu":
1183 return templates_draw(array(
1184 'submenuitems' => menu_join('&nbsp;|&nbsp;', $PAGE->menu_sub),
1185 'context' => 'submenu'
1187 break;
1189 case "topmenu":
1190 if (logged_on) {
1191 return templates_draw(array(
1192 'topmenuitems' => menu_join('', $PAGE->menu_top),
1193 'context' => 'topmenu'
1196 break;
1198 case "url":
1199 return url;
1200 break;
1202 case "userfullname":
1203 global $page_owner;
1204 if (!isset($page_owner) || $page_owner == -1) {
1205 return "";
1206 } else {
1207 return run("users:id_to_name", $page_owner);
1209 break;
1211 case "metatags":
1212 // $run_result = "<link href=\"/".$template_variable.".css\" rel=\"stylesheet\" type=\"text/css\" />";
1213 return "<style type=\"text/css\"><!--\n"
1214 . templates_draw(array(
1215 'template' => $template_id,
1216 'context' => 'css'
1219 . "// -->\n</style>\n"
1220 . $metatags;
1221 break;
1223 case 'perf':
1224 $perf = get_performance_info();
1225 if (defined('ELGG_PERFTOLOG')) {
1226 error_log("PERF: " . $perf['txt']);
1228 if (defined('ELGG_PERFTOFOOT') || $CFG->debug > 7 || $CFG->perfdebug > 7) {
1229 return $perf['html'];
1232 break;
1235 return $run_result;
1238 /***
1239 *** Fetches the template elements from disk or db.
1240 ***/
1241 function get_template_element ($template_id, $element_name) {
1242 global $CFG;
1244 if ($CFG->templatestore === 'db') {
1245 $result = get_record('template_elements','template_id',$template_id,'name',$element_name);
1246 } else {
1247 $template_name = get_field('templates', 'name', 'ident', $template_id);
1248 $template_name = strtolower(clean_param($template_name, PARAM_ALPHANUM));
1249 $template_file = $CFG->templatesroot . $template_name . '/' . $element_name;
1251 if (! $element_content = file_get_contents($template_file)) {
1252 trigger_error("Problems retrieving template element from $template_file");
1253 } else {
1254 $result = new StdClass;
1255 $result->content = $element_content;
1256 $result->ident = $template_id;
1257 $result->name = $element_name;
1260 return $result;
1263 /*** menu_join()
1264 *** performs a join on one of
1265 *** the $PAGE->menu* variables
1266 *** returns HTML
1267 ***/
1268 function menu_join ($separator, $menuarray) {
1269 $html = array();
1270 foreach ($menuarray as $entry) {
1271 array_push($html, $entry['html']);
1273 return join($separator, $html);