123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- class ElggRelationship extends \ElggData implements
- Importable // deprecated
- {
-
- const RELATIONSHIP_LIMIT = 50;
-
- public function __construct($row = null) {
- $this->initializeAttributes();
- if ($row === null) {
- elgg_deprecated_notice('Passing null to constructor is deprecated. Use add_entity_relationship()', 1.9);
- return;
- }
- if (!($row instanceof \stdClass)) {
- if (!is_numeric($row)) {
- throw new \InvalidArgumentException("Constructor accepts only a \stdClass or null.");
- }
- $id = (int)$row;
- elgg_deprecated_notice('Passing an ID to constructor is deprecated. Use get_relationship()', 1.9);
- $row = _elgg_get_relationship_row($id);
- if (!$row) {
- throw new \InvalidArgumentException("Relationship not found with ID $id");
- }
- }
- foreach ((array)$row as $key => $value) {
- $this->attributes[$key] = $value;
- }
- }
-
- protected function initializeAttributes() {
- parent::initializeAttributes();
- $this->attributes['id'] = null;
- $this->attributes['guid_one'] = null;
- $this->attributes['relationship'] = null;
- $this->attributes['guid_two'] = null;
- }
-
- public function __set($name, $value) {
- $this->attributes[$name] = $value;
- }
-
- public function set($name, $value) {
- elgg_deprecated_notice("Use -> instead of set()", 1.9);
- $this->__set($name, $value);
- return true;
- }
-
- public function __get($name) {
- if (array_key_exists($name, $this->attributes)) {
- return $this->attributes[$name];
- }
- return null;
- }
-
-
- public function get($name) {
- elgg_deprecated_notice("Use -> instead of get()", 1.9);
- return $this->__get($name);
- }
-
- public function save() {
- if ($this->id > 0) {
- delete_relationship($this->id);
- }
- $this->id = add_entity_relationship($this->guid_one, $this->relationship, $this->guid_two);
- if (!$this->id) {
- throw new \IOException("Unable to save new " . get_class());
- }
- return $this->id;
- }
-
- public function delete() {
- return delete_relationship($this->id);
- }
-
- public function getURL() {
- $url = '';
-
- if ($this->id) {
- global $CONFIG;
- $subtype = $this->getSubtype();
- $function = "";
- if (isset($CONFIG->relationship_url_handler[$subtype])) {
- $function = $CONFIG->relationship_url_handler[$subtype];
- }
- if (isset($CONFIG->relationship_url_handler['all'])) {
- $function = $CONFIG->relationship_url_handler['all'];
- }
- if (is_callable($function)) {
- $url = call_user_func($function, $this);
- }
- if ($url) {
- $url = elgg_normalize_url($url);
- }
- }
- $type = $this->getType();
- $params = array('relationship' => $this);
- $url = _elgg_services()->hooks->trigger('relationship:url', $type, $params, $url);
- return elgg_normalize_url($url);
- }
-
- public function toObject() {
- $object = new \stdClass();
- $object->id = $this->id;
- $object->subject_guid = $this->guid_one;
- $object->relationship = $this->relationship;
- $object->object_guid = $this->guid_two;
- $object->time_created = date('c', $this->getTimeCreated());
- $params = array('relationship' => $this);
- return _elgg_services()->hooks->trigger('to:object', 'relationship', $params, $object);
- }
-
-
- public function getExportableValues() {
- elgg_deprecated_notice(__METHOD__ . ' has been deprecated by toObject()', 1.9);
- return array(
- 'id',
- 'guid_one',
- 'relationship',
- 'guid_two'
- );
- }
-
- public function export() {
- elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
- $uuid = get_uuid_from_object($this);
- $relationship = new ODDRelationship(
- guid_to_uuid($this->guid_one),
- $this->relationship,
- guid_to_uuid($this->guid_two)
- );
- $relationship->setAttribute('uuid', $uuid);
- return $relationship;
- }
-
-
- public function import(ODD $data) {
- elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
- if (!($data instanceof ODDRelationship)) {
- throw new \InvalidParameterException("import() passed an unexpected ODD class");
- }
- $uuid_one = $data->getAttribute('uuid1');
- $uuid_two = $data->getAttribute('uuid2');
-
- $entity1 = get_entity_from_uuid($uuid_one);
- $entity2 = get_entity_from_uuid($uuid_two);
- if (($entity1) && ($entity2)) {
-
- $this->attributes['guid_one'] = $entity1->getGUID();
- $this->attributes['guid_two'] = $entity2->getGUID();
-
-
-
- $relationship = $data->getAttribute('type');
- if ($relationship) {
- $this->attributes['relationship'] = $relationship;
-
- $result = $this->save();
- if (!$result) {
- throw new \ImportException("There was a problem saving " . get_class());
- }
- return true;
- }
- }
- return false;
- }
-
-
- public function getSystemLogID() {
- return $this->id;
- }
-
- public function getObjectFromID($id) {
- return get_relationship($id);
- }
-
- public function getType() {
- return 'relationship';
- }
-
- public function getSubtype() {
- return $this->relationship;
- }
- }
|