You are here

google_adwords_remarketing.admin.inc in Google Adwords Remarketing 6

Same filename and directory in other branches
  1. 7 google_adwords_remarketing.admin.inc

Admin page callbacks for the Google Adwords Remarketing module. Google Adwords Remarketing Campaign aka GARC

File

google_adwords_remarketing.admin.inc
View source
<?php

/**
 * @file
 * Admin page callbacks for the Google Adwords Remarketing module.
 * Google Adwords Remarketing Campaign aka GARC
 */

/**
 * Menu callback for admin/settings/google-adwords-remarketing.
 */
function google_adwords_remarketing_admin_display() {

  // Fetch and sort GARCs
  $result = db_query("SELECT * FROM {google_adwords_remarketing}");
  while ($garc = db_fetch_array($result)) {
    $garcs[$garc['garcid']] = $garc;
  }
  if (!empty($garcs)) {
    usort($garcs, '_garc_compare');
  }
  return drupal_get_form('google_adwords_remarketing_admin_display_form', $garcs, $theme);
}

/**
 * Generate main GARCs administration form.
 */
function google_adwords_remarketing_admin_display_form(&$form_state, $garcs, $theme = NULL) {

  // If non-default theme configuration has been selected, set the custom theme.
  // @TODO implement per theme, right now it's just the theme_default thats being done.
  // global $theme_key, $custom_theme;
  // $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
  $garc_regions = array(
    GARC_REGION_DISABLED => '<' . t('Disabled') . '>',
  ) + array(
    GARC_REGION_ENABLED => '<' . t('Enabled') . '>',
  );

  // Weights range from -delta to +delta, so delta should be at least half
  // of the amount of GARCs present. This makes sure all GARCs in the same
  // region get an unique weight.
  $weight_delta = round(count($garcs) / 2);

  // Build form tree
  $form = array(
    // @TODO implement per theme, right now it's just the theme_default thats being done.

    //'#action' => arg(4) ? url('admin/settings/google-adwords-remarketing/'. $theme_key) : url('admin/settings/google-adwords-remarketing'),
    '#tree' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update the Google Adwords Remarketing Campaigns'),
  );
  if (empty($garcs)) {
    return $form;
  }
  foreach ($garcs as $i => $garc) {
    $key = $garc['garcid'];
    $form[$key]['garcid'] = array(
      '#type' => 'hidden',
      '#value' => $key,
    );
    $form[$key]['info'] = array(
      '#value' => check_plain($garc['info']),
    );
    $form[$key]['theme'] = array(
      '#type' => 'hidden',
      '#value' => $garc['theme'],
    );
    $form[$key]['weight'] = array(
      '#type' => 'weight',
      '#default_value' => $garc['weight'],
      '#delta' => $weight_delta,
    );
    $form[$key]['region'] = array(
      '#type' => 'select',
      '#default_value' => $garc['status'],
      '#options' => $garc_regions,
    );
    $form[$key]['configure'] = array(
      '#value' => l(t('configure'), 'admin/settings/google-adwords-remarketing/configure/' . $garc['garcid']),
    );
    $form[$key]['delete'] = array(
      '#value' => l(t('delete'), 'admin/settings/google-adwords-remarketing/delete/' . $garc['garcid']),
    );
  }
  return $form;
}

/**
 * Process main google adwords campaign administration form submission.
 */
function google_adwords_remarketing_admin_display_form_submit($form, &$form_state) {
  foreach ($form_state['values'] as $garc) {
    if (is_array($garc)) {
      $garc['status'] = $garc['region'];

      // update op
      drupal_write_record('google_adwords_remarketing', $garc, 'garcid');
    }
  }
  drupal_set_message(t('The Google Adwords Remarketing Campaign settings have been updated.'));

  // @TODO cache mechanism

  //cache_clear_all();
}

/**
 * Process variables for google-adwords-remarketing-admin-display.tpl.php.
 *
 * The $variables array contains the following arguments:
 * - $form
 *
 * @see google-adwords-remarketing-admin-display.tpl.php
 * @see theme_google_adwords_remarketing_admin_display()
 */
