From 893d92032770f74f2000e3bff121f53568d682b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Blon=C4=8F=C3=A1k?= Date: Thu, 12 Jan 2017 21:22:27 +0100 Subject: [PATCH] #2539 - More generic class UserSettings --- include/autoloader.inc.php | 1 + include/classes/base.class.php | 8 ++ include/classes/pushnotification.class.php | 33 +++---- include/classes/usersettings.class.php | 100 +++++++++++++++++++++ include/config/error_codes.inc.php | 1 + sql/000_base_structure.sql | 11 +-- upgrade/definitions/1.0.1_to_1.0.2.inc.php | 11 +-- 7 files changed, 132 insertions(+), 33 deletions(-) create mode 100644 include/classes/usersettings.class.php diff --git a/include/autoloader.inc.php b/include/autoloader.inc.php index ab0dd64a..2cc2c64a 100644 --- a/include/autoloader.inc.php +++ b/include/autoloader.inc.php @@ -78,6 +78,7 @@ require_once(CLASS_DIR . '/transaction.class.php'); require_once(CLASS_DIR . '/roundstats.class.php'); require_once(CLASS_DIR . '/news.class.php'); require_once(CLASS_DIR . '/api.class.php'); +require_once(CLASS_DIR . '/usersettings.class.php'); require_once(CLASS_DIR . '/ipushnotification.interface.php'); require_once(CLASS_DIR . '/pushnotification.class.php'); require_once(INCLUDE_DIR . '/lib/Michelf/Markdown.php'); diff --git a/include/classes/base.class.php b/include/classes/base.class.php index 41b8d245..a886da6b 100644 --- a/include/classes/base.class.php +++ b/include/classes/base.class.php @@ -16,6 +16,8 @@ class Base { public function getTableName() { return $this->table; } + + protected $debug; public function setDebug($debug) { $this->debug = $debug; } @@ -25,9 +27,13 @@ class Base { public function setCoinAddress($coin_address) { $this->coin_address = $coin_address; } + + protected $log; public function setLog($log) { $this->log = $log; } + + protected $mysqli; public function setMysql($mysqli) { $this->mysqli = $mysqli; } @@ -56,6 +62,8 @@ class Base { public function setConfig($config) { $this->config = $config; } + + protected $aErrorCodes; public function setErrorCodes(&$aErrorCodes) { $this->aErrorCodes =& $aErrorCodes; } diff --git a/include/classes/pushnotification.class.php b/include/classes/pushnotification.class.php index 05cfc5b6..5c8e3ce3 100644 --- a/include/classes/pushnotification.class.php +++ b/include/classes/pushnotification.class.php @@ -1,4 +1,6 @@ debug->append("STA " . __METHOD__, 4); - - $stmt = $this->mysqli->prepare("INSERT INTO $this->tableSettings (value, account_id) VALUES (?, ?) ON DUPLICATE KEY UPDATE value = VALUES(value)"); - if (!($stmt && $stmt->bind_param('si', json_encode($data), $account_id) && $stmt->execute())) { - $this->setErrorMessage($this->getErrorMsg('E0047', __CLASS__)); - return $this->sqlError(); - } - $this->log->log("info", "User $account_id updated notification settings"); + UserSettings::construct($account_id)->PushNotifications = $data; return true; } @@ -117,22 +112,14 @@ * @return array Notification settings **/ public function getNotificationSettings($account_id) { - $this->debug->append("STA " . __METHOD__, 4); - $stmt = $this->mysqli->prepare("SELECT value FROM $this->tableSettings WHERE account_id = ?"); - if ($stmt && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) { - if ($result->num_rows) { - /* @var $result mysqli_result */ - $aData = json_decode(current($result->fetch_row()), true); - return $aData; - } else { - return array( - 'class' => false, - 'params' => null, - 'file' => null, - ); - } + if ($settings = UserSettings::construct($account_id)->PushNotifications){ + return $settings; } - return $this->sqlError('E0045'); + return array( + 'class' => false, + 'params' => null, + 'file' => null, + ); } private static $instance = null; diff --git a/include/classes/usersettings.class.php b/include/classes/usersettings.class.php new file mode 100644 index 00000000..f65d2355 --- /dev/null +++ b/include/classes/usersettings.class.php @@ -0,0 +1,100 @@ +account_id = $account_id; + $this->__lazyWrite = $lazy_write; + if (is_callable(self::$__setup_callbacks)){ + call_user_func(self::$__setup_callbacks, $this); + } + } + + private static $__GetSTMT = null; + private static $__SetSTMT = null; + + public function __destruct(){ + if ($this->__lazyWrite){ + foreach ($this->__cache as $name=>$value){ + $this->_storeValue($name, $value); + } + } + } + + private function _storeValue($name, $value){ + if (empty(self::$__SetSTMT)){ + self::$__SetSTMT = $this->mysqli->prepare('REPLACE INTO '.$this->table.' (`account_id`, `name`, `value`) VALUES (?, ?, ?)'); + } + if (!(self::$__SetSTMT && self::$__SetSTMT->bind_param('iss', $this->account_id, $name, serialize($value)) && self::$__SetSTMT->execute())) { + $this->setErrorMessage($this->getErrorMsg('E0084', $this->table)); + return $this->sqlError(); + } + return true; + } + + private function _getValue($name, $default = null){ + if (empty(self::$__GetSTMT)){ + self::$__GetSTMT = $this->mysqli->prepare('SELECT `value` FROM '.$this->table.' WHERE `account_id` = ? AND `name` = ? LIMIT 1'); + } + if (self::$__GetSTMT && self::$__GetSTMT->bind_param('is', $this->account_id, $name) && self::$__GetSTMT->execute() && $result = self::$__GetSTMT->get_result()) { + if ($result->num_rows > 0) { + return unserialize($result->fetch_object()->value); + } else { + return $default; + } + } + $this->sqlError(); + return $default; + } + + public function __get($name){ + if (!$this->__lazyWrite){ + return $this->_getValue($name); + } + if (!array_key_exists($name, $this->__cache)){ + $this->__cache[$name] = $this->_getValue($name); + } + return $this->__cache[$name]; + } + + public function __set($name, $value){ + if (!$this->__lazyWrite){ + $this->_storeValue($name, $value); + } else { + $this->__cache[$name] = $value; + } + } + + private static $__setup_callbacks = null; + public static function setup($callback = null){ + self::$__setup_callbacks = $callback; + } + + private static $__lastInstanceId; + private static $__lastInstance; + /** + * @param int $account_id + * @param string $lazy_write + * @return UserSettings + */ + public static function construct($account_id, $lazy_write = true){ + if ((self::$__lastInstanceId == $account_id) && (self::$__lastInstance instanceof UserSettings)){ + return self::$__lastInstance; + } + self::$__lastInstanceId = $account_id; + return self::$__lastInstance = new self($account_id, $lazy_write); + } + } + + UserSettings::setup(function($instance)use($debug, $log, $mysqli, $aErrorCodes){ + $instance->setDebug($debug); + $instance->setLog($log); + $instance->setMysql($mysqli); + $instance->setErrorCodes($aErrorCodes); + }); diff --git a/include/config/error_codes.inc.php b/include/config/error_codes.inc.php index f7a8d7a8..a4b33471 100644 --- a/include/config/error_codes.inc.php +++ b/include/config/error_codes.inc.php @@ -79,3 +79,4 @@ $aErrorCodes['E0080'] = 'No new unaccounted shares since last run'; $aErrorCodes['E0081'] = 'Failed to insert new block into database'; $aErrorCodes['E0082'] = 'Block does not supply any usable confirmation information'; $aErrorCodes['E0083'] = 'Maintenance mode enabled, skipped'; +$aErrorCodes['E0084'] = 'Error updating %s table'; diff --git a/sql/000_base_structure.sql b/sql/000_base_structure.sql index 45718525..9ab29f71 100644 --- a/sql/000_base_structure.sql +++ b/sql/000_base_structure.sql @@ -248,11 +248,12 @@ CREATE TABLE `statistics_users` ( KEY `account_id_timestamp` (`account_id`,`timestamp`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -CREATE TABLE IF NOT EXISTS `push_notification_settings` ( - `account_id` int(11) NOT NULL, - `value` text DEFAULT NULL, - PRIMARY KEY (`account_id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE IF NOT EXISTS `user_settings` ( + `account_id` int(11) NOT NULL, + `name` varchar(50) NOT NULL, + `value` text DEFAULT NULL, + PRIMARY KEY (`account_id`,`name`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; diff --git a/upgrade/definitions/1.0.1_to_1.0.2.inc.php b/upgrade/definitions/1.0.1_to_1.0.2.inc.php index 8b18ac0d..d9c6aa80 100644 --- a/upgrade/definitions/1.0.1_to_1.0.2.inc.php +++ b/upgrade/definitions/1.0.1_to_1.0.2.inc.php @@ -10,11 +10,12 @@ function run_102() { // Upgrade specific variables $aSql[] = " - CREATE TABLE IF NOT EXISTS `push_notification_settings` ( - `account_id` int(11) NOT NULL, - `value` text DEFAULT NULL, - PRIMARY KEY (`account_id`) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + CREATE TABLE IF NOT EXISTS `user_settings` ( + `account_id` int(11) NOT NULL, + `name` varchar(50) NOT NULL, + `value` text DEFAULT NULL, + PRIMARY KEY (`account_id`,`name`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT; "; if ($db_version_now == $db_version_old && version_compare($db_version_now, DB_VERSION, '<')) {