PodsAPI::import PHP Method

import() public method

Import data from an array or a CSV file.
Since: 1.7.1
public import ( mixed $import_data, boolean $numeric_mode = false, string $format = null ) : array
$import_data mixed PHP associative array or CSV input
$numeric_mode boolean Use IDs instead of the name field when matching
$format string Format of import data, options are php or csv
return array IDs of imported items
    public function import($import_data, $numeric_mode = false, $format = null)
    {
        /**
         * @var $wpdb wpdb
         */
        global $wpdb;
        if (null === $format && null !== $this->format) {
            $format = $this->format;
        }
        if ('csv' == $format && !is_array($import_data)) {
            $data = pods_migrate('sv', ',')->parse($import_data);
            $import_data = $data['items'];
        }
        pods_query("SET NAMES utf8");
        pods_query("SET CHARACTER SET utf8");
        // Loop through the array of items
        $ids = array();
        // Test to see if it's an array of arrays
        if (!is_array(@current($import_data))) {
            $import_data = array($import_data);
        }
        $pod = $this->load_pod(array('name' => $this->pod));
        if (false === $pod) {
            return pods_error(__('Pod not found', 'pods'), $this);
        }
        $fields = array_merge($pod['fields'], $pod['object_fields']);
        $simple_tableless_objects = PodsForm::simple_tableless_objects();
        foreach ($import_data as $key => $data_row) {
            $data = array();
            // Loop through each field (use $fields so only valid fields get parsed)
            foreach ($fields as $field_name => $field_data) {
                if (!isset($data_row[$field_name]) && !isset($data_row[$field_data['label']])) {
                    continue;
                }
                $field_id = $field_data['id'];
                $type = $field_data['type'];
                $pick_object = isset($field_data['pick_object']) ? $field_data['pick_object'] : '';
                $pick_val = isset($field_data['pick_val']) ? $field_data['pick_val'] : '';
                if (isset($data_row[$field_name])) {
                    $field_value = $data_row[$field_name];
                } else {
                    $field_value = $data_row[$field_data['label']];
                }
                if (null !== $field_value && false !== $field_value && '' !== $field_value) {
                    if ('pick' == $type || in_array($type, PodsForm::file_field_types())) {
                        $field_values = is_array($field_value) ? $field_value : array($field_value);
                        $pick_values = array();
                        foreach ($field_values as $pick_value) {
                            if (in_array($type, PodsForm::file_field_types()) || 'media' == $pick_object) {
                                $where = "`guid` = '" . pods_sanitize($pick_value) . "'";
                                if (0 < pods_absint($pick_value) && false !== $numeric_mode) {
                                    $where = "`ID` = " . pods_absint($pick_value);
                                }
                                $result = pods_query("SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = 'attachment' AND {$where} ORDER BY `ID`", $this);
                                if (!empty($result)) {
                                    $pick_values[] = $result[0]->id;
                                }
                            } elseif ('pick' == $type) {
                                $related_pod = false;
                                if ('pod' == $pick_object) {
                                    $related_pod = $this->load_pod(array('name' => $pick_val, 'table_info' => true), false);
                                }
                                if (empty($related_pod)) {
                                    $related_pod = array('id' => 0, 'type' => $pick_object);
                                }
                                if (in_array('taxonomy', array($pick_object, $related_pod['type']))) {
                                    $where = "`t`.`name` = '" . pods_sanitize($pick_value) . "'";
                                    if (0 < pods_absint($pick_value) && false !== $numeric_mode) {
                                        $where = "`tt`.`term_id` = " . pods_absint($pick_value);
                                    }
                                    $result = pods_query("SELECT `t`.`term_id` AS `id` FROM `{$wpdb->term_taxonomy}` AS `tt` LEFT JOIN `{$wpdb->terms}` AS `t` ON `t`.`term_id` = `tt`.`term_id` WHERE `taxonomy` = '{$pick_val}' AND {$where} ORDER BY `t`.`term_id`", $this);
                                    if (!empty($result)) {
                                        $pick_values[] = $result[0]->id;
                                    }
                                } elseif (in_array('post_type', array($pick_object, $related_pod['type'])) || in_array('media', array($pick_object, $related_pod['type']))) {
                                    $where = "`post_title` = '" . pods_sanitize($pick_value) . "'";
                                    if (0 < pods_absint($pick_value) && false !== $numeric_mode) {
                                        $where = "`ID` = " . pods_absint($pick_value);
                                    }
                                    $result = pods_query("SELECT `ID` AS `id` FROM `{$wpdb->posts}` WHERE `post_type` = '{$pick_val}' AND {$where} ORDER BY `ID`", $this);
                                    if (!empty($result)) {
                                        $pick_values[] = $result[0]->id;
                                    }
                                } elseif (in_array('user', array($pick_object, $related_pod['type']))) {
                                    $where = "`user_login` = '" . pods_sanitize($pick_value) . "'";
                                    if (0 < pods_absint($pick_value) && false !== $numeric_mode) {
                                        $where = "`ID` = " . pods_absint($pick_value);
                                    }
                                    $result = pods_query("SELECT `ID` AS `id` FROM `{$wpdb->users}` WHERE {$where} ORDER BY `ID`", $this);
                                    if (!empty($result)) {
                                        $pick_values[] = $result[0]->id;
                                    }
                                } elseif (in_array('comment', array($pick_object, $related_pod['type']))) {
                                    $where = "`comment_ID` = " . pods_absint($pick_value);
                                    $result = pods_query("SELECT `comment_ID` AS `id` FROM `{$wpdb->comments}` WHERE {$where} ORDER BY `ID`", $this);
                                    if (!empty($result)) {
                                        $pick_values[] = $result[0]->id;
                                    }
                                } elseif (in_array($pick_object, $simple_tableless_objects)) {
                                    $pick_values[] = $pick_value;
                                } elseif (!empty($related_pod['id'])) {
                                    $where = "`" . $related_pod['field_index'] . "` = '" . pods_sanitize($pick_value) . "'";
                                    if (0 < pods_absint($pick_value) && false !== $numeric_mode) {
                                        $where = "`" . $related_pod['field_id'] . "` = " . pods_absint($pick_value);
                                    }
                                    $result = pods_query("SELECT `" . $related_pod['field_id'] . "` AS `id` FROM `" . $related_pod['table'] . "` WHERE {$where} ORDER BY `" . $related_pod['field_id'] . "`", $this);
                                    if (!empty($result)) {
                                        $pick_values[] = $result[0]->id;
                                    }
                                }
                            }
                        }
                        $field_value = implode(',', $pick_values);
                    }
                    $data[$field_name] = $field_value;
                }
            }
            if (!empty($data)) {
                $params = array('pod' => $this->pod, 'data' => $data);
                $ids[] = $this->save_pod_item($params);
            }
        }
        return $ids;
    }

Usage Example

Example #1
0
 /**
  * Import data / Save multiple rows of data at once
  *
  * @see PodsAPI::import
  *
  * @param mixed $import_data PHP associative array or CSV input
  * @param bool $numeric_mode Use IDs instead of the name field when matching
  * @param string $format Format of import data, options are php or csv
  *
  * @return array IDs of imported items
  *
  * @since 2.3
  */
 public function import($import_data, $numeric_mode = false, $format = null)
 {
     return $this->api->import($import_data, $numeric_mode, $format);
 }