Tripal v1.0 (6.x-1.0)
tripal_bulk_loader.constants.inc
Go to the documentation of this file.
00001 <?php
00029 function tripal_bulk_loader_update_constant($nid, $group_id, $table, $field, $record_id, $field_id, $value) {
00030 
00031   $record = array(
00032     'nid' => $nid,
00033     'group_id' => $group_id,
00034     'chado_table' => $table,
00035     'chado_field' => $field,
00036     'record_id' => $record_id,
00037     'field_id' => $field_id,
00038     'value' => $value
00039   );
00040 
00041   // Check to see if already exists
00042   $exists = db_fetch_object(db_query(
00043     "SELECT constant_id FROM {tripal_bulk_loader_constants} WHERE nid=%d AND record_id=%d AND field_id=%d AND group_id=%d",
00044     $record['nid'],
00045     $record['record_id'],
00046     $record['field_id'],
00047     $record['group_id']
00048   ));
00049   if ($exists->constant_id) {
00050     $record['constant_id'] = $exists->constant_id;
00051     $status = drupal_write_record('tripal_bulk_loader_constants', $record, 'constant_id');
00052     if ($status) {
00053       return $record;
00054     }
00055     else {
00056       return FALSE;
00057     }
00058   }
00059   else {
00060 
00061     $status = drupal_write_record('tripal_bulk_loader_constants', $record);
00062     if ($status) {
00063       return $record;
00064     }
00065     else {
00066       return FALSE;
00067     }
00068   }
00069 }
00070 
00071 function tripal_bulk_loader_has_exposed_fields($node) {
00072 
00073   // exposed fields isn't set
00074   if (!isset($node->exposed_fields)) {
00075     return FALSE;
00076   }
00077 
00078   // exposed fields has at least one element
00079   if (sizeof($node->exposed_fields) == 1) {
00080     // need to check if single element is an empty array
00081     $element = reset($node->exposed_fields);
00082     if ($element) {
00083       return TRUE;
00084     }
00085     else {
00086       return FALSE;
00087     }
00088   }
00089   elseif (sizeof($node->exposed_fields) > 1) {
00090     return TRUE;
00091   }
00092   else {
00093     return FALSE;
00094   }
00095 
00096   return FALSE;
00097 }
00098 
00100 // Set Constants Form (on Bulk Loader Node)
00102 
00114 function tripal_bulk_loader_set_constants_form($form_state, $node) {
00115   $form = array();
00116 
00117   $form['nid'] = array(
00118     '#type' => 'hidden',
00119     '#value' => $node->nid
00120   );
00121 
00122   if (!tripal_bulk_loader_has_exposed_fields($node)) {
00123     return $form;
00124   }
00125 
00126   $form['exposed_array'] = array(
00127     '#type' => 'hidden',
00128     '#value' => serialize($node->exposed_fields),
00129   );
00130 
00131   $form['exposed_fields'] = array(
00132     '#type' => 'fieldset',
00133     '#title' => t('Constant Values'),
00134     '#collapsible' => TRUE,
00135     '#collapsed' => ($node->template_id) ? FALSE : TRUE,
00136     '#prefix' => '<div id="set-constants">',
00137     '#suffix' => '</div>',
00138   );
00139 
00140   // Display table of already added constant sets with the ability to re-arrange and delete
00141   $first_constant = reset($node->constants);
00142   if (sizeof($node->constants) > 0 AND !empty($first_constant)) {
00143     $form['exposed_fields']['explanation-1'] = array(
00144       '#type' => 'item',
00145       '#value' => t('You have already added constants to this bulk loading job. Each '
00146         .'row in the following table represents a set of constants. Each set will be used '
00147         .'to load your data file with the specified template resulting in the each record '
00148         .'in the template to be loaded x number of times where there are x sets of '
00149         .'constants (rows in the following table).')
00150     );
00151 
00152     $form['exposed_fields']['existing'] = array(
00153       '#tree' => TRUE,
00154     );
00155 
00156     foreach ($node->constants as $set) {
00157 
00158       foreach ($set as $record) {
00159         foreach ($record as $field) {
00160           $index = $field['record_id'] . '-' . $field['field_id'];
00161           $group = $field['group_id'];
00162           $form['exposed_fields']['existing'][$group][$index] = array(
00163             '#type' => 'markup',
00164             '#value' => filter_xss($field['value']),
00165           );
00166         }
00167       }
00168 
00169       $form['exposed_fields']['existing'][$group]['delete'] = array(
00170         '#type' => 'markup',
00171         '#value' => filter_xss(l(t('Edit'), 'node/' . $node->nid . '/constants/' . $group . '/edit') . '  |  '  .
00172           l(t('Delete'), 'node/' . $node->nid . '/constants/' . $group . '/delete')),
00173       );
00174 
00175     }
00176   }
00177 
00178   $form['exposed_fields']['new'] = array(
00179     '#type' => 'fieldset',
00180     '#title' => t('New set of Constants'),
00181   );
00182 
00183   $form['exposed_fields']['new']['explanation-2'] = array(
00184     '#type' => 'item',
00185     '#value' => t('The following fields are constants in the selected template that you need to set values for.')
00186   );
00187 
00188   // Add textifelds for exposed fields of the current template
00189   $exposed_fields = FALSE;
00190   $indexes = array();
00191   if (tripal_bulk_loader_has_exposed_fields($node)) {
00192     foreach ($node->exposed_fields as $exposed_index) {
00193 
00194       $record_id = $exposed_index['record_id'];
00195       $field_id = $exposed_index['field_id'];
00196       $field = $node->template->template_array[$record_id]['fields'][$field_id];
00197 
00198       if ($field['exposed']) {
00199         $exposed_fields = TRUE;
00200         $indexes[$record_id][] = $field_id;
00201 
00202         switch ($field['type']) {
00203           case 'table field':
00204             $form['exposed_fields']['new'][$record_id . '-' . $field_id] = array(
00205               '#type' => 'textfield',
00206               '#title' => t('%title', array('%title' => $field['title'])),
00207               '#description' => t('%exposed_description', array('%exposed_description' => $field['exposed_description'])),
00208               '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
00209             );
00210           break;
00211           case 'constant':
00212             $form['exposed_fields']['new'][$record_id . '-' . $field_id] = array(
00213               '#type' => 'textfield',
00214               '#title' => t('%title', array('%title' => $field['title']) ),
00215               '#description' => t('Enter the case-sensitive value of this constant for your data file'),
00216               '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
00217             );
00218           break;
00219         }
00220 
00221         $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-table'] = array(
00222           '#type' => 'hidden',
00223           '#value' => $node->template->template_array[$record_id]['table'],
00224         );
00225         $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-field'] = array(
00226           '#type' => 'hidden',
00227           '#value' => $field['field'],
00228         );
00229         $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-type'] = array(
00230           '#type' => 'hidden',
00231           '#value' => $field['type'],
00232         );
00233 
00234       }
00235     }
00236   }
00237   $form['template'] = array(
00238     '#type' => 'hidden',
00239     '#value' => serialize($node->template->template_array)
00240   );
00241 
00242   $form['exposed_fields']['new']['indexes'] = array(
00243     '#type' => 'hidden',
00244     '#value' => serialize($indexes),
00245   );
00246 
00247   if (!$exposed_fields) {
00248     $form['exposed_fields']['new']['explanation'] = array(
00249       '#type' => 'item',
00250       '#value' => t('There are no exposed fields for this template.')
00251     );
00252   }
00253 
00254   $form['exposed_fields']['new']['submit-2'] = array(
00255     '#type' => 'submit',
00256     '#name' => 'add_constant',
00257     '#value' => t('Add Constant Set')
00258   );
00259 
00260   return $form;
00261 }
00262 
00267 function tripal_bulk_loader_set_constants_form_validate($form, $form_state) {
00268 
00269   $template = unserialize($form_state['values']['template']);
00270   $indexes = unserialize($form_state['values']['indexes']);
00271 
00272   $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
00273   if (strcmp('Add Constant Set', $op) == 0) {
00274       foreach ($indexes as $record_id => $array) {
00275         foreach ($array as $field_id) {
00276           if ($template[$record_id]['fields'][$field_id]['exposed_validate']) {
00277             $result = db_fetch_object(chado_query(
00278               "SELECT 1 as valid FROM %s WHERE %s='%s'",
00279               $template[$record_id]['table'],
00280               $template[$record_id]['fields'][$field_id]['field'],
00281               $form_state['values'][$record_id . '-' . $field_id]
00282             ));
00283 
00284             if (!$result->valid) {
00285               $msg = 'A ' . $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#title'] . ' of "' . $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#value'] . '" must already exist!';
00286               form_set_error($record_id . '-' . $field_id, $msg);
00287             }
00288             else {
00289               drupal_set_message(
00290                 t(
00291                   'Confirmed a %title of "%value" already exists.',
00292                   array(
00293                     '%title' => $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#title'],
00294                     '%value' => $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#value']
00295                   )
00296                 )
00297               );
00298             }
00299           }
00300         }
00301       }
00302   }
00303 
00304 }
00305 
00309 function tripal_bulk_loader_set_constants_form_submit($form, $form_state) {
00310 
00311   // Insert/Update constants
00312   $template = unserialize($form_state['values']['template']);
00313   $indexes = unserialize($form_state['values']['indexes']);
00314 
00315   $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
00316   if (strcmp('Add Constant Set', $op) == 0) {
00317       $max_group = db_fetch_object(db_query("SELECT max(group_id) as value FROM {tripal_bulk_loader_constants} WHERE nid=%d", $form_state['values']['nid']));
00318       foreach ($indexes as $record_id => $array) {
00319         foreach ($array as $field_id) {
00320           tripal_bulk_loader_update_constant(
00321             $form_state['values']['nid'],
00322             $max_group->value+1,
00323             $form_state['values'][$record_id . '-' . $field_id . '-table'],
00324             $form_state['values'][$record_id . '-' . $field_id . '-field'],
00325             $record_id,
00326             $field_id,
00327             $form_state['values'][$record_id . '-' . $field_id]
00328           );
00329         }
00330       }
00331   }
00332 
00333 }
00334 
00335 function theme_tripal_bulk_loader_set_constants_form($form) {
00336   $output = '';
00337 
00338   $exposed_fields = unserialize($form['exposed_array']['#value']);
00339   // need to put in the context of a node so we can use the has_exposed_fields function
00340   if ($exposed_fields) {
00341     $node->exposed_fields = $exposed_fields;
00342   }
00343   else {
00344     $node->exposed_fields = array();
00345   }
00346 
00347   // Add draggable table for constant sets
00348   if (tripal_bulk_loader_has_exposed_fields($node)) {
00349     $i=1;
00350     foreach (element_children($form['exposed_fields']['existing']) as $key) {
00351       $element = &$form['exposed_fields']['existing'][$key];
00352       $element['group']['#attributes']['class'] = 'weight-group';
00353 
00354       $row = array();
00355       foreach ($exposed_fields as $exposed) {
00356         if ($i==1) {
00357           $header[] = $exposed['title'];
00358         }
00359         $k = $exposed['record_id'] . '-' . $exposed['field_id'];
00360         $row[] = drupal_render($element[$k]);
00361       }
00362       $row[] = drupal_render($element['delete']);
00363       $row[] = drupal_render($element['group']) . drupal_render($element['id']);
00364       if (!empty($row[0])) {
00365         $rows[] = array('data' => $row, 'class' => 'draggable');
00366       }
00367       $i++;
00368     }
00369     //drupal_add_tabledrag('mytable', 'order', 'sibling', 'weight-group');
00370     // @coder-ignore: no user input thus don't need to filter
00371     $form['exposed_fields']['existing'] = array(
00372       '#type' => 'markup',
00373       '#value' => theme('table', $header, $rows, array('id' => 'mytable')) . '<br />',
00374     );
00375   }
00376 
00377   $output .= drupal_render($form);
00378   return $output;
00379 }
00380 
00382 // Set Constants Form (on Bulk Loader Node)
00384 
00398 function tripal_bulk_loader_edit_constant_set_form($form_state, $node, $group_id) {
00399   $form = array();
00400 
00401   $form['#redirect'] = 'node/' . $node->nid;
00402 
00403   $form['nid'] = array(
00404     '#type' => 'hidden',
00405     '#value' => $node->nid,
00406   );
00407 
00408   $form['group_id'] = array(
00409     '#type' => 'hidden',
00410     '#value' => $group_id,
00411   );
00412 
00413 
00414   $form['explanation'] = array(
00415     '#type' => 'item',
00416     '#value' => t('The following fields are constants in the selected template that you need to set values for.')
00417   );
00418 
00419   // Add textifelds for exposed fields of the current template
00420   $exposed_fields = FALSE;
00421   $indexes = array();
00422   if (tripal_bulk_loader_has_exposed_fields($node)) {
00423     foreach ($node->exposed_fields as $exposed_index) {
00424 
00425       $record_id = $exposed_index['record_id'];
00426       $record = $node->template->template_array[$record_id];
00427       $field_id = $exposed_index['field_id'];
00428       $field = $node->template->template_array[$record_id]['fields'][$field_id];
00429 
00430       if ($field['exposed']) {
00431         $exposed_fields = TRUE;
00432         $indexes[$record_id][] = $field_id;
00433 
00434         switch ($field['type']) {
00435           case 'table field':
00436             $form[$record_id . '-' . $field_id] = array(
00437               '#type' => 'textfield',
00438               '#title' => t('%title', array('%title' => $field['title'])),
00439               '#description' => t('%exposed_description', array('%exposed_description' => $field['exposed_description'])),
00440               '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
00441             );
00442           break;
00443           case 'constant':
00444             $form[$record_id . '-' . $field_id] = array(
00445               '#type' => 'textfield',
00446               '#title' => t('%title', array('%title' => $field['title'])),
00447               '#description' => t('Enter the case-sensitive value of this constant for your data file'),
00448               '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
00449             );
00450           break;
00451         }
00452 
00453         $form[$record_id . '-' . $field_id . '-table'] = array(
00454           '#type' => 'hidden',
00455           '#value' => $record['table'],
00456         );
00457         $form[$record_id . '-' . $field_id . '-field'] = array(
00458           '#type' => 'hidden',
00459           '#value' => $field['field'],
00460         );
00461         $form[$record_id . '-' . $field_id . '-type'] = array(
00462           '#type' => 'hidden',
00463           '#value' => $field['type'],
00464         );
00465       }
00466 
00467     }
00468   }
00469   $form['template'] = array(
00470     '#type' => 'hidden',
00471     '#value' => serialize($node->template->template_array)
00472   );
00473 
00474   $form['indexes'] = array(
00475     '#type' => 'hidden',
00476     '#value' => serialize($indexes),
00477   );
00478 
00479   $form['save'] = array(
00480     '#type' => 'submit',
00481     '#value' => 'Save',
00482   );
00483 
00484   $form['cancel'] = array(
00485     '#type' => 'submit',
00486     '#value' => 'Cancel',
00487   );
00488 
00489   return $form;
00490 }
00491 
00495 function tripal_bulk_loader_edit_constant_set_form_submit($form, $form_state) {
00496 
00497   // Update constants
00498   $template = unserialize($form_state['values']['template']);
00499   $indexes = unserialize($form_state['values']['indexes']);
00500 
00501   $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
00502   if (strcmp('Save', $op) == 0) {
00503 
00504     foreach ($indexes as $record_id => $array) {
00505       foreach ($array as $field_id) {
00506         tripal_bulk_loader_update_constant(
00507           $form_state['values']['nid'],
00508           $form_state['values']['group_id'],
00509           $form_state['values'][$record_id . '-' . $field_id . '-table'],
00510           $form_state['values'][$record_id . '-' . $field_id . '-field'],
00511           $record_id,
00512           $field_id,
00513           $form_state['values'][$record_id . '-' . $field_id]
00514         );
00515       }
00516     }
00517     drupal_set_message(t('The constant set was successfully updated.'));
00518 
00519   }
00520 
00521 }
00522 
00536 function tripal_bulk_loader_delete_constant_set_form($form_state, $node, $group_id) {
00537   $form = array();
00538 
00539   $form['#redirect'] = 'node/' . $node->nid;
00540 
00541   $form['nid'] = array(
00542     '#type' => 'value',
00543     '#value' => $node->nid,
00544   );
00545 
00546   $form['group_id'] = array(
00547     '#type' => 'hidden',
00548     '#value' => $group_id,
00549   );
00550 
00551   return confirm_form($form,
00552       t('Are you sure you want to delete this constant set?'),
00553       'node/' . $node->nid,
00554       t('This action cannot be undone.'),
00555       t('Delete'),
00556       t('Cancel')
00557   );
00558 
00559 }
00560 
00564 function tripal_bulk_loader_delete_constant_set_form_submit($form, $form_state) {
00565 
00566   $group_id = $form_state['values']['group_id'];
00567   $nid = $form_state['values']['nid'];
00568   if ($nid && $form_state['values']['confirm']) {
00569     db_query("DELETE FROM {tripal_bulk_loader_constants} WHERE nid=%d AND group_id=%d", $nid, $group_id);
00570     drupal_set_message(t('Constant set successfully deleted.'));
00571   }
00572 
00573 }
 All Classes Files Functions Variables