pierre@1: . pierre@1: */ pierre@1: pierre@1: define('AD_CHANNEL_LIST_BOTH', 0); pierre@1: define('AD_CHANNEL_LIST_CHANNEL', 1); pierre@1: define('AD_CHANNEL_LIST_GROUP', 2); pierre@1: pierre@1: /** pierre@1: * Implementation of hook_help(). pierre@1: */ pierre@1: function ad_channel_help($path) { pierre@1: $output = ''; pierre@1: switch ($path) { pierre@1: case 'admin/help#ad_channel': pierre@1: $output = '
'. t('This module provides the ability to create advertisement channels, for which rules can be defined.') .'
'; pierre@1: break; pierre@1: case 'admin/content/ad/channel': pierre@1: case 'admin/content/ad/channel/list': pierre@1: return ''. t('This is a list of existing containers and channels that you can edit. Containers hold channels, and channels hold advertisements.') .'
'; pierre@1: case 'admin/content/ad/channel/container': pierre@1: return ''. t('Containers help you organize your advertisement channels. A container can hold one or more related advertisement channels.') .'
'; pierre@1: case 'admin/content/ad/channel/channel': pierre@1: return ''. t('Advertisements can be assigned to one or more advertisement channels. Rules can then be applied to these channels.') .'
'; pierre@1: } pierre@1: return $output; pierre@1: } pierre@1: pierre@1: /** pierre@1: * Drupal _perm hook. Establishes permissions used by this module. pierre@1: * pierre@1: * @return An array of permissions used by this module. pierre@1: */ pierre@1: function ad_channel_perm() { pierre@1: return (array('administer channels', 'configure ad premier status')); pierre@1: } pierre@1: pierre@1: /** pierre@1: * Implementation of hook_menu. pierre@1: */ pierre@1: function ad_channel_menu() { pierre@1: $items = array(); pierre@1: pierre@1: $items['admin/content/ad/channel'] = array( pierre@1: 'title' => t('Channels'), pierre@1: 'page callback' => 'ad_channel_admin_overview', pierre@1: 'access arguments' => array('administer channels'), pierre@1: 'type' => MENU_LOCAL_TASK, pierre@1: 'weight' => 6); pierre@1: $items['admin/content/ad/channel/list'] = array( pierre@1: 'title' => t('List'), pierre@1: 'page callback' => 'ad_channel_admin_overview', pierre@1: 'access arguments' => array('administer channels'), pierre@1: 'type' => MENU_DEFAULT_LOCAL_TASK, pierre@1: 'weight' => 0); pierre@1: $items['admin/content/ad/channel/container'] = array( pierre@1: 'title' => t('Create container'), pierre@1: 'page callback' => 'drupal_get_form', pierre@1: 'page arguments' => array('ad_channel_admin_container'), pierre@1: 'access arguments' => array('administer channels'), pierre@1: 'type' => MENU_LOCAL_TASK, pierre@1: 'weight' => 2); pierre@1: $items['admin/content/ad/channel/channel'] = array( pierre@1: 'title' => t('Create channel'), pierre@1: 'page callback' => 'drupal_get_form', pierre@1: 'page arguments' => array('ad_channel_admin_channel'), pierre@1: 'access arguments' => array('administer channels'), pierre@1: 'type' => MENU_LOCAL_TASK, pierre@1: 'weight' => 4); pierre@1: $items['admin/content/ad/channel/settings'] = array( pierre@1: 'title' => t('Settings'), pierre@1: 'page callback' => 'drupal_get_form', pierre@1: 'page arguments' => array('ad_channel_admin_settings'), pierre@1: 'access arguments' => array('administer channels'), pierre@1: 'type' => MENU_LOCAL_TASK, pierre@1: 'weight' => 9); sly@2: $items['admin/content/ad/channel/container/%ad_channel_container/delete'] = array( sly@2: 'page callback' => 'drupal_get_form', sly@2: 'page arguments' => array('ad_channel_admin_confirm_delete_container', 5), sly@2: 'access arguments' => array('administer channels'), sly@2: 'type' => MENU_CALLBACK); sly@2: $items['admin/content/ad/channel/channel/%ad_channel_channel/delete'] = array( sly@2: 'page callback' => 'drupal_get_form', sly@2: 'page arguments' => array('ad_channel_admin_confirm_delete_channel', 5), sly@2: 'access arguments' => array('administer channels'), sly@2: 'type' => MENU_CALLBACK); pierre@1: pierre@1: return $items; pierre@1: } pierre@1: pierre@1: /** sly@2: * Load a specified container. sly@2: */ sly@2: function ad_channel_container_load($conid = 0) { sly@2: static $containers = array(); sly@2: if (!isset($containers[$conid])) { sly@2: $containers[$conid] = db_fetch_object(db_query('SELECT * FROM {ad_channel_container} WHERE conid = %d', $conid)); sly@2: } sly@2: return $containers[$conid]; sly@2: } sly@2: sly@2: /** sly@2: * Load a specified channel. sly@2: */ sly@2: function ad_channel_channel_load($chid = 0) { sly@2: static $channels = array(); sly@2: if (!isset($channels[$chid])) { sly@2: $channels[$chid] = db_fetch_object(db_query('SELECT * FROM {ad_channel} WHERE chid = %d', $chid)); sly@2: } sly@2: return $channels[$chid]; sly@2: } sly@2: sly@2: /** pierre@1: * Implementation of hook_form_alter(). pierre@1: * Generate a form for selecting channels to associate with an advertisement. pierre@1: */ pierre@1: function ad_channel_form_alter(&$form, &$form_state, $form_id) { pierre@1: if (isset($form['type']) && $form_id == 'ad_node_form') { pierre@1: $fieldset = FALSE; pierre@1: $containers = _ad_channel_get_containers(); pierre@1: foreach ($containers as $container) { pierre@1: $channels = _ad_channel_get_container_channels($container->conid); pierre@1: if (!empty($channels)) { pierre@1: if ($container->conid) { pierre@1: $fieldset = TRUE; pierre@1: } pierre@1: if ($fieldset) { pierre@1: $form['channel'][$container->conid] = array( pierre@1: '#type' => 'fieldset', pierre@1: '#title' => $container->name, pierre@1: '#collapsible' => FALSE, pierre@1: '#collapsed' => FALSE, pierre@1: ); pierre@1: } pierre@1: foreach ($channels as $channel) { pierre@1: if (is_object($form['#node'])) { pierre@1: $node = $form['#node']; pierre@1: $default = isset($node->channel[$channel->chid]); pierre@1: } pierre@1: else { pierre@1: $default = 0; pierre@1: } pierre@1: $form['channel'][$container->conid]["channel-$channel->chid"] = array( pierre@1: '#type' => 'checkbox', pierre@1: '#title' => $channel->name, pierre@1: '#description' => $channel->description, pierre@1: '#default_value' => $default, pierre@1: ); pierre@1: } pierre@1: } pierre@1: } pierre@1: $node = node_load($form['nid']['#value']); pierre@1: if (isset($form['channel']) && is_array($form['channel']) && !empty($form['channel'])) { pierre@1: $form['channel'] += array( pierre@1: '#type' => 'fieldset', pierre@1: '#title' => t('Channels'), pierre@1: '#collapsible' => TRUE, pierre@1: '#collapsed' => FALSE, pierre@1: ); pierre@1: $form['channel']['#weight'] = -2; pierre@1: $form['channel']['#tree'] = TRUE; pierre@1: } pierre@1: $form['priority'] = array( pierre@1: '#type' => 'fieldset', sly@2: '#access' => user_access('configure ad premier status'), pierre@1: '#title' => t('Priority'), pierre@1: '#collapsible' => TRUE, pierre@1: '#collapsed' => FALSE, pierre@1: ); pierre@1: $form['priority']['premiere'] = array( pierre@1: '#type' => 'checkbox', sly@2: '#access' => user_access('configure ad premier status'), pierre@1: '#title' => t('Premiere'), pierre@1: '#description' => t('An active premiere advertisement will override all other non-premiere advertisements in the same channel. As long as one or more premiere advertisements are active in a channel, non-premiere advertisements will not be displayed in that channel.'), pierre@1: '#default_value' => isset($node->premiere) ? $node->premiere : FALSE, pierre@1: ); pierre@1: $form['priority']['#weight'] = -1; pierre@1: } pierre@1: else if ($form_id == 'ad_filter_form') { pierre@1: $session = &$_SESSION['ad_overview_filter']; pierre@1: $session = is_array($session) ? $session : array(); pierre@1: pierre@1: $display_channel = TRUE; pierre@1: $display_premiere = TRUE; pierre@1: foreach ($session as $filter) { pierre@1: list($type, $value) = $filter; pierre@1: if ($type == 'channel') { pierre@1: $display_channel = FALSE; pierre@1: } pierre@1: else if ($type == 'premiere') { pierre@1: $display_premiere = FALSE; pierre@1: } pierre@1: } pierre@1: pierre@1: if ($display_channel) { pierre@1: $channels = _ad_channel_get_channels(); pierre@1: $options = array(); pierre@1: foreach ($channels as $channel) { pierre@1: $key = 'channel-'. $channel->chid; pierre@1: $options[$key] = $channel->name; pierre@1: } pierre@1: $form['filters']['status']['channel'] = array( pierre@1: '#type' => 'select', pierre@1: '#options' => $options pierre@1: ); pierre@1: $form['filters']['filter']['#options']['channel'] = 'channel'; pierre@1: } pierre@1: else { pierre@1: unset($form['filters']['status']['channel']); pierre@1: unset($form['filters']['filter']['#options']['channel']); pierre@1: } pierre@1: pierre@1: if ($display_premiere) { pierre@1: $options = array( pierre@1: '0' => t('false'), pierre@1: '1' => t('true')); pierre@1: $form['filters']['status']['premiere'] = array( pierre@1: '#type' => 'select', pierre@1: '#options' => $options pierre@1: ); pierre@1: $form['filters']['filter']['#options']['premiere'] = 'premiere'; pierre@1: } pierre@1: else { pierre@1: unset($form['filters']['status']['premiere']); pierre@1: unset($form['filters']['filter']['#options']['premiere']); pierre@1: } pierre@1: } pierre@1: else if ($form_id == 'ad_report_admin') { pierre@1: $channels = _ad_channel_get_channels(); pierre@1: if (is_array($channels) && !empty($channels)) { pierre@1: $channel = isset($_SESSION['ad_report_channel']) && is_array($_SESSION['ad_report_channel']) && !empty($_SESSION['ad_report_channel']) ? $_SESSION['ad_report_channel'] : array('any'); pierre@1: $form['channels'] = array( pierre@1: '#type' => 'fieldset', pierre@1: '#title' => t('Channels'), pierre@1: ); pierre@1: $options = array(); pierre@1: $options['any'] = t('- Any -'); pierre@1: foreach ($channels as $chan) { pierre@1: $options[$chan->chid] = $chan->name; pierre@1: } pierre@1: $form['channels']['channel'] = array( pierre@1: '#type' => 'select', pierre@1: '#title' => t('Ad channels'), pierre@1: '#options' => $options, pierre@1: '#multiple' => TRUE, pierre@1: '#required' => TRUE, pierre@1: '#default_value' => $channel, pierre@1: ); pierre@1: $form['#submit'] = array_merge(array('ad_channel_admin_report_submit'), $form['#submit']); pierre@1: } pierre@1: } pierre@1: else if ($form_id == 'ad_admin_ads' && is_array($form['group'])) { pierre@1: foreach ($form['group'] as $aid => $value) { pierre@1: $result = db_query('SELECT chid FROM {ad_channel_node} WHERE nid = %d', $aid); pierre@1: $names = array(); pierre@1: while ($channel = db_fetch_object($result)) { pierre@1: $names[] = _ad_channel_get_name($channel->chid); pierre@1: } pierre@1: if (empty($names)) { pierre@1: $names[] = t('none'); pierre@1: } pierre@1: $list = variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL); pierre@1: switch ($list) { pierre@1: case AD_CHANNEL_LIST_CHANNEL: pierre@1: unset($form['group']); pierre@1: $form['channel'][$aid]['#value'] = implode(', ', $names); pierre@1: break; pierre@1: case AD_CHANNEL_LIST_BOTH: pierre@1: $form['channel'][$aid]['#value'] = implode(', ', $names); pierre@1: break; pierre@1: case AD_CHANNEL_LIST_GROUP: pierre@1: // do nothing pierre@1: break; pierre@1: } pierre@1: } pierre@1: } pierre@1: } pierre@1: pierre@1: /** pierre@1: * Register our own ad_admin_ads theme function. pierre@1: */ pierre@1: function ad_channel_theme_registry_alter(&$theme_registry) { pierre@1: $list = variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL); pierre@1: if ($list == AD_CHANNEL_LIST_CHANNEL || $list == AD_CHANNEL_LIST_BOTH) { pierre@1: if (!empty($theme_registry['ad_admin_ads'])) { pierre@1: $theme_registry['ad_admin_ads']['function'] = 'ad_channel_ad_admin_ads'; pierre@1: } pierre@1: } pierre@1: } pierre@1: pierre@1: /** pierre@1: * Implement custom theme function for displaying ad overview, replacing groups pierre@1: * with channels. pierre@1: */ pierre@1: function ad_channel_ad_admin_ads($form) { pierre@1: $list = variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL); pierre@1: pierre@1: // Overview table: pierre@1: if ($list == AD_CHANNEL_LIST_CHANNEL) { pierre@1: $header = array(theme('table_select_header_cell'), t('Title'), t('Channel'), t('Type'), t('Status'), t('Operations')); pierre@1: } pierre@1: else { pierre@1: $header = array(theme('table_select_header_cell'), t('Title'), t('Group'), t('Channel'), t('Type'), t('Status'), t('Operations')); pierre@1: } pierre@1: pierre@1: $output = drupal_render($form['options']); pierre@1: if (isset($form['title']) && is_array($form['title'])) { pierre@1: foreach (element_children($form['title']) as $key) { pierre@1: $row = array(); pierre@1: $row[] = drupal_render($form['ads'][$key]); pierre@1: $row[] = drupal_render($form['title'][$key]); pierre@1: if ($list == AD_CHANNEL_LIST_BOTH) { pierre@1: $row[] = drupal_render($form['group'][$key]); pierre@1: } pierre@1: $row[] = drupal_render($form['channel'][$key]); pierre@1: $row[] = drupal_render($form['adtype'][$key]); pierre@1: $row[] = drupal_render($form['adstatus'][$key]); pierre@1: $row[] = drupal_render($form['operations'][$key]); pierre@1: $rows[] = $row; pierre@1: } pierre@1: pierre@1: } pierre@1: else { pierre@1: $rows[] = array(array('data' => t('No ads available.'), 'colspan' => '6')); pierre@1: } pierre@1: pierre@1: $output .= theme('table', $header, $rows); pierre@1: if ($form['pager']['#value']) { pierre@1: $output .= drupal_render($form['pager']); pierre@1: } pierre@1: pierre@1: $output .= drupal_render($form); pierre@1: pierre@1: return $output; pierre@1: } pierre@1: pierre@1: function _ad_channel_get_name($chid) { pierre@1: static $names = array(); pierre@1: if (!isset($names[$chid])) { pierre@1: $names[$chid] = db_result(db_query('SELECT name FROM {ad_channel} WHERE chid = %d', $chid)); pierre@1: } pierre@1: return $names[$chid]; pierre@1: } pierre@1: pierre@1: function ad_channel_admin_report_submit($form, $form_state) { pierre@1: if ($form_state['clicked_button']['#value'] == t('Reset report')) { pierre@1: unset($_SESSION['ad_report_channel']); pierre@1: } pierre@1: else if ($form_state['clicked_button']['#value'] == t('Generate report')) { pierre@1: if (isset($form_state['values']['channel']) && is_array($form_state['values']['channel'])) { pierre@1: $channels = array(); pierre@1: $any = FALSE; pierre@1: foreach ($form_state['values']['channel'] as $chid) { pierre@1: if (is_numeric($chid)) { pierre@1: $channels[] = $chid; pierre@1: } pierre@1: else { pierre@1: $any = TRUE; pierre@1: } pierre@1: } pierre@1: if (!$any && !empty($channels)) { pierre@1: $_SESSION['ad_report_channel'] = $channels; pierre@1: } pierre@1: else { pierre@1: if (isset($_SESSION['ad_report_channel'])) { pierre@1: unset($_SESSION['ad_report_channel']); pierre@1: } pierre@1: } pierre@1: } pierre@1: } pierre@1: } pierre@1: pierre@1: /** pierre@1: * Filter reports by selected channel. pierre@1: */ pierre@1: function ad_channel_adreport($join, $where, $args, $select) { pierre@1: if (isset($_SESSION['ad_report_channel']) && is_array($_SESSION['ad_report_channel']) && !empty($_SESSION['ad_report_channel'])) { pierre@1: $join = array('LEFT JOIN {ad_channel_node} acn ON acn.nid = a.aid'); pierre@1: $where = array('acn.chid IN (%s)'); pierre@1: $args = array(implode(',', $_SESSION['ad_report_channel'])); pierre@1: return array('join' => $join, 'where' => $where, 'args' => $args); pierre@1: } pierre@1: } pierre@1: pierre@1: /** pierre@1: * Implement hook _adapi. pierre@1: */ pierre@1: function ad_channel_adapi($op, &$node) { pierre@1: switch ($op) { pierre@1: case 'admin_filters': pierre@1: $channels = _ad_channel_get_channels(); pierre@1: $options = array(); pierre@1: foreach ($channels as $channel) { pierre@1: $key = 'channel-'. $channel->chid; pierre@1: $options[$key] = $channel->name; pierre@1: } pierre@1: $filters['channel'] = array( pierre@1: 'title' => t('channel'), pierre@1: 'options' => $options, pierre@1: ); pierre@1: $options = array( pierre@1: '0' => t('false'), pierre@1: '1' => t('true')); pierre@1: $filters['premiere'] = array( pierre@1: 'title' => t('premiere'), pierre@1: 'options' => $options, pierre@1: ); pierre@1: return $filters; pierre@1: case 'admin_filter_query': pierre@1: if (is_array($node)) { pierre@1: list($key, $value) = $node; pierre@1: if ($key == 'channel') { pierre@1: list($key, $value) = explode('-', $value, 2); pierre@1: return array( pierre@1: 'channel' => array( pierre@1: 'where' => 'c.chid = %d', pierre@1: 'join' => 'INNER JOIN {ad_channel_node} c ON n.nid = c.nid ', pierre@1: 'value' => $value, pierre@1: )); pierre@1: } pierre@1: else if ($key == 'premiere') { pierre@1: return array( pierre@1: 'premiere' => array( pierre@1: 'where' => 'p.priority = %d', pierre@1: 'join' => 'INNER JOIN {ad_priority} p ON n.nid = p.aid ', pierre@1: 'value' => $value, pierre@1: )); pierre@1: } pierre@1: } pierre@1: break; pierre@1: } pierre@1: } pierre@1: pierre@1: /** pierre@1: * Implementation of hook_nodeapi(). pierre@1: */ pierre@1: function ad_channel_nodeapi($node, $op, $arg = 0) { pierre@1: switch ($op) { pierre@1: case 'view': pierre@1: return _ad_channel_view_node($node); pierre@1: case 'load': pierre@1: return _ad_channel_load_node($node); pierre@1: case 'insert': pierre@1: case 'update': pierre@1: // Fully load the node object to confirm that we are working with an pierre@1: // advertisement. pierre@1: $ad = node_load($node->nid); pierre@1: if (isset($ad->adtype)) { pierre@1: return _ad_channel_save_node($node); pierre@1: } pierre@1: case 'delete': pierre@1: return _ad_channel_delete_node($node); pierre@1: case 'validate': pierre@1: return _ad_channel_validate_nodes($node); pierre@1: } pierre@1: } pierre@1: pierre@1: /** pierre@1: * Implementation of hook_ad_build_cache(). pierre@1: */ pierre@1: function ad_channel_ad_build_cache() { pierre@1: $cache = array(); pierre@1: $ads = array(); pierre@1: $active = db_query("SELECT aid FROM {ads} WHERE adstatus = 'active'"); pierre@1: while ($ad = db_fetch_object($active)) { pierre@1: // cache channel<->node relation pierre@1: $result = db_query('SELECT chid FROM {ad_channel_node} WHERE nid = %d', $ad->aid); pierre@1: while ($channel = db_fetch_object($result)) { pierre@1: $ads[$ad->aid][$channel->chid] = $channel->chid; pierre@1: //$ads[$channel->chid][$ad->aid] = $ad->aid; pierre@1: } pierre@1: } pierre@1: $channels = array(); sly@2: $result = db_query('SELECT chid, display, urls, no_channel_weight FROM {ad_channel}'); pierre@1: while ($channel = db_fetch_object($result)) { pierre@1: $channels[$channel->chid] = $channel; pierre@1: } pierre@1: $result = db_query("SELECT p.aid, p.priority FROM {ads} a LEFT JOIN {ad_priority} p ON a.aid = p.aid WHERE a.adstatus = 'active' AND p.priority = 1"); pierre@1: $premiere = array(); pierre@1: while ($priority = db_fetch_object($result)) { pierre@1: $premiere[$priority->aid] = $priority->aid; pierre@1: } pierre@1: $cache['channel']['ads'] = $ads; pierre@1: $cache['channel']['channels'] = $channels; pierre@1: $cache['channel']['display'] = variable_get('ad_channel_display', 0); pierre@1: $cache['premiere'] = $premiere; pierre@1: pierre@1: $cache['channel']['hook_filter'] = array( pierre@1: 'weight' => 0, pierre@1: 'file' => drupal_get_path('module', 'ad_channel') .'/ad_channel.inc', pierre@1: 'function' => 'ad_channel_cache_filter', pierre@1: ); pierre@1: pierre@1: return $cache; pierre@1: } pierre@1: pierre@1: /***/ pierre@1: pierre@1: /** pierre@1: * Settings form. pierre@1: */ pierre@1: function ad_channel_admin_settings() { pierre@1: $form = array(); pierre@1: pierre@1: $form['ad_channel_display'] = array( pierre@1: '#type' => 'radios', pierre@1: '#title' => t('Display advertisements not assigned to any channel'), pierre@1: '#options' => array(t('Only if no matching advertisements are found in the active channels'), t('Always'), t('Never')), pierre@1: '#default_value' => variable_get('ad_channel_display', 0), pierre@1: '#description' => t('By default, advertisements will first be selected out of the active channels, and if none are found they will be selected out of advertisements not assigned to any channel. If you select "Always", advertisements will be selected out of the active channels and from advertisements not assigned to any channels. If you select "Never", advertisements will only be selected out of the active channels, and advertisements not assigned to any channel will not ever be displayed.'), pierre@1: ); pierre@1: $form['ad_channel_admin_list'] = array( pierre@1: '#type' => 'radios', pierre@1: '#title' => t('Display channels on administrative ads overview listing'), pierre@1: '#options' => array(t('In addition to groups'), t('In place of groups'), t('Not at all')), pierre@1: '#default_value' => variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL), pierre@1: '#description' => t('By default, channels will be listed along with groups on the administrative ads list. You can optionally disable either the groups column (by selecting "in place of groups"), or the channel column (by selecting "not at all").'), pierre@1: ); sly@2: $options = array(0 => t('No limit')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); sly@2: $form['ad_channel_ad_limit'] = array( sly@2: '#type' => 'select', sly@2: '#title' => t('Maximum number of channels that can be assigned to a single ad'), sly@2: '#options' => $options, sly@2: '#default_value' => variable_get('ad_channel_ad_limit', 0), sly@2: '#description' => t('Optionally limit the number of channels that a single advertisement can be assigned to.'), sly@2: ); pierre@1: pierre@1: return system_settings_form($form); pierre@1: } pierre@1: pierre@1: /** pierre@1: * Add channel information when viewing node. pierre@1: */ pierre@1: function _ad_channel_view_node($node) { pierre@1: if (isset($node->adtype) && user_access('administer channels')) { pierre@1: if (isset($node->channel) && is_array($node->channel) && !empty($node->channel)) { pierre@1: $channels = array(); pierre@1: foreach ($node->channel as $chid) { pierre@1: $channel = _ad_channel_get_channels($chid); pierre@1: $channels[] = $channel->name; pierre@1: } pierre@1: $node->content['channel'] = array( pierre@1: '#value' => theme('box', t('Channels'), theme('item_list', $channels)), pierre@1: '#weight' => 1, pierre@1: ); pierre@1: } pierre@1: if (isset($node->premiere)) { pierre@1: if (isset($node->premiere) && $node->premiere == 1) { pierre@1: $output = t('This is a premiere advertisement.'); pierre@1: } pierre@1: else { pierre@1: $output = t('This is not a premiere advertisement.'); pierre@1: } pierre@1: $node->content['premiere'] = array( pierre@1: '#value' => theme('box', t('Premiere'), $output), pierre@1: '#weight' => 1, pierre@1: ); pierre@1: } pierre@1: } pierre@1: } pierre@1: pierre@1: /** pierre@1: * Load channels associated with specified node. pierre@1: */ pierre@1: function _ad_channel_load_node($node) { pierre@1: $result = db_query('SELECT chid FROM {ad_channel_node} WHERE nid = %d', $node->nid); pierre@1: $output['channel'] = array(); pierre@1: while ($chid = db_fetch_object($result)) { pierre@1: $output['channel'][$chid->chid] = $chid->chid; pierre@1: } pierre@1: // currently 0 or 1, with one being a 'premiere' advertisement. pierre@1: $output['premiere'] = (int)db_result(db_query('SELECT priority FROM {ad_priority} WHERE aid = %d', $node->nid)); pierre@1: return $output; pierre@1: } pierre@1: pierre@1: /** pierre@1: * Save channels associated with added or updated node. pierre@1: */ pierre@1: function _ad_channel_save_node($node) { pierre@1: // delete old channel information, then add new pierre@1: db_query('DELETE FROM {ad_channel_node} WHERE nid = %d', $node->nid); pierre@1: $channels = _ad_channel_get_enabled($node); pierre@1: foreach ($channels as $chid) { pierre@1: db_query('INSERT INTO {ad_channel_node} (chid, nid) VALUES(%d, %d)', $chid, $node->nid); pierre@1: } sly@2: if (user_access('configure ad premier status')) { pierre@1: db_query('UPDATE {ad_priority} SET priority = %d WHERE aid = %d', isset($node->premiere) ? $node->premiere : 0, $node->nid); pierre@1: if (!db_affected_rows()) { pierre@1: db_query('INSERT INTO {ad_priority} (aid, priority) VALUES(%d, %d)', $node->nid, isset($node->premiere) ? $node->premiere : 0); pierre@1: } pierre@1: } pierre@1: } pierre@1: pierre@1: /** pierre@1: * Delete channel information associated with node. pierre@1: */ pierre@1: function _ad_channel_delete_node($node) { pierre@1: if ($node->nid) { pierre@1: db_query('DELETE FROM {ad_channel_node} WHERE nid = %d', $node->nid); pierre@1: db_query('DELETE FROM {ad_priority} WHERE aid = %d', $node->nid); pierre@1: } pierre@1: } pierre@1: pierre@1: /** pierre@1: * Be sure that the enabled channels actually can be enabled. pierre@1: */ pierre@1: function _ad_channel_validate_nodes($node) { pierre@1: $channels = _ad_channel_get_enabled($node); sly@2: $limit = variable_get('ad_channel_ad_limit', 0); sly@2: if ($limit && sizeof($channels) > $limit) { sly@2: $quantity_error = TRUE; sly@2: } sly@2: else { sly@2: $quantity_error = FALSE; sly@2: } pierre@1: foreach ($channels as $chid) { pierre@1: $channel = _ad_channel_get_channels($chid); sly@2: if ($quantity_error) { sly@2: $quantity_error = FALSE; sly@2: form_set_error("channel[$channel->conid][channel-$chid]", t('You can not assign this advertisement to more than !limit.', array('!limit' => format_plural(sizeof($limit), '1 channel', '@count channels')))); sly@2: } pierre@1: $taxonomy = is_array($node->taxonomy) ? $node->taxonomy : array(); pierre@1: $groups = unserialize($channel->groups); pierre@1: if (!empty($groups)) { pierre@1: $enabled = FALSE; pierre@1: foreach($groups as $group) { pierre@1: if ($group) { pierre@1: $enabled = TRUE; pierre@1: break; pierre@1: } pierre@1: } pierre@1: if ($enabled) { pierre@1: $ad_groups = taxonomy_get_tree(_ad_get_vid()); pierre@1: foreach ($ad_groups as $ad_group) { pierre@1: if (is_array($taxonomy[$ad_group->vid]) && pierre@1: isset($taxonomy[$ad_group->vid][$ad_group->tid]) && pierre@1: isset($groups[$ad_group->tid]) && !$groups[$ad_group->tid] && pierre@1: !isset($groups[''])) { pierre@1: form_set_error("channel[$channel->conid][channel-$chid]", t('The %channel channel does not allow advertisements from the %group group.', array('%channel' => $channel->name, '%group' => $ad_group->name))); pierre@1: } pierre@1: } pierre@1: } pierre@1: } pierre@1: } pierre@1: } pierre@1: pierre@1: /** pierre@1: * Retrive list of enabled channels from node object. pierre@1: */ pierre@1: function _ad_channel_get_enabled($node) { pierre@1: static $enabled = array(); pierre@1: if (!isset($enabled[$node->nid])) { pierre@1: $enabled[$node->nid] = array(); pierre@1: if (isset($node->channel) && is_array($node->channel) && !empty($node->channel)) { pierre@1: foreach ($node->channel as $conid => $channels) { pierre@1: foreach ($channels as $id => $enable) { pierre@1: if ($enable) { pierre@1: $chid = explode('-', $id); pierre@1: $enabled[$node->nid][] = $chid[1]; pierre@1: } pierre@1: } pierre@1: } pierre@1: } pierre@1: } pierre@1: return $enabled[$node->nid]; pierre@1: } pierre@1: pierre@1: /** pierre@1: * Display containers and channels. pierre@1: */ pierre@1: function ad_channel_admin_overview() { pierre@1: drupal_add_css(drupal_get_path('module', 'ad_channel') .'/ad_channel.css'); pierre@1: pierre@1: $containers = _ad_channel_get_containers(); pierre@1: $rows = array(); pierre@1: if (count($containers)) { pierre@1: $header = array(t('Name'), t('Options')); pierre@1: $output = 'This action can not be undone.', array('%name' => $container->name)),
pierre@1: t('Delete'),
pierre@1: t('Cancel'));
pierre@1: }
pierre@1:
pierre@1: /**
pierre@1: * Delete a container.
pierre@1: */
pierre@1: function ad_channel_admin_confirm_delete_container_submit($form, &$form_state) {
pierre@1: $container = _ad_channel_get_containers($form_state['values']['conid']);
pierre@1: if ($container->conid) {
pierre@1: db_query('UPDATE {ad_channel} SET conid = 0 WHERE conid = %d', $container->conid);
pierre@1: db_query('DELETE FROM {ad_channel_container} WHERE conid = %d', $container->conid);
pierre@1: drupal_set_message(t('The %name container has been deleted.', array('%name' => $container->name)));
pierre@1: }
pierre@1: drupal_goto('admin/content/ad/channel');
pierre@1: }
pierre@1:
pierre@1: /**
pierre@1: * Administrative page for creating or editing channels.
pierre@1: */
pierre@1: function ad_channel_admin_channel($form_state, $chid = 0) {
pierre@1: $form = array();
pierre@1:
pierre@1: if ($chid) {
pierre@1: $channel = _ad_channel_get_channels($chid);
pierre@1: if (empty($channel)) {
pierre@1: drupal_goto('admin/content/ad/channel');
pierre@1: }
pierre@1: $form['chid'] = array(
pierre@1: '#type' => 'hidden',
pierre@1: '#value' => $chid,
pierre@1: );
pierre@1: }
pierre@1:
pierre@1: $form['name'] = array(
pierre@1: '#type' => 'textfield',
pierre@1: '#required' => TRUE,
pierre@1: '#title' => t('Channel name'),
pierre@1: '#description' => t('Enter a short, descriptive name for your channel.'),
pierre@1: '#default_value' => $chid ? $channel->name : '',
pierre@1: );
pierre@1: $form['description'] = array(
pierre@1: '#type' => 'textarea',
pierre@1: '#title' => t('Description'),
pierre@1: '#description' => t('Enter a full description of your channel.'),
pierre@1: '#default_value' => $chid ? $channel->description : '',
pierre@1: );
pierre@1: $result = db_query('SELECT conid, name FROM {ad_channel_container} ORDER BY weight ASC');
pierre@1: $containers = array(t(' This action can not be undone.', array('%name' => $channel->name)),
pierre@1: t('Delete'),
pierre@1: t('Cancel'));
pierre@1: }
pierre@1:
pierre@1: /**
pierre@1: * Delete a channel.
pierre@1: */
pierre@1: function ad_channel_admin_confirm_delete_channel_submit($form, &$form_state) {
pierre@1: $channel = _ad_channel_get_channels($form_state['values']['chid']);
pierre@1: if ($channel->chid) {
pierre@1: db_query('DELETE FROM {ad_channel} WHERE chid = %d', $channel->chid);
pierre@1: drupal_set_message(t('The %name channel has been deleted.', array('%name' => $channel->name)));
pierre@1: }
pierre@1: drupal_goto('admin/content/ad/channel');
pierre@1: }
pierre@1:
sly@2: /**
sly@2: * Available probabilities.
sly@2: */
sly@2: function _ad_channel_probabilities() {
sly@2: return array(
sly@2: 25 => t('1/4'),
sly@2: 33 => t('1/3'),
sly@2: 50 => t('1/2'),
sly@2: 100 => t('1'),
sly@2: 200 => t('2'),
sly@2: 300 => t('3'),
sly@2: 400 => t('4'),
sly@2: );
sly@2: }