tripal_bulk_loader.admin.inc 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  1. <?php
  2. /**
  3. * @file
  4. * Handles Create/Edit/Delete Template Admin Forms
  5. */
  6. /**
  7. * Provides a description page and quick links for the bulk loader
  8. */
  9. function tripal_bulk_loader_admin_template () {
  10. $output = '';
  11. $output .= '<br><h3>Quick Links:</h3>';
  12. $output .= l('Create a new bulk loader template', 'admin/tripal/tripal_bulk_loader_template/create')."<br>";
  13. $output .= l('Edit a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/edit')."<br>";
  14. $output .= l('Delete a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/delete')."<br>";
  15. $output .= l('Export a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/export')."<br>";
  16. $output .= l('Import a bulk loader template', 'admin/tripal/tripal_bulk_loader_template/import')."<br>";
  17. $output .= '<br>';
  18. $output .= '<h3>Module Description:</h3>';
  19. $output .= '<p>This module provides the ability to create loading templates for any tab-delimited '
  20. .'data file allowing it to be loaded into chado. The Loading Templates are a direct mapping '
  21. .'between the columns in your file and the columns in chado tables. As such to use this tool '
  22. .'you need to be very familar with the chado schema -See '
  23. .l('Chado -Getting Started', 'http://gmod.org/wiki/Chado_-_Getting_Started')
  24. .'. The ability to add constants and specify foreign key contraints is also provided '
  25. .'in order for the loader to fill chado columns which may be required but are not specified '
  26. .'in your input file.</p>';
  27. $output .= '<br>';
  28. $output .= '<h3>Setup Instructions</h3>';
  29. $output .= '<p>After intallation of the bulk loader module, the following tasks should be performed:</p>';
  30. $output .= '<ol>';
  31. $output .= '<li><b>Install Theme:</b> In order for Bulk Loading pages to be displayed correctly, '
  32. .'the contents of the Tripal Bulk Loader theme directory ([drupal root]/sites/all/modules/tripal/tripal_bulk_loader/theme) '
  33. .'should be moved to the base directory of the Tripal theme ([drupal root]/sites/all/themes/tripal). '
  34. .'Finally the drupal cache should be cleared for the new theme to take effect -navigate to admin/settings/performance '
  35. .'and click the Clear Cached Data button.</li>';
  36. $output .= '</ol>';
  37. return $output;
  38. }
  39. /**
  40. * A Configuration form for this module
  41. */
  42. function tripal_bulk_loader_configuration_form($form_state = NULL) {
  43. $form = array();
  44. $form['speed'] = array(
  45. '#type' => 'fieldset',
  46. '#title' => t('Possible Speed Improvements'),
  47. );
  48. $form['speed']['prepare'] = array(
  49. '#type' => 'checkbox',
  50. '#title' => t('Use Prepared Statements'),
  51. '#description' => t('SQL Prepared Statements allow for SQL queries which will be run '
  52. .'many times to be parsed, rewritten and planned only once rather then every time '
  53. .'the query is run. In the case of the bulk loader, this ensures that planning only '
  54. .'occurs once for each "record" in your bulk loading template.'),
  55. '#default_value' => variable_get('tripal_bulk_loader_prepare', TRUE),
  56. );
  57. $form['submit1'] = array(
  58. '#type' => 'submit',
  59. '#value' => t('Save')
  60. );
  61. return $form;
  62. }
  63. /**
  64. * A Configuration form for this module (Submit)
  65. */
  66. function tripal_bulk_loader_configuration_form_submit($form, $form_state) {
  67. variable_set('tripal_bulk_loader_prepare', $form_state['values']['prepare']);
  68. }
  69. //////////////////////////////////////////////////////////////////////////////////////
  70. // Modify Template
  71. //////////////////////////////////////////////////////////////////////////////////////
  72. /**
  73. * The main form reached at admin/tripal/tripal_bulk_loader/create and /edit
  74. */
  75. function tripal_bulk_loader_modify_template_base_form ($form_state = NULL, $mode){
  76. $form = array();
  77. // get template id from path and rebuild form
  78. if ($_GET['template_id']) {
  79. if (preg_match('/^\d+$/', $_GET['template_id'])) {
  80. $form_state['storage']['template_id'] = $_GET['template_id'];
  81. }
  82. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  83. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  84. $form_state['storage']['template'] = unserialize($result->template_array);
  85. $form_state['storage']['template_name'] = $result->name;
  86. $form_state['storage']['record2priority'] = array();
  87. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  88. if (!is_array($record_array)) { continue; }
  89. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  90. }
  91. }
  92. $form['mode'] = array(
  93. '#type' => 'hidden',
  94. '#value' => $mode,
  95. );
  96. if ($form_state['storage']['template_id']) {
  97. $form['template_name'] = array(
  98. '#type' => 'item',
  99. '#title' => 'Template',
  100. '#value' => $form_state['storage']['template_name'],
  101. '#weight' => 1,
  102. );
  103. } else {
  104. if (preg_match('/create/', $mode)) {
  105. $form['new_template_name'] = array(
  106. '#type' => 'textfield',
  107. '#title' => 'Template Name',
  108. '#weight' => 1,
  109. );
  110. } elseif (preg_match('/edit/', $mode)) {
  111. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  112. $resource = db_query($sql);
  113. $templates = array();
  114. $templates[''] = 'Select a Template';
  115. while ($r = db_fetch_object($resource)) {
  116. $templates[$r->template_id] = $r->name;
  117. }
  118. $form['template_id'] = array(
  119. '#title' => t('Template'),
  120. '#description' => t('Please select the template you would like to edit.'),
  121. '#type' => 'select',
  122. '#options' => $templates,
  123. '#default_value' => $form_state['storage']['template_id'],
  124. '#weight' => 0,
  125. '#required' => TRUE,
  126. '#weight' => 1,
  127. );
  128. }
  129. }
  130. $form['records'] = array(
  131. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  132. '#title' => t('Current Records'),
  133. '#collapsible' => TRUE,
  134. '#weight' => 2,
  135. );
  136. $form['records']['description'] = array(
  137. '#type' => 'item',
  138. '#value' => 'Records will be inserted into the chado database in the order listed below. To '
  139. .'change this order: <ul><li>Drag the rows into the correct order OR</li><li>Enter '
  140. .'the numbers 1 and up in the Order textboxes to indicate the correct order.</li></ul>',
  141. );
  142. $form['records']['records-data'] = array(
  143. '#tree' => TRUE,
  144. );
  145. $form['records']['no_records'] = array(
  146. '#type' => 'hidden',
  147. '#value' => TRUE,
  148. );
  149. $form['records']['submit-new_record'] = array(
  150. '#type' => 'submit',
  151. '#value' => 'New Record/Field',
  152. );
  153. $form['records']['submit-reorder'] = array(
  154. '#type' => 'submit',
  155. '#value' => 'Save Order',
  156. );
  157. $form['fields'] = array(
  158. '#type' => ($form_state['storage']['template_id'])? 'fieldset' : 'hidden',
  159. '#title' => t('Current Fields'),
  160. '#collapsible' => TRUE,
  161. '#weight' => 3,
  162. );
  163. $form['fields']['fields-data'] = array(
  164. '#tree' => TRUE,
  165. );
  166. if ($form_state['storage']['template']) {
  167. // List Current Fields -------------------------------------------------------------
  168. $i=1;
  169. foreach ($form_state['storage']['template'] as $priority => $table_array) {
  170. if (!is_array($table_array)) { continue; }
  171. $form['records']['no_records']['#value'] = FALSE;
  172. $form['records']['records-data'][$priority] = array(
  173. 'title' => array(
  174. '#type' => 'markup',
  175. '#value' => $table_array['record_id'],
  176. ),
  177. 'chado_table' => array(
  178. '#type' => 'markup',
  179. '#value' => $table_array['table'],
  180. ),
  181. 'mode' => array(
  182. '#type' => 'item',
  183. '#value' => ($table_array['mode']) ? $table_array['mode'] : 'insert_unique',
  184. ),
  185. 'new_priority' => array(
  186. '#type' => 'select',
  187. '#options' => range(1, sizeof($form_state['storage']['template'])),
  188. '#default_value' => $priority,
  189. ),
  190. 'old_priority' => array(
  191. '#type' => 'hidden',
  192. '#value' => $priority,
  193. ),
  194. 'id' => array(
  195. '#type' => 'hidden',
  196. '#value' => $priority,
  197. ),
  198. 'submit-edit_record' => array(
  199. '#type' => 'submit',
  200. '#name' => (string)$priority,
  201. '#value' => 'Edit Record',
  202. ),
  203. 'submit-add_field' => array(
  204. '#type' => 'submit',
  205. '#name' => (string)$priority,
  206. '#value' => 'Add Field',
  207. ),
  208. 'submit-duplicate_record' => array(
  209. '#type' => 'submit',
  210. '#name' => (string)$priority,
  211. '#value' => 'Duplicate Record'
  212. ),
  213. );
  214. foreach ($table_array['fields'] as $field_index => $field) {
  215. $form['fields']['fields-data'][$i] = array(
  216. 'record_id' => array(
  217. '#type' => 'item',
  218. '#value' => $table_array['record_id'],
  219. ),
  220. 'priority_hidden' => array(
  221. '#type' => 'hidden',
  222. '#value' => $priority,
  223. ),
  224. 'field_name' => array(
  225. '#type' => 'item',
  226. '#value' => $field['title'],
  227. ),
  228. 'chado_table_name' => array(
  229. '#type' => 'item',
  230. '#value' => $table_array['table'],
  231. ),
  232. 'chado_table_hidden' => array(
  233. '#type' => 'hidden',
  234. '#value' => $table_array['table'],
  235. ),
  236. 'chado_field_name' => array(
  237. '#type' => 'item',
  238. '#value' => $field['field'],
  239. ),
  240. 'sheet_name' => array(
  241. '#type' => 'item',
  242. '#value' => $field['spreadsheet sheet'],
  243. ),
  244. 'column_num' => array(
  245. '#type' => 'item',
  246. '#value' => $field['spreadsheet column'],
  247. ),
  248. 'constant_value' => array(
  249. '#type' => 'item',
  250. '#value' => $field['constant value'],
  251. ),
  252. 'field_index' => array(
  253. '#type' => 'hidden',
  254. '#value' => $field_index
  255. ),
  256. 'foreign_record_id' => array(
  257. '#type' => 'item',
  258. '#value' => $field['foreign key'],
  259. ),
  260. 'edit_submit' => array(
  261. '#type' => 'submit',
  262. '#name' => (string)$i,
  263. '#value' => "Edit Field",
  264. ),
  265. 'delete_submit' => array(
  266. '#type' => 'submit',
  267. '#name' => (string)$i,
  268. '#value' => "Delete Field",
  269. ),
  270. );
  271. $i++;
  272. }
  273. }
  274. $form['fields']['total_fields'] = array(
  275. '#type' => 'item',
  276. '#value' => $i,
  277. );
  278. }
  279. if ($form['records']['no_records']['#value']) {
  280. $form['records']['description'] = array(
  281. '#type' => 'item',
  282. '#value' => 'There are currently no records.',
  283. );
  284. unset($form['records']['submit-reorder']);
  285. $form['fields']['description'] = array(
  286. '#type' => 'item',
  287. '#value' => 'There are currently no fields.',
  288. );
  289. }
  290. $mode_title = (preg_match('/create/',$mode)) ? 'Create Template' : 'Edit Template';
  291. $value = ($form_state['storage']['template_id'])? 'Save Template' : $mode_title;
  292. $form['submit'] = array(
  293. '#type' => 'submit',
  294. '#value' => $value,
  295. '#weight' => 4,
  296. );
  297. return $form;
  298. }
  299. /**
  300. * Submit for tripal_bulk_loader_modify_template_base_form
  301. */
  302. function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state){
  303. $form_state['rebuild'] = TRUE;
  304. if ($form_state['storage']['template_id']) {
  305. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  306. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  307. $form_state['storage']['template'] = unserialize($result->template_array);
  308. }
  309. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  310. switch ($op) {
  311. // Initialize after template is chosen ----------------------------------------
  312. case 'Edit Template':
  313. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  314. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  315. $result = db_fetch_object(db_query($sql, $form_state['storage']['template_id']));
  316. $form_state['storage']['template'] = unserialize($result->template_array);
  317. $form_state['storage']['template_name'] = $result->name;
  318. $form_state['storage']['record2priority'] = array();
  319. foreach ($form_state['storage']['template'] as $priority => $record_array) {
  320. if (!is_array($record_array)) { continue; }
  321. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  322. }
  323. break;
  324. case 'Create Template':
  325. $record = array(
  326. 'name' => $form_state['values']['new_template_name'],
  327. 'template_array' => array(),
  328. );
  329. drupal_write_record('tripal_bulk_loader_template', $record);
  330. $form_state['storage']['template_id'] = $record['template_id'];
  331. $form_state['storage']['template_name'] = $record['name'];
  332. $form_state['storage']['template'] = array();
  333. break;
  334. // Save Reordered Records -----------------------------------------------------
  335. case 'Save Order':
  336. $new_template = $form_state['storage']['template'];
  337. // unset old elements
  338. $form_state['storage']['record2priority'] = array();
  339. foreach ($new_template as $priority => $record_array) {
  340. if (preg_match('/\d+/', $priority)) { unset($new_template[$priority]); }
  341. }
  342. //set elements in new order
  343. foreach ($form_state['values']['records-data'] as $item) {
  344. $new_template[$item['new_priority']] = $form_state['storage']['template'][$item['old_priority']];
  345. $record_name = $new_template[$item['new_priority']]['record_id'];
  346. $form_state['storage']['record2priority'][$record_name] = $item['new_priority'];
  347. }
  348. ksort($new_template);
  349. $form_state['storage']['template'] = $new_template;
  350. break;
  351. case 'New Record/Field':
  352. $query = array(
  353. 'template_id'=> $form_state['storage']['template_id'],
  354. 'record_id' => 'NEW',
  355. );
  356. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  357. break;
  358. case 'Edit Record':
  359. $query = array(
  360. 'template_id'=> $form_state['storage']['template_id'],
  361. 'record_id' => $form_state['clicked_button']['#name'],
  362. );
  363. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_record', $query);
  364. break;
  365. case 'Add Field':
  366. $query = array(
  367. 'template_id'=> $form_state['storage']['template_id'],
  368. 'record_id' => $form_state['clicked_button']['#name'],
  369. );
  370. drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
  371. break;
  372. case 'Duplicate Record':
  373. // original record (one to be duplicated)
  374. $orig_priority = $form_state['clicked_button']['#name'];
  375. $record = $form_state['storage']['template'][ $orig_priority ];
  376. // new record
  377. $new_priority = sizeof($form_state['storage']['template']) + 1;
  378. $record['record_id'] = $record['record_id'] . '_' . date('YzHi');
  379. $form_state['storage']['template'][ $new_priority ] = $record;
  380. break;
  381. case 'Edit Field':
  382. $field_data_index = $form_state['clicked_button']['#name'];
  383. $query = array(
  384. 'template_id'=> $form_state['storage']['template_id'],
  385. 'record_id' => $form_state['values']['fields-data'][$field_data_index]['priority_hidden'],
  386. 'field_index' => $form_state['values']['fields-data'][$field_data_index]['field_index'],
  387. );
  388. drupal_goto('admin/tripal/tripal_bulk_loader_template/edit_field', $query);
  389. break;
  390. case 'Delete Field':
  391. $field_data = $form_state['values']['fields-data'][$form_state['clicked_button']['#name']];
  392. $priority = $field_data['priority_hidden'];
  393. $field_key = $field_data['field_index'];
  394. unset($form_state['storage']['template'][$priority]['fields'][$field_key]);
  395. if (!$form_state['storage']['template'][$priority]['fields']) {
  396. unset($form_state['storage']['record2priority'][$form_state['storage']['template'][$priority]['record_id']]);
  397. unset($form_state['storage']['template'][$priority]);
  398. }
  399. drupal_set_message('Deleted Field from Template.');
  400. break;
  401. } //end of switch
  402. // Save Template
  403. $record = array(
  404. 'template_id' => $form_state['storage']['template_id'],
  405. 'template_array' => serialize($form_state['storage']['template'])
  406. );
  407. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  408. drupal_set_message('Template Saved.');
  409. }
  410. //////////////////////////////////////////////////////////////////////////////////////
  411. // Delete Template
  412. //////////////////////////////////////////////////////////////////////////////////////
  413. /**
  414. * Delete Template Form
  415. * This form allows admin to delete already existing templates
  416. */
  417. function tripal_bulk_loader_delete_template_base_form () {
  418. $form = array();
  419. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  420. $resource = db_query($sql);
  421. $templates = array();
  422. $templates[''] = 'Select a Template';
  423. while ($r = db_fetch_object($resource)) {
  424. $templates[$r->template_id] = $r->name;
  425. }
  426. $form['template_name'] = array(
  427. '#title' => t('Template'),
  428. '#description' => t('Please select the template you would like to delete.'),
  429. '#type' => 'select',
  430. '#options' => $templates,
  431. '#weight' => 0,
  432. '#required' => TRUE,
  433. );
  434. $form['submit'] = array(
  435. '#type' => 'submit',
  436. '#value' => 'Delete Template',
  437. );
  438. return $form;
  439. }
  440. /**
  441. * Delete Template Form Submit
  442. *
  443. * @param $form
  444. * The form that was submitted
  445. * @param $form_state
  446. * The values and storage that were submitted
  447. */
  448. function tripal_bulk_loader_delete_template_base_form_submit ($form, &$form_state) {
  449. $sql = "DELETE FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  450. db_query($sql, $form_state['values']['template_name']);
  451. }
  452. //////////////////////////////////////////////////////////////////////////////////////
  453. // Import/Export Template
  454. //////////////////////////////////////////////////////////////////////////////////////
  455. /**
  456. * Import/Export Template Form
  457. *
  458. * On export, simply selects the serialized array from the db for a given template
  459. * and presents it to the user. On import, a serialized template array and a name is
  460. * supplied and a template record is created.
  461. *
  462. * @todo Make array presented to the user more readable. (ie: unserialize and print to the screen)
  463. *
  464. * @param $form_state
  465. * The values and storage for the form
  466. * @param $mode
  467. * Either 'import' or 'export' to indicate which function is being performed
  468. * @return
  469. * A form array to be rendered by drupal_get_form
  470. */
  471. function tripal_bulk_loader_import_export_template_form ($form_state = NULL, $mode) {
  472. $form = array();
  473. $form['mode'] = array(
  474. '#type' => 'hidden',
  475. '#value' => $mode,
  476. );
  477. if (preg_match('/import/', $mode)) {
  478. $form['new_template_name'] = array(
  479. '#type' => 'textfield',
  480. '#title' => 'Template Name',
  481. '#weight' => 1,
  482. );
  483. } elseif (preg_match('/export/', $mode)) {
  484. $sql = "SELECT * FROM {tripal_bulk_loader_template}";
  485. $resource = db_query($sql);
  486. $templates = array();
  487. $templates[''] = 'Select a Template';
  488. while ($r = db_fetch_object($resource)) {
  489. $templates[$r->template_id] = $r->name;
  490. }
  491. $form['template_id'] = array(
  492. '#title' => t('Template'),
  493. '#description' => t('Please select the template you would like to edit.'),
  494. '#type' => 'select',
  495. '#options' => $templates,
  496. '#default_value' => $form_state['storage']['template_id'],
  497. '#weight' => 0,
  498. '#required' => TRUE,
  499. '#weight' => 1,
  500. );
  501. }
  502. $form['template_array'] = array(
  503. '#type' => 'textarea',
  504. '#title' => 'Template Array',
  505. '#default_value' => $form_state['storage']['template_array'],
  506. '#weight' => 2,
  507. );
  508. $form['submit'] = array(
  509. '#type' => 'submit',
  510. '#value' => 'Submit',
  511. '#weight' => 10,
  512. );
  513. return $form;
  514. }
  515. /**
  516. * Import/Export Template Form Submit
  517. *
  518. * @param $form
  519. * The form that was submitted
  520. * @param $form_state
  521. * The values and storage that were submitted
  522. */
  523. function tripal_bulk_loader_import_export_template_form_submit ($form, &$form_state) {
  524. switch ($form_state['values']['mode']) {
  525. case 'export':
  526. $record = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $form_state['values']['template_id']));
  527. $form_state['storage']['template_array'] = $record->template_array;
  528. $form_state['storage']['template_id'] = $form_state['values']['template_id'];
  529. break;
  530. case 'import':
  531. $record = array(
  532. 'name' => $form_state['values']['new_template_name'],
  533. 'template_array' => $form_state['values']['template_array'],
  534. );
  535. drupal_write_record('tripal_bulk_loader_template', $record);
  536. if ($record->template_id) {
  537. drupal_set_message('Successfully imported Tripal Bulk Loader Template.');
  538. }
  539. break;
  540. }
  541. }
  542. //////////////////////////////////////////////////////////////////////////////////////
  543. // Edit Record Form
  544. //////////////////////////////////////////////////////////////////////////////////////
  545. /**
  546. * Edit Record Form
  547. *
  548. * This form is meant to be called from a bulk loader form. The following should be set
  549. * in the query section of the path:
  550. * - template_id=\d+: the template which the edited record is part of
  551. * - record_id=\d+: the priority or key in the template array of the record to be edited
  552. *
  553. * @param $form_state
  554. * Contains the values and storage for the form
  555. * @return
  556. * A form array to be rendered by drupal_get_form
  557. */
  558. function tripal_bulk_loader_edit_template_record_form (&$form_state = NULL) {
  559. $form['#cache'] = TRUE; // Make sure the form is cached.
  560. // get template id from path
  561. $template_id = ($_GET['template_id'] !== NULL) ? $_GET['template_id'] : $form_state['values']['template_id'];
  562. // if there is no template supplied don't return rest of form
  563. if (!$template_id) {
  564. return $form;
  565. }
  566. // Pre-process values/defaults ---------------------------
  567. // If this is the first load of the form (no form state) we need to initialize some variables
  568. if (!$form_state['storage']['template']) {
  569. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  570. $template = db_fetch_object(db_query($sql, $template_id));
  571. $form_state['storage']['template_array'] = unserialize($template->template_array);
  572. $form_state['storage']['template'] = $template;
  573. $form_state['storage']['record2priority'] = array();
  574. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  575. if (!is_array($record_array)) { continue; }
  576. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  577. }
  578. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  579. } else {
  580. $template = $form_state['storage']['template'];
  581. }
  582. // get the record_id from the path
  583. if ($_GET['record_id'] !== NULL) {
  584. $form_state['values']['field_group'] = $_GET['record_id'];
  585. $form_state['storage']['original_priority'] = $_GET['record_id'];
  586. }
  587. // Tables and default table
  588. $tables = tripal_core_get_chado_tables();
  589. if ($form_state['values']['chado_table']) {
  590. $table = $form_state['values']['chado_table'];
  591. } else {
  592. $table = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['table'];
  593. }
  594. //dpm($form_state, 'form state');
  595. // Form Proper -------------------------------------------
  596. $form['template_name'] = array(
  597. '#type' => 'item',
  598. '#title' => 'Template',
  599. '#value' => $template->name,
  600. );
  601. $form['template_id'] = array(
  602. '#type' => 'hidden',
  603. '#value' => $template_id,
  604. );
  605. $form['edit_record'] = array(
  606. '#type' => 'fieldset',
  607. );
  608. // check template array for records then add one more
  609. if (!$form_state['storage']['record2priority']) {
  610. $groups = array();
  611. } else {
  612. $groups = array_flip($form_state['storage']['record2priority']);
  613. }
  614. $priority_default = $form_state['values']['field_group'];
  615. $form['edit_record']['field_group'] = array(
  616. '#type' => 'select',
  617. '#title' => 'Record',
  618. '#description' => 'By Changing the record here, you can move all the fields from the current record into the selected record.',
  619. '#options' => $groups,
  620. '#default_value' => $priority_default,
  621. '#required' => TRUE,
  622. );
  623. $form['edit_record']['record_name'] = array(
  624. '#type' => 'textfield',
  625. '#title' => 'Unique Record Name',
  626. '#default_value' => $groups[$priority_default],
  627. );
  628. $form['edit_record']['chado_table'] = array(
  629. '#type' => 'select',
  630. '#title' => t('Chado Table'),
  631. '#description' => 'This changes the chado table for all fields in this record.',
  632. '#options' => $tables,
  633. '#default_value' => $table,
  634. );
  635. $form['edit_record']['mode'] = array(
  636. '#type' => 'radios',
  637. '#title' => 'Action to take when Loading Record',
  638. '#options' => array(
  639. 'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record',
  640. 'insert' => 'INSERT: Insert the record',
  641. 'optional' => 'OPTIONAL: Record will only be inserted if all required data is filled in',
  642. 'insert_once' => 'INSERT ONCE: Record will be inserted once for the entire spreadsheet',
  643. 'insert_unique' => 'INSERT UNIQUE: Only insert record if there isn\'t a record with the same values',
  644. ),
  645. '#default_value' => 'insert_unique'
  646. );
  647. $form['edit_record']['submit-edit_record'] = array(
  648. '#type' => 'submit',
  649. '#value' => 'Edit Record'
  650. );
  651. $form['edit_record']['submit-cancel'] = array(
  652. '#type' => 'submit',
  653. '#value' => 'Cancel'
  654. );
  655. return $form;
  656. }
  657. /**
  658. * Edit Record Form Submit
  659. *
  660. * @param $form
  661. * The form that was submitted
  662. * @param $form_state
  663. * Contains the values and storage for the form
  664. */
  665. function tripal_bulk_loader_edit_template_record_form_submit ($form, &$form_state) {
  666. //dpm($form_state, 'form state -start submit');
  667. if (!$form_state['ahah_submission']) {
  668. if ($form_state['values']['op'] == 'Edit Record') {
  669. $template = $form_state['storage']['template_array'];
  670. // Edit Record
  671. $record = $template[ $form_state['storage']['original_priority'] ];
  672. $record['record_id'] = $form_state['values']['record_name'];
  673. $record['mode'] = $form_state['values']['mode'];
  674. $record['table'] = $form_state['values']['chado_table'];
  675. if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
  676. $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
  677. $template[ $form_state['values']['field_group'] ] = $record;
  678. unset($template[ $form_state['storage']['original_priority'] ]);
  679. } else {
  680. $template[ $form_state['storage']['original_priority'] ] = $record;
  681. }
  682. // Save Template
  683. $form_state['storage']['template']->template_array = serialize($template);
  684. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  685. if ($success) {
  686. drupal_set_message('Successfully Updated Template Record');
  687. drupal_set_message('Template Saved.');
  688. $path = explode('?',$form_state['storage']['referring URL']);
  689. parse_str($path[1], $query);
  690. $query['template_id'] = $form_state['storage']['template']->template_id;
  691. drupal_goto($path[0], $query);
  692. } else {
  693. drupal_set_message('Unable to Save Template!', 'error');
  694. watchdog('T_bulk_loader',
  695. 'Unable to save bulk loader template: %template',
  696. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  697. WATCHDOG_ERROR
  698. );
  699. }
  700. } elseif ($form_state['values']['op'] == 'Cancel') {
  701. $path = explode('?',$form_state['storage']['referring URL']);
  702. parse_str($path[1], $query);
  703. $query['template_id'] = $form_state['storage']['template']->template_id;
  704. //dpm('Redirecting to: '.$path[0].'?'.print_r($query,TRUE).' where the referring URL:'.$form_state['storage']['referring URL']);
  705. drupal_goto($path[0], $query);
  706. }
  707. }
  708. }
  709. //////////////////////////////////////////////////////////////////////////////////////
  710. // Add/Edit Field Forms
  711. //////////////////////////////////////////////////////////////////////////////////////
  712. /**
  713. * Add Field Form
  714. *
  715. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  716. * can use the following in the query of the path to set defaults for a given template:
  717. * - template_id=\d+: the template to add the field to
  718. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  719. *
  720. * @param $form_state
  721. * Contains the values and storage for the form
  722. * @return
  723. * A form array to be rendered by drupal_get_form
  724. */
  725. function tripal_bulk_loader_add_template_field_form (&$form_state = NULL) {
  726. $form = array();
  727. $form['#cache'] = TRUE; // Make sure the form is cached.
  728. // get template id from path
  729. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  730. // if there is no template supplied don't return rest of form
  731. if (!$template_id) {
  732. return $form;
  733. }
  734. // Pre-set Variables needed for form proper------------------------------------------
  735. // If this is the first load of the form (no form state) we need to initialize some variables
  736. if (!$form_state['storage']['template']) {
  737. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  738. $template = db_fetch_object(db_query($sql, $template_id));
  739. $form_state['storage']['template_array'] = unserialize($template->template_array);
  740. $form_state['storage']['template'] = $template;
  741. $form_state['storage']['record2priority'] = array();
  742. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  743. if (!is_array($record_array)) { continue; }
  744. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  745. }
  746. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  747. } else {
  748. $template = $form_state['storage']['template'];
  749. }
  750. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field';
  751. // Tables and default table
  752. $tables = tripal_core_get_chado_tables();
  753. if ($form_state['values']) {
  754. if (!preg_match('/^'.current($tables).'$/', $form_state['values']['chado_table'])) {
  755. $table = $form_state['values']['chado_table'];
  756. } elseif ($form_state['values']['record_name']) {
  757. $record_name = $form_state['values']['record_name'];
  758. $priority = $form_state['storage']['record2priority'][$record_name];
  759. $table = $form_state['storage']['template_array'][$priority]['table'];
  760. } else {
  761. $priority = $form_state['values']['field_group'];
  762. $table = $form_state['storage']['template_array'][$priority]['table'];
  763. }
  764. }
  765. if (!$table) {
  766. $table = reset($tables);
  767. }
  768. // get the record_id from the path
  769. if ($_GET['record_id'] !== NULL) {
  770. $form_state['values']['field_group'] = $_GET['record_id'];
  771. if (preg_match('/\d+/', $_GET['record_id'])) {
  772. $priority = $form_state['values']['field_group'];
  773. $table = $form_state['storage']['template_array'][$priority]['table'];
  774. }
  775. }
  776. // Fields and foreign key mappings
  777. $chado_fields = array();
  778. $fk_options = array();
  779. $fk_options['NULL'] = 'None';
  780. $table_description = module_invoke_all('chado_'.$table.'_schema');
  781. //dpm($table_description, 'table description for |'.$table.'|');
  782. if ($field_type == 'foreign key') {
  783. $foreign_field2table = array();
  784. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  785. foreach ($key_array['columns'] as $left_field => $right_field) {
  786. $chado_fields[$left_field] = $left_field;
  787. $foreign_field2table[$left_field] = $key_table;
  788. }
  789. }
  790. reset($chado_fields);
  791. // set default field
  792. if (empty($chado_fields)) {
  793. $field = NULL;
  794. } elseif ($chado_fields[$form_state['values']['chado_field']]) {
  795. $field = $form_state['values']['chado_field'];
  796. } else {
  797. $field = current($chado_fields);
  798. }
  799. // Foreign key options
  800. $foreign_table = $foreign_field2table[$field];
  801. if ($foreign_table) {
  802. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  803. if (preg_match('/^'.$foreign_table.'$/', $form_state['storage']['template_array'][$priority]['table'])) {
  804. $fk_options[$record_name] = $record_name;
  805. }
  806. }
  807. }
  808. } else {
  809. foreach($table_description['fields'] as $field_name => $field_array) {
  810. $chado_fields[$field_name] = $field_name;
  811. }
  812. }
  813. $variables = array(
  814. 'form_state' => $form_state,
  815. 'tables' => $tables,
  816. 'default table' => $table,
  817. 'fields' => $chado_fields,
  818. 'default_field' => $field,
  819. 'priority' => $priority,
  820. 'record_name' => $record_name,
  821. 'table description' => $table_description,
  822. 'foreign key options' => $fk_options,
  823. 'foreign field=>table' => $foreign_field2table,
  824. 'foreign table' => $foreign_table,
  825. );
  826. //dpm($variables, 'variables');
  827. // Start of Form Proper--------------------------------------------------------------
  828. //dpm($form_state, 'Form State');
  829. $form['template_name'] = array(
  830. '#type' => 'item',
  831. '#title' => 'Template',
  832. '#value' => $template->name,
  833. );
  834. $form['template_id'] = array(
  835. '#type' => 'hidden',
  836. '#value' => $template_id,
  837. );
  838. $form['add_fields'] = array(
  839. '#type' => 'fieldset',
  840. '#prefix' => '<div id="tripal_bulk_loader_template-add_field">',
  841. '#suffix' => '</div>',
  842. );
  843. $form['add_fields']['field_type'] = array(
  844. '#type' => 'radios',
  845. '#title' => t('Type of Field'),
  846. '#options' => array(
  847. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  848. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  849. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  850. ),
  851. '#required' => TRUE,
  852. '#default_value' => $field_type,
  853. '#ahah' => array(
  854. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  855. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  856. 'effect' => 'fade'
  857. ),
  858. );
  859. // check template array for records then add one more
  860. if (!$form_state['storage']['record2priority']) {
  861. $groups = array();
  862. } else {
  863. $groups = array_flip($form_state['storage']['record2priority']);
  864. }
  865. $groups['NONE'] = 'Select a Record';
  866. $groups['NEW'] = 'New Record';
  867. $form['add_fields']['field_group'] = array(
  868. '#type' => 'select',
  869. '#title' => 'Record',
  870. '#description' => 'This is used to group a set of fields together allowing '
  871. .'multiple records to be inserted into the same table per line of the spreadsheet',
  872. '#options' => $groups,
  873. '#default_value' => (preg_match('/\w+.*/',$form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE',
  874. '#ahah' => array(
  875. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  876. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  877. 'effect' => 'fade'
  878. ),
  879. '#required' => TRUE,
  880. );
  881. $form['add_fields']['record_name'] = array(
  882. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  883. '#title' => 'Unique Record Name',
  884. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  885. '#suffix' => '</div>',
  886. '#default_value' => $form_state['values']['record_name'],
  887. );
  888. $form['add_fields']['field_title'] = array(
  889. '#type' => 'textfield',
  890. '#title' => t('Human-readable Title for Field'),
  891. '#default_value' => $form_state['values']['field_title'],
  892. );
  893. // Spreadsheet column
  894. $form['add_fields']['columns'] = array(
  895. '#type' => 'fieldset',
  896. '#title' => t('Spreadsheet Column'),
  897. '#collapsible' => TRUE,
  898. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  899. );
  900. $form['add_fields']['columns']['sheet_name'] = array(
  901. '#type' => 'textfield',
  902. '#title' => t('Worksheet'),
  903. '#description' => t('Specify the name of the worksheet.'),
  904. '#size' => 5,
  905. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : 'Sheet1',
  906. );
  907. $form['add_fields']['columns']['column_number'] = array(
  908. '#type' => 'textfield',
  909. '#title' => t('Column'),
  910. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  911. '#size' => 5,
  912. '#default_value' => $form_state['values']['column_number'],
  913. );
  914. $form['add_fields']['columns']['column_exposed'] = array(
  915. '#type' => 'checkbox',
  916. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  917. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  918. '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
  919. );
  920. $form['add_fields']['columns']['column_exposed_desc'] = array(
  921. '#type' => 'textfield',
  922. '#title' => t('Description for exposed field on bulk loading job'),
  923. '#description' => t('This description should tell the user what column should be entered here.'),
  924. '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
  925. );
  926. // Global Value
  927. $form['add_fields']['constant'] = array(
  928. '#type' => 'fieldset',
  929. '#title' => t('Constant'),
  930. '#collapsible' => TRUE,
  931. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  932. );
  933. $form['add_fields']['constant']['constant_value'] = array(
  934. '#type' => 'textfield',
  935. '#title' => t('Constant Value'),
  936. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  937. '#default_value' => $form_state['values']['constant_value']
  938. );
  939. $form['add_fields']['constant']['constant_exposed'] = array(
  940. '#type' => 'checkbox',
  941. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  942. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.')
  943. );
  944. $form['add_fields']['constant']['constant_validate'] = array(
  945. '#type' => 'checkbox',
  946. '#title' => t('Ensure value is in table'),
  947. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  948. );
  949. // Foreign Key
  950. $form['add_fields']['foreign_key'] = array(
  951. '#type' => 'fieldset',
  952. '#title' => 'Foreign Key',
  953. '#collapsible' => TRUE,
  954. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  955. );
  956. $form['add_fields']['foreign_key']['foreign_record'] = array(
  957. '#type' => 'select',
  958. '#title' => 'Record to refer to',
  959. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  960. '#options' => $fk_options,
  961. );
  962. // Chado Field
  963. $form['add_fields']['chado'] = array(
  964. '#type' => 'fieldset',
  965. '#title' => t('Chado Field/Column Details'),
  966. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  967. );
  968. $form['add_fields']['chado']['chado_table'] = array(
  969. '#type' => 'select',
  970. '#title' => t('Chado Table'),
  971. '#options' => $tables,
  972. '#default_value' => $table,
  973. '#ahah' => array(
  974. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  975. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  976. 'effect' => 'fade'
  977. ),
  978. );
  979. $form['add_fields']['chado']['chado_field'] = array(
  980. '#type' => 'select',
  981. '#title' => t('Chado Field/Column'),
  982. '#options' => $chado_fields,
  983. '#default_value' => $form_state['values']['chado_field'],
  984. '#ahah' => array(
  985. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  986. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  987. 'effect' => 'fade'
  988. ),
  989. );
  990. $form['add_fields']['additional'] = array(
  991. '#type' => 'fieldset',
  992. '#title' => 'Additional Options',
  993. );
  994. $form['add_fields']['additional']['required'] = array(
  995. '#type' => 'checkbox',
  996. '#title' => 'Make this file required',
  997. );
  998. $form['add_fields']['additional']['regex_transform'] = array(
  999. '#type' => 'fieldset',
  1000. '#title' => 'Transform Spreadsheet Value Rules',
  1001. '#collapsible' => TRUE,
  1002. '#collapsed' => (!$form_state['storage']['regex']['pattern']) ? TRUE : FALSE,
  1003. );
  1004. $form['add_fields']['additional']['regex_transform']['regex_description'] = array(
  1005. '#type' => 'item',
  1006. '#value' => 'A transformation rule allows you to transform the original value '
  1007. .'(usually from a user submitted spreadsheet) into the form you would like it stored '
  1008. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1009. .'which determines which replacement patterns are applied and captures regions of the '
  1010. .'original value) and a replacement pattern (a string which may contain capture references '
  1011. .'that describes what the new value should be). Each rule is applied to the result of the '
  1012. .'previous rule.'
  1013. );
  1014. $form['add_fields']['additional']['regex_transform']['regex-data'] = array(
  1015. '#tree' => TRUE,
  1016. );
  1017. if (!$form_state['storage']['regex']['pattern']) { $form_state['storage']['regex']['pattern'] = array(); }
  1018. foreach ($form_state['storage']['regex']['pattern'] as $index => $pattern) {
  1019. $data_element = array(
  1020. 'pattern' => array(
  1021. '#type' => 'item',
  1022. '#value' => $pattern,
  1023. ),
  1024. 'replace' => array(
  1025. '#type' => 'item',
  1026. '#value' => $form_state['storage']['regex']['replace'][$index],
  1027. ),
  1028. 'old_index' => array(
  1029. '#type' => 'hidden',
  1030. '#value' => $index,
  1031. ),
  1032. 'new_index' => array(
  1033. '#type' => 'select',
  1034. '#options' => range(0,sizeof($form_state['storage']['regex']['pattern'])-1),
  1035. '#default_value' => $index,
  1036. ),
  1037. 'id' => array(
  1038. '#type' => 'hidden',
  1039. '#value' => $index,
  1040. ),
  1041. 'submit-delete' => array(
  1042. '#type' => 'submit',
  1043. '#value' => 'Delete Transformation',
  1044. '#name' => $index,
  1045. ),
  1046. );
  1047. $form['add_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1048. }
  1049. $form['add_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1050. '#type' => ($form_state['storage']['regex']['pattern']) ? 'submit' : 'hidden',
  1051. '#value' => 'Save Transformation Rule Order'
  1052. );
  1053. $form['add_fields']['additional']['regex_transform']['new_regex'] = array(
  1054. '#type' => 'fieldset',
  1055. '#title' => 'Add a new Transformation Rule',
  1056. );
  1057. $form['add_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1058. '#type' => 'textfield',
  1059. '#title' => 'Match Pattern',
  1060. '#description' => 'You can use standard php regular expressions in this field to specify a '
  1061. .'pattern. Only if this pattern matches the value in the spreadsheet does the replacement '
  1062. .'pattern get applied to the value. To capture a section of your value for use in the '
  1063. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1064. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1065. .' To match and capture any value use <i>.*</i>',
  1066. );
  1067. $form['add_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1068. '#type' => 'textfield',
  1069. '#title' => 'Replacement Pattern',
  1070. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  1071. .'mentioned above. It can include references of the form \n where n is the number of the '
  1072. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1073. .'first set of round brackets.',
  1074. );
  1075. if ($field_type == 'table field') {
  1076. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1077. $form['add_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1078. .'The following references are also available for spreadsheet fields: <b><#column:<i>number</i>#></b>. '
  1079. .'This allows you to substitute other spreadsheet values into the current field. For example, '
  1080. .'if you had the following line:<br>'
  1081. .$tab.'SNP'.$tab.'15-Jan-2011'.$tab.'1'.$tab.'54'.$tab.'Contig34355'
  1082. .'<br> and your current field is for column #1 and you\'re inserting into the chado field '
  1083. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1084. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1085. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1086. }
  1087. $form['add_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1088. '#type' => 'submit',
  1089. '#value' => 'Add Transformation',
  1090. );
  1091. $form['add_fields']['additional']['regex_transform']['test_regex'] = array(
  1092. '#type' => 'fieldset',
  1093. '#title' => 'Test Transformation Rules',
  1094. );
  1095. $form['add_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1096. '#type' => 'textfield',
  1097. '#title' => 'Test Value',
  1098. '#description' => 'This should be a value that you expect the above transformation rules '
  1099. .'to be applied to.',
  1100. '#default_value' => $form_state['storage']['test_regex_test'],
  1101. );
  1102. $form['add_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1103. '#type' => 'textfield',
  1104. '#title' => 'Test Result',
  1105. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1106. .'riles were applied to the Test Value.',
  1107. '#default_value' => $form_state['storage']['test_regex_result'],
  1108. );
  1109. $form['add_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1110. '#type' => 'submit',
  1111. '#value' => 'Test Transformation Rules'
  1112. );
  1113. $form['add_fields']['submit-add_field'] = array(
  1114. '#type' => 'submit',
  1115. '#value' => 'Add Field'
  1116. );
  1117. $form['add_fields']['submit-cancel'] = array(
  1118. '#type' => 'submit',
  1119. '#value' => 'Cancel'
  1120. );
  1121. return $form;
  1122. }
  1123. /**
  1124. * Add Field Submit
  1125. *
  1126. * @param $form
  1127. * The form that was submitted
  1128. * @param $form_state
  1129. * The values and storage for the form
  1130. */
  1131. function tripal_bulk_loader_add_template_field_form_submit ($form, &$form_state) {
  1132. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1133. if (!$form_state['ahah_submission']) {
  1134. if ($op == 'Add Field') {
  1135. $template = $form_state['storage']['template_array'];
  1136. // If new record
  1137. if (preg_match('/NEW/',$form_state['values']['field_group'])) {
  1138. $record_name = $form_state['values']['record_name'];
  1139. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1140. $record2priority[$record_name] = $priority;
  1141. $template[$priority]['table'] = $form_state['values']['chado_table'];
  1142. $template[$priority]['record_id'] = $record_name;
  1143. } else {
  1144. $priority = $form_state['values']['field_group'];
  1145. $record_name = $record2priority[$priority];
  1146. }
  1147. // Add field to template array
  1148. if ($form_state['values']['field_type'] == 'table field') {
  1149. $field = array(
  1150. 'type' => 'table field',
  1151. 'title' => $form_state['values']['field_title'],
  1152. 'field' => $form_state['values']['chado_field'],
  1153. 'required' => $form_state['values']['required'],
  1154. //'allowed values' => empty by default,
  1155. 'spreadsheet sheet' => $form_state['values']['sheet_name'],
  1156. 'spreadsheet column' => $form_state['values']['column_number'],
  1157. 'exposed' => $form_state['values']['column_exposed'],
  1158. 'exposed_description' => $form_state['values']['column_exposed_desc'],
  1159. );
  1160. } elseif ($form_state['values']['field_type'] == 'constant') {
  1161. $field = array(
  1162. 'type' => 'constant',
  1163. 'title' => $form_state['values']['field_title'],
  1164. 'field' => $form_state['values']['chado_field'],
  1165. 'required' => $form_state['values']['required'],
  1166. //'allowed values' => empty by default,
  1167. 'constant value' => $form_state['values']['constant_value'],
  1168. 'exposed' => $form_state['values']['constant_exposed'],
  1169. 'exposed_validate' => $form_state['values']['constant_validate'],
  1170. );
  1171. } elseif ($form_state['values']['field_type'] == 'foreign key') {
  1172. $field = array(
  1173. 'type' => 'foreign key',
  1174. 'title' => $form_state['values']['field_title'],
  1175. 'field' => $form_state['values']['chado_field'],
  1176. 'foreign key' => $form_state['values']['foreign_record'],
  1177. 'required' => $form_state['values']['required'],
  1178. );
  1179. }
  1180. // Deal with any additional options
  1181. if ($form_state['storage']['regex']) {
  1182. $field['regex'] = $form_state['storage']['regex'];
  1183. }
  1184. // Save Template
  1185. $template[$priority]['fields'][] = $field;
  1186. $form_state['storage']['template']->template_array = serialize($template);
  1187. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1188. if ($success) {
  1189. drupal_set_message('Successfully Added Field to Template');
  1190. drupal_set_message('Template Saved.');
  1191. $path = explode('?',$form_state['storage']['referring URL']);
  1192. parse_str($path[1], $query);
  1193. $query['template_id'] = $form_state['storage']['template']->template_id;
  1194. drupal_goto($path[0], $query);
  1195. } else {
  1196. drupal_set_message('Unable to Save Template!', 'error');
  1197. watchdog('T_bulk_loader',
  1198. 'Unable to save bulk loader template: %template',
  1199. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1200. WATCHDOG_ERROR
  1201. );
  1202. }
  1203. } elseif ($op == 'Cancel') {
  1204. $path = explode('?',$form_state['storage']['referring URL']);
  1205. parse_str($path[1], $query);
  1206. $query['template_id'] = $form_state['storage']['template']->template_id;
  1207. drupal_goto($path[0], $query);
  1208. } elseif ($op == 'Add Transformation') {
  1209. // Add transformation rule to original field
  1210. $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1211. $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
  1212. drupal_set_message('Successfully Added Transformation Rule');
  1213. } elseif ($op == 'Save Transformation Rule Order') {
  1214. // Generate new regex array
  1215. $new_regex = array();
  1216. $old_regex = $form_state['storage']['regex'];
  1217. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1218. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1219. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1220. }
  1221. // sort new regex arrays
  1222. asort($new_regex['pattern']);
  1223. asort($new_regex['replace']);
  1224. $form_state['storage']['regex'] = $new_regex;
  1225. } elseif ($op == 'Delete Transformation') {
  1226. // Unset regex rule
  1227. $index = $form_state['clicked_button']['#name'];
  1228. unset($form_state['storage']['regex']['pattern'][$index]);
  1229. unset($form_state['storage']['regex']['replace'][$index]);
  1230. } elseif ($op == 'Test Transformation Rules') {
  1231. $patterns = $form_state['storage']['regex']['pattern'];
  1232. $replaces = $form_state['storage']['regex']['replace'];
  1233. $test_string = $form_state['values']['test_string'];
  1234. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1235. $form_state['storage']['test_regex_test'] = $test_string;
  1236. }
  1237. }
  1238. }
  1239. /**
  1240. * Edit Field Form
  1241. *
  1242. * This form is meant to be called from a bulk loader form. The following should be set
  1243. * in the query section of the path:
  1244. * - template_id=\d+: the template which the edited field is part of
  1245. * - record_id=\d+: the priority or key in the template array of the record the field
  1246. * is currently part of
  1247. * - field_index=\d+: the key of the field in the fields array of the previously
  1248. * specified record
  1249. *
  1250. * @param $form_state
  1251. * Contains the values and storage for the form
  1252. * @return
  1253. * A form array to be rendered by drupal_get_form
  1254. */
  1255. function tripal_bulk_loader_edit_template_field_form (&$form_state = NULL) {
  1256. $form = array();
  1257. $form['#cache'] = TRUE; // Make sure the form is cached.
  1258. // get template id from path
  1259. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  1260. // if there is no template supplied don't return rest of form
  1261. if (!$template_id) {
  1262. return $form;
  1263. }
  1264. // Pre-set Variables needed for form proper------------------------------------------
  1265. // If this is the first load of the form (no form state) we need to initialize some variables
  1266. if (!$form_state['storage']['template']) {
  1267. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  1268. $template = db_fetch_object(db_query($sql, $template_id));
  1269. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1270. $form_state['storage']['template'] = $template;
  1271. $form_state['storage']['record2priority'] = array();
  1272. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1273. if (!is_array($record_array)) { continue; }
  1274. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1275. }
  1276. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  1277. } else {
  1278. $template = $form_state['storage']['template'];
  1279. }
  1280. // get the field from the path
  1281. if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) {
  1282. $priority = $_GET['record_id'];
  1283. $field_index = $_GET['field_index'];
  1284. $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
  1285. $form_state['storage']['original_field'] = $template_field;
  1286. $form_state['storage']['original_field']['priority'] = $priority;
  1287. $form_state['storage']['original_field']['field_index'] = $field_index;
  1288. }
  1289. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type'];
  1290. // Tables and default table
  1291. $tables = tripal_core_get_chado_tables();
  1292. if ($form_state['values']) {
  1293. $table = $form_state['values']['chado_table'];
  1294. } else {
  1295. $table = $form_state['storage']['template_array'][$priority]['table'];
  1296. }
  1297. // Fields and foreign key mappings
  1298. $chado_fields = array();
  1299. $fk_options = array();
  1300. $fk_options['NULL'] = 'None';
  1301. $table_description = module_invoke_all('chado_'.$table.'_schema');
  1302. if ($field_type == 'foreign key') {
  1303. $foreign_field2table = array();
  1304. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1305. foreach ($key_array['columns'] as $left_field => $right_field) {
  1306. $chado_fields[$left_field] = $left_field;
  1307. $foreign_field2table[$left_field] = $key_table;
  1308. }
  1309. }
  1310. reset($chado_fields);
  1311. // set default field
  1312. if (empty($chado_fields)) {
  1313. $field = NULL;
  1314. } elseif ($chado_fields[$form_state['values']['chado_field']]) {
  1315. $field = $form_state['values']['chado_field'];
  1316. } elseif ($template_field['field']) {
  1317. $field = $template_field['field'];
  1318. } else {
  1319. $field = current($chado_fields);
  1320. }
  1321. //dpm($field, 'field');
  1322. // Foreign key options
  1323. $foreign_table = $foreign_field2table[$field];
  1324. if ($foreign_table) {
  1325. foreach ($form_state['storage']['record2priority'] as $record_nameT => $priorityT ) {
  1326. if (preg_match('/^'.$foreign_table.'$/', $form_state['storage']['template_array'][$priorityT]['table'])) {
  1327. $fk_options[$record_nameT] = $record_nameT;
  1328. }
  1329. }
  1330. }
  1331. } else {
  1332. foreach($table_description['fields'] as $field_name => $field_array) {
  1333. $chado_fields[$field_name] = $field_name;
  1334. }
  1335. }
  1336. // dpm(array( 'tables' => $tables, 'default table' => $table, 'record name' => $record_name, 'priority' => $priority,
  1337. // 'fields' => $chad_fields, 'default field' => $field, 'foreign field=>table' => $foreign_field2table,
  1338. // 'table desc' => $table_description, 'foreign record options' => $fk_options, 'foreign table' => $foreign_table
  1339. // ), 'Variables');
  1340. // Start of Form Proper--------------------------------------------------------------
  1341. $form['template_name'] = array(
  1342. '#type' => 'item',
  1343. '#title' => 'Template',
  1344. '#value' => $template->name,
  1345. );
  1346. $form['template_id'] = array(
  1347. '#type' => 'hidden',
  1348. '#value' => $template_id,
  1349. );
  1350. $form['edit_fields'] = array(
  1351. '#type' => 'fieldset',
  1352. '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">',
  1353. '#suffix' => '</div>',
  1354. );
  1355. $form['edit_fields']['field_type'] = array(
  1356. '#type' => 'radios',
  1357. '#title' => t('Type of Field'),
  1358. '#options' => array(
  1359. 'table field' => t('Spreadsheet: Fields which maps to a Spreadsheet Column'),
  1360. 'constant' => t('Constant: Field which remains Constant throughout the Spreadsheet'),
  1361. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  1362. ),
  1363. '#required' => TRUE,
  1364. '#default_value' => $field_type,
  1365. '#ahah' => array(
  1366. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1367. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1368. 'effect' => 'fade'
  1369. ),
  1370. );
  1371. // check template array for records then edit one more
  1372. if (!$form_state['storage']['record2priority']) {
  1373. $groups = array();
  1374. } else {
  1375. $groups = array_flip($form_state['storage']['record2priority']);
  1376. }
  1377. $groups['NONE'] = 'Select a Record';
  1378. $groups['NEW'] = 'New Record';
  1379. $form['edit_fields']['field_group'] = array(
  1380. '#type' => 'select',
  1381. '#title' => 'Record',
  1382. '#description' => 'This is used to group a set of fields together allowing '
  1383. .'multiple records to be inserted into the same table per line of the spreadsheet',
  1384. '#options' => $groups,
  1385. '#default_value' => (preg_match('/(\d+|\w+)/',$form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority,
  1386. '#ahah' => array(
  1387. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1388. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1389. 'effect' => 'fade'
  1390. ),
  1391. '#required' => TRUE,
  1392. );
  1393. $form['edit_fields']['record_name'] = array(
  1394. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  1395. '#title' => 'Unique Record Name',
  1396. '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">',
  1397. '#suffix' => '</div>',
  1398. '#default_value' => $form_state['values']['record_name'],
  1399. );
  1400. $form['edit_fields']['field_title'] = array(
  1401. '#type' => 'textfield',
  1402. '#title' => t('Human-readable Title for Field'),
  1403. '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'],
  1404. );
  1405. // Spreadsheet column
  1406. $form['edit_fields']['columns'] = array(
  1407. '#type' => 'fieldset',
  1408. '#title' => t('Spreadsheet Column'),
  1409. '#collapsible' => TRUE,
  1410. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  1411. );
  1412. $form['edit_fields']['columns']['sheet_name'] = array(
  1413. '#type' => 'textfield',
  1414. '#title' => t('Worksheet'),
  1415. '#description' => t('Specify the name of the worksheet.'),
  1416. '#size' => 5,
  1417. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : $template_field['spreadsheet sheet'],
  1418. );
  1419. $form['edit_fields']['columns']['column_number'] = array(
  1420. '#type' => 'textfield',
  1421. '#title' => t('Column'),
  1422. '#description' => t('Specify the column in the spreadsheet that this field maps to where the first column is 1.'),
  1423. '#size' => 5,
  1424. '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'],
  1425. );
  1426. $form['edit_fields']['columns']['column_exposed'] = array(
  1427. '#type' => 'checkbox',
  1428. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1429. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1430. '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
  1431. );
  1432. $form['edit_fields']['columns']['column_exposed_desc'] = array(
  1433. '#type' => 'textfield',
  1434. '#title' => t('Description for exposed field on bulk loading job'),
  1435. '#description' => t('This description should tell the user what column should be entered here.'),
  1436. '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
  1437. );
  1438. // Global Value
  1439. $form['edit_fields']['constant'] = array(
  1440. '#type' => 'fieldset',
  1441. '#title' => t('Constant'),
  1442. '#collapsible' => TRUE,
  1443. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1444. );
  1445. $form['edit_fields']['constant']['constant_value'] = array(
  1446. '#type' => 'textfield',
  1447. '#title' => t('Constant Value'),
  1448. '#description' => t('Specify the value you wish this field to have regardless of spreadsheet data.'),
  1449. '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'],
  1450. );
  1451. $form['edit_fields']['constant']['constant_exposed'] = array(
  1452. '#type' => 'checkbox',
  1453. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1454. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'),
  1455. '#default_value' => ($form_state['values']['constant_exposed']) ? $form_state['values']['constant_exposed'] : $template_field['exposed'],
  1456. );
  1457. $form['edit_fields']['constant']['constant_validate'] = array(
  1458. '#type' => 'checkbox',
  1459. '#title' => t('Ensure value is in table'),
  1460. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1461. '#default_value' => ($form_state['values']['constant_validate']) ? $form_state['values']['constant_validate'] : $template_field['exposed_validate'],
  1462. );
  1463. // Foreign Key
  1464. $form['edit_fields']['foreign_key'] = array(
  1465. '#type' => 'fieldset',
  1466. '#title' => 'Foreign Key',
  1467. '#collapsible' => TRUE,
  1468. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1469. );
  1470. $form['edit_fields']['foreign_key']['foreign_record'] = array(
  1471. '#type' => 'select',
  1472. '#title' => 'Record to refer to',
  1473. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  1474. '#options' => $fk_options,
  1475. );
  1476. // Chado Field
  1477. $form['edit_fields']['chado'] = array(
  1478. '#type' => 'fieldset',
  1479. '#title' => t('Chado Field/Column Details'),
  1480. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1481. );
  1482. $form['edit_fields']['chado']['chado_table'] = array(
  1483. '#type' => 'select',
  1484. '#title' => t('Chado Table'),
  1485. '#options' => $tables,
  1486. '#default_value' => $table,
  1487. '#ahah' => array(
  1488. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1489. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1490. 'effect' => 'fade'
  1491. ),
  1492. );
  1493. $form['edit_fields']['chado']['chado_field'] = array(
  1494. '#type' => 'select',
  1495. '#title' => t('Chado Field/Column'),
  1496. '#options' => $chado_fields,
  1497. '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'],
  1498. '#ahah' => array(
  1499. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1500. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1501. 'effect' => 'fade'
  1502. ),
  1503. );
  1504. $form['edit_fields']['additional'] = array(
  1505. '#type' => 'fieldset',
  1506. '#title' => 'Additional Options',
  1507. );
  1508. $form['edit_fields']['additional']['required'] = array(
  1509. '#type' => 'checkbox',
  1510. '#title' => 'Make this file required',
  1511. '#default_value' => $template_field['required'],
  1512. );
  1513. $form['edit_fields']['additional']['regex_transform'] = array(
  1514. '#type' => 'fieldset',
  1515. '#title' => 'Transform Spreadsheet Value Rules',
  1516. '#collapsible' => TRUE,
  1517. '#collapsed' => (!$template_field['regex']['pattern']) ? TRUE : FALSE,
  1518. );
  1519. $transformation_msg = '<p>A transformation rule allows you to transform the original value '
  1520. .'(usually from a user submitted spreadsheet) into the form you would like it stored '
  1521. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1522. .'which determines which replacement patterns are applied and captures regions of the '
  1523. .'original value) and a replacement pattern (a string which may contain capture references '
  1524. .'that describes what the new value should be). Each rule is applied to the result of the '
  1525. .'previous rule.<p>';
  1526. $form['edit_fields']['additional']['regex_transform']['regex_description'] = array(
  1527. '#type' => 'item',
  1528. '#value' => $transformation_msg,
  1529. );
  1530. $form['edit_fields']['additional']['regex_transform']['regex-data'] = array(
  1531. '#tree' => TRUE,
  1532. );
  1533. foreach ($template_field['regex']['pattern'] as $index => $pattern) {
  1534. $data_element = array(
  1535. 'pattern' => array(
  1536. '#type' => 'item',
  1537. '#value' => $pattern,
  1538. ),
  1539. 'replace' => array(
  1540. '#type' => 'item',
  1541. '#value' => $template_field['regex']['replace'][$index],
  1542. ),
  1543. 'old_index' => array(
  1544. '#type' => 'hidden',
  1545. '#value' => $index,
  1546. ),
  1547. 'new_index' => array(
  1548. '#type' => 'select',
  1549. '#options' => range(0,sizeof($template_field['regex']['pattern'])-1),
  1550. '#default_value' => $index,
  1551. ),
  1552. 'id' => array(
  1553. '#type' => 'hidden',
  1554. '#value' => $index,
  1555. ),
  1556. 'submit-delete' => array(
  1557. '#type' => 'submit',
  1558. '#value' => 'Delete Transformation',
  1559. '#name' => $index,
  1560. ),
  1561. );
  1562. $form['edit_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1563. }
  1564. $form['edit_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1565. '#type' => 'submit',
  1566. '#value' => 'Save Transformation Rule Order'
  1567. );
  1568. $form['edit_fields']['additional']['regex_transform']['new_regex'] = array(
  1569. '#type' => 'fieldset',
  1570. '#title' => 'Add a new Transformation Rule',
  1571. );
  1572. $form['edit_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1573. '#type' => 'textfield',
  1574. '#title' => 'Match Pattern',
  1575. '#description' => 'You can use standard <b>php regular expressions</b> in this field to specify a '
  1576. .'pattern. Only if this pattern matches the value in the spreadsheet does the replacement '
  1577. .'pattern get applied to the value. To capture a section of your value for use in the '
  1578. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1579. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1580. .' To match and capture any value use <i>.*</i>',
  1581. );
  1582. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1583. '#type' => 'textfield',
  1584. '#title' => 'Replacement Pattern',
  1585. '#description' => '<p>This pattern should contain the text you want to replace the match pattern '
  1586. .'mentioned above. It can include references of the form <b>\n</b> where n is the number of the '
  1587. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1588. .'first set of round brackets.</p>',
  1589. );
  1590. if ($field_type == 'table field') {
  1591. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1592. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1593. .'The following references are also available for spreadsheet fields: <b><#column:<i>number</i>#></b>. '
  1594. .'This allows you to substitute other spreadsheet values into the current field. For example, '
  1595. .'if you had the following line:<br>'
  1596. .$tab.'SNP'.$tab.'15-Jan-2011'.$tab.'1'.$tab.'54'.$tab.'Contig34355'
  1597. .'<br> and your current field is for column #1 and you\'re inserting into the chado field '
  1598. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1599. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1600. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1601. }
  1602. $form['edit_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1603. '#type' => 'submit',
  1604. '#value' => 'Add Transformation',
  1605. );
  1606. $form['edit_fields']['additional']['regex_transform']['test_regex'] = array(
  1607. '#type' => 'fieldset',
  1608. '#title' => 'Test Transformation Rules',
  1609. );
  1610. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1611. '#type' => 'textfield',
  1612. '#title' => 'Test Value',
  1613. '#description' => 'This should be a value that you expect the above transformation rules '
  1614. .'to be applied to.',
  1615. '#default_value' => $form_state['storage']['test_regex_test'],
  1616. );
  1617. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1618. '#type' => 'textfield',
  1619. '#title' => 'Test Result',
  1620. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1621. .'riles were applied to the Test Value.',
  1622. '#default_value' => $form_state['storage']['test_regex_result'],
  1623. );
  1624. $form['edit_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1625. '#type' => 'submit',
  1626. '#value' => 'Test Transformation Rules'
  1627. );
  1628. $form['edit_fields']['submit-edit_field'] = array(
  1629. '#type' => 'submit',
  1630. '#value' => 'Edit Field'
  1631. );
  1632. $form['edit_fields']['submit-cancel'] = array(
  1633. '#type' => 'submit',
  1634. '#value' => 'Cancel'
  1635. );
  1636. return $form;
  1637. }
  1638. /**
  1639. * Edit Field Form Submit
  1640. *
  1641. * @param $form
  1642. * The form that was submitted
  1643. * @param $form_state
  1644. * The values and storage for the form
  1645. */
  1646. function tripal_bulk_loader_edit_template_field_form_submit ($form, &$form_state) {
  1647. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1648. //dpm($op, 'Operation Submitted');
  1649. //Clear Test
  1650. $form_state['storage']['test_regex_result'] = NULL;
  1651. $form_state['storage']['test_regex_test'] = NULL;
  1652. if (!$form_state['ahah_submission']) {
  1653. if ($op == 'Edit Field') {
  1654. // If new record
  1655. if (preg_match('/NEW/',$form_state['values']['field_group'])) {
  1656. // add new record
  1657. $record_name = $form_state['values']['record_name'];
  1658. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1659. $old_priority = $form_state['storage']['original_field']['priority'];
  1660. $field_index = $form_state['storage']['original_field']['field_index'];
  1661. $form_state['storage']['record2priority'][$record_name] = $priority;
  1662. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1663. $form_state['storage']['template_array'][$priority]['record_id'] = $record_name;
  1664. } else {
  1665. $priority = $form_state['values']['field_group'];
  1666. $old_priority = $form_state['storage']['original_field']['priority'];
  1667. $field_index = $form_state['storage']['original_field']['field_index'];
  1668. $record_name = $form_state['storage']['record2priority'][$priority];
  1669. }
  1670. $field = $form_state['storage']['original_field'];
  1671. if ($form_state['values']['field_type'] == 'table field') {
  1672. $field['type'] = 'table field';
  1673. $field['title'] = $form_state['values']['field_title'];
  1674. $field['field'] = $form_state['values']['chado_field'];
  1675. $field['required'] = $form_state['values']['required'];
  1676. //$field['allowed values'] = empty by default;
  1677. $field['spreadsheet sheet'] = $form_state['values']['sheet_name'];
  1678. $field['spreadsheet column'] = $form_state['values']['column_number'];
  1679. $field['exposed'] = $form_state['values']['column_exposed'];
  1680. $field['exposed_description'] = $form_state['values']['column_exposed_desc'];
  1681. } elseif ($form_state['values']['field_type'] == 'constant') {
  1682. $field['type'] = 'constant';
  1683. $field['title'] = $form_state['values']['field_title'];
  1684. $field['field'] = $form_state['values']['chado_field'];
  1685. $field['required'] = $form_state['values']['required'];
  1686. //$field['allowed values'] = empty by default;
  1687. $field['constant value'] = $form_state['values']['constant_value'];
  1688. $field['exposed_validate'] = $form_state['values']['constant_validate'];
  1689. $field['exposed'] = $form_state['values']['constant_exposed'];
  1690. } elseif ($form_state['values']['field_type'] == 'foreign key') {
  1691. $field['type'] = 'foreign key';
  1692. $field['title'] = $form_state['values']['field_title'];
  1693. $field['field'] = $form_state['values']['chado_field'];
  1694. $field['foreign key'] = $form_state['values']['foreign_record'];
  1695. $field['required'] = $form_state['values']['required'];
  1696. }
  1697. // Deal with any additional options
  1698. // if the record has changed...
  1699. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1700. if ($old_priority != $priority) {
  1701. $form_state['storage']['template_array'][$priority]['fields'][] = $field;
  1702. unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]);
  1703. // if there are no fields left delete the old record
  1704. if (!$form_state['storage']['template_array'][$old_priority]['fields']) {
  1705. unset($form_state['storage']['template_array'][$old_priority]);
  1706. }
  1707. } else {
  1708. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field;
  1709. }
  1710. // Save Template
  1711. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1712. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1713. if ($success) {
  1714. drupal_set_message('Successfully Updated Field');
  1715. drupal_set_message('Template Saved.');
  1716. $path = explode('?',$form_state['storage']['referring URL']);
  1717. parse_str($path[1], $query);
  1718. $query['template_id'] = $form_state['storage']['template']->template_id;
  1719. drupal_goto($path[0], $query);
  1720. } else {
  1721. drupal_set_message('Unable to Save Template!', 'error');
  1722. watchdog('T_bulk_loader',
  1723. 'Unable to save bulk loader template: %template',
  1724. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1725. WATCHDOG_ERROR
  1726. );
  1727. }
  1728. } elseif ($op == 'Cancel') {
  1729. $path = explode('?',$form_state['storage']['referring URL']);
  1730. parse_str($path[1], $query);
  1731. $query['template_id'] = $form_state['storage']['template']->template_id;
  1732. drupal_goto($path[0], $query);
  1733. } elseif ($op == 'Add Transformation') {
  1734. // Add transformation rule to original field
  1735. $form_state['storage']['original_field']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1736. $form_state['storage']['original_field']['regex']['replace'][] = $form_state['values']['replace'];
  1737. // Add original field back into template
  1738. $priority = $form_state['storage']['original_field']['priority'];
  1739. $field_index = $form_state['storage']['original_field']['field_index'];
  1740. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1741. // Save Template
  1742. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1743. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1744. if ($success) {
  1745. drupal_set_message('Successfully Added Transformation Rule');
  1746. drupal_set_message('Template Saved.');
  1747. } else {
  1748. drupal_set_message('Unable to Save Template!', 'error');
  1749. watchdog('T_bulk_loader',
  1750. 'Unable to save bulk loader template: %template',
  1751. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1752. WATCHDOG_ERROR
  1753. );
  1754. }
  1755. } elseif ($op == 'Save Transformation Rule Order') {
  1756. // Generate new regex array
  1757. $new_regex = array();
  1758. $old_regex = $form_state['storage']['original_field']['regex'];
  1759. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1760. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1761. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1762. }
  1763. // sort new regex arrays
  1764. asort($new_regex['pattern']);
  1765. asort($new_regex['replace']);
  1766. // Add back to original field
  1767. $form_state['storage']['original_field']['regex'] = $new_regex;
  1768. $priority = $form_state['storage']['original_field']['priority'];
  1769. $field_index = $form_state['storage']['original_field']['field_index'];
  1770. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1771. // Save Template
  1772. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1773. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1774. if ($success) {
  1775. drupal_set_message('Successfully Reordered Transformation Rules');
  1776. drupal_set_message('Template Saved.');
  1777. } else {
  1778. drupal_set_message('Unable to Save Template!', 'error');
  1779. watchdog('T_bulk_loader',
  1780. 'Unable to save bulk loader template: %template',
  1781. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1782. WATCHDOG_ERROR
  1783. );
  1784. }
  1785. } elseif ($op == 'Delete Transformation') {
  1786. // Unset regex rule
  1787. $index = $form_state['clicked_button']['#name'];
  1788. unset($form_state['storage']['original_field']['regex']['pattern'][$index]);
  1789. unset($form_state['storage']['original_field']['regex']['replace'][$index]);
  1790. $priority = $form_state['storage']['original_field']['priority'];
  1791. $field_index = $form_state['storage']['original_field']['field_index'];
  1792. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1793. // Save Template
  1794. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1795. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1796. if ($success) {
  1797. drupal_set_message('Successfully Reordered Transformation Rules');
  1798. drupal_set_message('Template Saved.');
  1799. } else {
  1800. drupal_set_message('Unable to Save Template!', 'error');
  1801. watchdog('T_bulk_loader',
  1802. 'Unable to save bulk loader template: %template',
  1803. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1804. WATCHDOG_ERROR
  1805. );
  1806. }
  1807. } elseif ($op == 'Test Transformation Rules') {
  1808. $patterns = $form_state['storage']['original_field']['regex']['pattern'];
  1809. $replaces = $form_state['storage']['original_field']['regex']['replace'];
  1810. $test_string = $form_state['values']['test_string'];
  1811. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1812. $form_state['storage']['test_regex_test'] = $test_string;
  1813. }
  1814. }
  1815. }
  1816. //////////////////////////////////////////////////////////////////////////////////////
  1817. // AHAH Callbacks
  1818. //////////////////////////////////////////////////////////////////////////////////////
  1819. /**
  1820. * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_field_form
  1821. *
  1822. * @return
  1823. * JSON Data printed to the screen
  1824. */
  1825. function tripal_bulk_loader_add_field_ahah () {
  1826. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1827. $form_build_id = $_POST['form_build_id'];
  1828. $form = form_get_cache($form_build_id, $form_state);
  1829. $args = $form['#parameters'];
  1830. $form_id = array_shift($args);
  1831. $form_state['post'] = $form['#post'] = $_POST;
  1832. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1833. $form_state['ahah_submission'] = TRUE;
  1834. $form['#programmed'] = $form['#redirect'] = FALSE;
  1835. drupal_process_form($form_id, $form, $form_state);
  1836. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1837. $form_element = $form['add_fields'];
  1838. // Remove the wrapper so we don't double it up.
  1839. //unset($form_element['#prefix'], $form_element['#suffix']);
  1840. $output = theme('status_messages');
  1841. $output .= drupal_render($form_element);
  1842. // Final rendering callback.
  1843. print drupal_json(array('status' => TRUE, 'data' => $output));
  1844. exit();
  1845. }
  1846. /**
  1847. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
  1848. *
  1849. * @return
  1850. * JSON Data printed to the screen
  1851. */
  1852. function tripal_bulk_loader_edit_field_ahah () {
  1853. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  1854. $form_build_id = $_POST['form_build_id'];
  1855. $form = form_get_cache($form_build_id, $form_state);
  1856. $args = $form['#parameters'];
  1857. $form_id = array_shift($args);
  1858. $form_state['post'] = $form['#post'] = $_POST;
  1859. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  1860. $form_state['ahah_submission'] = TRUE;
  1861. $form['#programmed'] = $form['#redirect'] = FALSE;
  1862. drupal_process_form($form_id, $form, $form_state);
  1863. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  1864. $form_element = $form['edit_fields'];
  1865. // Remove the wrapper so we don't double it up.
  1866. unset($form_element['#prefix'], $form_element['#suffix']);
  1867. $output = theme('status_messages');
  1868. $output .= drupal_render($form_element);
  1869. // Final rendering callback.
  1870. print drupal_json(array('status' => TRUE, 'data' => $output));
  1871. exit();
  1872. }