aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/river2.php
blob: 577cefc165a0562b0d5858245595b714efa8f5a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php

	/**
	 * Elgg river 2.0.
	 * Functions for listening for and generating the river separately from the system log.
	 * 
	 * @package Elgg
	 * @subpackage Core
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Curverider Ltd
	 * @copyright Curverider Ltd 2008-2009
	 * @link http://elgg.org/
	 */

	/**
	 * Adds an item to the river.
	 *
	 * @param string $view The view that will handle the river item (must exist)
	 * @param string $action_type An arbitrary one-word string to define the action (eg 'comment', 'create')
	 * @param int $subject_guid The GUID of the entity doing the action
	 * @param int $object_guid The GUID of the entity being acted upon
	 * @param int $access_id The access ID of the river item (default: same as the object) 
	 * @param int $posted The UNIX epoch timestamp of the river item (default: now)
	 * @return true|false Depending on success
	 */
		function add_to_river(
								$view,
								$action_type,
								$subject_guid,
								$object_guid,
								$access_id = -1,
								$posted = 0
							  ) {
							  	
			// Sanitise variables
				if (!elgg_view_exists($view)) return false;
				if (!($subject = get_entity($subject_guid))) return false;
				if (!($object = get_entity($object_guid))) return false;
				if (empty($action_type)) return false;
				if ($posted == 0) $posted = time();
				if ($access_id == -1) $access_id = $object->access_id;
				
				$type = $object->getType();
				$subtype = $object->getSubtype();
				
				$action_type = sanitise_string($action_type);
				
			// Load config
				global $CONFIG;
				
			// Attempt to save river item; return success status
				return insert_data("insert into {$CONFIG->dbprefix}river " .
										" set type = '{$type}', " .
										" subtype = '{$subtype}', " .
										" action_type = '{$action_type}', " .
										" access_id = {$access_id}, " .
										" view = '{$view}', " .
										" subject_guid = {$subject_guid}, " .
										" object_guid = {$object_guid}, " .
										" posted = {$posted} ");
							  	
		}
		
	/**
	 * Removes all items relating to a particular acting entity from the river
	 *
	 * @param int $subject_guid The GUID of the entity
	 * @return true|false Depending on success
	 */
		function remove_from_river_by_subject(
									$subject_guid
											) {
			
			// Sanitise
				$subject_guid = (int) $subject_guid;
				
			// Load config
				global $CONFIG;
				
			// Remove
				return delete_data("delete from {$CONFIG->dbprefix}river where subject_guid = {$subject_guid}");
												
		}
		
	/**
	 * Removes all items relating to a particular entity being acted upon from the river
	 *
	 * @param int $object_guid The GUID of the entity
	 * @return true|false Depending on success
	 */
		function remove_from_river_by_object(
									$object_guid
											) {
			
			// Sanitise
				$object_guid = (int) $object_guid;
				
			// Load config
				global $CONFIG;
				
			// Remove
				return delete_data("delete from {$CONFIG->dbprefix}river where object_guid = {$object_guid}");
												
		}
		
	/**
	 * Retrieves items from the river. All parameters are optional.
	 *
	 * @param int|array $subject_guid Acting entity to restrict to. Default: all
	 * @param int|array $object_guid Entity being acted on to restrict to. Default: all
	 * @param string $subject_relationship If set to a relationship type, this will use $subject_guid as the starting point and set the subjects to be all users this entity has this relationship with (eg 'friend'). Default: blank
	 * @param string $type The type of entity to restrict to. Default: all
	 * @param string $subtype The subtype of entity to restrict to. Default: all
	 * @param string $action_type The type of river action to restrict to. Default: all
	 * @param int $limit The number of items to retrieve. Default: 20
	 * @param int $offset The page offset. Default: 0
	 * @param int $posted_min The minimum time period to look at. Default: none
	 * @param int $posted_max The maximum time period to look at. Default: none
	 * @return array|false Depending on success
	 */
		function get_river_items(
							$subject_guid = 0,
							$object_guid = 0,
							$subject_relationship = '',
							$type = '',
							$subtype = '',
							$action_type = '',
							$limit = 20,
							$offset = 0,
							$posted_min = 0,
							$posted_max = 0
                         ) {
                         	
			// Sanitise variables
				if (!is_array($subject_guid)) {
					$subject_guid = (int) $subject_guid;
				} else {
					foreach($subject_guid as $key => $temp) {
						$subject_guid[$key] = (int) $temp;
					}
				}
                if (!is_array($object_guid)) {
					$object_guid = (int) $object_guid;
				} else {
					foreach($object_guid as $key => $temp) {
						$object_guid[$key] = (int) $temp;
					}
				}
				if (!empty($type)) $action_type = sanitise_string($type);
				if (!empty($subtype)) $action_type = sanitise_string($subtype);
				if (!empty($action_type)) $action_type = sanitise_string($action_type);
				$limit = (int) $limit;
				$offset = (int) $offset;
				$posted_min = (int) $posted_min;
				$posted_max = (int) $posted_max;
				
			// Construct 'where' clauses for the river
				$where = array();
				$where[] = get_access_sql_suffix();
				
				if (empty($subject_relationship)) {
					if (!empty($subject_guid))
						if (!is_array($subject_guid)) {
							$where[] = " subject_guid = {$subject_guid} ";
						} else {
							$where[] = " subject_guid in (" . implode(',',$subject_guid) . ") ";
						}
				} else {
					if (!is_array($subject_guid))
						if ($entities = get_entities_from_relationship($subject_relationship,$subject_guid,false,'','',0,'',9999)) {
							$guids = array();
							foreach($entities as $entity) $guids[] = (int) $entity->guid;
							$guids[] = $subject_guid;
							$where[] = " subject_guid in (" . implode(',',$guids) . ") "; 
						}
				}
				if (!empty($object_guid))
	                if (!is_array($object_guid)) {
						$where[] = " object_guid = {$object_guid} ";
					} else {
						$where[] = " object_guid in (" . implode(',',$object_guid) . ") ";
					}
				if (!empty($type)) $where[] = " type = '{$type}' ";
				if (!empty($subtype)) $where[] = " subtype = '{$subtype}' ";
				if (!empty($action_type)) $where[] = " action_type = '{$action_type}' ";
				if (!empty($posted_min)) $where[] = " posted > {$posted_min} ";
				if (!empty($posted_max)) $where[] = " posted < {$posted_max} ";
				
				$whereclause = implode(' and ', $where);
				
			// Construct main SQL
				$sql = "select id,type,subtype,action_type,access_id,view,subject_guid,object_guid,posted from {$CONFIG->dbprefix}river where {$whereclause} limit {$offset},{$limit}";
				
			// Get data
				return get_data($sql);
			
		}
		
	/**
	 * Returns a human-readable representation of a river item
	 *
	 * @see get_river_items
	 * 
	 * @param stdClass $item A river item object as returned from get_river_items
	 * @return string|false Depending on success
	 */
		function elgg_view_river_item($item) {
			if (isset($item->view)) {

				if (elgg_view_exists($item->view)) {
					$body = elgg_view($item->view,array(
										'item' => $item
									 ));
				}
				return elgg_view('river/item/wrapper',array(
									'item' => $item,
									'body' => $body
								 ));
				
			}
			return false;
		}
		
		function elgg_view_river_items($subject_guid = 0,
							$object_guid = 0,
							$subject_relationship = '',
							$type = '',
							$subtype = '',
							$action_type = '',
							$limit = 20,
							$posted_min = 0,
							$posted_max = 0) {
								
			// Get input from outside world and sanitise it
				$offset = (int) get_input('offset',0);
				
			// Get river items, if they exist
				if ($riveritems = get_river_items($subject_guid,$object_guid,$subject_relationship,$type,$subtype,$action_type,$limit,$offset,$posted_min,$posted_max)) {

					return elgg_view('river/item/list',array(
											'limit' => $limit,
											'offset' => $offset,
											'items' => $riveritems,
										));
					
				}
				
			return '';
				
		}

?>