/[drupal]/contributions/modules/hidden/hidden.action-pages.inc
ViewVC logotype

Contents of /contributions/modules/hidden/hidden.action-pages.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Thu Dec 18 15:29:06 2008 UTC (11 months, 1 week ago) by ekes
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.3: +27 -40 lines
File MIME type: text/x-php
#276732 Continuing D6 migration
  admin pages forms upgraded to D6
  tests written for admin pages
  filters section reworked and using drupal_write_record
1 <?php
2 // $Id: hidden.action-pages.inc,v 1.3 2008/12/13 18:42:43 ekes Exp $
3
4 /**
5 * @file
6 * Hidden action pages for hiding and unhiding content.
7 */
8
9 /**
10 * Menu callback; form to hide a node or comment.
11 *
12 * @param $form_state
13 * @param $type
14 * string 'node' or 'comment'
15 * @param $id
16 * int nid or cid
17 * @return
18 * form.
19 * @ingroup forms
20 * @see hidden_hide_validate()
21 * @see hidden_hide_submit()
22 */
23 function hidden_hide(&$form_state, $type, $id) {
24 global $user;
25 $type = (string) $type;
26 $id = (int) $id;
27
28 $hidden = (object) _hidden_hide_form_hidden_get($type, $id);
29 if (! $item = _hidden_hide_form_item_get($type, $id)) {
30 drupal_goto();
31 }
32
33 drupal_set_title(t('Hide @title', array('@title' => $item->title)));
34 $form = _hidden_hide_form($user, $hidden);
35 $form['#content_type'] = $item->type;
36 $form['#content_id'] = $item->id;
37
38 $form['submit'] = array(
39 '#type' => 'submit',
40 '#value' => t('Hide'),
41 );
42
43 return $form;
44 }
45
46 /**
47 * Form fields for hide form.
48 *
49 * Separated to allow embedding into the filter form as well to create the reason for
50 * filter hiding.
51 */
52 function _hidden_hide_form($user, $hidden = StdClass) {
53 $form = array();
54
55 if (user_access('administer hidden')) {
56 $form['user'] = array(
57 '#type' => 'textfield',
58 '#title' => t('Hidden by'),
59 '#maxlength' => 60,
60 '#autocomplete_path' => 'user/autocomplete',
61 '#default_value' => isset($hidden->name) ? check_plain($hidden->name) : check_plain($user->name),
62 );
63 }
64 else {
65 $form['user'] = array(
66 '#type' => 'textfield',
67 '#title' => t('Hidden by'),
68 '#value' => check_plain($user->name),
69 '#disabled' => TRUE,
70 );
71 }
72
73 // @todo change these to rid, publicnote and privatenote as they are stored in the db
74 // alos in hidden_report_form()
75 $reasons = _hidden_reasons_get_options();
76 $form['reason'] = array(
77 '#type' => 'radios',
78 '#title' => t('Preset Reason'),
79 '#default_value' => isset($hidden->rid) ? $hidden->rid : 0,
80 '#options' => $reasons,
81 '#description' => t('Preset reason why this item shall be hidden.'),
82 );
83
84 $form['publictext'] = array(
85 '#type' => 'textarea',
86 '#title' => t('Your Public notes'),
87 '#default_value' => isset($hidden->publicnote) ? $hidden->publicnote : '',
88 '#rows' => 5,
89 '#maxlength' => 128,
90 '#description' => t("Optional - type your reason why this item shall be hidden."),
91 );
92
93 $form['privatetext'] = array(
94 '#type' => 'textarea',
95 '#title' => t('Your Private notes'),
96 '#default_value' => isset($hidden->privatenote) ? $hidden->privatenote : '',
97 '#rows' => 5,
98 '#maxlength' => 128,
99 '#description' => t("Optional - notes that can only be seen by other users who can (un)hide content."),
100 );
101
102 return $form;
103 }
104 /**
105 * Menu callback; form to report a node or comment.
106 *
107 * @param $form_state
108 * @param $type
109 * string 'node' or 'comment'
110 * @param $id
111 * int nid or cid
112 *
113 * @return
114 * form.
115 * @ingroup forms
116 * @see hidden_report_submit()
117 */
118 function hidden_report(&$form_state, $type, $id) {
119 if (! user_access('report for hiding')) {
120 drupal_access_denied();
121 }
122 global $user;
123 $type = (string) $type;
124 $id = (int) $id;
125
126 _hidden_hide_form_hidden_get($type, $id);
127 if (! $item = _hidden_hide_form_item_get($type, $id)) {
128 drupal_goto();
129 }
130
131 drupal_set_title(t('Report @title', array('@title' => $item->title)));
132
133 $form = _hidden_hide_form($user);
134 unset($form['privatetext']);
135 $form['#content_type'] = $item->type;
136 $form['#content_id'] = $item->id;
137
138 $form['submit'] = array(
139 '#type' => 'submit',
140 '#value' => t('Report'),
141 );
142
143 return $form;
144 }
145
146 /**
147 * Retrieves hidden if it exists or handles error and goto.
148 *
149 * @param string $type 'node' or 'comment'.
150 * @param int $id nid or cid
151 * @return array hidden or FALSE
152 */
153 function _hidden_hide_form_hidden_get($type, $id) {
154 if (! _hidden_check_param($type, $id)) {
155 // @todo this is now called from hidden_hide and hidden_report - seperate
156 _hidden_log(HIDDEN_LOG_DEBUG, 'hidden_hide() called with invalid arg.');
157 drupal_goto('hidden');
158 }
159 if ($hidden = _hidden_hidden_get($type, $id)) {
160 if ($hidden->delay == 0) {
161 // already fully hidden
162 drupal_set_message(t('Item is already hidden.'), 'error');
163 drupal_goto(hidden_path($type, $id, 'view'));
164 }
165 // else it is hidden but it's on delay, so not yet actually hidden
166 }
167
168 return $hidden;
169 }
170
171 /**
172 * Retrieves node or comment.
173 *
174 * @todo this is generic MOVE
175 *
176 * Moves comment->subject to title to make consistent handling of both nodes and comments.
177 * Calling functions are only using the title, but for abstraction using _load commands -
178 * eg. should also work with node comments then.
179 *
180 * @param string $type 'node' or 'comment'.
181 * @param int $id nid or cid
182 * @return object node or comment.
183 */
184 function _hidden_hide_form_item_get($type, $id) {
185 if ($type == 'comment') {
186 if (! $item = _comment_load($id)) {
187 // @todo error
188 return FALSE;
189 }
190 $item->title = $item->subject;
191 }
192 else {
193 if (! $item = node_load($id)) {
194 // @todo error
195 return FALSE;
196 }
197 $item->cid = 0;
198 }
199
200 $item->type = $type;
201 $item->id = $id;
202
203 return $item;
204 }
205
206 /**
207 * Implementation of form API hook; hide content submit hidden_hide().
208 */
209 function hidden_hide_validate($form, &$form_state) {
210 if (user_access('administer hidden')) {
211 // Validate the "hidden by" field.
212 if (!empty($form_state['values']['user']) && !($user = user_load(array('name' => $form_state['values']['user'])))) {
213 form_set_error('user', t('The username %name does not exist.', array('%name' => $form_state['values']['user'])));
214 }
215 }
216 // @todo check nid/cid/type?
217 // probably not _needed_
218 // and note hidden_admin_filter_form_validate() uses this too
219 }
220
221 /**
222 * Implementation of form API hook; hide or report content submit hidden_hide().
223 */
224 function hidden_hide_submit($form, &$form_state) {
225 if (! user_access('mark as hidden')) {
226 drupal_access_denied();
227 }
228
229 $type = (string) $form['#content_type'];
230 $id = (int) $form['#content_id'];
231 if (! _hidden_check_param($type, $id)) {
232 _hidden_log(HIDDEN_LOG_DEBUG, 'hidden_hide_submit() passed invalid parameters.');
233 drupal_goto('hidden');
234 }
235
236 if (user_access('administer hidden')) {
237 $user = user_load(array('name' => $form_state['values']['user']));
238 $uid = $user->uid;
239 }
240 else {
241 global $user;
242 $uid = $user->uid;
243 }
244
245 $rid = (int) $form_state['values']['reason'];
246 $rid = hidden_reason_check($rid) ? $rid : 0;
247 $public = (string) $form_state['values']['publictext'];
248 $private = (string) $form_state['values']['privatetext'];
249
250 $t_args = array('%type' => $type);
251 if (hidden_hidden_hide($type, $id, $uid, $rid, $public, $private)) {
252 $msg = t('Hidden %type.', $t_args);
253 _hidden_log(HIDDEN_LOG_HIDE, $msg, $type, $id, $rid, $public);
254 drupal_set_message($msg);
255 }
256 else {
257 $msg = t('Error hiding %type.', $t_args);
258 _hidden_log(HIDDEN_LOG_ERROR, $msg, $type, $id, $rid, $public);
259 drupal_set_message($msg, 'error');
260 }
261 /**
262 * The redirect challenge.
263 *
264 * What do users want/expect to see next?
265 * What are they allowed to see?
266 */
267 if (! user_access('administer content')) {
268 if (isset($_REQUEST['destination'])) {
269 unset($_REQUEST['destination']);
270 }
271 }
272 $form_state['redirect'] = 'hidden/'. $type .'/'. $id;
273 }
274
275 /**
276 * Implementation of form API hook; hide or report content submit hidden_hide().
277 */
278 function hidden_report_submit($form, &$form_state) {
279 if (! user_access('report for hiding')) {
280 drupal_access_denied();
281 }
282
283 $type = (string) $form['#content_type'];
284 $id = (int) $form['#content_id'];
285 if (! _hidden_check_param($type, $id)) {
286 _hidden_log(HIDDEN_LOG_DEBUG, 'hidden_report_submit() passed invalid parameters.');
287 drupal_goto('hidden');
288 }
289
290 global $user;
291 $uid = $user->uid;
292
293 $rid = (int) $form_state['values']['reason'];
294 $rid = hidden_reason_check($rid) ? $rid : 0;
295 $public = (string) $form_state['values']['publictext'];
296
297 $t_args = array('%type' => $type);
298 if (_hidden_reported_hide($type, $id, $uid, $rid, $public)) {
299 $msg = t('Reported %type.', $t_args);
300 _hidden_log(HIDDEN_LOG_REPORTED, $msg, $type, $id, $rid, $public);
301 drupal_set_message($msg);
302 }
303 else {
304 $msg = t('Error reporting %type.', $t_args);
305 _hidden_log(HIDDEN_LOG_ERROR, $msg, $type, $id, $rid, $public);
306 drupal_set_message($msg, 'error');
307 }
308 }
309
310 /**
311 * Menu callback; unhide a node or comment.
312 *
313 * @param $hidden
314 * hidden details object see hidden_hidden_check()
315 */
316 function hidden_unhide($type, $id) {
317 $t_array = array('%type' => $type);
318 if (_hidden_hidden_unhide($type, $id)) {
319 $msg = t('Hidden %type unhidden', $t_array);
320 _hidden_log(HIDDEN_LOG_UNHIDE, $msg, $type, $id);
321 drupal_set_message($msg);
322 if ($type == 'node') {
323 drupal_goto('node/'. $id);
324 }
325 else {
326 $nid = _hidden_comment_nid($id);
327 drupal_goto('node/'. $nid, NULL, 'comment-'. $id);
328 }
329 }
330 else {
331 $msg = t('Error unhiding %type.', $t_array);
332 _hidden_log(HIDDEN_LOG_ERROR, $msg, $type, $id);
333 drupal_set_message($msg, 'error');
334 drupal_goto();
335 }
336
337 }

  ViewVC Help
Powered by ViewVC 1.1.2