Example #1
0
 */
include "functions/functions.input.php";
/**
 * Import functions
 */
include "functions/functions.import.php";
global $db;
$operator_id = get_operator_id();
$msg = "";
if (isset($_POST['submit'])) {
    $case_id = get_case_id($operator_id);
    $sql = "SELECT sivr.var\r\n\t\t\tFROM `sample_import_var_restrict` as sivr, `sample_var` as s, `case` as c\r\n\t\t\tWHERE c.case_id = '{$case_id}'\r\n            AND s.var_id = sivr.var_id\r\n\t\t\tAND s.sample_id = c.sample_id\r\n\t\t\tAND sivr.type = 3";
    $pphone = $db->GetOne($sql);
    //validate primary phone number supplied
    if (isset($_POST["v_{$pphone}"]) && only_numbers($_POST["v_{$pphone}"]) != "") {
        $phone = $db->qstr(only_numbers($_POST["v_{$pphone}"]));
        //Create a new sample record and add CASEREFERREDFROM to be this current case id
        $db->StartTrans();
        //create a new sample entry
        $sql = "SELECT s.import_id\r\n\t\t\tFROM `sample` as s, `case` as c\r\n\t\t\tWHERE c.case_id = '{$case_id}'\r\n\t\t\tAND s.sample_id = c.sample_id";
        $import_id = $db->GetOne($sql);
        //get all sample records
        $sql = "SELECT sivr.var,s.val, sivr.type\r\n\t\t\tFROM `sample_import_var_restrict` as sivr, `sample_var` as s, `case` as c\r\n\t\t\tWHERE c.case_id = '{$case_id}'\r\n\t\t\tAND s.sample_id = c.sample_id\r\n\t\t\tAND s.var_id = sivr.var_id";
        $rs = $db->GetAll($sql);
        $tzone = get_setting("DEFAULT_TIME_ZONE");
        //set this to default
        //Get the timezone
        foreach ($rs as $r) {
            $tz = get_time_zone($_POST["v_" . $r['var']], $r['type']);
            if ($tz !== false) {
                $tzone = $tz;
Example #2
0
<?php

function only_numbers($number)
{
    return preg_replace("/[^[:digit:]]/", "", $number);
}
echo only_numbers("Em 2011 eu fui com 3 amigos para uma festa que tinha 300 pessoas");
Example #3
0
/**
 * Import a CSV file to the sample database
 *
 * @param string $file File name to open
 * @param string $description A description of the sample
 * @param array $fields Which fields to import and what type they are assigned to
 *
 * @see verify_fields()
 * @see display_table()
 *
 */
function import_file($file, $description, $fields, $firstrow = 2)
{
    $row = 1;
    $handle = fopen($file, "r");
    //import into database
    global $db;
    $db->StartTrans();
    $sql = "INSERT INTO sample_import\r\n\t\t(sample_import_id, description)\r\n\t\tVALUES (NULL, '{$description}')";
    //	print("$sql<br/>");
    //	if ($db->HasFailedTrans()) { print "FAILED"; exit(); }
    $rs = $db->Execute($sql);
    $id = $db->Insert_ID();
    $selected_type = array();
    $selected_name = array();
    $sirv_id = array();
    foreach ($fields as $key => $val) {
        if (strncmp($key, "i_", 2) == 0) {
            $selected_type[substr($key, 2)] = $fields["t_" . substr($key, 2)];
            $selected_name[substr($key, 2)] = $fields["n_" . substr($key, 2)];
            $restrict = 1;
            //Set restrictions on columns
            if (isset($fields["a_" . substr($key, 2)])) {
                $restrict = 0;
            }
            $sql = "INSERT INTO sample_import_var_restrict\r\n\t\t\t\t(`sample_import_id`,`var`,`type`,`restrict`)\r\n\t\t\t\tVALUES ({$id},'" . $fields["n_" . substr($key, 2)] . "','" . $fields["t_" . substr($key, 2)] . "',{$restrict})";
            $db->Execute($sql);
            $sirv_id[substr($key, 2)] = $db->Insert_ID();
            //
        }
    }
    /**
     * create an ordered index of columns that contain data for obtaining the timezone
     * type of 5,4,3,2 preferred
     */
    arsort($selected_type);
    $imported = 0;
    while (($data = fgetcsv($handle)) !== FALSE) {
        //data contains an array of elements in the csv
        //selected contains an indexed array of elements to import with the type attached
        if ($row >= $firstrow) {
            //determine if there is a phone number - if not - do not import
            $numberavail = 0;
            foreach ($selected_type as $key => $val) {
                if ($val == 2 || $val == 3) {
                    $dkey = only_numbers($data[$key - 1]);
                    if (!empty($dkey)) {
                        $data[$key - 1] = $dkey;
                        $numberavail = 1;
                    }
                }
            }
            if ($numberavail == 1) {
                //insert into sample field
                //first find the timezone
                $tzone = get_setting("DEFAULT_TIME_ZONE");
                //set this to default
                /**
                 * Determine time zone from all possible sources in sample_var_type table
                 *
                 */
                foreach ($selected_type as $key => $val) {
                    $tz = get_time_zone($data[$key - 1], $val);
                    if ($tz !== false) {
                        $tzone = $tz;
                        break;
                    }
                }
                /**
                 * insert using primary phone number (3)
                 */
                $ppid = array_search('3', $selected_type);
                $dppid = only_numbers($data[$ppid - 1]);
                $sql = "INSERT INTO sample (sample_id,import_id,Time_zone_name,phone)\r\n\t\t\t\t\tVALUES (NULL,'{$id}','{$tzone}','{$dppid}')";
                $db->Execute($sql);
                $sid = $db->Insert_Id();
                /**
                 * insert into sample_var field
                 */
                foreach ($selected_name as $key => $val) {
                    $dkey = $db->Quote($data[$key - 1]);
                    $sql = "INSERT INTO sample_var (sample_id,var_id,val)\r\n\t\t\t\t\t\tVALUES ('{$sid}','{$sirv_id[$key]}',{$dkey})";
                    $db->Execute($sql);
                }
                $imported++;
            }
        }
        $row++;
    }
    fclose($handle);
    //cleanup
    unlink($file);
    return $db->CompleteTrans();
}