You are here

function node_patterns_validate in Patterns 7.2

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

File

patterns_components/components/node.inc, line 133

Code

function node_patterns_validate($action, $tag, &$data) {
  $status = PATTERNS_SUCCESS;
  $msg = '';
  $result = array();
  switch ($tag) {
    case 'node':
      switch ($action) {
        case PATTERNS_CREATE:
          if (empty($data['type'])) {
            $status = PATTERNS_ERR;
            $msg = t('"type" field is required.');
            return patterns_results($status, $msg, $result);
          }
          if (empty($data['title'])) {
            $status = PATTERNS_ERR;
            $msg = t('"title" field is required.');
            return patterns_results($status, $msg, $result);
          }
          $node_item = db_select('node_type', 'nt')
            ->fields('nt', array(
            'name',
          ))
            ->condition('type', $data['type'])
            ->execute()
            ->fetchField();
          if (!$node_item) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The content type: "%type" does not exist in the system.', array(
                '%type' => $data['type'],
              )),
            );
          }
          break;

        //check nid first, then title second, one of them is enough, but because of title is not unique, so nid is perfered.

        //if nid is existed, the title will not be check.
        case PATTERNS_DELETE:
        case PATTERNS_MODIFY:

          //nid and title both are empty, error.
          if (empty($data['nid']) && empty($data['title'])) {
            $status = PATTERNS_ERR;
            $msg = t('"id or title" field is required.');
            return patterns_results($status, $msg, $result);
          }
          elseif (!empty($data['nid'])) {
            $data['node'] = node_load($data['nid']);
            if ($data['node'] === FALSE) {
              $result[] = array(
                PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The node: "%oldname" does not exist in the system.', array(
                  '%oldname' => $data['nid'],
                )),
              );
            }
            else {
              $data['node'] = (object) $data['node'];
              if ($action == PATTERNS_MODIFY) {
                if ($data['type'] != $data['node']->type) {
                  $status = PATTERNS_ERR;
                  $msg = t("You can't change content type for already existing node.");
                  return patterns_results($status, $msg, $result);
                }
              }
            }
          }
          else {
            $num_nodeswithSameTitle = db_select('node')
              ->condition('title', $data['title'])
              ->countQuery()
              ->execute()
              ->fetchField();
            if ($num_nodeswithSameTitle > 1) {
              $result[] = array(
                PATTERNS_WARNING_NOT_UNIQUE_ALIAS => t('The node titled: "%title" is not unique in the system.', array(
                  '%title' => $data['title'],
                )),
              );
            }
            elseif ($num_nodeswithSameTitle == 1) {
              $node_item = db_select('node', 'nd')
                ->fields('nd', array(
                'nid',
              ))
                ->condition('title', $data['title'])
                ->execute()
                ->fetchField();
              $data['node'] = node_load($node_item);
              $data['nid'] = $node_item;
              if ($data['node'] === FALSE) {
                $result[] = array(
                  PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The node: "%oldname" does not exist in the system.', array(
                    '%oldname' => $data['nid'],
                  )),
                );
              }
              else {
                if ($action == PATTERNS_MODIFY) {
                  $data['node'] = (object) $data['node'];
                  if ($data['type'] != $data['node']->type) {
                    $status = PATTERNS_ERR;
                    $msg = t("You can't change content type for already existing node.");
                    return patterns_results($status, $msg, $result);
                  }
                }
              }
            }
            else {
              $result[] = array(
                PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The node: "%oldname" does not exist in the system.', array(
                  '%oldname' => $data['title'],
                )),
              );
            }
          }
          break;
      }
      break;
    case 'content_type':

      // three action all need to check 'type'
      if (empty($data['type'])) {
        $status = PATTERNS_ERR;
        $msg = t('"type" field is required.');
        return patterns_results($status, $msg, $result);
      }

      //TODO: check more required fields for content types
      switch ($action) {
        case PATTERNS_CREATE:
          if (empty($data['name'])) {
            $status = PATTERNS_ERR;
            $msg = t('"name" field is required.');
            return patterns_results($status, $msg, $result);
          }
          $set = db_select('node_type', 'nt')
            ->fields('nt', array(
            'type',
          ))
            ->condition('name', $data['name'])
            ->execute()
            ->fetchField();
          if ($set) {
            $result[] = array(
              PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The content_type named "%name" already exists in the system', array(
                '%name' => $data['name'],
              )),
            );
          }
          $set = db_select('node_type', 'nt')
            ->fields('nt', array(
            'name',
          ))
            ->condition('type', $data['type'])
            ->execute()
            ->fetchField();
          if ($set) {
            $result[] = array(
              PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The type: %type already exists in the system', array(
                '%type' => $data['type'],
              )),
            );
          }
          break;
        case PATTERNS_MODIFY:
          $set = db_select('node_type', 'nt')
            ->fields('nt', array(
            'type',
          ))
            ->condition('type', $data['type'])
            ->execute()
            ->fetchField();
          if (!$set) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The content type: "%oldname" does not exist in the system.', array(
                '%oldname' => $data['name'],
              )),
            );
          }
          if (isset($data['name'])) {
            $set = db_select('node_type', 'nt')
              ->fields('nt', array(
              'type',
            ))
              ->condition('name', $data['name'])
              ->execute()
              ->fetchField();
            if ($set) {
              $result[] = array(
                PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The content_type named "%name" already exists in the system', array(
                  '%name' => $data['name'],
                )),
              );
            }
          }
          break;
        case PATTERNS_DELETE:
          $set = db_select('node_type', 'nt')
            ->fields('nt', array(
            'type',
          ))
            ->condition('type', $data['type'])
            ->execute()
            ->fetchField();
          if (!$set) {
            $result[] = array(
              PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The content type: "%oldname" does not exist in the system.', array(
                '%oldname' => $data['name'],
              )),
            );
          }
          break;
      }
      break;
  }
  return patterns_results($status, $msg, $result);
}