-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmembership_entity.inc
156 lines (134 loc) · 3.03 KB
/
membership_entity.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* @file
* Contains the MembershipEntity class.
*/
/**
* Defines a membership entity.
*/
class MembershipEntity extends Entity {
/**
* The primary identifier for a membership.
*
* @var int
*/
public $mid;
/**
* A user enterable unique member id.
*
* @var int
*/
public $member_id; // @codingStandardsIgnoreLine
/**
* The type (bundle) of membership.
*
* @var string
*/
public $type;
/**
* The primary member.
*
* @var int
*/
public $uid;
/**
* An array of secondary members.
*
* @var array
*/
public $secondary_members; // @codingStandardsIgnoreLine
/**
* Integer code indicating the membership status.
*
* @var int
*
* @see membership_entity_get_status_list().
*/
public $status;
/**
* The Unix timestamp when the membership was created.
*
* @var int
*/
public $created;
/**
* The Unix timestamp when the membership was most recently saved.
*
* @var int
*/
public $changed;
/**
* Build a new membership entity.
*/
public function __construct($values = array()) {
return parent::__construct($values, 'membership_entity');
}
/**
* Returns the primary member.
*/
public function primaryMember() {
return user_load($this->uid);
}
/**
* Set the primary member for the membership.
*
* @param object|int $account
* The loaded account object or member uid.
*/
public function setPrimaryMember($account) {
$this->uid = is_object($account) ? $account->uid : $account;
}
/**
* Returns the full url to the membership.
*/
public function url() {
$uri = $this->uri();
return url($uri['path'], $uri);
}
/**
* Returns the Drupal path to the membership.
*/
public function path() {
$uri = $this->uri();
return $uri['path'];
}
/**
* Overrides Entity::defaultUri().
*/
public function defaultUri() {
return array('path' => 'membership/' . $this->mid);
}
/**
* Overrides Entity::save().
*/
public function save() {
$this->changed = REQUEST_TIME;
// If this is a new entity.
if (!isset($this->mid)) {
$this->is_new_revision = TRUE;
$this->created = REQUEST_TIME;
return parent::save();
}
// Load unchanged entity data.
if (!isset($this->original)) {
$original = entity_load_unchanged('membership_entity', $this->mid);
}
else {
$original = $this->original;
unset($this->original);
}
// The entity_type property cannot be changed in the UI, so we ignore
// it when comparing current and unchanged states.
if (isset($this->entity_type)) {
$original->entity_type = $this->entity_type;
}
// Compare new and old objects to check if any change was made.
if (md5(json_encode($this)) !== md5(json_encode($original))) {
// Flag to add a new revision.
$this->is_new_revision = TRUE;
}
// EntityAPIController will load original again if left unset.
$this->original = $original;
return parent::save();
}
}