function template_preprocess_google_adwords_remarketing_admin_display_form(&$variables) {
  global $theme_key;
  $garc_regions = array(
    GARC_REGION_ENABLED => t('Enabled'),
  ) + array(
    GARC_REGION_DISABLED => t('Disabled'),
  );
  $variables['garc_regions'] = $garc_regions;
  foreach ($garc_regions as $key => $value) {

    // Initialize an empty array for the region.
    $variables['garc_listing'][$key] = array();
  }
  foreach (element_children($variables['form']) as $i) {
    $garc =& $variables['form'][$i];

    // Only take form elements that are campaigns.
    if (isset($garc['info'])) {

      // Fetch region for current garc.
      $region = $garc['region']['#default_value'];

      // Set special classes needed for table drag and drop.
      $variables['form'][$i]['region']['#attributes']['class'] = 'garc-region-select garc-region-' . $region;
      $variables['form'][$i]['weight']['#attributes']['class'] = 'garc-weight garc-weight-' . $region;
      $variables['garc_listing'][$region][$i]->row_class = isset($garc['#attributes']['class']) ? $garc['#attributes']['class'] : '';
      $variables['garc_listing'][$region][$i]->garc_modified = isset($garc['#attributes']['class']) && strpos($garc['#attributes']['class'], 'garc-modified') !== FALSE ? TRUE : FALSE;
      $variables['garc_listing'][$region][$i]->garc_title = drupal_render($garc['info']);
      $variables['garc_listing'][$region][$i]->region_select = drupal_render($garc['region']) . drupal_render($garc['theme']);
      $variables['garc_listing'][$region][$i]->weight_select = drupal_render($garc['weight']);
      $variables['garc_listing'][$region][$i]->configure_link = drupal_render($garc['configure']);
      $variables['garc_listing'][$region][$i]->delete_link = !empty($garc['delete']) ? drupal_render($garc['delete']) : '';
      $variables['garc_listing'][$region][$i]->printed = FALSE;
    }
  }
  $variables['form_submit'] = drupal_render($variables['form']);
}

/**
 * Menu callback; displays the google_adwords_remarketing configuration form.
 */
