Index: legal.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/legal/legal.module,v
retrieving revision 1.11
diff -u -F^f -r1.11 legal.module
--- legal.module	26 Nov 2005 17:46:04 -0000	1.11
+++ legal.module	7 May 2006 18:09:26 -0000
@@ -55,17 +55,17 @@ function legal_menu($may_cache) {
       'access' => user_access('view Terms and Conditions'),
       'type' => MENU_CALLBACK);
   }
-  
+
   return $items;
 }
 
 function legal_page() {
 
     $conditions = legal_get_conditions();
-    
+
     // TO DO
     // convert all display formats to HTML
-    $output = theme('legal_account_form', $conditions['conditions'], $conditions['display'], $accepted, $read_only, NULL, FALSE);
+    $output = theme('legal_account_form',$conditions['conditions'], 2, $accepted, $read_only, NULL, FALSE);
     print theme('page', $output);
 }
 
@@ -102,19 +102,43 @@ function legal_page_admin() {
   } else {
     $output .= t('Terms & Conditions will not be shown to users, as no T&C have been saved.');
   }
-  
-  $output .= form_textarea(t('Terms & Conditions'), 'conditions', $conditions['conditions'], 70, 6, t('Your Terms & Conditions'), '', TRUE);
-  $output .= form_radios(t('Display Style'), 'display', $conditions['display'], array(t('Scroll Box'), t('Scroll Box (CSS)'), t('HTML Text')), t('How terms & conditions should be displayed to users.'), TRUE);
-  $output .= form_submit(t('Preview'));
-  $output .= form_submit(t('Save'));
-  
-  print theme('page', form($output));
-  return;
+
+$form['conditions'] = array(
+  '#type' => 'textarea',
+  '#title' => t('Terms & Conditions'),
+  '#default_value' => $conditions['conditions'],
+  '#cols' => 70,
+  '#rows' => 6,
+  '#description' => t('Your Terms & Conditions'),
+  '#attributes' => '',
+  '#required' => TRUE,
+);
+
+$form['display'] = array(
+  '#type' => 'radios',
+  '#title' => t('Display Style'),
+  '#default_value' => ($conditions['display'])?$conditions['display']:$edit['display'],
+  '#options' => array(t('Scroll Box'), t('Scroll Box (CSS)'), t('HTML Text')),
+  '#description' => t('How terms & conditions should be displayed to users.'),
+  '#required' => TRUE,
+);
+
+$form['preview'] = array(
+  '#type' => 'submit',
+  '#value' => t('Preview'),
+);
+
+$form['save'] = array(
+  '#type' => 'submit',
+  '#value' => t('Save'),
+);  
+  $output = drupal_get_form('legal_page_admin', $form);
+  return $output;
 }
 
 
 function legal_user($type, &$edit, &$user, $category = FALSE) {
-
+//echo "\n<br/> LEGAL_USER : [type:$type][edit:".var_export($edit,true)."][category:$category]";
     $edit['conditions'] = NULL;
     $account = $user;
     global $user;
@@ -123,84 +147,83 @@ function legal_user($type, &$edit, &$use
     if ($conditions['conditions'] == NULL) {
       return array();
     } 
-    
+
     switch ($type) {
       case 'register':
-      
-        $form = theme('legal_account_form', $conditions['conditions'], $conditions['display'], $edit['legal_accept']);
-        return array(array('title' => t('Terms and Conditions of Use'), 'data' => $form, 'weight' => 0));
-        
+
+        $form['legal'] = array(
+            '#type' => 'fieldset',
+            '#title' => t('Terms and Conditions of Use')
+        );
+        foreach( legal_account_form( $conditions['conditions'], $conditions['display'], $edit['legal_accept']) as $key => $value ) {
+          $form['legal'][$key] = $value;
+        }
+        return $form;
+
       case 'form':
-        
-        if ($category == 'account') {
-            $legal_account= legal_get_accept($account->uid);
-    
-            // User is not account owner - disable checkbox
-            if ($account->uid != $user->uid) {
-                $read_only = array('disabled' => 'disabled');
-            }
-            
-            // T&C has already been accepted - disable checkbox
-            if ($legal_account->uid) {
-                $read_only = array('disabled' => 'disabled');
-                $accepted = 1;
+
+          if ( $account->uid == $user->uid ) { // looking at your  own form
+            if ( !legal_get_accept($account->uid) ) {
+              // somehow you are looking at your own user form but you haven't accepted the TOS (It's time to pay the piper)
+              $form['legal'] = array(
+                  '#type' => 'fieldset',
+                  '#title' => t('Terms and Conditions of Use')
+              );
+              foreach( legal_account_form( $conditions['conditions'], $conditions['display'], $edit['legal_accept']) as $key => $value ) {
+                $form['legal'][$key] = $value;
+              }
+              return $form;
+
             }
-            
-            $form = theme('legal_account_form', $conditions['conditions'], $conditions['display'], $accepted, $read_only);
-            return array(array('title' => t('Terms and Conditions of Use'), 'data' => $form, 'weight' => 0));
-        }
-        
+          }
+          break;
+
       case 'validate':
         // User account owner?
         // Unregistered user?
         if ($account->uid == $user->uid || $user->uid == 0) {
-            
+
             // if already accepted skip validation
             $legal_account= legal_get_accept($account->uid);
             if ($legal_account->uid) break;
-            
             if ($edit['legal_accept'] != '1' && $category == 'account') {
               form_set_error('legal_accept', t('You must <strong>accept</strong> the Terms & Conditions of Use.'));
             }
-            
+
         }
         break;
-       
+
     case 'update':
     case 'insert':
-        
         $edit['legal_accept'] = NULL;
         // if already accepted skip data entry
         $legal_account= legal_get_accept($account->uid);
         if ($legal_account->uid) break;
-        
+
         if ($account->uid == $user->uid || $user->uid == 0) {
             legal_save_accept($account->uid, $conditions['tc_id']);
         }
         break;
     }
-    
+
     return;
   }
   
 function legal_get_accept($uid) {
-
     $accept = db_fetch_object(db_query("SELECT * FROM {legal_accepted} WHERE uid = '%d' ORDER BY legal_id DESC LIMIT 1", $uid));
     return $accept;
 }
 
 
 function legal_save_accept($uid, $tc_id) {
-
   db_query("INSERT INTO {legal_accepted} (legal_id, uid, tc_id, accepted) VALUES (NULL, '%d', '%d', '%d')", $uid, $tc_id, time());
-
   return;
 }
 
 function legal_save_conditions($conditions) {
 
     variable_set('legal_display', $conditions['display']);
-    
+
     // If new conditions are different from current permisions - enter in database
     $current_conditions = db_result(db_query("SELECT conditions FROM {legal_conditions} ORDER BY tc_id DESC LIMIT 1"));
     if ($current_conditions != $conditions['conditions']) {
@@ -211,36 +234,63 @@ function legal_save_conditions($conditio
 }
 
 function legal_get_conditions() {
-
     $conditions = db_fetch_array(db_query("SELECT * FROM {legal_conditions} ORDER BY tc_id DESC LIMIT 1"));
     $conditions['display'] = variable_get('legal_display', '0');
     return $conditions;
 }
 
 function legal_validate_conditions($edit) {
-
   if (!$edit['conditions']) form_set_error('conditions', t('Terms & Conditions must be entered.'));
   return;
 }
 
 function theme_legal_account_form($conditions, $display_style, $accept, $read_only = '', $preview = '', $include_checkbox = TRUE) {
-    
-    switch ($display_style) {
-        case 1:
-            $path = drupal_get_path('module', 'legal');
-            drupal_set_html_head('<style type="text/css" media="all">@import "' . $path . '/legal.css";</style>');
-            $output = '<div class="legal-terms">' . $conditions . '</div>';
-            break;
-        case 2:
-            $output = $conditions;
-            break;
-        default:
-            $output = form_textarea('', 'conditions' . $preview, $conditions, 70, 10, '', array('readonly' => '', 'wrap' => 'virtual'));
-    }
-    
-    if ($include_checkbox) $output .= form_checkbox(t('<strong>Accept</strong> Terms & Conditions of Use'), 'legal_accept', 1, $accept, '', $read_only, TRUE);
-    
-    return $output;
+  $legal = legal_account_form($conditions, $display_style, $accept, $read_only, $preview, $include_checkbox);
+	return $legal['#value'];
+} 
+
+function legal_account_form($conditions, $display_style, $accept, $read_only = '', $preview = '', $include_checkbox = TRUE) {
+//echo "\n<br/> legal_accout_form : [conditions:$conditions]";
+  switch ($display_style) {
+    case 1:
+      $path = drupal_get_path('module', 'legal');
+      drupal_set_html_head('<style type="text/css" media="all">@import "' . $path . '/legal.css";</style>');
+      $form = array(
+        '#type' => 'markup', 
+        '#value' => '<div style="height: 20em;overflow: scroll;">' . $conditions . '</div>'
+      );
+      break;
+    case 2:
+      $form = array(
+        '#type' => 'markup',
+        '#value' => $conditions
+      );
+      break;
+    default:
+        $form['conditions' . $preview] = array(
+          '#type' => 'textarea',
+          '#title' => '',
+          '#default_value' => $conditions,
+          '#cols' => 70,
+          '#rows' => 10,
+          '#description' => '',
+          '#attributes' => array('readonly' => '', 'wrap' => 'virtual'),
+        );
+  }
+
+  if ($include_checkbox) {
+    $form['legal_accept'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('<strong>Accept</strong> Terms & Conditions of Use'),
+      '#return_value' => 1,
+      '#default_value' => $accept,
+      '#description' => '',
+      '#attributes' => (bool)$read_only?array('readonly'=>''):'',
+      '#required' => TRUE,
+    );
+  }
+  return $form;
+
 }
 
 ?>
\ No newline at end of file
