function get_link_table_schema($link_tablename, $linked_column, $linked_value, $record_type_id, $row_id) { // TODO: Make similar logic change for layer schema $tab=""; if($record_type_id != NULL) { if($record_type_id == 0) { die(""); } else { $link_info=GetTableColsOfType($link_tablename, TABLE_TYPE_LINK, 'mandatory'); $mandatory_columns=$link_info['mandatory_columns']; $query="select record_columns from mlocate_data_record_types where id = %d"; $obj=db_fetch_object(db_query($query, $record_type_id)); $record_columns=$obj->record_columns; $columns=$mandatory_columns . ',' . $record_columns; if($columns[0] == ',') { $columns=substr($columns, 1); } elseif($columns[strlen($columns) - 1] == ',') { $columns=substr($columns, 0, - 1); } if($columns != '') { $query=GetQueryColumnDetails($link_tablename, $columns, FALSE); $result_cols=db_query($query[0], $query[1]); if($row_id != NULL) { $query='select %s from "%s" where id = %d'; $query_args=array(str_replace("'", '"', $columns), $link_tablename, $row_id); $result_link_row=db_query($query, $query_args); $vals_link_row=db_fetch_array($result_link_row); $tab=GetDBTableSchemaTable($link_tablename, TABLE_TYPE_LINK, $result_cols, $vals_link_row); } else { $tab=GetDBTableSchemaTable($link_tablename, TABLE_TYPE_LINK, $result_cols); } } else { die("Editable columns have not been specified for this record type. Please contact the admin."); } $query="select record_type_column from \"Meta_LinkTable\" where link_tablename = '%s'"; $obj=db_fetch_object(db_query($query, $link_tablename)); $record_type_column=str_replace("'", "", $obj->record_type_column); $query="select record_type from mlocate_data_record_types where id = %d"; $obj=db_fetch_object(db_query($query, $record_type_id)); $record_type_name=$obj->record_type; $hidden.="<input type='hidden' name='edit-{$record_type_column}' value='{$record_type_name}'>"; /* Hardcoding for India Birds - Trip Record */ if(ereg("lnk_[0-9]+_india_birdsightings", $link_tablename) && $record_type_name == "Trip Record") { $hidden.="<input id='btnAddSpecies' type='button' value='Add species' onClick='javascript:clearSpeciesForAdd(\"divRecordType\")' disabled='true'><br>"; } } } else { $record_types=array(); $query="select id, record_type, record_columns from mlocate_data_record_types where tablename = '%s' and table_type = '" . TABLE_TYPE_LINK . "' order by record_type"; $result=db_query($query, $link_tablename); $i=0; while($obj=db_fetch_object($result)) { $record_types[$i]['record_type_id']=$obj->id; $record_types[$i]['record_type']=$obj->record_type; $record_types[$i]['record_columns']=$obj->record_columns; $i++; } if(sizeof($record_types) == 0) { // Its simple data with no special record types. $link_info=GetTableColsOfType($link_tablename, TABLE_TYPE_LINK, 'mandatory'); $mandatory_columns=$link_info['mandatory_columns']; $link_info=GetTableColsOfType($link_tablename, TABLE_TYPE_LINK, 'editable'); $editable_columns=$link_info['editable_columns']; if($editable_columns == '') { die("No Editable Columns specified"); } else { $query=GetQueryColumnDetails($link_tablename, $editable_columns, FALSE); $result_cols=db_query($query[0], $query[1]); if($row_id != NULL) { $query='select %s from "%s" where id = %d'; $query_args=array(str_replace("'", '"', $editable_columns), $link_tablename, $row_id); $result_link_row=db_query($query, $query_args); $vals_link_row=db_fetch_array($result_link_row); $tab=GetDBTableSchemaTable($link_tablename, TABLE_TYPE_LINK, $result_cols, $vals_link_row); } else { $tab=GetDBTableSchemaTable($link_tablename, TABLE_TYPE_LINK, $result_cols); } } } else { // The data has record types. $lnk=$_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'] . "&record_type_id="; $options="<select onchange=\"javascript:getPopupAjaxDiv(jQuery('#divRecordType')[0], '$lnk'+this.value, maskInputs)\">"; $options.="<option value='0'>Select Record Type</option>"; foreach($record_types as $type) { $record_type_id=$type['record_type_id']; $record_type=$type['record_type']; $options.="<option value='{$record_type_id}'>$record_type</option>"; } $options.="</select>"; $html=$options . '<div id="divRecordType"></div>'; die($html); } } $hidden.="<input type='hidden' name='link_tablename' value='{$link_tablename}'>"; $hidden.="<input type='hidden' name='edit-{$linked_column}' value='{$linked_value}'>"; $hidden.="<input type='hidden' name='edit-__id' id='edit-__id' value='$id'>"; $html="<div id='form_mlocate_error' class='error'></div>"; $html.=$tab; $resp=""; $resp.="<form id='frmLinkSave' method='post' action='{$base_path}save_info.php?action=saveLinkData'>"; $resp.=$html . $hidden; $resp.="<input id='btnSubmit' type='button' value='Submit' onClick='javascript:submitForm(\"#frmLinkSave\",\"#form_mlocate_error\")'>"; $resp.="</form>"; echo $resp; }
function getDBColDesc($tablename, $cols = NULL, $column_type = null, $onlyDesc = TRUE) { $col_info = array(); $query = GetQueryColumnDetails($tablename, $cols, $column_type, $onlyDesc); $result_cols = db_query($query[0], $query[1]); if(!$result_cols) { } else { if($onlyDesc) { while($col = db_fetch_array($result_cols)) { $col_info[$col['column_name']] = $col['column_description']; } } else { while($col = db_fetch_array($result_cols)) { $col_name = $col['column_name']; $col_info[$col_name]['description'] = $col['column_description']; $col_info[$col_name]['type'] = $col['column_type']; } } } return $col_info; }
function runtimeSave($tablename, $table_type, $topology = NULL) { $tbl_cols = array(); $query = GetQueryColumnDetails($tablename, NULL, FALSE); $result_cols = db_query($query[0], $query[1]); if(!$result_cols) { } else { while($obj = db_fetch_object($result_cols)) { $tbl_cols[$obj->column_name]['col_type'] = $obj->column_type; $tbl_cols[$obj->column_name]['col_null'] = $obj->column_null; $tbl_cols[$obj->column_name]['col_desc'] = $obj->column_description; } } $tbl_cols_names = array_keys($tbl_cols); global $user; $fields = array(); foreach($_REQUEST as $key => $value) { if(substr($key, 0, 5) == 'edit-') { $value_encoded = htmlentities(str_replace("'", "''", $value)); $col = substr($key, 5); if(in_array($col, $tbl_cols_names)) { $col_type = $tbl_cols[$col]['col_type']; switch($col_type) { case 'smallint': case 'int': case 'integer': case 'bigint': case 'serial': case 'bigserial': $val = $value_encoded; if($value_encoded == '') { if($tbl_cols[$col]['col_null'] == 'NOT NULL') { die($tbl_cols[$obj->column_name]['col_desc'] . " cannot be empty."); } $val = 'NULL'; } $fields[$col] = $val; break; default: $val = $value_encoded; if($value_encoded == '') { if($tbl_cols[$col]['col_null'] == 'NOT NULL') { die($tbl_cols[$obj->column_name]['col_desc'] . " cannot be empty."); } $val = 'NULL'; $fields[$col] = $val; }else{ $fields[$col] = "'".$val."'"; } break; } } else { $fields[$col] = "'".$value_encoded."'"; } } } if($fields['__id'] == "''") { unset($fields['__id']); } /* hardcoding for India Birds */ if($table_type == TABLE_TYPE_LINK && ereg("lnk_[0-9]+_india_birdsightings", $tablename)) { $query = "select mlocate_id from birdspecies_list where c_name = '%s'"; $result = db_query($query, str_replace("'", "", $fields['name'])); if(!$result) { } else { $obj = db_fetch_object($result); if($obj->mlocate_id) { $fields['mlocate_id'] = $obj->mlocate_id; } } } $passed_fields = $fields; $fields[AUTO_DBCOL_PREFIX.'modified_by'] = $user->uid; $fields[AUTO_DBCOL_PREFIX.'modified_date'] = 'now()'; $op = ""; if(!isset($fields['__id'])) { $fields[AUTO_DBCOL_PREFIX.'created_by'] = $user->uid; $fields[AUTO_DBCOL_PREFIX.'created_date'] = 'now()'; if($topology != NULL) { //check if the topology type of the table is multipoint or multipolygon $geomtypequery = "select type,srid from geometry_columns where f_table_name = '%s'"; $geomtyperesult = db_query($geomtypequery, $tablename); while ($row= db_fetch_object($geomtyperesult)) { $geomtype = $row->type; $srid = $row->srid; } if(strpos($geomtype,'MULTI') === false){ //return false if the word is not found //here if not multipoint or multipolygon $fields[AUTO_DBCOL_PREFIX.'topology'] = "geomfromText('{$topology}',".$srid.")"; } else{ //type is multipoint or multipolygon $topology_edited = editGeomType($topology); $fields[AUTO_DBCOL_PREFIX.'topology'] = "geomfromText('{$topology_edited}',".$srid.")"; } } //if India Bird Layer then get assign the site_id while adding the point if(ereg("lyr_[0-9]+_india_birdlocations", $tablename)){ $query1 = 'select max(site_id) as site_id from "%s"' ; $query_linktb = 'select link_tablename from "Meta_LinkTable" where layer_id = (select layer_id from "Meta_Layer" where layer_tablename = \'%s\')'; $result = db_query($query1, $tablename); if(!$result) { //Error occured die('Error fetching max value'); } else { while($obj = db_fetch_object($result)) { $lyr_max = $obj->site_id; } } $result = db_query($query_linktb, $tablename); if(!$result) { //Error occured die('Error fetching link tablename'); } else { while($obj = db_fetch_object($result)) { $lnk_tbname = $obj->link_tablename; } } $query2 = 'select max(site_id) as site_id from "%s"'; $result = db_query($query2, $lnk_tbname); if(!$result) { //Error occured die('Error fetching max value'); } else { while($obj = db_fetch_object($result)) { $lnk_max = $obj->site_id; } } if($lyr_max > $lnk_max) $site_id = $lyr_max + 1; else $site_id = $lnk_max + 1; $query = 'insert into "%s" (%s, site_id) values ('.implode(",",array_values($fields)).' ,%d)'; $query_args = array($tablename, implode(",",array_keys($fields)), $site_id); } else { $query = 'insert into "%s" (%s) values ('.implode(",",array_values($fields)).')'; $query_args = array($tablename, implode(",",array_keys($fields))); } $op = "insert"; } else { $id = $fields['__id']; unset($fields['__id']); $set_val = ""; foreach($fields as $key => $value) { if($key == "layer_id" ) { $key = AUTO_DBCOL_PREFIX.$key; } $set_val .= " $key = $value,"; } $set_val = substr($set_val, 0, -1); $query = 'update "%s" set '.$set_val.' where '.AUTO_DBCOL_PREFIX.'id = %d'; $query_args = array($tablename, $id); $op = "update"; } $result = db_query($query, $query_args); if(!$result) { echo "Error saving info."; } else { notify_layer_admin($tablename, $table_type, $passed_fields); echo "Record saved."; if($table_type == TABLE_TYPE_LAYER && $op == 'insert') { setcookie('featureAddedTo', $tablename); } } }