diff --git a/src/Plugin/views/field/ViewsSend.php b/src/Plugin/views/field/ViewsSend.php index 64d990d..19bb3f6 100644 --- a/src/Plugin/views/field/ViewsSend.php +++ b/src/Plugin/views/field/ViewsSend.php @@ -108,7 +108,44 @@ class ViewsSend extends BulkForm { $display = $form['display']['#value']; $config = \Drupal::configFactory()->getEditable('views_send.user_settings'); $config_basekey = $display . '.uid:' . \Drupal::currentUser()->id(); + $config_defaults_basekey = $display . '.defaults'; $form_state_values = $form_state->getValues(); + $view_send_defaults = [ + 'views_send_hide_recipient' => [ + 'views_send_to_name', + 'views_send_to_mail' + ], + 'views_send_hide_sender' => [ + 'views_send_from_name', + 'views_send_from_mail' + ], + 'views_send_hide_email' => [ + 'views_send_subject', + 'views_send_message', + 'views_send_message_format' + ], + 'views_send_hide_additional' => [ + 'views_send_priority', + 'views_send_receipt', + 'views_send_headers' + ], + 'views_send_hide_direct' => [ + 'views_send_direct' + ] + ]; + foreach ($view_send_defaults as $default_group => $keys) { + $group_value = $form_state->getValue($default_group); + $config->set($config_defaults_basekey . '.' . substr($default_group,11), $group_value); + foreach ($keys as $key) { + if (!empty($group_value)) { + $value = $form_state->getValue($key); + $config->set($config_defaults_basekey . '.' . substr($key,11), $value); + } + else { + $config->clear($config_defaults_basekey . '.' . substr($key,11)); + } + } + } if ($form_state->getValue('views_send_remember')) { foreach ($form_state_values as $key => $value) { $key = ($key == 'format') ? 'views_send_message_format' : $key; @@ -116,11 +153,10 @@ class ViewsSend extends BulkForm { $config->set($config_basekey . '.' . substr($key,11), $value); } } - $config->save(); } else { $config->clear($config_basekey); - $config->save(); } + $config->save(); $form_state->set('configuration', $form_state_values); // If a file was uploaded, process it. diff --git a/views_send.module b/views_send.module index 823c680..ce5f3ee 100644 --- a/views_send.module +++ b/views_send.module @@ -106,30 +106,60 @@ function views_send_config_form(&$form, &$form_state, $view) { '#type' => 'value', '#value' => $display, ); + $config_basekey = $display . '.uid:' . \Drupal::currentUser()->id(); + $config_defaults_basekey = $display . '.defaults'; + $site_config = \Drupal::config('system.site'); + $config = \Drupal::config('views_send.user_settings')->get($config_basekey); + $config_defaults = \Drupal::config('views_send.user_settings')->get($config_defaults_basekey); + $user_can_see_hidden_fields = Drupal::currentUser()->hasPermission('settings per view with views_send'); + + $user_can_see_sender = $user_can_see_hidden_fields || empty($config_defaults['hide_sender']); $form['from'] = array( - '#type' => 'details', + '#type' => $user_can_see_sender ? 'details' : 'hidden', + '#disabled' => !$user_can_see_sender, '#title' => t('Sender'), '#open' => TRUE, ); - $config_basekey = $display . '.uid:' . \Drupal::currentUser()->id(); - $site_config = \Drupal::config('system.site'); - $config = \Drupal::config('views_send.user_settings')->get($config_basekey); + + if (isset($config['from_name'])) { + $from_name_default = $config['from_name']; + } + else if (isset($config_defaults['from_name'])){ + $from_name_default = $config_defaults['from_name']; + } + else { + $from_name_default = $site_config->get('name'); + } + $form['from']['views_send_from_name'] = array( - '#type' => 'textfield', + '#type' => $user_can_see_sender ? 'textfield' : 'hidden', + '#disabled' => !$user_can_see_sender, '#title' => t('Sender\'s name'), '#description' => t("Enter the sender's human readable name."), - '#default_value' => isset($config['from_name']) ? $config['from_name'] : $site_config->get('name'), + '#default_value' => $from_name_default, '#maxlen' => 255, ); + + if (isset($config['from_mail'])) { + $default_from_mail = $config['from_mail']; + } + else if (isset($config_defaults['from_mail'])){ + $default_from_mail = $config_defaults['from_mail']; + } + else { + $default_from_mail = $site_config->get('mail'); + } $form['from']['views_send_from_mail'] = array( - '#type' => 'textfield', + '#type' => $user_can_see_sender ? 'textfield' : 'hidden', + '#disabled' => !$user_can_see_sender, '#title' => t('Sender\'s e-mail'), '#description' => t("Enter the sender's e-mail address."), '#required' => TRUE, - '#default_value' => isset($config['from_mail']) ? $config['from_mail'] : $site_config->get('mail'), + '#default_value' => $default_from_mail, '#maxlen' => 255, ); + $fields = _views_send_get_fields_and_tokens($view, 'fields'); $tokens = _views_send_get_fields_and_tokens($view, 'tokens'); $fields_name_text = _views_send_get_fields_and_tokens($view, 'fields_name_text'); @@ -140,48 +170,92 @@ function views_send_config_form(&$form, &$form_state, $view) { '#value' => $tokens, ); + $user_can_see_recipient = $user_can_see_hidden_fields || empty($config_defaults['hide_recipient']); + $form['to'] = array( - '#type' => 'details', + '#type' => $user_can_see_recipient ? 'details' : 'hidden', + '#disabled' => !$user_can_see_recipient, '#title' => t('Recipients'), '#open' => TRUE, ); + if (isset($config['to_name'])) { + $default_to_name = $config['to_name']; + } + else if (isset($config_defaults['to_name'])) { + $default_to_name = $config_defaults['to_name']; + } + else { + $default_to_name = ''; + } $form['to']['views_send_to_name'] = array( - '#type' => 'select', + '#type' => $user_can_see_recipient ? 'select' : 'hidden', + '#disabled' => !$user_can_see_recipient, '#title' => t('Field used for recipient\'s name'), '#description' => t('Select which field from the current view will be used as recipient\'s name.'), '#options' => $fields_options, - '#default_value' => isset($config['to_name']) ? $config['to_name'] : '', + '#default_value' => $default_to_name, ); + if (isset($config['to_mail'])) { + $default_to_mail = $config['to_mail']; + } + else if (isset($config_defaults['to_mail'])) { + $default_to_mail = $config_defaults['to_mail']; + } + else { + $default_to_mail = ''; + } $form['to']['views_send_to_mail'] = array( - '#type' => 'select', + '#type' => $user_can_see_recipient ? 'select' : 'hidden', + '#disabled' => !$user_can_see_recipient, '#title' => t('Field used for recipient\'s e-mail'), '#description' => t('Select which field from the current view will be used as recipient\'s e-mail.'), '#options' => $fields_options, - '#default_value' => isset($config['to_mail']) ? $config['to_mail'] : '', + '#default_value' => $default_to_mail, '#required' => TRUE, ); + + $form['mail'] = array( '#type' => 'details', '#title' => t('E-mail content'), '#open' => TRUE, ); + if (isset($config['subject'])) { + $default_subject = $config['subject']; + } + else if (isset($config_defaults['subject'])) { + $default_subject = $config_defaults['subject']; + } + else { + $default_subject = ''; + } $form['mail']['views_send_subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), '#description' => t('Enter the e-mail\'s subject. You can use tokens in the subject.'), '#maxlen' => 255, '#required' => TRUE, - '#default_value' => isset($config['subject']) ? $config['subject'] : '', + '#default_value' => $default_subject, ); $saved_message = isset($config['message']) ? $config['message'] : array(); + + if (isset($config['message'])) { + $default_message = $config['message']; + } + else if (isset($config_defaults['message'])) { + $default_message = $config_defaults['message']; + } + else { + $default_message = array(); + } $form['mail']['views_send_message'] = array( '#type' => 'text_format', - '#format' => isset($saved_message['format']) ? $saved_message['format'] : 'plain_text', + '#format' => isset($default_message['format']) ? $default_message['format'] : 'plain_text', '#title' => t('Message'), '#description' => t('Enter the body of the message. You can use tokens in the message.'), '#required' => TRUE, '#rows' => 10, - '#default_value' => isset($saved_message['value']) ? $saved_message['value'] : '', + '#default_value' => $default_message['value'], ); $form['mail']['token'] = array( '#type' => 'details', @@ -223,12 +297,25 @@ function views_send_config_form(&$form, &$form_state, $view) { ); } + $user_can_see_additional = $user_can_see_hidden_fields || empty($config_defaults['hide_additional']); $form['additional'] = array( - '#type' => 'details', + '#type' => $user_can_see_additional ? 'details' : 'hidden', + '#disabled' => !$user_can_see_additional, '#title' => t('Additional e-mail options'), ); + + if (isset($config['priority'])) { + $default_priority = $config['priority']; + } + else if (isset($config_defaults['priority'])) { + $default_priority = $config_defaults['priority']; + } + else { + $default_priority = 0; + } $form['additional']['views_send_priority'] = array( - '#type' => 'select', + '#type' => $user_can_see_additional ? 'select' : 'hidden', + '#disabled' => !$user_can_see_additional, '#title' => t('Priority'), '#options' => array( VIEWS_SEND_PRIORITY_NONE => t('none'), @@ -239,27 +326,60 @@ function views_send_config_form(&$form, &$form_state, $view) { VIEWS_SEND_PRIORITY_LOWEST => t('lowest') ), '#description' => t('Note that e-mail priority is ignored by a lot of e-mail programs.'), - '#default_value' => isset($config['priority']) ? $config['priority'] : 0, + '#default_value' => $default_priority, ); + + if (isset($config['receipt'])) { + $default_receipt = $config['receipt']; + } + else if (isset($config_defaults['receipt'])) { + $default_receipt = $config_defaults['receipt']; + } + else { + $default_receipt = 0; + } $form['additional']['views_send_receipt'] = array( - '#type' => 'checkbox', + '#type' => $user_can_see_additional ? 'checkbox' : 'hidden', + '#disabled' => !$user_can_see_additional, '#title' => t('Request receipt'), - '#default_value' => isset($config['receipt']) ? $config['receipt'] : 0, + '#default_value' => $default_receipt, '#description' => t('Request a Read Receipt from your e-mails. A lot of e-mail programs ignore these so it is not a definitive indication of how many people have read your message.'), ); + if (isset($config['headers'])) { + $default_headers = $config['headers']; + } + else if (isset($config_defaults['headers'])) { + $default_headers = $config_defaults['headers']; + } + else { + $default_headers = ''; + } $form['additional']['views_send_headers'] = array( - '#type' => 'textarea', + '#type' => $user_can_see_additional ? 'textarea' : 'hidden', + '#disabled' => !$user_can_see_additional, '#title' => t('Additional headers'), '#description' => t("Additional headers to be send with the message. You'll have to enter one per line. Example:
Reply-To: noreply@example.com\nX-MyCustomHeader: Whatever
"), '#rows' => 4, - '#default_value' => isset($config['headers']) ? $config['headers'] : '', + '#default_value' => $default_headers, ); + $user_can_see_direct = $user_can_see_hidden_fields || empty($config_defaults['hide_direct']); + if (isset($config['direct'])) { + $default_direct = $config['direct']; + } + else if (isset($config_defaults['direct'])) { + $default_direct = $config_defaults['direct']; + } + else { + $default_direct = TRUE; + } $form['views_send_direct'] = array( - '#type' => 'checkbox', + '#type' => $user_can_see_direct ? 'checkbox' : 'hidden', + '#disabled' => !$user_can_see_direct, '#title' => t('Send the message directly using the Batch API.'), - '#default_value' => isset($config['direct']) ? $config['direct'] : TRUE, + '#default_value' => $default_direct, ); + $form['views_send_carbon_copy'] = array( '#type' => 'checkbox', '#title' => t('Send a copy of the message to the sender.'), @@ -271,6 +391,48 @@ function views_send_config_form(&$form, &$form_state, $view) { '#title' => t('Remember these values for the next time a mass mail is sent.'), '#default_value' => isset($config['remember']) ? $config['remember'] : FALSE, ); + + $form['default_settings'] = array( + '#type' => 'details', + '#title' => t('Set default settings for all users'), + '#access' => $user_can_see_hidden_fields, + ); + $form['default_settings']['views_send_hide_recipient'] = array( + '#type' => 'checkbox', + '#title' => t('Save & hide recipient fields'), + '#default_value' => isset($config_defaults['hide_recipient']) ? $config_defaults['hide_recipient'] : 0, + '#description' => t('The "Recipient" fields will be hidden and default values will be used. Only users with "configure default settings" permission can modify the field.'), + '#access' => $user_can_see_hidden_fields, + ); + $form['default_settings']['views_send_hide_sender'] = array( + '#type' => 'checkbox', + '#title' => t('Save & hide sender fields'), + '#default_value' => isset($config_defaults['hide_sender']) ? $config_defaults['hide_sender'] : 0, + '#description' => t('The "Sender" fields will be hidden and default values will be used. Only users with "configure default settings" permission can modify the field.'), + '#access' => $user_can_see_hidden_fields, + ); + $form['default_settings']['views_send_hide_email'] = array( + '#type' => 'checkbox', + '#title' => t('Save & hide email content fields'), + '#default_value' => isset($config_defaults['hide_email']) ? $config_defaults['hide_email'] : 0, + '#description' => t('The "Email Content" fields will be hidden and default values will be used. Only users with "configure default settings" permission can modify the field.'), + '#access' => $user_can_see_hidden_fields, + ); + $form['default_settings']['views_send_hide_additional'] = array( + '#type' => 'checkbox', + '#title' => t('Save & hide "additional" options'), + '#default_value' => isset($config_defaults['hide_additional']) ? $config_defaults['hide_additional'] : 0, + '#description' => t('The "additional" fieldset will be hidden and default values will be used. Only users with "configure default settings" permission can modify the field.'), + '#access' => $user_can_see_hidden_fields, + ); + $form['default_settings']['views_send_hide_direct'] = array( + '#type' => 'checkbox', + '#title' => t('Save & hide "send direct" checkbox'), + '#default_value' => isset($config_defaults['hide_direct']) ? $config_defaults['hide_direct'] : 0, + '#description' => t('The "send direct" checkbox will be hidden and the default value will be used. Only users with "configure default settings" permission can modify the field.'), + '#access' => $user_can_see_hidden_fields, + ); + $query = UrlHelper::filterQueryParameters($_GET, array('q')); $url = Url::fromRoute('')->setOption('query', $query); $form['actions'] = array( diff --git a/views_send.permissions.yml b/views_send.permissions.yml index 4c504b7..795a353 100644 --- a/views_send.permissions.yml +++ b/views_send.permissions.yml @@ -1,6 +1,9 @@ administer views_send: title: 'Administer mass mail with Views' description: 'Configure sending of e-mails to a list created with Views.' +settings per view with views_send: + title: 'Configure default settings per view' + description: 'Set default values and hide specific fields per view.' mass mailing with views_send: title: 'Send mass mail with Views' description: 'Send e-mails to a list created with Views.'