These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Save blog entity |
||
4 | * |
||
5 | * Can be called by clicking save button or preview button. If preview button, |
||
6 | * we automatically save as draft. The preview button is only available for |
||
7 | * non-published drafts. |
||
8 | * |
||
9 | * Drafts are saved with the access set to private. |
||
10 | * |
||
11 | * @package Blog |
||
12 | */ |
||
13 | |||
14 | // start a new sticky form session in case of failure |
||
15 | elgg_make_sticky_form('blog'); |
||
16 | |||
17 | // save or preview |
||
18 | $save = (bool) get_input('save'); |
||
19 | |||
20 | // store errors to pass along |
||
21 | $error = false; |
||
22 | $error_forward_url = REFERER; |
||
23 | $user = elgg_get_logged_in_user_entity(); |
||
24 | |||
25 | // edit or create a new entity |
||
26 | $guid = get_input('guid'); |
||
27 | |||
28 | if ($guid) { |
||
29 | $entity = get_entity($guid); |
||
30 | View Code Duplication | if (elgg_instanceof($entity, 'object', 'blog') && $entity->canEdit()) { |
|
31 | $blog = $entity; |
||
32 | } else { |
||
33 | register_error(elgg_echo('blog:error:post_not_found')); |
||
34 | forward(get_input('forward', REFERER)); |
||
35 | } |
||
36 | |||
37 | // save some data for revisions once we save the new edit |
||
38 | $revision_text = $blog->description; |
||
39 | $new_post = $blog->new_post; |
||
40 | } else { |
||
41 | $blog = new ElggBlog(); |
||
42 | $blog->subtype = 'blog'; |
||
43 | $new_post = true; |
||
44 | } |
||
45 | |||
46 | // set the previous status for the hooks to update the time_created and river entries |
||
47 | $old_status = $blog->status; |
||
48 | |||
49 | // set defaults and required values. |
||
50 | $values = [ |
||
51 | 'title' => '', |
||
52 | 'description' => '', |
||
53 | 'status' => 'draft', |
||
54 | 'access_id' => ACCESS_DEFAULT, |
||
55 | 'comments_on' => 'On', |
||
56 | 'excerpt' => '', |
||
57 | 'tags' => '', |
||
58 | 'container_guid' => (int) get_input('container_guid'), |
||
59 | ]; |
||
60 | |||
61 | // fail if a required entity isn't set |
||
62 | $required = ['title', 'description']; |
||
63 | |||
64 | // load from POST and do sanity and access checking |
||
65 | foreach ($values as $name => $default) { |
||
66 | View Code Duplication | if ($name === 'title') { |
|
1 ignored issue
–
show
|
|||
67 | $value = htmlspecialchars(get_input('title', $default, false), ENT_QUOTES, 'UTF-8'); |
||
68 | } else { |
||
69 | $value = get_input($name, $default); |
||
70 | } |
||
71 | |||
72 | if (in_array($name, $required) && empty($value)) { |
||
73 | $error = elgg_echo("blog:error:missing:$name"); |
||
74 | } |
||
75 | |||
76 | if ($error) { |
||
77 | break; |
||
78 | } |
||
79 | |||
80 | switch ($name) { |
||
81 | case 'tags': |
||
82 | $values[$name] = string_to_tag_array($value); |
||
83 | break; |
||
84 | |||
85 | case 'excerpt': |
||
86 | if ($value) { |
||
87 | $values[$name] = elgg_get_excerpt($value); |
||
88 | } |
||
89 | break; |
||
90 | |||
91 | case 'container_guid': |
||
92 | // this can't be empty or saving the base entity fails |
||
93 | if (!empty($value)) { |
||
94 | $container = get_entity($value); |
||
95 | if ($container && $container->canWriteToContainer(0, 'object', 'blog')) { |
||
96 | $values[$name] = $value; |
||
97 | } else { |
||
98 | $error = elgg_echo("blog:error:cannot_write_to_container"); |
||
99 | } |
||
100 | } else { |
||
101 | unset($values[$name]); |
||
102 | } |
||
103 | break; |
||
104 | |||
105 | default: |
||
106 | $values[$name] = $value; |
||
107 | break; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | // if preview, force status to be draft |
||
112 | if ($save == false) { |
||
113 | $values['status'] = 'draft'; |
||
114 | } |
||
115 | |||
116 | // if draft, set access to private and cache the future access |
||
117 | if ($values['status'] == 'draft') { |
||
118 | $values['future_access'] = $values['access_id']; |
||
119 | $values['access_id'] = ACCESS_PRIVATE; |
||
120 | } |
||
121 | |||
122 | // assign values to the entity, stopping on error. |
||
123 | if (!$error) { |
||
124 | foreach ($values as $name => $value) { |
||
125 | $blog->$name = $value; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // only try to save base entity if no errors |
||
130 | if (!$error) { |
||
131 | if ($blog->save()) { |
||
132 | // remove sticky form entries |
||
133 | elgg_clear_sticky_form('blog'); |
||
134 | |||
135 | // remove autosave draft if exists |
||
136 | $blog->deleteAnnotations('blog_auto_save'); |
||
137 | |||
138 | // no longer a brand new post. |
||
139 | $blog->deleteMetadata('new_post'); |
||
140 | |||
141 | // if this was an edit, create a revision annotation |
||
142 | if (!$new_post && $revision_text) { |
||
143 | $blog->annotate('blog_revision', $revision_text); |
||
144 | } |
||
145 | |||
146 | system_message(elgg_echo('blog:message:saved')); |
||
147 | |||
148 | $status = $blog->status; |
||
149 | |||
150 | // add to river if changing status or published, regardless of new post |
||
151 | // because we remove it for drafts. |
||
152 | if (($new_post || $old_status == 'draft') && $status == 'published') { |
||
153 | elgg_create_river_item([ |
||
154 | 'view' => 'river/object/blog/create', |
||
155 | 'action_type' => 'create', |
||
156 | 'subject_guid' => $blog->owner_guid, |
||
157 | 'object_guid' => $blog->getGUID(), |
||
158 | ]); |
||
159 | |||
160 | elgg_trigger_event('publish', 'object', $blog); |
||
161 | |||
162 | // reset the creation time for posts that move from draft to published |
||
163 | if ($guid) { |
||
164 | $blog->time_created = time(); |
||
165 | $blog->save(); |
||
166 | } |
||
167 | } elseif ($old_status == 'published' && $status == 'draft') { |
||
168 | elgg_delete_river([ |
||
169 | 'object_guid' => $blog->guid, |
||
170 | 'action_type' => 'create', |
||
171 | 'limit' => false, |
||
172 | ]); |
||
173 | } |
||
174 | |||
175 | if ($blog->status == 'published' || $save == false) { |
||
176 | forward($blog->getURL()); |
||
177 | } else { |
||
178 | forward("blog/edit/$blog->guid"); |
||
179 | } |
||
180 | } else { |
||
181 | register_error(elgg_echo('blog:error:cannot_save')); |
||
182 | forward($error_forward_url); |
||
183 | } |
||
184 | } else { |
||
185 | register_error($error); |
||
186 | forward($error_forward_url); |
||
187 | } |
||
188 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.