KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Visitor.php 2.8KB

9 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Class: Visitor
  4. * The model for the currently browsing <User>. Group falls back to whatever group is set as the "Guest Group".
  5. *
  6. * See Also:
  7. * <User>
  8. */
  9. class Visitor extends User {
  10. # Integer: $id
  11. # The ID of the currently visiting "user". 0 if not logged in.
  12. public $id = 0;
  13. /**
  14. * Function: __construct
  15. * Checks if a valid user is logged in.
  16. */
  17. public function __construct() {
  18. if (!empty($_SESSION['user_id']))
  19. parent::__construct($_SESSION['user_id']);
  20. }
  21. /**
  22. * Function: __get
  23. * A detour around belongs_to "group" to account for the default Guest group.
  24. */
  25. public function __get($name) {
  26. if (isset($this->$name))
  27. return $this->$name;
  28. elseif ($name == "group") {
  29. if (!isset($this->group_id))
  30. return new Group(Config::current()->guest_group);
  31. elseif (isset($this->group_name))
  32. return new Group(null, array("read_from" => array("id" => $this->group_id,
  33. "name" => $this->group_name)));
  34. else {
  35. $group = new Group($this->group_id);
  36. return ($group->no_results) ? new Group(Config::current()->default_group) : $group ;
  37. }
  38. }
  39. }
  40. /**
  41. * Function: find
  42. * See Also:
  43. * <Model::search>
  44. */
  45. static function find($options = array(), $options_for_object = array()) {
  46. fallback($options["order"], "id ASC");
  47. return parent::search("user", $options, $options_for_object);
  48. }
  49. /**
  50. * Function: group
  51. * Returns the user's <Group> or the "Guest Group".
  52. *
  53. * !! DEPRECATED AFTER 2.0 !!
  54. */
  55. public function group() {
  56. if (!isset($this->group_id))
  57. return new Group(Config::current()->guest_group);
  58. elseif (isset($this->group_name))
  59. return new Group(null, array("read_from" => array("id" => $this->group_id,
  60. "name" => $this->group_name)));
  61. else {
  62. $group = new Group($this->group_id);
  63. return ($group->no_results) ? new Group(Config::current()->default_group) : $group ;
  64. }
  65. }
  66. /**
  67. * Function: current
  68. * Returns a singleton reference to the current visitor.
  69. */
  70. public static function & current() {
  71. static $instance = null;
  72. return $instance = (empty($instance)) ? new self() : $instance ;
  73. }
  74. }