BxXeroDb.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Xero Xero
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxXeroDb extends BxDolModuleDb
  12. {
  13. protected $_oConfig;
  14. protected $_aDataKeys;
  15. public function __construct(&$oConfig)
  16. {
  17. parent::__construct($oConfig);
  18. $this->_oConfig = $oConfig;
  19. $this->_aDataKeys = ['token', 'expires', 'tenant_id', 'refresh_token', 'id_token'];
  20. }
  21. public function setData($aData)
  22. {
  23. foreach($aData as $sKey => $sValue)
  24. $this->setParam('bx_xero_' . $sKey, $sValue);
  25. }
  26. public function cleanData()
  27. {
  28. foreach($this->_aDataKeys as $sKey)
  29. $aData[$sKey] = $this->setParam('bx_xero_' . $sKey, '');
  30. }
  31. public function getData()
  32. {
  33. $aData = [];
  34. foreach($this->_aDataKeys as $sKey)
  35. $aData[$sKey] = $this->getParam('bx_xero_' . $sKey);
  36. return $aData;
  37. }
  38. public function getContact($aParams = array())
  39. {
  40. $CNF = &$this->_oConfig->CNF;
  41. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  42. $sSelectClause = "`tc`.*";
  43. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  44. switch($aParams['type']) {
  45. case 'id':
  46. $aMethod['name'] = 'getRow';
  47. $aMethod['params'][1] = array(
  48. 'id' => $aParams['id']
  49. );
  50. $sWhereClause = " AND `tc`.`" . $CNF['FIELD_ID'] . "`=:id";
  51. break;
  52. case 'email':
  53. $aMethod['name'] = 'getRow';
  54. $aMethod['params'][1] = array(
  55. 'email' => $aParams['email']
  56. );
  57. $sWhereClause = " AND `tc`.`" . $CNF['FIELD_EMAIL'] . "`=:email";
  58. break;
  59. case 'all':
  60. break;
  61. }
  62. $sOrderClause = !empty($sOrderClause) ? " ORDER BY " . $sOrderClause : $sOrderClause;
  63. $sLimitClause = !empty($sLimitClause) ? " LIMIT " . $sLimitClause : $sLimitClause;
  64. $aMethod['params'][0] = "SELECT
  65. " . $sSelectClause . "
  66. FROM `" . $CNF['TABLE_CONTACTS'] . "` AS `tc`" . $sJoinClause . "
  67. WHERE 1" . $sWhereClause . $sOrderClause . $sLimitClause;
  68. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  69. }
  70. public function isContact($sEmail)
  71. {
  72. $aContact = $this->getContact([
  73. 'type' => 'email',
  74. 'email' => $sEmail
  75. ]);
  76. return !empty($aContact) && is_array($aContact);
  77. }
  78. public function insertContact($aSet)
  79. {
  80. $CNF = &$this->_oConfig->CNF;
  81. if(empty($aSet))
  82. return false;
  83. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_CONTACTS'] . "` SET " . $this->arrayToSQL($aSet)) > 0;
  84. }
  85. public function updateContact($aSet, $aWhere)
  86. {
  87. $CNF = &$this->_oConfig->CNF;
  88. if(empty($aSet) || empty($aWhere))
  89. return false;
  90. return (int)$this->query("UPDATE `" . $CNF['TABLE_CONTACTS'] . "` SET " . $this->arrayToSQL($aSet) . " WHERE " . $this->arrayToSQL($aWhere, ' AND ')) > 0;
  91. }
  92. public function deleteContact($aWhere)
  93. {
  94. $CNF = &$this->_oConfig->CNF;
  95. if(empty($aWhere))
  96. return false;
  97. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_CONTACTS'] . "` WHERE " . $this->arrayToSQL($aWhere, ' AND ')) > 0;
  98. }
  99. }
  100. /** @} */