### Eclipse Workspace Patch 1.0
#P vcard-6
Index: vcard.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/vcard/vcard.admin.inc,v
retrieving revision 1.1.2.2
diff -u -r1.1.2.2 vcard.admin.inc
--- vcard.admin.inc	11 Jan 2011 11:14:20 -0000	1.1.2.2
+++ vcard.admin.inc	19 Jan 2011 07:30:15 -0000
@@ -45,6 +45,112 @@
       '#options' => $options,
     );
   }
+  $form['auto_create'] = array(
+    '#type' => 'submit',
+    '#value' => t('Auto-create profile fields'),
+    '#description' => t("Attempt to create the normal required profile fields, if you haven't already done so'"),
+  );
+  $form['#submit'][] = 'vcard_auto_create_profile_submit'; 
 
   return system_settings_form($form);
 }
+
+/**
+ * Add an action to auto-create a detailed contact profile with consistent
+ * names.
+ *
+ * Capture the vcard settings submission, and act on the button if pressed.
+ *  
+ * Although I could have just over-written everything, instead I take care to
+ * absorb half-profiles that have already been made, possibly with different
+ * names, and not replace anything that already works OK. This process just adds
+ * the supplemental contact vCard fields. Thus it should be safe to run as an
+ * upgrade over anything you already have.
+ */
+function vcard_auto_create_profile_submit($form, &$form_state) {
+  if ($form_state['clicked_button']['#value'] == t('Auto-create profile fields')) {
+    drupal_set_message(t("Creating required profile fields"));
+    $required_fields = _vcard_properties();
+
+    // Load in all currently existing fields, to see what exists and what they are matched to so far.
+    $available_fields = array();
+    $available_fields_result = db_query('SELECT * FROM {profile_fields} ORDER BY weight ');
+    while ($field = db_fetch_object($available_fields_result)) {
+      // Index these on a version of the internal id, not the text label
+      $key = preg_replace('|^profile_|', '', $field->name);
+      if ($linked = variable_get('vcard_profile_'. $field->fid, "")) {
+        // This field is already linked to a vcard tag. Add it to the index in the right place
+        $key = $linked;
+      }
+      // There may be confusion if there are more than one field named the same. Don't do that.
+      if (empty($available_fields[$key])) {
+        $available_fields[$key] = $field;
+      }
+    }
+    $vcard_fields = _vcard_profile_fields();
+
+    foreach ($required_fields as $required_field_id => $required_field_label) {
+      // Check if a field with its label is already existing
+      if (in_array($required_field_id, array_keys($available_fields))) {
+        $field_def = $available_fields[$required_field_id];
+        drupal_set_message(t(
+          "<a href='!field_link'>%field_label</a> already exists as a profile field. %alt_name", 
+          array(
+            '%field_label' => $required_field_label, 
+            '!field_link'  => url('admin/user/profile/edit/'. $field_def->fid), 
+            '%alt_name'    => (($field_def->title != $required_field_label) ? " (called $field_def->title)" : "")
+          )
+        ));
+        variable_set('vcard_profile_'. $field_def->name, $required_field_id);
+        // don't let the form submission override this now...
+        unset($form_state['values']['vcard_profile_'. $field_def->name]);
+      }
+      else {
+        // need to make it
+        drupal_set_message(t("Creating profile field ") . $required_field_label);
+        // @see profile_field_form_submit()
+        $field_def = array(
+          'title'       => $required_field_label, 
+          'name'        => 'profile_'. $required_field_id, 
+          'explanation' => '', 
+          'category'    => 'Contact Information', 
+          'type'        => 'textfield', 
+          'weight'      => 0, 
+          'required'    => 0, 
+          'register'    => 1, 
+          'visibility'  => 2,
+        );
+        if ($required_field_id == 'url') {$field_def['type']='url';}
+        if ($required_field_id == 'birthday') {$field_def['type']='date';}
+
+        drupal_write_record('profile_fields', $field_def);
+        // retrieve its new ID
+        $result = db_query('SELECT * FROM {profile_fields} WHERE name = "%s"', $field_def['name']);
+        $field_def = db_fetch_object($result);
+      }
+      // Now check the right profile field is matched up with the right vcard field
+      if ($field_def && (variable_get('vcard_profile_'. $field_def->fid, '') != $required_field_id)) {
+        drupal_set_message(t(
+          "Assigning profile field %fid (<a href='!field_link'>%title</a>) to vcard key %required_field_id", 
+          array(
+            '%fid' => $field_def->fid,
+            '%title' => $field_def->title,
+            '%required_field_id' => $required_field_id,
+            '!field_link' => url('admin/user/profile/edit/'. $field_def->fid)
+          )
+        ));
+        variable_set('vcard_profile_'. $field_def->name, $required_field_id);
+        // don't let the form submission override this now...
+        unset($form_state['values']['vcard_profile_'. $field_def->name]);
+      }
+    }
+    drupal_set_message(t("
+      Created profile fields! You may want to review the profile settings
+      individually to ensure their display order and public visibility 
+      settings are right for you. 
+      By default they are all displayed on the users profile page.  
+    "));
+  }
+  cache_clear_all();
+  menu_rebuild();
+}
Index: vcard.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/vcard/vcard.module,v
retrieving revision 1.17.2.15
diff -u -r1.17.2.15 vcard.module
--- vcard.module	12 Jan 2011 10:23:51 -0000	1.17.2.15
+++ vcard.module	19 Jan 2011 07:30:15 -0000
@@ -71,6 +71,30 @@
           '!microformat' => l('microformat', 'http://microformats.org'),
         )
       );
+      break;
+    case 'admin/user/profile':
+      return t("<p>
+        For assistance setting up a default profile with common fields, visit
+        <a href='!vcard_settings_link'>the VCard settings</a> which can preset an automatic profile definition.
+      </p><p>
+        To enable machine-readable <a href='!microformat_link'>microformats</a>
+        for your profiles, see the <a href='!vcard_help_link'>vCard help</a> and README. 
+      </p>",
+      array(
+        '!vcard_settings_link' => url('admin/settings/vcard'),
+        '!vcard_help_link' => url('admin/help/vcard'),
+        '!microformat_link' => url('http://microformats.org'),
+      )
+    );
+    case 'admin/settings/vcard':
+      return t("
+        Profile fields are added and administered on
+        <a href='!profile_settings_link'>the Profile Configuration page</a>.
+      ",
+      array(
+        '!profile_settings_link' => url('admin/user/profile'),
+      )
+    );
   }
 }
 
