1: <?php
2: /**
3: * Elgg Object
4: *
5: * Elgg objects are the most common means of storing information in the database.
6: * They are a child class of ElggEntity, so receive all the benefits of the Entities,
7: * but also include a title and description field.
8: *
9: * An ElggObject represents a row from the objects_entity table, as well
10: * as the related row in the entities table as represented by the parent
11: * ElggEntity object.
12: *
13: * @internal Title and description are stored in the objects_entity table.
14: *
15: * @package Elgg.Core
16: * @subpackage DataModel.Object
17: *
18: * @property string $title The title, name, or summary of this object
19: * @property string $description The body, description, or content of the object
20: * @property array $tags Array of tags that describe the object
21: */
22: class ElggObject extends ElggEntity {
23:
24: /**
25: * Initialise the attributes array to include the type,
26: * title, and description.
27: *
28: * @return void
29: */
30: protected function initializeAttributes() {
31: parent::initializeAttributes();
32:
33: $this->attributes['type'] = "object";
34: $this->attributes['title'] = NULL;
35: $this->attributes['description'] = NULL;
36: $this->attributes['tables_split'] = 2;
37: }
38:
39: /**
40: * Load or create a new ElggObject.
41: *
42: * If no arguments are passed, create a new entity.
43: *
44: * If an argument is passed, attempt to load a full ElggObject entity.
45: * Arguments can be:
46: * - The GUID of an object entity.
47: * - A DB result object from the entities table with a guid property
48: *
49: * @param mixed $guid If an int, load that GUID. If a db row, then will attempt to
50: * load the rest of the data.
51: *
52: * @throws IOException If passed an incorrect guid
53: * @throws InvalidParameterException If passed an Elgg* Entity that isn't an ElggObject
54: */
55: function __construct($guid = null) {
56: $this->initializeAttributes();
57:
58: // compatibility for 1.7 api.
59: $this->initialise_attributes(false);
60:
61: if (!empty($guid)) {
62: // Is $guid is a DB row from the entity table
63: if ($guid instanceof stdClass) {
64: // Load the rest
65: if (!$this->load($guid)) {
66: $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid));
67: throw new IOException($msg);
68: }
69: } else if ($guid instanceof ElggObject) {
70: // $guid is an ElggObject so this is a copy constructor
71: elgg_deprecated_notice('This type of usage of the ElggObject constructor was deprecated. Please use the clone method.', 1.7);
72:
73: foreach ($guid->attributes as $key => $value) {
74: $this->attributes[$key] = $value;
75: }
76: } else if ($guid instanceof ElggEntity) {
77: // @todo remove - do not need separate exception
78: throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggObject'));
79: } else if (is_numeric($guid)) {
80: // $guid is a GUID so load
81: if (!$this->load($guid)) {
82: throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid)));
83: }
84: } else {
85: throw new InvalidParameterException(elgg_echo('InvalidParameterException:UnrecognisedValue'));
86: }
87: }
88: }
89:
90: /**
91: * Loads the full ElggObject when given a guid.
92: *
93: * @param mixed $guid GUID of an ElggObject or the stdClass object from entities table
94: *
95: * @return bool
96: * @throws InvalidClassException
97: */
98: protected function load($guid) {
99: $attr_loader = new ElggAttributeLoader(get_class(), 'object', $this->attributes);
100: $attr_loader->requires_access_control = !($this instanceof ElggPlugin);
101: $attr_loader->secondary_loader = 'get_object_entity_as_row';
102:
103: $attrs = $attr_loader->getRequiredAttributes($guid);
104: if (!$attrs) {
105: return false;
106: }
107:
108: $this->attributes = $attrs;
109: $this->attributes['tables_loaded'] = 2;
110: _elgg_cache_entity($this);
111:
112: return true;
113: }
114:
115: /**
116: * Saves object-specific attributes.
117: *
118: * @internal Object attributes are saved in the objects_entity table.
119: *
120: * @return bool
121: */
122: public function save() {
123: // Save ElggEntity attributes
124: if (!parent::save()) {
125: return false;
126: }
127:
128: // Save ElggObject-specific attributes
129:
130: _elgg_disable_caching_for_entity($this->guid);
131: $ret = create_object_entity($this->get('guid'), $this->get('title'), $this->get('description'));
132: _elgg_enable_caching_for_entity($this->guid);
133:
134: return $ret;
135: }
136:
137: /**
138: * Return sites that this object is a member of
139: *
140: * Site membership is determined by relationships and not site_guid.d
141: *
142: * @todo This should be moved to ElggEntity
143: * @todo Unimplemented
144: *
145: * @param string $subtype Optionally, the subtype of result we want to limit to
146: * @param int $limit The number of results to return
147: * @param int $offset Any indexing offset
148: *
149: * @return array|false
150: */
151: function getSites($subtype = "", $limit = 10, $offset = 0) {
152: return get_site_objects($this->getGUID(), $subtype, $limit, $offset);
153: }
154:
155: /**
156: * Add this object to a site
157: *
158: * @param int $site_guid The guid of the site to add it to
159: *
160: * @return bool
161: */
162: function addToSite($site_guid) {
163: return add_site_object($this->getGUID(), $site_guid);
164: }
165:
166: /*
167: * EXPORTABLE INTERFACE
168: */
169:
170: /**
171: * Return an array of fields which can be exported.
172: *
173: * @return array
174: */
175: public function getExportableValues() {
176: return array_merge(parent::getExportableValues(), array(
177: 'title',
178: 'description',
179: ));
180: }
181:
182: /**
183: * Can a user comment on this object?
184: *
185: * @see ElggEntity::canComment()
186: *
187: * @param int $user_guid User guid (default is logged in user)
188: * @return bool
189: * @since 1.8.0
190: */
191: public function canComment($user_guid = 0) {
192: $result = parent::canComment($user_guid);
193: if ($result !== null) {
194: return $result;
195: }
196:
197: if ($user_guid == 0) {
198: $user_guid = elgg_get_logged_in_user_guid();
199: }
200:
201: // must be logged in to comment
202: if (!$user_guid) {
203: return false;
204: }
205:
206: // must be member of group
207: if (elgg_instanceof($this->getContainerEntity(), 'group')) {
208: if (!$this->getContainerEntity()->canWriteToContainer($user_guid)) {
209: return false;
210: }
211: }
212:
213: // no checks on read access since a user cannot see entities outside his access
214: return true;
215: }
216: }
217: