aboutsummaryrefslogtreecommitdiff
path: root/mod/thewire/start.php
blob: 1c984d7927261bb164cb542ca04db45d39a0b73d (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
<?php

function rest_wire_post($username, $text) {
	login(get_user(2));
	
    $user = get_user_by_username($username);
    if (!$user) {
        throw new InvalidParameterException('Bad username');
    }

    $obj = new ElggObject();
    $obj->subtype = 'thewire';
    $obj->owner_guid = $user->guid;
    $obj->access_id = ACCESS_PUBLIC;
    $obj->method = 'api';
    $obj->description = elgg_substr(strip_tags($text), 0, 140);

    $guid = $obj->save();

    add_to_river('river/object/thewire/create',
                 'create',
                 $user->guid,
                 $obj->guid
                );

    return 'success';
}




	/**
	 * Elgg wire plugin
	 * The wire is simple twitter like plugin that allows users to post notes to the wire
	 * 
	 * @package ElggTheWire
	 */

	/**
	 * thewire initialisation
	 *
	 * These parameters are required for the event API, but we won't use them:
	 * 
	 * @param unknown_type $event
	 * @param unknown_type $object_type
	 * @param unknown_type $object
	 */

		function thewire_init() {
				
	expose_function('wire.post',
                'rest_wire_post',
                array( 'username' => array ('type' => 'string'),
                       'text' => array ('type' => 'string'),
                     ),
                'Post a status update to the wire',
                'POST',
                false,
                false);

// Set up menu for logged in users
				$item = new ElggMenuItem('thewire', elgg_echo('thewire:title'), 'pg/thewire');
				elgg_register_menu_item('site', $item);

			// Extend system CSS with our own styles, which are defined in the thewire/css view
				elgg_extend_view('css/screen', 'thewire/css');
				
			//extend views
				elgg_extend_view('profile/status', 'thewire/profile_status');
				
			// Register a page handler, so we can have nice URLs
				register_page_handler('thewire','thewire_page_handler');
				
			// Register a URL handler for thewire posts
				register_entity_url_handler('thewire_url','object','thewire');
				
			// Your thewire widget
				elgg_register_widget_type('thewire',elgg_echo("thewire:read"),elgg_echo("thewire:yourdesc"));
				
			// Register entity type
				register_entity_type('object','thewire');
				
			// Listen for SMS create event
			elgg_register_event_handler('create','object','thewire_incoming_sms');
			
			// Register granular notification for this type
			if (is_callable('register_notification_object'))
				register_notification_object('object', 'thewire', elgg_echo('thewire:newpost'));
			
			// Listen to notification events and supply a more useful message for SMS'
			elgg_register_plugin_hook_handler('notify:entity:message', 'object', 'thewire_notify_message');

			$action_path = elgg_get_plugins_path() . 'thewire/actions';
			elgg_register_action("thewire/add", "$action_path/add.php");
			elgg_register_action("thewire/delete", "$action_path/delete.php");
		}
		
		function thewire_pagesetup() {

			$base_url = elgg_get_site_url();

			//add submenu options
				if (elgg_get_context() == "thewire") {
					if ((elgg_get_page_owner_guid() == get_loggedin_userid() || !elgg_get_page_owner_guid()) && isloggedin()) {
						add_submenu_item(elgg_echo('thewire:read'),"{$base_url}pg/thewire/" . get_loggedin_user()->username);
						add_submenu_item(elgg_echo('thewire:everyone'),"{$base_url}mod/thewire/everyone.php");
					} 
				}
			
		}
		
		/**
		 * thewire page handler; allows the use of fancy URLs
		 *
		 * @param array $page From the page_handler function
		 * @return true|false Depending on success
		 */
		function thewire_page_handler($page) {
			
			// The first component of a thewire URL is the username
			if (isset($page[0])) {
				set_input('username',$page[0]);
			}
			
			// The second part dictates what we're doing
			if (isset($page[1])) {
				switch($page[1]) {
					case "friends":		// TODO: add friends thewire page here
										break;
				}
			// If the URL is just 'thewire/username', or just 'thewire/', load the standard thewire index
			} else {
				require(dirname(__FILE__) . "/index.php");
				return true;
			}
			
			return false;
			
		}

		function thewire_url($thewirepost) {
			return "pg/thewire/" . $thewirepost->getOwnerEntity()->username;
		}
		
		/**
		 * Returns a more meaningful message for SMS messages.
		 *
		 * @param unknown_type $hook
		 * @param unknown_type $entity_type
		 * @param unknown_type $returnvalue
		 * @param unknown_type $params
		 */
		function thewire_notify_message($hook, $entity_type, $returnvalue, $params)
		{
			$entity = $params['entity'];
			$to_entity = $params['to_entity'];
			$method = $params['method'];
			if (($entity instanceof ElggEntity) && ($entity->getSubtype() == 'thewire'))
			{
				$descr = $entity->description;
				if ($method == 'sms') {
					$owner = $entity->getOwnerEntity();
					return $owner->username . ': ' . $descr;
				}
				if ($method == 'email') {
					$owner = $entity->getOwnerEntity();
					return $owner->username . ': ' . $descr . "\n\n" . $entity->getURL();
				}
			}
			return null;
		}
		
		/**
		 * Create a new wire post.
		 *
		 * @param string $post The post
		 * @param int $access_id Public/private etc
		 * @param int $parent Parent post (if any)
		 * @param string $method The method (default: 'site')
		 * @return bool
		 */
		function thewire_save_post($post, $access_id, $parent=0, $method = "site")
		{
			
			global $SESSION; 
			
			// Initialise a new ElggObject
			$thewire = new ElggObject();
			
			// Tell the system it's a thewire post
			$thewire->subtype = "thewire";
			
			// Set its owner to the current user
			$thewire->owner_guid = get_loggedin_userid();
			
			// For now, set its access to public (we'll add an access dropdown shortly)
			$thewire->access_id = $access_id;
			
			// Set its description appropriately
			$thewire->description = elgg_substr(strip_tags($post), 0, 160);
			
			// add some metadata
			$thewire->method = $method; //method, e.g. via site, sms etc
			$thewire->parent = $parent; //used if the note is a reply
			
			//save
			$save = $thewire->save();

			if($save)
				add_to_river('river/object/thewire/create','create',$SESSION['user']->guid,$thewire->guid);
			
			return $save;

		}
		
		/**
		 * Listen and process incoming SMS'
		 */
		function thewire_incoming_sms($event, $object_type, $object)
		{
			if (($object) && ($object->subtype == get_subtype_id('object', 'sms')))
			{
				// Get user from phone number
				if ((is_plugin_enabled('smsclient')) && (is_plugin_enabled('smslogin')))
				{
					// By this stage the owner should be logged in (requires SMS Login)
					if (thewire_save_post($object->description, get_default_access(), 0, 'sms'))
						return false;
					
				}
			}
					
			return true; // always create the shout even if it can't be sent
		}
	
	// Make sure the thewire initialisation function is called on initialisation
		elgg_register_event_handler('init','system','thewire_init');
		elgg_register_event_handler('pagesetup','system','thewire_pagesetup');
		

		
?>