Commit 61ea9eb1 authored by Croitor Alexandru's avatar Croitor Alexandru
Browse files

Added permissions system. Corrected Unit tests.

parent ec85f871
<?php
// $Id$
/**
* @file
* Node convert book.module include
*
* Performs necessary changes regarding book type conversions.
*/
/**
* Implementation of node_convert_change().
*/
function book_node_convert_change($data, $op) {
if ($op == 'insert') {
if ($data['dest_node_type'] == 'book') {
$book = array();
$node = $data['node'];
$book['link_path'] = 'node/'. $node->nid;
$book['link_title'] = $node->title;
$book['plid'] = 0;
$book['menu_name'] = book_menu_name($node->nid);
$mlid = menu_link_save($book);
$book['bid'] = $data['hook_options']['bid'];
if ($book['bid'] == 'self') $book['bid'] = $node->nid;
db_query("INSERT INTO {book} (nid, mlid, bid) VALUES (%d, %d, %d)", $node->nid, $book['mlid'], $book['bid']);
}
}
elseif ($op == 'delete') {
if ($data['node']->type == 'book') {
menu_link_delete($data['node']->book['mlid']);
db_query('DELETE FROM {book} WHERE mlid = %d', $data['node']->book['mlid']);
}
}
elseif ($op == 'options') {
$form = array();
if ($data['dest_node_type'] == 'book') {
foreach (book_get_books() as $book) {
$options[$book['nid']] = $book['title'];
}
$options = array('self' => '<'. t('create a new book') .'>') + $options;
$form['bid'] = array(
'#type' => 'select',
'#title' => t('Book'),
'#options' => $options,
'#description' => t('Your page will be a part of the selected book.'),
'#attributes' => array('class' => 'book-title-select'),
);
}
return $form;
}
}
\ No newline at end of file
<?php
// $Id$
/**
* @file
* Node convert forum.module include
*
* Performs necessary changes regarding forum type conversions.
*/
/**
* Implementation of node_convert_change().
*/
function forum_node_convert_change($data, $op) {
if ($op == 'insert') {
if ($data['dest_node_type'] == 'forum') {
db_query('INSERT INTO {forum} (tid, vid, nid) VALUES (%d, %d, %d)', $data['hook_options']['forum'], $data['node']->vid, $data['node']->nid);
db_query('INSERT INTO {term_node} (tid, vid, nid) VALUES (%d, %d, %d)', $data['hook_options']['forum'], $data['node']->vid, $data['node']->nid);
}
}
elseif ($op == 'delete') {
if ($data['node']->type == 'forum') {
db_query('DELETE FROM {forum} WHERE nid = %d', $data['node']->nid);
db_query('DELETE FROM {term_node} WHERE nid = %d', $data['node']->nid);
}
}
elseif ($op == 'options') {
$form = array();
if ($data['dest_node_type'] == 'forum') {
$vid = variable_get('forum_nav_vocabulary', '');
$form['forum'] = taxonomy_form($vid);
$form['forum']['#weight'] = 7;
$form['forum']['#required'] = TRUE;
$form['forum']['#options'][''] = t('- Please choose -');
}
return $form;
}
elseif ($op == 'options validate') {
$form_state = $data['form_state'];
if ($data['dest_node_type'] == 'forum') {
$containers = variable_get('forum_containers', array());
$term = $form_state['values']['hook_options']['forum'];
if (in_array($term, $containers)) {
$term = taxonomy_get_term($term);
form_set_error('hook_options][forum', t('The item %forum is only a container for forums. Please select one of the forums below it.', array('%forum' => $term->name)));
}
}
}
}
\ No newline at end of file
......@@ -27,6 +27,45 @@ function node_convert_help($path, $arg) {
return $output;
}
/**
* Implementation of hook_perm().
*/
function node_convert_init() {
require_once './'. drupal_get_path('module', 'node_convert') ."/includes/book.node_convert.inc";
require_once './'. drupal_get_path('module', 'node_convert') ."/includes/forum.node_convert.inc";
}
/**
* Implementation of hook_perm().
*/
function node_convert_perm() {
$types = node_get_types();
$permissions = array('administer conversion');
foreach ($types as $type => $parameters) {
$permissions[] = 'convert from '. $type;
$permissions[] = 'convert to '. $type;
}
return $permissions;
}
function node_convert_check_access($node) {
$access = user_access('convert from '. $node->type) && user_access('administer conversion');
return $access;
}
function node_convert_return_access_node_types($direction) {
global $user;
$list = array();
$types = node_get_types();
foreach ($types as $type => $parameters) {
if (user_access('convert '. $direction .' '. $type)) $list[$type] = $type;
}
if (!empty($list))
return $list;
else
return FALSE;
}
/**
* Implementation of hook_menu().
*/
......@@ -37,7 +76,7 @@ function node_convert_menu() {
'description' => 'Convert selected nodes from one node type, to another.',
'page callback' => 'drupal_get_form',
'page arguments' => array('node_convert_bulk'),
'access arguments' => array('administer content types'),
'access arguments' => array('administer conversion'),
'type' => MENU_NORMAL_ITEM,
);
......@@ -45,7 +84,8 @@ function node_convert_menu() {
'title' => 'Convert',
'page callback' => 'drupal_get_form',
'page arguments' => array('node_convert_conversion_form', 1),
'access arguments' => array('administer content types'),
'access callback' => 'node_convert_check_access',
'access arguments' => array(1),
'weight' => 6,
'type' => MENU_LOCAL_TASK,
);
......@@ -61,7 +101,8 @@ function node_convert_conversion_form($form_state, $node) {
}
elseif ($form_state['values']['step'] == 'choose_destination_type') {
$op = 'choose_destination_fields';
} elseif (isset($form_state['values']['step'])) {
}
elseif (isset($form_state['values']['step'])) {
$op = $form_state['values']['step'];
}
$form['step'] = array(
......@@ -75,12 +116,17 @@ function node_convert_conversion_form($form_state, $node) {
/* Form steps */
if ($op == 'choose_destination_type') {
$form['current_type'] = array('#type' => 'markup', '#value' => $node->type); // Remember current node type
$types = array_keys(content_types()); // Get available content types
$key = array_search($form['current_type']['#value'], $types);
if ($key !== FALSE) unset($types[$key]); // Delete the current content type from the list
foreach ($types as $value) $options[$value] = $value;
// Populate the select with possible content types
$form['destination_type'] = array('#type' => 'select', '#options' => $options, '#title' => t("To what content type should this node be converted"));
$types = node_convert_return_access_node_types('to'); // Get available content types
if ($types != FALSE) {
$key = array_search($form['current_type']['#value'], $types);
if ($key !== FALSE) unset($types[$key]); // Delete the current content type from the list
$options = $types;
// Populate the select with possible content types
$form['destination_type'] = array('#type' => 'select', '#options' => $options, '#title' => t("To what content type should this node be converted"));
}
else {
$form['destination_type'] = array('#type' => 'markup', '#value' => t("You don't have access to any node types."));
}
}
elseif ($op == "choose_destination_fields") {
$source_fields = content_types($node->type);
......@@ -137,12 +183,21 @@ function node_convert_conversion_form($form_state, $node) {
}
}
if ($op != 'choose_destination_fields') {
if ($op != 'choose_destination_fields' && isset($types) && $types != FALSE) {
$form['submit'] = array('#type' => 'submit', '#value' => t("Next"));
} else {
}
elseif ($op == "choose_destination_fields") {
$form['submit'] = array('#type' => 'submit', '#value' => t("Convert"));
}
/* if (isset($types) && $types != FALSE) {
if ($op != 'choose_destination_fields') {
$form['submit'] = array('#type' => 'submit', '#value' => t("Next"));
} else {
$form['submit'] = array('#type' => 'submit', '#value' => t("Convert"));
}
} */
return $form;
}
......@@ -175,7 +230,8 @@ function node_convert_conversion_form_submit($form, &$form_state) {
}
if (!empty($form['hook_options'])) {
$hook_options = $form_state['values']['hook_options'];
} else {
}
else {
$hook_options = NULL;
}
$result = node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_fields, $no_fields_flag, $hook_options);
......@@ -215,18 +271,24 @@ function node_convert_bulk($form_state) {
);
if ($op == 'choose_source_dest_type') {
$types = array_keys(content_types()); // Get available content types
foreach ($types as $value) $options[$value] = $value;
$form['source_type'] = array(
'#type' => 'select',
'#title' => t("Source type"),
'#options' => $options,
);
$form['dest_type'] = array(
'#type' => 'select',
'#title' => t("Destination type"),
'#options' => $options,
);
// Get available content types
$to_types = node_convert_return_access_node_types('to');
$from_types = node_convert_return_access_node_types('from');
if ($to_types != FALSE && $from_types != FALSE) {
$form['source_type'] = array(
'#type' => 'select',
'#title' => t("Source type"),
'#options' => $from_types,
);
$form['dest_type'] = array(
'#type' => 'select',
'#title' => t("Destination type"),
'#options' => $to_types,
);
}
else {
$form['no_types'] = array('#type' => 'markup', '#value' => t("You don't have access to any node types."));
}
}
elseif ($op == 'choose_nodes') {
$info = array('source_type' => $form_state['values']['source_type'], 'dest_type' => $form_state['values']['dest_type']);
......@@ -276,12 +338,17 @@ function node_convert_bulk($form_state) {
}
$form['info'] = array('#type' => 'value', '#value' => $info);
}
if ($op != 'choose_fields') {
//if ($to_types != FALSE && $from_types != FALSE) {
if ($op == 'choose_source_dest_type' && $to_types != FALSE && $from_types != FALSE) {
$form['submit'] = array('#type' => 'submit', '#value' => t("Next"));
} else {
}
elseif ($op == 'choose_nodes') {
$form['submit'] = array('#type' => 'submit', '#value' => t("Next"));
}
elseif ($op == "choose_fields") {
$form['submit'] = array('#type' => 'submit', '#value' => t("Convert"));
}
//}
return $form;
}
......@@ -303,7 +370,8 @@ function node_convert_bulk_validate($form, &$form_state) {
if ($no_nodes == TRUE) {
form_set_error('', t('Please select at least one node.'));
}
} elseif ($form_state['values']['step'] == 'choose_fields') {
}
elseif ($form_state['values']['step'] == 'choose_fields') {
module_invoke_all('node_convert_change', array('dest_node_type' => $form_state['values']['info']['dest_type'], 'form_state' => $form_state), 'options validate');
}
}
......@@ -321,7 +389,8 @@ function node_convert_bulk_submit($form, &$form_state) {
}
if (!empty($form['hook_options'])) {
$hook_options = $form_state['values']['hook_options'];
} else {
}
else {
$hook_options = NULL;
}
foreach ($info['nodes'] as $nid) {
......@@ -417,7 +486,7 @@ function node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_
// We make sure that all custom node modules do their changes at the appropriate steps
module_invoke_all('node_convert_change', $data, 'insert');
module_invoke_all('node_convert_change', $data, 'delete');
if (count($tables_info[$source_node_type]['tables']) != 0)
db_query("DELETE FROM {%s} WHERE nid = %d", "content_type_". $source_node_type, $nid); // We delete the source node_type info
db_query("DELETE FROM {cache_content} WHERE cid = '%s'", "content:". $nid .":". $vid); // We clear the cache
......@@ -525,7 +594,8 @@ function node_convert_messages($result, $params = array()) {
$params += array('display_success' => TRUE);
if ($result == FALSE) {
drupal_set_message(t("Conversion failed. Node nid @nid doesn't exist.", array('@nid' => $params['nid'])), 'error');
} elseif ($params['display_success'] == TRUE) {
}
elseif ($params['display_success'] == TRUE) {
drupal_set_message(t("The current node has been converted succesufuly."));
}
}
......@@ -549,7 +619,8 @@ function node_convert_messages($result, $params = array()) {
* @return
* Should return a FAPI array only when using the options operation.
*/
function node_convert_node_convert_change($data, $op) {
function hook_node_convert_change($data, $op) {
// All of this is just an example, there real data is being called from hook_init
if ($op == 'insert') {
if ($data['dest_node_type'] == 'book') {
$book = array();
......@@ -567,7 +638,8 @@ function node_convert_node_convert_change($data, $op) {
db_query('INSERT INTO {forum} (tid, vid, nid) VALUES (%d, %d, %d)', $data['hook_options']['forum'], $data['node']->vid, $data['node']->nid);
db_query('INSERT INTO {term_node} (tid, vid, nid) VALUES (%d, %d, %d)', $data['hook_options']['forum'], $data['node']->vid, $data['node']->nid);
}
} elseif ($op == 'delete') {
}
elseif ($op == 'delete') {
if ($data['node']->type == 'book') {
menu_link_delete($data['node']->book['mlid']);
db_query('DELETE FROM {book} WHERE mlid = %d', $data['node']->book['mlid']);
......@@ -576,7 +648,8 @@ function node_convert_node_convert_change($data, $op) {
db_query('DELETE FROM {forum} WHERE nid = %d', $data['node']->nid);
db_query('DELETE FROM {term_node} WHERE nid = %d', $data['node']->nid);
}
} elseif ($op == 'options') {
}
elseif ($op == 'options') {
$form = array();
if ($data['dest_node_type'] == 'book') {
foreach (book_get_books() as $book) {
......@@ -599,7 +672,8 @@ function node_convert_node_convert_change($data, $op) {
$form['forum']['#options'][''] = t('- Please choose -');
}
return $form;
} elseif ($op == 'options validate') {
}
elseif ($op == 'options validate') {
$form_state = $data['form_state'];
if ($data['dest_node_type'] == 'forum') {
$containers = variable_get('forum_containers', array());
......@@ -610,4 +684,44 @@ function node_convert_node_convert_change($data, $op) {
}
}
}
}
\ No newline at end of file
}
// This isn't usable yet
/*
function node_convert_action_info() {
return array(
'node_convert_convert_action' => array(
'description' => t("Convert a node"),
'type' => 'node',
'configurable' => TRUE,
'hooks' => array('any' => TRUE),
)
);
}
function node_convert_convert_action($node, $context = array()) {
if (empty($context['destination_type'])) {
drupal_set_message(t("There is no destination type."), 'error');
return;
} elseif ($context['destination_type'] == $context['node']->type) {
drupal_set_message(t("Please choose another destination type."), 'error');
}
drupal_set_message(print_r($context, TRUE));
}
function node_convert_convert_action_form($context) {
$types = node_convert_return_access_node_types('to');
if ($types != FALSE) {
$options = $types;
$form['destination_type'] = array('#type' => 'select', '#options' => $options, '#title' => t("To what content type should this node be converted"));
} else {
$form['destination_type'] = array('#type' => 'markup', '#value' => t("You don't have access to any node types."));
}
return $form;
}
function node_convert_convert_action_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
return array('destination_type' => $form_state['values']['destination_type']);
} */
\ No newline at end of file
<?php
// $Id$
/**
* @file
* Node convert Simpletest Unit
*
* Tests for basic Node convert functionality.
*/
class NodeConvertTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
......@@ -12,7 +19,7 @@ class NodeConvertTestCase extends DrupalWebTestCase {
'group' => t('NodeConvert'),
);
}
function setUp() {
// The first thing a setUp() method should always do is call its parent setUp() method.
// If you need to enable any modules (including the one being tested),
......@@ -32,7 +39,7 @@ class NodeConvertTestCase extends DrupalWebTestCase {
// The last thing a tearDown() method should always do is call its parent tearDown() method.
parent::tearDown();
}
function testSimpleNodeConvert() {
$type1_name = $this->randomName(4);
$type2_name = $this->randomName(4);
......@@ -44,29 +51,32 @@ class NodeConvertTestCase extends DrupalWebTestCase {
$result = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $node->nid));
$this->assertEqual($result, $type2_name, t("Simple node conversion passed."));
}
function testInvalidNidConvert() {
$result = node_convert_node_convert(-1, $this->randomName(4), array(), array(), TRUE);
$this->assertFalse($result, t("Node conversion didn't pass due to illegal nid."));
}
function testSimpleNodeConvertUI() {
$admin_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages', 'administer nodes', 'administer content types'));
$this->drupalLogin($admin_user);
$type1_name = $this->randomName(4);
$type2_name = $this->randomName(4);
$type1 = $this->drupalCreateContentType(array('type' => $type1_name, 'name' => $type1_name));
$type2 = $this->drupalCreateContentType(array('type' => $type2_name, 'name' => $type2_name));
$admin_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages', 'administer nodes', 'administer content types', 'administer conversion', 'convert from '. $type1_name, 'convert to '. $type2_name));
$this->drupalLogin($admin_user);
$edit['type'] = $type1_name;
$node = $this->drupalCreateNode($edit);
$node = $this->drupalCreateNode($edit);
$edit = array();
$edit['destination_type'] = $type2_name;
$this->drupalPost('node/'. $node->nid .'/convert', $edit, t("Next"));
$this->drupalPost(NULL, array(), t("Convert"));
$this->assertText(t("The current node has been converted succesufuly."), t("Simple node conversion ui test passed."));
$result = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $node->nid));
$this->assertEqual($result, $type2_name, t("The converted node type is equal to the destination node type."));
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment