ElggRelationship.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 type of the relationship (limit of 50 characters long)
  11. * @property int $guid_two The GUID of the target 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 // deprecated
  16. {
  17. // database column limit
  18. const RELATIONSHIP_LIMIT = 50;
  19. /**
  20. * Create a relationship object
  21. *
  22. * @param \stdClass $row Database row or null for new relationship
  23. * @throws InvalidArgumentException
  24. */
  25. public function __construct($row = null) {
  26. $this->initializeAttributes();
  27. if ($row === null) {
  28. elgg_deprecated_notice('Passing null to constructor is deprecated. Use add_entity_relationship()', 1.9);
  29. return;
  30. }
  31. if (!($row instanceof \stdClass)) {
  32. if (!is_numeric($row)) {
  33. throw new \InvalidArgumentException("Constructor accepts only a \stdClass or null.");
  34. }
  35. $id = (int)$row;
  36. elgg_deprecated_notice('Passing an ID to constructor is deprecated. Use get_relationship()', 1.9);
  37. $row = _elgg_get_relationship_row($id);
  38. if (!$row) {
  39. throw new \InvalidArgumentException("Relationship not found with ID $id");
  40. }
  41. }
  42. foreach ((array)$row as $key => $value) {
  43. $this->attributes[$key] = $value;
  44. }
  45. }
  46. /**
  47. * (non-PHPdoc)
  48. *
  49. * @see \ElggData::initializeAttributes()
  50. *
  51. * @return void
  52. */
  53. protected function initializeAttributes() {
  54. parent::initializeAttributes();
  55. $this->attributes['id'] = null;
  56. $this->attributes['guid_one'] = null;
  57. $this->attributes['relationship'] = null;
  58. $this->attributes['guid_two'] = null;
  59. }
  60. /**
  61. * Set an attribute of the relationship
  62. *
  63. * @param string $name Name
  64. * @param mixed $value Value
  65. * @return void
  66. */
  67. public function __set($name, $value) {
  68. $this->attributes[$name] = $value;
  69. }
  70. /**
  71. * Class member set overloading
  72. *
  73. * @param string $name Name
  74. * @param mixed $value Value
  75. * @return mixed
  76. * @deprecated 1.9
  77. */
  78. public function set($name, $value) {
  79. elgg_deprecated_notice("Use -> instead of set()", 1.9);
  80. $this->__set($name, $value);
  81. return true;
  82. }
  83. /**
  84. * Get an attribute of the relationship
  85. *
  86. * @param string $name Name
  87. * @return mixed
  88. */
  89. public function __get($name) {
  90. if (array_key_exists($name, $this->attributes)) {
  91. return $this->attributes[$name];
  92. }
  93. return null;
  94. }
  95. /**
  96. * Class member get overloading
  97. *
  98. * @param string $name Name
  99. * @return mixed
  100. * @deprecated 1.9
  101. */
  102. public function get($name) {
  103. elgg_deprecated_notice("Use -> instead of get()", 1.9);
  104. return $this->__get($name);
  105. }
  106. /**
  107. * Save the relationship
  108. *
  109. * @return int the relationship ID
  110. * @throws IOException
  111. */
  112. public function save() {
  113. if ($this->id > 0) {
  114. delete_relationship($this->id);
  115. }
  116. $this->id = add_entity_relationship($this->guid_one, $this->relationship, $this->guid_two);
  117. if (!$this->id) {
  118. throw new \IOException("Unable to save new " . get_class());
  119. }
  120. return $this->id;
  121. }
  122. /**
  123. * Delete this relationship from the database.
  124. *
  125. * @return bool
  126. */
  127. public function delete() {
  128. return delete_relationship($this->id);
  129. }
  130. /**
  131. * Get a URL for this relationship.
  132. *
  133. * Plugins can register for the 'relationship:url', 'relationship' plugin hook to
  134. * customize the url for a relationship.
  135. *
  136. * @return string
  137. */
  138. public function getURL() {
  139. $url = '';
  140. // @todo remove when elgg_register_relationship_url_handler() has been removed
  141. if ($this->id) {
  142. global $CONFIG;
  143. $subtype = $this->getSubtype();
  144. $function = "";
  145. if (isset($CONFIG->relationship_url_handler[$subtype])) {
  146. $function = $CONFIG->relationship_url_handler[$subtype];
  147. }
  148. if (isset($CONFIG->relationship_url_handler['all'])) {
  149. $function = $CONFIG->relationship_url_handler['all'];
  150. }
  151. if (is_callable($function)) {
  152. $url = call_user_func($function, $this);
  153. }
  154. if ($url) {
  155. $url = elgg_normalize_url($url);
  156. }
  157. }
  158. $type = $this->getType();
  159. $params = array('relationship' => $this);
  160. $url = _elgg_services()->hooks->trigger('relationship:url', $type, $params, $url);
  161. return elgg_normalize_url($url);
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function toObject() {
  167. $object = new \stdClass();
  168. $object->id = $this->id;
  169. $object->subject_guid = $this->guid_one;
  170. $object->relationship = $this->relationship;
  171. $object->object_guid = $this->guid_two;
  172. $object->time_created = date('c', $this->getTimeCreated());
  173. $params = array('relationship' => $this);
  174. return _elgg_services()->hooks->trigger('to:object', 'relationship', $params, $object);
  175. }
  176. // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
  177. /**
  178. * Return an array of fields which can be exported.
  179. *
  180. * @return array
  181. * @deprecated 1.9 Use toObject()
  182. */
  183. public function getExportableValues() {
  184. elgg_deprecated_notice(__METHOD__ . ' has been deprecated by toObject()', 1.9);
  185. return array(
  186. 'id',
  187. 'guid_one',
  188. 'relationship',
  189. 'guid_two'
  190. );
  191. }
  192. /**
  193. * Export this relationship
  194. *
  195. * @return array
  196. * @deprecated 1.9 Use toObject()
  197. */
  198. public function export() {
  199. elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
  200. $uuid = get_uuid_from_object($this);
  201. $relationship = new ODDRelationship(
  202. guid_to_uuid($this->guid_one),
  203. $this->relationship,
  204. guid_to_uuid($this->guid_two)
  205. );
  206. $relationship->setAttribute('uuid', $uuid);
  207. return $relationship;
  208. }
  209. // IMPORTABLE INTERFACE ////////////////////////////////////////////////////////////
  210. /**
  211. * Import a relationship
  212. *
  213. * @param ODD $data ODD data
  214. * @return bool
  215. * @throws ImportException|InvalidParameterException
  216. * @deprecated 1.9
  217. */
  218. public function import(ODD $data) {
  219. elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
  220. if (!($data instanceof ODDRelationship)) {
  221. throw new \InvalidParameterException("import() passed an unexpected ODD class");
  222. }
  223. $uuid_one = $data->getAttribute('uuid1');
  224. $uuid_two = $data->getAttribute('uuid2');
  225. // See if this entity has already been imported, if so then we need to link to it
  226. $entity1 = get_entity_from_uuid($uuid_one);
  227. $entity2 = get_entity_from_uuid($uuid_two);
  228. if (($entity1) && ($entity2)) {
  229. // Set the item ID
  230. $this->attributes['guid_one'] = $entity1->getGUID();
  231. $this->attributes['guid_two'] = $entity2->getGUID();
  232. // Map verb to relationship
  233. //$verb = $data->getAttribute('verb');
  234. //$relationship = get_relationship_from_verb($verb);
  235. $relationship = $data->getAttribute('type');
  236. if ($relationship) {
  237. $this->attributes['relationship'] = $relationship;
  238. // save
  239. $result = $this->save();
  240. if (!$result) {
  241. throw new \ImportException("There was a problem saving " . get_class());
  242. }
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. // SYSTEM LOG INTERFACE ////////////////////////////////////////////////////////////
  249. /**
  250. * Return an identification for the object for storage in the system log.
  251. * This id must be an integer.
  252. *
  253. * @return int
  254. */
  255. public function getSystemLogID() {
  256. return $this->id;
  257. }
  258. /**
  259. * For a given ID, return the object associated with it.
  260. * This is used by the river functionality primarily.
  261. * This is useful for checking access permissions etc on objects.
  262. *
  263. * @param int $id ID
  264. *
  265. * @return \ElggRelationship
  266. */
  267. public function getObjectFromID($id) {
  268. return get_relationship($id);
  269. }
  270. /**
  271. * Return a type of the object - eg. object, group, user, relationship, metadata, annotation etc
  272. *
  273. * @return string 'relationship'
  274. */
  275. public function getType() {
  276. return 'relationship';
  277. }
  278. /**
  279. * Return a subtype. For metadata & annotations this is the 'name' and for relationship this
  280. * is the relationship type.
  281. *
  282. * @return string
  283. */
  284. public function getSubtype() {
  285. return $this->relationship;
  286. }
  287. }