You are here

function block_patterns_validate in Patterns 7.2

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

Parameters

string $action Type of action being executed:

string $tag Type of tag to be validated:

array $data Associative array containing the data action processed from the pattern:

Return value

mixed through patterns_results($status, $msg, $result) function. Status of the operation, error messages and semantic warnings through $result

File

patterns_components/components/block.inc, line 190

Code

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

  /*
   * Syntactic validation:
   * - Syntactic warnings: They refer to wrong grammar statements that will not
   * provoke any execution error. Ex.: non-interpretable attributes.
   * - Syntactic errors: They refer to wrong grammar statements that will provoke
   * an execution error. Ex.: misspelling in required attribute.
   *
   */
  switch ($tag) {
    case 'block':
      switch ($action) {
        case PATTERNS_CREATE:

          //Assign mandatory fields depending on the type of block (custom required some extra fields but does not use delta)

          //$mandatory_attributes = array('module', 'delta', 'title');
          $mandatory_attributes = $data['module'] == 'block' ? array(
            'module',
            'title',
            'body',
            'info',
            'format',
          ) : array(
            'module',
            'delta',
            'title',
          );
          if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
            return patterns_results(PATTERNS_ERR, $msg);
          }

          //Define interpretable attributes depending on the type of block (custom required some extra fields but does not use delta)
          $interpretable_attributes = $data['module'] == 'block' ? array(
            'module',
            'title',
            'theme',
            'status',
            'weight',
            'region',
            'custom',
            'visibility',
            'pages',
            'cache',
            'body',
            'info',
            'format',
          ) : array(
            'module',
            'delta',
            'title',
            'theme',
            'status',
            'weight',
            'region',
            'custom',
            'visibility',
            'pages',
            'cache',
          );
          if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
            $status = PATTERNS_WARN;
          }
          break;
        case PATTERNS_MODIFY:

          //Assign mandatory fields depending, custom_block fields are not covered by modify actions
          $mandatory_attributes = array(
            'module',
            'delta',
            'title',
          );
          if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
            return patterns_results(PATTERNS_ERR, $msg);
          }

          //Define interpretable attributes, custom_block fields are not covered by modify actions
          $interpretable_attributes = array(
            'module',
            'delta',
            'title',
            'theme',
            'status',
            'weight',
            'region',
            'custom',
            'visibility',
            'pages',
            'cache',
          );
          if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
            $status = PATTERNS_WARN;
          }
          break;
        case PATTERNS_DELETE:

          //Check mandatory fields
          $mandatory_attributes = array(
            'module',
            'delta',
          );
          if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
            return patterns_results(PATTERNS_ERR, $msg);
          }

          //In this case mandatory and interpretable are the same
          if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
            $status = PATTERNS_WARN;
          }
          break;
      }
      break;
    case 'block_node_type':
      switch ($action) {
        case PATTERNS_CREATE:
        case PATTERNS_DELETE:

          //Check mandatory fields
          $mandatory_attributes = array(
            'module',
            'delta',
            'type',
          );
          if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
            return patterns_results(PATTERNS_ERR, $msg);
          }

          //In this case mandatory and interpretable are the same
          if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
            $status = PATTERNS_WARN;
          }
          break;
        default:
          $msg = t('Action %action is uncompatible for tag %tag.', array(
            '%action' => $action,
            '%tag' => $tag,
          ));
          return patterns_results(PATTERNS_ERR, $msg);
      }
      break;
    case 'block_role':
      switch ($action) {
        case PATTERNS_CREATE:
        case PATTERNS_DELETE:

          //Check mandatory fields
          $mandatory_attributes = array(
            'module',
            'delta',
            'role',
          );
          if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
            return patterns_results(PATTERNS_ERR, $msg);
          }

          //In this case mandatory and interpretable are the same
          if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
            $status = PATTERNS_WARN;
          }
          break;
        default:
          $msg = t('Action %action is uncompatible for tag %tag.', array(
            '%action' => $action,
            '%tag' => $tag,
          ));
          return patterns_results(PATTERNS_ERR, $msg);
      }
      break;
  }

  /*
   * Semantic validation:
   * - Semantic warnings: They refer to the meaning of the pattern itself, and they
   * might provoke execution errors if they are not solved.
   *
   */
  switch ($tag) {
    case 'block':
      switch ($action) {
        case PATTERNS_CREATE:
          if ($data['module'] == 'block') {

            //Check info field is unique in the case of custom blocks
            if (_block_patterns_info_exists($data['info'])) {
              $result[] = array(
                PATTERNS_WARNING_NOT_UNIQUE_ALIAS => t('A block defined by module %module with info field named %info already exists in the system.', array(
                  '%module' => $data['module'],
                  '%info' => $data['info'],
                )),
              );
            }
          }
          else {

            //Create semantic warning if the combination of module+delta already exists (only for not custom)
            if (_block_patterns_module_delta_exists($data['module'], $data['delta'])) {
              $result[] = array(
                PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('A block defined by module %module with delta %delta already exists in the system.', array(
                  '%module' => $data['module'],
                  '%delta' => $data['delta'],
                )),
              );
            }
          }

          //We raised a semantic error if the theme does not exist in any case
          if (isset($data['theme']) && !array_key_exists($data['theme'], list_themes())) {
            $result[] = array(
              PATTERNS_WARNING_UNMET_DEPENDENCY => t('The theme %theme is not currently installed in the system.', array(
                '%theme' => $data['theme'],
              )),
            );
          }
          break;
        case PATTERNS_MODIFY:

          //Create semantic warning if the combination of module+delta+theme does not exist
          if (!_block_patterns_mdt_exists($data['module'], $data['delta'], $data['theme'])) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('There are not any blocks defined by module %module whose delta is %delta for theme %theme in the system.', array(
                '%module' => $data['module'],
                '%theme' => $data['theme'],
                '%delta' => $data['delta'],
              )),
            );
          }
          break;
        case PATTERNS_DELETE:

          //Create semantic warning if the combination of module+delta does not exist
          if (!_block_patterns_module_delta_exists($data['module'], $data['delta'])) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('There are not any blocks defined by module %module whose delta is %delta in the system.', array(
                '%module' => $data['module'],
                '%delta' => $data['delta'],
              )),
            );
          }

          //Create semantic warning if the operation is performed in a block that is not custom.
          if ($data['module'] != 'block') {
            $result[] = array(
              PATTERNS_WARNING_INCONSISTENT_OPERATION => t('Delete operations are not supported for blocks created by the module %module. The execution
                    of this action might create some inconsistencies in the Database. Use MODIFY if you want to disable the block instead.', array(
                '%module' => $data['module'],
              )),
            );
          }
          break;
      }
      break;
    case 'block_node_type':
      switch ($action) {
        case PATTERNS_CREATE:

          //Create semantic warning if the combination of module+delta does not exist
          if (!_block_patterns_module_delta_exists($data['module'], $data['delta'])) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('There are not any blocks defined by module %module whose delta is %delta in the system.', array(
                '%module' => $data['module'],
                '%delta' => $data['delta'],
              )),
            );
          }

          //Create semantic warning if the content type does not exist
          if (!array_key_exists($data['type'], node_type_get_names())) {
            $result[] = array(
              PATTERNS_WARNING_UNMET_DEPENDENCY => t('There are not any content types named %type in the system.', array(
                '%type' => $data['type'],
              )),
            );
          }

          //Create semantic warning if the combination of module+delta+type already exists
          if (_block_patterns_mdtype_exists($data['module'], $data['delta'], $data['type'])) {
            $result[] = array(
              PATTERNS_WARNING_NOT_UNIQUE_ALIAS => t('An entry to restrict the visualization of the block defined by module %module with delta %delta
                    for the Content Type %type has already being set in the system.', array(
                '%module' => $data['module'],
                '%delta' => $data['delta'],
                '%type' => $data['type'],
              )),
            );
          }
          break;
        case PATTERNS_DELETE:

          //Create semantic warning if the combination of module+delta+type does not exist
          if (!_block_patterns_mdtype_exists($data['module'], $data['delta'], $data['type'])) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The restriction for the visualization of the block defined by module %module with delta %delta
                    for the Content Type %type trying to be deleted does not exists in the system.', array(
                '%module' => $data['module'],
                '%delta' => $data['delta'],
                '%type' => $data['type'],
              )),
            );
          }
          break;
      }
      break;
    case 'block_role':
      switch ($action) {
        case PATTERNS_CREATE:

          //Create semantic warning if the combination of module+delta does not exist
          if (!_block_patterns_module_delta_exists($data['module'], $data['delta'])) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('There are not any blocks defined by module %module whose delta is %delta in the system.', array(
                '%module' => $data['module'],
                '%delta' => $data['delta'],
              )),
            );
          }

          //Raise semantic warning if the role name does not exist
          if (!user_role_load_by_name($data['role'])) {
            $result[] = array(
              PATTERNS_WARNING_UNMET_DEPENDENCY => t('The role %role does not exist in the system.', array(
                '%role' => $data['role'],
              )),
            );
          }

          //Create semantic warning if the combination of module+delta+role already exists
          if (_block_patterns_mdtype_role($data['module'], $data['delta'], $data['role'])) {
            $result[] = array(
              PATTERNS_WARNING_NOT_UNIQUE_ALIAS => t('An entry to restrict the visualization of the block defined by module %module with delta %delta
                    for role %role has already being set in the system.', array(
                '%module' => $data['module'],
                '%delta' => $data['delta'],
                '%role' => $data['role'],
              )),
            );
          }
          break;
        case PATTERNS_DELETE:

          //Create semantic warning if the combination of module+delta+role does not exist
          if (!_block_patterns_mdtype_role($data['module'], $data['delta'], $data['role'])) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The restriction for the visualization of the block defined by module %module with delta %delta
                    for the role %role trying to be deleted does not exists in the system.', array(
                '%module' => $data['module'],
                '%delta' => $data['delta'],
                '%role' => $data['role'],
              )),
            );
          }
          break;
      }
      break;
  }
  return patterns_results($status, $msg, $result);
}