You are here

function protected_node_load in Protected Node 7

Same name and namespace in other branches
  1. 6 protected_node.module \protected_node_load()
  2. 1.0.x protected_node.module \protected_node_load()

Load the node extension fields.

@param[in] object $node The node to complement with the protected node parameters.

Return value

array An array with the node extended fields or FALSE.

1 call to protected_node_load()
protected_node_node_load in ./protected_node.module
Implements hook_node_load().

File

./protected_node.module, line 1016
Protected Node module.

Code

function protected_node_load($nodes) {
  foreach ($nodes as &$node) {

    // Valid input parameters?
    if (!is_object($node) || !is_numeric($node->nid)) {
      return FALSE;
    }

    // Default fields for protected nodes.
    static $default_fields = array(
      'protected_node_is_protected' => 0,
      'protected_node_passwd' => '',
      'protected_node_passwd_changed' => 0,
      'protected_node_show_title' => 0,
      'protected_node_emails' => '',
      'protected_node_hint' => '',
    );

    // Can the node be protected at all?
    $protection = variable_get('protected_node_protection_' . $node->type, PROTECTED_NODE_PROTECTION_PROTECTABLE);
    if ($protection == PROTECTED_NODE_PROTECTION_NEVER) {

      // By default the node is not protected, return that.
      return $default_fields;
    }
    $result = db_select('protected_nodes')
      ->fields('protected_nodes', array(
      'protected_node_is_protected',
      'protected_node_passwd',
      'protected_node_passwd_changed',
      'protected_node_show_title',
      'protected_node_emails',
      'protected_node_hint',
    ))
      ->condition('nid', $node->nid)
      ->execute()
      ->fetchAssoc();
    if (!is_array($result)) {

      // The SELECT failed, use the defaults.
      $result = $default_fields;
    }
    else {

      // Define any missing field.
      $result += $default_fields;
    }

    // The password is a CHAR(40) and when empty it's all spaces
    // (this is possible when the global password is used).
    $result['protected_node_passwd'] = trim($result['protected_node_passwd']);

    // If the user changed the mode to "always protected" then we force that
    // here it means the node may not be accessible to people without
    // administration privileges since it may not have a default password.
    if ($protection == PROTECTED_NODE_PROTECTION_ALWAYS) {
      $result['protected_node_is_protected'] = TRUE;
    }
    foreach ($result as $property => &$value) {
      $node->{$property} = $value;
    }
  }
}