diff --git a/userpoints_nodelimit/README.txt b/userpoints_nodelimit/README.txt index d320df3..624f092 100644 --- a/userpoints_nodelimit/README.txt +++ b/userpoints_nodelimit/README.txt @@ -4,7 +4,7 @@ Eric Wright, eric.wright@bluevestconsulting.com INSTALLATION 1. Extract to the site/all/modules -2. Enable in Administer > Modules (admin/build/modules). +2. Enable in Administer > Modules (admin/modules). CONFIGURATION diff --git a/userpoints_nodelimit/userpoints_nodelimit.info b/userpoints_nodelimit/userpoints_nodelimit.info index c9d6ac5..5768289 100644 --- a/userpoints_nodelimit/userpoints_nodelimit.info +++ b/userpoints_nodelimit/userpoints_nodelimit.info @@ -1,5 +1,6 @@ name = User Points Node Limit description = Limits the creation of specific nodes based on the available number of userpoints package = Userpoints -core = 6.x +core = 7.x +configure = admin/config/people/userpoints/settings dependencies[] = userpoints diff --git a/userpoints_nodelimit/userpoints_nodelimit.install b/userpoints_nodelimit/userpoints_nodelimit.install index fa861da..9928edd 100644 --- a/userpoints_nodelimit/userpoints_nodelimit.install +++ b/userpoints_nodelimit/userpoints_nodelimit.install @@ -1,32 +1,41 @@ -Content Types settings page.', array('@url' => url('admin/content/types/list')))); -} - - - -/** - * Implementation of hook_uninstall(). - */ -function userpoints_nodelimit_uninstall() { - foreach (node_get_types('names') as $type => $type_name) { - variable_del('userpoints_nodelimit_'. $type); - variable_del('userpoints_nodelimit_nodepoints_'. $type); - } -} - -/** - * make sure hooks are invoked after cck main hooks - */ -function userpoints_nodelimit_update_1() { - $ret = array(); - $ret[] = update_sql("UPDATE {system} SET weight = 5 WHERE name = 'userpoints_nodelimit'"); - return $ret; -} \ No newline at end of file +fields(array( + 'weight' => 5, + )) + ->condition('name', 'userpoints_nodelimit') + ->execute(); +} + +/** + * Implements hook_uninstall(). + */ +function userpoints_nodelimit_uninstall() { + foreach (node_type_get_types('names') as $type => $type_name) { + variable_del('userpoints_nodelimit_' . $type); + variable_del('userpoints_nodelimit_nodepoints_' . $type); + variable_del('userpoints_nodelimit_category_' . $type); + } + variable_del('userpoints_nodelimit'); + variable_del('userpoints_nodelimit_nodepoints'); + variable_del('userpoints_nodelimit_category'); +} + +/** + * Implements hook_update_last_removed(). + */ +function userpoints_nodelimit_update_last_removed() { + // Removed the legacy update_1, that set the weight of nodelimit. + // The next update function is userpoints_nodelimit_update_7100(). + return 1; +} diff --git a/userpoints_nodelimit/userpoints_nodelimit.module b/userpoints_nodelimit/userpoints_nodelimit.module index e73013a..0d32731 100644 --- a/userpoints_nodelimit/userpoints_nodelimit.module +++ b/userpoints_nodelimit/userpoints_nodelimit.module @@ -1,150 +1,208 @@ type .'_node_form' == $form_id) { - $type = $form['#node']->type; - // This is a node form; user is either adding a new node or editing an existing one. - // Don't check if user is editing node. Access check has happened already, so user is assumed to have access to edit / create this node type. - if (userpoints_nodelimit_get_setting($type) == USERPOINTS_NODELIMIT_ENABLED) { - // check the number of available user points against the requirements for this node - // Only check when user is adding a new node. $form['#node']->nid is unset when adding a new node. - if (!isset($form['#node']->nid) && !userpoints_nodelimit_check($type)) { - $friendly_name = node_get_types('name', $form['#node']); - drupal_set_message(theme('userpoints_nodelimit_insufficent_points_message', $type, $friendly_name), 'error'); - //destroy the form - $form = NULL; - //return a simple hidden field so that function doesn't fail - $form['dummy'] = array( - '#type' => 'hidden', - '#default_value' => '', - ); - } - else { - // add a validate function as the points may change between the opening for the form - // and the submission of the form, so we should check again prior to submission - $form['#validate'][] = 'userpoints_nodelimit_validate'; - } - } - } - } -} - -/** - * Form validation - */ -function userpoints_nodelimit_validate($form, &$form_state) { - // check the user points and confirm they have enough - $type = $form['#node']->type; - // check the number of available user points against the requirements for this node - // Only check when user is adding a new node. $form['#node']->nid is unset when adding a new node. - if (!isset($form['#node']->nid) && !userpoints_nodelimit_check($type)) { - $friendly_name = node_get_types('name', $form['#node']); - form_set_error('userpoints_nodelimit_details', theme('userpoints_nodelimit_insufficent_points_message',$type, $friendly_name)); - } -} - -/** - * Node Limit Error Message - */ -function theme_userpoints_nodelimit_insufficent_points_message($node_type, $friendly_name) { - $html_message = t('You do not have enough !points to create @friendly content.
You currently have @user_points !points, you need @node_points !points to create a @friendly.', - array_merge(userpoints_translation(), - array('@user_points' => userpoints_get_current_points($user->uid), - '@node_points' => variable_get('userpoints_nodelimit_nodepoints_'.$node_type, '?'), - '@friendly' => $friendly_name, - '@type' => $node_type - ) - ) - ); - return $html_message; -} - -/** - * Helper function for hook_form_alter() renders the settings per node-type. - */ -function userpoints_nodelimit_node_settings_form(&$form) { +function userpoints_nodelimit_form_node_type_form_alter(&$form, &$form_state, $form_id) { $form['userpoints_nodelimit'] = array( '#type' => 'fieldset', - '#title' => t('User points node limit settings'), - '#weight' => 0, + '#title' => t('!Points node limit settings', userpoints_translation()), '#collapsible' => TRUE, '#collapsed' => TRUE, + '#access' => user_access('administer userpoints'), + '#weight' => 25, + '#group' => 'additional_settings', + '#attached' => array( + 'js' => array( + 'userpoints_nodelimit' => drupal_get_path('module', 'userpoints_nodelimit') . '/userpoints_nodelimit.js', + ), + ), ); - $form['userpoints_nodelimit']['userpoints_nodelimit'] = array( '#type' => 'checkbox', - '#title' => t('Enable User Points Node Limit for this type.'), - '#description' => t('Node Limit will check to ensure users trying to create this node type have the required number of user points.'), - '#default_value' => userpoints_nodelimit_get_setting($form['#node_type']->type), + '#title' => t('Enable !points node limit for this type.', userpoints_translation()), + '#description' => t('Node limit will check to ensure users trying to create this node type have the required number of !points.', userpoints_translation()), + '#default_value' => variable_get('userpoints_nodelimit_' . $form['#node_type']->type, userpoints_nodelimit_get_setting($form['#node_type']->type)), ); - $form['userpoints_nodelimit']['userpoints_nodelimit_nodepoints'] = array( '#type' => 'textfield', - '#title' => t('User Points Required'), - '#description' => t('Enter the number of user points required to create a new node of this type.'), - '#default_value' => variable_get('userpoints_nodelimit_nodepoints_'. $form['#node_type']->type, '0'), + '#title' => t('!Points required', userpoints_translation()), + '#description' => t('Enter the number of !points required to create a new node of this type.', userpoints_translation()), + '#default_value' => variable_get('userpoints_nodelimit_nodepoints_' . $form['#node_type']->type, variable_get('userpoints_nodelimit_nodepoints', 0)), + ); + $form['userpoints_nodelimit']['userpoints_nodelimit_category'] = array( + '#type' => 'select', + '#title' => t('!Points category for new content', userpoints_translation()), + '#description' => t('Choose the category of !points to be required when a user adds new content.', userpoints_translation()), + '#options' => userpoints_get_categories(), + '#default_value' => variable_get('userpoints_nodelimit_category', variable_get('userpoints_nodelimit_category', 0)), ); } /** - * Gets the userpoints node limit setting associated with the given content type. + * Implements hook_node_type_delete(). */ -function userpoints_nodelimit_get_setting($type) { - return variable_get('userpoints_nodelimit_'. $type, USERPOINTS_NODELIMIT_DISABLED); +function userpoints_nodelimit_node_type_delete($info) { + variable_del('userpoints_nodelimit_' . $info->type); + variable_del('userpoints_nodelimit_nodepoints_' . $info->type); + variable_del('userpoints_nodelimit_category_' . $info->type); } /** - * Implementation of hook_node_type(). + * Implements hook_node_type_update(). */ -function userpoints_nodelimit_node_type($op, $info) { - switch ($op) { - case 'delete': - variable_del('userpoints_nodelimit_'. $info->type); - variable_del('userpoints_nodelimit_nodepoints_'. $info->type); - break; - case 'update': - if (!empty($info->old_type) && $info->old_type != $info->type) { - variable_set('userpoints_nodelimit_'. $info->type, userpoints_nodelimit_get_setting($info->old_type)); - variable_set('userpoints_nodelimit_nodepoints_'. $info->type, variable_get('userpoints_nodelimit_nodepoints_'. $info->old_type, '')); - variable_del('userpoints_nodelimit_'. $info->old_type); - variable_del('userpoints_nodelimit_nodepoints_'. $info->old_type); +function userpoints_nodelimit_node_type_update($info) { + if (!empty($info->old_type) && $info->old_type != $info->type) { + variable_set('userpoints_nodelimit_' . $info->type, userpoints_nodelimit_get_setting($info->old_type)); + variable_set('userpoints_nodelimit_nodepoints_' . $info->type, variable_get('userpoints_nodelimit_nodepoints_' . $info->old_type, '')); + variable_set('userpoints_nodelimit_category_' . $info->type, variable_get('userpoints_nodelimit_category_' . $info->old_type, '')); + variable_del('userpoints_nodelimit_' . $info->old_type); + variable_del('userpoints_nodelimit_nodepoints_' . $info->old_type); + variable_del('userpoints_nodelimit_category_' . $info->old_type); + } +} + +/** + * Implements hook_form_alter(). + */ +function userpoints_nodelimit_form_alter(&$form, &$form_state, $form_id) { + if (isset($form['#node']) && $form['#node']->type . '_node_form' == $form_id) { + // This is a node form; user is either adding a new node or editing an + // existing one. + // Don't check if user is editing node. Access check has happened already, + //so user is assumed to have access to edit / create this node type. + if (userpoints_nodelimit_get_setting($form['#node']->type) == USERPOINTS_NODELIMIT_ENABLED) { + // Check the number of available user points against the requirements for + // this node. + // Only check when user is adding a new node. $form['#node']->nid is unset + // when adding a new node. + if (!isset($form['#node']->nid) && !userpoints_nodelimit_check($form['#node']->type)) { + drupal_set_message(userpoints_nodelimit_insufficent_points_message($form['#node']->type), 'error'); + // Return no access to the form. This allows other modules to override + // this value as needed. + $form['#access'] = FALSE; } + else { + // Add a validate function as the points may change during submission. + // (With Rules for example.) + $form['#validate'][] = 'userpoints_nodelimit_validate'; + } + } + } +} + +/** + * Form validation for node submission. + */ +function userpoints_nodelimit_validate($form, &$form_state) { + // Check the number of available user points against the requirements for + // this node. + // Only check when user is adding a new node. $form['#node']->nid is unset + // when adding a new node. + if (!isset($form['#node']->nid) && !userpoints_nodelimit_check($form['#node']->type)) { + form_set_error('userpoints_nodelimit_details', userpoints_nodelimit_insufficent_points_message($form['#node']->type)); + } +} + +/** + * Implements hook_userpoints(). + */ +function userpoints_nodelimit_userpoints($op, $params = array()) { + switch ($op) { + case 'setting': + $form['userpoints_nodelimit'] = array( + '#type' => 'fieldset', + '#title' => t('Node limit'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#access' => user_access('administer userpoints'), + '#weight' => 25, + '#group' => 'settings_additional', + '#attached' => array( + 'js' => array( + 'userpoints_nodelimit' => drupal_get_path('module', 'userpoints_nodelimit') . '/userpoints_nodelimit.js', + ), + ), + ); + $form['userpoints_nodelimit']['userpoints_nodelimit'] = array( + '#type' => 'checkbox', + '#title' => t('Enabled by default.'), + '#default_value' => variable_get('userpoints_nodelimit', TRUE), + '#description' => t('If checked, all content types require !points by default. This can be overridden for each content type on the content type edit page.', userpoints_translation()), + ); + $form['userpoints_nodelimit']['userpoints_nodelimit_nodepoints'] = array( + '#type' => 'textfield', + '#title' => t('Default !points for new content', userpoints_translation()), + '#description' => t('Set the default number of !points to be required when a user adds new content. This can be overridden for each content type.', userpoints_translation()), + '#default_value' => variable_get('userpoints_nodelimit_nodepoints', 0), + '#size' => 5, + '#maxlength' => 5, + ); + $form['userpoints_nodelimit']['userpoints_nodelimit_category'] = array( + '#type' => 'select', + '#title' => t('Default !points category for new content', userpoints_translation()), + '#description' => t('Choose the category of !points to be required by default when a user adds new content. This can be overridden for each content type.', userpoints_translation()), + '#options' => userpoints_get_categories(), + '#default_value' => variable_get('userpoints_nodelimit_category', 0), + ); + return $form; break; } } /** - * Check if user has sufficient points to create a node of a certain type + * Helper function to return the node limit error mesage. */ -function userpoints_nodelimit_check($nodetype) { +function userpoints_nodelimit_insufficent_points_message($node_type) { global $user; + $html_message = t('You do not have enough !points to create @human_type content.
You currently have @user_points !points, you need @node_points !points to create a @human_type.', + array_merge(userpoints_translation(), array( + '@user_points' => userpoints_get_current_points($user->uid), + '@node_points' => variable_get('userpoints_nodelimit_nodepoints_' . $node_type, '?'), + '@human_type' => node_type_get_name($node_type), + ) + ) + ); + return $html_message; +} + +/** + * Helper function to get the userpoints node limit setting associated with the given content type. + */ +function userpoints_nodelimit_get_setting($type) { + return variable_get('userpoints_nodelimit_' . $type, variable_get('userpoints_nodelimit', USERPOINTS_NODELIMIT_DISABLED)); +} - $current_points = userpoints_get_current_points($user->uid); - $node_points = variable_get('userpoints_nodelimit_nodepoints_'. $nodetype, 0); +/** + * Helper function to check if user has sufficient points to create a node of a certain type. + */ +function userpoints_nodelimit_check($type) { + global $user; + $category = variable_get('userpoints_nodelimit_category_' . $type, variable_get('userpoints_nodelimit_category', 0)); + $node_points = variable_get('userpoints_nodelimit_nodepoints_' . $type, variable_get('userpoints_nodelimit_nodepoints', 0)); + $current_points = userpoints_get_current_points($user->uid, $category); if ($current_points >= $node_points) { return TRUE; } @@ -152,14 +210,3 @@ function userpoints_nodelimit_check($nodetype) { return FALSE; } } - -/** - * Implementation of hook_theme() - */ - -function userpoints_nodelimit_theme($existing, $type, $theme, $path) { - return array( - 'userpoints_nodelimit_insufficent_points_message' => array( - 'arguments' => array('node_type' => NULL, 'friendly_name' => NULL), - )); -} \ No newline at end of file