aboutsummaryrefslogtreecommitdiff
path: root/mod/lightbox/start.php
blob: c79ab100b18f6639566647e811e2769a4110893d (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
<?php
/**
 * Elgg lightbox plugin
 *
 * @package ElggLightbox
 */

elgg_register_event_handler('init', 'system', 'lightbox_init');

/**
 * Initialize the lightbox plugin.
 */
function lightbox_init() {

	elgg_register_library('elgg:lightbox', elgg_get_plugins_path() . 'lightbox/lib/lightbox.php');

	// register group entities for search
	elgg_register_entity_type('object', 'image', 'LightboxPluginImage');
	elgg_register_entity_type('object', 'album', 'LightboxPluginAlbum');

	// Set up the menu
	$item = new ElggMenuItem('lightbox', elgg_echo('lightbox'), 'photos/all');
	elgg_register_menu_item('site', $item);

	// Register a page handler, so we can have nice URLs
	elgg_register_page_handler('photos', 'lightbox_page_handler');

	// Register URL handlers for groups
	elgg_register_entity_url_handler('object', 'image', 'lightbox_url');
	elgg_register_entity_url_handler('object', 'album', 'lightbox_url');
	elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'lightbox_icon_url_override');

	// Register some actions
	$action_base = elgg_get_plugins_path() . 'lightbox/actions/lightbox';
	elgg_register_action("lightbox/edit", "$action_base/edit.php");
	elgg_register_action("lightbox/delete", "$action_base/delete.php");
	
	$action_base .= '/images';
	elgg_register_action("lightbox/albums/edit", "$action_base/edit.php");
	elgg_register_action("lightbox/albums/delete", "$action_base/delete.php");

	// Add some widgets
	elgg_register_widget_type('lightbox', elgg_echo('lightbox:widget'), elgg_echo('groups:widgets:description'));

	// add group photos tool option
	add_group_tool_option('lightbox', elgg_echo('groups:enablelightbox'), true);
	elgg_extend_view('groups/tool_latest', 'groups/profile/lightbox_module');

	// add link to owner block
	elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'lightbox_owner_block_menu');

	// photo and album entity menu
	elgg_register_plugin_hook_handler('register', 'menu:entity', 'lightbox_entity_menu_setup');

	//extend some views
	elgg_extend_view('css/elgg', 'lightbox/css');
	elgg_extend_view('js/elgg', 'lightbox/js');
	
	// Register vendors libraries
	elgg_register_css('jquery.lightbox', 'mod/lightbox/vendors/lightbox/jquery.lightbox-0.5.css');
	elgg_register_js('jquery.lightbox', 'mod/lightbox/vendors/lightbox/jquery.lightbox-0.5.min.js');
	
	$album_js = elgg_get_simplecache_url('js', 'lightbox/album');
	elgg_register_js('lightbox.album', $album_js);

}

/**
 * Dispatches photo and album pages.
 * URLs take the form of
 *  All albums:      photos/all
 *  User's albums:   photos/owner/<username>
 *  Friends' albums: photos/friends/<username>
 *  View album:      photos/album/<guid>/<title>
 *  View photo:      photos/view/<guid>/<title>
 *  New album:       photos/add/<guid>
 *  Edit album:      photos/edit/<guid>
 *  Group albums:    photos/group/<guid>/all
 *  Download:        photos/download/<guid>
 *
 * Title is ignored
 *
 * @param array $page
 * @return bool
 */
function lightbox_page_handler($page) {

	if (!isset($page[0])) {
		$page[0] = 'all';
	}

	$pages_dir = elgg_get_plugins_path() . 'lightbox/pages/lightbox';

	$page_type = $page[0];
	switch ($page_type) {
		case 'owner':
			include "$pages_dir/owner.php";
			break;
		case 'friends':
			include "$pages_dir/friends.php";
			break;
		case 'album':
		case 'view':
			set_input('guid', $page[1]);
			include "$pages_dir/view.php";
			break;
		case 'add':
			include "$pages_dir/new.php";
			break;
		case 'edit':
			set_input('guid', $page[1]);
			include "$pages_dir/edit.php";
			break;
		case 'group':
			include "$pages_dir/owner.php";
			break;
		case 'all':
			include "$pages_dir/world.php";
			break;
		case 'download':
			set_input('guid', $page[1]);
			include "$pages_dir/download.php";
			break;
		default:
			return false;
	}
	return true;
}

/**
 * Populates the ->getUrl() method for photo and album objects
 *
 * @param ElggEntity $entity Photo or album entity
 * @return string Photo or album URL
 */
function lightbox_url($entity) {
	$title = elgg_get_friendly_title($entity->name);

	if($entity->getSubtype() == 'album') {	
		return "photos/album/{$entity->guid}/$title";
	} else {
		return "photos/view/{$entity->guid}/$title";
	}
}

/**
 * Override the default entity icon for photos and albums
 *
 * @return string Relative URL
 */
function lightbox_icon_url_override($hook, $type, $returnvalue, $params) {
	
	$entity = $params['entity'];
	$size = $params['size'];

	if(in_array($entity->getSubtype(), array('image', 'album'))) {
		if (isset($entity->thumbnail) || isset($entity->cover)) {
			// return thumbnail
			return "mod/lightbox/thumbnail.php?guid=$entity->guid&size=$size";
		}

		return "mod/lightbox/graphics/default{$size}.jpg";
	}
}

/**
 * Add a menu item to the user ownerblock
 */
function lightbox_owner_block_menu($hook, $type, $return, $params) {
	if (elgg_instanceof($params['entity'], 'user')) {
		$url = "photos/owner/{$params['entity']->username}";
		$item = new ElggMenuItem('lightbox', elgg_echo('lightbox'), $url);
		$return[] = $item;
	} else {
		if ($params['entity']->lightbox_enable != "no") {
			$url = "photos/group/{$params['entity']->guid}/all";
			$item = new ElggMenuItem('lightbox', elgg_echo('lightbox:group'), $url);
			$return[] = $item;
		}
	}

	return $return;
}

/**
 * Add links/info to entity menu particular to group entities
 */
function lightbox_entity_menu_setup($hook, $type, $return, $params) {
	return $return;
}