function google_adwords_remarketing_admin_configure(&$form_state, $garcid = NULL) {
  $custom_theme = variable_get('theme_default', 'garland');
  $form['garcid'] = array(
    '#type' => 'value',
    '#value' => $garcid,
  );
  $edit = db_fetch_array(db_query("SELECT info, status, visibility, pages, conversion_id, language, format, color, label, ext_js FROM {google_adwords_remarketing} WHERE garcid = '%d'", $garcid));
  $form['google_adwords_remarketing_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Google Adwords Remarketing Specific Campaign settings'),
    '#collapsible' => TRUE,
  );
  $form['google_adwords_remarketing_settings']['info'] = array(
    '#type' => 'textfield',
    '#title' => t('Campaign Adminstrative Title'),
    '#maxlength' => 64,
    '#description' => 'A brief description of your Campaign. Used on the <a href="/admin/settings/google-adwords-remarketing"> Google Adwords Remarketing overview page</a>.',
    '#default_value' => $edit['info'],
    '#weight' => -18,
    '#required' => TRUE,
  );
  $garc_status = array(
    GARC_REGION_DISABLED => '<' . t('Disabled') . '>',
  ) + array(
    GARC_REGION_ENABLED => '<' . t('Enabled') . '>',
  );
  $form['google_adwords_remarketing_settings']['status'] = array(
    '#title' => t('Status'),
    '#type' => 'select',
    '#default_value' => !isset($edit['status']) ? GARC_REGION_ENABLED : $edit['status'],
    '#options' => $garc_status,
  );
  $form['google_adwords_remarketing_settings']['theme'] = array(
    '#type' => 'hidden',
    '#default_value' => empty($edit['theme']) ? $custom_theme : $edit['theme'],
  );
  $form['google_adwords_remarketing_settings']['conversion_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Conversion ID'),
    '#maxlength' => 15,
    '#default_value' => $edit['conversion_id'],
    '#required' => TRUE,
  );
  $form['google_adwords_remarketing_settings']['language'] = array(
    '#type' => 'textfield',
    '#title' => t('Conversion Language'),
    '#maxlength' => 15,
    '#default_value' => empty($edit['language']) ? 'en' : $edit['language'],
    '#required' => TRUE,
  );
  $form['google_adwords_remarketing_settings']['format'] = array(
    '#type' => 'select',
    '#title' => t('Conversion Format'),
    '#default_value' => empty($edit['format']) ? 2 : $edit['format'],
    '#options' => _google_adwords_remarketing_conversion_formats(),
    '#required' => TRUE,
  );
  $form['google_adwords_remarketing_settings']['color'] = array(
    '#type' => 'textfield',
    '#title' => t('Conversion Color'),
    '#default_value' => empty($edit['color']) ? 'FFFFFF' : $edit['color'],
    '#size' => 10,
    '#maxlength' => 10,
    '#required' => TRUE,
  );
  $form['google_adwords_remarketing_settings']['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Conversion Label'),
    '#default_value' => $edit['label'],
    '#size' => 15,
    '#maxlength' => 255,
  );
  $form['google_adwords_remarketing_settings']['ext_js'] = array(
    '#type' => 'textfield',
    '#title' => t('External JS'),
    '#default_value' => empty($edit['ext_js']) ? GARC_DEFAULT_EXT_JS : $edit['ext_js'],
    '#size' => 50,
    '#maxlength' => 255,
    '#required' => TRUE,
  );

  // Role-based visibility settings
  $default_role_options = array();
  $result = db_query("SELECT rid FROM {google_adwords_remarketing_roles} WHERE garcid = '%d'", $garcid);
  while ($role = db_fetch_object($result)) {
    $default_role_options[] = $role->rid;
  }
  $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
  $role_options = array();
  while ($role = db_fetch_object($result)) {
    $role_options[$role->rid] = $role->name;
  }
  $form['role_vis_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Role specific visibility settings'),
    '#collapsible' => TRUE,
  );
  $form['role_vis_settings']['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Show campaign for specific roles'),
    '#default_value' => $default_role_options,
    '#options' => $role_options,
    '#description' => t('Show this campaign only for the selected role(s). If you select no roles, the campaign will be visible to all users.'),
  );
  $form['page_vis_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Page specific visibility settings'),
    '#collapsible' => TRUE,
  );
  $access = user_access('use PHP for google adwords remarketing campaign GARC visibility');
  if ($edit['visibility'] == 2 && !$access) {
    $form['page_vis_settings'] = array();
    $form['page_vis_settings']['visibility'] = array(
      '#type' => 'value',
      '#value' => 2,
    );
    $form['page_vis_settings']['pages'] = array(
      '#type' => 'value',
      '#value' => $edit['pages'],
    );
  }
  else {
    $options = array(
      t('Show on every page except the listed pages.'),
      t('Show on only the listed pages.'),
    );
    $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    ));
    if ($access) {
      $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
      $description .= ' ' . t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array(
        '%php' => '<?php ?>',
      ));
    }
    $form['page_vis_settings']['visibility'] = array(
      '#type' => 'radios',
      '#title' => t('Show campaign on specific pages'),
      '#options' => $options,
      '#default_value' => $edit['visibility'],
    );
    $form['page_vis_settings']['pages'] = array(
      '#type' => 'textarea',
      '#title' => t('Pages'),
      '#default_value' => $edit['pages'],
      '#description' => $description,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Google Adwords Remarketing Campaign'),
  );
  return $form;
}
function google_adwords_remarketing_admin_configure_validate($form, &$form_state) {
  if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {google_adwords_remarketing} WHERE garcid != %d AND info = '%s'", $form_state['values']['garcid'], $form_state['values']['info']))) {
    form_set_error('info', t('Please ensure that each Google Adwords Remarketing Campaign description is unique.'));
  }
}
function google_adwords_remarketing_admin_configure_submit($form, &$form_state) {
  if (!form_get_errors()) {

    // update op
    drupal_write_record('google_adwords_remarketing', $form_state['values'], 'garcid');
    db_query("DELETE FROM {google_adwords_remarketing_roles} WHERE garcid = %d", $form_state['values']['garcid']);
    foreach (array_filter($form_state['values']['roles']) as $rid) {
      db_query("INSERT INTO {google_adwords_remarketing_roles} (rid, garcid) VALUES (%d, %d)", $rid, $form_state['values']['garcid']);
    }
    drupal_set_message(t('This Google Adwords Remarketing Campaign: ' . $form_state['values']['info'] . ' Configuration has been saved.'));

    // @TODO cache mechanism

    //cache_clear_all();
    $form_state['redirect'] = 'admin/settings/google-adwords-remarketing';
    return;
  }
}

/**
 * Menu callback: display the custom google_adwords_remarketing addition form.
 */
