소스 검색

Merge branch '6.x-1.x' of git.drupal.org:sandbox/spficklin/1337878 into 6.x-1.x

spficklin 12 년 전
부모
커밋
79b2d186ac
1개의 변경된 파일88개의 추가작업 그리고 4개의 파일을 삭제
  1. 88 4
      tripal_bulk_loader/tripal_bulk_loader.admin.templates.inc

+ 88 - 4
tripal_bulk_loader/tripal_bulk_loader.admin.templates.inc

@@ -1294,7 +1294,7 @@ function tripal_bulk_loader_add_template_field_form(&$form_state = NULL) {
 
   $form['add_fields']['submit-add_field'] = array(
       '#type' => 'submit',
-      '#value' => 'Add Field'
+      '#value' => 'Save Changes'
   );
 
   $form['add_fields']['submit-cancel'] = array(
@@ -1305,6 +1305,23 @@ function tripal_bulk_loader_add_template_field_form(&$form_state = NULL) {
   return $form;
 }
 
+function tripal_bulk_loader_add_template_field_form_validate($form, $form_state) {
+
+  // Don't worry about validation when Cancel button is clicked
+  if ($form_state['clicked_button']['#value'] == 'Save Changes') {
+    $is_unique = tripal_bulk_loader_is_record_name_unique(
+      $form_state['values']['record_name'],
+      $form_state['values']['template_id'],
+      $form_state['storage']['template_array']
+    );
+    $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
+    if ((!$is_unique) AND $new_record) {
+      form_set_error('record_name', "New Record Name must be unique. '".$form_state['values']['record_name']."' is not unique.");
+    }
+  }
+
+}
+
 /**
  * Add Field Submit
  *
@@ -1318,7 +1335,7 @@ function tripal_bulk_loader_add_template_field_form_submit($form, &$form_state)
   $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
 
   if (!$form_state['ahah_submission']) {
-    if ($op ==  'Add Field') {
+    if ($op ==  'Save Changes') {
 
       $template = $form_state['storage']['template_array'];
 
@@ -1903,7 +1920,7 @@ function tripal_bulk_loader_edit_template_field_form(&$form_state = NULL) {
 
   $form['edit_fields']['submit-edit_field'] = array(
       '#type' => 'submit',
-      '#value' => 'Edit Field'
+      '#value' => 'Save Changes'
   );
 
   $form['edit_fields']['submit-cancel'] = array(
@@ -1914,6 +1931,23 @@ function tripal_bulk_loader_edit_template_field_form(&$form_state = NULL) {
   return $form;
 }
 
+function tripal_bulk_loader_edit_template_field_form_validate($form, $form_state) {
+
+  // Don't worry about validation when Cancel button is clicked
+  if ($form_state['clicked_button']['#value'] == 'Save Changes') {
+    $is_unique = tripal_bulk_loader_is_record_name_unique(
+      $form_state['values']['record_name'],
+      $form_state['values']['template_id'],
+      $form_state['storage']['template_array']
+    );
+    $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
+    if ((!$is_unique) AND $new_record) {
+      form_set_error('record_name', "New Record Name must be unique. '".$form_state['values']['record_name']."' is not unique.");
+    }
+  }
+
+}
+
 /**
  * Edit Field Form Submit
  *
@@ -1932,7 +1966,7 @@ function tripal_bulk_loader_edit_template_field_form_submit($form, &$form_state)
   $form_state['storage']['test_regex_test'] = NULL;
 
   if (!$form_state['ahah_submission']) {
-    if ($op ==  'Edit Field') {
+    if ($op ==  'Save Changes') {
 
       // If new record
       if (preg_match('/NEW/', $form_state['values']['field_group'])) {
@@ -2136,6 +2170,56 @@ function tripal_bulk_loader_edit_template_field_form_submit($form, &$form_state)
 
 }
 
+/**
+ * @section
+ * Helper Functions
+ */
+
+/**
+ * Meant to be called from a form_validate function to ensure a newly added bulk loader record
+ * name is unique and not empty.
+ *
+ * @param $new_record_name
+ *   The record name to check for uniqueness
+ * @param $template_id
+ *   The template_id of the template to add the record to
+ * @param $template_array
+ *   The array describing the template. Optional -will be loaded using template_id if not provided
+ *
+ * @return
+ *   TRUE if the record name is not empty and not already in the template_array; FALSE otherwise
+ */
+function tripal_bulk_loader_is_record_name_unique($new_record_name, $template_id, $template_array = NULL) {
+
+  // get the template array if it's not supplied
+  if (empty($template_array)) {
+    $template = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d",$template_id));
+    $template_array = unserialize($template->template_array);
+    if (!is_array($template_array)) {
+      watchdog(
+        'tripal_bulk_loader',
+        'Unable to retrieve template array from database where template_id=%template_id',
+        array('%template_id' => $template_id),
+        WATCHDOG_WARNING
+      );
+      return FALSE;
+    }
+  }
+
+  // Check that the new record name is not empty
+  if (empty($new_record_name)) {
+    return FALSE;
+  }
+
+  // Check the new record name is unique
+  foreach ($template_array as $t) {
+    if (strcmp($t['record_id'], $new_record_name) == 0) {
+      return FALSE;
+    }
+  }
+  return TRUE;
+}
+
 /**
  * @section
  * AHAH Callbacks