Issues (946)

engine/classes/ElggMetadata.php (2 issues)

1
<?php
2
3
/**
4
 * ElggMetadata
5
 *
6
 * This class describes metadata that can be attached to an \ElggEntity.
7
 * It is rare that a plugin developer needs to use this API for metadata.
8
 * Almost all interaction with metadata occurs through the methods of \ElggEntity.
9
 * See its __set(), __get(), and setMetadata() methods.
10
 */
11
class ElggMetadata extends ElggExtender {
12
13
	/**
14
	 * {@inheritdoc}
15
	 */
16 1399
	protected function initializeAttributes() {
17 1399
		parent::initializeAttributes();
18
19 1399
		$this->attributes['type'] = "metadata";
20 1399
	}
21
22
	/**
23
	 * Constructor
24
	 *
25
	 * @param stdClass $row Database row
26
	 */
27 1399
	public function __construct(stdClass $row = null) {
28 1399
		$this->initializeAttributes();
29
30 1399
		if ($row) {
31 279
			foreach ((array) $row as $key => $value) {
32 279
				$this->$key = $value;
33
			}
34
		}
35
36 1399
		$this->access_id = ACCESS_PUBLIC;
37 1399
	}
38
39
	/**
40
	 * Determines whether or not the user can edit this piece of metadata
41
	 *
42
	 * @param int $user_guid The GUID of the user (defaults to currently logged in user)
43
	 *
44
	 * @return bool
45
	 */
46 1
	public function canEdit($user_guid = 0) {
47 1
		if ($entity = get_entity($this->entity_guid)) {
48 1
			return $entity->canEditMetadata($this, $user_guid);
49
		}
50
51
		return false;
52
	}
53
54
	/**
55
	 * Save metadata object
56
	 *
57
	 * Returns metadata on success, false on failure
58
	 *
59
	 * @return int|false
60
	 */
61 25
	public function save() {
62 25
		if (!$this->id) {
63 25
			return _elgg_services()->metadataTable->create($this);
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression return _elgg_services()-...ataTable->create($this) also could return the type integer which is incompatible with the return type mandated by ElggData::save() of boolean.
Loading history...
64
		}
65
66
		return _elgg_services()->metadataTable->update($this);
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression return _elgg_services()-...ataTable->update($this) returns the type boolean which is incompatible with the documented return type false|integer.
Loading history...
67
	}
68
69
	/**
70
	 * Delete the metadata
71
	 *
72
	 * @return bool
73
	 */
74 265
	public function delete() {
75 265
		return _elgg_services()->metadataTable->delete($this);
76
	}
77
78
	/**
79
	 * {@inheritdoc}
80
	 */
81 1
	public function getObjectFromID($id) {
82 1
		return elgg_get_metadata_from_id($id);
83
	}
84
}
85