eads@1: 'Drag and Drop Library', eads@1: 'page callback' => 'dnd_admin', eads@1: 'description' => 'Configure drag-and-drop enabled textareas.', eads@1: 'access arguments' => array('administer dnd'), eads@1: 'file' => 'dnd.admin.inc', eads@1: ); eads@1: return $items; eads@1: } eads@1: eads@1: /** eads@1: * Implementation of hook_perm(). eads@1: */ eads@1: function dnd_perm() { eads@1: return array('administer dnd'); eads@1: } eads@2: eads@2: /** eads@2: * Implementation of hook_theme(). eads@2: */ eads@2: function dnd_theme() { eads@2: return array( eads@2: 'dnd_library' => array('arguments' => array('element' => NULL, 'settings' => NULL), 'template' => 'dnd-library'), eads@2: ); eads@2: } eads@2: eads@2: eads@2: /** eads@2: * Implementation of hook_elements(). eads@2: * eads@2: * Overload textareas. eads@2: */ eads@2: function dnd_elements() { eads@2: $type = array(); eads@2: $type['textarea'] = array( eads@2: '#input' => TRUE, eads@2: '#cols' => 60, eads@2: '#rows' => 5, eads@2: '#resizable' => TRUE, eads@2: '#dnd-enabled' => FALSE, eads@2: '#dnd-settings' => NULL, eads@2: '#process' => array('form_expand_ahah', 'dnd_process_textarea'), eads@2: ); eads@2: return $type; eads@2: } eads@2: eads@2: /** eads@2: * Settings array: eads@2: * What should it take, if anything? Probably a source... eads@2: * maybe editor specific configuration shit? eads@2: * eads@2: * - source for library json/ajax shit eads@2: * - target selector eads@2: * - item selector eads@2: * eads@2: * perhaps like so: eads@2: * eads@2: * global => eads@2: * droppable targets eads@2: * library source for textarea eads@2: * eads@2: * tinymce/othereditor => eads@2: * target selector logic eads@2: * configuration options eads@2: * callback should be smart about attachment and detachment eads@2: */ eads@2: function dnd_process_textarea($element, $form_state) { eads@2: if ($element['#dnd-enabled']) { eads@2: eads@4: drupal_add_js(drupal_get_path('module', 'dnd') .'/js/dnd.js', 'footer'); eads@4: drupal_add_js(drupal_get_path('module', 'dnd') .'/js/dnd-library.js', 'footer'); eads@2: eads@2: $settings = array(); eads@2: eads@2: // We take a string or an eads@2: if (is_string($element['#dnd-settings'])) { eads@2: // @TODO load settings eads@2: } eads@2: else if (is_object($element['#dnd-settings'])) { eads@2: $settings = (array) $element['#dnd-settings']; eads@2: } eads@2: else if (is_array($element['#dnd-settings'])) { eads@2: $settings = $element['#dnd-settings']; eads@2: } eads@2: eads@2: // Set some important defaults eads@2: $settings = array('library_id' => $element['#id'] . DND_ID_SUFFIX) + $settings; eads@2: eads@2: // Add enabled libraries to settings for tracking eads@2: drupal_add_js(array( eads@2: 'dndEnabledLibraries' => array($element['#id'] => $settings), eads@2: ), 'setting'); eads@2: eads@2: $element['#prefix'] = theme('dnd_library', $element, $settings); eads@2: } eads@2: return $element; eads@2: } eads@2: eads@2: function template_preprocess_dnd_library(&$variables) { eads@2: global $base_url; eads@2: list($element, $settings) = array($variables['element'], $variables['settings']); eads@2: eads@2: // Get library via a backdoor HTTP request. This is plenty fast for this eads@2: // application and keeps things nice and consistent. eads@2: if (!($url = parse_url($settings['url']))) { eads@2: return t('This library is not available'); eads@2: } eads@2: eads@2: // Handle both relative and absolute urls eads@2: if (!isset($url['scheme'])) { eads@2: $settings['url'] = $base_url .'/'. $settings['url']; eads@2: } eads@2: eads@2: $request = drupal_http_request($settings['url']); eads@2: eads@2: // We must remove some Drupal escaping eads@2: $json = json_decode(str_replace(array('\x3c', '\x3e', '\x26'), array("<", ">", "&"), $request->data), TRUE); eads@2: eads@4: // Generate an array of editor representations to add eads@4: foreach ($json['editor_representations'] as $editor_id=>$editor_item) { eads@4: $editor_representations[$editor_id] = filter_xss_admin($editor_item); eads@2: } eads@2: eads@4: // Store editor representations in Drupal setting eads@4: drupal_add_js(array('dndEditorRepresentations' => $editor_representations,), 'setting'); eads@4: eads@2: $variables['library_id'] = $settings['library_id']; eads@4: $variables['header'] = filter_xss_admin($json['header']); eads@4: $variables['library'] = filter_xss_admin($json['library']); eads@4: $variables['footer'] = filter_xss_admin($json['footer']); eads@2: }