You are here

function toolbar_patterns_validate in Patterns 7

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

hook_patterns_validate()

File

patterns_components/components/toolbar.inc, line 59

Code

function toolbar_patterns_validate($action, $tag, &$data) {
  $status = PATTERNS_SUCCESS;
  $msg = '';

  //if both addroles and delroles is not given, there is a error.
  if (!isset($data['addroles']) && !isset($data['delroles'])) {
    $status = PATTERNS_ERR;
    $msg = t('there is nothing to do.');
    return patterns_results($status, $msg);
  }

  //if there is a same name in add and delroles, there is a error.
  if (is_array($data['addroles']) && is_array($data['delroles'])) {
    $a = array_intersect($data['addroles'], $data['delroles']);
    if (count($a) != 0) {
      $status = PATTERNS_ERR;
      $msg = t('same name in both addroles and delroles');
      return patterns_results($status, $msg);
    }
  }

  //the process of addroles.

  //role can not be administrator

  //the name should exist in the role table.

  //if the role already have the perm, there is a error.
  if (is_array($data['addroles'])) {
    foreach ($data['addroles'] as $key => $value) {
      $rid = db_select('role', 'r')
        ->fields('r', array(
        'rid',
      ))
        ->condition('r.name', $value)
        ->execute()
        ->fetchField();
      if ($rid == 3) {
        $status = PATTERNS_ERR;
        $msg = t('administrator should keep static.');
        return patterns_results($status, $msg);
      }
      if ($rid == FALSE) {
        $status = PATTERNS_ERR;
        $msg = t('there is a wrong role name');
        return patterns_results($status, $msg);
      }
      if (isset($data['status'][$rid])) {
        if (in_array("access toolbar", $data['status'][$rid])) {
          $status = PATTERNS_ERR;
          $msg = t('the role have the toolbar permission already.');
          return patterns_results($status, $msg);
        }
      }

      //add the "access toolbar" perm to the $data['status']
      $data['status'][$rid][] = "access toolbar";
    }
  }

  //the process of delroles.

  //role can not be administrator

  //the name should not exist in the role table.

  //if the role don't have the perm yet, there is a error.
  if (is_array($data['delroles'])) {
    foreach ($data['delroles'] as $key => $value) {
      $rid = db_select('role', 'r')
        ->fields('r', array(
        'rid',
      ))
        ->condition('r.name', $value)
        ->execute()
        ->fetchField();
      if ($rid == 3) {
        $status = PATTERNS_ERR;
        $msg = t('administrator should keep static.');
        return patterns_results($status, $msg);
      }
      if ($rid == FALSE) {
        $status = PATTERNS_ERR;
        $msg = t('there is a wrong role name');
        return patterns_results($status, $msg);
      }

      //ensure the role have some perms.
      if (isset($data['status'][$rid])) {
        if (!in_array("access toolbar", $data['status'][$rid])) {
          $status = PATTERNS_ERR;
          $msg = t('the role do not have the toolbar permission yet.');
          return patterns_results($status, $msg);
        }

        //delete the "access toolbar" from $data['status']
        $data['status'][$rid] = array_diff($data['status'][$rid], array(
          "access toolbar",
        ));
      }
      else {
        $status = PATTERNS_ERR;
        $msg = t('the role does not have any permissions.');
        return patterns_results($status, $msg);
      }
    }
  }

  //the $data[$rid] is the form's element.
  foreach ($data['status'] as $rid => $value) {
    $data[$rid] = $value;
  }
  return patterns_results($status, $msg);
}