annotate mee.module @ 12:da5d54d099b1

Fix the drag'n'drop, add a caption to the ressource manager. tableDrag makeDraggable method expect a standard DOM object, not the jQuery wrapper.
author Franck Deroche <franck@defr.org>
date Fri, 05 Jun 2009 09:56:08 +0200
parents 272628486e75
children 842d89897cb4
rev   line source
eads@0 1 <?php
eads@0 2 // $Id;
eads@0 3 /**
eads@0 4 * @file
eads@0 5 * Defines a special textarea, with drag and drop media driven by Scald and
eads@0 6 * dnd.module when rich text editing is enabled on the textarea via the
eads@0 7 * WYSIWYG API.
eads@0 8 */
eads@0 9
eads@0 10 /**
eads@0 11 * Implementation of hook_theme().
eads@0 12 */
eads@0 13 function mee_theme() {
eads@0 14 $theme = array(
eads@0 15 'mee_textarea' => array(
eads@0 16 'arguments' => array('element' => NULL),
eads@0 17 ),
franck@11 18 'mee_ressource_manager' => array(
franck@11 19 'arguments' => array('element' => NULL),
franck@11 20 ),
eads@0 21 'mee_formatter_default' => array(
eads@0 22 'arguments' => array('element' => NULL),
eads@0 23 ),
eads@0 24 );
tom@2 25
tom@2 26 $scald_config = variable_get('scald_config', 0);
tom@2 27 foreach ($scald_config->contexts as $context => $details) {
tom@2 28 $theme['mee_formatter_'. $context] = array(
tom@2 29 'arguments' => array('element' => NULL),
tom@2 30 'function' => 'theme_mee_context_formatter',
tom@2 31 );
tom@2 32 }
eads@0 33 return $theme;
eads@0 34 }
eads@0 35
eads@0 36 /**
eads@0 37 * Implementation of hook_field_info().
eads@0 38 */
eads@0 39 function mee_field_info() {
eads@0 40 return array(
eads@0 41 'multimedia_editorial_element' => array(
eads@0 42 'label' => t('Multimedia Editorial Element (MEE)'),
eads@0 43 'description' => t('MEE combines Scald, WYSIWYG, and DnD to create a multimedia enabled text field.'),
eads@0 44 ),
eads@0 45 );
eads@0 46 }
eads@0 47
eads@0 48 /**
eads@0 49 * Implementation of hook_field_settings().
eads@0 50 */
eads@0 51 function mee_field_settings($op, $field) {
eads@0 52 switch ($op) {
eads@0 53 case 'form':
eads@0 54 $form = array();
eads@0 55 $options = array(0 => t('Plain text'), 1 => t('Filtered text (user selects input format)'));
tom@5 56
tom@5 57 $scald_config = variable_get('scald_config', 0);
tom@5 58 $context_options = array();
tom@5 59 $contexts_result = db_query("SELECT context, title FROM {scald_contexts} WHERE context IN ('" . implode("', '", array_keys($scald_config->contexts)) . "') ORDER BY title");
tom@5 60 while ($context_raw = db_fetch_array($contexts_result)) {
tom@5 61 $context_options[$context_raw['context']] = $context_raw['title'];
tom@5 62 }
tom@5 63
eads@0 64 $form['mee_processing'] = array(
eads@0 65 '#type' => 'radios',
eads@0 66 '#title' => t('Text processing'),
eads@0 67 '#default_value' => is_numeric($field['mee_processing']) ? $field['mee_processing'] : 1,
eads@0 68 '#options' => $options,
eads@0 69 '#description' => t('Filtered text, with a WYSIWYG editor defined on one or more input formats, is strongly recommended.'),
eads@0 70 );
eads@0 71 // @TODO Ask Drupal about available libraries
eads@0 72 $form['mee_dnd_callback_url'] = array(
eads@0 73 '#type' => 'textfield',
eads@0 74 '#title' => t('Library callback URL'),
eads@0 75 '#default_value' => url($field['mee_dnd_callback_url']) ? $field['mee_dnd_callback_url'] : '',
eads@0 76 '#description' => t('The absolute URL or relative path of a callback URL that provides proper JSON to the drag and drop library.'),
eads@0 77 );
tom@5 78 $form['mee_scald_editor_context'] = array(
tom@5 79 '#type' => 'select',
tom@5 80 '#title' => t('Scald Editor Context'),
tom@5 81 '#description' => t('Choose a Scald Context to use for displaying Scald Atoms included in the textarea during editing.'),
tom@5 82 '#default_value' => $field['mee_scald_editor_context'],
tom@5 83 '#options' => $context_options,
tom@5 84 );
eads@0 85 return $form;
eads@0 86
eads@0 87 case 'save':
tom@4 88 return array('mee_processing', 'mee_dnd_callback_url', 'mee_scald_editor_context');
eads@0 89
eads@0 90 case 'database columns':
eads@0 91 $columns['value'] = array('type' => 'text', 'size' => 'big', 'not null' => FALSE, 'sortable' => TRUE);
eads@0 92 $columns['dnd_callback_url'] = array('type' => 'text', 'size' => 'small', 'not null' => FALSE);
eads@0 93 if (!empty($field['mee_processing'])) {
eads@0 94 $columns['format'] = array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE);
eads@0 95 }
tom@5 96 $columns['mee_scald_editor_context'] = array('type' => 'text', 'size' => 'small', 'not null' => FALSE);
eads@0 97 return $columns;
eads@0 98
eads@0 99 case 'views data':
eads@0 100 return content_views_field_views_data($field);
eads@0 101 }
eads@0 102 }
eads@0 103
eads@0 104 /**
eads@0 105 * Implementation of hook_field().
eads@0 106 */
eads@0 107 function mee_field($op, &$node, $field, &$items, $teaser, $page) {
eads@0 108 switch ($op) {
tom@2 109 case 'presave':
tom@5 110 foreach ($items as $delta => &$item) {
tom@5 111 if (!empty($item['value'])) {
tom@5 112 $item['value'] = scald_rendered_to_sas($item['value']);
tom@5 113 }
tom@5 114 }
tom@2 115 break; // end 'submit'
tom@2 116
tom@2 117 case 'insert':
tom@2 118 foreach ($items as $delta => $item) {
tom@5 119 $scald_included = scald_included($item['value']);
tom@3 120
tom@3 121 $temp_atom = new stdClass;
tom@3 122 $temp_atom->type = 'composite';
tom@3 123 $temp_atom->provider = 'mee';
tom@3 124 $temp_atom->base_id = $node->nid . ':' . $delta;
tom@3 125 $temp_atom->publisher = $node->uid;
tom@5 126 $temp_atom->title = $node->title . ' - ' . $field['widget']['label'] . ' (#' . $delta . ')';
tom@3 127 $temp_atom->authors = array(scald_uid_to_aid($node->uid));
tom@2 128 $temp_atom->relationships = empty($scald_included) ? array() : array('includes' => $scald_included);
tom@3 129
tom@2 130 $sid = scald_register_atom($temp_atom);
tom@2 131 }
tom@2 132 break; // end 'insert'
tom@2 133
tom@2 134 case 'update':
tom@2 135 foreach ($items as $delta => $item) {
tom@5 136 $scald_included = scald_included($item['value']);
tom@2 137
tom@3 138 // @@@TODO: Handle failure of fetch
tom@3 139 $atom = scald_fetch(scald_search(array('base_id' => $node->nid . ':' . $delta), FALSE, TRUE));
tom@3 140 $atom->publisher = $node->uid;
franck@6 141 $atom->title = $node->title;
tom@5 142 $atom->authors = array(scald_uid_to_aid($node->uid)); // @@@TODO: This will completely override any authors listed & replace only with the Publisher.
franck@6 143 $atom->relationships = empty($scald_included) ? array() : array('includes' => $scald_included);
tom@3 144
tom@3 145 scald_update_atom($atom);
tom@2 146 }
tom@2 147 break; // end 'update'
tom@2 148
tom@2 149 case 'delete':
tom@3 150 foreach ($items as $delta => $item) {
tom@3 151 scald_unregister_atom(scald_search(array('base_id' => $node->nid . ':' . $delta), FALSE, TRUE));
tom@3 152 }
tom@2 153 break; // end 'delete'
tom@2 154
eads@0 155 case 'sanitize':
eads@0 156 foreach ($items as $delta => $item) {
eads@0 157 if (!empty($field['mee_processing'])) {
eads@0 158 $check = is_null($node) || (isset($node->build_mode) && $node->build_mode == NODE_BUILD_PREVIEW);
eads@0 159 $text = isset($item['value']) ? check_markup($item['value'], $item['format'], $check) : '';
eads@0 160 }
eads@0 161 else {
eads@0 162 $text = check_plain($item['value']);
eads@0 163 }
eads@0 164 $items[$delta]['safe'] = $text;
eads@0 165 }
tom@2 166 break; // end 'sanitize'
eads@0 167 }
eads@0 168 }
eads@0 169
eads@0 170 /**
eads@0 171 * Implementation of hook_content_is_empty().
eads@0 172 */
eads@0 173 function mee_content_is_empty($item, $field) {
eads@0 174 if (empty($item['value']) && (string)$item['value'] !== '0') {
eads@0 175 return TRUE;
eads@0 176 }
eads@0 177 return FALSE;
eads@0 178 }
eads@0 179
eads@0 180 /**
eads@0 181 * Implementation of hook_field_formatter_info().
eads@0 182 */
eads@0 183 function mee_field_formatter_info() {
eads@0 184 $formatters = array(
eads@0 185 'default' => array(
eads@0 186 'label' => t('Filtered text'),
eads@0 187 'field types' => array('multimedia_editorial_element'),
eads@0 188 'multiple values' => CONTENT_HANDLE_CORE,
eads@0 189 ),
eads@0 190 'plain' => array(
eads@0 191 'label' => t('Plain text'),
eads@0 192 'field types' => array('multimedia_editorial_element'),
eads@0 193 'multiple values' => CONTENT_HANDLE_CORE,
eads@0 194 ),
eads@0 195 );
eads@0 196 //@TODO generate context processor based field formatters
eads@0 197 //foreach (scald_contexts() as $context) {
eads@0 198 // $formatters[$context] = array(
eads@0 199 // 'label' => t('Scald context processor: @context', array('@context' => $context),
eads@0 200 // 'field types' => 'mee',
eads@0 201 // );
eads@0 202 //}
eads@0 203 return $formatters;
eads@0 204 }
eads@0 205
eads@0 206 function theme_mee_formatter_default($element) {
tom@4 207 return scald_sas_to_rendered($element['#item']['safe']);
eads@0 208 }
eads@0 209
eads@0 210 /**
eads@0 211 * Theme function for 'plain' text field formatter.
eads@0 212 */
eads@0 213 function theme_mee_formatter_plain($element) {
tom@4 214 return strip_tags(scald_sas_to_rendered($element['#item']['safe'], 'title', TRUE));
eads@0 215 }
eads@0 216
tom@4 217 //function theme_mee_context_formatter($element) {
tom@4 218 // return 'foo';
tom@4 219 //}
eads@0 220
eads@0 221 /**
eads@0 222 * Implementation of hook_widget_info().
eads@0 223 */
eads@0 224 function mee_widget_info() {
eads@0 225 return array(
eads@0 226 'mee_textarea' => array(
eads@0 227 'label' => t('MEE Textarea'),
eads@0 228 'field types' => array('multimedia_editorial_element'),
eads@0 229 'multiple values' => CONTENT_HANDLE_CORE,
eads@0 230 ),
eads@0 231 );
eads@0 232 }
eads@0 233
eads@0 234 /**
eads@0 235 * Implementation of FAPI hook_elements().
eads@0 236 *
eads@0 237 * Any FAPI callbacks needed for individual widgets can be declared here,
eads@0 238 * and the element will be passed to those callbacks for processing.
eads@0 239 *
eads@0 240 * Drupal will automatically theme the element using a theme with
eads@0 241 * the same name as the hook_elements key.
eads@0 242 */
eads@0 243 function mee_elements() {
eads@0 244 return array(
eads@0 245 'mee_textarea' => array(
eads@0 246 '#input' => TRUE,
eads@0 247 '#columns' => array('value', 'format'), '#delta' => 0,
eads@0 248 '#process' => array('mee_textarea_process', 'dnd_process_textarea'),
eads@0 249 '#filter_value' => FILTER_FORMAT_DEFAULT,
eads@0 250 ),
eads@0 251 );
eads@0 252 }
eads@0 253
eads@0 254 /**
eads@0 255 * Implementation of hook_widget_settings().
eads@0 256 */
eads@0 257 function mee_widget_settings($op, $widget) {
eads@0 258 switch ($op) {
eads@0 259 case 'form':
eads@0 260 $form = array();
eads@0 261 $rows = (isset($widget['rows']) && is_numeric($widget['rows'])) ? $widget['rows'] : 5;
eads@0 262 $size = (isset($widget['size']) && is_numeric($widget['size'])) ? $widget['size'] : 60;
eads@0 263 $form['rows'] = array(
eads@0 264 '#type' => 'textfield',
eads@0 265 '#title' => t('Rows'),
eads@0 266 '#default_value' => $rows,
eads@0 267 '#element_validate' => array('_mee_widget_settings_row_validate'),
eads@0 268 '#required' => TRUE,
eads@0 269 );
eads@0 270 $form['size'] = array('#type' => 'hidden', '#value' => $size);
eads@0 271 return $form;
eads@0 272
eads@0 273 case 'save':
eads@0 274 return array('rows', 'size');
eads@0 275 }
eads@0 276 }
eads@0 277
eads@0 278 function _mee_widget_settings_row_validate($element, &$form_state) {
eads@0 279 $value = $form_state['values']['rows'];
eads@0 280 if (!is_numeric($value) || intval($value) != $value || $value <= 0) {
eads@0 281 form_error($element, t('"Rows" must be a positive integer.'));
eads@0 282 }
eads@0 283 }
eads@0 284
eads@0 285 function _mee_widget_settings_size_validate($element, &$form_state) {
eads@0 286 $value = $form_state['values']['size'];
eads@0 287 if (!is_numeric($value) || intval($value) != $value || $value <= 0) {
eads@0 288 form_error($element, t('"Size" must be a positive integer.'));
eads@0 289 }
eads@0 290 }
eads@0 291
eads@0 292 /**
eads@0 293 * Implementation of hook_widget().
eads@0 294 *
eads@0 295 * Attach a single form element to the form. It will be built out and
eads@0 296 * validated in the callback(s) listed in hook_elements. We build it
eads@0 297 * out in the callbacks rather than here in hook_widget so it can be
eads@0 298 * plugged into any module that can provide it with valid
eads@0 299 * $field information.
eads@0 300 *
eads@0 301 * Content module will set the weight, field name and delta values
eads@0 302 * for each form element. This is a change from earlier CCK versions
eads@0 303 * where the widget managed its own multiple values.
eads@0 304 *
eads@0 305 * If there are multiple values for this field, the content module will
eads@0 306 * call this function as many times as needed.
eads@0 307 *
eads@0 308 * @param $form
eads@0 309 * the entire form array, $form['#node'] holds node information
eads@0 310 * @param $form_state
eads@0 311 * the form_state, $form_state['values'][$field['field_name']]
eads@0 312 * holds the field's form values.
eads@0 313 * @param $field
eads@0 314 * the field array
eads@0 315 * @param $items
eads@0 316 * array of default values for this field
eads@0 317 * @param $delta
eads@0 318 * the order of this item in the array of subelements (0, 1, 2, etc)
eads@0 319 *
eads@0 320 * @return
eads@0 321 * the form item for a single element for this field
eads@0 322 */
eads@0 323 function mee_widget(&$form, &$form_state, $field, $items, $delta = 0) {
tom@5 324 if (isset($items[$delta]['value'])) {
tom@5 325 $items[$delta]['value'] = scald_sas_to_rendered($items[$delta]['value'], $field['mee_scald_editor_context'], TRUE);
tom@5 326 }
eads@0 327 $element = array(
eads@0 328 '#type' => $field['widget']['type'],
tom@5 329 '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
eads@0 330 );
tom@5 331
eads@0 332 return $element;
eads@0 333 }
eads@0 334
eads@0 335 /**
eads@0 336 * Process an individual element.
eads@0 337 *
eads@0 338 * Build the form element. When creating a form using FAPI #process,
eads@0 339 * note that $element['#value'] is already set.
eads@0 340 *
eads@0 341 * The $fields array is in $form['#field_info'][$element['#field_name']].
eads@0 342 */
eads@0 343 function mee_textarea_process($element, $edit, $form_state, $form) {
eads@0 344 drupal_add_css(drupal_get_path('module', 'mee') .'/css/mee.css');
franck@8 345 drupal_add_js(drupal_get_path('module', 'mee') .'/mee.js');
eads@0 346
franck@11 347 $element['mee'] = array(
franck@11 348 '#type' => 'markup',
franck@11 349 '#prefix' => '<div class="mee-wrap-editor-library">',
franck@11 350 '#suffix' => '</div>',
franck@11 351 );
franck@11 352
eads@0 353 $field = $form['#field_info'][$element['#field_name']];
eads@0 354 $field_key = $element['#columns'][0];
franck@11 355 $element['mee']['ressource_manager'] = array(
franck@11 356 '#type' => 'markup',
franck@11 357 '#weight' => 0.5,
franck@11 358 '#theme' => 'mee_ressource_manager'
franck@11 359 );
franck@11 360 $element['mee']['ressource_manager'][0] = array(
franck@11 361 'title' => array(
franck@11 362 '#type' => 'markup',
franck@11 363 '#value' => 'Lorem ipsum'
franck@11 364 ),
franck@11 365 'weight' => array(
franck@11 366 '#type' => 'weight',
franck@11 367 )
franck@11 368 );
franck@11 369 $element['mee'][$field_key] = array(
eads@0 370 '#type' => 'textarea',
eads@0 371 '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
eads@0 372 '#rows' => !empty($field['widget']['rows']) ? $field['widget']['rows'] : 10,
eads@0 373 '#weight' => 0,
eads@0 374 // The following values were set by the content module and need
eads@0 375 // to be passed down to the nested element.
eads@0 376 '#title' => $element['#title'],
eads@0 377 '#description' => $element['#description'],
eads@0 378 '#required' => $element['#required'],
eads@0 379 '#field_name' => $element['#field_name'],
eads@0 380 '#type_name' => $element['#type_name'],
eads@0 381 '#delta' => $element['#delta'],
eads@0 382 '#columns' => $element['#columns'],
franck@11 383 # '#prefix' => '<div class="mee-wrap-editor-library">',
franck@11 384 # '#suffix' => '</div>',
eads@0 385 '#dnd-enabled' => TRUE,
eads@0 386 '#dnd-settings' => array(
eads@0 387 'drop_selector' => '#'. $element['#id'] .' .drop',
eads@0 388 'url' => $field['mee_dnd_callback_url'],
eads@0 389 ),
eads@0 390 );
eads@0 391
eads@0 392 if (!empty($field['mee_processing'])) {
eads@0 393 $filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
eads@0 394 $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
eads@0 395 $parents = array_merge($element['#parents'] , array($filter_key));
franck@11 396 $element['mee'][$filter_key] = filter_form($format, 1, $parents);
franck@11 397 $element['mee'][$filter_key]['#prefix'] = '<div class="mee-filter-form">';
franck@11 398 $element['mee'][$filter_key]['#suffix'] = '</div>';
eads@0 399 }
eads@0 400
eads@0 401 // Used so that hook_field('validate') knows where to flag an error.
eads@0 402 $element['_error_element'] = array(
eads@0 403 '#type' => 'value',
eads@0 404 '#value' => implode('][', array_merge($element['#parents'], array($field_key))),
eads@0 405 );
franck@11 406 $element['mee']['short'] = array(
franck@11 407 '#type' => 'textarea',
franck@11 408 '#title' => 'Contenu court',
franck@11 409 '#rows' => 5,
franck@11 410 '#weight' => 100
franck@11 411 );
eads@0 412
eads@0 413
eads@0 414 return $element;
eads@0 415 }
eads@0 416
eads@0 417 /**
eads@0 418 * FAPI theme for an individual text elements.
eads@0 419 *
eads@0 420 * The textfield or textarea is already rendered by the
eads@0 421 * textfield or textarea themes and the html output
eads@0 422 * lives in $element['#children']. Override this theme to
eads@0 423 * make custom changes to the output.
eads@0 424 *
eads@0 425 * $element['#field_name'] contains the field name
eads@0 426 * $element['#delta] is the position of this element in the group
eads@0 427 */
eads@0 428 function theme_mee_textarea($element) {
eads@0 429 return $element['#children'];
eads@0 430 }
tom@2 431
franck@11 432 function theme_mee_ressource_manager(&$form) {
franck@11 433 static $count = 0;
franck@11 434 $id = 'mee-ressource-manager-'. $count;
franck@11 435 drupal_add_tabledrag($id, 'order', 'sibling', 'mee-rm-weight');
franck@11 436 $count++;
franck@11 437 $header = array('', t('Title'), t('Weight'));
franck@11 438 $rows = array();
franck@11 439 foreach(element_children($form) as $key) {
franck@11 440 $form[$key]['weight']['#attributes']['class'] = 'mee-rm-weight';
franck@11 441
franck@11 442 $row = array('');
franck@11 443 $row[] = drupal_render($form[$key]['title']);
franck@11 444 $row[] = drupal_render($form[$key]['weight']);
franck@11 445 $rows[] = array('data' => $row, 'class' => 'draggable');
franck@11 446 }
franck@11 447 $output = theme('table', $header, $rows, array(
franck@11 448 'id' => $id,
franck@11 449 'class' => 'mee-ressource-manager'
franck@12 450 ),
franck@12 451 t('Ressource Manager')
franck@12 452 );
franck@11 453 $output .= drupal_render($form);
franck@11 454 return $output;
franck@11 455 }
tom@2 456
tom@2 457
tom@2 458
tom@2 459
tom@2 460
tom@2 461 /*******************************************************************************
tom@2 462 * SCALD HOOK IMPLEMENTATIONS
tom@2 463 ******************************************************************************/
tom@2 464
tom@2 465 /**
tom@2 466 * Implementation of hook_scald_provider().
tom@2 467 */
tom@2 468 function mee_scald_provider() {
tom@2 469 return array(
tom@2 470 'atoms' => array(
tom@5 471 'composite' => array(
tom@5 472 t('The MEE CCK field.'),
tom@5 473 ),
tom@2 474 ),
tom@2 475 );
tom@2 476 }
tom@2 477
tom@2 478
tom@2 479
tom@2 480 /**
tom@2 481 * Implementation of hook_scald_register_atom().
tom@2 482 */
tom@2 483 function mee_scald_register_atom($atom, $mode) {
tom@2 484
tom@2 485 } // end mee_scald_register_atom()
tom@2 486
tom@2 487
tom@2 488
tom@2 489 /**
tom@2 490 * Implementation of hook_scald_update_atom().
tom@2 491 */
tom@2 492 function mee_scald_update_atom($atom, $mode) {
tom@2 493
tom@2 494 } // end mee_scald_update_atom()
tom@2 495
tom@2 496
tom@2 497
tom@2 498 /**
tom@2 499 * Implementation of hook_scald_unregister_atom().
tom@2 500 */
tom@2 501 function mee_scald_unregister_atom($atom, $mode) {
tom@2 502
tom@2 503 } // end mee_scald_unregister_atom()
tom@2 504
tom@2 505
tom@2 506
tom@2 507 /**
tom@2 508 * Implementation of hook_scald_fetch().
tom@2 509 */
tom@2 510 function mee_scald_fetch(&$atom) {
tom@2 511 $atom->thumbnail_source = drupal_get_path('module', 'scald_composites') . '/assets/thumbnail_composite.png';
tom@2 512 } // end mee_scald_fetch()
tom@2 513
tom@2 514
tom@2 515
tom@2 516 /**
tom@2 517 * Implementation of hook_scald_prerender().
tom@2 518 */
tom@2 519 function mee_scald_prerender(&$atom, $mode) {
tom@2 520
tom@2 521 } // end mee_scald_prerender()
tom@2 522