--- /Users/mainuser/Downloads/nodetype/nodetype.module 2008-03-23 16:48:34.000000000 +0200 +++ /Volumes/10.0.1.8/drupalmultisite/sites/all/modules/contrib-modified/nodetype/nodetype.module 2010-01-11 18:09:23.000000000 +0200 @@ -19,6 +19,10 @@ function nodetype_nodeapi(&$node, $op, $ $types = node_get_types(); drupal_set_message(t('The type of the post %title has been changed from %old to %new.', array('%title' => $node->title, '%old' => $types[$node->type]->name, '%new' => $types[$node->nodetype]->name))); $node->type = $node->nodetype; + module_invoke_all('nodetype', 'changed', $node); + if (function_exists('rules_invoke_event')) { + rules_invoke_event('nodetype_changed', $node); + } } break; } @@ -28,26 +32,203 @@ function nodetype_nodeapi(&$node, $op, $ * Implementation of hook_form_alter(). */ function nodetype_form_alter(&$form, $form_state, $form_id) { - if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && isset($form['#node']->nid) && user_access('change node types')) { + if ($form_id == 'node_type_form') { + $ctype = $form['#node_type']->type; $types = node_get_types(); foreach ($types as $type => $info) { - if ($form['type']['#value'] == $type) { - $current_type = $types[$type]; + if ($ctype == $type) { + unset($types[$type]); + } + else { + $types[$type] = $info->name; + } + } + $form['nodetype'] = array( + '#type' => 'fieldset', + '#title' => t('Content type change'), + '#description' => t('These settings control whether the type of nodes of this content type can be changed.'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + + $form['nodetype']['nodetype_change_'. $ctype] = array( + '#type' => 'checkbox', + '#title' => t('Allow nodes of this type to have their content type changed.'), + '#default_value' => variable_get('nodetype_change_'. $ctype, FALSE), + ); + + $form['nodetype']['nodetype_changeto_'. $ctype] = array( + '#type' => 'select', + '#title' => t('Can be changed into'), + '#multiple' => TRUE, + '#description' => t('The node types that can be used when changing the content type.'), + '#options' => $types, + '#default_value' => variable_get('nodetype_changeto_'. $ctype, array()), + ); + + $form['#submit'][] = 'nodetype_node_settings_submit'; + } + + if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && isset($form['#node']->nid) && user_access('change node types')) { + $ctype = $form['type']['#value']; + if (!variable_get('nodetype_change_'. $ctype, FALSE)) { + return; + } + $types = node_get_types(); + $cname = $types[$ctype]->name; + $atypes = variable_get('nodetype_changeto_'. $ctype, array()); + foreach ($types as $type => $info) { + if (!in_array($type, $atypes)) { unset($types[$type]); } else { $types[$type] = $info->name; } } - $form['nodetype'] = array( '#type' => 'select', '#title' => t('Content type'), '#description' => t('Select a new content type that this post will be changed to. Only continue if you know what you are doing. This does not migrate data from CCK fields!'), - '#options' => array(0 => '<'. t('leave as is: @type', array('@type' => $current_type->name)) .'>') + $types, + '#options' => array(0 => '<'. t('leave as is: @type', array('@type' => $cname)) .'>') + $types, '#default_value' => isset($form_state['values']['nodetype']) ? $form_state['values']['nodetype'] : 0, - '#weight' => 10, + '#weight' => -100, ); } } + +function nodetype_rules_event_info() { + return array( + 'nodetype_changed' => array( + 'label' => t('Node type is changed'), + 'module' => 'Nodetype', + 'arguments' => array( + 'content' => array('type' => 'node', 'label' => t('The node whose type was changed')), + ), + ), + ); +} + +/** + * Implementation of hook_hook_info(). + */ +function nodetype_hook_info() { + return array( + 'nodetype' => array( + 'nodetype' => array( + 'changed' => array( + 'runs when' => t('When node type has been changed'), + ), + ), + ), + ); +} + +/** + * Implementation of hook_nodetype(). + */ +function nodetype_nodetype($op, $node) { + if ($op == 'changed') { + $aids = _trigger_get_hook_aids('nodetype', $op); + $context = array( + 'hook' => 'nodetype', + 'op' => $op, + 'node' => $node, + ); + actions_do(array_keys($aids), $node, $context); + watchdog('nodetype', 'Node type was changed to @type', array('@type' => $node->type)); + } + else { + watchdog('nodetype', 'Trigger not ok!'); + } +} + +/** + * Implementation of hook_action_info(); + */ + +function nodetype_action_info() { + return array( + 'node_change_type_action' => array( + 'type' => 'node', + 'description' => t('Change type of node'), + 'configurable' => TRUE, + 'triggers' => array(''), + ), + ); +} + +function node_change_type_action_form($context) { + $types = node_get_types(); + foreach ($types as $type => $info) { + $types[$type] = $info->name; + } + + + $form['nodetype'] = array( + '#type' => 'select', + '#title' => t('Content type'), + '#description' => t('Select a new content type. Only continue if you know what you are doing. This does not migrate data from CCK fields!'), + '#options' => $types, + ); + + return $form; +} + +function node_change_type_action_submit($form, $form_state) { + return array('nodetype' => $form_state['values']['nodetype']); +} + +function node_change_type_action($node, $context) { + $newtype = $context['nodetype']; + $ctype = $node->type; + if (!variable_get('nodetype_change_'. $ctype, FALSE)) { + drupal_set_message(t('Cannot change type of ').$node->title.t(' to ').$newtype, 'warning'); + return; + } + $types = node_get_types(); + $cname = $types[$ctype]->name; + $atypes = variable_get('nodetype_changeto_'. $ctype, array()); + foreach ($types as $type => $info) { + if (!in_array($type, $atypes)) { + unset($types[$type]); + } + else { + $types[$type] = $info->name; + } + } + if (array_key_exists($newtype, $types)) { + $node->type = $newtype; + node_save($node); + if (function_exists('rules_invoke_event')) { + rules_invoke_event('nodetype_changed', $node); + } + } + else { + drupal_set_message(t('Cannot change type of ').$node->title.t(' to ').$newtype, 'warning'); + } +} + +/** + * Implementation of hook_action_info_alter(). + */ + +function nodetype_action_info_alter(&$info) { + foreach ($info as $type => $data) { + if (stripos($type, "send_email") === 0) { + if (isset($info[$type]['hooks'])) { + array_merge($info[$type]['hooks']['nodetype'], array('changed',)); + } + else { + $info[$type]['hooks']['nodetype'] = array('changed',); + } + } + } +} + +function nodetype_node_settings_submit($form, &$form_state) { + $ctype = $form['#node_type']->type; + + variable_set('nodetype_change_'. $ctype, $form_state['values']['nodetype_change_'. $ctype]); + variable_set('nodetype_changeto_'. $ctype, $form_state['values']['nodetype_changeto_'. $ctype]); +} \ No newline at end of file