View source
<?php
include_once dirname(__FILE__) . '/dlike.inc';
function dlike_init() {
drupal_add_css(drupal_get_path('module', 'dlike') . '/dlike.css');
drupal_add_js(drupal_get_path('module', 'dlike') . '/dlike.js');
}
function dlike_views_api() {
return array(
'api' => 2.0,
'path' => drupal_get_path('module', 'dlike'),
);
}
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;
}
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;
}
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';
}
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']]);
}
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',
),
),
);
}
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',
),
),
);
}
function dlike_flag_link($type, $object = NULL, $teaser = FALSE) {
if (!isset($object) || !flag_fetch_definition($type)) {
return;
}
global $user;
$flags = flag_get_flags($type);
foreach ($flags as $flag) {
$content_id = $flag
->get_content_id($object);
if (!$flag
->uses_hook_link($teaser)) {
continue;
}
if (!$flag
->access($content_id) && (!$flag
->is_flagged($content_id) || !$flag
->access($content_id, 'flag'))) {
continue;
}
$dlike_append = dlike_append($type, $flag
->get_content_id($object), $flag->name);
$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) {
return;
}
if (!$flag
->access($content_id) && (!$flag
->is_flagged($content_id) || !$flag
->access($content_id, '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;
}