I've made a few improvements to the edit groups page and I would like it to be integrated to your code. These fall into two categories: how this page looks like and possibility to edit buddy groups even if you don't have any buddies yet (use case: pre-populated default buddy groups such as Friends, Scool Mates, ...). I hope you will forgive my negligence to not provide these as patches. I will provide the changed functions. I hope these changes will be useful. Please note that I did my best but I am not a Drupal UI expert, so if you think there is something you can do better don't hesitate to change it.


function buddylist_form_edit_groups_add() {
  // Add group form
  $form['add_group']['group_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Group name'),
    '#description' => t('Groups are a way to keep your @buddies organized. Groups can be named whatever you like.', buddylist_translation()),
  );

  $form['add_group']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add'),
  );

  $form['add_group']['#type'] = 'fieldset';
  $form['add_group']['#title'] = t('Add new group');
  $form['add_group']['#collapsible'] = TRUE;
  $form['add_group']['#collapsed'] = FALSE;

  return $form;
}

function buddylist_form_edit_groups_remove($all_groups) {
  // Make a form to remove groups
  $form['remove']['groups'] = array(
    '#type' => 'checkboxes',
    '#return_value' => 1,
    '#title' => '',
    '#default_value' => null,
    '#options' => array_map('check_plain', $all_groups),
  );

  $form['remove']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Remove'),
  );

  $form['remove']['#type'] = 'fieldset';
  $form['remove']['#title'] = t('Remove group');
  $form['remove']['#collapsible'] = TRUE;
  $form['remove']['#collapsed'] = FALSE;

  return $form;
}


/**
 * Callback for the buddygroup editing page
 */
function buddylist_buddiesgroups_edit($uid) {
  $thisuser = user_load(array('uid' => $uid));
  drupal_set_title(t('%username\'s @buddy groups', array('%username' => $thisuser->name) + buddylist_translation()));

  $output['add'] = drupal_get_form('buddylist_form_edit_groups_add');

  $groups = buddylist_get_buddy_groups($uid);

  if (count($groups) > 0) {
    $output['remove'] = drupal_get_form('buddylist_form_edit_groups_remove', $groups);
    if ($buddies = buddylist_get_buddies($thisuser->uid))
      $output['table'] = drupal_get_form('buddylist_form_edit_groups_table', $buddies, $groups, $thisuser);
    else {
      // TODO: invent some informative message here
      // drupal_set_message(t('Unable to edit @buddy groups. Add @buddies to your @buddylist before making groups.', buddylist_translation()));
      return t('No @buddies found.', buddylist_translation()) . '<BR/><BR/>' . theme('buddylist_edit_groups', $output);
    }
  }
  else {
    drupal_set_message(t("You don't have any groups defined."));
  }

  return theme('buddylist_edit_groups', $output);
}


function theme_buddylist_form_edit_groups_table($form) {
  $rows = array();

  foreach ($form['table']['groups'] as $key => $value) {
    if(is_numeric($key)) {
      $rows[] = array(theme('username', user_load(array('uid' => $key))), drupal_render($form['table']['groups'][$key]));
    }
  }

  $headers = array(t('buddy'), t('@buddy groups', buddylist_translation()));
  $output .= theme('table', $headers, $rows);

  $output .= drupal_render($form);

  $fieldset['#type'] = 'fieldset';
  $fieldset['#title'] = t('Add or remove buddies to @buddy groups', buddylist_translation());
  $fieldset['#collapsible'] = TRUE;
  $fieldset['#collapsed'] = FALSE;
  $fieldset['#children'] = $output;
  $output = theme_fieldset($fieldset);

  return $output;
}


function buddylist_form_edit_groups_add_submit($form_id, $form_values) {
  global $user;
  $label_id = buddylist_buddygroup_new($user->uid, $form_values['group_name']);
}

Comments

attila75’s picture

Here is the code to validate the group name. The define line of course goes after the other define lines.


// inferred from buddylist.install
define('GROUPNAME_MAX_LENGTH', 255);

/**
 * Validate buddylist_form_edit_groups_add submissions.
 * Based on user_validate_name() and profile_field_form_validate().
 */
function buddylist_form_edit_groups_add_validate($form_id, $form_values) {
  global $user;
  
  if (!strlen($form_values['group_name']))
    form_set_error('group_name', t('You must enter a group name.'));
  if (substr($form_values['group_name'], 0, 1) == ' ')
    form_set_error('group_name', t('The group name cannot begin with a space.'));
  if (substr($form_values['group_name'], -1) == ' ')
    form_set_error('group_name', t('The group name cannot end with a space.'));
  if (strpos($form_values['group_name'], '  ') !== FALSE)
    form_set_error('group_name', t('The group name cannot contain multiple spaces in a row.'));
  if (ereg("[^\x80-\xF7 [:alnum:]@_.-]", $form_values['group_name']))
    form_set_error('group_name', t('The group name contains an illegal character.'));
  if (preg_match('/[\x{80}-\x{A0}'.          // Non-printable ISO-8859-1 + NBSP
  '\x{AD}'.                 // Soft-hyphen
  '\x{2000}-\x{200F}'.      // Various space characters
  '\x{2028}-\x{202F}'.      // Bidirectional text overrides
  '\x{205F}-\x{206F}'.      // Various text hinting characters
  '\x{FEFF}'.               // Byte order mark
  '\x{FF01}-\x{FF60}'.      // Full-width latin
  '\x{FFF9}-\x{FFFD}'.      // Replacement characters
  '\x{0}]/u',               // NULL byte
  $form_values['group_name'])) {
    form_set_error('group_name', t('The group name contains an illegal character.'));
  }
  if (strlen($form_values['group_name']) > GROUPNAME_MAX_LENGTH)
    form_set_error('group_name', t('The group name %name is too long: it must be %max characters or less.', array('%name' => $form_values['group_name'], '%max' => GROUPNAME_MAX_LENGTH)));
  if (db_result(db_query("SELECT label FROM {buddylist_groups} WHERE label = '%s' AND uid = '%d'", $form_values['group_name'], $user->uid)))
    form_set_error('group_name', t('The specified group name is already in use.'));
}