aboutsummaryrefslogtreecommitdiff
path: root/mod/sitepages/sitepages_functions.php
blob: 000a5eb1a6766ae39405d91814bc2851d92dd061 (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
<?php
/**
 * Helper functions for Site Pages.
 *
 * @package SitePages
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
 * @author Curverider Ltd
 * @copyright Curverider Ltd 2008-2010
 * @link http://elgg.org/
 */


/**
 * Returns a single object that holds information about
 * customizations for the $section site page.  The object guid
 * is stored as private data on the site entity.  This allows the pages
 * to still be searchable as standard entities.
 *
 * @param $type
 * @return mixed ElggSitePage on success, FALSE on fail
 */
function sitepages_get_sitepage_object($page_type) {
	global $CONFIG;

	$page_guid = get_private_setting($CONFIG->site->getGUID(), "sitepages:$page_type");
	$sitepage = get_entity($page_guid);

	if ($sitepage instanceof ElggSitePage || $sitepage->page_type == $page_type) {
		return $sitepage;
	}

	return FALSE;
}

/**
 * Creates a site page object.
 *
 * @param str $page_type
 * @return mixed ElggSitePage on success, FALSE on fail.
 */
function sitepages_create_sitepage_object($page_type) {
	global $CONFIG;

	$sitepage = new ElggSitePage();
	$sitepage->page_type = $page_type;
	$sitepage->access_id = ACCESS_PUBLIC;
	$sitepage->save();

	if ($sitepage->save() && set_private_setting($CONFIG->site->getGUID(), "sitepages:$page_type", $sitepage->getGUID())) {
		return $sitepage;
	}

	return FALSE;
}

/**
 * Assembles html for edit sections of site pages.
 *
 * @param str $section
 * @return str html
 */
function sitepages_get_edit_section_content($page_type) {
	set_context('admin');

	$keywords = '';

	$title = elgg_view_title(elgg_echo('sitepages'));
	$menu = elgg_view('sitepages/menu', array('page_type' => $page_type));

	switch ($page_type) {
		case 'front':
			$view = 'sitepages/forms/editfront';
			$keywords = elgg_view('sitepages/keywords');
			break;

		case 'seo':
			$view = 'sitepages/forms/editmeta';
			break;

		default:
			$view = 'sitepages/forms/edit';
			break;

	}

	$form .= elgg_view($view, array('page_type' => $page_type));
	$body = $title .  $menu . $form;

	$content = elgg_view_layout('one_column_with_sidebar', $body, $keywords);
	return $content;
}

/**
 * Assembles html for displaying site pages
 *
 * @param string $page_type
 * @return string Formatted html
 */
function sitepages_get_page_content($page_type) {
	$body = elgg_view_title(elgg_echo("sitepages:". strtolower($page_type)));

	$sitepage = sitepages_get_sitepage_object($page_type);

	if ($sitepage) {
		$body .= elgg_view('page_elements/elgg_content', array('body' => $sitepage->description));
	} else {
		$body .= elgg_view('page_elements/elgg_content', array('body' => elgg_echo('sitepages:notset')));
	}

	$content = elgg_view_layout('one_column_with_sidebar', $body);
	return $content;
}


/**
 * Used to determine how to handle special non-static keywords.
 *
 * @param unknown_type $matches
 * @return html
 */
function sitepages_parse_view_match($matches) {
	$keyword = $matches[0];
	$type = trim($matches[1]);
	$params_string = trim($matches[2]);

	switch ($type) {
		case 'entity':
			$options = sitepages_keywords_parse_entity_params($params_string);
			// must use this lower-level function because I missed refactoring
			// the list entity functions for relationships.
			// (which, since you're here, is the only function that runs through all
			// possible options for elgg_get_entities*() functions...)
			$entities = elgg_get_entities_from_relationship($options);
			$content = elgg_view_entity_list($entities, count($entities), $options['offset'],
				$options['limit'], $options['full_view'], $options['view_type_toggle'], $options['pagination']);
			break;

		case 'view':
			// parses this into an acceptable array for $vars.
			$info = sitepages_keywords_parse_view_params($params_string);
			$content = elgg_view($info['view'], $info['vars']);

			break;

	}

	return $content;
}

/**
 * Creates an array from a "name=value, name1=value2" string.
 *
 * @param $string
 * @return array
 */
function sitepages_keywords_tokenize_params($string) {
	$pairs = array_map('trim', explode(',', $string));

	$params = array();

	foreach ($pairs as $pair) {
		list($name, $value) = explode('=', $pair);

		$name = trim($name);
		$value = trim($value);
		$params[$name] = $value;
	}

	return $params;
}

/**
 *
 * @param $string
 * @return unknown_type
 */
function sitepages_keywords_parse_view_params($string) {
	$vars = sitepages_keywords_tokenize_params($string);

	// the first element key is the view
	$var_keys = array_keys($vars);
	$view = $var_keys[0];

	$info = array(
		'view' => $view,
		'vars' => $vars
	);

	return $info;

}

/**
 * Returns an options array suitable for using in elgg_get_entities()
 *
 * @param string $string "name=value, name2=value2"
 * @return array
 */
function sitepages_keywords_parse_entity_params($string) {
	$params = sitepages_keywords_tokenize_params($string);

	// handle some special cases
	if (isset($params['owner'])) {
		if ($user = get_user_by_username($params['owner'])) {
			$options['owner_guid'] = $user->getGUID();
		}
	}

	// @todo probably need to add more for
	// group -> container_guid, etc

	return $params;
}



/**
 * Utility object to store site page information.
 */
class ElggSitePage extends ElggObject {
	public function initialise_attributes() {
		parent::initialise_attributes();

		$this->attributes['subtype'] = 'sitepages_page';
	}
}