Fixing a small css issue in the user class.
[elgg.git] / lib / templates.php
blob64b834e68463aee1ef24fea280d0a231350ff1df
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 /***
14 *** Takes in the short name of a template and returns the comparable version
15 *** from the file system
16 *** @param string $shortname short name of template to convert to file version
17 ***/
18 function templates_shortname_to_file($shortname) {
19 $shortname = str_replace(" ","_",$shortname);
20 return $shortname;
23 /***
24 *** Takes in the directory name of a template and returns the comparable version
25 *** for use as a shortname to display
26 *** @param string $foldername folder name of template to convert to internal version
27 ***/
28 function templates_file_to_shortname($foldername) {
29 $foldername = str_replace("_"," ",$foldername);
30 return $foldername;
33 function default_template () {
35 global $CFG;
36 global $template;
37 global $template_definition;
38 $sitename = $CFG->sitename;
40 $run_result = '';
42 $template_definition[] = array(
43 'id' => 'css',
44 'name' => __gettext("Stylesheet"),
45 'description' => __gettext("The Cascading Style Sheet for the template."),
46 'glossary' => array(),
47 'display' => 1,
50 $template['css'] = file_get_contents($CFG->templatesroot . "Default_Template/css");
52 $template_definition[] = array(
53 'id' => 'pageshell',
54 'name' => __gettext("Page Shell"),
55 'description' => __gettext("The main page shell, including headers and footers."),
56 'glossary' => array(
57 '{{metatags}}' => __gettext("Page metatags (mandatory) - must be in the 'head' portion of the page"),
58 '{{title}}' => __gettext("Page title"),
59 '{{menu}}' => __gettext("Menu"),
60 '{{topmenu}}' => __gettext("Status menu"),
61 '{{mainbody}}' => __gettext("Main body"),
62 '{{sidebar}}' => __gettext("Sidebar")
66 $welcome = __gettext("Welcome"); // gettext variable
68 $template['pageshell'] = file_get_contents($CFG->templatesroot . "Default_Template/pageshell");
70 $template['frontpage_loggedout'] = file_get_contents($CFG->templatesroot . "Default_Template/frontpage_loggedout");
71 $template['frontpage_loggedin'] = file_get_contents($CFG->templatesroot . "Default_Template/frontpage_loggedin");
73 // REMOVED stylesheet (was old version and should not have been here)
74 // TODO: extract all Default_Template stuff from lib/templates.php
76 $template_definition[] = array(
77 'id' => 'contentholder',
78 'name' => __gettext("Content holder"),
79 'description' => __gettext("Contains the main content for a page (as opposed to the sidebar or the title)."),
80 'glossary' => array(
81 '{{title}}' => __gettext("The title"),
82 '{{submenu}}' => __gettext("The page submenu"),
83 '{{body}}' => __gettext("The body of the page")
85 );
87 $template['contentholder'] = <<< END
89 <div class="SectionContent">
91 <h1>{{title}}</h1>
92 {{submenu}}
93 </div>
94 {{body}}
96 END;
98 $template_definition[] = array(
99 'id' => 'ownerbox',
100 'name' => __gettext("Owner box"),
101 'description' => __gettext("A box containing a description of the owner of the current profile."),
102 'glossary' => array(
103 '{{name}}' => __gettext("The user's name"),
104 '{{profileurl}}' => __gettext("The URL of the user's profile page, including terminating slash"),
105 '{{usericon}}' => __gettext("The user's icon, if it exists"),
106 '{{tagline}}' => __gettext("A short blurb about the user"),
107 '{{usermenu}}' => __gettext("Links to friend / unfriend a user"),
108 '{{lmshosts}}' => __gettext("Links to any lms hosts the user is attached to"),
112 $tags = __gettext("Tags");
113 $resources = __gettext("Resources");
114 $template['ownerbox'] = <<< END
116 <div class="me">
117 <div style="float: left; width: 70px"><a href="{{profileurl}}">{{usericon}}</a></div>
118 <div style="margin-left: 75px; margin-top: 0px; padding-top: 0px; text-align: left" ><p>
119 <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>
120 <p>{{tagline}}</p>
121 <p>{{lmshosts}}</p>
122 <p style="margin-bottom: 3px" class="usermenu">{{usermenu}}</p>
123 </div>
124 </div>
126 END;
128 $template_definition[] = array(
129 'id' => 'infobox',
130 'name' => __gettext("Information Box"),
131 '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."),
132 'glossary' => array(
133 '{{name}}' => __gettext("The title"),
134 '{{contents}}' => __gettext("The contents of the box")
137 $template['infobox'] = <<< END
139 <table class="infobox" width="100%">
140 <caption align="top">
141 {{name}}
142 </caption>
143 <tr>
144 <td>
145 {{contents}}
146 </td>
147 </tr>
148 </table><br />
149 END;
151 $template_definition[] = array(
152 'id' => 'messageshell',
153 'name' => __gettext("System message shell"),
154 'description' => __gettext("A list of system messages will be placed within the message shell."),
155 'glossary' => array(
156 '{{messages}}' => __gettext("The messages")
160 $template['messageshell'] = <<< END
162 <div id="js">{{messages}}</div><br />
164 END;
166 $template_definition[] = array(
167 'id' => 'messages',
168 'name' => __gettext("Individual system messages"),
169 'description' => __gettext("Each individual system message."),
170 'glossary' => array(
171 '{{message}}' => __gettext("The system message")
175 $template['messages'] = <<< END
178 {{message}}
179 </p>
181 END;
184 $template_definition[] = array(
185 'id' => 'menu',
186 'name' => __gettext("Main menu shell"),
187 'description' => __gettext("A list of main menu items will be placed within the menubar shell."),
188 'glossary' => array(
189 '{{menuitems}}' => __gettext("The menu items")
193 $template['menu'] = <<< END
195 {{menuitems}}
196 END;
198 $template_definition[] = array(
199 'id' => 'menuitem',
200 'name' => __gettext("Individual main menu item"),
201 'description' => __gettext("This is the template for each individual main menu item. A series of these is placed within the menubar shell template."),
202 'glossary' => array(
203 '{{location}}' => __gettext("The URL of the menu item"),
204 '{{name}}' => __gettext("The menu item's name")
208 $template['menuitem'] = <<< END
210 <li><a href="{{location}}">{{name}}</a></li>
212 END;
214 $template_definition[] = array(
215 'id' => 'selectedmenuitem',
216 'name' => __gettext("Selected individual main menu item"),
217 'description' => __gettext("This is the template for an individual main menu item if it is selected."),
218 'glossary' => array(
219 '{{location}}' => __gettext("The URL of the menu item"),
220 '{{name}}' => __gettext("The menu item's name")
224 $template['selectedmenuitem'] = <<< END
226 <li><a class="current" href="{{location}}">{{name}}</a></li>
228 END;
230 $template_definition[] = array(
231 'id' => 'submenu',
232 'name' => __gettext("Sub-menubar shell"),
233 'description' => __gettext("A list of sub-menu items will be placed within the menubar shell."),
234 'glossary' => array(
235 '{{submenuitems}}' => __gettext("The menu items")
239 $template['submenu'] = <<< END
241 <h3>
242 {{submenuitems}}
243 </h3>
244 END;
246 $template_definition[] = array(
247 'id' => 'submenuitem',
248 'name' => __gettext("Individual sub-menu item"),
249 'description' => __gettext("This is the template for each individual sub-menu item. A series of these is placed within the sub-menubar shell template."),
250 'glossary' => array(
251 '{{location}}' => __gettext("The URL of the menu item"),
252 '{{menu}}' => __gettext("The menu item's name")
256 $template['submenuitem'] = <<< END
258 <a href="{{location}}">{{name}}</a>&nbsp;|
260 END;
262 $template_definition[] = array(
263 'id' => 'topmenu',
264 'name' => __gettext("Status menubar shell"),
265 'description' => __gettext("A list of statusbar menu items will be placed within the status menubar shell."),
266 'glossary' => array(
267 '{{topmenuitems}}' => __gettext("The menu items")
271 $template['topmenu'] = <<< END
273 <div id="StatusRight">
274 {{topmenuitems}}
275 </div>
277 END;
279 $template_definition[] = array(
280 'id' => 'topmenuitem',
281 'name' => __gettext("Individual statusbar menu item"),
282 'description' => __gettext("This is the template for each individual statusbar menu item. A series of these is placed within the status menubar shell template."),
283 'glossary' => array(
284 '{{location}}' => __gettext("The URL of the menu item"),
285 '{{menu}}' => __gettext("The menu item's name")
289 $template['topmenuitem'] = <<< END
291 [<a href="{{location}}">{{name}}</a>]&nbsp;
293 END;
295 $template_definition[] = array(
296 'id' => 'databox',
297 'name' => __gettext("Data input box (two columns)"),
298 '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."),
299 'glossary' => array(
300 '{{name}}' => __gettext("The name for the data we're inputting"),
301 '{{column1}}' => __gettext("The first item of data"),
302 '{{column2}}' => __gettext("The second item of data")
306 $template['databox'] = <<< END
308 <div class="infobox">
309 <table width="95%" class="profiletable" align="center" style="margin-bottom: 3px">
310 <tr>
312 <td width="20%" class="fieldname" valign="top">
313 <p><b>{{name}}</b></p>
314 </td>
315 <td width="50%" valign="top">
316 <p>{{column1}}</p>
317 </td>
318 <td width="30%" valign="top">
319 <p>{{column2}}</p>
320 </td>
321 </tr>
322 </table>
323 </div>
325 END;
327 $template_definition[] = array(
328 'id' => 'databox1',
329 'name' => __gettext("Data input box (one column)"),
330 'description' => __gettext("A single-column version of the data box."),
331 'glossary' => array(
332 '{{name}}' => __gettext("The name of the data we're inputting"),
333 '{{column1}}' => __gettext("The data itself")
337 $template['databox1'] = <<< END
339 <div class="infobox">
340 <table width="95%" class="profiletable" align="center" style="margin-bottom: 3px">
341 <tr>
343 <td width="20%" class="fieldname" valign="top">
344 <p><b>{{name}}</b></p>
345 </td>
346 <td width="80%" valign="top">
347 <p>{{column1}}</p>
348 </td>
349 </tr>
350 </table>
351 </div>
353 END;
355 $template_definition[] = array(
356 'id' => 'databoxvertical',
357 'name' => __gettext("Data input box (vertical)"),
358 'description' => __gettext("A slightly different version of the data box, used on this edit page amongst other places."),
359 'glossary' => array(
360 '{{name}}' => __gettext("Name of the data we\'re inputting"),
361 '{{contents}}' => __gettext("The data itself")
365 $template['databoxvertical'] = <<< END
366 <div class="infobox">
367 <table width="95%" class="fileTable" align="center" style="margin-bottom: 3px">
368 <tr>
369 <td class="fieldname">
370 <p><b>{{name}}</b></p>
371 </td>
372 </tr>
373 <tr>
374 <td>
375 <p>{{contents}}</p>
376 </td>
377 </tr>
378 </table>
379 </div>
381 END;
382 return $run_result;
385 function templates_main () {
387 global $PAGE;
389 $run_result = '';
392 * Templates unit
395 // Load default values
396 $function['init'][] = path . "units/templates/default_template.php";
398 // Actions
399 $function['templates:init'][] = path . "units/templates/template_actions.php";
401 // Draw template (returns HTML as opposed to echoing it straight to the screen)
402 $function['templates:draw'][] = path . "units/templates/template_draw.php";
404 // Function to substitute variables within a template, used in templates:draw
405 $function['templates:variables:substitute'][] = path . "units/templates/variables_substitute.php";
407 // Function to draw the page, once supplied with a main body and title
408 $function['templates:draw:page'][] = path . "units/templates/page_draw.php";
410 // Function to display a list of templates
411 $function['templates:view'][] = path . "units/templates/templates_view.php";
412 $function['templates:preview'][] = path . "units/templates/templates_preview.php";
414 // Function to display input fields for template editing
415 $function['templates:edit'][] = path . "units/templates/templates_edit.php";
417 // Function to allow the user to create a new template
418 $function['templates:add'][] = path . "units/templates/templates_add.php";
420 if ($context == "account") {
421 $PAGE->menu_sub[] = array( 'name' => 'template:edit',
422 'html' => templates_draw(array( 'context' => 'submenuitem',
423 'name' => __gettext("Change theme"),
424 'location' => url . '_templates/')));
427 return $run_result;
430 function templates_page_setup (){
432 global $PAGE;
433 global $CFG;
435 if (!empty($PAGE->setupdone)) {
436 return false; // don't run twice
439 $PAGE->setupdone = true; // leave your mark
442 // Populate $PAGE with links for non-module core code
445 if (isadmin()) {
446 $PAGE->menu_top [] = array( 'name' => 'admin',
447 //'html' => a_href("{$CFG->wwwroot}_admin/",
448 // "Administration"));
449 'html' => "<li><a href=\"" . $CFG->wwwroot . "_admin/\">" . __gettext("Administration") . "</a></li>");
452 if (logged_on) {
453 $PAGE->menu_top[] = array(
454 'name' => 'userdetails',
455 //'html' => a_href("{$CFG->wwwroot}_userdetails/",
456 // "Account settings"));
457 'html' => "<li><a href=\"" . $CFG->wwwroot . "_userdetails/\">" . __gettext("Account settings") . "</a></li>");
459 $PAGE->menu_top[] = array(
460 'name' => 'logoff',
461 //'html' => a_href("{$CFG->wwwroot}login/logout.php",
462 // "Log off"));
463 'html' => "<li><a href=\"" . $CFG->wwwroot . "login/logout.php\">" . __gettext("Log off") . "</a></li>");
466 if (defined("context") && context == "account") {
467 $PAGE->menu_sub[] = array(
468 'name' => 'user:edit',
469 'html' => a_href("{$CFG->wwwroot}_userdetails/",
470 __gettext("Edit user details")));
471 $PAGE->menu_sub[] = array(
472 'name' => 'user:icon',
473 'html' => a_href("{$CFG->wwwroot}_icons/",
474 __gettext("Your site picture")));
477 if (defined("context") && context == "admin" && logged_on && user_flag_get("admin", $_SESSION['userid'])) {
478 $PAGE->menu_sub[] = array(
479 'name' => 'admin',
480 'html' => a_href("{$CFG->wwwroot}_admin/",
481 __gettext("Main")));
483 $PAGE->menu_sub[] = array(
484 'name' => 'admin:useradd',
485 'html' => a_href("{$CFG->wwwroot}_admin/users_add.php",
486 __gettext("Add users")));
488 $PAGE->menu_sub[] = array(
489 'name' => 'admin:users',
490 'html' => a_href("{$CFG->wwwroot}_admin/users.php",
491 __gettext("Manage users")));
493 $PAGE->menu_sub[] = array(
494 'name' => 'admin:flaggedcontent',
495 'html' => a_href("{$CFG->wwwroot}_admin/flags.php",
496 __gettext("Manage flagged content")));
498 $PAGE->menu_sub[] = array(
499 'name' => 'admin:spam',
500 'html' => a_href("{$CFG->wwwroot}_admin/antispam.php",
501 __gettext("Spam control")));
507 // Give a chance to all registered modules
509 if ($allmods = get_list_of_plugins('mod') ) {
510 foreach ($allmods as $mod) {
511 $mod_pagesetup = $mod . '_pagesetup';
512 if (function_exists($mod_pagesetup)) {
513 $mod_pagesetup();
514 } else {
515 notify("Function $mod_pagesetup doesn't exist!");
521 function templates_page_draw ($param) {
522 // Draws the page, given a title and a main body (parameters[0] and [1]).
523 $title = $param[0];
524 $mainbody = $param[1];
526 $run_result = '';
528 global $messages;
530 ////
531 //// Prepare things for the module run
532 //// populating $PAGE as required
533 ////
534 if (empty($PAGE->setupdone)) {
535 templates_page_setup();
538 $messageshell = "";
539 if (isset($messages) && sizeof($messages) > 0) {
540 foreach($messages as $message) {
541 $messageshell .=templates_draw(array(
542 'context' => 'messages',
543 'message' => $message
547 $messageshell =templates_draw(array(
548 'context' => 'messageshell',
549 'messages' => $messageshell
554 // If $parameter[2] is set, we'll substitute it for the
555 // sidebar
556 if (isset($param[2])) {
557 $sidebarhtml = $param[2];
558 } else {
559 $sidebarhtml = run("display:sidebar");
562 $run_result .= templates_draw(array(
563 'context' => 'pageshell',
564 'title' => htmlspecialchars($title, ENT_COMPAT, 'utf-8'),
565 'menu' => displaymenu(),
566 'submenu' => displaymenu_sub(),
567 'top' => displaymenu_top(),
568 'sidebar' => $sidebarhtml,
569 'mainbody' => $mainbody,
570 'messageshell' => $messageshell
573 return $run_result;
576 function templates_actions() {
578 global $CFG,$USER,$db;
580 // Actions
582 global $template, $messages, $CFG;
584 $action = optional_param('action');
585 if (!logged_on) {
586 return false;
589 $run_result = '';
591 switch ($action) {
592 case "templates:select":
593 $id = optional_param('selected_template');
594 if (substr($id, 0, 2) == "db") {
595 $template_id = (int) substr($id,2);
596 $exists = record_exists_sql('SELECT ident, shortname FROM '.$CFG->prefix.'templates WHERE ident = '.$template_id.' AND (owner = '.$USER->ident ." OR public='yes')");
597 } else {
598 $exists = file_exists($CFG->templatesroot . templates_shortname_to_file($id));
600 if ($exists) {
601 $affected_areas = optional_param('affected_areas',0,PARAM_INT);
602 if(is_array($affected_areas)) {
603 foreach($affected_areas as $index => $value) {
604 //TODO - check security
605 set_field('users','template_name',$id,'ident',$value);
607 $messages[] = __gettext("The templates have been changed according to your choices.");
608 } else {
609 $messages[] = __gettext("No changes made as no area of change was selected!");
612 break;
615 case "templates:save":
616 $templatearray = optional_param('template','','');
617 $id = optional_param('save_template_id',0,PARAM_INT);
618 $templatetitle = trim(optional_param('templatetitle'));
619 if (!empty($templatearray) && !empty($id) && !empty($templatetitle)) {
620 unset($_SESSION['template_element_cache'][$id]);
621 $exists = record_exists('templates','ident',$id,'owner',$USER->ident);
622 if ($exists) {
623 set_field('templates','name',$templatetitle,'ident',$id);
624 delete_records('template_elements','template_id',$id);
625 foreach($templatearray as $name => $content) {
626 //TODO Fix this with PARAM_CLEANHTML or similar
627 $cleanname = trim($name);
628 $cleancontent = trim($content);
629 if ($content != "" && $content != $template[$name]) {
630 $te = new StdClass;
631 $te->name = $cleanname;
632 $te->content = $cleancontent;
633 $te->template_id = $id;
634 insert_record('template_elements',$te);
637 $messages[] = __gettext("Your template has been updated.");
640 break;
643 case "deletetemplate":
644 $id = optional_param('delete_template_id',0,PARAM_INT);
645 unset($_SESSION['template_element_cache'][$id]);
646 $exists = record_exists('templates','ident',$id,'owner',$USER->ident);
647 if ($exists) {
648 //$db->execute('UPDATE '.$CFG->prefix.'users SET template_id = -1 WHERE template_id = '.$id);
649 set_field('users', 'template_id', -1, 'template_id', $id);
650 delete_records('template_elements','template_id',$id);
651 delete_records('templates','ident',$id);
652 $messages[] = __gettext("Your template was deleted.");
654 break;
657 case "templates:create":
658 $based_on = optional_param('template_based_on');
659 $name = trim(optional_param('new_template_name'));
660 if (empty($CFG->disable_usertemplates) && !empty($name)) {
661 $t = new StdClass;
662 $t->name = $name;
663 $t->public = 'no';
664 $t->owner = $USER->ident;
665 $new_template_id = insert_record('templates',$t);
666 $t->shortname = 'db'.$new_template_id;
667 $t->ident = $new_template_id;
668 update_record('templates',$t);
669 foreach(array('pageshell','css') as $template_element) {
670 if ($result = get_template_element($based_on, $template_element)) {
671 $element = new stdClass;
672 $element->template_id = $new_template_id;
673 $element->content = $result->content;
674 $element->name = $template_element;
675 insert_record('template_elements',$element);
679 break;
681 return $run_result;
684 /// NOTE: this function takes a named array as single parameter
685 function templates_draw ($parameter) {
687 // Draw a page element using a specified template (or, if the template is -1, the default)
688 // $parameter['template'] = the template ID, $parameter['element'] = the template element,
689 // all other $parameter[n] = template elements
691 // Initialise global template variable, which contains the Default_Template
692 global $template;
694 // Initialise global template ID variable, which contains the template ID we're using
695 global $template_name;
696 global $page_owner;
697 global $CFG;
699 global $page_template_cache;
701 $run_result = '';
703 if ($parameter['context'] === 'topmenuitem') {
704 // error_log("templates_draw pcontext " . print_r($parameter,1));
706 // Get template details
707 if (!isset($template_name)) {
708 if (!isset($page_owner) || $page_owner == -1) {
709 $template_name = "Default_Template";
710 } else {
711 if (!$template_name = user_info('template_name',$page_owner)) {
712 $template_name = "Default_Template";
717 // Template ID override
718 $t = optional_param('template_preview');
719 if (!empty($t)) {
720 $template_name = $t;
723 // Grab the template content
724 if ($template_name == "Default_Template" || ($parameter['context'] != "css" && $parameter['context'] != "pageshell")) {
725 $template_element = $template[$parameter['context']];
726 } else {
727 if (!isset($page_template_cache[$parameter['context']])) {
728 if ($result = get_template_element($template_name, $parameter['context'])) {
729 $page_template_cache[$parameter['context']] = $result;
730 } else {
731 $page_template_cache[$parameter['context']] = $template[$parameter['context']];
733 } else {
734 $result = $page_template_cache[$parameter['context']];
736 if (!empty($result)) {
737 $template_element = $result->content;
738 } else {
739 $template_element = $template[$parameter['context']];
743 if (!empty($CFG->templates->variables_substitute) && !empty($CFG->templates->variables_substitute[$parameter['context']])) {
744 if (is_array($CFG->templates->variables_substitute[$parameter['context']])) {
745 foreach ($CFG->templates->variables_substitute[$parameter['context']] as $sub_function) {
746 $template_element .= $sub_function($vars);
748 } elseif (is_callable($CFG->templates->variables_substitute[$parameter['context']])) {
749 $template_element .= $CFG->templates->variables_substitute[$parameter['context']]($vars);
753 if ($parameter['context'] === 'topmenuitem') {
754 // error_log("templates_draw pcontext " . print_r($template_element));
757 // Substitute elements
759 $functionbody = "
760 \$passed = array(".var_export($parameter,true).",\$matches[1], '" . $template_name . "');
761 return templates_variables_substitute(\$passed);
764 // $template_element = templates_variables_substitute(array($parameter,$template_element));
765 $body = preg_replace_callback("/\{\{([A-Za-z_0-9: ]*)\}\}/i",create_function('$matches',$functionbody),$template_element);
767 $run_result = $body;
768 return $run_result;
771 /***
772 *** Draws a form to create a new template based on one of the existing choices.
773 ***/
774 function templates_add () {
775 global $USER;
778 // Create a new template
779 $header = __gettext("Create theme"); // gettext variable
780 $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
782 $panel = <<< END
784 <h2>$header</h2>
785 <p>$desc</p>
786 <form action="index.php" method="post">
788 END;
790 $panel .= <<< END
792 END;
794 $panel .=templates_draw(array(
795 'context' => 'databox1',
796 'name' => __gettext("Theme name"),
797 'column1' => display_input_field(array("new_template_name","","text"))
801 $default = __gettext("Default Template"); // gettext variable
802 $templates_list = templates_list();
803 $column1 = "<select name=\"template_based_on\">";
804 foreach($templates_list as $template) {
805 $name = __gettext($template['name']);
806 $column1 .= "<option value=\"".$template['name']."\"";
807 if ($template['name'] == "Default Template") {
808 $column1 .= " selected=\"selected\"";
810 $column1 .= ">" . $name . "</option>";
813 $column1 .= <<< END
814 </select>
815 END;
817 $panel .=templates_draw(array(
818 'context' => 'databox1',
819 'name' => __gettext("Based on"),
820 'column1' => $column1
824 $buttonValue = __gettext("Create Theme"); // gettext variable
825 $panel .= <<< END
828 <input type="hidden" name="action" value="templates:create" />
829 <input type="submit" value="$buttonValue" />
830 </p>
832 </form>
834 END;
836 return $panel;
839 function templates_edit () {
842 global $template;
843 global $template_definition;
844 global $USER;
846 if (!isset($parameter)) {
847 // Get template details
848 if (!$template_name = user_info('template_name',$USER->ident)) {
849 $template_name = "Default_Template";
851 } else {
852 if (!is_array($parameter)) {
853 $template_name = trim($parameter);
854 } else {
855 $template_name = "Default_Template";
859 // Grab title, see if we can edit the template
860 $editable = 0;
861 if ($template_name == "Default_Template") {
862 $templatetitle = __gettext("Default Theme");
863 } else {
864 if ($templatestuff = get_record('templates','shortname',$template_name)) {
865 $templatetitle = $templatestuff->name;
866 if ($templatestuff->owner == $USER->ident) {
867 $editable = 1;
869 if (($templatestuff->owner != $USER->ident) && ($templatestuff->public != 'yes')) {
870 $template_name = 'Default_Template';
875 // Grab the template content
876 if ($template_name == "Default_Template") {
877 $current_template = $template;
878 } else {
880 if (substr($template_name, 0, 2) == "db") {
881 $template_id = (int) substr($template_name,2);
882 $result = get_record('template_elements','template_id',$template_id,'name',$element_name);
883 if ($elements = get_records('template_elements','template_id',$template_id)) {
884 foreach($result as $element) {
885 $current_template[$element->name] = $element->content;
887 } else {
888 $current_template = $template;
890 } else {
891 foreach(array('pageshell','css') as $element_name) {
892 $template_file = $CFG->templatesroot . templates_shortname_to_file($template_name) . '/' . $element_name;
893 if ($element_content = file_get_contents($template_file)) {
894 $current_template[$element_name] = $element_content;
900 $run_result .= <<< END
902 <form action="" method="post">
904 END;
906 $run_result .= templates_draw(array(
907 'context' => 'databoxvertical',
908 'name' => __gettext("Theme Name"),
909 'contents' => display_input_field(array("templatetitle",$templatetitle,"text"))
913 foreach($template_definition as $element) {
915 $name = "<b>" . $element['name'] . "</b><br /><i>" . $element['description'] . "</i>";
916 $glossary = __gettext("Glossary"); // gettext variable
918 if (is_array($element['glossary']) && sizeof($element['glossary']) > 0) {
919 $column1 = "<b>$glossary</b><br />";
920 foreach($element['glossary'] as $gloss_id => $gloss_descr) {
921 $column1 .= $gloss_id . " -- " . $gloss_descr . "<br />";
923 } else {
924 $column1 = "";
927 if ($current_template[$element['id']] == "" || !isset($current_template[$element['id']])) {
928 $current_template[$element['id']] = $template[$element['id']];
931 $column2 = display_input_field(array("template[" . $element['id'] . "]",$current_template[$element['id']],"longtext"));
933 $run_result .=templates_draw(array(
934 'context' => 'databox',
935 'name' => $name,
936 'column2' => $column1,
937 'column1' => $column2
941 $run_result .=templates_draw(array(
942 'context' => 'databoxvertical',
943 'name' => $name,
944 'contents' => $column1 . "<br />" . $column2
951 if ($editable) {
952 $save = __gettext("Save"); // gettext variable
953 $run_result .= <<< END
955 <p align="center">
956 <input type="hidden" name="action" value="templates:save" />
957 <input type="hidden" name="save_template_id" value="$template_id" />
958 <input type="submit" value="$save" />
959 </p>
961 END;
962 } else {
963 $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
964 $run_result .= <<< END
967 $noEdit
968 </p>
970 END;
972 $run_result .= <<< END
974 </form>
976 END;
977 return $run_result;
980 function templates_preview () {
982 global $CFG;
983 $run_result = '';
985 // Preview template
987 // Basic page elements
989 $name = "Basic page elements";
990 $heading1 = __gettext("Heading one"); // gettext variable
991 $heading2 = __gettext("Heading two"); // gettext variable
992 $bulletList = __gettext("A bullet list"); // gettext variable
993 $heading3 = __gettext("Heading three"); // gettext variable
994 $numberedList = __gettext("A numbered list"); // gettext variable
995 $body = <<< END
997 <img src="{$CFG->wwwroot}_templates/leaves.jpg" width="300" height="225" alt="A test image" align="right" />
998 <h1>$heading1</h1>
999 <p>Paragraph text</p>
1000 <h2>$heading2</h2>
1001 <ul>
1002 <li>$bulletList</li>
1003 </ul>
1004 <h3>$heading3</h3>
1005 <ol>
1006 <li>$numberedList</li>
1007 </ol>
1009 END;
1011 $run_result .= templates_draw(array(
1012 'context' => 'contentholder',
1013 'title' => $name,
1014 'body' => $body
1018 // Form elements
1020 $name = "Data input";
1022 $body =templates_draw(array(
1023 'context' => 'databox',
1024 'name' => __gettext("Some text input"),
1025 'column1' => display_input_field(array("blank","","text")),
1026 'column2' => run("display:access_level_select",array("blank","PUBLIC"))
1029 $body .=templates_draw(array(
1030 'context' => 'databox1',
1031 'name' => __gettext("Some longer text input"),
1032 'column1' => display_input_field(array("blank","","longtext"))
1035 $body .=templates_draw(array(
1036 'context' => 'databoxvertical',
1037 'name' => __gettext("Further text input"),
1038 'contents' => display_input_field(array("blank","","longtext")) . "<br />" . display_input_field(array("blank","","text")) . "<br /><input type='button' value='Button' />"
1042 $run_result .=templates_draw(array(
1043 'context' => 'contentholder',
1044 'title' => $name,
1045 'body' => $body,
1046 'submenu' => ''
1049 return $run_result;
1052 /***
1053 *** Returns a list of templates as an array.
1054 ***/
1055 function templates_list() {
1057 $template_list = array();
1058 if ($templates = get_list_of_plugins('mod/template/templates','theme_master')) {
1059 foreach($templates as $template) {
1060 $template_list[] = array(
1061 'name' => templates_file_to_shortname($template),
1062 'id' => NULL,
1063 'shortname' => $template
1067 if ($templates = get_records('templates','public','yes')) {
1068 foreach($templates as $template) {
1069 $template_list[] = array(
1070 'name' => $template->name,
1071 'id' => $template->ident,
1072 'shortname' => $template->shortname
1076 return $template_list;
1079 function templates_view () {
1080 global $USER, $CFG;
1081 $run_result = "";
1083 $user_template = user_info('template_name',$USER->ident);
1084 $sitename = sitename;
1085 $title = __gettext("Select / Create / Edit Themes"); // gettext variable
1086 $header = __gettext("Public Themes"); // gettext variable
1087 $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
1088 $panel = <<< END
1090 <h2>$title</h2>
1091 <form action="" method="post">
1092 <h3>
1093 $header
1094 </h3>
1096 $desc
1097 </p>
1099 END;
1101 $template_list[] = array(
1102 'name' => __gettext("Default Theme"),
1103 'shortname' => "Default_Template",
1104 'id' => -1
1107 $template_list = templates_list();
1108 foreach($template_list as $template) {
1109 if ($template['name'] == "Default Template") {
1110 $template['name'] = __gettext("Default Template");
1112 $name = "<input type='radio' name='selected_template' value='".$template['shortname']."' ";
1113 if ($template['shortname'] == $user_template) {
1114 $name .= "checked=\"checked\"";
1116 $name .=" /> ";
1117 $column1 = "<b>" . $template['name'] . "</b>";
1118 $column2 = "<a href=\"".url."_templates/preview.php?template_preview=".$template['shortname']."\" target=\"preview\">" . __gettext("Preview") . "</a>";
1119 $panel .=templates_draw(array(
1120 'context' => 'databox1',
1121 'name' => $name . $column1,
1122 'column1' => $column2,
1123 // 'column2' =>
1128 $templates = get_records('templates','owner',$USER->ident);
1129 $header2 = __gettext("Personal themes"); // gettext variable
1130 $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
1132 if (is_array($templates) && sizeof($templates) > 0) {
1133 $panel .= <<< END
1134 <h3>
1135 $header2
1136 </h3>
1138 $desc2
1139 </p>
1141 END;
1143 foreach($templates as $template) {
1144 $name = "<input type='radio' name='selected_template' value='".$template->ident."'";
1145 if ($template->shortname == $user_template) {
1146 $name .= " checked=\"checked\"";
1148 $name .=" /> ";
1149 $column1 = "<b>" . $template->name . "</b>";
1150 $column2 = "<a href=\"".url."_templates/preview.php?template_preview=".$template->shortname."\" target=\"preview\">" . __gettext("Preview") . "</a>";
1152 $column2 .= " | <a href=\"".url."_templates/edit.php?id=".$template->ident."\" >". __gettext("Edit") ."</a>";
1153 $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>";
1154 $panel .=templates_draw(array(
1155 'context' => 'databox1',
1156 'name' => $name . $column1,
1157 'column1' => $column2,
1158 // 'column2' => $column2
1164 $ownerCommunities = get_records('users','owner',$USER->ident);
1165 $header3 = __gettext("Change templates");
1166 $decs3 = __gettext("The selected changes will affect:");
1168 $panel .= <<< END
1169 <br />
1170 <h2>
1171 $header3
1172 </h2>
1174 $decs3
1175 </p>
1176 END;
1178 $name = "<input type='checkbox' name='affected_areas[]' value='".$USER->ident."' checked=\"checked\" />";
1179 $column1 = "<h4>User page</h4>";
1180 $column2 = "<h4>". __gettext("Your personal space") ."</h4>";
1181 $panel .= templates_draw(array(
1182 'context' => 'adminTable',
1183 'name' => $name,
1184 'column1' => $column1,
1185 'column2' => $column2
1189 if(!empty($ownerCommunities)) {
1190 foreach($ownerCommunities as $ownerCommunity) {
1191 $name = "<input type='checkbox' name='affected_areas[]' value='".$ownerCommunity->ident."' />";
1192 $column1 = "<h4>".stripslashes($ownerCommunity->name)."</h4>";
1193 $column2 = "<h4>".__gettext("Community: ") . stripslashes($ownerCommunity->name)."</h4>";
1194 $panel .= templates_draw(array(
1195 'context' => 'adminTable',
1196 'name' => $name,
1197 'column1' => $column1,
1198 'column2' => $column2
1205 $submitValue = __gettext("Select new theme"); // gettext variable
1206 $panel .= <<< END
1209 <input type="submit" value="$submitValue" />
1210 <input type="hidden" name="action" value="templates:select" />
1211 </p>
1213 </form>
1215 END;
1217 $run_result .= $panel;
1218 return $run_result;
1221 function templates_variables_substitute ($param) {
1223 global $CFG;
1225 $variables = $param[0];
1226 $template_variable = $param[1];
1228 $run_result = '';
1230 // Substitute variables in templates:
1231 // where {{variablename}} is found in the template, this function is passed
1232 // "variablename" and returns the proper variable
1234 global $menubar;
1235 global $submenubar;
1236 global $metatags;
1237 global $PAGE;
1238 global $template_id;
1239 global $template_name;
1240 global $db;
1242 //error_log("tvs " . print_r($template_variable,1));
1244 $result = "";
1245 if (isset($variables[$template_variable])) {
1246 $result .= $variables[$template_variable];
1247 } else {
1248 $vars = array();
1249 if (substr_count($template_variable,":") > 0) {
1250 $vars = explode(":",$template_variable);
1251 $template_variable = $vars[0];
1253 switch($template_variable) {
1255 case "username":
1256 if (logged_on) {
1257 $result = $_SESSION['username'];
1258 } else {
1259 $result = __gettext("Guest");
1261 break;
1262 case "usericonid":
1263 if (logged_on) {
1264 $result = user_info("icon",$_SESSION['userid']);
1265 } else {
1266 $result = 0;
1268 break;
1269 case "name":
1270 if (logged_on) {
1271 $result = htmlspecialchars($_SESSION['name'], ENT_COMPAT, 'utf-8');
1272 } else {
1273 $result = __gettext("Guest");
1275 break;
1276 case "userfullname":
1277 if (logged_on) {
1278 $result = __gettext("Welcome") . " " . htmlspecialchars($_SESSION['name'], ENT_COMPAT, 'utf-8');
1279 } else {
1280 $result = __gettext("Welcome") . " " . __gettext("Guest") . " [<a href=\"".url."login/index.php\">" . __gettext("Log in") . "</a>]";
1282 break;
1283 case "menu":
1284 if (logged_on) {
1285 $result = templates_draw(array(
1286 'menuitems' => menu_join('', $PAGE->menu),
1287 'context' => 'menu'
1290 break;
1292 case "submenu":
1293 $result = templates_draw(array(
1294 'submenuitems' => menu_join('&nbsp;|&nbsp;', $PAGE->menu_sub),
1295 'context' => 'submenu'
1297 break;
1299 case "topmenu":
1300 $result = templates_draw(array(
1301 'topmenuitems' => menu_join('', $PAGE->menu_top),
1302 'context' => 'topmenu'
1304 break;
1306 case "url":
1307 $result = url;
1308 break;
1310 case "sitename":
1311 $result = $CFG->sitename;
1312 break;
1314 case "tagline":
1315 $result = $CFG->tagline;
1316 break;
1318 case "metatags":
1319 // $run_result = "<link href=\"/".$template_variable.".css\" rel=\"stylesheet\" type=\"text/css\" />";
1320 $result = "<style type=\"text/css\">\n"
1321 . templates_draw(array(
1322 'template' => $template_name,
1323 'context' => 'css'
1326 . "\n</style>\n"
1327 . $metatags;
1328 break;
1330 case 'perf':
1331 $perf = get_performance_info();
1332 if (defined('ELGG_PERFTOLOG')) {
1333 error_log("PERF: " . $perf['txt']);
1335 if (defined('ELGG_PERFTOFOOT') || $CFG->debug > 7 || $CFG->perfdebug > 7) {
1336 $result = $perf['html'];
1339 break;
1341 case 'randomusers':
1342 $result = "";
1344 if (isset($vars[1])) {
1345 $vars[1] = (int) $vars[1];
1346 } else {
1347 $vars[1] = 3;
1350 if ($users = get_records_sql("SELECT DISTINCT u.*,i.filename AS iconfile, ".$db->random." as rand
1351 FROM ".$CFG->prefix."profile_data t JOIN ".$CFG->prefix."users u ON u.ident = t.owner
1352 LEFT JOIN ".$CFG->prefix."icons i ON i.ident = u.icon
1353 WHERE t.name IN (?,?,?) AND u.icon != ? AND t.access = ? AND u.user_type = ?
1354 ORDER BY rand LIMIT " . $vars[1],array('biography','minibio','interests',-1,'PUBLIC','person'))) {
1355 $usercount = 0;
1356 foreach($users as $user) {
1357 if ($usercount > 0) {
1358 $result .= ", ";
1359 } else {
1360 $result .= " ";
1362 $result .= "<a href=\"" . $CFG->wwwroot . $user->username . "/\">" . $user->name . "</a>";
1363 $usercount++;
1365 } else {
1366 $result .= __gettext("Sorry, no users have filled in their profiles yet.");
1369 break;
1370 case 'people':
1371 $result = "";
1372 if (isset($vars[1])) {
1373 $vars[1] = $db->qstr($vars[1]);
1374 } else {
1375 $vars[1] = "'interests'";
1378 if (isset($vars[2])) {
1379 $vars[2] = $db->qstr($vars[2]);
1380 } else {
1381 $vars[2] = "'foo'";
1384 if (isset($vars[3])) {
1385 $vars[3] = (int) $vars[3];
1386 } else {
1387 $vars[3] = 5;
1390 $users = get_records_sql("SELECT users.*, icons.filename as iconfile, icons.ident as iconid FROM ".$CFG->prefix."tags LEFT JOIN ".$CFG->prefix."users ON users.ident = tags.owner left join ".$CFG->prefix."icons on icons.ident = users.icon WHERE tags.tag = ".$vars[2]." AND tags.tagtype = ".$vars[1]." AND users.icon != -1 AND tags.access = 'PUBLIC' and users.user_type = 'person' ORDER BY rand( ) LIMIT " . $vars[3]);
1391 if (sizeof($users) > 0 && is_array($users)) {
1393 $result .= <<< END
1394 <table width="550px" border="0" cellpadding="0" cellspacing="0">
1395 <tr>
1396 END;
1398 foreach($users as $user) {
1399 $result .= <<< END
1401 <td align="center">
1402 <div class="image_holder">
1403 <a href="{$CFG->wwwroot}{$user->username}/"><img src="{$CFG->wwwroot}_icon/user/{$user->iconid}/w/80/h/80" border="0" /></a>
1404 </div>
1405 <div class="userdetails">
1406 <p><a href="{$CFG->wwwroot}{$user->username}/">{$user->name}</a></p>
1407 </div>
1408 END;
1411 $result .= <<< END
1412 <tr>
1413 </table>
1414 END;
1417 break;
1419 case "toptags":
1420 if (isset($vars[1])) {
1421 $vars[1] = $db->qstr($vars[1]);
1422 } else {
1423 $vars[1] = "'town'";
1425 $tags = get_records_sql("SELECT tag, count(ident) as numtags FROM `".$CFG->prefix."tags` WHERE access = 'public' and tagtype=".$vars[1]." group by tag order by numtags desc limit 20");
1426 $tag_count = 0;
1427 foreach($tags as $tag) {
1428 $result .= "<a href=\"".url."tag/".urlencode(htmlspecialchars(strtolower($tag->tag), ENT_COMPAT, 'utf-8'))."\" title=\"".htmlspecialchars($tag->tag, ENT_COMPAT, 'utf-8')." (" .$tag->numtags. ")\">";
1429 $result .= $tag->tag . "</a>";
1430 if ($tag_count < sizeof($tags) - 1) {
1431 $result .= ", ";
1433 $tag_count++;
1436 break;
1438 case "populartags":
1439 $result = "";
1440 $tags = get_records_sql("SELECT tag, count(ident) as numtags FROM `".$CFG->prefix."tags` WHERE access = 'public' and tag!='' group by tag having numtags > 1 order by ident desc limit 20");
1441 $max = 0;
1442 foreach($tags as $tag) {
1443 if ($tag->numtags > $max) {
1444 $max = $tag->numtags;
1448 $tag_count = 0;
1449 foreach($tags as $tag) {
1451 if ($max > 1) {
1452 $size = round((log($tag->numtags) / log($max)) * 300);
1453 } else {
1454 $size = 100;
1457 $result .= "<a href=\"".url."tag/".urlencode(htmlspecialchars(strtolower($tag->tag), ENT_COMPAT, 'utf-8'))."\" style=\"font-size: $size%\" title=\"".htmlspecialchars($tag->tag, ENT_COMPAT, 'utf-8')." (" .$tag->numtags. ")\">";
1458 $result .= $tag->tag . "</a>";
1459 if ($tag_count < sizeof($tags) - 1) {
1460 $result .= ", ";
1462 $tag_count++;
1465 break;
1466 default:
1467 break;
1471 if (!empty($CFG->templates->variables_substitute) && !empty($CFG->templates->variables_substitute[$template_variable])) {
1472 if (is_array($CFG->templates->variables_substitute[$template_variable])) {
1473 foreach ($CFG->templates->variables_substitute[$template_variable] as $sub_function) {
1474 $result .= $sub_function($vars);
1476 } elseif (is_callable($CFG->templates->variables_substitute[$template_variable])) {
1477 $result .= $CFG->templates->variables_substitute[$template_variable]($vars);
1480 $run_result .= $result;
1481 return $run_result;
1484 /***
1485 *** Fetches the template elements from disk or db.
1486 *** If $template_name starts with 'db', it will assume the rest of the name
1487 *** is a database ident. Otherwise, it will
1488 ***/
1489 function get_template_element ($template_name, $element_name) {
1490 global $CFG;
1491 static $template_cache;
1493 // $template_name = strtolower(clean_param($template_name, PARAM_ALPHANUM));
1494 $result = false;
1495 if ($CFG->templatestore === 'db') {
1496 if (substr($template_name, 0, 2) == "db") {
1497 $template_id = (int) substr($template_name,2);
1498 $result = get_record('template_elements','template_id',$template_id,'name',$element_name);
1499 } else {
1500 $template_file = $CFG->templatesroot . templates_shortname_to_file($template_name) . '/' . $element_name;
1501 if ($element_content = @file_get_contents($template_file)) {
1502 $result = new StdClass;
1503 $result->content = $element_content;
1504 $result->ident = NULL;
1505 $result->name = $element_name;
1506 $result->shortname = $template_name;
1509 } else {
1510 // $template_name = get_field('templates', 'name', 'ident', $template_id);
1511 // $template_name = strtolower(clean_param($template_name, PARAM_ALPHANUM));
1512 $template_file = $CFG->templatesroot . templates_shortname_to_file($template_name) . '/' . $element_name;
1514 if ($element_content = @file_get_contents($template_file)) {
1515 $result = new StdClass;
1516 $result->content = $element_content;
1517 $result->ident = $template_id;
1518 $result->name = $element_name;
1519 $result->shortname = $template_name;
1522 return $result;
1525 /*** menu_join()
1526 *** performs a join on one of
1527 *** the $PAGE->menu* variables
1528 *** returns HTML
1529 ***/
1530 function menu_join ($separator, $menuarray) {
1531 $html = array();
1532 foreach ($menuarray as $entry) {
1533 array_push($html, $entry['html']);
1535 return join($separator, $html);