diff --git a/admin/vvexport.php b/admin/vvexport.php index aa6272fdef3..ec1f9080148 100644 --- a/admin/vvexport.php +++ b/admin/vvexport.php @@ -37,53 +37,121 @@ require_once("config.php"); if (!isset($sid)) {$sid=returnglobal('sid');} +if (!isset($action)) {$action = returnglobal('action');} -header("Content-Disposition: attachment; filename=vvexport_$sid.xls"); -header("Content-type: application/vnd.ms-excel"); -$s="\t"; - -$fieldmap=createFieldMap($sid, "full"); -$surveytable = "{$dbprefix}survey_$sid"; - -loadPublicLangFile($sid); - -$fldlist = mysql_list_fields($databasename, $surveytable); -$columns = mysql_num_fields($fldlist); -for ($i = 0; $i < $columns; $i++) +if (!$action == "export") { - $fieldnames[] = mysql_field_name($fldlist, $i); + echo $htmlheader; + echo "
+ + + + + + + + + + + + + + + + + +
VV Export
"._EXPORTSURVEY.":
+ Mode: + + +
  + +   +
["._B_ADMIN_BT."]
"; } +elseif (isset($sid) && $sid) + { + //Export is happening, first lets do the exporting + header("Content-Disposition: attachment; filename=vvexport_$sid.xls"); + header("Content-type: application/vnd.ms-excel"); + $s="\t"; + $fieldmap=createFieldMap($sid, "full"); + $surveytable = "{$dbprefix}survey_$sid"; -//Create the human friendly first line -$firstline=""; -$secondline=""; -foreach ($fieldnames as $field) - { - $fielddata=arraySearchByKey($field, $fieldmap, "fieldname", 1); - //echo "
";print_r($fielddata);echo "
"; - if (count($fielddata) < 1) {$firstline.=$field;} - else - {$firstline.=str_replace("\n", " ", str_replace("\t", " ", $fielddata['question']));} - $firstline .= $s; - $secondline .= $field.$s; - } -echo $firstline."\n"; -echo $secondline."\n"; -$query = "SELECT * FROM $surveytable"; -$result = mysql_query($query) or die("Error:
$query
".mysql_error()); - -while ($row=mysql_fetch_array($result)) - { + loadPublicLangFile($sid); + + $fldlist = mysql_list_fields($databasename, $surveytable); + $columns = mysql_num_fields($fldlist); + for ($i = 0; $i < $columns; $i++) + { + $fieldnames[] = mysql_field_name($fldlist, $i); + } + + + //Create the human friendly first line + $firstline=""; + $secondline=""; foreach ($fieldnames as $field) { - echo $row[$field].$s; + $fielddata=arraySearchByKey($field, $fieldmap, "fieldname", 1); + //echo "
";print_r($fielddata);echo "
"; + if (count($fielddata) < 1) {$firstline.=$field;} + else + {$firstline.=str_replace("\n", " ", str_replace("\t", " ", $fielddata['question']));} + $firstline .= $s; + $secondline .= $field.$s; + } + echo $firstline."\n"; + echo $secondline."\n"; + $query = "SELECT * FROM $surveytable"; + $result = mysql_query($query) or die("Error:
$query
".mysql_error()); + + while ($row=mysql_fetch_array($result)) + { + foreach ($fieldnames as $field) + { + echo $row[$field].$s; + } + echo "\n"; } - echo "\n"; + + //echo "
$firstline
"; + //echo "
$secondline
"; + //echo "
"; print_r($fieldnames); echo "
"; + //echo "
"; print_r($fieldmap); echo "
"; + + //Now lets finalised according to the "method" + if (!isset($method)) {$method=returnglobal('method');} + switch($method) + { + case "deactivate": //Deactivate the survey + $date = date('YmdHi'); //'Hi' adds 24hours+minutes to name to allow multiple deactiviations in a day + $result = mysql_list_tables($databasename); + while ($row = mysql_fetch_row($result)) + { + $tablelist[]=$row[0]; + } + if (in_array("{$dbprefix}tokens_{$_GET['sid']}", $tablelist)) + { + $toldtable="{$dbprefix}tokens_{$_GET['sid']}"; + $tnewtable="{$dbprefix}old_tokens_{$_GET['sid']}_{$date}"; + $tdeactivatequery = "RENAME TABLE $toldtable TO $tnewtable"; + $tdeactivateresult = mysql_query($tdeactivatequery) or die ("\n\n"._ERROR."Couldn't deactivate tokens table because:
".mysql_error()."

Survey was not deactivated either.

"._GO_ADMIN.""); + } + $oldtable="{$dbprefix}survey_{$_GET['sid']}"; + $newtable="{$dbprefix}old_{$_GET['sid']}_{$date}"; + $deactivatequery = "RENAME TABLE $oldtable TO $newtable"; + $deactivateresult = mysql_query($deactivatequery) or die ("\n\n"._ERROR."Couldn't deactivate because:
".mysql_error()."

Admin"); + break; + case "delete": //Delete the rows + break; + default: + + } // switch } -//echo "
$firstline
"; -//echo "
$secondline
"; -//echo "
"; print_r($fieldnames); echo "
"; -//echo "
"; print_r($fieldmap); echo "
"; ?> \ No newline at end of file diff --git a/admin/vvimport.php b/admin/vvimport.php index f7634eef393..26ef6337a34 100644 --- a/admin/vvimport.php +++ b/admin/vvimport.php @@ -44,16 +44,39 @@ { //PRESENT FORM echo $htmlheader; - echo "
- - - - - - - - -
Import a VV survey file
File:
Survey ID:
Exclude id?
 
"; + + //Make sure that the survey is active + $result = mysql_list_tables($databasename); + while ($row = mysql_fetch_row($result)) + { + $tablelist[]=$row[0]; + } + if (in_array("survey_$sid", $tablelist)) + { + echo "
+ + + + + + + + +
Import a VV survey file
File:
Survey ID:
Exclude id?
 
"; + } + else + { + echo "
+ + +
Import a VV survey file
+ Cannot import

+ This survey is not active. You must activate the survey before attempting to import a VVexport file.

+ ["._B_ADMIN_BT."] +
"; + } + + } else { @@ -80,8 +103,7 @@ $handle = fopen($the_full_file_path, "r"); while (!feof($handle)) { - //$buffer = fgets($handle, 1024); //Length parameter is required for PHP versions < 4.2.0 - $buffer = fgets($handle, 10240); //To allow for very long survey welcomes (up to 10k) + $buffer = fgets($handle, 10240); //To allow for very long lines (up to 10k) $bigarray[] = $buffer; } fclose($handle); @@ -99,22 +121,42 @@ $fieldcount--; } if ($noid == "noid") {unset($fieldnames[0]);} -// echo "FIELDS: Count - ".count($fieldnames); + + $fldlist = mysql_list_fields($databasename, $surveytable); + $columns = mysql_num_fields($fldlist); + for ($i = 0; $i < $columns; $i++) + { + $realfieldnames[] = mysql_field_name($fldlist, $i); + } + if ($noid == "noid") {unset($realfieldnames[0]);} unset($bigarray[1]); //delete the second line -// echo "[$noid]
"; +// echo "Import Fields:
"; print_r($fieldnames); echo "
"; +// echo "Actual Fields:
"; print_r($realfieldnames); echo '
'; + + //See if any fields in the import file don't exist in the active survey + $missing = array_diff($fieldnames, $realfieldnames); + if (is_array($missing) && count($missing) > 0) + { + foreach ($missing as $key=>$val) + { + $donotimport[]=$key; + unset($fieldnames[$key]); + } + } foreach($bigarray as $row) { if (trim($row) != "") { $fieldvalues=explode("\t", mysql_escape_string(str_replace("\n", "", $row)), $fieldcount+1); -// if (trim($fieldvalues[count($fieldvalues)-1]) == "") //Get rid of a blank entry at the end -// { -// unset($fieldvalues[count($fieldvalues)-1]); -// } -// echo "INSERT: Count - ".count($fieldvalues); -// echo "
";print_r($fieldvalues);echo "
"; + if (isset($donotimport)) + { + foreach ($donotimport as $not) + { + unset($fieldvalues[$not]); + } + } if ($noid == "noid") {unset($fieldvalues[0]);} $insert = "INSERT INTO $surveytable\n"; $insert .= "(".implode(", ", $fieldnames).")\n"; @@ -125,12 +167,14 @@ { echo "Failed insert:
$insert
".mysql_error(); } - //echo $insert."
\n"; + } } -// echo "
";print_r($bigarray);echo "
"; - + echo ""._SUCCESS." + File upload completed.

+ Note: Do NOT refresh this page

"; + echo "["._B_ADMIN_BT."]"; echo ""; } ?> \ No newline at end of file