1: <?php
2: /**
3: * Relationship class.
4: *
5: * @package Elgg.Core
6: * @subpackage Core
7: *
8: * @property int $id The unique identifier (read-only)
9: * @property int $guid_one The GUID of the subject of the relationship
10: * @property string $relationship The name of the relationship
11: * @property int $guid_two The GUID of the object of the relationship
12: * @property int $time_created A UNIX timestamp of when the relationship was created (read-only, set on first save)
13: */
14: class ElggRelationship extends ElggData implements
15: Importable
16: {
17:
18: /**
19: * Create a relationship object, optionally from a given id value or row.
20: *
21: * @param mixed $id ElggRelationship id, database row, or null for new relationship
22: */
23: function __construct($id = null) {
24: $this->initializeAttributes();
25:
26: if (!empty($id)) {
27: if ($id instanceof stdClass) {
28: $relationship = $id; // Create from db row
29: } else {
30: $relationship = get_relationship($id);
31: }
32:
33: if ($relationship) {
34: $objarray = (array) $relationship;
35: foreach ($objarray as $key => $value) {
36: $this->attributes[$key] = $value;
37: }
38: }
39: }
40: }
41:
42: /**
43: * Class member get overloading
44: *
45: * @param string $name Name
46: *
47: * @return mixed
48: */
49: function get($name) {
50: if (array_key_exists($name, $this->attributes)) {
51: return $this->attributes[$name];
52: }
53:
54: return null;
55: }
56:
57: /**
58: * Class member set overloading
59: *
60: * @param string $name Name
61: * @param mixed $value Value
62: *
63: * @return mixed
64: */
65: function set($name, $value) {
66: $this->attributes[$name] = $value;
67: return true;
68: }
69:
70: /**
71: * Save the relationship
72: *
73: * @return int the relationship id
74: * @throws IOException
75: */
76: public function save() {
77: global $CONFIG;
78: $guid_one = (int)$this->guid_one;
79: $relationship = sanitise_string($this->relationship);
80: $guid_two = (int)$this->guid_two;
81: $timestamp = $this->time_created;
82: if ($this->id > 0) {
83: $result = update_data(
84: "UPDATE ".$CONFIG->dbprefix."entity_relationships
85: SET guid_one = $guid_one,
86: relationship = '$relationship',
87: guid_two = $guid_two,
88: time_created = $timestamp
89: WHERE id = $this->id");
90: if(empty($result)){
91: return null;
92: }
93: } else{
94: $this->id = add_entity_relationship($this->guid_one, $this->relationship, $this->guid_two);
95: if (!$this->id) {
96: throw new IOException(elgg_echo('IOException:UnableToSaveNew', array(get_class())));
97: }
98: }
99: return $this->id;
100: }
101:
102: /**
103: * Delete a given relationship.
104: *
105: * @return bool
106: */
107: public function delete() {
108: return delete_relationship($this->id);
109: }
110:
111: /**
112: * Get a URL for this relationship.
113: *
114: * @return string
115: */
116: public function getURL() {
117: return get_relationship_url($this->id);
118: }
119:
120: // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
121:
122: /**
123: * Return an array of fields which can be exported.
124: *
125: * @return array
126: */
127: public function getExportableValues() {
128: return array(
129: 'id',
130: 'guid_one',
131: 'relationship',
132: 'guid_two'
133: );
134: }
135:
136: /**
137: * Export this relationship
138: *
139: * @return array
140: */
141: public function export() {
142: $uuid = get_uuid_from_object($this);
143: $relationship = new ODDRelationship(
144: guid_to_uuid($this->guid_one),
145: $this->relationship,
146: guid_to_uuid($this->guid_two)
147: );
148:
149: $relationship->setAttribute('uuid', $uuid);
150:
151: return $relationship;
152: }
153:
154: // IMPORTABLE INTERFACE ////////////////////////////////////////////////////////////
155:
156: /**
157: * Import a relationship
158: *
159: * @param ODD $data ODD data
160:
161: * @return bool
162: * @throws ImportException|InvalidParameterException
163: */
164: public function import(ODD $data) {
165: if (!($data instanceof ODDRelationship)) {
166: throw new InvalidParameterException(elgg_echo('InvalidParameterException:UnexpectedODDClass'));
167: }
168:
169: $uuid_one = $data->getAttribute('uuid1');
170: $uuid_two = $data->getAttribute('uuid2');
171:
172: // See if this entity has already been imported, if so then we need to link to it
173: $entity1 = get_entity_from_uuid($uuid_one);
174: $entity2 = get_entity_from_uuid($uuid_two);
175: if (($entity1) && ($entity2)) {
176: // Set the item ID
177: $this->attributes['guid_one'] = $entity1->getGUID();
178: $this->attributes['guid_two'] = $entity2->getGUID();
179:
180: // Map verb to relationship
181: //$verb = $data->getAttribute('verb');
182: //$relationship = get_relationship_from_verb($verb);
183: $relationship = $data->getAttribute('type');
184:
185: if ($relationship) {
186: $this->attributes['relationship'] = $relationship;
187: // save
188: $result = $this->save();
189: if (!$result) {
190: throw new ImportException(elgg_echo('ImportException:ProblemSaving', array(get_class())));
191: }
192:
193: return true;
194: }
195: }
196:
197: return false;
198: }
199:
200: // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
201:
202: /**
203: * Return an identification for the object for storage in the system log.
204: * This id must be an integer.
205: *
206: * @return int
207: */
208: public function getSystemLogID() {
209: return $this->id;
210: }
211:
212: /**
213: * For a given ID, return the object associated with it.
214: * This is used by the river functionality primarily.
215: * This is useful for checking access permissions etc on objects.
216: *
217: * @param int $id ID
218: *
219: * @return ElggRelationship
220: */
221: public function getObjectFromID($id) {
222: return get_relationship($id);
223: }
224:
225: /**
226: * Return a type of the object - eg. object, group, user, relationship, metadata, annotation etc
227: *
228: * @return string 'relationship'
229: */
230: public function getType() {
231: return 'relationship';
232: }
233:
234: /**
235: * Return a subtype. For metadata & annotations this is the 'name' and for relationship this
236: * is the relationship type.
237: *
238: * @return string
239: */
240: public function getSubtype() {
241: return $this->relationship;
242: }
243:
244: }
245: