PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/manageQuickBooks/QuickBooks/WebConnector/Server/SQL.php

https://github.com/kjavitz/SalesIgniter1
PHP | 336 lines | 128 code | 46 blank | 162 comment | 9 complexity | 3a1b5cb4d7be3144ff29742770581269 MD5 | raw file
  1. <?php
  2. /**
  3. * Mirror a QuickBooks database in a query-able SQL database
  4. *
  5. * Copyright (c) {2010-04-16} {Keith Palmer / ConsoliBYTE, LLC.
  6. * All rights reserved. This program and the accompanying materials
  7. * are made available under the terms of the Eclipse Public License v1.0
  8. * which accompanies this distribution, and is available at
  9. * http://www.opensource.org/licenses/eclipse-1.0.php
  10. *
  11. * Essentially, this package tries to import your QuickBooks database into an
  12. * SQL database of your choice, mapping the QuickBooks schema to SQL tables,
  13. * and then allowing you to query (and possibly insert/update) the SQL database
  14. * and keep this consistently syncronized with the original QuickBooks
  15. * database.
  16. *
  17. * @author Keith Palmer <keith@consolibyte.com>
  18. * @author Garrett Griffin <grgisme@gmail.com>
  19. * @license LICENSE.txt
  20. *
  21. * @package QuickBooks
  22. * @subpackage Server
  23. */
  24. if (!defined('QUICKBOOKS_SERVER_SQL_ON_ERROR'))
  25. {
  26. /**
  27. *
  28. */
  29. define('QUICKBOOKS_SERVER_SQL_ON_ERROR', 'continueOnError');
  30. }
  31. if (!defined('QUICKBOOKS_SERVER_SQL_VALUE_CLEAR'))
  32. {
  33. define('QUICKBOOKS_SERVER_SQL_VALUE_CLEAR', '*CLEAR*');
  34. }
  35. if (!defined('QUICKBOOKS_SERVER_SQL_ITERATOR_PRIORITY'))
  36. {
  37. /**
  38. * The priority value to use when re-queueing a request for the next part of an iterator
  39. *
  40. * @var integer
  41. */
  42. define('QUICKBOOKS_SERVER_SQL_ITERATOR_PRIORITY', 1000);
  43. }
  44. if (!defined('QUICKBOOKS_SERVER_SQL_CONFLICT_QUEUE_PRIORITY'))
  45. {
  46. /**
  47. * The priority value to use when issuing requests from an Error Handler for Add/Mods
  48. *
  49. * @var integer
  50. */
  51. define('QUICKBOOKS_SERVER_SQL_CONFLICT_QUEUE_PRIORITY', 9999);
  52. }
  53. if (!defined('QUICKBOOKS_SERVER_SQL_ITERATOR_MAXRETURNED'))
  54. {
  55. /**
  56. * How many records an iterator should grab in a single transaction
  57. *
  58. * @var integer
  59. */
  60. define('QUICKBOOKS_SERVER_SQL_ITERATOR_MAXRETURNED', 25);
  61. }
  62. /*function __temp_error_handler($requestID, $action, $ident, $extra, &$err, $xml, $errnum, $errmsg)
  63. {
  64. return true;
  65. }*/
  66. /**
  67. * QuickBooks driver classes
  68. */
  69. QuickBooks_Loader::load('/QuickBooks/Driver/Singleton.php');
  70. /**
  71. * Server base class
  72. */
  73. QuickBooks_Loader::load('/QuickBooks/WebConnector/Server.php');
  74. /**
  75. * SQL schema generation
  76. */
  77. QuickBooks_Loader::load('/QuickBooks/SQL/Schema.php');
  78. /**
  79. * SQL objects (convert qbXML to objects to schema)
  80. */
  81. QuickBooks_Loader::load('/QuickBooks/SQL/Object.php');
  82. /**
  83. * SQL callbacks (request and response handlers)
  84. */
  85. QuickBooks_Loader::load('/QuickBooks/Callbacks/SQL/Callbacks.php');
  86. /**
  87. * SQL error handlers
  88. */
  89. QuickBooks_Loader::load('/QuickBooks/Callbacks/SQL/Errors.php');
  90. /**
  91. * Handlers file (we need this for soem constant declarations)
  92. */
  93. QuickBooks_Loader::load('/QuickBooks/WebConnector/Handlers.php', false);
  94. /**
  95. *
  96. *
  97. */
  98. class QuickBooks_WebConnector_Server_SQL extends QuickBooks_WebConnector_Server
  99. {
  100. /**
  101. * Read from the QuickBooks database, and write to the SQL database
  102. */
  103. const MODE_READONLY = 'r';
  104. /**
  105. * Read from the SQL database, and write to the QuickBooks database
  106. */
  107. const MODE_WRITEONLY = 'w';
  108. /**
  109. * Read and write from both sources, keeping both sources in sync
  110. */
  111. const MODE_READWRITE = '+';
  112. const CONFLICT_LOG = 2;
  113. const CONFLICT_NEWER = 4;
  114. const CONFLICT_QUICKBOOKS = 8;
  115. const CONFLICT_SQL = 16;
  116. const CONFLICT_CALLBACK = 32;
  117. /**
  118. * Delete Modes. Decides whether an item actually gets deleted, or just remains marked deleted.
  119. *
  120. */
  121. const DELETE_REMOVE = 2;
  122. //define('QUICKBOOKS_SERVER_SQL_ON_DELETE_REMOVE', QUICKBOOKS_SERVER_SQL_DELETE_REMOVE);
  123. const DELETE_FLAG = 4;
  124. //define('QUICKBOOKS_SERVER_SQL::ON_DELETE_FLAG', QUICKBOOKS_SERVER_SQL_DELETE_FLAG);
  125. /**
  126. *
  127. *
  128. * You can run this server in one of three modes:
  129. * - QUICKBOOKS_SERVER_SQL_MODE_READONLY: Data will only be read from
  130. * QuickBooks; changes to data in the SQL database will never be
  131. * pushed back to QuickBooks.
  132. * - QUICKBOOKS_SERVER_SQL_MODE_WRITEONLY: Data will only be pushed to
  133. * QuickBooks, and nothing that already exists in QuickBooks will be
  134. * imported into the SQL database.
  135. * - QUICKBOOKS_SERVER_SQL_MODE_READWRITE: The server will do it's best to
  136. * try to import all QuickBooks data into the SQL database, and then
  137. * push changes that occur in either location to the other location.
  138. * The server will try to syncronise the two locations as much as is
  139. * possible.
  140. *
  141. * @param string $dsn_or_conn DSN-style connection string or an already opened connection to the driver
  142. * @param string $how_often The maximum time we wait between updates/syncs (you can use any valid interval: "1 hour", "15 minutes", 60, etc.)
  143. * @param char $mode The mode the server should run in (see constants above)
  144. * @param char $conflicts The steps towards update conflict resolution the server should take (see constants above)
  145. * @param mixed $users The user (or an array of users) who will be using the SQL server
  146. * @param array $map
  147. * @param array $onerror
  148. * @param string $wsdl
  149. * @param array $soap_options
  150. * @param array $handler_options
  151. * @param array $driver_options
  152. */
  153. public function __construct(
  154. $dsn_or_conn,
  155. $how_often,
  156. $mode,
  157. $conflicts,
  158. $delete,
  159. $users = null,
  160. $map = array(),
  161. $onerror = array(),
  162. $hooks = array(),
  163. $log_level = QUICKBOOKS_LOG_NORMAL,
  164. $soap = QUICKBOOKS_SOAPSERVER_BUILTIN,
  165. $wsdl = QUICKBOOKS_WSDL,
  166. $soap_options = array(),
  167. $handler_options = array(),
  168. $driver_options = array(),
  169. $sql_options = array(),
  170. $callback_options = array())
  171. {
  172. // $dsn_or_conn, $map, $onerror = array(), $hooks = array(), $log_level = QUICKBOOKS_LOG_NORMAL, $soap = QUICKBOOKS_SOAPSERVER_BUILTIN, $wsdl = QUICKBOOKS_WSDL, $soap_options = array(), $handler_options = array(), $driver_options = array()
  173. if (!is_array($users))
  174. {
  175. $users = array( $users );
  176. }
  177. // Map of callback handlers
  178. $sql_map = array();
  179. foreach (get_class_methods('QuickBooks_Callbacks_SQL_Callbacks') as $method)
  180. {
  181. if (strtolower(substr($method, -7)) == 'request')
  182. {
  183. $action = substr($method, 0, -7);
  184. $sql_map[$action] = array(
  185. 'QuickBooks_Callbacks_SQL_Callbacks::' . $action . 'Request',
  186. 'QuickBooks_Callbacks_SQL_Callbacks::' . $action . 'Response' );
  187. }
  188. }
  189. /*
  190. $sql_map[QUICKBOOKS_DERIVE_ITEM] = array(
  191. 'QuickBooks_Callbacks_SQL_Callbacks::ItemDeriveRequest',
  192. 'QuickBooks_Callbacks_SQL_Callbacks::ItemDeriveResponse' );
  193. $sql_map[QUICKBOOKS_DERIVE_CUSTOMER] = array(
  194. 'QuickBooks_Callbacks_SQL_Callbacks::CustomerDeriveRequest',
  195. 'QuickBooks_Callbacks_SQL_Callbacks::CustomerDeriveResponse' );
  196. $sql_map[QUICKBOOKS_DERIVE_INVOICE] = array(
  197. 'QuickBooks_Callbacks_SQL_Callbacks::InvoiceDeriveRequest',
  198. 'QuickBooks_Callbacks_SQL_Callbacks::InvoiceDeriveResponse' );
  199. */
  200. //print_r($sql_map);
  201. //exit;
  202. // Default error handlers
  203. $sql_onerror = array(
  204. '*' => 'QuickBooks_Callbacks_SQL_Errors::catchall',
  205. );
  206. $sql_onerror = $this->_merge($sql_onerror, $onerror, false);
  207. // Default hooks
  208. $sql_hooks = array(
  209. // This hook is neccessary for queueing up the appropriate actions to perform the sync (use login success so we know user to sync for)
  210. QuickBooks_WebConnector_Handlers::HOOK_LOGINSUCCESS => array( 'QuickBooks_Callbacks_SQL_Callbacks::onAuthenticate' ),
  211. );
  212. // Merge with user-defined hooks
  213. $sql_hooks = $this->_merge($hooks, $sql_hooks, true);
  214. // @TODO Prefix these with _ so that people don't accidentally overwrite them
  215. $sql_callback_options = array(
  216. 'hooks' => $sql_hooks,
  217. 'conflicts' => $conflicts,
  218. 'mode' => $mode,
  219. 'delete' => $delete,
  220. 'recur' => QuickBooks_Utilities::intervalToSeconds($how_often),
  221. 'map' => $sql_map,
  222. );
  223. //print_r($sql_options);
  224. //exit;
  225. $defaults = $this->_sqlDefaults($sql_options);
  226. //$sql_callback_options['_only_query'] = $defaults['only_query'];
  227. //$sql_callback_options['_dont_query'] = $defaults['dont_query'];
  228. $sql_callback_options['_only_import'] = $defaults['only_import'];
  229. $sql_callback_options['_dont_import'] = $defaults['dont_import'];
  230. $sql_callback_options['_only_add'] = $defaults['only_add'];
  231. $sql_callback_options['_dont_add'] = $defaults['dont_add'];
  232. $sql_callback_options['_only_modify'] = $defaults['only_modify'];
  233. $sql_callback_options['_dont_modify'] = $defaults['dont_modify'];
  234. $sql_callback_options['_only_misc'] = $defaults['only_misc'];
  235. $sql_callback_options['_dont_misc'] = $defaults['dont_misc'];
  236. // Merge default values with passed in values
  237. // (in this case, we are *required* to have these values present, so
  238. // we make sure that the SQL options override any user-defined options
  239. $sql_callback_options = $this->_merge($callback_options, $sql_callback_options, false);
  240. // Initialize the Driver singleton
  241. $Driver = QuickBooks_Driver_Singleton::getInstance($dsn_or_conn, $driver_options, $sql_hooks, $log_level);
  242. // $dsn_or_conn, $map, $onerror = array(), $hooks = array(), $log_level = QUICKBOOKS_LOG_NORMAL, $soap = QUICKBOOKS_SOAPSERVER_BUILTIN, $wsdl = QUICKBOOKS_WSDL, $soap_options = array(), $handler_options = array(), $driver_options = array()
  243. parent::__construct($dsn_or_conn, $sql_map, $sql_onerror, $sql_hooks, $log_level, $soap, $wsdl, $soap_options, $handler_options, $driver_options, $sql_callback_options);
  244. /*
  245. // TESTING only
  246. $requestID = null;
  247. $user = 'quickbooks';
  248. $hook = QUICKBOOKS_HANDLERS_HOOK_LOGINSUCCESS;
  249. $err = null;
  250. $hook_data = array();
  251. $callback_config = $sql_callback_options;
  252. QuickBooks_Callbacks_SQL_Callbacks::onAuthenticate($requestID, $user, $hook, $err, $hook_data, $callback_config);
  253. */
  254. }
  255. /**
  256. * Apply default options to an array of configuration options
  257. *
  258. * @param array $config
  259. * @return array
  260. */
  261. protected function _sqlDefaults($config)
  262. {
  263. $tmp = array(
  264. //'only_query',
  265. //'dont_query',
  266. 'only_import',
  267. 'dont_import',
  268. 'only_add',
  269. 'dont_add',
  270. 'only_modify',
  271. 'dont_modify',
  272. 'only_misc',
  273. 'dont_misc',
  274. );
  275. foreach ($tmp as $filter)
  276. {
  277. if (empty($config[$filter]) or
  278. (!empty($config[$filter]) and !is_array($config[$filter])))
  279. {
  280. $config[$filter] = array();
  281. }
  282. }
  283. // Any other configuration defaults go here
  284. $defaults = array(
  285. );
  286. return array_merge($defaults, $config);
  287. }
  288. }