You are here

function gathercontent_import_page in GatherContent 7.2

AJAX function to process individual pages.

1 string reference to 'gathercontent_import_page'
gathercontent_menu in ./gathercontent.module
Implements hook_menu().

File

includes/pages_import.inc, line 209
Contains page field mapping form.

Code

function gathercontent_import_page() {
  $obj = gathercontent_get_obj();
  $out = array(
    'error' => t('Verification failed, please refreshing the page and try again.'),
  );
  if (isset($_POST['gc']) && isset($_POST['gc']['page_id'])) {
    $gc = $_POST['gc'];
    $page_id = $gc['page_id'];
    $obj
      ->getPostTypes();
    $project_id = variable_get('gathercontent_project_id');
    $pages = variable_get('gathercontent_saved_pages');
    $file_counter = 0;
    $total_files = 0;
    $files = array(
      'files' => array(),
      'total_files' => 0,
    );
    if ($_POST['cur_counter'] == 0) {
      variable_set('gathercontent_media_files', array());
    }
    if (is_array($pages) && isset($pages[$project_id]) && isset($pages[$project_id]['pages'][$page_id])) {
      extract($pages[$project_id]);
      $obj
        ->getFiles($page_id);
      $page = $pages[$page_id];
      $config = $obj
        ->getFieldConfig($page, $obj
        ->val($obj->files, $page_id, array()));
      $fields = $obj
        ->val($gc, 'fields', array());
      $save_settings = array(
        'post_type' => $gc['post_type'],
        'overwrite' => $gc['overwrite'],
        'filter' => $gc['filter'],
        'parent_id' => $gc['parent_id'],
        'fields' => array(),
      );
      $create_new = TRUE;
      if ($save_settings['overwrite'] > 0) {
        $node = node_load($save_settings['overwrite']);
        if ($node !== FALSE) {
          $create_new = FALSE;
          $entity = entity_metadata_wrapper('node', $node);
        }
      }
      if ($create_new === TRUE) {
        $e = entity_create('node', array(
          'type' => $save_settings['post_type'],
        ));
        $e->uid = $GLOBALS['user']->uid;
        $entity = entity_metadata_wrapper('node', $e);
      }
      if ($save_settings['overwrite'] == 0) {
        $entity->title
          ->set($page->name);
      }
      $field_values = array();
      if (isset($gc['fields'])) {
        foreach ($gc['fields'] as $info) {
          $tab = $info['field_tab'];
          $map_to = $info['map_to'];
          $field_name = $info['field_name'];
          if (empty($map_to)) {
            continue;
          }
          if ($map_to == '_dont_import_') {
            $save_settings['fields'][$tab . '_' . $field_name] = $map_to;
            continue;
          }
          elseif (isset($config[$tab]) && isset($config[$tab]['elements'][$field_name])) {
            $field = $config[$tab]['elements'][$field_name];
          }
          else {
            continue;
          }
          $save_settings['fields'][$tab . '_' . $field_name] = $map_to;
          if ($map_to != 'title') {
            $cur_field = $entity->{$map_to}
              ->info();
            if ($field['type'] == 'files') {
              if (is_array($field['value']) && count($field['value']) > 0) {
                $new_files = array();
                $dir = '';
                if (isset($cur_field['property info']['file'])) {
                  $info = field_info_instance('node', $map_to, $save_settings['post_type']);
                  if (isset($info['settings']['file_directory'])) {
                    $dir = $info['settings']['file_directory'];
                  }
                }
                foreach ($field['value'] as $file) {
                  $new_files[] = array(
                    'id' => $file->id,
                    'filename' => $file->filename,
                    'original_filename' => $file->original_filename,
                    'size' => $file->size,
                    'field' => $map_to,
                    'file_directory' => $dir,
                    'counter' => $file_counter,
                    'filter' => $save_settings['filter'],
                    'node_type' => $save_settings['post_type'],
                    'field_type' => $cur_field['type'],
                    'is_file_field' => isset($cur_field['property info']['file']),
                  );
                }
                $total_files += count($new_files);
                $files['files'][] = $new_files;
                $files['total_files'] = $total_files;
                $field['value'] = '#_gc_file_name_' . $file_counter . '#';
                $file_counter++;
              }
              else {
                $field['value'] = '';
              }
            }
          }
          if ($map_to == 'title') {
            $entity->title
              ->set(strip_tags($field['value']));
          }
          elseif (!empty($field['value'])) {
            if (isset($cur_field['property info']['file'])) {
              continue;
            }
            elseif (strpos($cur_field['type'], 'taxonomy_term') !== FALSE) {
              if (!is_array($field['value'])) {
                $field['value'] = array_filter(explode(',', strip_tags($field['value'])));
              }
              else {
                $field['value'] = array_filter($field['value']);
              }
              $is_list = strpos($cur_field['type'], 'list') !== FALSE;
              if (count($field['value']) > 0) {
                if (isset($cur_field['options list'])) {
                  if ($is_list) {
                    $options = $entity->{$map_to}
                      ->optionsList();
                    $values = array();
                    foreach ($field['value'] as $term) {
                      $key = array_search($term, $options);
                      if ($key !== FALSE) {
                        $values[] = $key;
                      }
                      elseif (isset($options[$term])) {
                        $values[] = $term;
                      }
                      else {
                        $tag = entity_property_values_create_entity('taxonomy_term', array(
                          'name' => $term,
                          'vocabulary' => $cur_field['bundle'],
                        ))
                          ->save()
                          ->value();
                        $values[] = $tag->tid;
                      }
                    }
                    $entity->{$map_to}
                      ->set($values);
                  }
                  else {
                    $options = $entity->{$map_to}
                      ->optionsList();
                    $term = array_shift($field['value']);
                    $key = array_search($term, $options);
                    if ($key !== FALSE) {
                      $entity->{$map_to}
                        ->set($key);
                    }
                    elseif (isset($options[$term])) {
                      $entity->{$map_to}
                        ->set($term);
                    }
                    else {
                      $tag = entity_property_values_create_entity('taxonomy_term', array(
                        'name' => $term,
                        'vocabulary' => $cur_field['bundle'],
                      ))
                        ->save()
                        ->value();
                      $entity->{$map_to}
                        ->set($tag);
                    }
                  }
                }
                else {
                  $tags = _gathercontent_add_terms($cur_field['bundle'], $field['value']);
                  if (count($tags) > 0) {
                    if (method_exists($entity->{$map_to}, 'getBundle')) {
                      $entity->{$map_to}
                        ->set(array_shift($tags));
                    }
                    else {
                      $entity->{$map_to}
                        ->set($tags);
                    }
                  }
                }
              }
            }
            elseif (isset($cur_field['options list'])) {
              $options = $entity->{$map_to}
                ->optionsList();
              if (!is_array($field['value'])) {
                $field['value'] = array(
                  strip_tags($field['value']),
                );
              }
              $values = array();
              foreach ($field['value'] as $val) {
                $key = array_search($val, $options);
                if ($key !== FALSE) {
                  $values[] = $key;
                }
                elseif (isset($options[$val])) {
                  $values[] = $val;
                }
              }
              if (count($values) > 0) {
                if (strpos($cur_field['type'], 'list') === 0) {
                  $entity->{$map_to}
                    ->set($values);
                }
                else {
                  $value = array_shift($values);
                  $entity->{$map_to}
                    ->set($value);
                }
              }
            }
            elseif ($cur_field['type'] == 'text_formatted') {
              if ($field['type'] == 'choice_checkbox') {
                if ($save_settings['filter'] == 'plain_text') {
                  $tmp = '';
                  foreach ($field['value'] as $value) {
                    $tmp .= $value . "\n";
                  }
                }
                else {
                  $tmp = '<ul>';
                  foreach ($field['value'] as $value) {
                    $tmp .= '<li>' . $value . '</li>';
                  }
                  $tmp .= "</ul>\n";
                  $field['value'] = $tmp;
                }
              }
              elseif ($field['type'] != 'text') {
                $field['value'] = '<p>' . $field['value'] . '</p>';
              }
              if ($save_settings['filter'] == 'plain_text') {
                $field['value'] = strip_tags($field['value']);
              }
              if (!isset($field_values[$map_to])) {
                $field_values[$map_to] = array(
                  'value' => '',
                  'format' => $save_settings['filter'],
                );
              }
              $val = $field_values[$map_to]['value'];
              if (!empty($val)) {
                $val .= "\n";
              }
              $val .= $field['value'];
              $field_values[$map_to]['value'] = $val;
            }
            else {
              $field['value'] = strip_tags($field['value']);
              $entity->{$map_to}
                ->set($field['value']);
            }
          }
        }
      }
      foreach ($field_values as $name => $val) {
        $entity->{$name}
          ->set($val);
      }
      $entity
        ->save();
      $save_settings['overwrite'] = $entity
        ->getIdentifier();
      $entity_uri = entity_uri($entity
        ->type(), $entity
        ->value());
      $mlid = gathercontent_create_menu_item($entity->title
        ->value(), $save_settings['parent_id'], $entity_uri['path']);
      $media = variable_get('gathercontent_media_files');
      if (!isset($media['total_files'])) {
        $media['total_files'] = 0;
      }
      if ($total_files > 0) {
        $media[$save_settings['overwrite']] = $files;
        if (!isset($media['total_files'])) {
          $media['total_files'] = 0;
        }
        $media['total_files'] += $total_files;
        variable_set('gathercontent_media_files', $media);
      }
      $cur_settings = variable_get('gathercontent_saved_settings');
      if (!is_array($cur_settings)) {
        $cur_settings = array();
      }
      if (!isset($cur_settings[$project_id])) {
        $cur_settings[$project_id] = array();
      }
      $cur_settings[$project_id][$page_id] = $save_settings;
      variable_set('gathercontent_saved_settings', $cur_settings);
      $out = array(
        'success' => TRUE,
        'page_percent' => $obj
          ->percent(++$_POST['cur_counter'], $_POST['total']),
        'redirect_url' => $media['total_files'] > 0 ? 'media' : 'finished',
        'page_id' => $page_id,
        'new_page_id' => $mlid,
        'new_page_html' => '<li data-post-type="' . $save_settings['post_type'] . '"><a href="#" title="' . check_plain($entity->title
          ->value()) . '" data-value="' . $mlid . '">' . check_plain($entity->title
          ->value()) . '</a></li>',
      );
    }
    else {
      $out = array(
        'error' => t('There was a problem importing the page, please refresh and try again.'),
      );
    }
  }
  else {
    $out = array(
      'error' => t('There was a problem importing the page, please refresh and try again.'),
    );
  }
  echo json_encode($out);
  exit;
}