function import_ensure_valid_data(&$data)
{
    $errors = array();
    $record = 1;
    foreach ($data as $row) {
        $rsp = dots_ensure_valid_data($row);
        if (!$rsp['ok']) {
            $errors[] = array('error' => $rsp['error'], 'record' => $record);
        }
        $record++;
    }
    $ok = count($errors) ? 0 : 1;
    return array('ok' => $ok, 'errors' => $errors);
}
function dots_create_dot(&$user, &$sheet, &$data, $more = array())
{
    # if we've gotten here via lib_uploads then
    # we will have already done validation.
    if (!$more['skip_validation']) {
        $rsp = dots_ensure_valid_data($row);
        if (!$rsp['ok']) {
            return $rsp;
        }
    }
    #
    $id = dbtickets_create(64);
    if (!$id) {
        return array('ok' => 0, 'error' => 'Ticket server failed');
    }
    #
    # Assign basic geo bits - keep track of stuff that has
    # been derived so that we can flag them accordingly in
    # the DotsExtras table.
    #
    list($data, $derived) = dots_derive_location_data($data);
    # Note that we return $derived with the response below
    # (assuming everything else works) and check for any errors
    # out of band, read: the _import_dots function (20110311/straup)
    #
    # creation date for the point (different from import date)
    # should this be stored/flagged as an extra?
    #
    $now = time();
    if ($created = $data['created']) {
        #
        # Because intval("2010-09-23T00:18:55Z") returns '2010' ...
        # Because is_numeric(20101029154025.000) returns true ...
        # Because strtotime(time()) returns false ...
        # BECAUSE GOD HATES YOU ...
        #
        $created = preg_match("/^\\d+\$/", $created) ? $created : strtotime($created);
        # if ! $created then reassign $now ?
        # Now convert everything back in to a datetime string
        if ($created) {
            $data['created'] = gmdate('Y-m-d H:i:s', $created);
        }
    } else {
        $data['created'] = gmdate('Y-m-d H:i:s', $now);
    }
    #
    # permissions
    #
    $perms_map = dots_permissions_map('string keys');
    $perms = $perms_map['public'];
    if ($data['perms'] == 'private' || $more['mark_all_private']) {
        $perms = $perms_map['private'];
    }
    #
    # Go! Or rather... start!
    #
    $dot = array('id' => $id, 'user_id' => $user['id'], 'sheet_id' => $sheet['id'], 'perms' => $perms);
    # Always store created date in the user Sheets table; it's
    # not clear how this relates/works with the dots extras
    # stuff yet (20101210/straup)
    $to_denormalize = array('created');
    foreach ($to_denormalize as $key) {
        if (isset($data[$key]) && !empty($data[$key])) {
            $dot[$key] = $data[$key];
        }
    }
    # Please to write me: A discussion on the relationship between
    # details, extras, 'indexed' and search. (20101213/straup)
    #
    # Dots extras (as in: extra things you can search for)
    #
    $details = array();
    $extras = array();
    if ($GLOBALS['cfg']['enable_feature_dots_indexing']) {
        $index_on = array();
        if ($GLOBALS['cfg']['dots_indexing_index_all']) {
            $tmp = array();
            $skip = array('latitude', 'longitude', 'created', 'title_internal');
            foreach (array_keys($data) as $f) {
                if (!in_array($f, $skip)) {
                    $tmp[] = $f;
                }
            }
        } else {
            $tmp = explode(",", $more['dots_index_on'], $GLOBALS['cfg']['dots_indexing_max_cols']);
        }
        #
        foreach ($tmp as $field) {
            $field = trim($field);
            if (!isset($data[$field])) {
                continue;
            }
            $extras[] = array('dot_id' => $id, 'sheet_id' => $sheet['id'], 'user_id' => $user['id'], 'name' => $field, 'value' => $data[$field]);
            $index_on[] = AddSlashes($field);
        }
        $dot['index_on'] = implode(",", $index_on);
    }
    #
    # Store any remaining fields in a big old JSON blob
    #
    foreach (array_keys($data) as $label) {
        $label = filter_strict(trim($label));
        if (!$label) {
            continue;
        }
        $value = $data[$label];
        $value = filter_strict(trim($value));
        if (!$value) {
            continue;
        }
        $ns = null;
        $pred = $label;
        if (strpos($label, ':')) {
            list($ns, $pred) = explode(':', $label, 2);
        }
        $detail = array('namespace' => $ns, 'label' => $pred, 'value' => $data[$label]);
        if (isset($derived[$label])) {
            $extra['derived_from'] = $derived[$label];
        }
        if (!is_array($details[$label])) {
            $details[$label] = array();
        }
        $details[$label][] = $detail;
    }
    $dot['details_json'] = json_encode($details);
    #
    # Look, we are FINALLY NOW creating the dot
    #
    $insert = array();
    foreach ($dot as $key => $value) {
        $insert[$key] = AddSlashes($value);
    }
    $rsp = db_insert_users($user['cluster_id'], 'Dots', $insert);
    if (!$rsp['ok']) {
        return $rsp;
    }
    $dot['details'] = $details;
    #
    # Update the DotsLookup table
    #
    $lookup = array('dot_id' => $id, 'sheet_id' => $sheet['id'], 'user_id' => $user['id'], 'imported' => $now, 'last_modified' => $now);
    if ($more['buffer_lookup_inserts']) {
        $rsp['lookup'] = $lookup;
    } else {
        $lookup_rsp = dots_lookup_create($lookup);
        if (!$lookup_rsp['ok']) {
            # What then...
        }
    }
    #
    # Now the searching (first the basics then any 'extras' specific to this dot)
    #
    $search = array('dot_id' => $id, 'sheet_id' => $sheet['id'], 'user_id' => $user['id'], 'imported' => $now, 'created' => $data['created'], 'perms' => $perms, 'geohash' => $data['geohash'], 'latitude' => $data['latitude'], 'longitude' => $data['longitude']);
    if ($more['buffer_search_inserts']) {
        $rsp['search'] =& $search;
    } else {
        $search_rsp = dots_search_add_dot($search);
        if (!$search_rsp['ok']) {
            # What then...
        }
    }
    # extras
    if ($more['buffer_extras_inserts']) {
        $rsp['extras'] = $extras;
    } else {
        $extras_rsp = dots_search_extras_add_lots_of_extras($extras);
        if (!$extras_rsp['ok']) {
            # What then...
        }
    }
    #
    # Happy happy
    #
    $rsp['dot'] =& $dot;
    $rsp['derived'] =& $derived;
    return $rsp;
}