#2539 - More generic class UserSettings
This commit is contained in:
parent
556db98835
commit
893d920327
@ -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');
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
|
||||
|
||||
class PushNotification extends Base {
|
||||
var $tableSettings = 'push_notification_settings';
|
||||
|
||||
@ -100,14 +102,7 @@
|
||||
* @return bool
|
||||
**/
|
||||
public function updateSettings($account_id, $data) {
|
||||
$this->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;
|
||||
|
||||
100
include/classes/usersettings.class.php
Normal file
100
include/classes/usersettings.class.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
|
||||
|
||||
class UserSettings extends Base {
|
||||
protected $table = 'user_settings';
|
||||
|
||||
private $__cache = array();
|
||||
protected $account_id = null;
|
||||
private $__lazyWrite;
|
||||
|
||||
public function __construct($account_id, $lazy_write = true){
|
||||
$this->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);
|
||||
});
|
||||
@ -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';
|
||||
|
||||
@ -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 */;
|
||||
|
||||
@ -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, '<')) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user