You are here

function mee_field in Scald: Media Management made easy 6

Implementation of hook_field().

File

mee/mee.module, line 112
Defines a special textarea, with drag and drop media driven by Scald and dnd.module when rich text editing is enabled on the textarea via the WYSIWYG API.

Code

function mee_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'presave':
      foreach ($items as $delta => &$item) {

        // Put everything in the ['mee'] namespace back into the array.
        // This let CCK store the value and short fields natively.
        if (is_array($item['mee'])) {
          foreach ($item['mee'] as $k => $v) {
            $items[$delta][$k] = $v;
          }
        }
        if (!empty($item['value']) && variable_get('mee_store_sas', TRUE)) {
          $item['value'] = scald_rendered_to_sas($item['value']);
        }
      }
      break;

    // end 'submit'
    case 'insert':
      foreach ($items as $delta => $item) {

        // Process the value and generate an atom
        $sas = scald_rendered_to_sas($item['value']);
        $scald_included = scald_included($sas);
        $sids = array_unique($scald_included);

        // Parse copyright informations
        $copyrights = mee_extract_copyrights($item['value']);
        if (variable_get('mee_register_composites', FALSE)) {
          $temp_atom = new stdClass();
          $temp_atom->type = 'composite';
          $temp_atom->provider = 'mee';
          $temp_atom->base_id = $node->nid . ':' . $delta;
          $temp_atom->publisher = $node->uid;
          $temp_atom->title = $node->title . ' - ' . $field['widget']['label'] . ' (#' . $delta . ')';
          $temp_atom->authors = array(
            scald_uid_to_aid($node->uid),
          );
          $temp_atom->relationships = empty($scald_included) ? array() : array(
            'includes' => $scald_included,
          );
          scald_register_atom($temp_atom);
        }

        // Ressource manager associations
        if (empty($item['ressource_manager'])) {
          _mee_load_ressources($node, $field, $item);
        }
        $separator = $item['ressource_manager'][0]['weight'];
        foreach ($sids as $sid) {
          $ressource = $item['ressource_manager'][$sid];
          $weight = $ressource['weight'] - $separator;
          $required = $ressource['required'];
          $query = "INSERT into {mee_ressources} (content_nid, atom_sid, field, weight, required, copyright) VALUES (%d, %d, '%s', %d, %d, '%s')";
          db_query($query, $node->nid, $sid, $field['field_name'], $weight, $required, $copyrights[$sid]);
        }
      }
      break;

    // end 'insert'
    case 'update':
      foreach ($items as $delta => $item) {

        // Process the value
        $sas = scald_rendered_to_sas($item['value']);
        $scald_included = scald_included($sas);
        $sids = array_unique($scald_included);

        // Parse copyright informations
        $copyrights = mee_extract_copyrights($item['value']);

        // Update ressources weight
        // In fact, we'll delete all the associations and recreate afterwards
        // the needed one, to be sure that new ressources are correctly
        // registered, and that no longer used one are removed.
        if (!is_array($item['ressource_manager'])) {
          _mee_load_ressources($node, $field, $item);
        }
        db_query("DELETE FROM {mee_ressources} WHERE content_nid=%d AND field='%s'", $node->nid, $field['field_name']);

        // We'll normalize the weight, putting our separator at 0.
        $separator = $item['ressource_manager'][0]['weight'];
        foreach ($sids as $sid) {
          $ressource = $item['ressource_manager'][$sid];
          $required = $ressource['required'];
          $weight = $ressource['weight'] - $separator;

          // insert in the table
          $query = "INSERT into {mee_ressources} (content_nid, atom_sid, field, weight, required, copyright) VALUES (%d, %d, '%s', %d, %d, '%s')";
          db_query($query, $node->nid, $sid, $field['field_name'], $weight, $required, $copyrights[$sid]);
        }

        // @@@TODO: Handle failure of fetch
        if (variable_get('mee_register_composites', FALSE)) {
          $atom = scald_fetch(scald_search(array(
            'base_id' => $node->nid . ':' . $delta,
          ), FALSE, TRUE));
          $atom->publisher = $node->uid;
          $atom->title = $node->title;
          $atom->authors = array(
            scald_uid_to_aid($node->uid),
          );

          // @@@TODO: This will completely override any authors listed & replace only with the Publisher.
          $atom->relationships = empty($scald_included) ? array() : array(
            'includes' => $scald_included,
          );
          scald_update_atom($atom);
        }
      }
      break;

    // end 'update'
    case 'delete':
      foreach ($items as $delta => $item) {
        scald_unregister_atom(scald_search(array(
          'base_id' => $node->nid . ':' . $delta,
        ), FALSE, TRUE));
      }

      // Delete all ressources associations for this field
      $query = "DELETE FROM {mee_ressources} WHERE content_nid = %d AND field = '%s'";
      db_query($query, $node->nid, $field['field_name']);
      break;

    // end 'delete'
    case 'sanitize':
      foreach ($items as $delta => $item) {
        if (!empty($field['mee_processing'])) {
          $check = is_null($node) || isset($node->build_mode) && $node->build_mode == NODE_BUILD_PREVIEW;
          $text = isset($item['value']) ? check_markup($item['value'], $item['format'], $check) : '';
        }
        else {
          $text = check_plain($item['value']);
        }
        $items[$delta]['safe'] = $text;
      }
      break;
  }
}