/[drupal]/contributions/modules/jq/jq.module
ViewVC logotype

Contents of /contributions/modules/jq/jq.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Sun May 4 19:03:24 2008 UTC (18 months, 3 weeks ago) by aaron
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +162 -16 lines
File MIME type: text/x-php
revert to 5.x-dev
1 <?php
2 // $Id: jq.module,v 1.9 2008/04/06 15:28:58 aaron Exp $
3
4 /**
5 * implements hook_menu
6 */
7 function jq_menu($may_cache) {
8 $items = array();
9 if ($may_cache) {
10 $items[] = array(
11 'path' => 'admin/settings/jq',
12 'title' => t('jQ Plugin Administration'),
13 'description' => t('Administer jQ (jQuery) Plugin Repository.'),
14 'callback' => 'drupal_get_form',
15 'callback arguments' => array('jq_settings_form'),
16 'type' => MENU_NORMAL_ITEM,
17 'access' => user_access('administer jq'),
18 );
19 }
20 return $items;
21 }
22
23 function jq_perm() {
24 return array('administer jq');
25 }
26
27 /**
28 * The administration form. Allows an administrator to turn off specific plugins
29 */
30 function jq_settings_form() {
31 $plugins = jq_plugins(NULL, FALSE, TRUE);
32 $form = array();
33 $form['jq'] = array(
34 '#type' => 'fieldset',
35 '#title' => t('jQuery Plugins'),
36 '#description' => t('These jQuery plugins are currently registered through the jQ module.'),
37 '#collapsible' => TRUE,
38 );
39 foreach ($plugins as $key => $plugin) {
40 $description = $plugin['description'];
41 if ($plugin['version'] || $plugin['url']) {
42 $description .= '<br />';
43 }
44 if ($plugin['version']) {
45 $description .= t('<br />%Version: @version', array('%Version' => t('Version'), '@version' => $plugin['version']));
46 }
47 if ($plugin['url']) {
48 $description .= t('<br />%Homepage: !url', array('%Homepage' => t('Homepage'), '!url' => l($plugin['name'], $plugin['url'])));
49 }
50 $description .= t('<br />%Invocation: ', array('%Invocation' => t('Invocation')));
51 $description .= $plugin['invocation'] ? $plugin['invocation'] : t('Invoke this plugin with %code', array('%code' => "jq_add('$key');"));
52 $form['jq']['jq_allow_' . $key] = array(
53 '#type' => 'fieldset',
54 '#title' => $plugin['name'],
55 '#collapsible' => TRUE,
56 '#collapsed' => TRUE,
57 );
58 $form['jq']['jq_allow_' . $key]['allow'] = array(
59 '#type' => 'checkbox',
60 '#title' => t('Enable @plugin (%code)', array('@plugin' => $plugin['name'], '%code' => $key)),
61 '#description' => $description,
62 '#default_value' => variable_get('jq_allow_' . $key, TRUE),
63 );
64 $subform = module_invoke($plugin['module'], 'jq', 'settings');
65 if (is_array($subform)) {
66 $form['jq']['jq_allow_' . $key]['settings'] = array(
67 '#type' => 'fieldset',
68 '#title' => t('Extra settings'),
69 '#collapsible' => TRUE,
70 '#collapsed' => FALSE,
71 );
72 $form['jq']['jq_allow_' . $key]['settings']['subform'] = $subform;
73 }
74 }
75 if (empty($plugins)) {
76 drupal_set_message(t('There are currently no defined jQuery plugins registered through the jQ module. You only need to enable this module if directed to by another.'), 'error');
77 }
78 return system_settings_form($form);
79 }
80
81 /**
82 * This will add a specific jquery plugin to a page, if it hasn't already been.
83 * Returns whether the plugin was successfully loaded or not. It will pass through any extra arguments.
84 *
85 * For a plugin to be registered with this module, a module needs to invoke hook_jq.
86 * hook_jq($op = 'info|add', $plugin = NULL)
87 * if $op is add, then it is called with the plugin when invoked on a page
88 * if $op is info, then it needs to return an associative array of defined plugins:
89 * 'name' => name of the plugin
90 * 'description' => description of the plugin
91 * 'files' => array(
92 * 'js' => array(
93 * an array of js files to be loaded on the page
94 * ),
95 * 'css' => array(
96 * an array of css files to be loaded on the page
97 * ),
98 * ),
99 *
100 * individual plugins may be disabled by the admin at /admin/settings/jq
101 */
102 function jq_add($plugin) {
103 $extra = func_get_args();
104 array_shift($extra);
105 return _jq_add($plugin, $extra);
106 }
107
108 function _jq_add($plugin, $extra = array(), $cached = TRUE, $display_errors = FALSE, $log_errors = TRUE) {
109 static $invoked_plugins, $errors;
110 $jq = jq_plugins($plugin, $cached, $display_errors);
111 if (!isset($invoked_plugins[$plugin])) {
112 if (isset($plugin) && isset($jq)) {
113 if (!variable_get('jq_allow_' . $plugin, TRUE)) {
114 $error = t('The %plugin jQuery plugin has been disabled.', array('%plugin' => $plugin));
115 if ($log_errors) {
116 watchdog('jq', $error, WATCHDOG_NOTICE);
117 }
118 if ($display_errors) {
119 drupal_set_message($error, 'error');
120 }
121 $invoked_plugins[$plugin] = FALSE;
122 }
123 else {
124 if (is_array($jq['files']['js'])) {
125 foreach ($jq['files']['js'] as $file) {
126 drupal_add_js($file);
127 }
128 }
129 if (is_array($jq['files']['css'])) {
130 foreach ($jq['files']['css'] as $file) {
131 drupal_add_css($file);
132 }
133 }
134 $invoked_plugins[$plugin] = TRUE;
135 }
136 }
137 else {
138 // log & display an error, but only if we haven't already. don't want to overwhelm with a lot of identical errors per page
139 $error = t('The %plugin jQuery plugin is not defined.', array('%plugin' => $plugin));
140 if ($log_errors) {
141 watchdog('jq', $error, WATCHDOG_ERROR);
142 }
143 if ($display_errors) {
144 drupal_set_message($error, 'error');
145 }
146 $invoked_plugins[$plugin] = FALSE;
147 }
148 }
149 if ($invoked_plugins[$plugin]) {
150 $args = array('add', $plugin);
151 $args = array_merge($args, (array)$extra);
152 $function = $jq['module'] .'_jq';
153 call_user_func_array($function, $args);
154 }
155 return $invoked_plugins[$plugin];
156 }
157
158 /**
159 * returns all module defined plugins that are registered using hook_jq
160 * this is cached, so a module is responsible for calling this on installation
161 */
162 function jq_plugins($plugin = NULL, $cached = TRUE, $display_errors = FALSE, $log_errors = TRUE) {
163 static $plugins;
164 if (!isset($plugins) || !$cached) {
165 if ($cached && $cache = cache_get('jq_plugins', 'cache')) {
166 $plugins = unserialize($cache->data);
167 }
168 else {
169 $plugins = _jq_plugins($display_errors);
170 cache_set('jq_plugins', 'cache', serialize($plugins));
171 }
172 }
173 if (isset($plugin)) {
174 return $plugins[$plugin];
175 }
176 return $plugins;
177 }
178
179 function _jq_plugins($display_errors = FALSE, $log_errors = TRUE) {
180 $plugins = array();
181 foreach (module_implements('jq') as $module) {
182 $mod_jq = module_invoke($module, 'jq', 'info');
183 if (is_array($mod_jq)) {
184 foreach ($mod_jq as $key => $plugin) {
185 if (isset($plugins[$key])) {
186 $error = t('There is a conflict with the %plugin jQuery plugin. It is defined by both the %module1 and %module2 modules.', array('%plugin' => $key, '%module1' => $plugins[$key]['module'], '%module2' => $module));
187 if ($log_errors) {
188 watchdog('jq', $error, WATCHDOG_WARNING);
189 }
190 if ($display_errors) {
191 drupal_set_message($error, 'error');
192 }
193 }
194 else {
195 $plugins[$key] = $plugin;
196 $plugins[$key]['plugin'] = $key;
197 $plugins[$key]['module'] = $module;
198 }
199 }
200 }
201 }
202 ksort($plugins);
203 return $plugins;
204 }

  ViewVC Help
Powered by ViewVC 1.1.2