--- salesforce_api.admin.inc	25 Feb 2009 23:46:02 -0000	1.2.2.3
+++ salesforce_api.admin.inc	16 Mar 2009 22:19:44 -0000
@@ -101,7 +101,6 @@ function salesforce_api_fieldmap_add_for
   $form_state['redirect'] = SALESFORCE_PATH_FIELDMAPS .'/'. $index .'/edit';
 }
 
-
 /**
  * Creates a new fieldmap in the database and returns its index.
  *
@@ -337,3 +336,121 @@ function theme_salesforce_api_fieldmap_e
   return theme('table', $header, $rows, $attributes, $caption);
 }
 
+
+/**
+ * Ask salesforce for a list of objects and display a checklist for the user.
+ * Based on user selection, set up or tear down cached/synched Salesforce data.
+ * @TODO make this more user friendly. At the moment it's possible for an admin user to blow away 
+ * their entire local SalesForce cache with a few clicks. This is not necessarily desirable.
+ *
+ * @param string $form_state 
+ * @return void
+ * @author aaron
+**/
+function salesforce_api_admin_object(&$form_state) {
+	$objects = salesforce_api_describeGlobal();
+  $data = salesforce_api_get_types(array('full' => FALSE));
+  $defaults = array_keys($data);
+	
+	if(empty($objects->types)) {
+		drupal_set_message(t('There was an error retrieving the list of SalesForce objects. Please verify that your SalesForce instance is properly configured.'), 'error');
+		return;
+	}
+	
+	$options = array_combine($objects->types, $objects->types);
+	$fields = array('objects' => array(
+    		'#type' => 'checkboxes',
+    		'#title' => 'SalesForce Objects',
+    		'#description' => 'Check the SalesForce objects you would like to synchronize locally.',
+    		'#options' => $options,
+    		'#default_value' => $defaults,
+		  ),
+		'#theme' => 'salesforce_api_object_options',
+		'submit' => array(
+		  '#type' => 'submit', 
+		  '#value' => 'Save'
+		),
+	);
+	return $fields;
+}
+
+/**
+ * FAPI submit handler 
+**/
+function salesforce_api_admin_object_submit($form, &$form_state) {
+	$values = $form_state['values']['objects'];
+	$real_types = array();
+	foreach($values as $i => $t) {
+		if(empty($t)) {
+		  $type = $form['objects']['#options'][$i];
+		  db_query("DELETE FROM {salesforce_objects} WHERE name = '%s'", $type);
+		  continue;
+		}
+		$real_types[] = $t;
+	}
+  
+  if(!empty($real_types)) {
+  	$objects = salesforce_api_describeSObjects($real_types);
+  	foreach($objects as $i => $o) {
+  		$id = null;
+  		foreach($o->fields as $j => $field) {
+  			if($field->type == 'id') {
+  				$id = $field->name;
+  				break;
+  			}
+  		}
+  		if(empty($id)) { continue; }
+  		$obj = new SObjectDefinition($o->name);
+      $obj->validate = TRUE;
+      //in this case, if the cache is empty, we want to create a new one.
+      //don't validate in this case.
+  		if(empty($obj->cache)) {
+  		  $obj->validate = FALSE;
+  		}
+  		$obj->primarykey = $id;
+  		$obj->cache = $o;
+  		if(!$obj->save()) {
+  		  $repl = array('@name' => $o->name, '@msg' => $obj->getError());
+  			form_set_error($o->name, 
+  			  t('Unable to update object cache for "@name" at this time: @msg', $repl));
+  			$_SESSION['objects_error'][$o->name] = $o->name;
+  		}
+  	}
+  }
+  
+	if(empty($_SESSION['messages']['error'])) {
+		drupal_set_message(t('SalesForce object cache has been updated.'));
+	}
+}
+
+function salesforce_api_admin_object_settings($form_state, $type) {
+  return array('settings' => array(
+    '#type' => 'markup', 
+    '#value' => 'Placeholder for per-object configuration settings.'
+    )
+  );
+}
+
+/**
+ * Theming function for @see salesforce_api_admin_setup
+ * For locally-cached SF Objects, add a "configure" or "re-map" link next to the checkbox
+**/
+function theme_salesforce_api_object_options($element = null) {
+  if(empty($element['objects']['#options'])) { return drupal_render($element); }
+  $objects = $element['objects'];
+  $options = $objects['#options'];
+  foreach($options as $i) {
+    if(empty($objects[$i]['#value'])) { continue; }
+    $link = l('configure', SALESFORCE_PATH_OBJECT.'/'.$i);
+    if($_SESSION['objects_error'][$i]) {
+      $element['objects'][$i]['#prefix'] = '<div class="error">';
+      $element['objects'][$i]['#suffix'] = '</div>';
+      unset($_SESSION['objects_error'][$i]);
+      $link = l('re-map fields', SALESFORCE_PATH_OBJECT.'/'.$i);
+    }
+    $element['objects'][$i]['#title'] .= ' | ' . $link;
+  }
+  unset($_SESSION['objects_error']);
+  return drupal_render($element);
+}
+
