UserSetting.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. class UserSetting extends DBObject {
  3. public function __construct(User $user, $key) {
  4. parent::__construct("user_settings", array("user_id", "key"), array($user->UserId, $key));
  5. if (!$this->UserId)
  6. $this->UserId=$user->UserId;
  7. if (!$this->Key)
  8. $this->Key=$key;
  9. $this->Value=$this->GetValue();
  10. unset($this->_changedFields['Value']);
  11. }
  12. private function GetValue(){
  13. switch ($this->Type){
  14. case DataTypes::Integer:
  15. return (int)$this->Value;
  16. case DataTypes::Float:
  17. return (float)$this->Value;
  18. case DataTypes::Boolean:
  19. return (bool)$this->Value;
  20. case DataTypes::String:
  21. return (string)$this->Value;
  22. case DataTypes::Collection:
  23. case DataTypes::Object:
  24. return json_decode($this->Value);
  25. }
  26. return null;
  27. }
  28. public function Save() {
  29. if (!$this->HasChangedFields())
  30. return;
  31. switch (gettype($this->Value)){
  32. case "integer":
  33. $this->Type=DataTypes::Integer;
  34. break;
  35. case "double":
  36. $this->Type=DataTypes::Float;
  37. break;
  38. case "boolean":
  39. $this->Type=DataTypes::Boolean;
  40. break;
  41. case "string":
  42. $this->Type=DataTypes::String;
  43. break;
  44. case "Array":
  45. $this->Type=DataTypes::Collection;
  46. $this->Value=json_encode($this->Value);
  47. break;
  48. case "object":
  49. $this->Type=DataTypes::Object;
  50. $this->Value=json_encode($this->Value);
  51. break;
  52. default:
  53. $this->Type=DataTypes::Null;
  54. }
  55. parent::Save();
  56. }
  57. }