. */ require_once('ad_token.inc'); /** * Implementation of hook_theme(). */ function ad_theme() { return array( 'ad_display' => array( 'file' => 'ad.module', 'arguments' => array( 'group' => NULL, 'display' => NULL, 'method' => 'javascript', ), ), 'ad_status_display' => array( 'file' => 'ad.module', 'arguments' => array( 'node' => NULL, ), ), 'ad_statistics_display' => array( 'file' => 'ad.pages.inc', 'arguments' => array( 'statistics' => NULL, ), ), 'node_ad' => array( 'file' => 'ad.pages.inc', 'arguments' => array( 'node' => NULL, 'yield_form' => TRUE, ), ), 'ad_admin_ads' => array( 'file' => 'ad.admin.inc', 'arguments' => array( 'form' => NULL, ), ), 'ad_filters' => array( 'file' => 'ad.admin.inc', 'arguments' => array( 'form' => NULL, ), ), 'ad_filter_form' => array( 'file' => 'ad.admin.inc', 'arguments' => array( 'form' => NULL, ), ), ); }; /** * Use this function to display ads from a specified group. * * @param $group * The ad group tid to display ads from. * @param $quantity * Optionally specify the number of unique ads to display. * @param $options * Any number of options from this list: hostid, nids. */ function ad($group = FALSE, $quantity = 1, $options = array()) { global $base_url; $adserve = variable_get('adserve', ''); $adserveinc = variable_get('adserveinc', ''); if (empty($adserve) || empty($adserveinc)) { // This is probably the first time ad() has been called. _ad_check_installation(); $adserve = variable_get('adserve', ''); $adserveinc = variable_get('adserveinc', ''); } if (!file_exists($adserve) || !file_exists($adserveinc)) { drupal_set_message(t('Ads cannot be displayed. The ad module is misconfigured, failed to locate the required serve.php ond/or adserve.inc file.', array('@misconfigured' => url('admin/content/ad/configure'))), 'error'); _ad_check_installation(); return (t('The ad module is misconfigured.', array('@misconfigured' => url('admin/content/ad/configure')))); } // Be sure a display method has been chosen. if (!isset($options['ad_display'])) { $options['ad_display'] = variable_get('ad_display', 'javascript'); } $options['quantity'] = isset($quantity) ? $quantity : 1; if (!isset($options['tids'])) { $options['tids'] = $group; } $options['cache'] = variable_get('ad_cache', 'none'); switch ($options['ad_display']) { case 'raw': require_once(drupal_get_path('module', 'ad') .'/adserve.inc'); require_once(drupal_get_path('module', 'ad') .'/adcache.inc'); $output = adserve_ad($options); break; case 'iframe': case 'jquery': $query['m'] = $options['ad_display']; // Fall through... case 'javascript': default: $query['q'] = $quantity; if (isset($options['hostid'])) { $query['k'] = $options['hostid']; } // Allow external cache files to define additional display variables. if ($options['cache'] != 'none') { $query['c'] = $options['cache']; $cache_variables = module_invoke('ad_cache_'. $options['cache'], 'adcacheapi', 'display_variables', array()); if (is_array($cache_variables)) { foreach ($cache_variables as $key => $value) { $query[$key] = $value; } } } // Allow ad_type modules to define additional display variables. $type_variables = module_invoke_all('adapi', 'display_variables', array()); if (is_array($type_variables)) { foreach ($type_variables as $key => $value) { $query[$key] = $value; } } if (isset($options['nids'])) { // Choose ads from the provided list of node Id's. $nids = $options['nids']; $query['n'] = $nids; $group = "nids-$nids"; } else if (isset($options['tids'])) { // Choose ads from the provided list of taxonomy terms. $tids = $options['tids']; $query['t'] = $tids; $group = "tids-$tids"; } else { // Choose ads from the specified group. $query['t'] = $group; $options['tids'] = $group; } if (isset($options['url'])) { $query['u'] = $options['url']; } else { $query['u'] = $_GET['q']; if ($alias = drupal_get_path_alias($_GET['q'])) { $query['l'] = $alias; } } $src = url($base_url .'/'. $adserve, array('query' => $query)); if ($options['ad_display'] == 'iframe') { // TODO: We need to know the IFrame size before it is displayed. This // limits the flexibility of what can be displayed in these frames. // For now we'll have a global value, later we'll add per-group // over-rides. $append = 'frameborder="'. variable_get('ad_iframe_frameborder', 0) .'" '; $append .= 'scrolling="'. variable_get('ad_iframe_scroll', 'auto') .'" '; $append .= 'name="'. $group .'" '; if ($height = variable_get('ad_iframe_height', '')) { $append .= 'height="'. $height .'" '; } if ($width = variable_get('ad_iframe_width', '')) { $append .= 'width="'. $width .'" '; } $output = '"; } else if ($options['ad_display'] == 'jquery') { // The theme function uses this to generate a CSS id for jQuery to use. $output = $src; } else { $output = ""; } break; } if (user_access('show advertisements')) { return theme('ad_display', $group, $output, $options['ad_display']); } else { return theme('ad_display', 'none', ""); } } /** * Function to display the actual advertisement to the screen. Wrap it in a * theme function to make it possible to customize in your own theme. */ function theme_ad_display($group, $display, $method = 'javascript') { static $id = -1; // Increment counter for displaying multiple advertisements on the page. $id++; // The naming convention for the id attribute doesn't allow commas. $group = preg_replace('/[,]/', '+', $group); if ($method == 'jquery') { return "\n
'. t($status_array[$node->adstatus]) .'
'; switch ($node->adstatus) { case 'approved': if ($node->autoactivate) { $output .= ''. t('This advertisement will be automatically activated on %timestamp, in %time.', array('%timestamp' => format_date($node->autoactivate, 'large'), '%time' => format_interval($node->autoactivate - time()))) .'
'; } break; case 'active': $activated = db_result(db_query("SELECT activated FROM {ads} WHERE aid = %d", $node->nid)); if ($activated) { $output .= ''. t('This advertisement has been active since %date.', array('%date' => format_date($activated, 'large'))) .'
'; } if ($node->autoexpire) { $output .= ''. t('This advertisement will expire on %timestamp, in %time.', array('%timestamp' => format_date($node->autoexpire, 'large'), '%time' => format_interval($node->autoexpire - time()))) .'
'; } if ($node->maxviews) { $views = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $node->nid, date('YmdH', $node->activated))); $output .= ''. t('This advertisement will expire after %left more impressions.', array('%left' => $node->maxviews - $views)) .'
'; } if ($node->maxclicks) { $clicks = (int)db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $node->nid, date('YmdH', $node->activated))); $output .= ''. t('This advertisement will expire after %left more clicks.', array('%left' => $node->maxclicks - $clicks)) .'
'; } break; case 'expired': $expired = db_result(db_query("SELECT expired FROM {ads} WHERE aid = %d", $node->nid)); if ($expired) { $output .= ''. t('This advertisement expired %date.', array('%date' => format_date($expired, 'large'))) .'
'; } break; } $output .= ''. t('The ad module provides a complete advertising system for Drupal powered websites. It does this through an API that allow other modules to handle various types of advertising content. For example, if enabled together with the ad_image module you will be able to display image based advertisements such as banner ads.') .'
'; break; case 'node/add/ad': $output = ''. t('Advertisements can be randomly displayed to visitors of your website.') .'
'; break; } return $output; } /** * Implementation of hook_cron(). */ function ad_cron() { if (time() - variable_get('ad_cron_timestamp', 0) >= 60) { // Locate ads that need to be activated or expired. $result = db_query('SELECT aid, adstatus, adtype, autoactivate, autoactivated, autoexpire, autoexpired FROM {ads} WHERE autoactivate <> 0 OR autoexpire <> 0'); while ($ad = db_fetch_object($result)) { switch ($ad->adstatus) { case 'approved': { // See if this ad is ready to be activated. if ($ad->autoactivate && $ad->autoactivate <= time()) { $node = node_load($ad->aid); // Activate the ad. db_query("UPDATE {ads} SET adstatus = 'active', autoactivate = 0, autoactivated = %d, activated = %d WHERE aid = %d", time(), time(), $ad->aid); ad_statistics_increment($ad->aid, 'autoactivated'); ad_statistics_increment($ad->aid, 'active'); watchdog('ad', 'Automatically activated ad %title with nid %nid.', array('%title' => $node->title, '%nid' => $node->nid)); // Allow modules to do special processing to automatically // activated advertisements. module_invoke('ad_'. $ad->adtype, 'adapi', 'autoactivate', $node); } else if (!$ad->autoactivate) { // Once daily warn that there's an ad stuck in approved state. if (time() - variable_get("ad_autoactivate_warning_$ad->aid", 0) >= 8600) { watchdog('ad', 'Warning: ad %title with nid %nid in approved state has no autoactivate date set.', array('%title' => $node->title, '%nid' => $node->nid)); variable_set("ad_autoactivate_warning_$ad->aid", time()); } } break; } case 'active': { // See if this ad is ready to be activated. if ($ad->autoexpire && $ad->autoexpire <= time()) { $node = node_load($ad->aid); // Expire the ad. db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $ad->aid); ad_statistics_increment($ad->aid, 'autoexpired'); ad_statistics_increment($ad->aid, 'expired'); watchdog('ad', 'Automatically expired ad %title with nid %nid.', array('%title' => $node->title, '%nid' => $node->nid)); // Allow modules to do special processing to automatically // activated advertisements. module_invoke('ad_'. $ad->adtype, 'adapi', 'autoexpire', $node); } else if (!$ad->autoexpire) { // Ad is already activated, but has autoactivate timestamp set. db_query("UPDATE {ads} SET autoactivate = 0 WHERE aid = %d", $ad->aid); } break; } default: $node = node_load($ad->aid); db_query('UPDATE {ads} SET autoactivate = 0, autoexpire = 0 WHERE aid = %d', $ad->aid); watchdog('ad', 'Warning: reset %type timestamp on advertisement %title with nid %nid because it is in %state state.', array('%title' => $node->title, '%nid' => $node->nid, '%type' => $ad->autoactivate ? 'autoactivate' : 'autoexpire', '%state' => $ad->adstatus)); } } variable_set('ad_cron_timestamp', time()); } } /** * Implementation of hook_perm(). */ function ad_perm() { return array('administer advertisements', 'create advertisements', 'edit own advertisements', 'edit any advertisement', 'delete own advertisements', 'delete any advertisement', 'show advertisements'); } /** * Implementation of hook_node_info(). */ function ad_node_info() { $items['ad'] = array( 'name' => t('Advertisement'), 'module' => 'ad', 'description' => t('Advertisements can be randomly displayed to visitors of your website.'), ); return $items; } /** * Implementation of hook_access(). */ function ad_access($op, $node, $account) { switch ($op) { case 'create': return (user_access('create advertisements', $account) || user_access('administer advertisements')); case 'update': return (user_access('edit any advertisement', $account) || (user_access('edit own advertisements', $account) && is_ad_owner($node->nid)) || user_access('administer advertisements', $account)); case 'delete': return (user_access('delete any advertisement', $account) || (user_access('delete own advertisements', $account) && is_ad_owner($node->nid)) || user_access('administer advertisements', $account)); case 'view': // Return false if user doesn't have basic permissions to view // advertisements. Don't return true to let default Drupal // domain/node access checks happen. if (!user_access('show advertisements', $account) && !user_access('administer advertisements', $account)) { return false; } } } /** * Implementation of hook_form(). */ function ad_form(&$node, &$form_state) { $form = array(); $type = node_get_types('type', $node); // When form_state is not empty, we should rather use it's values // to not loose data if validation fails. if (!empty($form_state['values'])) { $node = (object)$form_state['values']; } $form['aid'] = array( '#type' => 'value', '#value' => isset($node->nid) ? $node->nid : 0, ); if ($type->has_title) { $form['title'] = array( '#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => isset($node->title) ? $node->title : '', '#weight' => -5, ); } if ($type->has_body) { $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count); } else { // The body array has to exist otherwise the format field gets removed. $form['body'] = array( '#type' => 'hidden', '#value' => '', ); $form['body_field']['format'] = filter_form($node->format); } // determine the current ad type if (!isset($adtype)) { $adtypes = ad_get_types(); if (count($adtypes) == 1) { $adtype = key($adtypes); } else if (!count($adtypes)) { drupal_set_message(t('At least one ad type module must be enabled before you can create advertisements. For example, try enabling the ad_text or ad_image module.', array('!url' => url('admin/build/modules'))), 'error'); } } // display ad type switch if ((!isset($node->adtype) || isset($node->adtype_select)) && count($adtypes) >1) { $adtypes = array(0 => '---'); $adtypes += ad_get_types('name'); $form['select'] = array( '#type' => 'fieldset', '#title' => t('Select Ad type'), '#prefix' => '