You are here

node_class.module in Node Class 6.2

Same filename and directory in other branches
  1. 8 node_class.module
  2. 6 node_class.module
  3. 7 node_class.module

File

node_class.module
View source
<?php

/*
 * Implementation of hook_perm()
 */
function node_class_perm() {
  return array(
    'administer nodes',
  );
}

/*
 * Getting css class information for a given node
 */
function node_class_attributes($node) {
  $ret = db_fetch_object(db_query('SELECT nid, css_class FROM {node_class} WHERE nid = "%s"', $node->nid));
  if ($ret !== FALSE) {
    return $ret;
  }
}

/*
 * Implementation of hook_form_alter()
 */
function node_class_form_alter(&$form, &$form_state, $form_id) {

  // Check form type && if user_access is ok
  if (isset($form['#node']) && $form_id == $form['#node']->type . '_node_form' && user_access('administer nodes')) {
    $form['node_class'] = array(
      '#type' => 'fieldset',
      '#title' => t('Node Class settings'),
      '#collapsible' => TRUE,
      '#weight' => -1,
      '#tree' => TRUE,
    );

    // Getting css attributes information for nodes that already exist.
    if (isset($form['#node']->nid)) {
      $attributes = node_class_attributes($form['#node']);
    }

    // Check CSS exists in DB to prepare SQL update
    $old_classes = NULL;

    // added check up front to prevent object errors. no need for null object
    if ($attributes && $attributes->nid) {
      $old_classes = $attributes->css_class;
      $form['node_class']['existing_css'] = array(
        '#type' => 'hidden',
        '#value' => '1',
      );
    }
    $form['node_class']['css_class'] = array(
      '#type' => 'textfield',
      '#title' => t('CSS class(es)'),
      '#default_value' => $old_classes,
      '#description' => t('Separate classes with a space. IMPORTANT: You must add &lt;?php print node_class($node); ?&gt; to your theme\'s node.tpl.php file to make the classes appear.'),
    );

    // Adding submit handler for node_class module
    $form['#submit'][] = 'node_class_form_submit';
  }
}

/**
 * Implementation of hook_nodeapi
 */
function node_class_nodeapi(&$node, $op, $a3 = NUL, $a4 = NULL) {
  switch ($op) {
    case 'delete':

      // Deleting class information for the node
      db_query("DELETE FROM {node_class} WHERE nid='%s'", $node->nid);
      break;
    case 'insert':

      // Inserting class information for the node when a class has been entered.
      if (strlen($node->node_class['css_class']) >= 1) {
        db_query("INSERT INTO {node_class} (nid, css_class) VALUES('%s','%s')", $node->nid, $node->node_class['css_class']);
      }
      break;
  }
}

/**
 * Submit handler for node form submit
 */
function node_class_form_submit($form, &$form_state) {
  if ($form_state['values']['form_id'] == $form['#node']->type . '_node_form') {
    $nid = $form_state['values']['nid'];

    // need nid for all below.
    if (isset($form_state['values']['node_class']['css_class']) && user_access('administer nodes') && !is_null($nid)) {
      $class = check_plain($form_state['values']['node_class']['css_class']);

      // existing node, had classes already
      if (isset($form_state['values']['node_class']['existing_css'])) {

        // is there still text in the box?
        if (strlen($form_state['values']['node_class']['css_class']) >= 1) {
          db_query("UPDATE {node_class} SET css_class='%s' WHERE nid='%s'", $class, $nid);
        }
        else {

          // they emptied the texbox, delete record from db.
          db_query("DELETE FROM {node_class} WHERE nid='%s'", $nid);
        }
      }
      else {

        // this is a prexisting node, just now adding node classes.
        if (strlen($form_state['values']['node_class']['css_class']) >= 1) {
          db_query("INSERT INTO {node_class} (nid, css_class) VALUES('%s','%s')", $nid, $class);
        }
      }
    }
  }
}

/*
* Accessor for css_class information
*/
function node_class($node) {
  $attributes = node_class_attributes($node);
  return $attributes->css_class;
}

Functions

Namesort descending Description
node_class
node_class_attributes
node_class_form_alter
node_class_form_submit Submit handler for node form submit
node_class_nodeapi Implementation of hook_nodeapi
node_class_perm