You are here

function toolbar_patterns_validate in Patterns 7.2

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

hook_patterns_validate()

File

patterns_components/components/toolbar.inc, line 103

Code

function toolbar_patterns_validate($action, $tag, &$data) {
  $result = array();
  $status = PATTERNS_SUCCESS;
  $msg = '';
  switch ($tag) {
    case 'toolbarrole':

      //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, $result);
      }

      //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, $result);
        }
      }

      //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, $result);
          }
          if ($rid == FALSE) {
            $result[] = array(
              PATTERNS_WARNING_UNMET_DEPENDENCY => t('The role %role does not exist in the system.', array(
                '%role' => $value,
              )),
            );
          }
          if (isset($data['status'][$rid])) {
            if (in_array("access toolbar", $data['status'][$rid])) {
              $result[] = array(
                PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The role %role already have toolbar permission in the system', array(
                  '%role' => $value,
                )),
              );
            }
          }

          //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, $result);
          }
          if ($rid == FALSE) {
            $result[] = array(
              PATTERNS_WARNING_UNMET_DEPENDENCY => t('The role %role does not exist in the system.', array(
                '%role' => $value,
              )),
            );
          }

          //ensure the role have some perms.
          if (isset($data['status'][$rid])) {
            if (!in_array("access toolbar", $data['status'][$rid])) {
              $result[] = array(
                PATTERNS_WARNING_UNMET_DEPENDENCY => t('The role %role does not have toolbar permission yet in the system.', array(
                  '%role' => $value,
                )),
              );
            }

            //delete the "access toolbar" from $data['status']
            $data['status'][$rid] = array_diff($data['status'][$rid], array(
              "access toolbar",
            ));
          }
          else {
            $result[] = array(
              PATTERNS_WARNING_UNMET_DEPENDENCY => t('The role %role does not have any permission yet in the system.', array(
                '%role' => $value,
              )),
            );
          }
        }
      }

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