comparison mee.module @ 14:50a57b1517cb

Fully working MEE, with Ressource Manager & all
author Franck Deroche <franck@defr.org>
date Fri, 18 Sep 2009 15:18:53 +0200
parents 842d89897cb4
children a3d780c2979a
comparison
equal deleted inserted replaced
13:842d89897cb4 14:50a57b1517cb
22 'arguments' => array('element' => NULL), 22 'arguments' => array('element' => NULL),
23 ), 23 ),
24 ); 24 );
25 25
26 $scald_config = variable_get('scald_config', 0); 26 $scald_config = variable_get('scald_config', 0);
27 foreach ($scald_config->contexts as $context => $details) { 27 if(!empty($scald_config->contexts)) {
28 $theme['mee_formatter_'. $context] = array( 28 foreach ($scald_config->contexts as $context => $details) {
29 'arguments' => array('element' => NULL), 29 $theme['mee_formatter_'. $context] = array(
30 'function' => 'theme_mee_context_formatter', 30 'arguments' => array('element' => NULL),
31 ); 31 'function' => 'theme_mee_context_formatter',
32 );
33 }
32 } 34 }
33 return $theme; 35 return $theme;
34 } 36 }
35 37
36 /** 38 /**
107 */ 109 */
108 function mee_field($op, &$node, $field, &$items, $teaser, $page) { 110 function mee_field($op, &$node, $field, &$items, $teaser, $page) {
109 switch ($op) { 111 switch ($op) {
110 case 'presave': 112 case 'presave':
111 foreach ($items as $delta => &$item) { 113 foreach ($items as $delta => &$item) {
112 if (!empty($item['mee']['value'])) { 114 // Put everything in the ['mee'] namespace back into the array.
113 $item['mee']['value'] = scald_rendered_to_sas($item['mee']['value']); 115 // This let CCK store the value and short fields natively.
116 if (is_array($item['mee'])) {
117 foreach ($item['mee'] as $k => $v) {
118 $items[$delta][$k] = $v;
119 }
120 }
121 if (!empty($item['value']) && variable_get('mee_store_sas', FALSE)) {
122 $item['value'] = scald_rendered_to_sas($item['value']);
114 } 123 }
115 } 124 }
116 break; // end 'submit' 125 break; // end 'submit'
117 126
118 case 'insert': 127 case 'insert':
119 foreach ($items as $delta => $item) { 128 foreach ($items as $delta => $item) {
120 // Let CCK store the value and short fields
121 $items[$delta]['value'] = $item['mee']['value'];
122 $items[$delta]['short'] = $item['mee']['short'];
123 // Process the value and generate an atom 129 // Process the value and generate an atom
124 $scald_included = scald_included($item['mee']['value']); 130 $sas = scald_rendered_to_sas($item['value']);
131 $scald_included = scald_included($sas);
132 $sids = array_unique($scald_included);
125 133
126 $temp_atom = new stdClass; 134 $temp_atom = new stdClass;
127 $temp_atom->type = 'composite'; 135 $temp_atom->type = 'composite';
128 $temp_atom->provider = 'mee'; 136 $temp_atom->provider = 'mee';
129 $temp_atom->base_id = $node->nid . ':' . $delta; 137 $temp_atom->base_id = $node->nid . ':' . $delta;
130 $temp_atom->publisher = $node->uid; 138 $temp_atom->publisher = $node->uid;
131 $temp_atom->title = $node->title . ' - ' . $field['widget']['label'] . ' (#' . $delta . ')'; 139 $temp_atom->title = $node->title . ' - ' . $field['widget']['label'] . ' (#' . $delta . ')';
132 $temp_atom->authors = array(scald_uid_to_aid($node->uid)); 140 $temp_atom->authors = array(scald_uid_to_aid($node->uid));
133 $temp_atom->relationships = empty($scald_included) ? array() : array('includes' => $scald_included); 141 $temp_atom->relationships = empty($scald_included) ? array() : array('includes' => $scald_included);
134 142
135 $sid = scald_register_atom($temp_atom); 143 $sid = scald_register_atom($temp_atom);
136 // Short content 144
137 $short = $item['mee']['short'];
138 // TODO: Store the short content value
139 // Ressource manager associations 145 // Ressource manager associations
140 foreach ($item['mee']['ressource_manager'] as $sid => $ressource) { 146 if (empty($item['ressource_manager'])) {
141 // TODO: Store the assocation 147 _mee_load_ressources($node, $field, $item);
148 }
149 $separator = $item['ressource_manager'][0]['weight'];
150 foreach ($sids as $sid) {
151 $ressource = $item['ressource_manager'][$sid];
152 $weight = $ressource['weight'] - $separator;
153 $required = $ressource['required'];
154 $query = "INSERT into {mee_ressources} (content_nid, atom_sid, field, weight, required) VALUES (%d, %d, '%s', %d, %d)";
155 db_query($query, $node->nid, $sid, $field['field_name'], $weight, $required);
142 } 156 }
143 } 157 }
144 break; // end 'insert' 158 break; // end 'insert'
145 159
146 case 'update': 160 case 'update':
147 foreach ($items as $delta => $item) { 161 foreach ($items as $delta => $item) {
148 // Let CCK store the value and short fields
149 $items[$delta]['value'] = $item['mee']['value'];
150 $items[$delta]['short'] = $item['mee']['short'];
151 // Process the value 162 // Process the value
152 $scald_included = scald_included($item['mee']['value']); 163 $sas = scald_rendered_to_sas($item['value']);
164 $scald_included = scald_included($sas);
165 $sids = array_unique($scald_included);
166
167 // Update ressources weight
168 // In fact, we'll delete all the associations and recreate afterwards
169 // the needed one, to be sure that new ressources are correctly
170 // registered, and that no longer used one are removed.
171 if (!is_array($item['ressource_manager'])) {
172 _mee_load_ressources($node, $field, $item);
173 }
174 db_query("DELETE FROM {mee_ressources} WHERE content_nid=%d AND field='%s'", $node->nid, $field['field_name']);
175 // We'll normalize the weight, putting our separator at 0.
176 $separator = $item['ressource_manager'][0]['weight'];
177 foreach ($sids as $sid) {
178 $ressource = $item['ressource_manager'][$sid];
179 $required = $ressource['required'];
180 $weight = $ressource['weight'] - $separator;
181 // insert in the table
182 $query = "INSERT into {mee_ressources} (content_nid, atom_sid, field, weight, required) VALUES (%d, %d, '%s', %d, %d)";
183 db_query($query, $node->nid, $sid, $field['field_name'], $weight, $required);
184 }
153 185
154 // @@@TODO: Handle failure of fetch 186 // @@@TODO: Handle failure of fetch
155 $atom = scald_fetch(scald_search(array('base_id' => $node->nid . ':' . $delta), FALSE, TRUE)); 187 $atom = scald_fetch(scald_search(array('base_id' => $node->nid . ':' . $delta), FALSE, TRUE));
156 $atom->publisher = $node->uid; 188 $atom->publisher = $node->uid;
157 $atom->title = $node->title; 189 $atom->title = $node->title;
164 196
165 case 'delete': 197 case 'delete':
166 foreach ($items as $delta => $item) { 198 foreach ($items as $delta => $item) {
167 scald_unregister_atom(scald_search(array('base_id' => $node->nid . ':' . $delta), FALSE, TRUE)); 199 scald_unregister_atom(scald_search(array('base_id' => $node->nid . ':' . $delta), FALSE, TRUE));
168 } 200 }
201
202 // Delete all ressources associations for this field
203 $query = "DELETE FROM {mee_ressources} WHERE content_nid = %d AND field = '%s'";
204 db_query($query, $node->nid, $field['field_name']);
169 break; // end 'delete' 205 break; // end 'delete'
170 206
171 case 'sanitize': 207 case 'sanitize':
172 foreach ($items as $delta => $item) { 208 foreach ($items as $delta => $item) {
173 if (!empty($field['mee_processing'])) { 209 if (!empty($field['mee_processing'])) {
174 $check = is_null($node) || (isset($node->build_mode) && $node->build_mode == NODE_BUILD_PREVIEW); 210 $check = is_null($node) || (isset($node->build_mode) && $node->build_mode == NODE_BUILD_PREVIEW);
175 $text = isset($item['mee']['value']) ? check_markup($item['mee']['value'], $item['format'], $check) : ''; 211 $text = isset($item['value']) ? check_markup($item['value'], $item['format'], $check) : '';
176 } 212 }
177 else { 213 else {
178 $text = check_plain($item['mee']['value']); 214 $text = check_plain($item['value']);
179 } 215 }
180 $items[$delta]['safe'] = $text; 216 $items[$delta]['safe'] = $text;
181 } 217 }
182 break; // end 'sanitize' 218 break; // end 'sanitize'
183 } 219 }
185 221
186 /** 222 /**
187 * Implementation of hook_content_is_empty(). 223 * Implementation of hook_content_is_empty().
188 */ 224 */
189 function mee_content_is_empty($item, $field) { 225 function mee_content_is_empty($item, $field) {
190 if (empty($item['mee']['value']) && (string)$item['mee']['value'] !== '0') { 226 if (empty($item['value']) && (string)$item['value'] !== '0') {
191 return TRUE; 227 return TRUE;
192 } 228 }
193 return FALSE; 229 return FALSE;
194 } 230 }
195 231
218 //} 254 //}
219 return $formatters; 255 return $formatters;
220 } 256 }
221 257
222 function theme_mee_formatter_default($element) { 258 function theme_mee_formatter_default($element) {
259 // What's stored is exactly what the user entered, and not the SAS
260 // representation. In general, that's that we want to output, *but*
261 // we should also check that the atom is still available... And replace
262 // it if it isn't.
263 if (!variable_get('mee_store_sas', FALSE)) {
264 $sas = scald_rendered_to_sas($element['#item']['value']);
265 $included = scald_included($sas);
266 $altered = FALSE;
267 foreach($included as $sid) {
268 $atom = scald_fetch($sid);
269 if (!scald_action_permitted($atom, 'view')) {
270 $altered = TRUE;
271 $replace = scald_scald_render($atom, 'no-access');
272 $element['#item']['value'] = preg_replace(
273 "/<!--(\s*)scald=$sid(.*)END scald=$sid(\s*)-->/sU",
274 scald_scald_render($atom, 'no-access'),
275 $element['#item']['value']
276 );
277 }
278 }
279 if ($altered) {
280 $element['#item']['safe'] = check_markup($element['#item']['value']);
281 }
282 }
223 return scald_sas_to_rendered($element['#item']['safe']); 283 return scald_sas_to_rendered($element['#item']['safe']);
224 } 284 }
225 285
226 /** 286 /**
227 * Theme function for 'plain' text field formatter. 287 * Theme function for 'plain' text field formatter.
357 * The $fields array is in $form['#field_info'][$element['#field_name']]. 417 * The $fields array is in $form['#field_info'][$element['#field_name']].
358 */ 418 */
359 function mee_textarea_process($element, $edit, $form_state, $form) { 419 function mee_textarea_process($element, $edit, $form_state, $form) {
360 drupal_add_css(drupal_get_path('module', 'mee') .'/css/mee.css'); 420 drupal_add_css(drupal_get_path('module', 'mee') .'/css/mee.css');
361 drupal_add_js(drupal_get_path('module', 'mee') .'/mee.js'); 421 drupal_add_js(drupal_get_path('module', 'mee') .'/mee.js');
362
363 $element['mee'] = array( 422 $element['mee'] = array(
364 '#type' => 'markup', 423 '#type' => 'markup',
365 '#prefix' => '<div class="mee-wrap-editor-library">', 424 '#prefix' => '<div class="mee-wrap-editor-library">',
366 '#suffix' => '</div>', 425 '#suffix' => '</div>',
367 ); 426 );
368 427
369 $field = $form['#field_info'][$element['#field_name']]; 428 $field = $form['#field_info'][$element['#field_name']];
370 $field_key = $element['#columns'][0]; 429 $field_key = $element['#columns'][0];
371 $element['mee']['ressource_manager'] = array( 430 $element['mee']['ressource_manager'] = array(
372 '#type' => 'markup', 431 '#type' => 'markup',
373 '#weight' => 0.5, 432 '#weight' => 0.5,
374 '#theme' => 'mee_ressource_manager' 433 '#theme' => 'mee_ressource_manager'
375 ); 434 );
435
436
437 if (!isset($element['#value']['mee']['ressource_manager'])) {
438 $element['#value']['mee']['ressource_manager'] = array();
439 // Restore/Generate the associated ressources in a proper order
440 $query = "SELECT * FROM {mee_ressources} WHERE content_nid=%d AND field='%s' ORDER BY weight ASC";
441 $result = db_query($query, $form['nid']['#value'], $element['#field_name']);
442 while ($item = db_fetch_object($result)) {
443 $element['#value']['mee']['ressource_manager'][$item->atom_sid] = (array)$item;
444 }
445 $element['#value']['mee']['ressource_manager'][0] = array('weight' => 0);
446 }
447
448 foreach($element['#value']['mee']['ressource_manager'] as $sid => $item) {
449 $atom = scald_fetch($sid);
450 $title = $atom->title;
451
452 $element['mee']['ressource_manager'][$sid] = array(
453 'title' => array(
454 '#type' => 'markup',
455 '#value' => $title,
456 ),
457 'required' => array(
458 '#type' => 'select',
459 '#options' => array(t('Optional'), t('Required')),
460 '#default_value' => $item['required']
461 ),
462 'weight' => array(
463 '#type' => 'weight',
464 '#default_value' => $item['weight'],
465 ),
466 '#weight' => $item['weight']
467 );
468 }
469
470 // And now we add the separator
376 $element['mee']['ressource_manager'][0] = array( 471 $element['mee']['ressource_manager'][0] = array(
377 'title' => array( 472 'title' => array(
378 '#type' => 'markup', 473 '#type' => 'markup',
379 '#value' => 'Lorem ipsum' 474 '#value' => t('< Primaire / Secondaire >'),
475 ),
476 'required' => array(
477 '#type' => 'markup',
478 '#value' => '-'
380 ), 479 ),
381 'weight' => array( 480 'weight' => array(
382 '#type' => 'weight', 481 '#type' => 'weight',
383 ) 482 '#prefix' => '<div class="mee-rm-separator">',
384 ); 483 '#suffix' => '</div>'
484 ),
485 '#weight' => $element['#value']['mee']['ressource_manager'][0]['weight']
486 );
487 if ($element['#value'][$field_key]) {
488 $element['#value']['mee'][$field_key] = $element['#value'][$field_key];
489 }
385 $element['mee'][$field_key] = array( 490 $element['mee'][$field_key] = array(
386 '#type' => 'textarea', 491 '#type' => 'textarea',
387 '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL, 492 '#default_value' => isset($element['#value']['mee'][$field_key]) ? $element['#value']['mee'][$field_key] : NULL,
388 '#rows' => !empty($field['widget']['rows']) ? $field['widget']['rows'] : 10, 493 '#rows' => !empty($field['widget']['rows']) ? $field['widget']['rows'] : 10,
389 '#weight' => 0, 494 '#weight' => 0,
390 // The following values were set by the content module and need 495 // The following values were set by the content module and need
391 // to be passed down to the nested element. 496 // to be passed down to the nested element.
392 '#title' => $element['#title'], 497 '#title' => $element['#title'],
447 function theme_mee_ressource_manager(&$form) { 552 function theme_mee_ressource_manager(&$form) {
448 static $count = 0; 553 static $count = 0;
449 $id = 'mee-ressource-manager-'. $count; 554 $id = 'mee-ressource-manager-'. $count;
450 drupal_add_tabledrag($id, 'order', 'sibling', 'mee-rm-weight'); 555 drupal_add_tabledrag($id, 'order', 'sibling', 'mee-rm-weight');
451 $count++; 556 $count++;
452 $header = array('', t('Title'), t('Weight')); 557 $header = array('', t('Title'), t('Required'), t('Weight'));
453 $rows = array(); 558 $rows = array();
454 foreach(element_children($form) as $key) { 559 foreach(element_children($form) as $key) {
455 $form[$key]['weight']['#attributes']['class'] = 'mee-rm-weight'; 560 $form[$key]['weight']['#attributes']['class'] = 'mee-rm-weight';
456 561
457 $row = array(''); 562 $row = array('');
458 $row[] = drupal_render($form[$key]['title']); 563 $row[] = drupal_render($form[$key]['title']);
564 $row[] = drupal_render($form[$key]['required']);
459 $row[] = drupal_render($form[$key]['weight']); 565 $row[] = drupal_render($form[$key]['weight']);
460 $rows[] = array('data' => $row, 'class' => 'draggable'); 566 $rows[] = array('data' => $row, 'class' => 'draggable');
461 } 567 }
462 $output = theme('table', $header, $rows, array( 568 $output = theme('table', $header, $rows, array(
463 'id' => $id, 569 'id' => $id,
467 ); 573 );
468 $output .= drupal_render($form); 574 $output .= drupal_render($form);
469 return $output; 575 return $output;
470 } 576 }
471 577
472 578 function _mee_load_ressources($node, $field, &$item) {
473 579 $results = db_query("
580 SELECT atom_sid, weight
581 FROM {mee_ressources}
582 WHERE content_nid=%d AND field='%s'",
583 array(
584 ':nid' => $node->nid,
585 ':field' => $field['field_name']
586 )
587 );
588 $item['ressource_manager'] = array();
589 while($r = db_fetch_object($results)) {
590 $item['ressource_manager'][$r->atom_sid] = array('weight' => $r->weight);
591 }
592 $item['ressource_manager'][0] = array('weight' => 0);
593 }
474 594
475 595
476 /******************************************************************************* 596 /*******************************************************************************
477 * SCALD HOOK IMPLEMENTATIONS 597 * SCALD HOOK IMPLEMENTATIONS
478 ******************************************************************************/ 598 ******************************************************************************/