You are here

function print_ui_link_allowed in Printer, email and PDF versions 7.2

Check if the link to the PF version is allowed depending on the settings.

Parameters

array $link: Array returned by the hook_print_link() call.

array $args: Array containing the possible parameters: view_mode, node, type, path.

Return value

int FALSE if not allowed PRINT_UI_ALLOW_NORMAL_LINK if a normal link is allowed PRINT_UI_ALLOW_BOOK_LINK if a link is allowed in a book node

2 calls to print_ui_link_allowed()
print_ui_insert_link in print_ui/print_ui.module
Auxiliary function to display a formatted Printer-friendly link.
print_ui_node_view in print_ui/print_ui.module
Implements hook_node_view().

File

print_ui/print_ui.module, line 663
Printer-friendly pages User Interface module.

Code

function print_ui_link_allowed($link, $args) {
  if (isset($args['view_mode'])) {
    $view_mode = $args['view_mode'];
    if ($view_mode == 'teaser' && !variable_get('print_' . $link['format'] . '_link_teaser', PRINT_UI_LINK_TEASER_DEFAULT) || !in_array($view_mode, array(
      'full',
      'teaser',
    ))) {

      // If the teaser link is disabled.
      return FALSE;
    }
  }
  $link_allowed_func = $link['module'] . '_link_allowed';
  if (function_exists($link_allowed_func)) {
    if (!$link_allowed_func($args)) {

      // If the format-specific function disallows the link.
      return FALSE;
    }
  }
  if (!empty($args['path'])) {
    $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
    if (ctype_digit($nid)) {
      $args['node'] = node_load($nid);
    }
  }
  if (!empty($args['node'])) {
    static $node_type = '';
    $node = $args['node'];
    if (isset($node->type)) {
      $node_type = $node->type;
    }

    // Node.
    if (isset($args['type']) && $args['type'] == 'comment' && isset($node_type)) {

      // Link is for a comment, return the configured setting
      // Cache this statically to avoid duplicate queries for every comment.
      static $res = array();
      if (!isset($res[$link['format']][$node->nid])) {
        $res[$link['format']][$node->nid] = db_query("SELECT comments FROM {" . $link['module'] . "_node_conf} WHERE nid = :nid", array(
          ':nid' => $node->nid,
        ))
          ->fetchField();
      }
      $display_comment = $res && $res[$link['format']][$node->nid] !== FALSE ? $res[$link['format']][$node->nid] : variable_get('print_' . $link['format'] . '_display_comment_' . $node_type, PRINT_UI_TYPE_COMMENT_LINK_DEFAULT);
      if ($display_comment) {
        return PRINT_UI_ALLOW_NORMAL_LINK;
      }
    }
    else {

      // Node link.
      $display = 'print_' . $link['format'] . '_display';
      if (isset($node->{$display}) && !$node->{$display}) {

        // Link for this node is disabled.
        return FALSE;
      }
      elseif (isset($node->book)) {

        // Node is a book.
        $book_link = variable_get('print_' . $link['format'] . '_book_link', PRINT_UI_BOOK_LINK_DEFAULT);
        switch ($book_link) {
          case 1:
            if (user_access('access printer-friendly version')) {
              return PRINT_UI_ALLOW_BOOK_LINK;
            }
            break;
          case 2:
            return PRINT_UI_ALLOW_NORMAL_LINK;
        }
      }
      else {
        return PRINT_UI_ALLOW_NORMAL_LINK;
      }
    }
  }
  else {

    // 'System' page.
    $sys_link_visibility = variable_get('print_' . $link['format'] . '_sys_link_visibility', PRINT_UI_SYS_LINK_VISIBILITY_DEFAULT);
    $sys_link_pages = variable_get('print_' . $link['format'] . '_sys_link_pages', PRINT_UI_SYS_LINK_PAGES_DEFAULT);
    return _print_ui_page_match($sys_link_visibility, $_GET['q'], $sys_link_pages);
  }
  return FALSE;
}