View source
<?php
function commentformsettings_enable() {
$types = node_type_get_types();
foreach ($types as $type => $content_type) {
$variable = variable_get('commentformsettings_' . $type);
if (!isset($variable)) {
variable_set('commentformsettings_' . $type, array());
}
}
}
function commentformsettings_elements_default() {
return array(
'cfs_anonymousname' => 0,
'cfs_anonymousmail' => 0,
'cfs_anonymoushomepage' => 0,
'cfs_author' => 0,
'cfs_comment_cancel' => 1,
'cfs_inputformat' => 0,
'cfs_newadmin' => 1,
'cfs_submit' => t('Submit'),
'cfs_title' => 0,
);
}
function commentformsettings_elements_validate() {
return array(
'cfs_comment_cancel',
'cfs_newadmin',
);
}
function commentformsettings_get_settings($type) {
return variable_get('commentformsettings_' . $type, '');
}
function commentformsettings_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form') {
$path = drupal_get_path('module', 'commentformsettings') . '/includes/';
$form['var_comments'] = array(
'#type' => 'hidden',
'#value' => $form['#node_type']->type,
);
$settings = commentformsettings_get_settings($form['#node_type']->type);
if (module_exists('comment') && isset($form['comment'])) {
include_once DRUPAL_ROOT . '/' . $path . 'settings_comments.inc';
_commentformsettings_settings_form($form, $settings);
}
$form['#validate'][] = 'commentformsettings_settings_validate';
$form['#submit'][] = 'commentformsettings_settings_submit';
}
if (isset($form['#node']) && $form_id === 'comment_node_' . $form['#node']->type . '_form') {
$node = $form['#node'];
$path = drupal_get_path("module", "commentformsettings") . '/includes/';
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
}
if (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) {
$node = node_load(arg(2));
}
if (arg(0) == 'comment' && arg(1) == 'edit' && is_numeric(arg(2))) {
$node = node_load(arg(2));
}
if (module_exists('content_profile')) {
if (arg(0) == 'user' && is_numeric(arg(1))) {
$content = array();
foreach (content_profile_get_types('names') as $type => $type_name) {
$node = content_profile_load($type, arg(1));
if ($node) {
break;
}
}
}
}
$settings = commentformsettings_get_settings($node->type);
$elements = commentformsettings_elements_default();
foreach ($elements as $key => $vals) {
if (isset($settings[$key])) {
$ignore = array(
'cfs_pnc',
'cfs_inputformat',
);
if (!in_array($key, $ignore)) {
include_once DRUPAL_ROOT . '/' . $path . 'option_' . $key . '.inc';
$function = '_option_' . $key;
if (function_exists($function)) {
$function($form, $form_state, $settings, $node);
}
}
}
}
}
}
function commentformsettings_settings_validate($form, &$form_state) {
$path = drupal_get_path('module', 'commentformsettings') . '/includes/';
$elements = commentformsettings_elements_validate();
foreach ($elements as $key) {
include_once DRUPAL_ROOT . '/' . $path . 'validate_' . $key . '.inc';
$function = '_validate_' . $key;
if (function_exists($function)) {
$function($form, $form_state);
}
}
}
function commentformsettings_settings_submit($form, &$form_state) {
$values = $form_state['values'];
$name = 'commentformsettings_' . $values['var_comments'];
$elements = commentformsettings_elements_default();
foreach ($elements as $k => $v) {
$ignore[] = $k;
}
$ignore[] = $name;
foreach ($values as $key => $value) {
if (!in_array($key, $ignore)) {
unset($values[$key]);
}
else {
$data[$key] = $value;
}
}
variable_set($name, $data);
if (isset($values['var_comments'])) {
commentformsettings_purge($values['var_comments']);
}
}
function commentformsettings_purge($type = NULL) {
$elements = commentformsettings_elements_default();
if (isset($type)) {
variable_del('var_comments_' . $type);
foreach ($elements as $k => $v) {
variable_del($k . '_' . $type);
}
}
else {
foreach (node_type_get_names() as $type => $type_name) {
variable_del('var_comments_' . $type);
foreach ($elements as $k => $v) {
variable_del($k . '_' . $type);
}
}
}
}
function commentformsettings_features_pipe_node_alter(&$pipe, $data, $export) {
if (!empty($data) && module_exists('strongarm')) {
$variables = array(
'commentformsettings_',
);
foreach ($data as $node_type) {
foreach ($variables as $variable_name) {
$pipe['variable'][] = "{$variable_name}_{$node_type}";
}
}
}
}
function commentformsettings_node_type_delete($info) {
variable_del('commentformsettings_' . $info->type);
}
function commentformsettings_node_type_update($info) {
if (!empty($info->old_type) && $info->old_type != $info->type) {
$setting = variable_get('commentformsettings_' . $info->old_type, '');
variable_del('commentformsettings_' . $info->old_type);
variable_set('commentformsettings_' . $info->type, $setting);
}
}
function commentformsettings_element_info_alter(&$type) {
if (!module_exists('nodeformsettings') && isset($type['text_format']['#process'])) {
foreach ($type['text_format']['#process'] as &$callback) {
if ($callback === 'filter_process_format') {
$callback = 'commentformsettings_process_format';
}
}
}
}
function commentformsettings_process_format($element) {
if (!module_exists('nodeformsettings')) {
$element = filter_process_format($element);
}
if (isset($element['#entity'], $element['#entity']->node_type)) {
$type = $element['#entity']->node_type;
if (strpos($type, 'comment_node') !== FALSE) {
$fields = array(
'comment_body',
);
$settings = commentformsettings_get_settings(str_replace('comment_node_', '', $type));
$setting = 'cfs_inputformat';
if (isset($settings[$setting], $element['#field_name']) && count($settings) && $settings[$setting] == 1 && in_array($element['#field_name'], $fields)) {
$element['format']['#access'] = FALSE;
}
}
}
return $element;
}