You are here

dlike.module in Drupal like (Flag counter) 7

Same filename and directory in other branches
  1. 8 dlike.module
  2. 7.3 dlike.module
  3. 7.2 dlike.module

File

dlike.module
View source
<?php

include_once dirname(__FILE__) . '/dlike.inc';

/**
 * Implementation of hook_init().
 */
function dlike_init() {
  drupal_add_css(drupal_get_path('module', 'dlike') . '/dlike.css');
  drupal_add_js(drupal_get_path('module', 'dlike') . '/dlike.js');
}

/**
 * Implementation of hook_views_api().
 */
function dlike_views_api() {
  return array(
    'api' => 2.0,
    'path' => drupal_get_path('module', 'dlike'),
  );
}

/**
 * Implementation of hook_perm().
 */
function dlike_permission() {
  $dlike_perm = array();
  $dlike_perm['dlike access list'] = array(
    'title' => t('Access list of users who flagged a content'),
  );
  return $dlike_perm;
}

/**
 * Implementation of hook_menu().
 */
function dlike_menu() {
  $items['dlike/%/%/%'] = array(
    'page callback' => 'dlike_user_list',
    'page arguments' => array(
      1,
      2,
      3,
    ),
    'access arguments' => array(
      'dlike access list',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['dlike/%/%/%/append'] = array(
    'page callback' => 'dlike_append',
    'page arguments' => array(
      1,
      2,
      3,
    ),
    'access arguments' => array(
      'dlike access list',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implementation of hook_form_form_id_alter().
 */
function dlike_form_flag_form_alter(&$form, $form_state) {
  $form['dlike-' . $form['#flag']->name . '_option'] = array(
    '#type' => 'checkbox',
    '#title' => 'Add Drupal like',
    '#default_value' => variable_get('dlike-' . $form['#flag']->name . '_option', 0),
    '#weight' => '900',
  );
  $form['dlike-' . $form['#flag']->name . '_text_value_singular'] = array(
    '#type' => 'textfield',
    '#title' => t('Add the text you want to display for Singular'),
    '#description' => t('Text to be show when only one person has flagged.<br/>e.g. "(1 like)" or "One person has flagged this" or "See who all have flagged this content"'),
    '#default_value' => variable_get('dlike-' . $form['#flag']->name . '_text_value_singular', t('(1 like)')),
    '#weight' => '901',
  );
  $form['dlike-' . $form['#flag']->name . '_text_value'] = array(
    '#type' => 'textfield',
    '#title' => t('Add the text you want to display'),
    '#description' => t('Use <em>@count</em> token for displaying the number of times content has been flagged.<br>e.g. "(@count likes)" or "@count people have flagged this" or "See who all have flagged this content"'),
    '#default_value' => variable_get('dlike-' . $form['#flag']->name . '_text_value', t('(@count)')),
    '#weight' => '902',
  );
  $form['dlike-modal-window-title-' . $form['#flag']->name] = array(
    '#type' => 'textfield',
    '#title' => t('Title for modal window'),
    '#default_value' => variable_get('dlike-modal-window-title-' . $form['#flag']->name, NULL),
    '#description' => t('e.g. "People who like this"'),
    '#weight' => '903',
  );
  $form['#submit'][] = 'dlike_form_data';
}

/**
 * Submit function to store the field values.
 */
function dlike_form_data(&$form, $form_state) {
  variable_set('dlike-' . $form_state['input']['name'] . '_option', $form_state['input']['dlike-' . $form_state['input']['name'] . '_option']);
  variable_set('dlike-' . $form_state['input']['name'] . '_text_value', $form_state['input']['dlike-' . $form_state['input']['name'] . '_text_value']);
  variable_set('dlike-modal-window-title-' . $form_state['input']['name'], $form_state['input']['dlike-modal-window-title-' . $form_state['input']['name']]);
}

/**
 * Implementation of hook_node_view().
 */
function dlike_node_view($node, $view_mode) {
  $links = dlike_flag_link('node', $node, $view_mode == 'teaser');
  $node->content['links']['flag'] = array(
    '#theme' => 'links__node__flag',
    '#links' => $links,
    '#attributes' => array(
      'class' => array(
        'links',
        'inline',
      ),
    ),
  );
}

/**
 * Implementation of hook_comment_view().
 */
function dlike_comment_view($comment) {
  $links = dlike_flag_link('comment', $comment);
  $comment->content['links']['flag'] = array(
    '#theme' => 'links',
    '#links' => $links,
    '#attributes' => array(
      'class' => array(
        'links',
        'inline',
      ),
    ),
  );
}

/**
 * This function is part of flag.module file.
 * This function is overridden here.
 */
function dlike_flag_link($type, $object = NULL, $teaser = FALSE) {
  if (!isset($object) || !flag_fetch_definition($type)) {
    return;
  }
  global $user;

  // Get all possible flags for this content-type.
  $flags = flag_get_flags($type);
  foreach ($flags as $flag) {
    $content_id = $flag
      ->get_content_id($object);
    if (!$flag
      ->uses_hook_link($teaser)) {

      // Flag is not configured to show its link here.
      continue;
    }
    if (!$flag
      ->access($content_id) && (!$flag
      ->is_flagged($content_id) || !$flag
      ->access($content_id, 'flag'))) {

      // User has no permission to use this flag or flag does not apply to this
      // content. The link is not skipped if the user has "flag" access but
      // not "unflag" access (this way the unflag denied message is shown).
      continue;
    }
    $dlike_append = dlike_append($type, $flag
      ->get_content_id($object), $flag->name);

    // The flag links are actually fully rendered theme functions.
    // The HTML attribute is set to TRUE to allow whatever the themer desires.
    $links['flag-' . $flag->name] = array(
      'title' => $flag
        ->theme($flag
        ->is_flagged($content_id) ? 'unflag' : 'flag', $content_id) . $dlike_append,
      'html' => TRUE,
    );
  }
  if (isset($links)) {
    return $links;
  }
}
function dlike_create_link($type, $flag_name, $content_id) {
  $flag = flag_get_flag($flag_name);
  if (!$flag) {

    // Flag does not exist.
    return;
  }
  if (!$flag
    ->access($content_id) && (!$flag
    ->is_flagged($content_id) || !$flag
    ->access($content_id, 'flag'))) {

    // User has no permission to use this flag.
    return;
  }
  $dlike_append = dlike_append($type, $content_id, $flag_name);
  return $flag
    ->theme($flag
    ->is_flagged($content_id) ? 'unflag' : 'flag', $content_id) . $dlike_append;
}

Functions

Namesort descending Description
dlike_comment_view Implementation of hook_comment_view().
dlike_create_link
dlike_flag_link This function is part of flag.module file. This function is overridden here.
dlike_form_data Submit function to store the field values.
dlike_form_flag_form_alter Implementation of hook_form_form_id_alter().
dlike_init Implementation of hook_init().
dlike_menu Implementation of hook_menu().
dlike_node_view Implementation of hook_node_view().
dlike_permission Implementation of hook_perm().
dlike_views_api Implementation of hook_views_api().