| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Custom Page Module
|
| 7 |
**/
|
| 8 |
|
| 9 |
function custompage_init() {
|
| 10 |
|
| 11 |
//For Development Only. Do NOT un-comment in production
|
| 12 |
//drupal_flush_all_caches();
|
| 13 |
|
| 14 |
}
|
| 15 |
|
| 16 |
function custompage_perm() {
|
| 17 |
return array(
|
| 18 |
'administer custompage',
|
| 19 |
);
|
| 20 |
}
|
| 21 |
|
| 22 |
function custompage_menu() {
|
| 23 |
|
| 24 |
$items = array();
|
| 25 |
|
| 26 |
$items['admin/build/custompage/flushcache'] = array(
|
| 27 |
'title' => 'Flush Cache',
|
| 28 |
'page callback' => 'custompage_flushcache',
|
| 29 |
'access arguments' => array('administer custompage'),
|
| 30 |
'type' => MENU_CALLBACK
|
| 31 |
);
|
| 32 |
|
| 33 |
$styled_pathes = _custompage_get_mappings();
|
| 34 |
|
| 35 |
foreach ( $styled_pathes as $path ) {
|
| 36 |
|
| 37 |
if ( $path->enabled ) {
|
| 38 |
$access = array('access content');
|
| 39 |
} else {
|
| 40 |
$access = array('administer custompage');
|
| 41 |
}
|
| 42 |
|
| 43 |
$items[$path->path] = array(
|
| 44 |
'title' => t($path->title),
|
| 45 |
'page callback' => 'custompage_delegate',
|
| 46 |
'page arguments' => array($path->key),
|
| 47 |
'access arguments' => $access,
|
| 48 |
'type' => MENU_CALLBACK
|
| 49 |
);
|
| 50 |
|
| 51 |
}
|
| 52 |
|
| 53 |
return $items;
|
| 54 |
|
| 55 |
}
|
| 56 |
|
| 57 |
function custompage_flushcache() {
|
| 58 |
drupal_flush_all_caches();
|
| 59 |
drupal_set_message('Cache cleared.');
|
| 60 |
drupal_goto($_SERVER['HTTP_REFERER']);
|
| 61 |
}
|
| 62 |
|
| 63 |
function custompage_theme() {
|
| 64 |
|
| 65 |
$styled_pathes = _custompage_get_mappings();
|
| 66 |
$themes = array();
|
| 67 |
|
| 68 |
foreach ( $styled_pathes as $path ) {
|
| 69 |
$themes[$path->key] = array(
|
| 70 |
'arguments' => array('title' => '', 'user' => '', 'data' => ''),
|
| 71 |
'template' => $path->key,
|
| 72 |
'function' => $path->key,
|
| 73 |
);
|
| 74 |
}
|
| 75 |
|
| 76 |
return $themes;
|
| 77 |
}
|
| 78 |
|
| 79 |
|
| 80 |
/**
|
| 81 |
* Find a theme function and/or template file and return content
|
| 82 |
*/
|
| 83 |
function custompage_delegate( $arg ) {
|
| 84 |
global $user;
|
| 85 |
|
| 86 |
// Let's prepare some data for this baby.
|
| 87 |
$data = module_invoke_all( 'customdata_'.$arg );
|
| 88 |
|
| 89 |
$themed = theme( $arg, drupal_get_title(), $user, $data );
|
| 90 |
|
| 91 |
//Remove ANNOYING errors about not finding functions that are optional anyway
|
| 92 |
$messages = drupal_get_messages( 'error', TRUE);
|
| 93 |
if ( is_array( $messages ) & is_array( $messages['error'] ) ) {
|
| 94 |
foreach ( $messages['error'] as $msg ) {
|
| 95 |
$pattern = '/warning: call_user_func_array.+?First argument is expected to be a valid callback.+?/ims';
|
| 96 |
if ( preg_match( $pattern, $msg ) ) {
|
| 97 |
continue;
|
| 98 |
}
|
| 99 |
drupal_set_message( $msg, 'error');
|
| 100 |
}
|
| 101 |
}
|
| 102 |
|
| 103 |
if ( trim ($themed) == "" ) {
|
| 104 |
drupal_set_message ( "custompage could not find an appropriate theming function or template file for this path [$arg].
|
| 105 |
<br /><b>The viable options (in ascending priority) are:</b>
|
| 106 |
<ul>
|
| 107 |
<li>'phptemplate_$arg' in any module
|
| 108 |
<li>'THEMENAME_$arg' in a theme with the name THEMENAME
|
| 109 |
<li>$arg.tpl.php template file in a current theme
|
| 110 |
</ul>
|
| 111 |
Please make sure at least one of these exist and returns a non-empty output".custompage_clearcache_message() , 'error' );
|
| 112 |
return " "; //Returning empty leads to undesired effect of getting a blank page
|
| 113 |
} else {
|
| 114 |
return $themed;
|
| 115 |
}
|
| 116 |
|
| 117 |
}
|
| 118 |
|
| 119 |
function custompage_clearcache_message() {
|
| 120 |
|
| 121 |
return "<div style=\"border-top: 1px solid silver;\"><b>ATTENTION:</b> Theme engine is strongly cached in Drupal versions 6 and up and
|
| 122 |
if/when you add a new tpl.php or a theming function, you need to ".l(t('clear cache'), 'admin/build/custompage/flushcache')." before you can see any changes.</div>";
|
| 123 |
|
| 124 |
}
|
| 125 |
|
| 126 |
function custompage_uri_load($id) {
|
| 127 |
return TRUE;
|
| 128 |
}
|
| 129 |
|
| 130 |
function _custompage_get_mappings() {
|
| 131 |
static $mappings;
|
| 132 |
|
| 133 |
if ( is_array( $mappings ) ) { //performance optimization
|
| 134 |
return $mappings;
|
| 135 |
}
|
| 136 |
|
| 137 |
$mappings = module_invoke_all('custompages');
|
| 138 |
|
| 139 |
/*** Structure:
|
| 140 |
$obj = new StdClass();
|
| 141 |
|
| 142 |
$obj->uri = 'homepage';
|
| 143 |
$obj->title = 'Home Page';
|
| 144 |
$obj->key = 'homepage';
|
| 145 |
|
| 146 |
$mappings[] = $obj;
|
| 147 |
**/
|
| 148 |
|
| 149 |
return $mappings;
|
| 150 |
}
|