PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/datasource_classes/RetrieveCafePHP4pcData.class.php

http://github.com/yodarunamok/fxphp
PHP | 181 lines | 165 code | 7 blank | 9 comment | 49 complexity | 2c3532f18ed119e00656da7378668560 MD5 | raw file
  1. <?php
  2. require_once('RetrieveFXSQLData.class.php');
  3. #### Part of FX.php #####################################################
  4. # #
  5. # License: Artistic License and addendum (included with release) #
  6. # Web Site: www.iviking.org #
  7. # #
  8. #########################################################################
  9. class RetrieveCafePHP4pcData extends RetrieveFXSQLData {
  10. function doQuery ($action) {
  11. // Note that because of the way in which CAFEphp and FileMaker are implemented, CAFEphp must be running on the same
  12. // machine that is serving as the web server. (You'll note that PHP creates a COM object which looks for a locally
  13. // running application.) For this same reason, the server IP and port are irrelevant.
  14. $availableActions = array('-delete', '-edit', '-find', '-findall', '-new', '-sqlquery');
  15. if (! in_array(strtolower($action), $availableActions)) { // first off, toss out any requests for actions NOT supported under CAFEphp
  16. return new FX_Error("The action requested ({$action}) is not supported in CAFEphp.");
  17. }
  18. $CAFEphp_res = new COM('CAFEphp.Application'); // although username and password are optional for this function, FX.php expects them to be set
  19. if ($CAFEphp_res == false) {
  20. return new FX_Error('Unable to load to CAFEphp.');
  21. }
  22. if ((defined("DEBUG") and DEBUG) or DEBUG_FUZZY) {
  23. $currentDebugString = "<p>CAFEphp version: " . $CAFEphp_res->Version() . "</p>\n";
  24. $this->FX->lastDebugMessage .= $currentDebugString;
  25. if (defined("DEBUG") and DEBUG) {
  26. echo $currentDebugString;
  27. }
  28. }
  29. $theResult = $CAFEphp_res->Connect($this->FX->database, $this->FX->DBUser, $this->FX->DBPassword);
  30. if ($theResult != 0) {
  31. $CAFEphp_res->EndConnection();
  32. switch ($theResult) {
  33. case -1:
  34. return new FX_Error('Unable to connect. Be sure the FileMaker database and CAFEphp are running.');
  35. break;
  36. case -2:
  37. return new FX_Error('Certificate not present. You MUST have a certificate.');
  38. break;
  39. case -3:
  40. return new FX_Error('Certificate is corrupt.');
  41. break;
  42. case -4:
  43. return new FX_Error('CAFEphp is not running or the demo version has expired.');
  44. break;
  45. case -5:
  46. return new FX_Error('The current demo of CAFEphp has expired.');
  47. break;
  48. default:
  49. return new FX_Error('An unknown error has occured while attempting to create the COM object.');
  50. break;
  51. }
  52. }
  53. switch ($action) {
  54. case '-delete':
  55. case '-edit':
  56. case '-find':
  57. case '-findall':
  58. case '-new':
  59. $this->FX->dataQuery = $this->BuildSQLQuery($action);
  60. if (FX::isError($this->FX->dataQuery)) {
  61. return $this->FX->dataQuery;
  62. }
  63. case '-sqlquery': // note that there is no preceding break, as we don't want to build a query
  64. if (substr(trim($this->FX->dataQuery), 0, 6) == 'SELECT') {
  65. $currentSelect = true;
  66. $theResult = $CAFEphp_res->Query($this->FX->dataQuery, $this->FX->groupSize);
  67. } else {
  68. $currentSelect = false;
  69. $theResult = $CAFEphp_res->Execute($this->FX->dataQuery);
  70. }
  71. if ($theResult < 0) {
  72. $CAFEphp_res->EndConnection();
  73. switch ($theResult) {
  74. case -1:
  75. return new FX_Error('No CAFEphp connection for the query.');
  76. break;
  77. default:
  78. return new FX_Error('An unknown error occured during the query.');
  79. break;
  80. }
  81. }
  82. $this->FX->foundCount = $theResult;
  83. $theResult = $CAFEphp_res->FieldCount();
  84. if ($theResult < 0) {
  85. $CAFEphp_res->EndConnection();
  86. switch ($theResult) {
  87. case -1:
  88. return new FX_Error('No CAFEphp connection for the field count.');
  89. break;
  90. case -2:
  91. return new FX_Error('No query was performed for a field count.');
  92. break;
  93. default:
  94. return new FX_Error('An unknown error occured during the query.');
  95. break;
  96. }
  97. } else {
  98. $currentFieldCount = $theResult;
  99. }
  100. for ($i = 0; $i < $currentFieldCount; ++$i) {
  101. $theResult = $CAFEphp_res->FieldName($i);
  102. if ($theResult == '$-CAFEphpNOCONNECTION') {
  103. $CAFEphp_res->EndConnection();
  104. return new FX_Error("No CAFEphp connection while retieving the name of field {$i}.");
  105. } elseif ($theResult == '$-CAFEphpNOQUERY') {
  106. $CAFEphp_res->EndConnection();
  107. return new FX_Error("CAFEphp returned a \"No Query\" error while retieving the name of field {$i}.");
  108. } elseif ($theResult == '$-CAFEphpUNKNOWNERROR') {
  109. $CAFEphp_res->EndConnection();
  110. return new FX_Error("CAFEphp returned an unknown error while retieving the name of field {$i}.");
  111. }
  112. $this->FX->fieldInfo[$i]['name'] = $theResult;
  113. $this->FX->fieldInfo[$i]['type'] = 'NO DATA';
  114. $this->FX->fieldInfo[$i]['emptyok'] = 'NO DATA';
  115. $this->FX->fieldInfo[$i]['maxrepeat'] = 'NO DATA';
  116. $this->FX->fieldInfo[$i]['extra'] = '';
  117. }
  118. if ($currentSelect) {
  119. $tempRow = array();
  120. for ($i = 0; $i < $this->FX->foundCount; ++$i) {
  121. for ($j = 0; $j < $currentFieldCount; ++$j) {
  122. $theResult = $CAFEphp_res->FieldValue($j);
  123. if ($theResult == '$-CAFEphpNOCONNECTION') {
  124. $CAFEphp_res->EndConnection();
  125. return new FX_Error("No CAFEphp connection while retieving the value of field {$i} for record {$j}.");
  126. } elseif ($theResult == '$-CAFEphpNOQUERY') {
  127. $CAFEphp_res->EndConnection();
  128. return new FX_Error("CAFEphp returned a \"No Query\" error while retieving the value of field {$i} for record {$j}.");
  129. } elseif ($theResult == '$-CAFEphpUNKNOWNERROR') {
  130. $CAFEphp_res->EndConnection();
  131. return new FX_Error("CAFEphp returned an unknown error while retieving the value of field {$i} for record {$j}.");
  132. }
  133. if (! $this->FX->useInnerArray) {
  134. $tempRow[$this->FX->fieldInfo[$j]['name']] = $theResult;
  135. } else {
  136. $tempRow[$this->FX->fieldInfo[$j]['name']] = array($theResult);
  137. }
  138. if ($this->FX->fieldInfo[$j]['name'] == $this->FX->primaryKeyField) {
  139. $currentKey = $value;
  140. }
  141. }
  142. if ($this->FX->genericKeys || $this->FX->primaryKeyField == '') {
  143. $this->FX->currentData[] = $tempRow;
  144. } else {
  145. $this->FX->currentData[$currentKey] = $tempRow;
  146. }
  147. $theResult = $CAFEphp_res->MoveNext();
  148. if ($theResult < 0) {
  149. $CAFEphp_res->EndConnection();
  150. $next = $i + 1;
  151. switch ($theResult) {
  152. case -1:
  153. return new FX_Error('No CAFEphp connection while moving from record {$i} to {$next}.');
  154. break;
  155. case -2:
  156. return new FX_Error('There was no current query while moving from record {$i} to {$next}.');
  157. break;
  158. default:
  159. return new FX_Error('An unknown error occured while moving from record {$i} to {$next}.');
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. break;
  166. default:
  167. return new FX_Error("The action requested ({$action}) is not supported in CAFEphp.");
  168. break;
  169. }
  170. $this->FX->fxError = 0;
  171. return true;
  172. }
  173. }
  174. ?>