tripal_bulk_loader.admin.inc 78 KB

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