dfp.install in Doubleclick for Publishers (DFP) 7
Same filename and directory in other branches
Installation file for DFP module.
File
dfp.installView source
<?php
/**
* @file
* Installation file for DFP module.
*/
/**
* Implements hook_enable().
*/
function dfp_enable() {
$t = get_t();
if (!module_exists('token')) {
drupal_set_message($t('While it is not required, the Doubleclick for Publishers administration forms are vastly improved by using the <a href="http://drupal.org/project/token">token module</a>.'));
}
}
/**
* Implements hook_schema().
*/
function dfp_schema() {
$schema['dfp_tags'] = array(
'description' => 'Stores information about DFP ad tags.',
'export' => array(
'key' => 'machinename',
'primary key' => 'adid',
'identifier' => 'tag',
// Exports will be available as $tag
'default hook' => 'default_dfp_tag',
// Function hook name.
'api' => array(
'owner' => 'dfp',
'api' => 'default_dfp_tags',
// Base name for api include files.
'minimum_version' => 1,
'current_version' => 1,
),
'load callback' => 'dfp_tag_load',
),
'fields' => array(
'adid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
'no export' => TRUE,
),
'machinename' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => "The tag's unique name.",
),
'slot' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => "The ad slot's name.",
),
'adunit' => array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => '',
'description' => "The ad unit name.",
),
'size' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => "The tag's size(s).",
),
'block' => array(
'type' => 'int',
'default' => 1,
'description' => 'If true, a block will be created in which to display this ad tag.',
),
'settings' => array(
'type' => 'blob',
'description' => "Serialized array of settings.",
'size' => 'big',
'serialize' => TRUE,
),
),
'primary key' => array(
'adid',
),
'unique keys' => array(
'machinename' => array(
'machinename',
),
),
);
return $schema;
}
/**
* Implements hook_requirements().
*/
function dfp_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
if (variable_get('dfp_token_cache_enabled', 0)) {
if (!function_exists('entity_modified_last')) {
$requirements['dfp_token_cache'] = array(
'title' => t('DFP dependency'),
'value' => t('DFP token cache requires Entity Modified module.'),
'description' => t('Please install <a href="@module">Entity Modified</a> module or disable the option.', array(
'@module' => 'https://www.drupal.org/project/entity_modified',
)),
'severity' => REQUIREMENT_ERROR,
);
}
}
}
return $requirements;
}
/**
* Implements hook_install().
*/
function dfp_install() {
// Migrate data from the DART module if possible.
dfp_update_7000();
$vocabulary_machinename = 'dfp_ad_categories';
// If it doesn't already exist, create a vocabulary so that terms can be
// targeted in groups.
$existing_vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machinename);
if (!$existing_vocabulary) {
taxonomy_vocabulary_save((object) array(
'name' => 'DFP Ad Categories',
'machine_name' => $vocabulary_machinename,
));
}
// Create a new term_reference field for the new vocabulary.
if (field_read_field('field_dfp_ad_categories') === FALSE) {
$field = array(
'field_name' => 'field_dfp_ad_categories',
'type' => 'taxonomy_term_reference',
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $vocabulary_machinename,
'parent' => 0,
),
),
),
);
field_create_field($field);
}
}
/**
* Implements hook_uninstall().
*/
function dfp_uninstall() {
// Delete variables.
variable_del('dfp_set_centering');
variable_del('dfp_token_cache_enabled');
variable_del('dfp_token_cache_lifetime');
}
/**
* Migrate existing DART tags to DFP tags. Note: this migration will destroy
* the any DFP global settings that have already been set.
*/
function dfp_update_7000() {
$t = get_t();
if (module_exists('dart')) {
// Migrate global settings.
$network_id = variable_get('dart_global_network_id', '');
if (!empty($network_id)) {
variable_set('dfp_network_id', $network_id);
}
$slug = variable_get('dart_global_slug', '');
if (!empty($slug)) {
variable_set('dfp_default_slug', $slug);
}
$adunit = array();
$adunit[] = variable_get('dart_global_prefix', '');
$adunit[] = variable_get('dart_global_site', '');
$adunit[] = variable_get('dart_global_zone', '');
$adunit = array_filter($adunit);
if (!empty($adunit)) {
variable_set('dfp_default_adunit', implode('/', $adunit));
}
$js = variable_get('dart_js', '');
if (!empty($js)) {
variable_set('dfp_injected_js', $js);
}
$asyncronous = variable_get('dart_load_last', NULL);
if (!is_null($asyncronous)) {
variable_set('dfp_async_rendering', $asyncronous);
}
// Migrate DART tags to DFP tags.
if (db_table_exists('dart_tags')) {
$dart_tags_query = db_select('dart_tags', 'dt')
->fields('dt')
->execute();
while ($tag = $dart_tags_query
->fetchObject()) {
$tag->settings = unserialize($tag->settings);
$fields = array(
'machinename' => (string) $tag->machinename,
'slot' => (string) $tag->pos,
'size' => (string) $tag->sz,
'block' => (int) $tag->block,
);
$adunit = array();
if (isset($tag->settings['overrides']['site'])) {
$adunit[] = $tag->settings['overrides']['site'];
}
if (isset($tag->settings['overrides']['zone'])) {
$adunit[] = $tag->settings['overrides']['zone'];
}
$fields['adunit'] = !empty($adunit) ? implode('/', $adunit) : '';
$settings = array();
if (isset($tag->settings['overrides']['slug'])) {
$settings['slug'] = $tag->settings['overrides']['slug'];
}
if (isset($tag->settings['options']['scriptless'])) {
$settings['short_tag'] = $tag->settings['options']['scriptless'];
}
$settings['targeting'] = array();
foreach ($tag->settings['key_vals'] as $kv) {
$settings['targeting'][] = array(
'target' => $kv['key'],
'value' => $kv['val'],
);
}
$fields['settings'] = serialize($settings);
$insert_tag_query = db_insert('dfp_tags')
->fields($fields)
->execute();
}
}
return $t('All DART tags and settings have been migrated to coresponding DFP tags and settings. It is highly recomended that you uninstall the DART module.');
}
}
/**
* The network id is now included automatically in the ad unit pattern therefore
* existing tags need the ad unit id token removed so it doesnt appear twice.
*/
function dfp_update_7001() {
$t = get_t();
$tags = db_select('dfp_tags', 'd')
->fields('d')
->execute();
foreach ($tags as $tag) {
if (strpos($tag->adunit, '[dfp_tag:network_id]/') === 0) {
$tag->adunit = str_replace('[dfp_tag:network_id]/', '', $tag->adunit);
$record = array(
'adid' => $tag->adid,
'adunit' => $tag->adunit,
);
drupal_write_record('dfp_tags', $record, 'adid');
}
}
return $t('The [dfp_tag:network_id] token has been removed from the beginning of the Ad Unit Pattern for all tags because it is now automatically included.');
}
/**
* Fix a typo in the name of the DFP Ad Categories vocabulary.
*/
function dfp_update_7002() {
$vocabulary_machinename = 'dfp_ad_categories';
$existing_vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machinename);
if ($existing_vocabulary && $existing_vocabulary->name == 'DFP Ad Catergories') {
$existing_vocabulary->name = 'DFP Ad Categories';
taxonomy_vocabulary_save($existing_vocabulary);
}
}
/**
* Prepend ths NetworkID with a "/" as per Google's recommendations.
*/
function dfp_update_7003() {
$current_network_id = variable_get('dfp_network_id', '');
if (!empty($current_network_id) && strpos($current_network_id, "/") !== 0) {
variable_set('dfp_network_id', "/" . $current_network_id);
drupal_set_message(t('Your network ID has been prepended with a "/" to match Google\'s recommendations. It\'s possible this could effect your reporting on DFP so if you are not certain this change is right for your site, you should remove the "/" by editing the network id on the <a href="admin/structure/dfp_ads/settings">global DFP settings form</a>.'), 'warning');
}
}
/**
* Add the default setting for slug placement
*/
function dfp_update_7004() {
variable_set('dfp_slug_placement', 0);
}
Functions
Name | Description |
---|---|
dfp_enable | Implements hook_enable(). |
dfp_install | Implements hook_install(). |
dfp_requirements | Implements hook_requirements(). |
dfp_schema | Implements hook_schema(). |
dfp_uninstall | Implements hook_uninstall(). |
dfp_update_7000 | Migrate existing DART tags to DFP tags. Note: this migration will destroy the any DFP global settings that have already been set. |
dfp_update_7001 | The network id is now included automatically in the ad unit pattern therefore existing tags need the ad unit id token removed so it doesnt appear twice. |
dfp_update_7002 | Fix a typo in the name of the DFP Ad Categories vocabulary. |
dfp_update_7003 | Prepend ths NetworkID with a "/" as per Google's recommendations. |
dfp_update_7004 | Add the default setting for slug placement |