| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* The Drawing API module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_theme().
|
| 11 |
*/
|
| 12 |
function drawing_theme() {
|
| 13 |
$drawing_themes = array();
|
| 14 |
// all our themes have the same registry data:
|
| 15 |
$drawing = array(
|
| 16 |
'arguments' => array(
|
| 17 |
'element' => NULL,
|
| 18 |
),
|
| 19 |
'function' => 'theme_drawing',
|
| 20 |
);
|
| 21 |
// list of themes
|
| 22 |
$themes = array(
|
| 23 |
'drawing_canvas',
|
| 24 |
'drawing_group',
|
| 25 |
'drawing_file',
|
| 26 |
'drawing_ellipse',
|
| 27 |
'drawing_rectangle',
|
| 28 |
'drawing_line',
|
| 29 |
'drawing_polyline',
|
| 30 |
'drawing_polygon',
|
| 31 |
'drawing_path',
|
| 32 |
'drawing_text',
|
| 33 |
'drawing_circle',
|
| 34 |
'drawing',
|
| 35 |
);
|
| 36 |
foreach ($themes as $theme) {
|
| 37 |
$drawing_themes[$theme] = $drawing;
|
| 38 |
}
|
| 39 |
return $drawing_themes;
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Implementation of hook_menu().
|
| 44 |
*/
|
| 45 |
function drawing_menu() {
|
| 46 |
$items = array();
|
| 47 |
$items['admin/settings/drawing'] = array(
|
| 48 |
'title' => 'Drawing API',
|
| 49 |
'description' => 'Drawing API settings',
|
| 50 |
'page callback' => 'drupal_get_form',
|
| 51 |
'page arguments' => array('drawing_admin'),
|
| 52 |
'access arguments' => array('access administration pages'),
|
| 53 |
'type' => MENU_NORMAL_ITEM,
|
| 54 |
);
|
| 55 |
return $items;
|
| 56 |
}
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Admin form for Drawing API settings.
|
| 60 |
*/
|
| 61 |
function drawing_admin() {
|
| 62 |
$form = array();
|
| 63 |
$form['drawing_type'] = array(
|
| 64 |
'#type' => 'radios',
|
| 65 |
'#title' => t('Set toolkit'),
|
| 66 |
'#default_value' => variable_get('drawing_type', 'drawing_svg'),
|
| 67 |
'#description' => t('Select which toolkit you want to enable on the site.'),
|
| 68 |
'#options' => drawing_get_all_methods(),
|
| 69 |
);
|
| 70 |
return system_settings_form($form);
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Retrieves a canvas from a builder function, passes it on for
|
| 75 |
* processing, and renders the canvas.
|
| 76 |
*
|
| 77 |
* @param $canvas_id
|
| 78 |
* The unique string identifying the desired canvas. If a function
|
| 79 |
* with that name exists, it is called to build the canvas array.
|
| 80 |
* Modules that need to generate the same canvas (or very similar canvases)
|
| 81 |
* using different $canvas_ids can implement hook_canvases(), which maps
|
| 82 |
* different $canvas_id values to the proper canvas building function.
|
| 83 |
* @param ...
|
| 84 |
* Any additional arguments needed by the canvas building function.
|
| 85 |
* @return
|
| 86 |
* The rendered canvas.
|
| 87 |
*/
|
| 88 |
function drawing_get_canvas($canvas_id) {
|
| 89 |
$args = func_get_args();
|
| 90 |
$canvas = call_user_func_array('drawing_retrieve_canvas', $args);
|
| 91 |
drawing_prepare_canvas($args[0], $canvas);
|
| 92 |
return drawing_render_canvas($canvas_id, $canvas);
|
| 93 |
}
|
| 94 |
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Retrieves the structured array that defines a given canvas.
|
| 98 |
*
|
| 99 |
* @param $canvas_id
|
| 100 |
* The unique string identifying the desired canvas. If a function
|
| 101 |
* with that name exists, it is called to build the canvas array.
|
| 102 |
* Modules that need to generate the same canvas (or very similar canvases)
|
| 103 |
* using different $canvas_ids can implement hook_canvases(), which maps
|
| 104 |
* different $canvas_id values to the proper canvas building function.
|
| 105 |
* @param ...
|
| 106 |
* Any additional arguments needed by the canvas building function.
|
| 107 |
*/
|
| 108 |
function drawing_retrieve_canvas($canvas_id) {
|
| 109 |
static $canvases;
|
| 110 |
$args = func_get_args();
|
| 111 |
$saved_args = $args;
|
| 112 |
array_shift($args);
|
| 113 |
|
| 114 |
if (!function_exists($canvas_id)) {
|
| 115 |
// if you want to handle your canvas uniquely, do it with hook_canvases()
|
| 116 |
if (!isset($canvases) || !isset($canvases[$canvas_id])) {
|
| 117 |
$canvases = module_invoke_all('canvases', $saved_args);
|
| 118 |
}
|
| 119 |
$canvas_definition = $canvases[$canvas_id];
|
| 120 |
if (isset($canvas_definition['callback arguments'])) {
|
| 121 |
$args = array_merge($canvas_definition['callback arguments'], $args);
|
| 122 |
}
|
| 123 |
if (isset($canvas_definition['callback'])) {
|
| 124 |
$callback = $canvas_definition['callback'];
|
| 125 |
}
|
| 126 |
}
|
| 127 |
$canvas = call_user_func_array(isset($callback) ? $callback : $canvas_id, $args);
|
| 128 |
$canvas['#parameters'] = $saved_args;
|
| 129 |
return $canvas;
|
| 130 |
}
|
| 131 |
|
| 132 |
/**
|
| 133 |
* Renders a structured canvas array into themed output.
|
| 134 |
*
|
| 135 |
* @param $canvas_id
|
| 136 |
* A unique string identifying the canvas for hook_drawing_canvas_alter
|
| 137 |
* functions.
|
| 138 |
* @param $canvas
|
| 139 |
* An associative array containing the structure of the canvas.
|
| 140 |
* @return
|
| 141 |
* The themed output as rendered by drupal_render().
|
| 142 |
*/
|
| 143 |
function drawing_render_canvas($canvas_id, &$canvas) {
|
| 144 |
// Don't override #theme if someone already set it.
|
| 145 |
if (!isset($canvas['#theme'])) {
|
| 146 |
init_theme();
|
| 147 |
$registry = theme_get_registry();
|
| 148 |
if (isset($registry[$canvas_id])) {
|
| 149 |
$canvas['#theme'] = $canvas_id;
|
| 150 |
}
|
| 151 |
}
|
| 152 |
$output = drupal_render($canvas);
|
| 153 |
return $output;
|
| 154 |
}
|
| 155 |
|
| 156 |
/**
|
| 157 |
* Prepares a structured canvas array by adding required elements,
|
| 158 |
* and executing any hook_drawing_canvas_alter functions.
|
| 159 |
*
|
| 160 |
* @param $canvas_id
|
| 161 |
* A unique string identifying the canvas for hook_drawing_canvas_alter
|
| 162 |
* functions.
|
| 163 |
* @param $canvas
|
| 164 |
* An associative array containing the structure of the canvas.
|
| 165 |
*/
|
| 166 |
function drawing_prepare_canvas($canvas_id, &$canvas) {
|
| 167 |
if ($canvas['#type'] == 'drawing_canvas') {
|
| 168 |
$canvas += _element_info('drawing_canvas');
|
| 169 |
}
|
| 170 |
elseif ($canvas['#type'] == 'drawing_file') {
|
| 171 |
$canvas += _element_info('drawing_file');
|
| 172 |
}
|
| 173 |
foreach (module_implements('drawing_canvas_alter') as $module) {
|
| 174 |
$function = $module .'_drawing_canvas_alter';
|
| 175 |
$function($form_id, $form);
|
| 176 |
}
|
| 177 |
return $canvas;
|
| 178 |
}
|
| 179 |
|
| 180 |
/**
|
| 181 |
* Extract the defined method, defaults to 'drawing_svg'.
|
| 182 |
*/
|
| 183 |
function drawing_get_method() {
|
| 184 |
static $drawing_method;
|
| 185 |
if (empty($drawing_method)) {
|
| 186 |
$drawing_method = variable_get('drawing_method', 'drawing_svg');
|
| 187 |
}
|
| 188 |
return $drawing_method;
|
| 189 |
}
|
| 190 |
|
| 191 |
/**
|
| 192 |
* Get a list of drawing methods from other modules.
|
| 193 |
*/
|
| 194 |
function drawing_get_all_methods() {
|
| 195 |
static $drawing_methods;
|
| 196 |
if (empty($drawing_methods)) {
|
| 197 |
$drawing_methods = module_invoke_all('drawing_method');
|
| 198 |
if (empty($drawing_methods)) {
|
| 199 |
drupal_set_message(t('Drawing API could not find any enabled <em>toolkit</em> modules.'), 'error');
|
| 200 |
}
|
| 201 |
}
|
| 202 |
return $drawing_methods;
|
| 203 |
}
|
| 204 |
|
| 205 |
/**
|
| 206 |
* Implementation of hook_elements().
|
| 207 |
*/
|
| 208 |
function drawing_elements() {
|
| 209 |
$method = variable_get('drawing_method', 'drawing_svg');
|
| 210 |
$type['drawing_canvas'] = array(
|
| 211 |
'#width' => '200px',
|
| 212 |
'#height' => '200px',
|
| 213 |
);
|
| 214 |
$type['drawing_group'] = array(
|
| 215 |
'#width' => '200px',
|
| 216 |
'#height' => '200px',
|
| 217 |
);
|
| 218 |
$type['drawing_file'] = array(
|
| 219 |
'#height' => '200px',
|
| 220 |
'#width' => '200px',
|
| 221 |
);
|
| 222 |
return $type;
|
| 223 |
}
|
| 224 |
|
| 225 |
/**
|
| 226 |
* Theme function.
|
| 227 |
*/
|
| 228 |
function theme_drawing($element) {
|
| 229 |
$method = drawing_get_method();
|
| 230 |
return theme($method .'_'. $element['#type'], $element);
|
| 231 |
}
|
| 232 |
|
| 233 |
/*
|
| 234 |
To do:
|
| 235 |
|
| 236 |
chx says we should use '#process to tell the element that it's parent is svg.
|
| 237 |
quote: "add a #process to the element and propagate down the value.
|
| 238 |
for now I'm just applying svg
|
| 239 |
|
| 240 |
Another idea; use preprocess functions in hook_theme to deal with the 'method'?
|
| 241 |
|
| 242 |
*/
|