| 1 |
<?php
|
| 2 |
//$Id: attached_node.module,v 1.11 2004/12/08 22:17:18 javanaut Exp $
|
| 3 |
/*
|
| 4 |
This file is attached_node module - It allows users to
|
| 5 |
attach a node to another node.
|
| 6 |
|
| 7 |
Copyright (c) 2004 Mark Howell
|
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or
|
| 10 |
modify it under the terms of the GNU General Public License
|
| 11 |
as published by the Free Software Foundation; either version 2
|
| 12 |
of the License, or any later version.
|
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful,
|
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 17 |
GNU General Public License for more details.
|
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License
|
| 20 |
along with this program; if not, write to the Free Software
|
| 21 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 22 |
|
| 23 |
*/
|
| 24 |
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Adds attached_node stylesheet to html head section of pages
|
| 28 |
*/
|
| 29 |
function attached_node_init() {
|
| 30 |
global $base_url;
|
| 31 |
drupal_set_html_head("<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"$base_url/modules/attached_node/attached_node.css\" />");
|
| 32 |
}
|
| 33 |
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Hook which handles filtering.
|
| 37 |
*/
|
| 38 |
function attached_node_filter($op, $delta = 0, $format = -1, $text = '') {
|
| 39 |
//drupal_set_message("attached_node_filter invoked with \$op=$op");
|
| 40 |
switch ($op) {
|
| 41 |
case 'list':
|
| 42 |
return array(t("Attached Node Filter"));
|
| 43 |
case 'name':
|
| 44 |
return t("Attached Node Filter");
|
| 45 |
case 'description':
|
| 46 |
return t("Allows you to create meta tags that link to other nodes.");
|
| 47 |
case 'settings':
|
| 48 |
return attached_node_filter_settings();
|
| 49 |
case 'process':
|
| 50 |
return attached_node_filter_process($text);
|
| 51 |
default:
|
| 52 |
return $text;
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Function used to discover what filters are supplied by this module.
|
| 58 |
*/
|
| 59 |
function attached_node_filter_settings() {
|
| 60 |
$form['filter_attached_node'] = array(
|
| 61 |
'#type' => 'fieldset',
|
| 62 |
'#title' => t("Attached Node codes"),
|
| 63 |
'#collapsible' => TRUE,
|
| 64 |
'#collapsed' => FALSE
|
| 65 |
);
|
| 66 |
$form['filter_attached_node']['attached_node'] = array(
|
| 67 |
'#type' => 'item',
|
| 68 |
'#title' => t('Instructions'),
|
| 69 |
'#value' => t('This filter translates special node tags into thumbnailed representations that link to other nodes.
|
| 70 |
Syntax: [node:node_id,param1="val1",param2="val2"]; every param but node_id is optional.
|
| 71 |
Each list below should be a comma separated list (no spaces) of what node properties users are
|
| 72 |
allowed to override for the purposes of displaying a node. E.g. <em>title,res,border</em>')
|
| 73 |
);
|
| 74 |
foreach(node_get_types() as $type => $name) {
|
| 75 |
$form['filter_attached_node']["attached_node_allowed_vars_$type"] = array(
|
| 76 |
'#type' => 'textfield',
|
| 77 |
'#title' => t("Allowed parameters for %type nodes", array('%type'=>$type)),
|
| 78 |
'#default_value' => variable_get("attached_node_allowed_vars_$type", ""),
|
| 79 |
'#size' => 40,
|
| 80 |
'#maxlength' => 256
|
| 81 |
);
|
| 82 |
}
|
| 83 |
return $form;
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Actually execute filter on given text.
|
| 88 |
* Parse text for all meta tags.
|
| 89 |
* Lookup each specified node.
|
| 90 |
* Parse all parameters specified in each meta tag and apply them to the corresponding node.
|
| 91 |
* Replace meta tag with themed thumbnail view of the referrent node.
|
| 92 |
*/
|
| 93 |
function attached_node_filter_process($text, $show_error_msgs=1) {
|
| 94 |
if (variable_get("attached_node_enabled", 1)) {
|
| 95 |
// *** [node:NID,a="b",c="d",width="40",etc="foo"] ***
|
| 96 |
// first we find every tag
|
| 97 |
$meta_tag_regexp = '/\[node:(\d+)(.*)\]/i';
|
| 98 |
$meta_tag_params_regexp = '/,\s*([\w_-]+)\s*=\s*"([^"]+)"\s*/';
|
| 99 |
if (preg_match_all($meta_tag_regexp, $text, $match)) {
|
| 100 |
//print("matches:<br><pre>".var_dump($match, 1)."</pre>");
|
| 101 |
// then we make the html
|
| 102 |
foreach ($match[1] as $key => $nid) {
|
| 103 |
$map[$nid] = $key;
|
| 104 |
$node = node_load(array("nid"=>$nid));
|
| 105 |
if($node) {
|
| 106 |
// check here to see if user has permissions to access this node
|
| 107 |
if(!node_access("view", $node)) {
|
| 108 |
$matches_html[$key] = t('You do not have access to view this node');
|
| 109 |
continue;
|
| 110 |
}
|
| 111 |
|
| 112 |
//drupal_set_message("found node $nid and it's of type $node->type");
|
| 113 |
// iterate over all name=value pairs and override $node values with them
|
| 114 |
if (preg_match_all($meta_tag_params_regexp, $match[2][$key], $parm_match)) {
|
| 115 |
//print("\nparm_match = <pre>\n"); var_dump($parm_match); print("\n</pre>\n");
|
| 116 |
$allowed_vars = explode(',', variable_get("attached_node_allowed_vars_$node->type", ""));
|
| 117 |
for($i=0; $i<count($parm_match[1]); $i++) {
|
| 118 |
//$parm_match[1] contains the param names, $parm_match[2] contains values
|
| 119 |
$parm = $parm_match[1][$i];
|
| 120 |
$val = $parm_match[2][$i];
|
| 121 |
|
| 122 |
// check the name ($parm) to make sure it's an allowable value (like "align" or "border" or something)
|
| 123 |
if(in_array($parm, $allowed_vars)) {
|
| 124 |
//if($show_error_msgs) {
|
| 125 |
// drupal_set_message("setting node param '$parm' to value '$val' for ".$node->type);
|
| 126 |
//}
|
| 127 |
$node->$parm = $val;
|
| 128 |
}
|
| 129 |
else {
|
| 130 |
if($show_error_msgs) {
|
| 131 |
drupal_set_message("ignoring param '$parm' for ".$node->type." as it is not valid. Possible values are '<code>". variable_get("attached_node_allowed_vars_$node->type", "") ."</code>'.");
|
| 132 |
}
|
| 133 |
}
|
| 134 |
}
|
| 135 |
}
|
| 136 |
//$matches_html[$key] = "thumbnail of node $node->nid goes here";
|
| 137 |
//$matches_html[$key] = theme("attached_node_thumbnail", $node);
|
| 138 |
$matches_html[$key] = theme_attached_node_thumbnail($node);
|
| 139 |
}
|
| 140 |
else {
|
| 141 |
if($show_error_msgs) {
|
| 142 |
$matches_html[$key] = theme("attached_node_format", t("Nonexistent node nid: ") . "$nid.");
|
| 143 |
}
|
| 144 |
else {
|
| 145 |
$matches_html[$key] = "";
|
| 146 |
}
|
| 147 |
}
|
| 148 |
}
|
| 149 |
|
| 150 |
// PHP (4.3.2) str_replace appears to use some internal order
|
| 151 |
// rather than array index when finding replace elements
|
| 152 |
// corresponding to search elements. To make sure that the
|
| 153 |
// order of the arrays correspond, we make new copies here.
|
| 154 |
foreach ($match[1] as $key => $value) {
|
| 155 |
$mtch[] = $match[0][$key];
|
| 156 |
$repl[] = $matches_html[$key];
|
| 157 |
}
|
| 158 |
$text = str_replace($mtch, $repl, $text);
|
| 159 |
}
|
| 160 |
}
|
| 161 |
return $text;
|
| 162 |
}
|
| 163 |
|
| 164 |
/**
|
| 165 |
* Generates a thumbnail view of a node.
|
| 166 |
*/
|
| 167 |
function theme_attached_node_thumbnail(&$node) {
|
| 168 |
|
| 169 |
$main = true;
|
| 170 |
|
| 171 |
if($node->type == "image") {
|
| 172 |
|
| 173 |
// offer theme developers and module developers to override this
|
| 174 |
if(function_exists("image_attached_node_thumbnail")) {
|
| 175 |
return image_attached_node_thumbnail($node);
|
| 176 |
}
|
| 177 |
else {
|
| 178 |
// tweak upon image node's resolution control mechanism.
|
| 179 |
if(isset($node->res) || isset($_GET['res'])) {
|
| 180 |
|
| 181 |
// force override of whatever the currently set res is using metatag values
|
| 182 |
$res_tmp = $_GET['res'];
|
| 183 |
if(isset($node->res)) {
|
| 184 |
$_GET['res'] = $node->res;
|
| 185 |
}
|
| 186 |
$main = false;
|
| 187 |
}
|
| 188 |
}
|
| 189 |
}
|
| 190 |
|
| 191 |
$output = theme("attached_node_format",node_view($node, $main));
|
| 192 |
|
| 193 |
// image module cleanup
|
| 194 |
if(isset($res_tmp)) {
|
| 195 |
$_GET['res'] = $res_tmp;
|
| 196 |
}
|
| 197 |
|
| 198 |
return $output;
|
| 199 |
}
|
| 200 |
|
| 201 |
/**
|
| 202 |
* Puts a box around the attached_node's content
|
| 203 |
*/
|
| 204 |
function theme_attached_node_format($content) {
|
| 205 |
return '<div class="attached_node">' . $content . "</div>\n";
|
| 206 |
}
|
| 207 |
|
| 208 |
/**
|
| 209 |
* Clears the filter cache when any node is updated
|
| 210 |
*/
|
| 211 |
function attached_node_nodeapi(&$node, $op) {
|
| 212 |
if($op == "update") {
|
| 213 |
// since this is a node-specific filter, clear the cache when nodes are updated
|
| 214 |
cache_clear_all('filter', true);
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
/**
|
| 219 |
* Generates help text for this module
|
| 220 |
*/
|
| 221 |
function attached_node_help($section = "admin/help#attached_node") {
|
| 222 |
$output = "";
|
| 223 |
|
| 224 |
switch ($section) {
|
| 225 |
case 'admin/help#attached_node':
|
| 226 |
$output = t('<p><strong>Embedding Nodes Within Other Nodes</strong></p>
|
| 227 |
<p>
|
| 228 |
You can embed nodes within other nodes using the following syntax:<br>
|
| 229 |
[node:<em>node_id</em>,param_1="val1",param_2="val2"]<br><br>
|
| 230 |
In the special case of <strong>image</strong> nodes, you can specify the <em>res</em>
|
| 231 |
parameter to be any of the allowed resolutions that the image module handles.
|
| 232 |
Additionally, you can set to the value "original" to display the image in its original resolution.
|
| 233 |
</p>
|
| 234 |
<p>
|
| 235 |
Some examples:<br>
|
| 236 |
[node:123,res="640x480"]<br>
|
| 237 |
[node:123,res="original"]<br>
|
| 238 |
</p><p>Currently available parameters:<table border="1">');
|
| 239 |
foreach(node_get_types() as $type => $name) {
|
| 240 |
$output .= "<tr><td>" . t("Allowed parameters for <strong>$type</strong> nodes: ") . "</td><td>" . variable_get("attached_node_allowed_vars_$type", "") . "</td></tr>\n";
|
| 241 |
}
|
| 242 |
$output .= t('</table></p><p>
|
| 243 |
Site admin: If there are properties of a node that you want to allow your users to modify when referring to them,
|
| 244 |
you can specify those in the filter configuration.
|
| 245 |
</p>');
|
| 246 |
break;
|
| 247 |
case 'admin/modules#description':
|
| 248 |
$output = t("A utility module to attach nodes to other nodes.");
|
| 249 |
break;
|
| 250 |
}
|
| 251 |
return $output;
|
| 252 |
}
|
| 253 |
|
| 254 |
/**
|
| 255 |
* Implementation of hook_filter_tips().
|
| 256 |
*/
|
| 257 |
function attached_node_filter_tips($delta, $format, $long = false) {
|
| 258 |
if($long) {
|
| 259 |
$txt = t('<strong>Embedding Nodes Within Other Nodes</strong>
|
| 260 |
<p>
|
| 261 |
You can embed nodes within other nodes using the following syntax:<br>
|
| 262 |
[node:<em>node_id</em>,param_1="val1",param_2="val2"]<br><br>
|
| 263 |
In the special case of <strong>image</strong> nodes, you can specify the <em>res</em>
|
| 264 |
parameter to be any of the allowed resolutions that the image module handles.
|
| 265 |
Additionally, you can set to the value "original" to display the image in its original resolution.
|
| 266 |
</p>
|
| 267 |
<p>
|
| 268 |
Some examples:<br>
|
| 269 |
[node:123,res="640x480"]<br>
|
| 270 |
[node:123,res="original"]<br>
|
| 271 |
</p><p>Currently available parameters:<table border="1">');
|
| 272 |
foreach(node_get_types() as $type => $name) {
|
| 273 |
$txt .= "<tr><td>" . t("Allowed parameters for <strong>$type</strong> nodes: ") . "</td><td>" . variable_get("attached_node_allowed_vars_$type", "") . "</td></tr>\n";
|
| 274 |
}
|
| 275 |
|
| 276 |
$txt .= t('</table></p><p>
|
| 277 |
Site admin: If there are properties of a node that you want to allow your users to modify when referring to them,
|
| 278 |
you can specify those in the filter configuration.
|
| 279 |
|
| 280 |
</p>');
|
| 281 |
return $txt;
|
| 282 |
}
|
| 283 |
else {
|
| 284 |
return t('You can embed nodes within other nodes using the following syntax:<br>
|
| 285 |
[node:<em>node_id</em>,param_1="val1",param_2="val2"]');
|
| 286 |
}
|
| 287 |
}
|
| 288 |
|
| 289 |
?>
|