You are here

function field_patterns_validate in Patterns 7.2

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

File

patterns_components/components/field.inc, line 188
Patterns component for Fields.

Code

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

  /*
   * Syntactic validation
   *
   */
  switch ($tag) {
    case 'field':
      switch ($action) {
        case PATTERNS_CREATE:
          $msg .= t('Field could not be created without identify a instance.<br>');
          return patterns_results(PATTERNS_ERR, $msg);
          break;
        case PATTERNS_MODIFY:

          //Check mandatory fields.
          $mandatory_attributes = array(
            'field_name',
            'entity_type',
            'bundle',
          );
          if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
            return patterns_results(PATTERNS_ERR, $msg);
          }

          //In this case we will need to define as well the attributes generated by the hook_prepare: uid and pass
          $interpretable_attributes = array(
            'field_name',
            'entity_type',
            'bundle',
            'settings',
            'field',
          );
          if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
            $status = PATTERNS_WARN;
          }
          break;
        case PATTERNS_DELETE:

          //Check mandatory fields, in this case is only one.
          $msg .= t('Field could not be deleted independently outside a indtance.<br>');
          return patterns_results(PATTERNS_ERR, $msg);
          break;
      }
      break;
    case 'instance':
      switch ($action) {
        case PATTERNS_CREATE:

          //Check mandatory fields.
          $mandatory_attributes = array(
            'field_name',
            'entity_type',
            'bundle',
            'widget',
          );
          if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
            return patterns_results(PATTERNS_ERR, $msg);
          }

          //In this case we will need to define as well the attributes generated by the hook_prepare: uid and pass
          $interpretable_attributes = array(
            'field_name',
            'entity_type',
            'bundle',
            'label',
            'description',
            'settings',
            'required',
            'widget',
            'display',
            'fields',
          );
          if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
            $status = PATTERNS_WARN;
          }
          break;
        case PATTERNS_MODIFY:

          //Check mandatory fields
          $mandatory_attributes = array(
            'field_name',
            'entity_type',
            'bundle',
          );
          if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
            return patterns_results(PATTERNS_ERR, $msg);
          }
          $interpretable_attributes = array(
            'field_name',
            'entity_type',
            'bundle',
            'instance',
            'field',
            'fields',
            'settings',
            'required',
            'widget',
            'display',
            $data['field_name'],
          );
          if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
            $status = PATTERNS_WARN;
          }
          break;
        case PATTERNS_DELETE:

          //Check mandatory fields
          $mandatory_attributes = array(
            'field_name',
            'entity_type',
            'bundle',
            'confirm',
            'op',
          );
          if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
            return patterns_results(PATTERNS_ERR, $msg);
          }

          //In this case there are not optional attributes, so we can also use this set as interpretable attributes
          if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
            $status = PATTERNS_WARN;
          }
          break;
      }
      break;
  }

  /*
   * Semantic validation
   */
  $exist = field_read_instance($data['entity_type'], $data['field_name'], $data['bundle']);

  // TODO: count()
  switch ($tag) {
    case 'field':
      switch ($action) {
        case PATTERNS_MODIFY:

          //Create semantic warning if the rid does not exist
          if (!$exist) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The field instance %field_name with entity type %entity_type in bundle %bundle dose not exist in the system.', array(
                '%field_name' => $data['field_name'],
                '%entity_type' => $data['entity_type'],
                '%bundle' => $data['bundle'],
              )),
            );
          }
          break;
          break;
      }
      break;
    case 'instance':
      switch ($action) {
        case PATTERNS_CREATE:

          //Create semantic warning if the role name has already been defined
          if ($exist) {
            $result[] = array(
              PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The field instance %field_name with entity type %entity_type in bundle %bundle already exists in the system.', array(
                '%field_name' => $data['field_name'],
                '%entity_type' => $data['entity_type'],
                '%bundle' => $data['bundle'],
              )),
            );
          }
          break;
        case PATTERNS_MODIFY:

          //Create semantic warning if the rid does not exist
          if (!$exist) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The field instance %field_name with entity type %entity_type in bundle %bundle dose not exist in the system.', array(
                '%field_name' => $data['field_name'],
                '%entity_type' => $data['entity_type'],
                '%bundle' => $data['bundle'],
              )),
            );
          }
          break;
        case PATTERNS_DELETE:
          if (!$exist) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The field instance %field_name with entity type %entity_type in bundle %bundle dose not exist in the system.', array(
                '%field_name' => $data['field_name'],
                '%entity_type' => $data['entity_type'],
                '%bundle' => $data['bundle'],
              )),
            );
          }
          break;
      }
      break;
  }
  return patterns_results($status, $msg, $result);

  /* $status = PATTERNS_SUCCESS;
    $msg = '';
    if ($tag == 'field_weight') {
      if (!isset($data['entity_type'])) {
        $status = PATTERNS_ERR;
        $msg = t('FieldException: Field entity type is required.');
      }
      elseif (!isset($data['bundle'])) {
        $status = PATTERNS_ERR;
        $msg = t('FieldException: Field bundle is required.');
      }
    }
    if ($tag == 'field') {
      if (!isset($data['field_name'])) {
        $status = PATTERNS_ERR;
        $msg = t('FieldException: Field name is required.');
      }
    }
    if ($tag == 'instance') {
      $exist = db_query("SELECT COUNT(*) FROM {field_config_instance} WHERE field_name = :name and entity_type = :type and bundle = :bundle", array('bundle' => $data['bundle'], 'type' => $data['entity_type'], 'name' => $data['field_name']))->fetchField(); // TODO: count()
      if (!isset($data['field_name'])) {
        $status = PATTERNS_ERR;
        $msg = t('FieldException: Field name is required.');
      }
      elseif (!isset($data['entity_type'])) {
        $status = PATTERNS_ERR;
        $msg = t('FieldException: Field entity type is required.');
      }
      elseif (!isset($data['bundle'])) {
        $status = PATTERNS_ERR;
        $msg = t('FieldException: Field bundle is required.');
      }
      elseif ($action == PATTERNS_CREATE && $exist) {
        $status = PATTERNS_ERR;
        $msg = t('Add new field: the field name %name already exists.', array('%name' => $data['field_name']));
      }
      elseif ($action == PATTERNS_MODIFY && !$exist) {
        $status = PATTERNS_ERR;
        $msg = t('FieldException: Attempt to update an instance of a nonexistent field.');
      }
      elseif ($action == PATTERNS_DELETE && !$exist) {
        $status = PATTERNS_ERR;
        $msg = t('FieldException: Attempt to delete an instance of a nonexistent field');
      }
    }

    return patterns_results($status, $msg);*/
}