Index: ad.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ad/ad.module,v
retrieving revision 1.2.2.29.2.83.2.16.2.1
diff -u -r1.2.2.29.2.83.2.16.2.1 ad.module
--- ad.module 23 Feb 2009 22:39:02 -0000 1.2.2.29.2.83.2.16.2.1
+++ ad.module 25 Feb 2009 00:26:40 -0000
@@ -541,12 +541,6 @@
'module' => 'ad',
'description' => t('Advertisements can be randomly displayed to visitors of your website.'),
);
-
- $adtypes = ad_get_types('data');
- // New 'type' notation returns everything we need to define content type
- foreach ($adtypes as $type => $data) {
- $items['ad/'. $type] = $data;
- }
return $items;
}
@@ -567,15 +561,13 @@
/**
* Implementation of hook_form().
*/
-function ad_form($node, &$form_state) {
+function ad_form(&$node, &$form_state) {
$form = array();
- $type = arg(3);
- if (function_exists('ad_'. $type .'_display_ad')) {
- $adtype = $type;
- }
- else {
- $adtype = isset($node->adtype) ? $node->adtype : '';
+ // 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(
@@ -608,17 +600,45 @@
}
}
+ // display ad type switch
+ if (!isset($node->adtype) || isset($node->adtype_select)) {
+ $adtypes = ad_get_types('name');
+ $form['select'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Select Ad type'),
+ '#prefix' => '
',
+ '#suffix' => '
',
+ '#weight' => 3,
+ );
+ $form['select']['adtype_select'] = array(
+ '#type' => 'select',
+ '#options' => $adtypes,
+ '#default_value' => $node->adtype_select,
+ );
+ $form['select']['adtype_submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Select'),
+ '#validate' => array('ad_select_adtype'),
+ '#ahah' => array(
+ 'path' => 'node/add/ad/ahah',
+ 'wrapper' => 'adtype-ahah-wrapper',
+ ),
+ );
+ }
// display type-specific options
- if (isset($adtype)) {
- $elements = module_invoke('ad_'. $adtype, 'adapi', 'form', $node);
- if (is_array($elements)) {
- foreach ($elements as $element => $values) {
- $form[$element] = $values;
- }
+ if (isset($node->adtype)) {
+ if (isset($node->adtype_select) && $node->adtype_select != $node->adtype) {
+ $node->adtype = $node->adtype_select;
}
- $form['adtype'] = array(
- '#type' => 'hidden',
- '#value' => $adtype,
+ ad_form_add_adtype_elements($form, $node->adtype, $node);
+ // add ahah wrapper
+ $form['adtype_elements']['#prefix'] = '';
+ $form['adtype_elements']['#suffix'] = '
';
+ }
+ if (!isset($form['adtype_elements'])) {
+ $form['adtype_elements'] = array(
+ '#value' => '',
+ '#weight' => 3.1,
);
}
@@ -626,7 +646,8 @@
$form['adstatus'] = array(
'#type' => 'fieldset',
'#title' => t('Status'),
- '#collapsible' => TRUE
+ '#collapsible' => TRUE,
+ '#weight' => 4,
);
// display all available status options
foreach (ad_status_array($node->nid, $node->adstatus) as $status => $description) {
@@ -656,6 +677,7 @@
isset($node->maxclicks) ||
isset($form_state['values']['maxclicks']))
? FALSE : TRUE,
+ '#weight' => 5,
);
if (ad_permission($node->nid, 'manage status')) {
@@ -715,49 +737,56 @@
'#value' => isset($node->autoexpire) ? $node->autoexpire : 0,
);
}
+
+ $form['#validate'][] = 'ad_select_adtype';
return $form;
}
/**
+ * Ad type switch submit handler.
+ */
+function ad_select_adtype(&$form, &$form_state) {
+ if (!isset($form_state['values']['adtype']) || isset($form_state['values']['adtype_select']) && $form_state['values']['adtype'] != $form_state['values']['adtype_select']) {
+ $form_state['adtype'] = $form_state['adtype_select'];
+ $form_state['rebuild'] = TRUE;
+ }
+}
+
+function ad_form_ahah() {
+ $form_state = array('storage' => NULL, 'submitted' => FALSE);
+ $form_build_id = $_POST['form_build_id'];
+ $form = form_get_cache($form_build_id, $form_state);
+ ad_form_add_adtype_elements($form, $_POST['adtype_select']);
+ form_set_cache($form_build_id, $form, $form_state);
+ $form += array(
+ '#post' => $_POST,
+ '#programmed' => FALSE,
+ );
+ // Rebuild the form.
+ $form = form_builder($_POST['form_id'], $form, $form_state);
+ $output = drupal_render($form['adtype_elements']);
+ drupal_json(array(
+ 'status' => TRUE,
+ 'data' => $output,
+ ));
+}
+
+function ad_form_add_adtype_elements(&$form, $adtype, $node = NULL) {
+ unset($form['adtype_elements']);
+ $form['adtype_elements'] = module_invoke('ad_'. $adtype, 'adapi', 'form', $node);
+ $form['adtype'] = array(
+ '#type' => 'hidden',
+ '#value' => $adtype,
+ );
+ $form['adtype_elements']['#weight'] = 3.1;
+}
+
+
+/**
* Implementation of hook_form_alter().
*/
function ad_form_alter(&$form, &$form_state, $form_id) {
- if ($form_id == 'ad_node_form') {
- $adtypes = ad_get_types('data');
- if (isset($form['adtype']) && isset($form['adtype']['#value'])) {
- $adtype = $form['adtype']['#value'];
- if (isset($adtypes[$adtype])) {
- // Valid advertisement type selected.
- return;
- }
- }
- if (sizeof($adtypes) == 1) {
- // Auto select the appropriate advertisement type.
- drupal_goto('node/add/ad/'. key($adtypes));
- }
- else {
- foreach ($adtypes as $key => $adtype) {
- $out = ''. l($adtype['name'], "node/add/ad/$key", array('title' => t('Add a !key', array('!key' => $adtype['name'])))) .'';
- $out .= ''. $adtype['description'] .'';
- $item[$key] = $out;
- }
- if (isset($item)) {
- $output = t('Choose from the following advertisement types:');
- $output .= ''. implode('', $item) .'
';
- }
- else {
- $output = t('You are not allowed to create advertisements.');
- }
-
- $form = array();
- $form['select'] = array(
- '#value' => $output,
- );
- $form['#tree'] = FALSE;
- $form['#programmed'] = FALSE;
- }
- }
if ($form_id == 'taxonomy_form_vocabulary') {
// Remove taxonomy form options not applicable for ad groups.
if ($form['vid']['#value'] == _ad_get_vid()) {
@@ -976,6 +1005,11 @@
'weight' => 3,
'file' => 'ad.admin.inc',
);
+ $items['node/add/ad/ahah'] = array(
+ 'access arguments' => array('create advertisements'),
+ 'page callback' => 'ad_form_ahah',
+ 'type' => MENU_CALLBACK,
+ );
ad_menu_add_global_settings($items);
Index: text/ad_text.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ad/text/ad_text.module,v
retrieving revision 1.2.2.7.2.24.2.13.2.1
diff -u -r1.2.2.7.2.24.2.13.2.1 ad_text.module
--- text/ad_text.module 23 Feb 2009 22:39:05 -0000 1.2.2.7.2.24.2.13.2.1
+++ text/ad_text.module 24 Feb 2009 17:13:39 -0000
@@ -199,9 +199,7 @@
break;
case 'update':
- if (ad_adaccess($node, 'manage ad text')) {
db_query("UPDATE {ad_text} SET url = '%s', adheader = '%s', adbody = '%s' WHERE aid = %d", $node->url, $node->adheader, $node->adbody, $node->nid);
- }
break;
case 'delete':