function importCsvProcess(&$importSet, $id_reqs, $regfid, $validateOverride)
{
    global $xoopsDB, $xoopsUser, $xoopsConfig, $myts;
    // $xoopsDB is required by q
    if (!$myts) {
        $myts =& MyTextSanitizer::getInstance();
    }
    echo "<b>** Importing</b><br><br>";
    $form_uid = "0";
    global $override_import_proxyid;
    if ($override_import_proxyid) {
        $form_proxyid = $override_import_proxyid;
    } else {
        $form_proxyid = $xoopsUser->getVar('uid');
    }
    // lock formulize_form -- note that every table we use needs to be locked, so linked selectbox lookups may fail
    if ($regfid == $importSet[4]) {
        // only lockup reg codes table if we're dealing with a profile form, in which case we assume reg codes is installed and the table exists
        $xoopsDB->query("LOCK TABLES " . $xoopsDB->prefix("formulize_" . $importSet[8]) . " WRITE, " . $xoopsDB->prefix("users") . " WRITE, " . $xoopsDB->prefix("formulize_entry_owner_groups") . " WRITE, " . $xoopsDB->prefix("reg_codes") . " WRITE, " . $xoopsDB->prefix("groups_users_link") . " WRITE, " . $xoopsDB->prefix("modules") . " READ, " . $xoopsDB->prefix("config") . " READ, " . $xoopsDB->prefix("formulize") . " READ, " . $xoopsDB->prefix("formulize_id") . " READ, " . $xoopsDB->prefix("formulize_saved_views") . " READ, " . $xoopsDB->prefix("formulize_group_filters") . " READ");
    } else {
        $xoopsDB->query("LOCK TABLES " . $xoopsDB->prefix("formulize_" . $importSet[8]) . " WRITE, " . $xoopsDB->prefix("users") . " READ, " . $xoopsDB->prefix("formulize_entry_owner_groups") . " WRITE, " . $xoopsDB->prefix("groups_users_link") . " READ, " . $xoopsDB->prefix("formulize") . " READ, " . $xoopsDB->prefix("formulize_id") . " READ, " . $xoopsDB->prefix("formulize_saved_views") . " READ, " . $xoopsDB->prefix("formulize_group_filters") . " READ");
    }
    $rowCount = 1;
    $other_values = array();
    $usersMap = array();
    $entriesMap = array();
    while (!feof($importSet[1])) {
        $row = fgetcsv($importSet[1], 99999);
        if (is_array($row) and count($row) > 1) {
            $rowCount++;
            $this_id_req = "";
            if (is_array($id_reqs)) {
                // get the id_req if necessary.  will happen regardless of position of idreq column
                $this_id_req = $row[$importSet[7]['idreqs']];
            }
            $links = count($importSet[6]);
            for ($link = 0; $link < $links; $link++) {
                if (isset($GLOBALS['formulize_ignoreColumnsOnImport'][$link])) {
                    continue;
                }
                if ($importSet[6][$link] == -1) {
                    if ($importSet[3][$link] == _formulize_DE_CALC_CREATOR) {
                        $form_uid = getUserId($row[$link]);
                    }
                }
            }
            // get the current max id_req
            if (!$this_id_req) {
                $max_id_reqq = q("SELECT MAX(entry_id) FROM " . $xoopsDB->prefix("formulize_" . $importSet[8]));
                $max_id_req = $max_id_reqq[0]["MAX(entry_id)"] + 1;
            } else {
                $max_id_req = $this_id_req;
                // get the uid and creation date too
                $member_handler =& xoops_gethandler('member');
                $this_metadata = getMetaData($this_id_req, $member_handler, $importSet[4]);
                // importSet[4] is id_form (fid)
                $this_uid = $this_metadata['created_by_uid'];
                $this_creation_date = $this_metadata['created'];
            }
            // if this is the registration form, and we're making new entries, then handle the creation of the necessary user account
            // need to get the five userprofile fields from the form, $importSet[7] contains the keys for them -- email, username, fullname, password, regcode
            if ($regfid == $importSet[4] and !is_array($id_reqs)) {
                $up_regcode = $row[$importSet[7]['regcode']];
                $up_username = $row[$importSet[7]['username']];
                $up_fullname = $row[$importSet[7]['fullname']];
                $up_password = $row[$importSet[7]['password']];
                $up_email = $row[$importSet[7]['email']];
                $tz = $xoopsConfig['default_TZ'];
                list($newid, $actkey) = createMember(array('regcode' => '', 'approval' => false, 'user_viewemail' => 0, 'uname' => $up_username, 'name' => $up_fullname, 'email' => $up_email, 'pass' => $up_password, 'timezone_offset' => $tz));
                processCode($up_regcode, $newid);
                $form_uid = $newid;
                // put in new user id here
            }
            $links = count($importSet[6]);
            $fieldValues = array();
            $newEntryId = "";
            for ($link = 0; $link < $links; $link++) {
                // used as a flag to indicate whether we're dealing with a linked selectbox or not, since if we are, that is the only case where we don't want to do HTML special chars on the value // deprecated in 3.0
                $all_valid_options = false;
                if ($importSet[6][$link] != -1) {
                    $element = $importSet[5][0][$importSet[6][$link]];
                    $id_form = $importSet[4];
                    if ($link == $links - 1) {
                        // remove some really odd line endings if present, only happens when dealing with legacy outputs of really old/odd systems
                        $row_value = str_replace(chr(19) . chr(16), "", $row[$link]);
                    } else {
                        $row_value = $row[$link];
                    }
                    if ($row_value != "") {
                        switch ($element["ele_type"]) {
                            case "select":
                                if ($importSet[5][1][$link] and !strstr($row_value, ",") and (!is_numeric($row_value) or $row_value < 10000000)) {
                                    // Linked element
                                    $linkElement = $importSet[5][1][$link];
                                    $ele_value = unserialize($element["ele_value"]);
                                    list($all_valid_options, $all_valid_options_ids) = getElementOptions($linkElement[2]['ele_handle'], $linkElement[2]['id_form']);
                                    if ($ele_value[1]) {
                                        // Multiple options
                                        $element_value = $linkElement[0] . "#*=:*" . $linkElement[1] . "#*=:*";
                                        $items = explode("\n", $row_value);
                                        $row_value = ",";
                                        foreach ($items as $item) {
                                            $item_value = trim($item);
                                            if ($optionIndex = array_search($item_value, $all_valid_options)) {
                                                $ele_id = $all_valid_options_ids[$optionIndex];
                                            } else {
                                                foreach ($all_valid_options as $optionIndex => $thisoption) {
                                                    if (trim($item_value) == trim(trans($thisoption))) {
                                                        $item_value = $thisoption;
                                                        $ele_id = $all_valid_options_ids[$optionIndex];
                                                        break;
                                                    }
                                                }
                                            }
                                            $row_value .= $ele_id . ",";
                                        }
                                    } else {
                                        // Single option
                                        if ($optionIndex = array_search($row_value, $all_valid_options)) {
                                            $ele_id = $all_valid_options_ids[$optionIndex];
                                        } else {
                                            foreach ($all_valid_options as $optionIndex => $thisoption) {
                                                if (trim($row_value) == trim(trans($thisoption))) {
                                                    $row_value = $thisoption;
                                                    $ele_id = $all_valid_options_ids[$optionIndex];
                                                    break;
                                                }
                                            }
                                        }
                                        $row_value = $ele_id;
                                    }
                                } elseif (!strstr($row_value, ",") and (!is_numeric($row_value) or $row_value < 10000000)) {
                                    // Not-Linked element
                                    $ele_value = unserialize($element["ele_value"]);
                                    // handle fullnames or usernames
                                    $temparraykeys = array_keys($ele_value[2]);
                                    if ($temparraykeys[0] === "{FULLNAMES}" or $temparraykeys[0] === "{USERNAMES}") {
                                        // ADDED June 18 2005 to handle pulling in usernames for the user's group(s) -- updated for real live use September 6 2006
                                        if ($temparraykeys[0] === "{FULLNAMES}") {
                                            $nametype = "name";
                                        }
                                        if ($temparraykeys[0] === "{USERNAMES}") {
                                            $nametype = "uname";
                                        }
                                        if (!isset($fullnamelist)) {
                                            $fullnamelistq = q("SELECT uid, {$nametype} FROM " . $xoopsDB->prefix("users") . " ORDER BY uid");
                                            static $fullnamelist = array();
                                            foreach ($fullnamelistq as $thisname) {
                                                $fullnamelist[$thisname['uid']] = $thisname[$nametype];
                                            }
                                        }
                                        if ($ele_value[1]) {
                                            // multiple
                                            $items = explode("\n", $row_value);
                                        } else {
                                            $items = array(0 => $row_value);
                                        }
                                        $numberOfNames = 0;
                                        $row_value = "";
                                        foreach ($items as $item) {
                                            if (is_numeric($item)) {
                                                $row_value .= "*=+*:" . $item;
                                                $numberOfNames++;
                                            } else {
                                                $uids = array_keys($fullnamelist, $item);
                                                // instead of matching on all values, like we used to, match only the first name found (lowest user id)
                                                // to match other users besides the first one, use a user id number instead of a name in the import spreadsheet
                                                $row_value .= "*=+*:" . $uids[0];
                                                $numberOfNames++;
                                            }
                                        }
                                        if ($numberOfNames == 1) {
                                            // single entries are not supposed to have the separator at the front
                                            $row_value = substr_replace($row_value, "", 0, 5);
                                        }
                                        break;
                                    }
                                    if ($ele_value[1]) {
                                        // Multiple options
                                        $element_value = "";
                                        $options = $ele_value[2];
                                        $items = explode("\n", $row_value);
                                        foreach ($items as $item) {
                                            $item_value = trim($item);
                                            if (!in_array($item_value, $options, true)) {
                                                // last option causes strict matching by type
                                                foreach ($options as $thisoption => $default_value) {
                                                    if (trim($item_value) == trim(trans($thisoption))) {
                                                        $item_value = $thisoption;
                                                        break;
                                                    }
                                                }
                                            }
                                            $element_value .= "*=+*:" . $item_value;
                                        }
                                        $row_value = $element_value;
                                    } else {
                                        // Single option
                                        $options = $ele_value[2];
                                        if (!in_array($row_value, $options, true)) {
                                            // last option causes strict matching by type
                                            foreach ($options as $thisoption => $default_value) {
                                                if (trim($row_value) == trim(trans($thisoption))) {
                                                    $row_value = $thisoption;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                } elseif (strstr($row_value, ",") or is_numeric($row_value) and $row_value > 10000000) {
                                    // the value is a comma separated list of linked values, so we need to add commas before and after, to adhere to the Formulize data storage spec
                                    if (substr($row_value, 0, 1) != ",") {
                                        $row_value = "," . $row_value;
                                    }
                                    if (substr($row_value, -1) != ",") {
                                        $row_value = $row_value . ",";
                                    }
                                }
                                break;
                            case "checkbox":
                                $options = unserialize($element["ele_value"]);
                                $element_value = "";
                                $items = explode("\n", $row_value);
                                foreach ($items as $item) {
                                    $item_value = trim($item);
                                    if (!in_array($item_value, $options, true)) {
                                        // last option causes strict matching by type
                                        $foundit = false;
                                        $hasother = false;
                                        foreach ($options as $thisoption => $default_value) {
                                            if (trim($item_value) == trim(trans($thisoption))) {
                                                $item_value = $thisoption;
                                                $foundit = true;
                                                break;
                                            }
                                            if (preg_match('/\\{OTHER\\|+[0-9]+\\}/', $thisoption)) {
                                                $hasother = $thisoption;
                                            }
                                        }
                                        if ($foundit) {
                                            $element_value .= "*=+*:" . $item_value;
                                        } elseif ($hasother) {
                                            $other_values[] = "INSERT INTO " . $xoopsDB->prefix("formulize_other") . " (id_req, ele_id, other_text) VALUES (\"{$max_id_req}\", \"" . $element["ele_id"] . "\", \"" . $myts->htmlSpecialChars(trim($item_value)) . "\")";
                                            $element_value .= "*=+*:" . $hasother;
                                        } elseif (!$validateOverride) {
                                            print "ERROR: INVALID TEXT FOUND FOR A CHECKBOX ITEM -- {$item_value} -- IN ROW:<BR>";
                                            print_r($row);
                                            print "<br><br>";
                                        }
                                    } else {
                                        $element_value .= "*=+*:" . $item_value;
                                    }
                                }
                                $row_value = $element_value;
                                break;
                            case "radio":
                                $options = unserialize($element["ele_value"]);
                                if (!in_array($row_value, $options, true)) {
                                    // last option causes strict matching by type
                                    $foundit = false;
                                    $hasother = false;
                                    foreach ($options as $thisoption => $default_value) {
                                        if (trim($row_value) == trim(trans($thisoption))) {
                                            $row_value = $thisoption;
                                            $foundit = true;
                                            break;
                                        }
                                        if (preg_match('/\\{OTHER\\|+[0-9]+\\}/', $thisoption)) {
                                            $hasother = $thisoption;
                                        }
                                    }
                                    if (!$foundit and $hasother) {
                                        $other_values[] = "INSERT INTO " . $xoopsDB->prefix("formulize_other") . " (id_req, ele_id, other_text) VALUES (\"{$max_id_req}\", \"" . $element["ele_id"] . "\", \"" . $myts->htmlSpecialChars(trim($row_value)) . "\")";
                                        $row_value = $hasother;
                                    } elseif (!$foundit and !$validateOverride) {
                                        print "ERROR: INVALID TEXT FOUND FOR A RADIO BUTTON ITEM -- {$row_value} -- IN ROW:<BR>";
                                        print_r($row);
                                        print "<br><br>";
                                    }
                                }
                                break;
                            case "date":
                                $row_value = date("Y-m-d", strtotime(str_replace("/", "-", $row_value)));
                                break;
                            case "yn":
                                if (!is_numeric($row_value)) {
                                    $yn_value = strtoupper($row_value);
                                    if ($yn_value == "YES") {
                                        $row_value = 1;
                                    } else {
                                        if ($yn_value == "NO") {
                                            $row_value = 2;
                                        }
                                    }
                                }
                                break;
                        }
                        // record the values for inserting as part of this record
                        // prior to 3.0 we did not do the htmlspecialchars conversion if this was a linked selectbox...don't think that's a necessary exception in 3.0 with new data structure
                        $fieldValues[$element['ele_handle']] = $myts->htmlSpecialChars($row_value);
                    }
                    // end of if there's a value in the current column
                } elseif (isset($importSet[7]['usethisentryid']) and $link == $importSet[7]['usethisentryid']) {
                    // if this is not a valid column, but it is an entry id column, then capture the entry id from the cell
                    $newEntryId = $row[$link] ? $row[$link] : "";
                }
                // end of if this is a valid column
            }
            // end of looping through $links (columns?)
            // now that we've recorded all the values, do the actual updating/inserting of this record
            if ($this_id_req) {
                // updating an entry
                $form_uid = $this_uid;
                $updateSQL = "UPDATE " . $xoopsDB->prefix("formulize_" . $importSet[8]) . " SET ";
                $start = true;
                foreach ($fieldValues as $elementHandle => $fieldValue) {
                    if (!$start) {
                        // on subsequent fields, add a comma
                        $updateSQL .= ", ";
                    }
                    $start = false;
                    $updateSQL .= "`{$elementHandle}` = '" . formulize_db_escape($fieldValue) . "'";
                }
                $updateSQL .= ", mod_datetime=NOW(), mod_uid={$form_proxyid} WHERE entry_id=" . intval($this_id_req);
                if (IMPORT_WRITE) {
                    if (!($result = $xoopsDB->queryF($updateSQL))) {
                        print "<br><b>FAILED</b> to update data, SQL: {$updateSQL}<br>" . $xoopsDB->error() . "<br>";
                    }
                }
            } else {
                // inserting a new entry
                $fields = "";
                $values = "";
                $element_handler = xoops_getmodulehandler('elements', 'formulize');
                foreach ($fieldValues as $elementHandle => $fieldValue) {
                    $fields .= ", `" . $elementHandle . "`";
                    $values .= ", '" . formulize_db_escape($fieldValue) . "'";
                    $elementObject = $element_handler->get($elementHandle);
                    if ($elementObject->getVar('ele_desc') == "Primary Key") {
                        $newEntryId = $fieldValue;
                    }
                }
                if ($form_uid == 0) {
                    $form_uid = $form_proxyid;
                }
                $entryIdFieldText = $newEntryId ? "entry_id, " : "";
                $newEntryId .= $newEntryId ? ", " : "";
                $insertElement = "INSERT INTO " . $xoopsDB->prefix("formulize_" . $importSet[8]) . " (" . $entryIdFieldText . "creation_datetime, mod_datetime, creation_uid, mod_uid" . $fields . ") VALUES (" . $newEntryId . "NOW(), NOW(), '" . intval($form_uid) . "', '" . intval($form_proxyid) . "'" . $values . ")";
                if (IMPORT_WRITE) {
                    if (!($result = $xoopsDB->queryF($insertElement))) {
                        static $duplicatesFound = false;
                        if (strstr($xoopsDB->error(), "Duplicate entry")) {
                            if (!$duplicatesFound) {
                                print "<br><b>FAILED</b> to insert <i>some</i> data.  At least one duplicate value was found in a column that does not allow duplicate values.<br>";
                                $duplicatesFound = true;
                            }
                        } else {
                            print "<br><b>FAILED</b> to insert data, SQL: {$insertElement}<br>" . $xoopsDB->error() . "<br>";
                        }
                    } else {
                        // need to record new group ownership info too
                        $usersMap[] = $form_uid;
                        $entriesMap[] = $xoopsDB->getInsertId();
                    }
                } else {
                    echo "<br>" . $insertElement . "<br>";
                }
            }
            $idToShow = $newEntryId ? $newEntryId : $max_id_req;
            //echo "line $rowCount, id $idToShow<br>";
        }
        // end of if we have contents in this row
    }
    // end of looping through each row of the file
    if (count($usersMap) > 0) {
        // if new entries were created...
        include_once XOOPS_ROOT_PATH . "/modules/formulize/class/data.php";
        $data_handler = new formulizeDataHandler($id_form);
        if (!($groupResult = $data_handler->setEntryOwnerGroups($usersMap, $entriesMap))) {
            print "ERROR: failed to write the entry ownership information to the database.<br>" . $xoopsDB->error() . "<br>";
        }
    }
    // unlock tables
    $xoopsDB->query("UNLOCK TABLES");
    // insert all the other values that were recorded
    foreach ($other_values as $other) {
        if (!($result = $xoopsDB->query($other))) {
            print "ERROR: could not insert 'other' value: {$other}<br>";
        }
    }
}
Example #2
0
$message = $result['latest_message'];
global $name, $dob, $location, $pwds, $phoneNumber;
switch (strtolower($level)) {
    case 0:
        $result = getMember($phoneNumber);
        $phonecheck = $result['phoneNumber'];
        if ($phoneNumber == $phonecheck) {
            $response = getHomeMenu2();
            sendOutput($response, 2);
            exit;
        }
        $response = getHomeMenu();
        break;
    case 1:
        $response = getLevelOneMenu($message);
        $result = createMember($phoneNumber, $name);
        break;
    case 2:
        $response = getLevelTwoMenu($message);
        //take $dob
        $result = updateMemberDob($phoneNumber, $dob);
        break;
    case 3:
        $response = getLevelThreeMenu($message);
        //take $location
        $result = updateMemberLocation($phoneNumber, $location);
        break;
    case 4:
        $response = getLevelFourMenu($message);
        //take $pwds
        $result = updateMemberPassword($phoneNumber, $pwds);