Mercurial > defr > drupal > scald > dnd
comparison dnd.module @ 2:5a44c430b7ac
Added dnd_test module, tons and tons of fixes and changes.
author | David Eads <eads@chicagotech.org> |
---|---|
date | Tue, 17 Feb 2009 15:34:56 -0600 |
parents | cee053085755 |
children | 5df0783706f7 |
comparison
equal
deleted
inserted
replaced
1:cee053085755 | 2:5a44c430b7ac |
---|---|
1 <?php | 1 <?php |
2 | |
3 // A suffix for auto generated IDs | |
4 define(DND_ID_SUFFIX, '-dnd-library'); | |
2 | 5 |
3 /** | 6 /** |
4 * Implementation of hook_menu(). | 7 * Implementation of hook_menu(). |
5 */ | 8 */ |
6 function dnd_menu() { | 9 function dnd_menu() { |
19 * Implementation of hook_perm(). | 22 * Implementation of hook_perm(). |
20 */ | 23 */ |
21 function dnd_perm() { | 24 function dnd_perm() { |
22 return array('administer dnd'); | 25 return array('administer dnd'); |
23 } | 26 } |
27 | |
28 /** | |
29 * Implementation of hook_theme(). | |
30 */ | |
31 function dnd_theme() { | |
32 return array( | |
33 'dnd_library' => array('arguments' => array('element' => NULL, 'settings' => NULL), 'template' => 'dnd-library'), | |
34 ); | |
35 } | |
36 | |
37 | |
38 /** | |
39 * Implementation of hook_elements(). | |
40 * | |
41 * Overload textareas. | |
42 */ | |
43 function dnd_elements() { | |
44 $type = array(); | |
45 $type['textarea'] = array( | |
46 '#input' => TRUE, | |
47 '#cols' => 60, | |
48 '#rows' => 5, | |
49 '#resizable' => TRUE, | |
50 '#dnd-enabled' => FALSE, | |
51 '#dnd-settings' => NULL, | |
52 '#process' => array('form_expand_ahah', 'dnd_process_textarea'), | |
53 ); | |
54 return $type; | |
55 } | |
56 | |
57 /** | |
58 * Settings array: | |
59 * What should it take, if anything? Probably a source... | |
60 * maybe editor specific configuration shit? | |
61 * | |
62 * - source for library json/ajax shit | |
63 * - target selector | |
64 * - item selector | |
65 * | |
66 * perhaps like so: | |
67 * | |
68 * global => | |
69 * droppable targets | |
70 * library source for textarea | |
71 * | |
72 * tinymce/othereditor => | |
73 * target selector logic | |
74 * configuration options | |
75 * callback should be smart about attachment and detachment | |
76 */ | |
77 function dnd_process_textarea($element, $form_state) { | |
78 if ($element['#dnd-enabled']) { | |
79 | |
80 drupal_add_js(drupal_get_path('module', 'dnd') .'/dnd/dnd.js'); | |
81 drupal_add_js(drupal_get_path('module', 'dnd') .'/js/dnd-library.js', 'footer'); | |
82 | |
83 $settings = array(); | |
84 | |
85 // We take a string or an | |
86 if (is_string($element['#dnd-settings'])) { | |
87 // @TODO load settings | |
88 } | |
89 else if (is_object($element['#dnd-settings'])) { | |
90 $settings = (array) $element['#dnd-settings']; | |
91 } | |
92 else if (is_array($element['#dnd-settings'])) { | |
93 $settings = $element['#dnd-settings']; | |
94 } | |
95 | |
96 // Set some important defaults | |
97 $settings = array('library_id' => $element['#id'] . DND_ID_SUFFIX) + $settings; | |
98 | |
99 // Add enabled libraries to settings for tracking | |
100 drupal_add_js(array( | |
101 'dndEnabledLibraries' => array($element['#id'] => $settings), | |
102 ), 'setting'); | |
103 | |
104 $element['#prefix'] = theme('dnd_library', $element, $settings); | |
105 } | |
106 return $element; | |
107 } | |
108 | |
109 function template_preprocess_dnd_library(&$variables) { | |
110 global $base_url; | |
111 list($element, $settings) = array($variables['element'], $variables['settings']); | |
112 | |
113 // Get library via a backdoor HTTP request. This is plenty fast for this | |
114 // application and keeps things nice and consistent. | |
115 if (!($url = parse_url($settings['url']))) { | |
116 return t('This library is not available'); | |
117 } | |
118 | |
119 // Handle both relative and absolute urls | |
120 if (!isset($url['scheme'])) { | |
121 $settings['url'] = $base_url .'/'. $settings['url']; | |
122 } | |
123 | |
124 $request = drupal_http_request($settings['url']); | |
125 | |
126 // We must remove some Drupal escaping | |
127 $json = json_decode(str_replace(array('\x3c', '\x3e', '\x26'), array("<", ">", "&"), $request->data), TRUE); | |
128 | |
129 foreach ($json['library'] as $id=>$item) { | |
130 $editor_representations[$id] = filter_xss_admin($item['editor']); | |
131 $library_representations[$id] = filter_xss_admin($item['library']); | |
132 } | |
133 | |
134 $variables['library_id'] = $settings['library_id']; | |
135 $variables['header'] = filter_xss_admin($json['header']); | |
136 $variables['items'] = $library_representations; | |
137 $variables['footer'] = filter_xss_admin($json['footer']); | |
138 | |
139 // Store editor representations in Drupal setting | |
140 drupal_add_js(array( | |
141 'dndEditorRepresentations' => $editor_representations, | |
142 ), 'setting'); | |
143 } |