BxDolParams.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolCacheFile');
  10. class BxDolParams extends BxDol {
  11. var $_oDb;
  12. var $_oCache;
  13. var $_sCacheFile;
  14. var $_aParams;
  15. /**
  16. * constructor
  17. */
  18. function BxDolParams($oDb) {
  19. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  20. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  21. parent::BxDol();
  22. $this->_oDb = $oDb;
  23. $this->_oCache = new BxDolCacheFile(); // feel free to change to another cache system if you are sure that it is available
  24. $this->_sCacheFile = 'sys_options_' . bx_site_hash('', true) . '.php';
  25. $this->_aParams = $this->_oCache->getData($this->_sCacheFile);
  26. if(empty($this->_aParams) && $this->_oDb != null)
  27. $this->cache();
  28. }
  29. /**
  30. * Prevent cloning the instance
  31. */
  32. public function __clone() {
  33. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  34. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  35. }
  36. /**
  37. * Get singleton instance of the class
  38. */
  39. public static function getInstance($oDb) {
  40. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  41. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolParams($oDb);
  42. return $GLOBALS['bxDolClasses'][__CLASS__];
  43. }
  44. function isInCache($sKey) {
  45. return isset($this->_aParams[$sKey]);
  46. }
  47. function exists($sKey, $bFromCache = true) {
  48. if($bFromCache && $this->isInCache($sKey))
  49. return true;
  50. $sQuery = $this->_oDb->prepare("SELECT `name` FROM `sys_options` WHERE `name`=? LIMIT 1", $sKey);
  51. return $this->_oDb->getOne($sQuery) == $sKey;
  52. }
  53. function add($sName, $sValue, $iKateg, $sDesc, $sType) {
  54. //--- Update Database ---//
  55. $sQuery = $this->_oDb->prepare("INSERT INTO `sys_options` SET `category_id`=?, `name`=?, `caption`=?, `value`=?, `type`=?", $iKateg, $sName, $sDesc, $sValue, $sType);
  56. $this->_oDb->query($sQuery);
  57. //--- Update Cache ---//
  58. $this->cache();
  59. }
  60. function get($sKey, $bFromCache = true) {
  61. if (!$sKey)
  62. return false;
  63. if ($bFromCache && $this->isInCache($sKey)) {
  64. return $this->_aParams[$sKey];
  65. } else {
  66. $sQuery = $this->_oDb->prepare("SELECT `value` FROM `sys_options` WHERE `name`=? LIMIT 1", $sKey);
  67. return $this->_oDb->getOne($sQuery);
  68. }
  69. }
  70. function set($sKey, $mixedValue) {
  71. //--- Update Database ---//
  72. $sQuery = $this->_oDb->prepare("UPDATE `sys_options` SET `value`=? WHERE `name`=? LIMIT 1", $mixedValue, $sKey);
  73. $this->_oDb->query($sQuery);
  74. //--- Update Cache ---//
  75. $this->cache();
  76. }
  77. function cache() {
  78. $this->_aParams = $this->_oDb->getPairs("SELECT `name`, `value` FROM `sys_options`", "name", "value");
  79. if (empty($this->_aParams)) {
  80. $this->_aParams = array ();
  81. return false;
  82. }
  83. return $this->_oCache->setData($this->_sCacheFile, $this->_aParams);
  84. }
  85. function clearCache() {
  86. $this->_oCache->delData($this->_sCacheFile);
  87. }
  88. }
  89. /** @} */