/trans_peep.php

https://github.com/sjorz/property-exporter · PHP · 233 lines · 127 code · 49 blank · 57 comment · 7 complexity · 32343ac05f37e978449c075b65c8e807 MD5 · raw file

  1. <?php
  2. //**********************************************************************
  3. //
  4. // View what is in the transaction log.
  5. //
  6. // History:
  7. // -------
  8. // 1.0 12Sep11 GB Original
  9. //
  10. //**********************************************************************
  11. require_once "table.php";
  12. require_once "sql.php";
  13. //**********************************************************************
  14. //
  15. // Some helpers.
  16. //
  17. //**********************************************************************
  18. function error ($msg)
  19. {
  20. echo ($msg);
  21. }
  22. function warning ($msg)
  23. {
  24. echo ($msg);
  25. }
  26. function info ($msg)
  27. {
  28. echo ($msg);
  29. }
  30. //**********************************************************************
  31. //
  32. // Print JSON encoded array of rows
  33. //
  34. //**********************************************************************
  35. function toJson($rows)
  36. {
  37. return json_encode($rows);
  38. }
  39. //**********************************************************************
  40. //
  41. // Print array of rows
  42. //
  43. //**********************************************************************
  44. function printRows($rows)
  45. {
  46. $nRows = 0;
  47. foreach ($rows as $row)
  48. {
  49. $nRows++;
  50. printCols($row);
  51. }
  52. }
  53. function printCols($row)
  54. {
  55. $nCols = 0;
  56. $colNames = array_keys ($row);
  57. foreach ($row as $col)
  58. {
  59. if ($nCols > 0)
  60. echo "|";
  61. echo (sprintf ("%s=>%s", $colNames[$nCols], $col));
  62. $nCols++;
  63. }
  64. echo "\n";
  65. }
  66. //**********************************************************************
  67. //
  68. // Delete one property
  69. //
  70. //**********************************************************************
  71. function remove ($pid)
  72. {
  73. info (sprintf ("Remove property %d", $pid));
  74. printf ("%s\n", toJson (array ("legacy_property_id" => $pid)));
  75. }
  76. //**********************************************************************
  77. //
  78. // Process one property
  79. //
  80. //**********************************************************************
  81. function process ($pid)
  82. {
  83. info (sprintf ("Update property %d", $pid));
  84. $propertyTable = new Table ('dbo.Property');
  85. $addressTable = new Table ('dbo.Address');
  86. $personTable = new Table ('dbo.Person');
  87. $photoTable = new Table ('dbo.PropertyImages');
  88. $inspectionTable = new Table ('dbo.Inspections');
  89. $inspectionDetailsTable = new Table ('dbo.InspectionDetails');
  90. $featureRelTable = new Table ('dbo.PropertyFeatureRel');
  91. // Read the property. We expect only one row.
  92. $propertyTable->executeSQL (getSqlForProperty($pid));
  93. $propertyRows = $propertyTable->asArray();
  94. // The description and byline may contain non-utf8 if the user wrote it
  95. // using MS Word. The json encoder cannot handle that, so we need to force
  96. // utf8 encoding:
  97. if (count($propertyRows) == 0)
  98. {
  99. info ("Property not found");
  100. return;
  101. }
  102. $personTable->executeSQL (getSqlForContact ($pid));
  103. $contactRows = $personTable->asArray();
  104. $personTable->executeSQL (getSqlForLandlord ($pid));
  105. $landlordRows = $personTable->asArray();
  106. //echo "LANDLORD\n";
  107. //print_r (array_values ($landlordRows));
  108. $personTable->executeSQL (getSqlForCompany ($pid));
  109. $companyRows = $personTable->asArray();
  110. //echo "COMPANY\n";
  111. //print_r (array_values ($companyRows));
  112. // Get the photo table entries
  113. $photoTable->executeSQL (getSqlForPhotos($pid));
  114. $photoRows = $photoTable->asArray();
  115. // Get the inspection table entries
  116. $inspectionTable->read (sprintf ("PropertyId=%d", $pid));
  117. $inspectionRows = $inspectionTable->asArray();
  118. // Get the inspection detail table entries
  119. $inspectionDetailsTable->read (sprintf ("intPropertyId=%d", $pid));
  120. $inspectionDetailsRows = $inspectionDetailsTable->asArray();
  121. // Get the features detail table entries
  122. $featureRelTable->executeSQL (getSqlForFeatures($pid));
  123. $featureRelRows = $featureRelTable->asArray();
  124. // Get the inspection date/times
  125. $inspectionTable->executeSQL (getSqlForInspectionTimes($pid));
  126. $inspectionTimes = $inspectionTable->asArray();
  127. $theProperty = array_shift ($propertyRows);
  128. // If there are no properties with id $pid the property is no
  129. // longer there, skip.
  130. if (isset ($theProperty ['byline']))
  131. $theProperty ['byline'] =
  132. utf8_encode ( $theProperty ['byline']);
  133. if (isset ($theProperty ['description']))
  134. $theProperty ['description'] =
  135. utf8_encode ( $theProperty ['description']);
  136. //print_r ($theProperty);
  137. $theProperty ['contact'] = array_shift ($contactRows);
  138. if (count ($landlordRows) > 0)
  139. {
  140. $theProperty ['landlord'] = array_shift ($landlordRows);
  141. }
  142. if (count ($companyRows) > 0)
  143. {
  144. $theProperty ['company'] = array_shift ($companyRows);
  145. }
  146. $theProperty ['photos'] = array_values ($photoRows);
  147. $theProperty ['features'] = array_values ($featureRelRows);
  148. $theProperty ['inspection_times'] = array_values ($inspectionTimes);
  149. echo toJson ($theProperty), "\n";
  150. }
  151. //**********************************************************************
  152. //
  153. // Main
  154. //
  155. //**********************************************************************
  156. function main()
  157. {
  158. //$lvl = Logger::$DEBUG;
  159. $lvl = Logger::$ERROR;
  160. $transactionLog = new Table ('dbo.PropertyTransactionLog',
  161. array ('created_at', 'PropertyId'), 'PropertyId');
  162. // Remember the date/time of the last row for deletion of transactions
  163. $sql = "select max (created_at) as count from dbo.PropertyTransactionLog";
  164. $transactionLog->executeSQL($sql);
  165. $row = array_shift ($transactionLog->asArray());
  166. $lastdt = $row['count'];
  167. $sql = "select max(created_at) as created_at, PropertyId, [action]
  168. from dbo.PropertyTransactionLog
  169. group by PropertyId,[action]
  170. order by created_at,[action]";
  171. $transactionLog->executeSQL($sql);
  172. $rows = $transactionLog->asArray();
  173. $n = 0;
  174. foreach ($rows as $row)
  175. {
  176. $n++;
  177. $pid = $row ['PropertyId'];
  178. $action = $row ['action'];
  179. info (sprintf ("%d: %s\n", $pid, $action));
  180. }
  181. info (sprintf ("%d transactions found\n", $n));
  182. }
  183. main();
  184. ?>