aboutsummaryrefslogtreecommitdiff
path: root/mod/blog/blog_lib.php
blob: 0fe1b80d4a684fc85a90c3336e2be29643b72792 (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
<?php
/**
 * Blog helper functions
 *
 * @package Blog
 * @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 HTML for a blog post.
 *
 * @param int $guid of a blog entity.
 * @return string html
 */
function blog_get_page_content_read($owner_guid, $guid) {
	if ($guid) {
		$blog = get_entity($guid);

		if (!elgg_instanceof($blog, 'object', 'blog') && $blog->status == 'final') {
			$content = elgg_echo('blog:error:post_not_found');
		} else {
			elgg_push_breadcrumb($blog->title, $blog->getURL());
			$content = elgg_view_entity($blog, TRUE);
		}
	} else {
		$content = elgg_list_entities_from_metadata(array(
			'type' => 'object',
			'subtype' => 'blog',
			'owner_guid' => $owner_guid,
			'full_view' => FALSE,
			'metadata_name_value_pair' => array('name' => 'status', 'value' => 'final')
		));
	}

	return $content;
}

/**
 * Returns HTML to edit a blog post.
 *
 * @param int $guid
 * @return string html
 */
function blog_get_page_content_edit($guid) {
	$vars = array();
	if ($guid) {
		$blog = get_entity($guid);
		$vars['entity'] = $blog;

		if (!elgg_instanceof($blog, 'object', 'blog') || !$blog->canEdit()) {
			$content = elgg_echo('blog:error:post_not_found');
		}

		elgg_push_breadcrumb($blog->title, $blog->getURL());
		elgg_push_breadcrumb(elgg_echo('edit'));
	} else {
		elgg_push_breadcrumb(elgg_echo('blog:new'));
	}

	$content = elgg_view('blog/forms/edit', $vars);

	return $content;
}

/**
 * Saves a blog
 *
 * @param array $info An array of name=>value pairs to save to the blog entity
 *
 * @return array('success' => BOOL, 'message' => string);
 */
function blog_save_blog($info) {
	// store errors to pass along
	$error = FALSE;

	if ($info['guid']) {
		$entity = get_entity($info['guid']);
		if (elgg_instanceof($entity, 'object', 'blog') && $entity->canEdit()) {
			$blog = $entity;
		} else {
			$error = elgg_echo('blog:error:post_not_found');
		}
	} else {
		$blog = new ElggObject();
		$blog->subtype = 'blog';
	}

	// check required vars
	$required = array('title', 'description');

	// load from POST and do sanity and access checking
	foreach ($info as $name => $value) {
		if (in_array($name, $required) && empty($value)) {
			$error = elgg_echo("blog:error:missing:$name");
		}

		if ($error) {
			break;
		}

		switch ($name) {
			case 'tags':
				if ($value) {
					$info[$name] = string_to_tag_array($value);
				} else {
					unset ($info[$name]);
				}
				break;

			case 'excerpt':
				// restrict to 300 chars
				if ($value) {
					$value = substr(strip_tags($value), 0, 300);
				} else {
					$value = substr(strip_tags($info['description']), 0, 300);
				}
				$info[$name] = $value;
				break;

			case 'container_guid':
				// this can't be empty.
				if (!empty($value)) {
					if (can_write_to_container($user->getGUID(), $value)) {
						$info[$name] = $value;
					} else {
						$error = elgg_echo("blog:error:cannot_write_to_container");
					}
				} else {
					unset($info[$name]);
				}
				break;

			// don't try to set the guid
			case 'guid':
				unset($info['guid']);
				break;

			default:
				$info[$name] = $value;
				break;
		}
	}

	// assign values to the entity, stopping on error.
	if (!$error) {
		foreach ($info as $name => $value) {
			if (!$blog->$name = $value) {
				$error = elgg_echo('blog:error:cannot_save');
				break;
			}
		}
	}

	// only try to save base entity if no errors
	if (!$error && !$blog->save()) {
		$error = elgg_echo('blog:error:cannot_save');
	}

	if ($error) {
		$return = array(
			'success' => FALSE,
			'message' => $error
		);
	} else {
		$return = array(
			'success' => TRUE,
			'message' => elgg_echo('blog:message:saved')
		);
	}

	return $return;
}


class ElggBlog extends ElggObject {

}