function google_adwords_remarketing_add_campaign_form(&$form_state) {
  return google_adwords_remarketing_admin_configure($form_state, NULL);
}
function google_adwords_remarketing_add_campaign_form_validate($form, &$form_state) {
  if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {google_adwords_remarketing} WHERE info = '%s'", $form_state['values']['info']))) {
    form_set_error('info', t('Please ensure that each google_adwords_remarketing description is unique.'));
  }
}

/**
 * Save the new google_adwords_remarketing campaign aka garc
 */
function google_adwords_remarketing_add_campaign_form_submit($form, &$form_state) {
  drupal_write_record('google_adwords_remarketing', $form_state['values']);
  $garcid = db_last_insert_id('google_adwords_remarketing', 'garcid');

  // @TODO apply multi theme, is the approach i want?

  /*
  foreach (list_themes() as $key => $theme) {
    if ($theme->status) {
      db_query("INSERT INTO {google_adwords_remarketings} (field(s),theme) VALUES(%d)", $theme->name);
    }
  }
  */
  foreach (array_filter($form_state['values']['roles']) as $rid) {
    db_query("INSERT INTO {google_adwords_remarketing_roles} (rid, garcid) VALUES (%d, %d)", $rid, $garcid);
  }
  drupal_set_message(t('A Google Adwords Remarketing Campaign has been created.'));

  // @TODO cache mechanism

  //cache_clear_all();
  $form_state['redirect'] = 'admin/settings/google-adwords-remarketing';
  return;
}

/**
 * Menu callback; confirm deletion of a garc record.
 */
function google_adwords_remarketing_campaign_delete(&$form_state, $garcid = 0) {
  $garc = google_adwords_remarketing_campaign_get($garcid);
  $form['info'] = array(
    '#type' => 'hidden',
    '#value' => $garc['info'],
  );
  $form['garcid'] = array(
    '#type' => 'hidden',
    '#value' => $garcid,
  );
  return confirm_form($form, t('Are you sure you want to delete this Google Adwords Remarketing Campaign %name?', array(
    '%name' => $garc['info'],
  )), 'admin/settings/google-adwords-remarketing', '', t('Delete'), t('Cancel'));
}

/**
 * Deletion of a garc record
 */
function google_adwords_remarketing_campaign_delete_submit($form, &$form_state) {
  db_query('DELETE FROM {google_adwords_remarketing} WHERE garcid = %d', $form_state['values']['garcid']);
  db_query("DELETE FROM {google_adwords_remarketing_roles} WHERE garcid = %d", $form_state['values']['garcid']);
  drupal_set_message(t('This Google Adwords Remarketing Campaign %name has been removed.', array(
    '%name' => $form_state['values']['info'],
  )));

  // @TODO cache mechanism

  //cache_clear_all();
  $form_state['redirect'] = 'admin/settings/google-adwords-remarketing';
  return;
}

/**
 * Helper function for sorting garcs on admin/settings/google-adwords-remarketing.
 *
 * Active GARCS are sorted by status, then by weight.
 * Disabled garcs are sorted by name.
 */
function _garc_compare($a, $b) {

  // Separate enabled from disabled.
  $status = $b['status'] - $a['status'];
  if ($status) {
    return $status;
  }

  // Sort by weight.
  $weight = $a['weight'] - $b['weight'];
  if ($weight) {
    return $weight;
  }

  // Sort by title.
  return strcmp($a['info'], $b['info']);
}

Functions

Namesort descending Description
google_adwords_remarketing_add_campaign_form Menu callback: display the custom google_adwords_remarketing addition form.
google_adwords_remarketing_add_campaign_form_submit Save the new google_adwords_remarketing campaign aka garc
google_adwords_remarketing_add_campaign_form_validate
google_adwords_remarketing_admin_configure Menu callback; displays the google_adwords_remarketing configuration form.
google_adwords_remarketing_admin_configure_submit
google_adwords_remarketing_admin_configure_validate
google_adwords_remarketing_admin_display Menu callback for admin/settings/google-adwords-remarketing.
google_adwords_remarketing_admin_display_form Generate main GARCs administration form.
google_adwords_remarketing_admin_display_form_submit Process main google adwords campaign administration form submission.
google_adwords_remarketing_campaign_delete Menu callback; confirm deletion of a garc record.
google_adwords_remarketing_campaign_delete_submit Deletion of a garc record
template_preprocess_google_adwords_remarketing_admin_display_form Process variables for google-adwords-remarketing-admin-display.tpl.php.
_garc_compare Helper function for sorting garcs on admin/settings/google-adwords-remarketing.