aboutsummaryrefslogtreecommitdiff
path: root/start.php
blob: abf285f7ebe7b13d4fc254207585956232bae4db (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
253
254
255
256
257
258
259
<?php
	/**
	 * Elgg tidypics
	 * 
	 */

	/**
	 * tidypics plugin initialisation functions.
	 */
	function tidypics_init() 
	{
		global $CONFIG;
				
		// Set up menu for logged in users
		if (isloggedin()) 
		{
			add_menu(elgg_echo('photos'), $CONFIG->wwwroot . "pg/photos/owned/" . $_SESSION['user']->username);
		}
				
		// Extend CSS
		extend_view('css', 'tidypics/css');
		
		// Extend hover-over and profile menu
		extend_view('profile/menu/links','tidypics/menu');
		
		//group view  ** psuedo widget view for group pages**
		extend_view('groups/right_column','tidypics/groupprofile_albums');
		
		// Register a page handler, so we can have nice URLs
		register_page_handler('photos','tidypics_page_handler');
			
		// Add a new tidypics widget
		add_widget_type('album_view', elgg_echo("album:widget"), elgg_echo("album:widget:description"), 'profile');
		
		// Register a URL handler for files
		register_entity_url_handler('image_url','object','image');
		register_entity_url_handler('album_url','object','album');

		// Register entity type
		register_entity_type('object','image');
		register_entity_type('object','album');
		
		add_group_tool_option('photos',elgg_echo('tidypics:enablephotos'),true);
		
		register_plugin_hook('permissions_check', 'object', 'tidypics_permission_override');
		
		// Register for notifications 
		if (is_callable('register_notification_object')) {
			register_notification_object('object', 'album', elgg_echo('tidypics:newalbum'));
			
			register_plugin_hook('notify:entity:message', 'object', 'tidypics_notify_message');
		}
	}
	
	/**
	 * Sets up submenus for tidypics.  Triggered on pagesetup.
	 */
	function tidypics_submenus() {
		
		global $CONFIG;
		
		$page_owner = page_owner_entity();
		
		if ($page_owner instanceof ElggGroup) {
			if (get_context() == "groups") {
				if ($page_owner->photos_enable != "no") {
					add_submenu_item(	sprintf(elgg_echo('album:group'),$page_owner->name), 
										$CONFIG->wwwroot . "pg/photos/owned/" . $page_owner->username);
				}
			}
		}
		// context is only set to photos on individual pages, not on group pages		
		else if (get_context() == "photos") {
			
			// owner gets "your albumn", "your friends albums"
			if (get_loggedin_userid() == $page_owner->guid && get_loggedin_userid()) {
				add_submenu_item(	elgg_echo('album:create'), 
									$CONFIG->wwwroot . "pg/photos/new/". $page_owner->username, 
									'tidypics' );

				add_submenu_item(	elgg_echo("album:yours"), 
									$CONFIG->wwwroot . "pg/photos/owned/" . $_SESSION['user']->username, 
									'tidypics' );

				add_submenu_item( 	elgg_echo('album:yours:friends'), 
									$CONFIG->wwwroot . "pg/photos/friends/". $page_owner->username, 
									'tidypics');
			} else if (isloggedin()) {
				// logged nut not owner gets "your albums", "page owners albums", "page owner's friends albums"
				add_submenu_item(	elgg_echo("album:yours"), 
									$CONFIG->wwwroot . "pg/photos/owned/" . $_SESSION['user']->username, 
									'tidypics' );
				add_submenu_item(	sprintf(elgg_echo("album:user"), $page_owner->name), 
									$CONFIG->wwwroot . "pg/photos/owned/" . $page_owner->username, 
									'tidypics' );
				add_submenu_item( 	sprintf(elgg_echo('album:friends'),$page_owner->name), 
									$CONFIG->wwwroot . "pg/photos/friends/". $page_owner->username, 
									'tidypics');
			} else if ($page_owner->guid) {
				// non logged in user gets "page owners albums", "page owner's friends albums" 
				add_submenu_item(	sprintf(elgg_echo("album:user"), $page_owner->name), 
									$CONFIG->wwwroot . "pg/photos/owned/" . $page_owner->username, 
									'tidypics' );
				add_submenu_item( 	sprintf(elgg_echo('album:friends'),$page_owner->name), 
									$CONFIG->wwwroot . "pg/photos/friends/". $page_owner->username, 
									'tidypics');
			}
			
			add_submenu_item(	sprintf(elgg_echo('album:all'),$page_owner->name), 
								$CONFIG->wwwroot . "pg/photos/world/", 
								'tidypics');
		}
		
	}

	/**
	 * tidypics page handler
	 *
	 * @param array $page Array of page elements, forwarded by the page handling mechanism
	 */
	function tidypics_page_handler($page) {
		
		global $CONFIG;
		
		if (isset($page[0])) 
		{
			switch($page[0]) 
			{
				case "owned":  //view list of albums owned by container
					if (isset($page[1])) set_input('username',$page[1]);
					include($CONFIG->pluginspath . "tidypics/index.php");
				break;

				case "view": //view an image individually
					if (isset($page[1])) set_input('guid',$page[1]);
					include($CONFIG->pluginspath . "tidypics/viewimage.php");
				break;

				case "album": //view an album individually
					if (isset($page[1])) set_input('guid',$page[1]);
					include($CONFIG->pluginspath . "tidypics/viewalbum.php");
				break;

				case "new":  //create new album
					if (isset($page[1])) set_input('username',$page[1]); 
					include($CONFIG->pluginspath . "tidypics/newalbum.php");
				break;

				case "upload": //upload images to album
					if (isset($page[1])) set_input('container_guid',$page[1]);
					include($CONFIG->pluginspath . "tidypics/upload.php");
				break;

				case "edit": //edit image or album
					if (isset($page[1])) set_input('guid',$page[1]);
					include($CONFIG->pluginspath . "tidypics/edit.php");
				break;

				case "delete": //edit image or album
					if (isset($page[1])) set_input('guid',$page[1]);
					include($CONFIG->pluginspath . "tidypics/actions/delete.php");
				break;

				case "friends": 
					if (isset($page[1])) set_input('username',$page[1]);
					include($CONFIG->pluginspath . "tidypics/friends.php");
				break;

				case "world":  
					include($CONFIG->pluginspath . "tidypics/world.php");
				break;

			}
		}
		else
		{
			// going to the index because something is wrong with the page handler 
			include($CONFIG->pluginspath . "tidypics/index.php");
		}
		
	}

	/**
	 * Override permissions for group albums and images
	 *
	 */
	function tidypics_permission_override($hook, $entity_type, $returnvalue, $params)
	{
		$entity = $params['entity'];
		$user   = $params['user'];
		
		if ($entity->subtype == get_subtype_id('object', 'album')) {
			// test that the user can edit the container
			return can_write_to_container(0, $entity->container_guid);
		}

		if ($entity->subtype == get_subtype_id('object', 'image')) {
			// test that the user can edit the container
			return can_write_to_container(0, $entity->container_guid);
		}

		return false;
	}
	
	
	/**
	 * Notification message handler
	 */
	function tidypics_notify_message($hook, $entity_type, $returnvalue, $params)
	{
		$entity = $params['entity'];
		$to_entity = $params['to_entity'];
		$method = $params['method'];
		if (($entity instanceof ElggEntity) && ($entity->getSubtype() == 'album'))
		{
			$descr = $entity->description;
			$title = $entity->title;
			if ($method == 'email') {
				$owner = $entity->getOwnerEntity();
				return sprintf(elgg_echo('album:river:created'), $owner->username) . $title . "\n\n" . $descr . "\n\n" . $entity->getURL();
			}
		}
		return null;
	}
	
	/**
	 * Populates the ->getUrl() method for file objects
	 *
	 * @param ElggEntity $entity album/image entity
	 * @return string File URL
	 */
	function image_url($entity) {
		global $CONFIG;
		$title = $entity->title;
		$title = friendly_title($title);
		return $CONFIG->url . "pg/photos/view/" . $entity->getGUID() . "/" . $title;
	}
		
	function album_url($entity) {
		global $CONFIG;
		$title = $entity->title;
		$title = friendly_title($title);
		return $CONFIG->url . "pg/photos/album/" . $entity->getGUID() . "/" . $title;	
	}
	
	// Make sure tidypics_init is called on initialisation
	register_elgg_event_handler('init','system','tidypics_init');
	register_elgg_event_handler('pagesetup','system','tidypics_submenus');
	
	// Register actions
	register_action("tidypics/upload", false, $CONFIG->pluginspath . "tidypics/actions/upload.php");
	register_action("tidypics/addalbum", false, $CONFIG->pluginspath. "tidypics/actions/addalbum.php");
	register_action("tidypics/edit", false, $CONFIG->pluginspath. "tidypics/actions/edit.php");
	register_action("tidypics/delete", false, $CONFIG->pluginspath. "tidypics/actions/delete.php");
	register_action("tidypics/icon", true, $CONFIG->pluginspath. "tidypics/actions/icon.php");
	register_action("tidypics/edit_multi", false, $CONFIG->pluginspath. "tidypics/actions/edit_multi.php");
	register_action("tidypics/download", true, $CONFIG->pluginspath . "tidypics/actions/download.php");

?>