You are here

function ml_image_basic_upload_options_form_submit in Media Library 6

Submit callback for ml_image_basic_upload_options_form()

File

ml_image/ml_image_basic/ml_image_basic.module, line 287
This module aims to provide some basic sources for images to use with Media Library, such as navigating on existing files and uploading new images

Code

function ml_image_basic_upload_options_form_submit($form, &$form_state) {
  $metadata = ml_image_get_metadata();
  if (!isset($form_state['media_obj']->metatags) || !is_array($form_state['media_obj']->metatags)) {
    $form_state['media_obj']->metatags = array();
  }
  $metatags = new stdClass();
  foreach ($metadata as $field => $data) {
    switch ($data['type']) {
      case 'textfield':
      case 'textarea':
        if (!empty($form_state['values'][$field])) {
          $form_state['media_obj']->metatags[$field] = $form_state['values'][$field];
          $metatags->{$field} = $form_state['values'][$field];
        }
        break;
      case 'term':
        $vocabulary = $data['vocabulary'];
        $vid = $vocabulary->vid;

        // Free tagging vocabularies do not send their tids in the form,
        // so we'll detect them here and process them independently.
        $terms = array();
        if ($vocabulary->tags) {
          $typed_terms = drupal_explode_tags($form_state['values'][$field]);
          $inserted = array();
          foreach ($typed_terms as $typed_term) {

            // See if the term exists in the chosen vocabulary
            // and return the tid; otherwise, add a new record.
            $possibilities = taxonomy_get_term_by_name($typed_term);
            $typed_term_tid = NULL;

            // tid match, if any.
            foreach ($possibilities as $possibility) {
              if ($possibility->vid == $vid) {
                $typed_term_tid = $possibility->tid;
              }
            }
            if (!$typed_term_tid) {
              $edit = array(
                'vid' => $vid,
                'name' => $typed_term,
              );
              $status = taxonomy_save_term($edit);
              $typed_term_tid = $edit['tid'];
            }
            $terms[$typed_term_tid] = $typed_term;
          }
        }
        else {
          $terms = $form_state['values'][$field];
        }

        // Save original string to object, ready to be used
        $form_state['media_obj']->metatags[$field] = $form_state['values'][$field];

        // Save array version to object being inserted
        $metatags->{$field} = $terms;
        break;
    }
  }
  $metatags->fid = $form_state['media_obj']->fid;
  $metatags->source = 'upload';

  // Save metatags do database
  ml_image_save_metatags($metatags);
}