tripal_bulk_loader.admin.templates.inc 98 KB

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