diff --git a/message_private.admin.inc b/message_private.admin.inc index b3778b8..631b326 100644 --- a/message_private.admin.inc +++ b/message_private.admin.inc @@ -8,69 +8,73 @@ */ function message_private_admin_settings() { - $form['message_private_email_notifications'] = array( + // Global email notifications on/off checkbox. + $form[MESSAGE_PRIVATE_EMAIL_NOTIFICATIONS] = array( '#type' => 'checkbox', '#title' => t('Message Private Email Notifications'), - '#default_value' => variable_get('message_private_email_notifications', TRUE), + '#default_value' => variable_get(MESSAGE_PRIVATE_EMAIL_NOTIFICATIONS, TRUE), '#description' => t('Global On / Off checkbox for emails notifying users of a new private message'), ); - $form['message_private_message_limit'] = array( + // Role based message create limit on/off checkbox. + $form[MESSAGE_PRIVATE_MESSAGE_LIMIT] = array( '#type' => 'checkbox', '#title' => t('Limit Message Create By Role'), - '#default_value' => variable_get('message_private_message_limit', FALSE), + '#default_value' => variable_get(MESSAGE_PRIVATE_MESSAGE_LIMIT, FALSE), '#description' => t('Impose a message creation limit per interval. Users with multiple roles, get the highest limit from these roles'), ); - $form['message_private_message_interval_limit'] = array( + // Conditional fieldset for all message limitation settings. + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT] = array( '#title' => t('Message interval limits'), '#type' => 'fieldset', '#collapsible' => TRUE, '#collapsed' => TRUE, '#states' => array( 'invisible' => array( - ':input[name="message_private_message_limit"]' => array('checked' => FALSE), + ':input[name="' . MESSAGE_PRIVATE_MESSAGE_LIMIT . '"]' => array('checked' => FALSE), ), ), ); // Add a default fieldset. - $form['message_private_message_interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX] = array( + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT][MESSAGE_PRIVATE_DEFAULT_INDEX] = array( '#type' => 'fieldset', '#title' => 'Default limit', '#description' => t('Applies to all roles with blank entries below') ); - $form['message_private_message_interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX]['limit'] = array( + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_LIMIT_NAME] = array( '#type' => 'textfield', '#title' => t('Limit'), - '#default_value' => variable_get('message_private_default_limit', TRUE), + '#default_value' => variable_get(MESSAGE_PRIVATE_DEFAULT_LIMIT_NAME), '#description' => t('Enter a message limit ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX), ); - $form['message_private_message_interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX]['interval'] = array( + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_INTERVAL_NAME] = array( '#type' => 'textfield', '#title' => t('Interval'), - '#default_value' => variable_get('message_private_default_interval', TRUE), + '#default_value' => variable_get(MESSAGE_PRIVATE_DEFAULT_INTERVAL_NAME), '#description' => t('Enter an interval in minutes ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX), ); - foreach(user_roles() as $id => $role) { + // Generate variable names for all roles used with get/set in admin form. + foreach (user_roles() as $id => $role) { $role_name = str_replace(' ', '_', $role); $limit_name = 'message_private_' . $role_name . '_limit'; $interval_name = 'message_private_' . $role_name . '_interval'; - $form['message_private_message_interval_limit'][$id] = array( + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT][$id] = array( '#type' => 'fieldset', '#title' => $role, ); - $form['message_private_message_interval_limit'][$id][$limit_name] = array( + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT][$id][$limit_name] = array( '#type' => 'textfield', '#title' => t('Limit'), '#default_value' => variable_get($limit_name), '#description' => t('Enter a message limit ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX), ); - $form['message_private_message_interval_limit'][$id][$interval_name] = array( + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT][$id][$interval_name] = array( '#type' => 'textfield', '#title' => t('Interval'), '#default_value' => variable_get($interval_name), @@ -89,32 +93,59 @@ function message_private_admin_settings() { */ function message_private_admin_settings_validate($form, &$form_state) { + // Validate the default fieldset values. + _message_private_validate_fieldset( + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_LIMIT_NAME], + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_INTERVAL_NAME], + $form_state['values'][MESSAGE_PRIVATE_DEFAULT_LIMIT_NAME], + $form_state['values'][MESSAGE_PRIVATE_DEFAULT_INTERVAL_NAME] + ); + + // Cycle through the settings for each role and validate. foreach (user_roles() as $id => $role) { $role_name = str_replace(' ', '_', $role); $limit_name = 'message_private_' . $role_name . '_limit'; $interval_name = 'message_private_' . $role_name . '_interval'; - // Check both textfields per fieldset are set, give error if only 1 is set. - if (isset($form_state['values'][$limit_name]) && isset($form_state['values'][$interval_name])) { - // Check is numeric and between the boundaries. - if (!ctype_digit($form_state['values'][$limit_name]) - || $form_state['values'][$limit_name] > MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX - || $form_state['values'][$limit_name] < MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN) { - form_error($form['message_private_message_interval_limit'][$id][$limit_name], - t('Enter a numerical message limit between ' - . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX . '.')); - } - // Check is numeric and between the boundaries. - if (!ctype_digit($form_state['values'][$interval_name]) - || $form_state['values'][$interval_name] > MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX - || $form_state['values'][$interval_name] < MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN) { - form_error($form['message_private_message_interval_limit'][$id][$interval_name], - t('Enter a numerical interval in minutes between ' - . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX . '.')); - } - } elseif (isset($form_state['values'][$limit_name]) || isset($form_state['values'][$interval_name])) { - form_error($form['message_private_message_interval_limit'][$id][$limit_name], t('Both a limit and interval value are required.')); - form_error($form['message_private_message_interval_limit'][$id][$interval_name]); + _message_private_validate_fieldset( + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT][$id][$limit_name], + $form[MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT][$id][$interval_name], + $form_state['values'][$limit_name], + $form_state['values'][$interval_name] + ); + } +} + +/** + * Validate limit and interval values and show any errors on the form elements. + * + * @param $limit_element + * Limit form element reference. + * @param $interval_element + * Interval form element reference. + * @param $limit + * Limit value to validate. + * @param $interval + * Interval value to validate. + */ +function _message_private_validate_fieldset(&$limit_element, &$interval_element, $limit, $interval) { + // Validate role settings, check both textfields per fieldset are set. + if (!empty($limit) && !empty($interval)) { + // Check is numeric and between the boundaries. + if (!ctype_digit($limit) || $limit > MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX || $limit < MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN) { + form_error($limit_element, + t('Enter a numerical message limit between ' + . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX . '.')); + } + // Check is numeric and between the boundaries. + if (!ctype_digit($interval) || $interval > MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX || $interval < MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN) { + form_error($interval_element, + t('Enter a numerical interval in minutes between ' + . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX . '.')); } + } elseif (!empty($limit) || !empty($interval)) { + // Show error if only 1 textfield is set in each fieldset. + form_error($limit_element, t('Both a limit and interval value are required.')); + form_error($interval_element); } } diff --git a/message_private.module b/message_private.module index 82271bb..b04b1f3 100755 --- a/message_private.module +++ b/message_private.module @@ -3,6 +3,31 @@ * Message Private with access permissions based on message fields. */ +/** + * The variable name for email notifications. + */ +define('MESSAGE_PRIVATE_EMAIL_NOTIFICATIONS', 'message_private_email_notifications'); + +/** + * The string name for message limit. + */ +define('MESSAGE_PRIVATE_MESSAGE_LIMIT', 'message_private_message_limit'); + +/** + * The string name for message interval limit. + */ +define('MESSAGE_PRIVATE_MESSAGE_INTERVAL_LIMIT', 'message_private_message_interval_limit'); + +/** + * The variable name to be used when fetching default limit. + */ +define('MESSAGE_PRIVATE_DEFAULT_LIMIT_NAME', 'message_private_default_limit'); + +/** + * The variable name to be used when fetching default interval. + */ +define('MESSAGE_PRIVATE_DEFAULT_INTERVAL_NAME', 'message_private_default_interval'); + /** * The default index for settings such as role. */ @@ -28,6 +53,7 @@ define('MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX', 1440); */ define('MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN', 1); + /** * Implements hook_help(). */ @@ -247,26 +273,36 @@ function message_private_form_message_ui_instance_message_manage_alter(&$form, & * The form state including values submitted. */ function message_private_form_message_ui_instance_message_manage_validate($form, &$form_state) { - // If there is an imposed message limit, set in the admin settings interface. - if (variable_get('message_private_message_limit', FALSE)) { + // If there is an imposed message limit set in the admin settings interface. + if (variable_get('message_private_message_limit', FALSE) + && !user_access('bypass private message access control')) { global $user; + + // Get the role belonging to user with max message limit. $rid = _message_private_max_message_limit_role($user->roles); $role = user_role_load($rid); - $limit_name = 'message_private_' . $role->name . '_limit'; - $interval_name = 'message_private_' . $role->name . '_interval'; + + // Get the message limitation settings for this role. + $role_name = str_replace(' ', '_', $role->name); + $limit_name = 'message_private_' . $role_name . '_limit'; + $interval_name = 'message_private_' . $role_name . '_interval'; $interval = variable_get($interval_name); $limit = variable_get($limit_name); - // Get total amount of messages since last interval. + + // Calculate timestamp of the last interval. $current_timestamp = time(); $interval_timestamp = strtotime('-' . $interval . ' minutes', $current_timestamp); + + // Get total amount of this user's messages since last interval. $query = new EntityFieldQuery(); $total = $query->entityCondition('entity_type', 'message') ->entityCondition('bundle', 'private_message') - ->propertyCondition('created', $interval_timestamp, '>') + ->propertyCondition('timestamp', $interval_timestamp, '>') ->propertyCondition('uid', $user->uid) ->count() ->execute(); - // if total < limit, send message, if not display error. + + // Display error preventing message create when total messages over limit. if($total >= $limit) { form_error($form, t('Message create limit reached. Please try again later.')); } @@ -338,10 +374,12 @@ function message_private_form_user_profile_form_alter(&$form, &$form_state, $for */ function _message_private_max_message_limit_role($roles) { $limits = array(); - // Get the default limit and interval. - $limit_name = 'message_private_default_limit'; - $interval_name = 'message_private_default_interval'; - $limits[MESSAGE_PRIVATE_DEFAULT_INDEX] = variable_get($interval_name) / variable_get($limit_name); + $limit = variable_get(MESSAGE_PRIVATE_DEFAULT_LIMIT_NAME); + $interval = variable_get(MESSAGE_PRIVATE_DEFAULT_INTERVAL_NAME); + // Ensure we have existing valid numerical values for both variables. + if (!empty($limit) && ctype_digit($limit) && !empty($interval) && ctype_digit($interval)) { + $limits[MESSAGE_PRIVATE_DEFAULT_INDEX] = $interval / $limit; + } // Cycle through the roles and get each limit and interval. foreach ($roles as $rid => $role) { @@ -351,10 +389,11 @@ function _message_private_max_message_limit_role($roles) { $interval = variable_get($interval_name); $limit = variable_get($limit_name); - // Ensure values are greater than 0 & get interval required for one message. - if ($interval >= MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN && $limit >= MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN) { - $limits[$rid] = $interval_name / $limit; + // Ensure we have existing valid numerical values for both variables. + if (!empty($limit) && ctype_digit($limit) && !empty($interval) && ctype_digit($interval)) { + $limits[$rid] = $interval / $limit; } } + // The min value corresponds to the lowest interval required per message. return array_search(min($limits), $limits); }