1 | <?php |
---|
2 | // $Id: beautytips_ui.module,v 1.1.2.4 2010/05/10 21:48:24 kleinmp Exp $ |
---|
3 | |
---|
4 | /** |
---|
5 | * @file |
---|
6 | * Adds includes to Beautytips settings page and provides some built in functionality. |
---|
7 | * TODO: hook_theme invoke - maybe |
---|
8 | * Invoked include hooks: admin_info, menu_change, theme_change, form_change, menu_items |
---|
9 | * ex. beautytips_textinput_info |
---|
10 | */ |
---|
11 | |
---|
12 | function beautytips_ui_admin_settings(&$form) { |
---|
13 | $items = beautytips_ui_include_invoke('beautytips', 'admin_info', $args = array()); |
---|
14 | if (is_array($items)) { |
---|
15 | foreach ($items as $form_item) { |
---|
16 | if (count($form_item)) { |
---|
17 | $keys = array_keys($form_item); |
---|
18 | if (count($keys)) { |
---|
19 | foreach ($keys as $menu_item) { |
---|
20 | $form[$menu_item] = $form_item[$menu_item]; |
---|
21 | } |
---|
22 | } |
---|
23 | } |
---|
24 | } |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | /** |
---|
29 | * Clear menu cache and theme registry in case beautytips help tips turned on or off |
---|
30 | */ |
---|
31 | function beautytips_ui_admin_submit($form, $form_state) { |
---|
32 | module_invoke('menu', 'rebuild'); |
---|
33 | cache_clear_all('*', 'cache_menu', TRUE); |
---|
34 | drupal_rebuild_theme_registry(); |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * Implementation of hook_menu. |
---|
39 | */ |
---|
40 | function beautytips_ui_menu() { |
---|
41 | $items = array(); |
---|
42 | |
---|
43 | $menu_items = beautytips_ui_include_invoke('beautytips', 'menu_items', $args = array()); |
---|
44 | if (is_array($menu_items)) { |
---|
45 | foreach ($menu_items as $menu_item) { |
---|
46 | if (is_array($menu_item)) { |
---|
47 | $path = key($menu_item); |
---|
48 | $items[$path] = $menu_item[$path]; |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | return $items; |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Determine whether an include implements a hook, cf. module_hook. |
---|
57 | * |
---|
58 | * @param $tooltip |
---|
59 | * The name of the tooltip file (without the .inc extension), such as 'youtube' or 'google'. |
---|
60 | * @param $hook |
---|
61 | * The name of the hook (e.g. "thumbnail", "settings", etc.). |
---|
62 | * @return |
---|
63 | * TRUE if the tooltip is loaded and the hook is implemented. |
---|
64 | */ |
---|
65 | function beautytips_ui_include_hook($module, $tooltip, $hook) { |
---|
66 | return function_exists($module .'_'. $tooltip .'_'. $hook); |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Invoke hook in a particular include. |
---|
71 | * |
---|
72 | * @param $module |
---|
73 | * the helper module |
---|
74 | * @param $tooltip |
---|
75 | * The name of the tooltip (without the .inc extension). |
---|
76 | * @param $hook |
---|
77 | * The name of the hook (e.g. "menu_change", "form_change", etc.). |
---|
78 | * @param ... |
---|
79 | * Arguments to pass to the hook implementation. |
---|
80 | * @return |
---|
81 | * The return value of the hook implementation. |
---|
82 | */ |
---|
83 | function beautytips_ui_include_invoke($module, $hook, $args) { |
---|
84 | $includes = beautytips_ui_include_list(); |
---|
85 | foreach ($includes as $tooltip) { |
---|
86 | $function = $module .'_'. $tooltip .'_'. $hook; |
---|
87 | $params[] = beautytips_ui_include_hook($module, $tooltip, $hook) ? call_user_func_array($function, $args) : NULL; |
---|
88 | } |
---|
89 | return $params; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Maintains a list of all loaded include files. |
---|
94 | * |
---|
95 | * @param $file |
---|
96 | * ------------------------- |
---|
97 | * @return |
---|
98 | * An array of all loaded includes. (w/o appended .inc) |
---|
99 | */ |
---|
100 | // TODO: This may need to change - only unclude the items needed - maybe |
---|
101 | function beautytips_ui_include_list() { |
---|
102 | static $list = array(); |
---|
103 | $dir = drupal_get_path('module', 'beautytips') .'/includes'; |
---|
104 | $files = file_scan_directory($dir, '.inc'); |
---|
105 | foreach ($files as $file) { |
---|
106 | include_once('./'. $file->filename); |
---|
107 | $list[$file->filename] = $file->name; |
---|
108 | } |
---|
109 | |
---|
110 | return $list; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Implementation of hook_form_alter. |
---|
115 | * Adds beautytips for textareas and textfields |
---|
116 | */ |
---|
117 | function beautytips_ui_form_alter(&$form, $form_state, $form_id) { |
---|
118 | beautytips_ui_include_invoke('beautytips', 'form_change', |
---|
119 | array( |
---|
120 | 'form' => &$form, |
---|
121 | 'form_state' => $form_state, |
---|
122 | 'form_id' => $form_id, |
---|
123 | ) |
---|
124 | ); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Implementation of hook_menu_alter(). |
---|
129 | */ |
---|
130 | function beautytips_ui_menu_alter(&$items) { |
---|
131 | beautytips_ui_include_invoke('beautytips', 'menu_change', array('items' => &$items)); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Implementation of hook_theme_registry_alter(). |
---|
136 | */ |
---|
137 | function beautytips_ui_theme_registry_alter(&$theme_registry) { |
---|
138 | beautytips_ui_include_invoke('beautytips', 'theme_change', array('theme_registry' => &$theme_registry)); |
---|
139 | } |
---|