pierre@0: . pierre@0: */ pierre@0: pierre@0: /** pierre@0: * Implementation of hook_perm(). pierre@0: */ pierre@0: function ad_remote_perm() { pierre@0: return array('host remote advertisements'); pierre@0: } pierre@0: pierre@0: /** pierre@0: * Implementation of hook_menu(). pierre@0: */ pierre@0: function ad_remote_menu() { pierre@0: $items = array(); pierre@0: pierre@0: $items['admin/content/ad/ad_remote'] = array( pierre@0: 'title' => 'Remote ads', pierre@0: 'page callback' => 'drupal_get_form', pierre@0: 'page arguments' => array('ad_remote_form'), pierre@0: 'access arguments' => array('host remote advertisements'), pierre@0: 'type' => MENU_LOCAL_TASK, pierre@0: 'weight' => 1, pierre@0: ); pierre@0: return $items; pierre@0: } pierre@0: pierre@0: /** pierre@0: * A simple page providing source snippets for displaying ads on remote pierre@0: * websites. When form is being submitted, it rebuilds with needed code snippet. pierre@0: */ pierre@0: function ad_remote_form($form_state) { pierre@0: global $user; pierre@0: pierre@0: $form['settings'] = array( pierre@0: '#type' => 'fieldset', pierre@0: '#title' => t('Settings'), pierre@0: '#description' => t('Use the following options to build a source snippet for displaying ads on your website.'), pierre@0: '#collapsible' => TRUE, pierre@0: '#collapsed' => isset($form_state['values']['group']), pierre@0: '#weight' => -1, pierre@0: ); pierre@0: pierre@0: $form['settings']['group'] = taxonomy_form(_ad_get_vid(), 0, t('Select one or more groups to display ads from.')); pierre@0: $form['settings']['group']['#default_value'] = isset($form_state['values']['group']) ? $form_state['values']['group'] : ''; pierre@0: pierre@0: if (isset($form_state['values']['quantity'])) { pierre@0: // Sanity check, be sure quantity is an integer. pierre@0: $quantity = (int)$form_state['values']['quantity']; pierre@0: } pierre@0: if (!isset($quantity)) { pierre@0: // Must display at least one advertisement. pierre@0: $quantity = 1; pierre@0: } pierre@0: pierre@0: $form['settings']['quantity'] = array( pierre@0: '#type' => 'select', pierre@0: '#title' => t('Quantity'), pierre@0: '#options' => drupal_map_assoc(array(1,2,3,4,5,6,7,8,9,10,15,20,25,50)), pierre@0: '#default_value' => $quantity, pierre@0: '#description' => t('Select the maximum number of unique ads that should be displayed together.'), pierre@0: ); pierre@0: pierre@0: if (isset($form_state['values']['group'])) { pierre@0: $form['code'] = array( pierre@0: '#type' => 'fieldset', pierre@0: '#title' => t('Code snippet'), pierre@0: '#description' => t('Insert the following source snippet into your web page to display ads hosted on this web site. Include the entire snippet, and do not modify it in any way.'), pierre@0: ); pierre@0: pierre@0: $hostid = ad_host_id_create($user->uid); pierre@0: $group = NULL; pierre@0: if (is_array($form_state['values']['group']) && !empty($form_state['values']['group'])) { pierre@0: if (isset($form_state['values']['group'][0]) && $form_state['values']['group'][0] == 0) { pierre@0: unset($form_state['values']['group'][0]); pierre@0: } pierre@0: $group = implode(',', $form_state['values']['group']); pierre@0: // Sanity check, be sure group is only numbers and commas. pierre@0: $group = preg_replace('/[^0-9,]/', '', $group); pierre@0: } pierre@0: if (!$group) { pierre@0: $group = 0; pierre@0: } pierre@0: pierre@0: $output = ''. ad($group, $quantity, array('raw' => 1, 'hostid' => $hostid)) .''; pierre@0: $form['code']['snippet'] = array( pierre@0: '#type' => 'textarea', pierre@0: '#value' => $output, pierre@0: '#attributes' => array( pierre@0: 'onclick' => 'this.select();', pierre@0: 'onfocus' => 'this.select();', pierre@0: ), pierre@0: ); pierre@0: } pierre@0: pierre@0: $form['submit'] = array( pierre@0: '#type' => 'submit', pierre@0: '#value' => t('Generate code snippet'), pierre@0: ); pierre@0: pierre@0: return $form; pierre@0: } pierre@0: pierre@0: /** pierre@0: * Form validator. pierre@0: */ pierre@0: function ad_remote_form_validate($form, &$form_state) { pierre@0: if (empty($form_state['values']['group'])) { pierre@0: form_set_error('group', t('At least one group should be selected')); pierre@0: } pierre@0: } pierre@0: /** pierre@0: * Tell the form to rebuild. pierre@0: */ pierre@0: function ad_remote_form_submit($form, &$form_state) { pierre@0: $form_state['rebuild'] = TRUE; pierre@0: } pierre@0: