View source
<?php
include "includes/simpleads.theme.inc";
include "includes/simpleads.helper.inc";
include "includes/simpleads.filter.inc";
include "includes/simpleads.integration.inc";
function simpleads_node_info() {
$t = get_t();
return array(
'simpleads' => array(
'title_label' => $t('Advertisement Title'),
'name' => $t('Simple Ad'),
'base' => 'simpleads',
'description' => $t('Advertisement'),
),
);
}
function simpleads_form($node, $form_state) {
return node_content_form($node, $form_state);
}
function simpleads_menu() {
$items = array();
$items['simpleads/load/%/%'] = array(
'title' => 'Display Ads',
'page callback' => '_simpleads_show_ads',
'page arguments' => array(
2,
3,
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['admin/content/simpleads/%simpleadsid/stat'] = array(
'title' => 'Ad Statistics',
'page callback' => '_simpleads_stat_tab',
'page arguments' => array(
3,
),
'access arguments' => array(
'access ad statistics',
),
'weight' => 15,
'type' => MENU_LOCAL_TASK,
'file' => 'simpleads.stat.inc',
);
$items['node/%simpleadsid/stat'] = array(
'title' => 'Ad Statistics',
'page callback' => '_simpleads_stat_tab',
'page arguments' => array(
1,
),
'access callback' => '_simpleads_node_stat_tab',
'weight' => 15,
'type' => MENU_LOCAL_TASK,
'file' => 'simpleads.stat.inc',
);
$items['admin/content/simpleads'] = array(
'title' => 'Ads List',
'page callback' => '_simpleads_listing',
'access arguments' => array(
'access ad statistics',
),
'weight' => 15,
'type' => MENU_LOCAL_TASK,
'file' => 'simpleads.stat.inc',
);
$items['admin/content/simpleads/create_ad'] = array(
'title' => 'Create new Ad',
'page callback' => '_simpleads_new_add',
'access callback' => '_simpleads_access_callback',
'weight' => 15,
'type' => MENU_LOCAL_ACTION,
);
$items['admin/content/simpleads/create_ad_group'] = array(
'title' => 'Create new Ad Group',
'page callback' => '_simpleads_new_group_add',
'access callback' => '_simpleads_access_callback',
'weight' => 15,
'type' => MENU_LOCAL_ACTION,
);
$items['simpleads/redirect/%'] = array(
'title' => 'Redirect Ads',
'page callback' => '_simpleads_redirect',
'page arguments' => array(
2,
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['simpleads/dashboard/%'] = array(
'title' => 'SimpleAds Filter',
'description' => 'Filter Dashboard',
'page callback' => '_simpleads_filter_dashboard_page',
'access arguments' => array(
'administer nodes',
),
'type' => MENU_CALLBACK,
'page arguments' => array(
2,
),
'theme callback' => '_simpleads_filter_dashboard_theme',
);
return $items;
}
function simpleads_admin_paths() {
if (variable_get('node_admin_theme')) {
$paths = array(
'node/*/stat' => TRUE,
);
return $paths;
}
}
function simpleads_permission() {
return array(
'access ad statistics' => array(
'title' => t('Access ad statistics'),
),
'access ad node' => array(
'title' => t('Access ad node'),
'description' => t('Please use this permission to allow editors access ad node'),
),
'count ad impressions' => array(
'title' => t('Count ad impressions'),
),
'count ad clicks' => array(
'title' => t('Count ad clicks'),
),
);
}
function simpleads_node_access($node, $op, $account) {
if (isset($node->type) && $node->type == 'simpleads' && $op == 'view' && !user_access('access ad node', $account)) {
return NODE_ACCESS_DENY;
}
}
function simpleads_node_presave($node) {
if ($node->type == 'simpleads') {
$start_time = 0;
$end_time = 0;
if (isset($node->field_ad_start_date[$node->language]) && !empty($node->field_ad_start_date[$node->language][0]['value'])) {
$start_time = $node->field_ad_start_date[$node->language][0]['value'];
}
if (isset($node->field_ad_end_date[$node->language]) && !empty($node->field_ad_end_date[$node->language][0]['value'])) {
$end_time = $node->field_ad_end_date[$node->language][0]['value'];
}
$start_time = strtotime($start_time);
$end_time = strtotime($end_time);
if ($start_time != 0) {
$node->field_ad_start_date[$node->language][0]['value'] = format_date($start_time, 'custom', 'm/d/Y h:i a');
}
if ($end_time != 0) {
$node->field_ad_end_date[$node->language][0]['value'] = format_date($end_time, 'custom', 'm/d/Y h:i a');
}
_simpleads_activate_deactive_ad($node, 'presave');
}
}
function simpleads_node_delete($node) {
_simpleads_delete_click($node);
_simpleads_delete_impression($node);
_simpleads_delete_aggregated($node);
}
function simpleads_taxonomy_term_insert($term) {
if ($term->vocabulary_machine_name == 'ad_groups') {
$ad_group_name = $term->name;
$tid = $term->tid;
$block_delta = $term->vocabulary_machine_name . '_' . $tid;
$settings = array(
'ads_limit' => 1,
'ads_width' => '',
'ads_height' => '',
'block_info' => $ad_group_name,
'block_subject' => t('Advertisement'),
'block_delta' => $block_delta,
'ads_page' => '',
'ads_order' => 0,
);
db_insert('simpleads_blocks')
->fields(array(
'tid' => $tid,
'settings' => serialize($settings),
))
->execute();
}
}
function simpleads_taxonomy_term_delete($term) {
db_delete('simpleads_blocks')
->condition('tid', $term->tid)
->execute();
}
function simpleads_block_info() {
$blocks = array();
$result = db_select('simpleads_blocks', 'sb')
->fields('sb')
->execute();
foreach ($result as $block) {
$data = unserialize($block->settings);
$blocks[$data['block_delta']] = array(
'info' => t('Advertisement Block: @name', array(
'@name' => $data['block_info'],
)),
'cache' => DRUPAL_NO_CACHE,
);
}
return $blocks;
}
function simpleads_block_view($delta = '') {
$blocks = array();
$result = db_select('simpleads_blocks', 'sb')
->fields('sb')
->execute();
foreach ($result as $block) {
$data = unserialize($block->settings);
if ($data['block_delta'] == $delta) {
$blocks = array(
'subject' => check_plain($data['block_subject']),
'content' => theme('simpleads_block', array(
'tid' => $block->tid,
'ads_limit' => $data['ads_limit'],
'ads_page' => $data['ads_page'],
'ads_width' => $data['ads_width'],
'ads_height' => $data['ads_height'],
)),
);
}
}
return $blocks;
}
function simpleads_block_configure($delta = '') {
$form = array();
$max_ads = variable_get('simpleads_max_ads_per_block', 25);
$options = array();
for ($i = 1; $i <= $max_ads; $i++) {
$options[$i] = $i;
}
$result = db_select('simpleads_blocks', 'sb')
->fields('sb')
->execute();
foreach ($result as $block) {
$data = unserialize($block->settings);
if ($data['block_delta'] == $delta) {
$form['ads_limit'] = array(
'#type' => 'select',
'#title' => t('Number of ads to display'),
'#default_value' => $data['ads_limit'],
'#options' => $options,
);
$form['ads_page'] = array(
'#type' => 'textfield',
'#title' => t('Advertise page'),
'#default_value' => check_plain($data['ads_page']),
);
$form['ads_width'] = array(
'#type' => 'textfield',
'#title' => t('Ad width'),
'#default_value' => check_plain($data['ads_width']),
);
$form['ads_height'] = array(
'#type' => 'textfield',
'#title' => t('Ad height'),
'#default_value' => check_plain($data['ads_height']),
);
$order_options = array();
$order_options_all = module_invoke_all('simpleads_order_info');
if (count($order_options_all) > 0) {
foreach ($order_options_all as $delta => $name) {
$order_options[$delta] = check_plain($name);
}
}
$form['ads_order'] = array(
'#type' => 'select',
'#title' => t('Ads order'),
'#default_value' => isset($data['ads_order']) ? $data['ads_order'] : 'random',
'#options' => $order_options,
);
$effects = _simpleads_rotation_effects();
$form['ads_rotation_type'] = array(
'#type' => 'select',
'#title' => t('Ads auto rotation type'),
'#default_value' => isset($data['ads_rotation_type']) ? $data['ads_rotation_type'] : 0,
'#options' => $effects,
'#prefix' => '<div id="ads_rotation_settings">',
);
$form['ads_rotation_delay'] = array(
'#type' => 'textfield',
'#title' => t('Ad rotation delay'),
'#default_value' => isset($data['ads_rotation_delay']) ? check_plain($data['ads_rotation_delay']) : 10,
'#description' => t('Delay value is in seconds'),
'#suffix' => '</div>',
);
}
}
return $form;
}
function simpleads_block_save($delta = '', $edit = array()) {
$blocks = db_select('simpleads_blocks', 'sb')
->fields('sb')
->execute();
foreach ($blocks as $block) {
$data = unserialize($block->settings);
if ($data['block_delta'] == $delta) {
$data['ads_limit'] = $edit['ads_limit'];
$data['ads_page'] = $edit['ads_page'];
$data['ads_width'] = $edit['ads_width'];
$data['ads_height'] = $edit['ads_height'];
$data['ads_order'] = $edit['ads_order'];
$data['ads_rotation_type'] = $edit['ads_rotation_type'];
$data['ads_rotation_delay'] = $edit['ads_rotation_delay'];
db_update('simpleads_blocks')
->fields(array(
'settings' => serialize($data),
))
->condition('tid', $block->tid)
->execute();
}
}
}
function _simpleads_access_callback() {
global $user;
if (user_access('access ad node') || user_access('administer nodes') && user_access('bypass node access')) {
return TRUE;
}
}
function _simpleads_show_ads($tid, $limit) {
drupal_page_is_cacheable(FALSE);
if (is_numeric($tid)) {
$nodes = _simpleads_load_ads($tid, $limit);
print _simpleads_render_ajax_template($nodes, $tid, $limit);
}
exit;
}
function _simpleads_redirect($nid) {
global $user;
drupal_page_is_cacheable(FALSE);
if (is_numeric($nid)) {
$node = node_load($nid);
$url = "";
$default_message = t("You have been redirected here because you have permission to edit ads.\n Please log out from the site or open the site in different browser if you wish to test that the ad redirection is correct.");
$message = variable_get('simpleads_redirect_message', $default_message);
$show_message = variable_get('simpleads_redirect_message_show', TRUE);
$delta = variable_get('simpleads_redirect_delta', 'default');
if (isset($node->field_ad_url[$node->language]) && !empty($node->field_ad_url[$node->language][0]['value'])) {
$url = $node->field_ad_url[$node->language][0]['value'];
if (user_access('administer nodes') || user_access('access ad node')) {
if ($show_message) {
drupal_set_message(filter_xss($message), 'warning');
}
drupal_goto('node/' . $node->nid);
}
else {
_simpleads_save_click($node);
$url = _simpleads_validate_url($url);
module_invoke_all('simpleads_redirect', $url, $delta);
}
}
else {
return t('URL Address is not specified');
}
}
return ' ';
}
function _simpleads_new_add() {
drupal_goto('node/add/simpleads', array(
'query' => array(
'destination' => 'admin/content/simpleads',
),
));
}
function _simpleads_new_group_add() {
drupal_goto('admin/structure/taxonomy/ad_groups/add', array(
'query' => array(
'destination' => 'admin/content/simpleads',
),
));
}
function simpleads_simpleads_redirect($url, $delta) {
if ($delta == 'default') {
header("Location: " . $url, TRUE, 301);
}
}
function simpleads_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'simpleads_node_form') {
$fields_to_hide = variable_get('simpleads_form_alter', array());
if (count($fields_to_hide)) {
foreach ($fields_to_hide as $field) {
unset($form[$field]);
}
}
}
}
function simpleads_simpleads_ad_click($op, $node) {
$internal = variable_get('simpleads_clicks_internal', TRUE);
if ($op == 'insert' && $internal) {
if (user_access('count ad clicks') && !_simpleads_bot_detect()) {
db_insert('simpleads_clicks')
->fields(array(
'nid' => $node->nid,
'timestamp' => REQUEST_TIME,
'ip_address' => ip_address(),
))
->execute();
}
}
if ($op == 'delete' && $internal) {
db_delete('simpleads_clicks')
->condition('nid', $node->nid)
->execute();
}
}
function simpleads_simpleads_ad_impression($op, $node) {
global $user;
$internal = variable_get('simpleads_impressions_internal', TRUE);
if ($op == 'insert' && $internal) {
if (user_access('count ad impressions')) {
if ($user->uid != 1 && !_simpleads_bot_detect()) {
db_insert('simpleads_impressions')
->fields(array(
'nid' => $node->nid,
'timestamp' => REQUEST_TIME,
'ip_address' => ip_address(),
))
->execute();
}
}
}
if ($op == 'delete' && $internal) {
db_delete('simpleads_impressions')
->condition('nid', $node->nid)
->execute();
}
}
function simpleads_simpleads_stats_info($node, $op) {
$activity = t('Active');
if (isset($node->field_ad_status[$node->language])) {
if ($node->field_ad_status[$node->language][0]['value'] == 1) {
$activity = t('Active');
}
else {
$activity = t('Inactive');
}
}
if ($op == 'list') {
$ad_title = l($node->title, 'admin/content/simpleads/' . $node->nid . '/stat', array(
'html' => FALSE,
));
return array(
$ad_title,
);
}
if ($op == 'ad_group') {
$term_name = "";
if (isset($node->field_ad_category[$node->language]) && !empty($node->field_ad_category[$node->language][0]['tid'])) {
$term = taxonomy_term_load($node->field_ad_category[$node->language][0]['tid']);
$term_name = $term->name;
}
elseif (isset($node->field_ad_category[LANGUAGE_NONE]) && !empty($node->field_ad_category[LANGUAGE_NONE][0]['tid'])) {
$term = taxonomy_term_load($node->field_ad_category[LANGUAGE_NONE][0]['tid']);
$term_name = $term->name;
}
$ad_group = !empty($term_name) ? $term_name : '';
return array(
$ad_group,
);
}
if ($op == 'ad_status') {
return array(
_simpleads_statuses($node),
);
}
if ($op == 'ad_activity') {
return array(
$activity,
);
}
if ($op == 'ad_status_detail') {
$days_left_str = "";
$days_left = _simpleads_date_diff($node);
if ($days_left && $days_left > 0) {
$days_left_str = ', ' . format_plural($days_left, '1 day left.', '@count days left.');
}
$domain_module = "";
if (module_exists('domain')) {
if (isset($node->subdomains) && count($node->subdomains)) {
$domain_module = t('Published on !list', array(
'!list' => join(", ", $node->subdomains),
));
}
}
return array(
_simpleads_statuses($node) . ' (' . $activity . ')' . $days_left_str,
$domain_module,
);
}
if ($op == 'ad_operations') {
$operations = array();
$operations[] = l(t('Statistics'), 'admin/content/simpleads/' . $node->nid . '/stat') . ' ' . l(t('Edit'), 'node/' . $node->nid . '/edit', array(
'query' => array(
'destination' => 'admin/content/simpleads',
),
)) . ' ' . l(t('Delete'), 'node/' . $node->nid . '/delete', array(
'query' => array(
'destination' => 'admin/content/simpleads',
),
));
return $operations;
}
}
function simpleads_simpleads_order_info() {
$delta = array();
$delta['random'] = t('Random');
$delta['order_created_desc'] = t('Order by created date DESC');
$delta['order_modified_desc'] = t('Order by modifed date DESC');
$delta['order_title_asc'] = t('Order by node title ASC');
if (module_exists('nodequeue')) {
$qids = nodequeue_get_qids('simpleads');
foreach ($qids as $row) {
$nq = db_select('nodequeue_queue', 'nq')
->fields('nq', array(
'qid',
'name',
'title',
))
->condition('nq.qid', $row->qid)
->execute()
->fetchObject();
$delta['nqueue_' . $nq->qid] = t('Nodequeue: @name', array(
'@name' => $nq->title,
));
}
}
return $delta;
}
function simpleads_simpleads_order($delta, $term_ids, $limit) {
$query = db_select('node', 'n');
$query
->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
$query
->join('field_data_field_ad_status', 'f', 'n.nid = f.entity_id');
$query
->fields('n', array(
'nid',
));
$query
->condition('n.type', 'simpleads');
$query
->condition('f.entity_type', 'node');
$query
->condition('f.field_ad_status_value', 1);
$query
->condition('ti.tid', $term_ids, 'IN');
$query
->condition('n.status', 1);
$query
->range(0, $limit);
$query
->addTag('node_access');
if ($delta == 'random') {
$query
->orderRandom();
return $query
->execute();
}
if ($delta == 'order_created_desc') {
$query
->orderBy('n.created', 'DESC');
return $query
->execute();
}
if ($delta == 'order_modified_desc') {
$query
->orderBy('n.changed', 'DESC');
return $query
->execute();
}
if ($delta == 'order_title_asc') {
$query
->orderBy('n.title', 'ASC');
return $query
->execute();
}
if (module_exists('nodequeue')) {
$qids = nodequeue_get_qids('simpleads');
foreach ($qids as $row) {
if ($delta == 'nqueue_' . $row->qid) {
$query = db_select('nodequeue_nodes', 'nn');
$query
->fields('nn', array(
'nid',
));
$query
->condition('nn.qid', $row->qid);
$query
->orderBy('nn.position', 'DESC');
$query
->addTag('node_access');
return $query
->execute();
}
}
}
}
function simpleads_cron() {
$ads = db_select('node', 'n')
->fields('n')
->condition('n.type', 'simpleads')
->execute();
foreach ($ads as $ad) {
$node = node_load($ad->nid);
_simpleads_activate_deactive_ad($node);
}
$timestamp = variable_get('simpleads_last_aggregation_time', 0);
if (date('m-d-Y', $timestamp) != date('m-d-Y', REQUEST_TIME)) {
$result = db_query("SELECT\n FROM_UNIXTIME(s.timestamp, '%Y%m%d') AS date,\n s.nid, COUNT(*) AS all_count,\n COUNT(DISTINCT s.ip_address) AS unique_count\n FROM {simpleads_impressions} s\n WHERE FROM_UNIXTIME(s.timestamp, '%Y%m%d') != FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y%m%d')\n GROUP BY date, s.nid");
$nids = array();
foreach ($result as $row) {
db_merge('simpleads_stats')
->key(array(
'nid' => $row->nid,
'date' => $row->date,
))
->fields(array(
'nid' => $row->nid,
'date' => $row->date,
'clicks' => 0,
'clicks_unique' => 0,
'impressions' => $row->all_count,
'impressions_unique' => $row->unique_count,
))
->execute();
if (!in_array($row->nid, $nids)) {
$nids[] = $row->nid;
}
}
$result = db_query("SELECT\n FROM_UNIXTIME(s.timestamp, '%Y%m%d') as date,\n s.nid,\n COUNT(*) as all_count,\n COUNT(DISTINCT s.ip_address) as unique_count\n FROM {simpleads_clicks} s\n WHERE FROM_UNIXTIME(s.timestamp, '%Y%m%d') != FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y%m%d')\n GROUP BY date, s.nid");
foreach ($result as $row) {
db_update('simpleads_stats')
->fields(array(
'clicks' => $row->all_count,
'clicks_unique' => $row->unique_count,
))
->condition('date', $row->date)
->condition('nid', $row->nid)
->execute();
if (!in_array($row->nid, $nids)) {
$nids[] = $row->nid;
}
}
if (count($nids)) {
db_query("DELETE FROM {simpleads_impressions}\n WHERE nid IN (" . join(', ', $nids) . ")\n AND FROM_UNIXTIME(timestamp, '%Y%m%d') != FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y%m%d')");
db_query("DELETE FROM {simpleads_clicks}\n WHERE nid IN (" . join(', ', $nids) . ")\n AND FROM_UNIXTIME(timestamp, '%Y%m%d') != FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y%m%d')");
}
variable_set('simpleads_last_aggregation_time', REQUEST_TIME);
}
}