This CodeLet tracks the number of views of tabs, created using field collection module. When user clicks on respective tab, an ajax call is initiated and a value is stored in the db. Which can then be used for manipulating further. This CodeLet allows five free views for respective tab.
.Install file
<?php
/**
* @file
*
* Schema for tabs_addons. Developed by Bhavin Joshi
*/
/**
* Implementation of hook_schema
*
* @author DrupalD
*/
function tabs_addon_schema() {
$schema['tabs_access'] = array(
'fields' => array(
'id' => array(
'type' => 'serial',
'length' => 11,
),
'uid' => array(
'type' => 'int',
'length' => 11,
'not null' => TRUE,
),
'nid' => array(
'type' => 'int',
'length' => 11
),
'tab_type' => array(
'type' => 'varchar',
'length' => 255,
),
'access_count' => array(
'type' => 'int',
'length' => 2
),
),
'primary key' => array('id'),
);
return $schema;
}
/**
* Implementation of hook_install
*
* @author DrupalD
*/
function tabs_addon_install() {
drupal_install_schema('tabs_addon');
}
/**
* Implemenation of hook_uninstall
*
* @author DrupalD
*/
function tabs_addon_uninstall() {
drupal_uninstall_schema('tabs_addon');
}
?>
.Module file
<?php
/**
* @file
*
* Addon features for tabs module.
*
* @author DrupalD
*/
/**
* Implementation of hook_menu
*
* @author DrupalD
*/
function tabs_addon_menu() {
$items['circuit'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'tabs_addons_track_circuit',
'access callback' => TRUE,
);
$items['code'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'tabs_addons_track_code',
'access callback' => TRUE,
);
return $items;
}
/**
* Implementation of hook_init
*
* @author DrupalD
*/
function tabs_addon_init() {
drupal_add_js(drupal_get_path('module', 'tabs_addon') . '/tabs_addon.js');
}
/**
* tabs_addons_track_circuit
*
* @author DrupalD
*/
function tabs_addons_track_circuit($sn__nid) {
global $user;
unset($am__id);
$sn__count = db_result(db_query('SELECT COUNT(access_count) FROM {tabs_access} WHERE uid = %d AND tab_type ="circuit"', $user->uid));
if ($sn__count > 4) {
return TRUE;
}
$r__result = db_query('SELECT id FROM {tabs_access} WHERE nid = %d
AND uid = %d AND tab_type ="circuit"', $sn__nid, $user->uid);
while ($om__result = db_fetch_object($r__result)) {
$am__id[] = $om__result->id;
}
$r__record = db_query('SELECT nid FROM {tabs_access} WHERE
uid = %d AND tab_type ="circuit" ORDER BY id DESC LIMIT 0, 1', $user->uid);
$om__record = db_fetch_object($r__record);
if ($om__record->nid == NULL) {
db_query('INSERT INTO {tabs_access} VALUES(id, %d, %d, "circuit", 0)', $user->uid, $sn__nid);
}
}
/**
* tabs_addons_track_code
*
* @author DrupalD
*/
function tabs_addons_track_code() {
global $user;
unset($am__id);
$sn__count = db_result(db_query('SELECT COUNT(access_count) FROM {tabs_access} WHERE uid = %d AND tab_type ="code"', $user->uid));
if ($sn__count > 4) {
return drupal_json('exceed');
}
list($dummy, $sn__nid) = explode('-', $_GET['node']);
$r__result = db_query('SELECT id FROM {tabs_access} WHERE nid = %d AND
uid = %d AND tab_type = "code"', $sn__nid, $user->uid);
while ($om__result = db_fetch_object($r__result)) {
$am__id[] = $om__result->id;
}
$r__record = db_query('SELECT nid FROM {tabs_access} WHERE
uid = %d AND tab_type = "code" ORDER BY id DESC LIMIT 0, 1', $user->uid);
$om__record = db_fetch_object($r__record);
if ($om__record->nid != $sn__nid) {
db_query('INSERT INTO {tabs_access} VALUES(id, %d, %d, "code", 0)', $user->uid, $sn__nid);
}
}
?>
JS file
$(document).ready(function() {
$('a').click(function() {
if($(this).attr('href') == '#tabset-tab-3') {
node_id = $('.ja-node').attr('id');
$.ajax({
url: '/circuit',
type: 'GET',
data: 'node=' + node_id,
success: function(status) {
if (status != '') {
$('#tabset-tab-3').html('You have exceeded free views');
}
}
});
}
if($(this).children('span.tab').html() == 'Code' || $(this).children('span.tab').html() == 'Code2') {
node_id = $('.ja-node').attr('id');
$.ajax({
url: '/code',
type: 'GET',
data: 'node=' + node_id,
success: function(status) {
if (status != '') {
$('.field-field-code').html('You have exceeded free views');
$('.field-field-code2').html('You have exceeded free views');
}
}
});
}
});
});