tripal_bulk_loader.admin.inc 83 KB

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