tripal_bulk_loader.admin.inc 80 KB

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