*/ /** * Menu callback; */ function moderation_callback_switch($obj_type, $obj_id, $op, $attribute) { $js = isset($_REQUEST['js']); $token = $_REQUEST['token']; // Check for valid token // We don't need a token if $op is get if (!drupal_valid_token($token, $obj_type .'-'. $obj_id) && $op != 'get') { drupal_access_denied(); exit(); } $attributes = array('status', 'promote', 'sticky', 'moderate', 'preview'); $types = array('node', 'comment', 'thread', 'flag'); if (!is_numeric($obj_id) OR !in_array($obj_type, $types) OR !in_array($attribute, $attributes)) { return drupal_not_found(); } if ($op == 'get') { if ($attribute == 'preview') { if ($js) { // Return content for preview if js is available moderation_get_preview($obj_id, $obj_type); } else { // Redirect to node/comment if js is not available moderation_goto($obj_id, $obj_type); } } else { // Return the current value for an attribute moderation_get_attribute($obj_id, $obj_type, $attribute); } exit(); } if ($op == 'set') { switch ($attribute) { case 'status': case 'promote': case 'sticky': $result = moderation_switch_attribute($obj_id, $obj_type, $attribute); break; case 'moderate': $result = moderation_switch_moderation($obj_id, $obj_type); break; } if ($js) { $GLOBALS['devel_shutdown'] = FALSE; print moderation_json_encode($result); exit(); } else { drupal_set_message(t('The changes have been saved.')); drupal_goto(); } } return drupal_not_found(); } /** * Redirect to a node/comment */ function moderation_goto($obj_id, $obj_type) { if ($obj_type == 'node') { drupal_goto('node/'. $obj_id); } else { if ($comment = _comment_load($obj_id)) { drupal_goto('node/'. $comment->nid, NULL, 'comment-'. $comment->cid); } } } /** * Switch moderation flag * * @param string $obj_type one of 'node', 'comment' * @param integer $obj_id */ function moderation_switch_moderation($obj_id, $obj_type) { global $user; $status = moderation_get_status($obj_type, $obj_id); moderation_insert_moderation_log($obj_type, $obj_id, !$status, 'moderate', $user); $result = moderation_update_moderation($obj_type, $obj_id, !$status); return array($result, !$status, 'moderate', $obj_id, $obj_type); } /** * Switch an attribute * * @param integer $obj_id * @param string $obj_type one of 'node', 'comment' * @param string $attribute one of 'status', 'promote', 'sticky' */ function moderation_switch_attribute($obj_id, $obj_type, $attribute) { global $user; if ($obj_type == 'node') { $status_new = !db_result(db_query("SELECT %s FROM {node} WHERE nid=%d", $attribute, $obj_id)); $success = db_query("UPDATE {node} SET %s=%d WHERE nid=%d", $attribute, $status_new, $obj_id); } elseif ($obj_type == 'comment') { $status_new = !db_result(db_query("SELECT %s FROM {comments} WHERE cid=%d", $attribute, $obj_id)); $success = db_query("UPDATE {comments} SET %s=%d WHERE cid=%d", $attribute, $status_new, $obj_id); } $result = moderation_insert_moderation_log($obj_type, $obj_id, !$status, $attribute, $user); return array($result, $status_new, $attribute, $obj_type); } /** * Get the preview markup for a node or a comment * * @param integer $obj_id * @param string $obj_type */ function moderation_get_preview($obj_id, $obj_type) { $data = ''; switch ($obj_type) { case 'node': if ($node = node_load($obj_id)) { $data = theme('moderation_node_preview', $node); } break; case 'comment': if ($comment = _comment_load($obj_id)) { $_GET['q'] = 'node/'. $comment->nid; $node = node_load($comment->nid); $data = '