/processes/codes.php

https://github.com/caprenter/IATI-Data-Spotter · PHP · 165 lines · 101 code · 14 blank · 50 comment · 18 complexity · 049f0ba36e88aeef6fc65f811347c444 MD5 · raw file

  1. <?php
  2. if (in_array($myinputs['group'],array_keys($available_groups))) {
  3. //Include variables for each group. Use group name for the argument
  4. //e.g. php detect_html.php dfid
  5. require_once 'variables/' . $_GET['group'] . '.php';
  6. require_once 'functions/xml_child_exists.php';
  7. $participating_org_ref_count = 0;
  8. $exclude = array("GB","EU");
  9. $bad_codes = array();
  10. $bad_files = array();
  11. include_once('helpers/parse_csv.php');
  12. //$unique_codes = array_keys($codes);
  13. //if (array_key_exists('41300', $codes)) {
  14. // echo "iuyyu";
  15. //}
  16. //die;
  17. //print_r($unique_codes);
  18. //die;
  19. if ($handle = opendir($dir)) {
  20. //echo "Directory handle: $handle\n";
  21. //echo "Files:\n";
  22. /* This is the correct way to loop over the directory. */
  23. while (false !== ($file = readdir($handle))) {
  24. if ($file != "." && $file != "..") { //ignore these system files
  25. //echo $file . PHP_EOL;
  26. //load the xml
  27. if ($xml = simplexml_load_file($dir . $file)) {;
  28. //print_r($xml); //debug
  29. foreach ($xml as $activity) {
  30. //CHECK: Participating Org code is on the code list
  31. foreach ($activity->{'participating-org'} as $participating_org) {
  32. $participating_org_ref = (string)$participating_org->attributes()->ref;
  33. if (!array_key_exists($participating_org_ref, $codes) && !in_array($participating_org_ref,$exclude)) {
  34. array_push($bad_codes,$participating_org_ref);
  35. array_push($bad_files,$url . $file);
  36. //echo $url . $file . PHP_EOL;
  37. $participating_org_ref_count ++;
  38. //continue 3;
  39. }
  40. }
  41. //CHECK: Participating org Code matches output text
  42. foreach ($activity->{'participating-org'} as $participating_org) {
  43. $participating_org_ref = (string)$participating_org->attributes()->ref;
  44. //if ($participating_org_ref == NULL) { $participating_org_ref = ""; }
  45. //echo $participating_org_ref . PHP_EOL;
  46. if (array_key_exists($participating_org_ref, $codes) && !in_array($participating_org_ref,$exclude)) {
  47. if ($codes[$participating_org_ref][2] == $participating_org[0]) {
  48. //echo "match";
  49. } else {
  50. //echo "mismatch";
  51. $expected = $codes[$participating_org_ref][2];
  52. $found = $participating_org[0];
  53. if ($participating_org_ref == NULL) { $participating_org_ref = "empty string"; } //no-ref given
  54. $rows .= '<tr><td>'. $participating_org_ref . '</td><td>' . $expected . '</td><td>' . $found . '</td><td>' . $file . '</td></tr>';
  55. //echo '"'. $participating_org_ref . '","' ;
  56. //echo $codes[$participating_org_ref][2] . '","';
  57. //echo $participating_org[0] . '","' . $file . '"' . PHP_EOL;
  58. }
  59. }
  60. //echo $participating_org_ref . " - " ;
  61. //echo $participating_org[0] . " - " . $file . PHP_EOL;
  62. }
  63. //print_r($activity);
  64. //die;
  65. //CHECK: Participating Org Code exists!
  66. /*foreach ($activity->{'participating-org'} as $participating_org) {
  67. $participating_org_ref = (string)$participating_org->attributes()->ref;
  68. if ($participating_org_ref == NULL) {
  69. echo $participating_org_ref . " - " ;
  70. echo $participating_org[0] . " - " . $file . PHP_EOL;
  71. }
  72. }
  73. }
  74. //print_r($activity);
  75. //die;
  76. */
  77. }
  78. } else { //simpleXML failed to load a file
  79. //echo $file . ' empty';
  80. }
  81. }// end if file is not a system file
  82. } //end while
  83. closedir($handle);
  84. }
  85. print('<div id="main-content">');
  86. //Print out a table of all the files that have a good file count
  87. print("
  88. <p class='table-title'>Table of mismatch &lt;participating-org&gt; code strings to found string.</p>
  89. <table id='table' class='sortable'>
  90. <thead>
  91. <tr>
  92. <th><h3>Partcipating Org Ref</h3></th>
  93. <th><h3>Expected</h3></th>
  94. <th><h3>Found</h3></th>
  95. <th><h3>File</h3></th>
  96. </tr>
  97. </thead>
  98. <tbody>
  99. ");
  100. echo $rows;
  101. print("</tbody>
  102. </table>");
  103. print("
  104. <p class='table-title'>Table of &lt;participating-org&gt; codes not on code lists.</p>
  105. <p>Occurances: " . count($bad_codes). " from " . count(array_unique($bad_files)) . " affected files.</p>
  106. <table id='table2' class='sortable'>
  107. <thead>
  108. <tr>
  109. <th><h3>Code</h3></th>
  110. </tr>
  111. </thead>
  112. <tbody>
  113. ");
  114. $bad_codes = array_unique($bad_codes);
  115. sort($bad_codes);
  116. foreach ($bad_codes as $code) {
  117. echo "<tr><td>" . $code ."</td></tr>";
  118. }
  119. print("</tbody>
  120. </table>");
  121. //echo $participating_org_ref_count;
  122. //$bad_codes = array_unique($bad_codes);
  123. //sort($bad_codes);
  124. //print_r($bad_codes). PHP_EOL;
  125. //print_r(array_unique($bad_files)). PHP_EOL;
  126. //echo count(array_unique($bad_files)). PHP_EOL;
  127. print('<div class="notes"><p>Excluded codes:</p>
  128. <ul>');
  129. foreach ($exclude as $ex) {
  130. echo "<li>" . $ex ."</li>";
  131. }
  132. print('</ul>
  133. </div>');
  134. print('</div>');
  135. }
  136. ?>
  137. <script type="text/javascript" src="javascript/tinytable/script.js"></script>
  138. <script type="text/javascript">
  139. var sorter = new TINY.table.sorter("sorter");
  140. sorter.head = "head";
  141. sorter.asc = "asc";
  142. sorter.desc = "desc";
  143. sorter.even = "evenrow";
  144. sorter.odd = "oddrow";
  145. sorter.evensel = "evenselected";
  146. sorter.oddsel = "oddselected";
  147. sorter.paginate = true;
  148. sorter.currentid = "currentpage";
  149. sorter.limitid = "pagelimit";
  150. sorter.init("table");
  151. </script>