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