tripal_bulk_loader.admin.inc 86 KB

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