00001 <?php
00019 class ElggUser extends ElggEntity
00020 implements Friendable {
00021
00030 protected function initializeAttributes() {
00031 parent::initializeAttributes();
00032
00033 $this->attributes['type'] = "user";
00034 $this->attributes['name'] = NULL;
00035 $this->attributes['username'] = NULL;
00036 $this->attributes['password'] = NULL;
00037 $this->attributes['salt'] = NULL;
00038 $this->attributes['email'] = NULL;
00039 $this->attributes['language'] = NULL;
00040 $this->attributes['code'] = NULL;
00041 $this->attributes['banned'] = "no";
00042 $this->attributes['admin'] = 'no';
00043 $this->attributes['prev_last_action'] = NULL;
00044 $this->attributes['last_login'] = NULL;
00045 $this->attributes['prev_last_login'] = NULL;
00046 $this->attributes['tables_split'] = 2;
00047 }
00048
00057 function __construct($guid = null) {
00058 $this->initializeAttributes();
00059
00060
00061 $this->initialise_attributes(false);
00062
00063 if (!empty($guid)) {
00064
00065 if ($guid instanceof stdClass) {
00066
00067 if (!$this->load($guid)) {
00068 $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid));
00069 throw new IOException($msg);
00070 }
00071 } else if (is_string($guid)) {
00072
00073 $user = get_user_by_username($guid);
00074 if ($user) {
00075 foreach ($user->attributes as $key => $value) {
00076 $this->attributes[$key] = $value;
00077 }
00078 }
00079 } else if ($guid instanceof ElggUser) {
00080
00081 elgg_deprecated_notice('This type of usage of the ElggUser constructor was deprecated. Please use the clone method.', 1.7);
00082
00083 foreach ($guid->attributes as $key => $value) {
00084 $this->attributes[$key] = $value;
00085 }
00086 } else if ($guid instanceof ElggEntity) {
00087
00088 throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggUser'));
00089 } else if (is_numeric($guid)) {
00090
00091 if (!$this->load($guid)) {
00092 throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid)));
00093 }
00094 } else {
00095 throw new InvalidParameterException(elgg_echo('InvalidParameterException:UnrecognisedValue'));
00096 }
00097 }
00098 }
00099
00107 protected function load($guid) {
00108 $attr_loader = new ElggAttributeLoader(get_class(), 'user', $this->attributes);
00109 $attr_loader->secondary_loader = 'get_user_entity_as_row';
00110
00111 $attrs = $attr_loader->getRequiredAttributes($guid);
00112 if (!$attrs) {
00113 return false;
00114 }
00115
00116 $this->attributes = $attrs;
00117 $this->attributes['tables_loaded'] = 2;
00118 _elgg_cache_entity($this);
00119
00120 return true;
00121 }
00122
00128 public function save() {
00129
00130 if (!parent::save()) {
00131 return false;
00132 }
00133
00134
00135 _elgg_disable_caching_for_entity($this->guid);
00136 $ret = create_user_entity($this->get('guid'), $this->get('name'), $this->get('username'),
00137 $this->get('password'), $this->get('salt'), $this->get('email'), $this->get('language'),
00138 $this->get('code'));
00139 _elgg_enable_caching_for_entity($this->guid);
00140
00141 return $ret;
00142 }
00143
00149 public function delete() {
00150 global $USERNAME_TO_GUID_MAP_CACHE, $CODE_TO_GUID_MAP_CACHE;
00151
00152
00153 if (isset($USERNAME_TO_GUID_MAP_CACHE[$this->username])) {
00154 unset($USERNAME_TO_GUID_MAP_CACHE[$this->username]);
00155 }
00156 if (isset($CODE_TO_GUID_MAP_CACHE[$this->code])) {
00157 unset($CODE_TO_GUID_MAP_CACHE[$this->code]);
00158 }
00159
00160 clear_user_files($this);
00161
00162
00163 return parent::delete();
00164 }
00165
00173 public function ban($reason = "") {
00174 return ban_user($this->guid, $reason);
00175 }
00176
00182 public function unban() {
00183 return unban_user($this->guid);
00184 }
00185
00191 public function isBanned() {
00192 return $this->banned == 'yes';
00193 }
00194
00200 public function isAdmin() {
00201
00202
00203
00204
00205
00206 return $this->attributes['admin'] == 'yes';
00207 }
00208
00214 public function makeAdmin() {
00215
00216 if ($this->guid && !make_user_admin($this->guid)) {
00217 return FALSE;
00218 }
00219
00220
00221 $this->attributes['admin'] = 'yes';
00222
00223 return TRUE;
00224 }
00225
00231 public function removeAdmin() {
00232
00233 if ($this->guid && !remove_user_admin($this->guid)) {
00234 return FALSE;
00235 }
00236
00237
00238 $this->attributes['admin'] = 'no';
00239
00240 return TRUE;
00241 }
00242
00252 function getSites($subtype = "", $limit = 10, $offset = 0) {
00253 return get_user_sites($this->getGUID(), $subtype, $limit, $offset);
00254 }
00255
00263 function addToSite($site_guid) {
00264 return add_site_user($site_guid, $this->getGUID());
00265 }
00266
00274 function removeFromSite($site_guid) {
00275 return remove_site_user($site_guid, $this->getGUID());
00276 }
00277
00285 function addFriend($friend_guid) {
00286 return user_add_friend($this->getGUID(), $friend_guid);
00287 }
00288
00296 function removeFriend($friend_guid) {
00297 return user_remove_friend($this->getGUID(), $friend_guid);
00298 }
00299
00305 function isFriend() {
00306 return $this->isFriendOf(elgg_get_logged_in_user_guid());
00307 }
00308
00316 function isFriendsWith($user_guid) {
00317 return user_is_friend($this->getGUID(), $user_guid);
00318 }
00319
00327 function isFriendOf($user_guid) {
00328 return user_is_friend($user_guid, $this->getGUID());
00329 }
00330
00340 function getFriends($subtype = "", $limit = 10, $offset = 0) {
00341 return get_user_friends($this->getGUID(), $subtype, $limit, $offset);
00342 }
00343
00353 function getFriendsOf($subtype = "", $limit = 10, $offset = 0) {
00354 return get_user_friends_of($this->getGUID(), $subtype, $limit, $offset);
00355 }
00356
00367 function listFriends($subtype = "", $limit = 10, array $vars = array()) {
00368 $defaults = array(
00369 'type' => 'user',
00370 'relationship' => 'friend',
00371 'relationship_guid' => $this->guid,
00372 'limit' => $limit,
00373 'full_view' => false,
00374 );
00375
00376 $options = array_merge($defaults, $vars);
00377
00378 if ($subtype) {
00379 $options['subtype'] = $subtype;
00380 }
00381
00382 return elgg_list_entities_from_relationship($options);
00383 }
00384
00394 function getGroups($subtype = "", $limit = 10, $offset = 0) {
00395 $options = array(
00396 'type' => 'group',
00397 'relationship' => 'member',
00398 'relationship_guid' => $this->guid,
00399 'limit' => $limit,
00400 'offset' => $offset,
00401 );
00402
00403 if ($subtype) {
00404 $options['subtype'] = $subtype;
00405 }
00406
00407 return elgg_get_entities_from_relationship($options);
00408 }
00409
00419 function listGroups($subtype = "", $limit = 10, $offset = 0) {
00420 $options = array(
00421 'type' => 'group',
00422 'relationship' => 'member',
00423 'relationship_guid' => $this->guid,
00424 'limit' => $limit,
00425 'offset' => $offset,
00426 'full_view' => false,
00427 );
00428
00429 if ($subtype) {
00430 $options['subtype'] = $subtype;
00431 }
00432
00433 return elgg_list_entities_from_relationship($options);
00434 }
00435
00445 public function getObjects($subtype = "", $limit = 10, $offset = 0) {
00446 $params = array(
00447 'type' => 'object',
00448 'subtype' => $subtype,
00449 'owner_guid' => $this->getGUID(),
00450 'limit' => $limit,
00451 'offset' => $offset
00452 );
00453 return elgg_get_entities($params);
00454 }
00455
00465 public function getFriendsObjects($subtype = "", $limit = 10, $offset = 0) {
00466 return get_user_friends_objects($this->getGUID(), $subtype, $limit, $offset);
00467 }
00468
00476 public function countObjects($subtype = "") {
00477 return count_user_objects($this->getGUID(), $subtype);
00478 }
00479
00489 public function getCollections($subtype = "", $limit = 10, $offset = 0) {
00490 elgg_deprecated_notice("ElggUser::getCollections() has been deprecated", 1.8);
00491 return false;
00492 }
00493
00501 function getOwnerGUID() {
00502 if ($this->owner_guid == 0) {
00503 return $this->guid;
00504 }
00505
00506 return $this->owner_guid;
00507 }
00508
00515 function getOwner() {
00516 elgg_deprecated_notice("ElggUser::getOwner deprecated for ElggUser::getOwnerGUID", 1.8);
00517 $this->getOwnerGUID();
00518 }
00519
00520
00521
00527 public function getExportableValues() {
00528 return array_merge(parent::getExportableValues(), array(
00529 'name',
00530 'username',
00531 'language',
00532 ));
00533 }
00534
00543 public function __set($name, $value) {
00544 if ($name == 'admin' || $name == 'siteadmin') {
00545 elgg_deprecated_notice('The admin/siteadmin metadata are not longer used. Use ElggUser->makeAdmin() and ElggUser->removeAdmin().', 1.7);
00546
00547 if ($value == 'yes' || $value == '1') {
00548 $this->makeAdmin();
00549 } else {
00550 $this->removeAdmin();
00551 }
00552 }
00553 return parent::__set($name, $value);
00554 }
00555
00563 public function __get($name) {
00564 if ($name == 'admin' || $name == 'siteadmin') {
00565 elgg_deprecated_notice('The admin/siteadmin metadata are not longer used. Use ElggUser->isAdmin().', 1.7);
00566 return $this->isAdmin();
00567 }
00568
00569 return parent::__get($name);
00570 }
00571
00581 public function canComment($user_guid = 0) {
00582 $result = parent::canComment($user_guid);
00583 if ($result !== null) {
00584 return $result;
00585 }
00586 return false;
00587 }
00588 }