BxAttendantDb.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 Attendant Attendant
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolModuleDb');
  12. class BxAttendantDb extends BxBaseModGeneralDb
  13. {
  14. protected $_sTableEntries;
  15. public function __construct(&$oConfig)
  16. {
  17. parent::__construct($oConfig);
  18. $this->_oConfig = $oConfig;
  19. $CNF = $oConfig->CNF;
  20. $this->_sTableEntries = $CNF['TABLE_ENTRIES'];
  21. }
  22. public function getEvents($aParams, $bReturnCount = false)
  23. {
  24. $CNF = &$this->_oConfig->CNF;
  25. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  26. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  27. $sSelectClause = "`{$this->_sTableEntries}`.*";
  28. switch($aParams['type']) {
  29. case 'active_by_action_and_profile_id':
  30. $aMethod['name'] = 'getAll';
  31. $aMethod['params'][1] = array(
  32. 'action' => $aParams['action'],
  33. 'profile_id' => $aParams['profile_id']
  34. );
  35. $sWhereClause = "AND `{$this->_sTableEntries}`.`" . $CNF['FIELD_ACTION'] . "` =:action AND `{$this->_sTableEntries}`.`" . $CNF['FIELD_PROFILE_ID'] . "` =:profile_id AND `" . $CNF['FIELD_PROCESSED'] . "` IS NULL";
  36. break;
  37. case 'all':
  38. $aMethod['name'] = 'getAll';
  39. $aMethod['params'][1] = array(
  40. 'request_id' => $aParams['value']
  41. );
  42. break;
  43. }
  44. $sSql = "SELECT {select} FROM `{$this->_sTableEntries}` " . $sJoinClause . " WHERE 1 " . $sWhereClause . " {order} {limit}";
  45. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql);
  46. $aEntries = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  47. if(!$bReturnCount)
  48. return $aEntries;
  49. $aMethod['name'] = 'getOne';
  50. $aMethod['params'][0] = str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql);
  51. return array($aEntries, (int)call_user_func_array(array($this, $aMethod['name']), $aMethod['params']));
  52. }
  53. public function addEvent($sAction, $iObjectId, $sModule, $sEvent, $iProfileId)
  54. {
  55. $CNF = &$this->_oConfig->CNF;
  56. $aBindings = array(
  57. 'module' => $sModule,
  58. 'event' => $sEvent,
  59. 'action' => $sAction,
  60. 'added' => time(),
  61. 'object_id' => $iObjectId,
  62. 'profile_id' => $iProfileId,
  63. 'event' => $sEvent
  64. );
  65. $this->query("INSERT INTO `" . $CNF['TABLE_ENTRIES'] . "` (`" . $CNF['FIELD_MODULE'] . "`, `" . $CNF['FIELD_EVENT'] . "`, `" . $CNF['FIELD_ACTION'] . "`, `" . $CNF['FIELD_ADDED'] . "`, `" . $CNF['FIELD_OBJECT_ID'] . "`, `" . $CNF['FIELD_PROFILE_ID'] . "`) values (:module, :event, :action, :added, :object_id, :profile_id)", $aBindings);
  66. }
  67. public function setEventProcessed($id)
  68. {
  69. $CNF = &$this->_oConfig->CNF;
  70. $aBindings = array(
  71. 'processed' => time()
  72. );
  73. $this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `" . $CNF['FIELD_PROCESSED'] . "` = :processed", $aBindings);
  74. }
  75. }
  76. /** @} */