You are here

function homebox_admin_page_submit in Homebox 6

Same name and namespace in other branches
  1. 6.3 homebox.admin.inc \homebox_admin_page_submit()
  2. 6.2 homebox.admin.inc \homebox_admin_page_submit()
  3. 7.3 homebox.admin.inc \homebox_admin_page_submit()
  4. 7.2 homebox.admin.inc \homebox_admin_page_submit()
1 string reference to 'homebox_admin_page_submit'
homebox_admin_page in ./homebox.admin.inc
@file Home box admin file, takes care admin interface for homebox

File

./homebox.admin.inc, line 133
Home box admin file, takes care admin interface for homebox

Code

function homebox_admin_page_submit($form, &$form_state) {
  if ($form_state['values']['op'] == t('Save page')) {

    // We need the old homebox name to update permission name
    $old_name = db_result(db_query("SELECT name FROM {homebox_pages} WHERE pid = %d", $form_state['values']['pid']));
    $filter = '![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s';
    $old_name = preg_replace($filter, '-', drupal_strtolower($old_name));
    db_query("UPDATE {homebox_pages} SET name = '%s' WHERE pid = %d", $form_state['values']['name'], $form_state['values']['pid']);
    $res = db_query('SELECT rid, perm FROM {permission}');
    $perms = array();
    while ($p = db_fetch_object($res)) {
      $perm = $p->perm;
      foreach (homebox_pages() as $page) {
        $filter = '![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s';
        $safe_name = preg_replace($filter, '-', drupal_strtolower($form_state['values']['name']));
        $perm = preg_replace('/access homebox ' . $old_name . '/', 'access homebox ' . $safe_name, $perm);
      }
      $perms[$p->rid] = $perm;
    }
    foreach ($perms as $rid => $permission) {
      db_query("UPDATE {permission} SET perm = '%s' WHERE rid = %d", $permission, $rid);
    }
    if ((user_access('create url aliases') || user_access('administer url aliases')) && module_exists('path')) {
      $path = trim($form_state['values']['path']);
      $path = $path == '' ? NULL : $path;
      $pid = db_result(db_query("SELECT pid FROM {url_alias} WHERE src = 'homebox/%d'", $form_state['values']['pid']));
      path_set_alias('homebox/' . $form_state['values']['pid'], isset($path) ? $path : NULL, isset($pid) ? $pid : NULL);
    }
    drupal_set_message(t('Changes have been saved.'));
  }
  else {
    if ($form_state['values']['op'] == t('Delete page')) {
      db_query('DELETE FROM {homebox_pages} WHERE pid = %d', $form_state['values']['pid']);
      db_query('DELETE FROM {homebox_default} WHERE pid = %d', $form_state['values']['pid']);

      // Update the users who have this role set:
      db_query('DELETE FROM {homebox_users} WHERE pid = %d', $form_state['values']['pid']);

      // Delete variables
      variable_del('homebox_column_count_' . $form_state['values']['pid']);
      variable_del('homebox_users_use_colors_' . $form_state['values']['pid']);

      // Colors
      for ($i = 0; $i < HOMEBOX_NUMBER_OF_COLOURS; $i++) {
        variable_del('homebox_color_' . $form_state['values']['pid'] . '_' . $i);
      }
      if (module_exists('path')) {
        $path = 'homebox/' . $form_state['values']['pid'];
        if (drupal_get_path_alias($path) != $path) {
          path_set_alias($path);
        }
      }
      drupal_set_message(t('The page has been deleted.'));
    }
    else {
      if ($form_state['values']['op'] == t('Add page')) {
        db_query("INSERT INTO {homebox_pages} (name) VALUES ('%s')", $form_state['values']['name']);
        drupal_set_message(t('The page has been added.'));
      }
    }
  }
  $form_state['redirect'] = 'admin/build/homebox';
  menu_rebuild();
  return;
}