You are here

function cpn_nodeapi in Code per Node 6

Implementation of hook_nodeapi().

1 call to cpn_nodeapi()
cpn_ctools_render_alter in ./cpn.module
Implements hook_ctools_render_alter().

File

./cpn.module, line 246
Primary hook implementations.

Code

function cpn_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  static $previous_op = NULL;
  switch ($op) {

    // Validating: ensure no "style" or "script" tags are included.
    case 'validate':
      if (isset($node->cpn['css']) and cpn_validate($node->cpn['css'], 'css')) {
        form_set_error('cpn][css', t('Do not include @style tags in the CSS.', array(
          '@style' => '<style>',
        )));
      }
      if (isset($node->cpn['js']) and cpn_validate($node->cpn['js'], 'js')) {
        form_set_error('cpn][js', t('Do not include @script tags in the JavaScript.', array(
          '@script' => '<script>',
        )));
      }
      break;

    // Updating: delete from DB and file system, and intentionally fallthrough
    // to "insert" op below.
    case 'update':
      if (isset($node->cpn)) {
        db_query("DELETE FROM {cpn} WHERE nid = %d", $node->nid);
        cpn_delete_file($node->nid . '.css');
        cpn_delete_file($node->nid . '.js');
      }

    // Inserting: save in DB and file system.
    case 'insert':
      if (isset($node->cpn) and drupal_strlen(trim($node->cpn['css'] . $node->cpn['js']))) {
        db_query("INSERT INTO {cpn} VALUES (%d, '%s', '%s')", $node->nid, $node->cpn['css'], $node->cpn['js']);

        // Add the global wrapper code.
        foreach (array(
          'css',
          'js',
        ) as $type) {
          $output = cpn_wrap_output($node->cpn[$type], 'node', $type);
          if (!empty($output)) {
            cpn_save_file($output, $node->nid . '.' . $type);
          }
        }
      }
      break;

    // Deleting: delete from DB and file system.
    case 'delete':
      db_query("DELETE FROM {cpn} WHERE nid = %d", $node->nid);
      cpn_delete_file($node->nid . '.css');
      cpn_delete_file($node->nid . '.js');
      break;

    // Loading: add "cpn" variable to the node object.
    case 'load':
      return array(
        'cpn' => db_fetch_array(db_query("SELECT css, js FROM {cpn} WHERE nid = %d", $node->nid)),
      );
      break;

    // Viewing & Previewing.
    case 'view':

      // Attach the content type CSS/JS.
      $css = variable_get('cpn_css_' . $node->type, '');
      if (!empty($css)) {
        $css = file_create_path(variable_get('cpn_path', 'cpn') . '/' . $node->type . '.css');
        if (!empty($css)) {
          drupal_add_css($css, 'theme', NULL, variable_get('cpn_aggregation_css', TRUE));
        }
      }
      $js = variable_get('cpn_js_' . $node->type, '');
      if (!empty($js)) {
        $js = file_create_path(variable_get('cpn_path', 'cpn') . '/' . $node->type . '.js');
        if (!empty($js)) {
          drupal_add_js($js, 'theme', 'header', TRUE, variable_get('cpn_aggregation_js', TRUE));
        }
      }

      // Previewing: add CSS and/or JS to the page, inline.
      if ($previous_op == 'validate') {
        if (drupal_strlen(trim($node->cpn['css']))) {

          // NOTE: This is injected at the top of the page, before all <link> tags.
          // It should be injected after the <link> tags.
          // Possible in D7: http://drupal.org/node/259368
          // Not easily possible in D6: http://drupal.org/node/143209
          drupal_set_html_head('<style type="text/css" media="all"> ' . $node->cpn['css'] . ' </style>');
        }
        if (drupal_strlen(trim($node->cpn['js']))) {
          drupal_add_js($node->cpn['js'], 'inline');
        }
      }
      else {
        if (!empty($node->cpn['css']) && drupal_strlen(trim($node->cpn['css']))) {
          $css = file_create_path(variable_get('cpn_path', 'cpn') . '/' . $node->nid . '.css');
          if (!empty($css)) {
            drupal_add_css($css, 'theme', NULL, variable_get('cpn_aggregation_css', TRUE));
          }
        }
        if (!empty($node->cpn['js']) && drupal_strlen(trim($node->cpn['js']))) {
          $js = file_create_path(variable_get('cpn_path', 'cpn') . '/' . $node->nid . '.js');
          if (!empty($js)) {
            drupal_add_js($js, 'theme', 'header', TRUE, variable_get('cpn_aggregation_js', TRUE));
          }
        }
      }
      break;
  }
  $previous_op = $op;
}