You are here

function user_patterns_build in Patterns 7

Same name and namespace in other branches
  1. 7.2 patterns_components/components/user.inc \user_patterns_build()

File

patterns_components/components/user.inc, line 541

Code

function user_patterns_build($action, $form_id, &$data = NULL, &$a) {
  $status = PATTERNS_SUCCESS;
  $msg = '';
  $result = NULL;
  if ($form_id == 'user_register_form') {
    if (isset($data['uid'])) {
      unset($data['uid']);
    }

    // Ste: added
    //        if (!isset($data['user'])) {
    //          $data['user'] = (object) $data;
    //        }
  }
  elseif ($form_id == 'user_profile_form') {

    // Make sure that required fields are not left empty.
    if (empty($data['mail'])) {
      $data['mail'] = db_query("SELECT mail FROM {users} WHERE uid = :uid", array(
        'uid' => $data['uid'],
      ))
        ->fetchField();
    }
    if (empty($data['name'])) {
      $data['name'] = db_query("SELECT name FROM {users} WHERE uid = :uid", array(
        'uid' => $data['uid'],
      ))
        ->fetchField();
    }
    static $old_q;

    // TODO: What is this?
    $old_q = $_GET['q'];
    $_GET['q'] = 'user/' . $data['uid'] . '/edit';
  }
  elseif ($id == 'user_cancel_confirm_form') {
    $data['confirm'] = 1;
    switch ($data['method']) {
      case '1':
        $data['user_cancel_method'] = 'user_cancel_block';
        break;
      case '2':
        $data['user_cancel_method'] = 'user_cancel_block_unpublish';
        break;
      case '3':
        $data['user_cancel_method'] = 'user_cancel_reassign';
        break;
      case '4':
        $data['user_cancel_method'] = 'user_cancel_delete';
        break;
    }
  }
  elseif ($form_id == 'user_admin_roles' || $form_id == 'user_admin_role_delete_confirm' || $form_id == 'user_admin_role') {
    if ($action === PATTERNS_DELETE) {
      $data['op'] = t('Delete');
    }
    elseif ($data['rid']) {
      $data['op'] = t('Save role');
    }
    else {
      $data['op'] = t('Add role');
    }
  }
  elseif ($form_id == 'user_admin_permissions') {

    // Get list of permissions to ensure only available permissions are saved.
    $permissions = array_keys(user_permission_get_modules());
    if (is_string($data['value'])) {
      $p = explode(',', $data['value']);
      unset($data['value']);
      foreach ($p as $key => $value) {
        $val = trim(trim($value), '\'"');
        if (in_array($val, $permissions)) {

          //if ($action === PATTERNS_DELETE) {

          //$perms[$val] = NULL;

          //}

          //else {
          $perms[$val] = $val;

          //}
        }
      }
      $data[$data['rid']] = $perms;
    }
    else {
      for ($i = 0; $item = $data[$i]; $i++) {
        if (in_array($item, $permissions)) {
          if ($action === PATTERNS_DELETE) {
            $perms[$item] = 0;
          }
          else {
            $perms[$item] = $item;
          }
        }
        unset($data[$i]);
      }
      $data[$data['rid']] = $perms;
    }
    module_load_include('inc', 'user', 'user.admin');

    // TODO: Isn't this already loaded?
    if (is_numeric($data['rid'])) {
      $rid = $data['rid'];
    }
    elseif (is_string($data['rid'])) {
      $rid = db_query('SELECT rid FROM {role} WHERE name = :name', array(
        'name' => $data['rid'],
      ))
        ->fetchField();
      $role = $data['rid'];
      $data[$rid] = $data[$role];
      $data['rid'] = $rid;
      unset($data[$role]);
    }
    $data['op'] = t('Save permissions');
  }

  // TODO: check if these validations work

  /*
   * if ($id == 'user_admin_permissions') {
   *   if (is_numeric($data['rid'])) {
   *     $rid = $data['rid'];
   *   }
   *   elseif (is_string($data['rid'])) {
   *     $rid = db_query('SELECT rid FROM {role} WHERE name = :name', array('name' => $data['rid']))->fetchField();
   *   }
   *
   *   if (!db_query("SELECT COUNT(*) FROM {role} WHERE rid = :rid", array('rid' => $rid))->fetchField()) {
   *     return t('Invalid role %role to set permissions for.', array('%role' => $data['role'] ? $data['role'] : $data['rid']));
   *   }
   * }
   */
  $result = $data;
  return patterns_results($status, $msg, $result);
}