PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/export.php

https://bitbucket.org/geoffwong/airscore
PHP | 372 lines | 265 code | 43 blank | 64 comment | 28 complexity | 9c12570dd64d172636fddc1d28ad5eb8 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. require 'authorisation.php';
  3. $debug = 0;
  4. // mapping one field to another name for insert ...
  5. // valmap is original table key => val pairs
  6. // map gives field mapping name changes or absolute values
  7. // or "-" exclude the field
  8. function insert_map($table,$map,$valmap)
  9. {
  10. $sql = "insert into $table (";
  11. $count = 0;
  12. $fieldarr = [];
  13. #print_r($valmap);
  14. foreach ($valmap as $key => $val)
  15. {
  16. if (key_exists($key, $map))
  17. {
  18. $altkey = $map[$key];
  19. if (is_numeric($altkey))
  20. {
  21. $fieldarr[] = $key;
  22. $valarr[] = $altkey;
  23. }
  24. elseif ($altkey != "-")
  25. {
  26. $fieldarr[] = $altkey;
  27. if (!$val || is_numeric($val))
  28. {
  29. $valarr[] = 0 + $val;
  30. }
  31. else
  32. {
  33. $valarr[] = "'$val'";
  34. }
  35. }
  36. }
  37. else
  38. {
  39. $fieldarr[] = $key;
  40. if (!$val || is_numeric($val))
  41. {
  42. $valarr[] = 0 + $val;
  43. }
  44. else
  45. {
  46. $valarr[] = "'$val'";
  47. }
  48. }
  49. }
  50. $sql = $sql . join(",",$fieldarr) . ") values (" . join(",", $valarr) . ")";
  51. return $sql;
  52. }
  53. $comPk=reqival('comPk');
  54. if ($debug)
  55. {
  56. $comPk = 7;
  57. }
  58. $link = db_connect();
  59. $usePk = check_auth('system');
  60. $link = db_connect();
  61. $isadmin = is_admin('admin',$usePk,$comPk);
  62. # nuke normal header - for exported race file
  63. #header("Content-type: text/plain");
  64. #header("Cache-Control: no-store, no-cache");
  65. #AXXXZZZGPSBabel
  66. #HFDTE040207
  67. #HFPLTPILOT:Unknown
  68. # insert new competition .. get hgfa_ladder.comPk
  69. # check if it exists .. if so update/delete other stuff ..
  70. $sql = "select comName,comType,comLocation,comDateFrom,comDateTo,comMeetDirName,comTimeOffset,comSanction,forPk from xcdb.tblCompetition where comPk=$comPk";
  71. print $sql;
  72. $result = mysql_query($sql, $link) or die("can't select comp details");
  73. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  74. $cname = $row['comName'];
  75. $ctype = $row['comType'];
  76. $cloc = $row['comLocation'];
  77. $cfrom = $row['comDateFrom'];
  78. $cto = $row['comDateTo'];
  79. $cdirname = $row['comMeetDirName'];
  80. $coffset = $row['comTimeOffset'];
  81. $csanction = $row['comSanction'];
  82. $forPk = $row['forPk'];
  83. $sql = "select comPk from hgfa_ladder.tblCompetition where comName='$cname' and comDateFrom='$cfrom'";
  84. $result = mysql_query($sql, $link) or die("can't select comp details" . mysql_error() . "\n");
  85. $HcomPk = 0;
  86. if (mysql_num_rows($result) > 0)
  87. {
  88. $HcomPk = 0 + mysql_result($result,0,0);
  89. }
  90. # if we have one .. then update
  91. if ($HcomPk == 0)
  92. {
  93. $sql = "insert into hgfa_ladder.tblCompetition (comName,comLocation,comDateFrom,comDateTo,comMeetDirName,sanValue) values ('$cname','$cloc','$cfrom','$cto','$cdirname','$csanction')";
  94. $result = mysql_query($sql, $link) or die("can't insert new competition: " . mysql_error() . "\n");
  95. $HcomPk = mysql_insert_id();
  96. }
  97. else
  98. {
  99. $sql = "update hgfa_ladder.tblCompetition set comLocation='$cloc', comDateTo='$cto',comMeetDirName='$cdirname',sanValue='$csanction' where comPk=$HcomPk";
  100. $result = mysql_query($sql, $link) or die("can't update competition" . mysql_error() . "\n");
  101. }
  102. if ($HcomPk == 0)
  103. {
  104. exit(1);
  105. }
  106. # Formula?
  107. $sql = "select * from xcdb.tblFormula where forPk=$forPk";
  108. $result = mysql_query($sql,$link) or die("Unable to find formula: " . mysql_error() . "\n");
  109. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  110. $fgoal = $row['forNomGoal'];
  111. $fmdist = $row['forMinDistance'];
  112. $fndist = $row['forNomDistance'];
  113. $fntime = $row['forNomTime'];
  114. $sql = "select forPk from hgfa_ladder.tblFormulaCompetition where
  115. forNomGoal=$fgoal and forMinDistance=$fmdist and forNomDistance=$fndist and forNomTime=$fntime";
  116. $result = mysql_query($sql,$link) or die("Unable to select formula: " . mysql_error() . "\n");
  117. if (mysql_num_rows($result) > 0)
  118. {
  119. $HforPk=mysql_result($result,0,0);
  120. }
  121. else
  122. {
  123. $hntime = $fntime / 60;
  124. $fparam = "ÿþ $fmdist; $fndist; $fgoal; $hntime; 1; 2000; 1";
  125. $sql = "insert into hgfa_ladder.tblFormulaCompetition (forParameter,forNomGoal,forMinDistance,forNomDistance,forNomTime) values ('$fparam',$fgoal, $fmdist, $fndist, $fntime)";
  126. $result = mysql_query($sql,$link) or die("Unable to insert formula: " . mysql_error() . "\n");
  127. $HforPk = mysql_insert_id();
  128. }
  129. $sql = "update hgfa_ladder.tblCompetition set comFormulaID=$HforPk where comPk=$HcomPk";
  130. $result = mysql_query($sql,$link) or die("Unable to update comp formula: " . mysql_error() . "\n");
  131. # make pilPk map
  132. if ($ctype == 'Team-RACE')
  133. {
  134. print "<br>Com-type=$ctype<br>";
  135. $sql ="SELECT P.pilPk, P.pilHGFA, P.pilFirstName as fname, P.pilLastName as lname, P.pilSex as sex,HP.pilPk as HpilPk,HP.pilLastName,HP.pilFirstName,HP.pilIDGlobal from tblTeamResult TR, tblTeam TM, tblTeamPilot TP, tblTask T, tblPilot P left outer join hgfa_ladder.tblPilot HP on HP.pilIDGlobal=P.pilHGFA where TM.comPk=$comPk and TP.teaPk=TM.teaPk and P.pilPk=TP.pilPk and T.tasPk=TR.tasPk and T.comPk=$comPk group by P.pilPk";
  136. }
  137. else
  138. {
  139. $sql ="SELECT P.pilPk, P.pilHGFA, P.pilNationCode, P.pilFirstName as fname, P.pilLastName as lname, P.pilSex as sex,HP.pilPk as HpilPk,HP.pilLastName,HP.pilFirstName,HP.pilIDGlobal from tblTaskResult TR, tblTrack TL, tblTask T, tblPilot P left outer join hgfa_ladder.tblPilot HP on HP.pilIDGlobal=P.pilHGFA where TL.traPk=TR.traPk and P.pilPk=TL.pilPk and T.tasPk=TR.tasPk and T.comPk=$comPk group by P.pilPk";
  140. }
  141. $result = mysql_query($sql,$link) or die("Unable to find pilots: " . mysql_error() . "\n");
  142. $pilmap = [];
  143. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  144. {
  145. #echo "pilmap: " . $row['pilPk'] . " => " . $row['HpilPk'] . "<br>";
  146. $pilmap[$row['pilPk']] = $row;
  147. }
  148. # insert missing pilots
  149. foreach ($pilmap as $pilPk => $row)
  150. {
  151. if ($row['HpilPk'] == 0)
  152. {
  153. $fname = $row['fname'];
  154. $lname = $row['lname'];
  155. print "add missing pilot $fname $lname\n";
  156. $fai = $row['pilHGFA'];
  157. $natcode = $row['pilNationCode'];
  158. $oriPk = 2;
  159. if ($natcode != '')
  160. {
  161. $sql = "select O.* from tblOrigin O, tblNation N where O.natPk=N.natPk and N.natCode3='$natcode' order by oriPk limit 1";
  162. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  163. $oriPk = 0 + $row['oriPk'];
  164. if ($oriPk == 0)
  165. {
  166. $oriPk = 2;
  167. }
  168. }
  169. if ($row['sex'] == 'F')
  170. {
  171. $sex = 0;
  172. }
  173. else
  174. {
  175. $sex = 1;
  176. }
  177. $sql = "insert into hgfa_ladder.tblPilot (pilIDGlobal, pilFirstName, pilLastName, pilSex, oriPk, gliPk) values ('$fai', '$fname', '$lname', $sex, $oriPk, 267)";
  178. $result = mysql_query($sql,$link) or die("Unable to insert pilot: " . mysql_error() . "\n");
  179. $HpilPk = mysql_insert_id();
  180. $pilmap[$pilPk]['HpilPk'] = $HpilPk;
  181. }
  182. }
  183. # insert new tasks / make tasPk map
  184. $taskmap = [];
  185. $sql = "select comPk, tasPk, tasDate, tasName, tasResultsType, (tasDistance/1000) as tasDistance, tasSSDistance, tasSSOpen, tasSSClose, tasESClose, (tasTotalDistanceFlown/1000) as tasTotalDistanceFlown, tasQuality, tasPilotsLaunched, tasPilotsTotal, (tasMaxDistance/1000) as tasMaxDistance, tasFastestTime from xcdb.tblTask where comPk=$comPk";
  186. $result = mysql_query($sql,$link) or die("Unable to select tasks: " . mysql_error() . "\n");
  187. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  188. {
  189. // make a tasPk map ..
  190. $taskmap[$row['tasPk']] = $row;
  191. }
  192. # delete all existing tasks related to this competition?
  193. $sql = "delete from hgfa_ladder.tblTask where comPk=$HcomPk";
  194. $result = mysql_query($sql,$link) or die("Unable to delete old tasks: " . mysql_error() . "\n");
  195. $rowmap = array(
  196. 'tasMaxDistance' => 'tasTopDistance',
  197. 'tasFastestTime' => '-',
  198. 'comPk' => $HcomPk,
  199. 'tasPk' => '-'
  200. );
  201. foreach ($taskmap as $tasPk => $row)
  202. {
  203. $row['tasStartType'] = 'AIR';
  204. $row['tasResultsType'] = 'COMPUTE';
  205. if (0 + $row['tasFastestTime'] > 0)
  206. {
  207. $row['tasTopSpeed'] = $row['tasDistance'] * 3600 / $row['tasFastestTime'];
  208. }
  209. else
  210. {
  211. $row['tasTopSpeed'] = 0;
  212. }
  213. # topscore?
  214. $sql = "select max(tarScore) as maxScore from tblTaskResult where tasPk=$tasPk";
  215. $result = mysql_query($sql,$link) or die("Unable to get max score " . mysql_error() . "\n");
  216. $maxscore = mysql_result($result,0,0);
  217. $row['tasTopScore'] = $maxscore;
  218. $row['tasTopOzScore'] = $maxscore;
  219. $sql = insert_map("hgfa_ladder.tblTask", $rowmap, $row);
  220. $result = mysql_query($sql,$link) or die("Unable to insert tasks " . mysql_error() . "\n");
  221. $HtasPk = mysql_insert_id();
  222. $taskmap[$tasPk]['HtasPk'] = $HtasPk;
  223. }
  224. # TaskWaypoints / hgfa_ladder.tblTurnpoint
  225. #CREATE TABLE tblTaskTurnpoint
  226. # ttpPk Integer not null PRIMARY KEY auto_increment,
  227. # tasPk Integer not null,
  228. # trnPk Integer not null,
  229. # ttpNr Integer not null,
  230. # ttpNrText varchar(8),
  231. # ttpTimeGateType Text,
  232. # ttpDistanceDiff Double,
  233. # ttpDistanceTotal Double,
  234. # ttpAngle Double,
  235. # ttpCircle Double
  236. #CREATE TABLE tblTurnpoint
  237. # trnPk Integer not null PRIMARY KEY auto_increment,
  238. # trnID varchar(12),
  239. # trnName varchar(100),
  240. # trnAbbr varchar(40),
  241. # trnPositionType varchar(6),
  242. # trnPositionLatitudeDecimal Double,
  243. # trnPositionLatitudeDirection varchar(2),
  244. # trnPositionLatitudeDegree Double,
  245. # trnPositionLatitudeMinute Double,
  246. # trnPositionLatitudeSecond Double,
  247. # trnPositionLongitudeDecimal Double,
  248. # trnPositionLongitudeDirection varchar(2),
  249. # trnPositionLongitudeDegree Double,
  250. # trnPositionLongitudeMinute Double,
  251. # trnPositionLongitudeSecond Double,
  252. # trnPositionEastings Double,
  253. # trnPositionNorthings Double,
  254. # trnPositionAltitude Double,
  255. # trnPositionMapZone Integer,
  256. # geoPk Integer,
  257. # oriPk Integer
  258. $taskkeys = array_keys($taskmap);
  259. $taskvals = implode(",", $taskkeys);
  260. $sql = "select * from tblTaskWaypoint T, tblRegionWaypoint R where T.rwpPk=R.rwpPk and T.tasPk in ($taskvals)";
  261. $result = mysql_query($sql,$link) or die("Unable to fetch waypoints: " . mysql_error() . "\n");
  262. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  263. {
  264. $taskpoints = $row;
  265. }
  266. # FIX this up.
  267. #foreach ($taskpoints as $resPk => $row)
  268. #{
  269. #}
  270. # delete all existing tasks related to this competition?
  271. $sql = "delete from hgfa_ladder.tblResult where comPk=$HcomPk";
  272. $result = mysql_query($sql,$link) or die("Unable to delete old results: " . mysql_error() . "\n");
  273. # Actual results ..
  274. if ($ctype == 'Team-RACE')
  275. {
  276. $sql = "select TP.pilPk,T.tasPk,T.terResultType as tarResultType,T.terTurnpoints as tarTurnpoints,TIMESTAMPADD(SECOND,T.terSS,TK.tasDate) as tarSS,TIMESTAMPADD(SECOND,T.terES,TK.tasDate) as tarES,T.terPenalty as tarPenalty,T.terComment as tarComment,round(T.terScore) as tarScore,T.terPlace as tarPlace,T.terSpeed as tarSpeed,(T.terDistance/1000) as tarDistance,T.terArrival as tarArrival,T.terDeparture as tarDeparture,T.terSpeedScore as tarSpeedScore,T.terDistanceScore as tarDistanceScore,T.terLeadingCoeff as tarLeadingCoeff from xcdb.tblTeamResult T, xcdb.tblTeam TM, xcdb.tblTeamPilot TP, xcdb.tblTask TK where T.teaPk=TM.teaPk and TM.comPk=$comPk and TP.teaPk=TM.teaPk and TK.tasPk=T.tasPk and T.tasPk in (select tasPk from xcdb.tblComTaskTrack where comPk=$comPk)";
  277. }
  278. else
  279. {
  280. $sql = "select TL.pilPk,T.tasPk,T.tarResultType,T.tarTurnpoints,TIMESTAMPADD(SECOND,T.tarSS,TK.tasDate) as tarSS,TIMESTAMPADD(SECOND,T.tarES,TK.tasDate) as tarES,T.tarPenalty,T.tarComment,round(T.tarScore) as tarScore,T.tarPlace,T.tarSpeed,(T.tarDistance/1000) as tarDistance,T.tarArrival,T.tarDeparture,T.tarSpeedScore,T.tarDistanceScore,T.tarLeadingCoeff from xcdb.tblTaskResult T, xcdb.tblTrack TL, xcdb.tblTask TK where TL.traPk=T.traPk and TK.tasPk=T.tasPk and T.tasPk in (select tasPk from xcdb.tblComTaskTrack where comPk=$comPk)";
  281. }
  282. $result = mysql_query($sql,$link) or die("Unable query task results: " . mysql_error() . "\n");
  283. $taskresult = [];
  284. $count = 1;
  285. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  286. {
  287. $taskresult[$count++] = $row;
  288. }
  289. $rowmap = array(
  290. 'tarPk' => '-',
  291. 'tarTurnpoints' => 'resTurnpointsReached',
  292. 'tarPenalty' => 'resPenaltyValue',
  293. 'tarComment' => 'resComment',
  294. 'tarSS' => 'resSSTime',
  295. 'tarES' => 'resESTime',
  296. 'tarSpeed' => 'resSpeed',
  297. 'tarDistance' => 'resDistance',
  298. 'tarArrival' => 'resArrivalScore',
  299. 'tarDeparture' => 'resDepartureScore',
  300. 'tarSpeedScore' => 'resSpeedScore',
  301. 'tarDistanceScore' => 'resDistanceScore',
  302. 'tarScore' => 'resScore',
  303. 'tarPlace' => 'resPlace',
  304. 'tarResultType' => 'resStatus',
  305. 'tarLeadingCoeff' => 'resLeadingCoeff'
  306. );
  307. #print_r($taskmap);
  308. foreach ($taskresult as $resPk => $row)
  309. {
  310. echo "add result for: " . $row['pilPk'] . "<br>";
  311. $row['pilPk'] = $pilmap[$row['pilPk']]['HpilPk'];
  312. $row['tasPk'] = $taskmap[$row['tasPk']]['HtasPk'];
  313. $row['comPk'] = $HcomPk;
  314. if (0 + $row['tarGoal'] > 0)
  315. {
  316. $row['resReachedES'] = 1;
  317. }
  318. else
  319. {
  320. $row['resReachedES'] = 0;
  321. }
  322. // Need to convert tasPks & pilPks
  323. $sql = insert_map("hgfa_ladder.tblResult", $rowmap, $row);
  324. print $sql . "\n";
  325. $result = mysql_query($sql,$link) or die("Unable to insert result " . mysql_error() . "\n");
  326. }
  327. ?>