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

Contents of /contributions/modules/markitup/markitup.module

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


Revision 1.7 - (show annotations) (download) (as text)
Sat Jan 10 07:24:22 2009 UTC (10 months, 2 weeks ago) by garrettalbright
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1, DRUPAL-6--2
Changes since 1.6: +14 -20 lines
File MIME type: text/x-php
Various quick fixes and patches; removal of the JS from the project
1 <?php
2 // $Id:
3
4 /**
5 * @file
6 * Enables the markitup universal content editor jQuery plugin.
7 */
8
9 // Define the path for the module.
10 define("MARKITUP_PATH", drupal_get_path('module', 'markitup'));
11
12 /**
13 * Implementation of hook_perm().
14 */
15 function markitup_perm() {
16 return array('administer markitup', 'use markitup editor');
17 }
18
19
20 /**
21 * Implementation of hook_menu().
22 */
23 function markitup_menu() {
24 $items['admin/settings/markitup'] = array(
25 'title' => 'markItUp Editor',
26 'description' => 'Edit content editor settings.',
27 'page callback' => 'markitup_admin_overview',
28 'access arguments' => array('administer markitup'),
29 'file' => 'markitup.admin.inc',
30 );
31 $items['admin/settings/markitup/overview'] = array(
32 'title' => 'Overview',
33 'type' => MENU_DEFAULT_LOCAL_TASK,
34 'weight' => 0,
35 );
36 $items['admin/settings/markitup/add'] = array(
37 'title' => 'Add markItUp Editor',
38 'page callback' => 'markitup_editor_page',
39 'type' => MENU_LOCAL_TASK,
40 'access arguments' => array('administer markitup'),
41 'file' => 'markitup.admin.inc',
42 'weight' => 1,
43 );
44 $items['admin/settings/markitup/%markitup_editor/edit'] = array(
45 'title' => 'Edit markItUp Editor',
46 'page callback' => 'markitup_editor_page',
47 'page arguments' => array(3),
48 'type' => MENU_CALLBACK,
49 'access arguments' => array('administer markitup'),
50 'file' => 'markitup.admin.inc',
51 );
52 $items['admin/settings/markitup/delete'] = array(
53 'title' => 'Delete markItUp Editor',
54 'page callback' => 'drupal_get_form',
55 'page arguments' => array('markitup_editor_delete'),
56 'type' => MENU_CALLBACK,
57 'access arguments' => array('administer markitup'),
58 'file' => 'markitup.admin.inc',
59 );
60
61 return $items;
62 }
63
64
65 /**
66 * Menu callback; menu wildcard loader for markItUp Editors.
67 */
68 function markitup_editor_load($meid) {
69
70 if (!is_numeric($meid)) {
71 return FALSE;
72 }
73
74 // Use markitup_editors to check for an editor with matching $meid
75 $editors = markitup_editors($meid);
76 //drupal_set_message('<pre>'. var_export($editors,TRUE) .'</pre>');
77 if (!isset($editors)) {
78 return FALSE;
79 }
80
81 return $editors;
82 }
83
84
85 /**
86 * Retrieve a list of markItUp Editors.
87 */
88 function markitup_editors($meid = NULL) {
89
90 $query = "SELECT * FROM {markitup_editors}";
91 $results = db_query($query);
92 while ($editor = db_fetch_array($results)) {
93 $editors[$editor['meid']] = $editor;
94 }
95
96 if (isset($meid)) {
97 // See if we've gotten a $meid argument (probably from menu callack) and if
98 // so, return just the one editor's meid.
99 return isset($editors[$meid]) ? $editors[$meid] : FALSE;
100 }
101 // return an array of editors
102 return $editors;
103 }
104
105
106 /**
107 * Retrieve a list of markItUp Editor and Input Format associations.
108 */
109 function markitup_editors_formats($full = FALSE) {
110 if ($full) {
111 // if $full is TRUE, we also want the info from the markitup_editors table.
112 $query = "SELECT * FROM {markitup_editors_formats} a LEFT OUTER JOIN {markitup_editors} b ON a.meid = b.meid;";
113 }
114 else {
115 $query = "SELECT * FROM {markitup_editors_formats} order by format";
116 }
117 $results = db_query($query);
118 $editors_formats = array();
119 while ($editor_format = db_fetch_array($results)) {
120 $editors_formats[$editor_format['format']] = $editor_format;
121 }
122 // return an array of editor and format associations.
123 return $editors_formats;
124 }
125
126
127 /**
128 * Internal function to get and then clean up directories lists.
129 */
130 function _markitup_dirs($dir) {
131 // Use $dir (which will be a plugin directory either skins or sets) to get
132 // a listing of sub-directories and feed them to $dirs.
133 $subdirs = scandir(MARKITUP_PATH .'/markitup/'. $dir);
134 $options = array();
135 // Clean our paths for security, since they'll be displayed in HTML.
136 // Also, we want an array where the key and value match.
137 foreach ($subdirs as $subdir) {
138 if (strpos($subdir, '.') !== 0 /*&& (file_exists(MARKITUP_PATH . "/markitup/{$subdir}/set.js") || file_exists(MARKITUP_PATH . "/markitup/{$subdir}/style.css"))*/) {
139 $options[$subdir] = $subdir;
140 }
141 }
142 return $options;
143 }
144
145
146 /**
147 * Invoke the markItUp plugin. Setup js and css.
148 */
149 function markitup() {
150
151 // Main markItUp js package.
152 // drupal_add_js(MARKITUP_PATH ."/markitup/jquery.markitup.pack.js", 'module', $scope = 'footer');
153 drupal_add_js(MARKITUP_PATH ."/markitup/jquery.markitup.js", 'module', $scope = 'footer');
154
155 $editors_formats = markitup_editors_formats($full = TRUE);
156
157 // We want to add all the js and css (for skins and sets) for each editor
158 // associated with an input format, since the editor will switch with the input
159 // format selected. In the case of users who don't have perms to change input
160 // formats, they'll just use the default input format and matching editor.
161 foreach ($editors_formats as $editor_format) {
162 // If an meid == 0, skip it. It was a "- none -" selection, i.e. no editor
163 // associated with the format.
164 if ($editor_format['meid'] != 0) {
165 $editors = markitup_editors($editor_format['meid']);
166 // print_r($editor_format['meid']);
167 drupal_add_css(MARKITUP_PATH ."/markitup/skins/{$editors['skin']}/style.css", $type = 'theme');
168 drupal_add_css(MARKITUP_PATH ."/markitup/sets/{$editors['miu_set']}/style.css", $type = 'theme');
169
170 drupal_add_js(MARKITUP_PATH ."/markitup/sets/{$editors['miu_set']}/set.js", 'module', $scope = 'footer');
171 }
172 }
173
174 // CSS for Drupal integration
175 drupal_add_css(MARKITUP_PATH ."/markitup.css", $type = 'module', $media = 'all', $preprocess = TRUE);
176
177 // Build js to invoke markitup.
178 $invoke_markitup = 'if (Drupal.jsEnabled) {';
179 $invoke_markitup .= "\n";
180
181 // Retrieve our textarea IDs.
182 $ids = variable_get('markitup_ids', '');
183
184 $textareas = array();
185 if (!empty($ids)) {
186 $textareas = explode("\r\n", $ids);
187 }
188 else {
189 $textareas[] = 'edit-body';
190 }
191
192 // Set up the textareaIDs array for our js.
193 $invoke_markitup .= 'var textareaIDs = new Array();';
194 $invoke_markitup .= "\n";
195 foreach($textareas as $id) {
196 static $i = 0; // IS STARTING AT ZERO OKAY? OR WILL IT MISMATCH US SOMEWHERE?
197 // Write a selector for the textarea.
198 $invoke_markitup .= 'textareaIDs['. $i .'] = "'. $id .'";';
199 $invoke_markitup .= "\n";
200 $i++;
201 }
202
203 // Setup our array associating formats and editor details - the editorsFormats
204 // array.
205
206 // Write JS array for use in editor switching.
207 $invoke_markitup .= 'var editorsFormats = new Array();';
208 $invoke_markitup .= "\n";
209
210 foreach ($editors_formats as $js_editor_format) {
211 // add to our array.
212 $invoke_markitup .= 'editorsFormats['. $js_editor_format['format'] .'] = [\''. $js_editor_format['skin'] .'\', \''. $js_editor_format['miu_set'] .'\'];';
213 $invoke_markitup .= "\n";
214 }
215
216 // Set up our default editor values for the js. We can draw upon the PHP
217 // $defaults object we setup, for variables like defaultSet.
218
219 $default_format = variable_get('filter_default_format', 1);
220
221 $results = db_query("SELECT * FROM {markitup_editors_formats} a LEFT OUTER JOIN {markitup_editors} b ON a.meid = b.meid WHERE a.format = '%s';", $default_format);
222 $defaults = db_fetch_object($results);
223
224 // Set up our default set.
225 $invoke_markitup .= 'var defaultSet';
226 $invoke_markitup .= "\n";
227 $invoke_markitup .= 'defaultSet = "'. $defaults->miu_set .'"';
228 $invoke_markitup .= "\n";
229
230 // The function used to invoke markitup.
231 $invoke_markitup .= '
232 var miuSet;
233
234
235 function markitup_invoke(textareaIDs, defaultSet, miuSet) {
236 //alert("textareaIDs: "+textareaIDs);
237 //alert("defaultSet: "+defaultSet);
238 //alert("miuSet: "+miuSet);
239 (miuSet) ? (miuSet = miuSet) : (miuSet = defaultSet);
240
241 $(document).ready(function() {
242 // Invoke the markItUp editor for all visible textareas (preventing)
243 // trouble with the Drupal teaser splitter.
244
245 function markitup_call(textareaID, set) {
246 // markItUp wants the name of the set presented to it as an object.
247 // So evaluate the set name in variable variable fashion.
248 setArg = eval("mySettings");
249
250 // Call the markitup plugin.
251 $("textarea#" + textareaID + ":visible").markItUp(setArg);
252 }
253
254 // Iterate through our list of textareaIDs setting up the calls.
255 $.each(textareaIDs, function(i, n) {
256 markitup_call(n, miuSet);
257 });
258
259 // Remove the Drupal grippie textarea resizer, since markItUp! comes
260 // with its own.
261 $("div.grippie").remove();
262 });
263 }';
264
265 $invoke_markitup .= '
266 // Trigger markItUp for first time. It will invoke the editor for the
267 // default input format.
268 //alert("textareaIDs" + textareaIDs);
269 //alert("defaultSet: " + defaultSet);
270 markitup_invoke(textareaIDs, defaultSet);
271 }';
272
273 drupal_add_js($invoke_markitup, 'inline', $scope = 'footer');
274
275 // If we've invoked markitup, turn off the teaser.
276 if (!empty($invoke_markitup)) {
277 // Let's avoid the issues that arise if the textarea splitter is on.
278 $turnoff_teaser = '
279 if (Drupal.jsEnabled) {
280 // Skip out of teaser splitter as defined in teaser.js.
281 Drupal.behaviors.teaser = function(context) {
282 return;
283 }
284 }
285 ';
286 drupal_add_js($turnoff_teaser, 'inline', $scope = 'footer');
287 }
288
289 // Get the default input format, if it's not been set, default to 1 - filtered HTML.
290 // TO DO: In the future we could add input format default matching.
291 $default_format = variable_get('filter_default_format', 1);
292
293 // For stylesheet switching (aka, changing editors) we want to turn off all editors, then turn only one on
294 $editor_switching = '
295 if (Drupal.jsEnabled) {
296 // set our default
297 var defaultEditor = editorsFormats['. $default_format .'];
298
299 // turn off editor skin css files
300 function markitup_editors_off() {
301 // onclick of input format, disable all skin css
302 $("link[@href*=markitup/skins]").each(function() {
303 this.disabled = true;
304 });
305 // disable all set css as well
306 $("link[@href*=markitup/sets]").each(function() {
307 this.disabled = true;
308 });
309
310 }
311
312 // turn on editor skin css file
313 function markitup_editor_css_on(skin, muiSet) {
314 // find each link where the href contains a string match and then iterate through each
315 $("link[@href*="+skin+"/style.css]").each(function() {
316 this.disabled = false;
317 });
318
319 $("link[@href*="+muiSet+"/style.css]").each(function() {
320 this.disabled = false;
321 });
322
323 //alert("skin: "+skin);
324 //alert("set: "+miuSet);
325 }
326
327 // initial state. we load all the editors on the page, but only want one on by default
328 markitup_editors_off();
329 markitup_editor_css_on(defaultEditor[0], defaultEditor[1]);
330 //alert("0 (skin): "+defaultEditor[0]);
331 //alert("1 (set): "+defaultEditor[1]);
332
333 // if user clicks on div with id containing format-[n]-wrapper, switch editor to match the clicked on format
334 function markitup_editor_switch(i) {
335 //alert(i);
336 // Listen for clicks...
337 $("div[@id*=format-" + i + "-wrapper]").click(function() {
338 //alert(i);
339
340 markitup_editors_off();
341
342 //remove formatting....
343 $("textarea").markItUpRemove();
344
345 markitup_editor_css_on(editorsFormats[i][0], editorsFormats[i][1]);
346 //alert(editorsFormats[i][0]);
347 //alert(editorsFormats[i][1]);
348
349 // Invoke markItUp if we have values for the editor
350 if ((editorsFormats[i][0] != \'\') || (editorsFormats[i][1] != \'\')) {
351 miuSet = editorsFormats[i][1];
352 markitup_invoke(textareaIDs, defaultSet, miuSet);
353 }
354 } );
355 }
356
357 // iterate through our editorsFormats relationships and setup the switch code
358 $.each(editorsFormats, function(i, n) {
359 //alert("sending you i: "+i);
360 markitup_editor_switch(i);
361 });
362
363 }
364 ';
365 drupal_add_js($editor_switching, 'inline', $scope = 'footer');
366
367 }
368
369
370 /**
371 * Implmentation of hook_init().
372 */
373 function markitup_init() {
374 // Invoke the loading of our js and css if user has permission.
375 if (user_access('use markitup editor')) {
376 // Thank you block.module (and yui_editor module :P )
377 $path = drupal_get_path_alias($_GET['q']);
378 $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote(variable_get("markitup_include", ''), '/')) .')$/';
379
380 $markitup_include = variable_get("markitup_include", "");
381 if(preg_match($regexp, $path) or empty($markitup_include)) {
382 markitup();
383 }
384 }
385 }
386
387
388 /**
389 * Implementation of hook_form_alter().
390 */
391 function markitup_form_alter(&$form, $form_state, $form_id){
392 //drupal_set_message('<pre>$form = '. var_export($form,TRUE) .'</pre>');
393
394 switch($form_id) {
395 case 'filter_admin_delete':
396 // If an input format is deleted via the input formats section, clean up
397 // the markitup_editors_formats table.
398 $form['#submit'][] = 'markitup_format_delete';
399 break;
400 }
401 }
402
403 /**
404 * Custom submit handler, overriding filter_admin_delete(). (See
405 * markitup_form_alter()).
406 */
407 function markitup_format_delete($form, &$form_state) {
408 db_query("DELETE FROM {markitup_editors_formats} WHERE format = %d", $form_state['values']['format']);
409 $form_state['redirect'] = 'admin/settings/filters';
410 }

  ViewVC Help
Powered by ViewVC 1.1.2