tripal_bulk_loader.admin.templates.inc 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641
  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. $form['edit_record']['update_if_duplicate'] = array(
  739. '#type' => 'checkbox',
  740. '#title' => t('UPDATE if duplicate (no insert)'),
  741. '#description' => t('If this is not the first time this record has been added then perform an update rather than an insert.'),
  742. '#default_value' => $update_if_duplicate
  743. );
  744. $form['edit_record']['optional'] = array(
  745. '#type' => 'checkbox',
  746. '#title' => t('Optional'),
  747. '#description' => t('The insert, update or select will only be performed only if all required data are present'),
  748. '#default_value' => $optional
  749. );
  750. $form['edit_record']['disable'] = array(
  751. '#type' => 'checkbox',
  752. '#title' => t('Disable this record'),
  753. '#description' => t("Check this box to ignore this record (not perform select or insert) during template loading. Uncheck to re-enable the record"),
  754. '#default_value' => $disable,
  755. );
  756. $form['edit_record']['submit-edit_record'] = array(
  757. '#type' => 'submit',
  758. '#value' => 'Edit Record'
  759. );
  760. $form['edit_record']['submit-cancel'] = array(
  761. '#type' => 'submit',
  762. '#value' => 'Cancel'
  763. );
  764. return $form;
  765. }
  766. function tripal_bulk_loader_edit_template_record_form_validate($form, $form_state) {
  767. // Don't worry about validation when Cancel button is clicked
  768. if ($form_state['clicked_button']['#value'] == 'Edit Record') {
  769. $is_unique = tripal_bulk_loader_is_record_name_unique(
  770. $form_state['values']['record_name'],
  771. $form_state['values']['template_id'],
  772. $form_state['storage']['template_array'],
  773. $form_state['values']['field_group']
  774. );
  775. if (!$is_unique) {
  776. form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
  777. }
  778. }
  779. }
  780. /**
  781. * Edit Record Form Submit
  782. *
  783. * @param $form
  784. * The form that was submitted
  785. * @param $form_state
  786. * Contains the values and storage for the form
  787. */
  788. function tripal_bulk_loader_edit_template_record_form_submit($form, &$form_state) {
  789. //dpm($form_state, 'form state -start submit');
  790. if (!$form_state['ahah_submission']) {
  791. if ($form_state['values']['op'] == 'Edit Record') {
  792. $template = $form_state['storage']['template_array'];
  793. $original_record_name = $template[ $form_state['storage']['original_priority'] ]['record_id'];
  794. // Edit Record
  795. $record = $template[ $form_state['storage']['original_priority'] ];
  796. $record['record_id'] = $form_state['values']['record_name'];
  797. $record['mode'] = $form_state['values']['mode'];
  798. $record['table'] = $form_state['values']['chado_table'];
  799. $record['select_if_duplicate'] = $form_state['values']['select_if_duplicate'];
  800. $record['update_if_duplicate'] = $form_state['values']['update_if_duplicate'];
  801. $record['select_optional'] = $form_state['values']['select_optional'];
  802. $record['disable'] = $form_state['values']['disable'];
  803. $record['optional'] = $form_state['values']['optional'];
  804. if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
  805. $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
  806. $template[ $form_state['values']['field_group'] ] = $record;
  807. unset($template[ $form_state['storage']['original_priority'] ]);
  808. }
  809. else {
  810. $template[ $form_state['storage']['original_priority'] ] = $record;
  811. }
  812. // Go through all records and update this record name where it was used for a foreign key
  813. // Foreach field in each record that is of type: foreign key
  814. foreach ($template as $priority => $record) {
  815. foreach ($record['fields'] as $field_index => $field) {
  816. if ($field['type'] === 'foreign key') {
  817. // Check if this points to the old record name and if so, update it
  818. if ($field['foreign key'] === $original_record_name) {
  819. $template[$priority]['fields'][$field_index]['foreign key'] = $form_state['values']['record_name'];
  820. }
  821. }
  822. }
  823. }
  824. // Save Template
  825. $form_state['storage']['template']->template_array = serialize($template);
  826. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  827. if ($success) {
  828. drupal_set_message(t('Successfully Updated Template Record'));
  829. drupal_set_message(t('Template Saved.'));
  830. $path = explode('?', $form_state['storage']['referring URL']);
  831. parse_str($path[1], $query);
  832. $query['template_id'] = $form_state['storage']['template']->template_id;
  833. drupal_goto($path[0], $query);
  834. }
  835. else {
  836. drupal_set_message(t('Unable to Save Template!'), 'error');
  837. watchdog('T_bulk_loader',
  838. 'Unable to save bulk loader template: %template',
  839. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  840. WATCHDOG_ERROR
  841. );
  842. }
  843. }
  844. elseif ($form_state['values']['op'] == 'Cancel') {
  845. $path = explode('?', $form_state['storage']['referring URL']);
  846. parse_str($path[1], $query);
  847. $query['template_id'] = $form_state['storage']['template']->template_id;
  848. //dpm('Redirecting to: '.$path[0].'?'.print_r($query,TRUE).' where the referring URL:'.$form_state['storage']['referring URL']);
  849. drupal_goto($path[0], $query);
  850. }
  851. }
  852. }
  853. /**
  854. * @section
  855. * Add/Edit Field Forms
  856. */
  857. /**
  858. * Add Field Form
  859. *
  860. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  861. * can use the following in the query of the path to set defaults for a given template:
  862. * - template_id=\d+: the template to add the field to
  863. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  864. *
  865. * @param $form_state
  866. * Contains the values and storage for the form
  867. * @return
  868. * A form array to be rendered by drupal_get_form
  869. */
  870. function tripal_bulk_loader_add_template_field_form(&$form_state = NULL) {
  871. $form = array();
  872. $form['#cache'] = TRUE; // Make sure the form is cached.
  873. // get template id from path
  874. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  875. // if there is no template supplied don't return rest of form
  876. if (!$template_id) {
  877. return $form;
  878. }
  879. // Pre-set Variables needed for form proper------------------------------------------
  880. // If this is the first load of the form (no form state) we need to initialize some variables
  881. if (!$form_state['storage']['template']) {
  882. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  883. $template = db_fetch_object(db_query($sql, $template_id));
  884. $form_state['storage']['template_array'] = unserialize($template->template_array);
  885. $form_state['storage']['template'] = $template;
  886. $form_state['storage']['record2priority'] = array();
  887. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  888. if (!is_array($record_array)) {
  889. continue;
  890. }
  891. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  892. }
  893. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  894. }
  895. else {
  896. $template = $form_state['storage']['template'];
  897. }
  898. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field';
  899. // Tables and default table
  900. $tables = tripal_core_get_chado_tables(TRUE);
  901. if ($form_state['values']) {
  902. if (!preg_match('/^' . current($tables) . '$/', $form_state['values']['chado_table'])) {
  903. $table = $form_state['values']['chado_table'];
  904. }
  905. elseif ($form_state['values']['record_name']) {
  906. $record_name = $form_state['values']['record_name'];
  907. $priority = $form_state['storage']['record2priority'][$record_name];
  908. $table = $form_state['storage']['template_array'][$priority]['table'];
  909. }
  910. else {
  911. $priority = $form_state['values']['field_group'];
  912. $table = $form_state['storage']['template_array'][$priority]['table'];
  913. }
  914. }
  915. if (!$table) {
  916. $table = reset($tables);
  917. }
  918. // get the record_id from the path
  919. if ($_GET['record_id'] !== NULL) {
  920. $form_state['values']['field_group'] = $_GET['record_id'];
  921. if (preg_match('/\d+/', $_GET['record_id'])) {
  922. $priority = $form_state['values']['field_group'];
  923. $table = $form_state['storage']['template_array'][$priority]['table'];
  924. }
  925. }
  926. $show_all = $form_state['values']['show_all_records'];
  927. $table_description = tripal_core_get_chado_table_schema($table);
  928. // Fields and foreign key / referral mappings
  929. // build the fields array
  930. $chado_fields = array();
  931. foreach ($table_description['fields'] as $field_name => $field_array) {
  932. $chado_fields[$field_name] = $field_name;
  933. }
  934. $fk_options = array();
  935. $fk_options['NULL'] = 'None';
  936. if ($field_type == 'foreign key' and !$show_all) {
  937. $foreign_field2table = array();
  938. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  939. foreach ($key_array['columns'] as $left_field => $right_field) {
  940. //$chado_fields[$left_field] = $left_field;
  941. $foreign_field2table[$left_field] = $key_table;
  942. }
  943. }
  944. // reset($chado_fields);
  945. // set default field
  946. if (empty($chado_fields)) {
  947. $field = NULL;
  948. }
  949. elseif ($chado_fields[$form_state['values']['chado_field']]) {
  950. $field = $form_state['values']['chado_field'];
  951. }
  952. else {
  953. $field = current($chado_fields);
  954. }
  955. // Foreign key options
  956. $foreign_table = $foreign_field2table[$field];
  957. if ($foreign_table) {
  958. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  959. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority]['table'])) {
  960. $fk_options[$record_name] = $record_name;
  961. }
  962. }
  963. }
  964. }
  965. // the user wants to see all of the records
  966. elseif ($field_type == 'foreign key' and $show_all) {
  967. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  968. $fk_options[$record_name] = $record_name;
  969. }
  970. }
  971. // build the list of referrer table fields. This list is used
  972. // for the referring table field drop down. It has two groups
  973. // one for foreign keys and another for additional fields.
  974. $ref_chado_fields = array();
  975. if ($field_type == 'foreign key') {
  976. $fk_rec = $form_state['values']['foreign_record'];
  977. if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') {
  978. // first add in the foreign key field in the referring table
  979. // that corresponds to this field (if one exists).
  980. $fk_priority = $form_state['storage']['record2priority'][$fk_rec];
  981. $fk_table = $form_state['storage']['template_array'][$fk_priority]['table'];
  982. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  983. foreach ($key_array['columns'] as $left_field => $right_field) {
  984. if ($key_table == $fk_table and $left_field == $field) {
  985. $ref_chado_fields['Foreign Key'][$right_field] = $right_field;
  986. }
  987. }
  988. }
  989. // now add in the remaining fields of the referring record's table.
  990. $fk_description = tripal_core_get_chado_table_schema($fk_table);
  991. foreach ($fk_description['fields'] as $fk_field_name => $fk_farray) {
  992. // don't include the FK field it's included above
  993. if (in_array('Foreign Key', $ref_chado_fields)) {
  994. if (in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) {
  995. continue;
  996. }
  997. }
  998. $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name;
  999. }
  1000. }
  1001. }
  1002. // Ensure other field type fields are reset/cleared except for those of the current type
  1003. // ie: if the type is changed from spreadsheet column to constant, the spreadsheet column
  1004. // fields should be cleared since they don't apply to a constant
  1005. switch ($field_type) {
  1006. case 'table field':
  1007. // clear constant
  1008. $form_state['values']['constant_value'] = NULL;
  1009. // clear foreign key
  1010. // clears by default :)
  1011. break;
  1012. case 'constant':
  1013. // clear spreadsheet column
  1014. $form_state['values']['column_number'] = NULL;
  1015. $form_state['values']['column_exposed'] = NULL;
  1016. $template_field['exposed'] = NULL;
  1017. $form_state['values']['column_exposed_desc'] = NULL;
  1018. $template_field['exposed_description'] = NULL;
  1019. // clear foreign key
  1020. // clears by default :)
  1021. break;
  1022. case 'foreign key':
  1023. // clear constant
  1024. $form_state['values']['constant_value'] = NULL;
  1025. // clear spreadsheet column
  1026. $form_state['values']['column_number'] = NULL;
  1027. $form_state['values']['column_exposed'] = NULL;
  1028. $template_field['exposed'] = NULL;
  1029. $form_state['values']['column_exposed_desc'] = NULL;
  1030. $template_field['exposed_description'] = NULL;
  1031. break;
  1032. }
  1033. // Start of Form Proper-------------------------------------------------------------
  1034. $form['template_name'] = array(
  1035. '#type' => 'item',
  1036. '#title' => 'Template',
  1037. '#value' => $template->name,
  1038. );
  1039. $form['template_id'] = array(
  1040. '#type' => 'hidden',
  1041. '#value' => $template_id,
  1042. );
  1043. $form['add_fields'] = array(
  1044. '#type' => 'fieldset',
  1045. '#prefix' => '<div id="tripal_bulk_loader_template-add_field">',
  1046. '#suffix' => '</div>',
  1047. );
  1048. $form['add_fields']['field_type'] = array(
  1049. '#type' => 'radios',
  1050. '#title' => t('Type of Field'),
  1051. '#options' => array(
  1052. 'table field' => t('Data: A Field which maps to a column in the supplied file.'),
  1053. 'constant' => t('Constant: Field which remains Constant throughout the file'),
  1054. 'foreign key' => t('Record Referral: Fields which map to a record in another table'),
  1055. ),
  1056. '#required' => TRUE,
  1057. '#default_value' => $field_type,
  1058. '#ahah' => array(
  1059. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1060. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1061. 'effect' => 'fade'
  1062. ),
  1063. );
  1064. // check template array for records then add one more
  1065. if (!$form_state['storage']['record2priority']) {
  1066. $groups = array();
  1067. }
  1068. else {
  1069. $groups = array_flip($form_state['storage']['record2priority']);
  1070. }
  1071. $groups['NONE'] = 'Select a Record';
  1072. $groups['NEW'] = 'New Record';
  1073. $form['add_fields']['field_group'] = array(
  1074. '#type' => 'select',
  1075. '#title' => 'Record',
  1076. '#description' => 'This is used to group a set of fields together allowing '
  1077. .'multiple records to be inserted into the same table per line of the file',
  1078. '#options' => $groups,
  1079. '#default_value' => (preg_match('/\w+.*/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE',
  1080. '#ahah' => array(
  1081. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1082. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1083. 'effect' => 'fade'
  1084. ),
  1085. '#required' => TRUE,
  1086. );
  1087. $form['add_fields']['record_name'] = array(
  1088. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  1089. '#title' => 'Unique Record Name',
  1090. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  1091. '#suffix' => '</div>',
  1092. '#default_value' => $form_state['values']['record_name'],
  1093. );
  1094. $form['add_fields']['field_title'] = array(
  1095. '#type' => 'textfield',
  1096. '#title' => t('Human-readable Title for Field'),
  1097. '#default_value' => $form_state['values']['field_title'],
  1098. );
  1099. // Chado Field
  1100. $form['add_fields']['chado'] = array(
  1101. '#type' => 'fieldset',
  1102. '#title' => t('Chado Field/Column Details'),
  1103. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1104. );
  1105. $form['add_fields']['chado']['chado_table'] = array(
  1106. '#type' => 'select',
  1107. '#title' => t('Chado Table'),
  1108. '#options' => $tables,
  1109. '#default_value' => $table,
  1110. '#ahah' => array(
  1111. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1112. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1113. 'effect' => 'fade'
  1114. ),
  1115. );
  1116. $form['add_fields']['chado']['chado_field'] = array(
  1117. '#type' => 'select',
  1118. '#title' => t('Chado Field/Column'),
  1119. '#options' => $chado_fields,
  1120. '#default_value' => $form_state['values']['chado_field'],
  1121. '#ahah' => array(
  1122. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1123. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1124. 'effect' => 'fade'
  1125. ),
  1126. );
  1127. // loading file data column
  1128. $form['add_fields']['columns'] = array(
  1129. '#type' => 'fieldset',
  1130. '#title' => t('Data File Column'),
  1131. '#collapsible' => TRUE,
  1132. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  1133. );
  1134. /**
  1135. $form['add_fields']['columns']['sheet_name'] = array(
  1136. '#type' => 'textfield',
  1137. '#title' => t('Worksheet'),
  1138. '#description' => t('Specify the name of the worksheet.'),
  1139. '#size' => 5,
  1140. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : 'Sheet1',
  1141. );
  1142. */
  1143. $form['add_fields']['columns']['column_number'] = array(
  1144. '#type' => 'textfield',
  1145. '#title' => t('Column'),
  1146. '#description' => t('Specify the column in the data that this field maps to where the first column is 1.'),
  1147. '#size' => 5,
  1148. '#default_value' => $form_state['values']['column_number'],
  1149. );
  1150. $form['add_fields']['columns']['column_exposed'] = array(
  1151. '#type' => 'checkbox',
  1152. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1153. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1154. '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
  1155. );
  1156. $form['add_fields']['columns']['column_exposed_desc'] = array(
  1157. '#type' => 'textfield',
  1158. '#title' => t('Description for exposed field on bulk loading job'),
  1159. '#description' => t('This description should tell the user what column should be entered here.'),
  1160. '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
  1161. );
  1162. // Global Value
  1163. $form['add_fields']['constant'] = array(
  1164. '#type' => 'fieldset',
  1165. '#title' => t('Constant'),
  1166. '#collapsible' => TRUE,
  1167. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1168. );
  1169. $form['add_fields']['constant']['constant_value'] = array(
  1170. '#type' => 'textfield',
  1171. '#title' => t('Constant Value'),
  1172. '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
  1173. '#default_value' => $form_state['values']['constant_value']
  1174. );
  1175. $form['add_fields']['constant']['constant_exposed'] = array(
  1176. '#type' => 'checkbox',
  1177. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1178. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.')
  1179. );
  1180. $form['add_fields']['constant']['constant_validate'] = array(
  1181. '#type' => 'checkbox',
  1182. '#title' => t('Ensure value is in table'),
  1183. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1184. );
  1185. // Foreign Key / Referrer
  1186. $form['add_fields']['foreign_key'] = array(
  1187. '#type' => 'fieldset',
  1188. '#title' => 'Record Referral',
  1189. '#collapsible' => TRUE,
  1190. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1191. );
  1192. $form['add_fields']['foreign_key']['show_all_records'] = array(
  1193. '#type' => 'checkbox',
  1194. '#title' => 'Refer to any record',
  1195. '#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'),
  1196. '#default_value' => $show_all,
  1197. '#ahah' => array(
  1198. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1199. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1200. 'effect' => 'fade'
  1201. ),
  1202. );
  1203. $form['add_fields']['foreign_key']['foreign_record'] = array(
  1204. '#type' => 'select',
  1205. '#title' => 'Record to refer to',
  1206. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1207. '#options' => $fk_options,
  1208. '#ahah' => array(
  1209. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1210. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1211. 'effect' => 'fade'
  1212. ),
  1213. '#default_value' => ($form_state['values']['foreign_record']) ? $form_state['values']['foreign_record'] : $template_field['foreign key'],
  1214. );
  1215. $form['add_fields']['foreign_key']['foreign_field'] = array(
  1216. '#type' => 'select',
  1217. '#title' => 'Field to refer to',
  1218. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1219. '#options' => $ref_chado_fields,
  1220. '#default_value' => ($form_state['values']['foreign_field']) ? $form_state['values']['foreign_field'] : $template_field['foreign_field'],
  1221. );
  1222. $form['add_fields']['additional'] = array(
  1223. '#type' => 'fieldset',
  1224. '#title' => 'Additional Options',
  1225. );
  1226. $form['add_fields']['additional']['required'] = array(
  1227. '#type' => 'checkbox',
  1228. '#title' => 'Make this field required',
  1229. '#default_value' => (!empty($form_state['values']['required'])) ? $form_state['values']['required'] : $template_field['required'],
  1230. );
  1231. $form['add_fields']['additional']['regex_transform'] = array(
  1232. '#type' => 'fieldset',
  1233. '#title' => 'Transform Data File Value Rules',
  1234. '#collapsible' => TRUE,
  1235. '#collapsed' => (!$form_state['storage']['regex']['pattern']) ? TRUE : FALSE,
  1236. );
  1237. $form['add_fields']['additional']['regex_transform']['regex_description'] = array(
  1238. '#type' => 'item',
  1239. '#value' => 'A transformation rule allows you to transform the original value '
  1240. .'(usually from a user submitted data file) into the form you would like it stored '
  1241. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1242. .'which determines which replacement patterns are applied and captures regions of the '
  1243. .'original value) and a replacement pattern (a string which may contain capture references '
  1244. .'that describes what the new value should be). Each rule is applied to the result of the '
  1245. .'previous rule.'
  1246. );
  1247. $form['add_fields']['additional']['regex_transform']['regex-data'] = array(
  1248. '#tree' => TRUE,
  1249. );
  1250. if (!$form_state['storage']['regex']['pattern']) {
  1251. $form_state['storage']['regex']['pattern'] = array();
  1252. }
  1253. foreach ($form_state['storage']['regex']['pattern'] as $index => $pattern) {
  1254. $data_element = array(
  1255. 'pattern' => array(
  1256. '#type' => 'item',
  1257. '#value' => $pattern,
  1258. ),
  1259. 'replace' => array(
  1260. '#type' => 'item',
  1261. '#value' => $form_state['storage']['regex']['replace'][$index],
  1262. ),
  1263. 'old_index' => array(
  1264. '#type' => 'hidden',
  1265. '#value' => $index,
  1266. ),
  1267. 'new_index' => array(
  1268. '#type' => 'select',
  1269. '#options' => range(0, sizeof($form_state['storage']['regex']['pattern'])-1),
  1270. '#default_value' => $index,
  1271. ),
  1272. 'id' => array(
  1273. '#type' => 'hidden',
  1274. '#value' => $index,
  1275. ),
  1276. 'submit-delete' => array(
  1277. '#type' => 'submit',
  1278. '#value' => 'Delete Transformation',
  1279. '#name' => $index,
  1280. ),
  1281. );
  1282. $form['add_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1283. }
  1284. $form['add_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1285. '#type' => ($form_state['storage']['regex']['pattern']) ? 'submit' : 'hidden',
  1286. '#value' => 'Save Transformation Rule Order'
  1287. );
  1288. $form['add_fields']['additional']['regex_transform']['new_regex'] = array(
  1289. '#type' => 'fieldset',
  1290. '#title' => 'Add a new Transformation Rule',
  1291. );
  1292. $form['add_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1293. '#type' => 'textfield',
  1294. '#title' => 'Match Pattern',
  1295. '#description' => 'You can use standard php regular expressions in this field to specify a '
  1296. .'pattern. Only if this pattern matches the value in the data file does the replacement '
  1297. .'pattern get applied to the value. To capture a section of your value for use in the '
  1298. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1299. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1300. .' To match and capture any value use <i>.*</i>',
  1301. );
  1302. $form['add_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1303. '#type' => 'textfield',
  1304. '#title' => 'Replacement Pattern',
  1305. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  1306. .'mentioned above. It can include references of the form \n where n is the number of the '
  1307. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1308. .'first set of round brackets.',
  1309. );
  1310. if ($field_type == 'table field') {
  1311. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1312. $form['add_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1313. .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
  1314. .'This allows you to substitute other data file values into the current field. For example, '
  1315. .'if you had the following line:<br />'
  1316. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1317. .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
  1318. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1319. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1320. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1321. }
  1322. $form['add_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1323. '#type' => 'submit',
  1324. '#value' => 'Add Transformation',
  1325. );
  1326. $form['add_fields']['additional']['regex_transform']['test_regex'] = array(
  1327. '#type' => 'fieldset',
  1328. '#title' => 'Test Transformation Rules',
  1329. );
  1330. $form['add_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1331. '#type' => 'textfield',
  1332. '#title' => 'Test Value',
  1333. '#description' => 'This should be a value that you expect the above transformation rules '
  1334. .'to be applied to.',
  1335. '#default_value' => $form_state['storage']['test_regex_test'],
  1336. );
  1337. $form['add_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1338. '#type' => 'textfield',
  1339. '#title' => 'Test Result',
  1340. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1341. .'riles were applied to the Test Value.',
  1342. '#default_value' => $form_state['storage']['test_regex_result'],
  1343. );
  1344. $form['add_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1345. '#type' => 'submit',
  1346. '#value' => 'Test Transformation Rules'
  1347. );
  1348. $form['add_fields']['submit-add_field'] = array(
  1349. '#type' => 'submit',
  1350. '#value' => 'Save Changes'
  1351. );
  1352. $form['add_fields']['submit-cancel'] = array(
  1353. '#type' => 'submit',
  1354. '#value' => 'Cancel'
  1355. );
  1356. return $form;
  1357. }
  1358. function tripal_bulk_loader_add_template_field_form_validate($form, $form_state) {
  1359. // Don't worry about validation when Cancel button is clicked
  1360. if ($form_state['clicked_button']['#value'] == 'Save Changes') {
  1361. $is_unique = tripal_bulk_loader_is_record_name_unique(
  1362. $form_state['values']['record_name'],
  1363. $form_state['values']['template_id'],
  1364. $form_state['storage']['template_array']
  1365. );
  1366. $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
  1367. if ((!$is_unique) AND $new_record) {
  1368. form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
  1369. }
  1370. }
  1371. }
  1372. /**
  1373. * Add Field Submit
  1374. *
  1375. * @param $form
  1376. * The form that was submitted
  1377. * @param $form_state
  1378. * The values and storage for the form
  1379. */
  1380. function tripal_bulk_loader_add_template_field_form_submit($form, &$form_state) {
  1381. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1382. if (!$form_state['ahah_submission']) {
  1383. if ($op == 'Save Changes') {
  1384. $template = $form_state['storage']['template_array'];
  1385. // If new record
  1386. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  1387. $record_name = $form_state['values']['record_name'];
  1388. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1389. $record2priority[$record_name] = $priority;
  1390. $template[$priority]['table'] = $form_state['values']['chado_table'];
  1391. $template[$priority]['record_id'] = $record_name;
  1392. }
  1393. else {
  1394. $priority = $form_state['values']['field_group'];
  1395. $record_name = $record2priority[$priority];
  1396. }
  1397. // Add field to template array
  1398. if ($form_state['values']['field_type'] == 'table field') {
  1399. $field = array(
  1400. 'type' => 'table field',
  1401. 'title' => $form_state['values']['field_title'],
  1402. 'field' => $form_state['values']['chado_field'],
  1403. 'required' => $form_state['values']['required'],
  1404. //'allowed values' => empty by default,
  1405. 'spreadsheet column' => $form_state['values']['column_number'],
  1406. 'exposed' => $form_state['values']['column_exposed'],
  1407. 'exposed_description' => $form_state['values']['column_exposed_desc'],
  1408. );
  1409. }
  1410. elseif ($form_state['values']['field_type'] == 'constant') {
  1411. $field = array(
  1412. 'type' => 'constant',
  1413. 'title' => $form_state['values']['field_title'],
  1414. 'field' => $form_state['values']['chado_field'],
  1415. 'required' => $form_state['values']['required'],
  1416. //'allowed values' => empty by default,
  1417. 'constant value' => $form_state['values']['constant_value'],
  1418. 'exposed' => $form_state['values']['constant_exposed'],
  1419. 'exposed_validate' => $form_state['values']['constant_validate'],
  1420. );
  1421. }
  1422. elseif ($form_state['values']['field_type'] == 'foreign key') {
  1423. $field = array(
  1424. 'type' => 'foreign key',
  1425. 'title' => $form_state['values']['field_title'],
  1426. 'field' => $form_state['values']['chado_field'],
  1427. 'show_all_records' => $form_state['values']['show_all_records'],
  1428. 'foreign key' => $form_state['values']['foreign_record'],
  1429. 'foreign field' => $form_state['values']['foreign_field'],
  1430. 'required' => $form_state['values']['required'],
  1431. );
  1432. }
  1433. // Deal with any additional options
  1434. if ($form_state['storage']['regex']) {
  1435. $field['regex'] = $form_state['storage']['regex'];
  1436. }
  1437. // Save Template
  1438. $template[$priority]['fields'][] = $field;
  1439. $form_state['storage']['template']->template_array = serialize($template);
  1440. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1441. if ($success) {
  1442. drupal_set_message(t('Successfully Added Field to Template'));
  1443. drupal_set_message(t('Template Saved.'));
  1444. $path = explode('?', $form_state['storage']['referring URL']);
  1445. parse_str($path[1], $query);
  1446. $query['template_id'] = $form_state['storage']['template']->template_id;
  1447. drupal_goto($path[0], $query);
  1448. }
  1449. else {
  1450. drupal_set_message(t('Unable to Save Template!'), 'error');
  1451. watchdog('T_bulk_loader',
  1452. 'Unable to save bulk loader template: %template',
  1453. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1454. WATCHDOG_ERROR
  1455. );
  1456. }
  1457. }
  1458. elseif ($op == 'Cancel') {
  1459. $path = explode('?', $form_state['storage']['referring URL']);
  1460. parse_str($path[1], $query);
  1461. $query['template_id'] = $form_state['storage']['template']->template_id;
  1462. drupal_goto($path[0], $query);
  1463. }
  1464. elseif ($op == 'Add Transformation') {
  1465. // Add transformation rule to original field
  1466. $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1467. $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
  1468. drupal_set_message(t('Successfully Added Transformation Rule'));
  1469. }
  1470. elseif ($op == 'Save Transformation Rule Order') {
  1471. // Generate new regex array
  1472. $new_regex = array();
  1473. $old_regex = $form_state['storage']['regex'];
  1474. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1475. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1476. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1477. }
  1478. // sort new regex arrays
  1479. ksort($new_regex['pattern']);
  1480. ksort($new_regex['replace']);
  1481. $form_state['storage']['regex'] = $new_regex;
  1482. }
  1483. elseif ($op == 'Delete Transformation') {
  1484. // Unset regex rule
  1485. $index = $form_state['clicked_button']['#name'];
  1486. unset($form_state['storage']['regex']['pattern'][$index]);
  1487. unset($form_state['storage']['regex']['replace'][$index]);
  1488. }
  1489. elseif ($op == 'Test Transformation Rules') {
  1490. $patterns = $form_state['storage']['regex']['pattern'];
  1491. $replaces = $form_state['storage']['regex']['replace'];
  1492. $test_string = $form_state['values']['test_string'];
  1493. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1494. $form_state['storage']['test_regex_test'] = $test_string;
  1495. }
  1496. }
  1497. }
  1498. /**
  1499. * Edit Field Form
  1500. *
  1501. * This form is meant to be called from a bulk loader form. The following should be set
  1502. * in the query section of the path:
  1503. * - template_id=\d+: the template which the edited field is part of
  1504. * - record_id=\d+: the priority or key in the template array of the record the field
  1505. * is currently part of
  1506. * - field_index=\d+: the key of the field in the fields array of the previously
  1507. * specified record
  1508. *
  1509. * @param $form_state
  1510. * Contains the values and storage for the form
  1511. * @return
  1512. * A form array to be rendered by drupal_get_form
  1513. */
  1514. function tripal_bulk_loader_edit_template_field_form(&$form_state = NULL) {
  1515. $form = array();
  1516. $form['#cache'] = TRUE; // Make sure the form is cached.
  1517. // get template id from path
  1518. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  1519. // if there is no template supplied don't return rest of form
  1520. if (!$template_id) {
  1521. return $form;
  1522. }
  1523. // Pre-set Variables needed for form proper------------------------------------------
  1524. // If this is the first load of the form (no form state) we need to initialize some variables
  1525. if (!$form_state['storage']['template']) {
  1526. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  1527. $template = db_fetch_object(db_query($sql, $template_id));
  1528. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1529. $form_state['storage']['template'] = $template;
  1530. $form_state['storage']['record2priority'] = array();
  1531. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1532. if (!is_array($record_array)) {
  1533. continue;
  1534. }
  1535. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1536. }
  1537. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  1538. }
  1539. else {
  1540. $template = $form_state['storage']['template'];
  1541. }
  1542. // get the field from the path
  1543. if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) {
  1544. $priority = $_GET['record_id'];
  1545. $field_index = $_GET['field_index'];
  1546. $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
  1547. $form_state['storage']['original_field'] = $template_field;
  1548. $form_state['storage']['original_field']['priority'] = $priority;
  1549. $form_state['storage']['original_field']['field_index'] = $field_index;
  1550. }
  1551. // get field from the form_state
  1552. elseif (!empty($form_state['storage']['original_field']['priority']) && !empty($form_state['storage']['original_field']['field_index'])) {
  1553. $priority = $form_state['storage']['original_field']['priority'];
  1554. $field_index = $form_state['storage']['original_field']['field_index'];
  1555. $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
  1556. }
  1557. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type'];
  1558. // Get the tables array and default table
  1559. $tables = tripal_core_get_chado_tables(TRUE);
  1560. if ($form_state['values']) {
  1561. $table = $form_state['values']['chado_table'];
  1562. }
  1563. else {
  1564. $table = $form_state['storage']['template_array'][$priority]['table'];
  1565. }
  1566. $show_all = ($form_state['values']['show_all_records'] ? $form_state['values']['show_all_records'] : $template_field['show_all_records']);
  1567. $table_description = tripal_core_get_chado_table_schema($table);
  1568. // Fields and foreign key / referral mappings
  1569. // build the fields array
  1570. $chado_fields = array();
  1571. foreach ($table_description['fields'] as $field_name => $field_array) {
  1572. $chado_fields[$field_name] = $field_name;
  1573. }
  1574. $ref_chado_fields = array();
  1575. $fk_options = array();
  1576. $fk_options['NULL'] = 'None';
  1577. if ($field_type == 'foreign key' and !$show_all) {
  1578. $foreign_field2table = array();
  1579. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1580. foreach ($key_array['columns'] as $left_field => $right_field) {
  1581. //$chado_fields[$left_field] = $left_field;
  1582. $foreign_field2table[$left_field] = $key_table;
  1583. }
  1584. }
  1585. // reset($chado_fields);
  1586. // set default field
  1587. if (empty($chado_fields)) {
  1588. $field = NULL;
  1589. }
  1590. elseif ($chado_fields[$form_state['values']['chado_field']]) {
  1591. $field = $form_state['values']['chado_field'];
  1592. }
  1593. elseif ($template_field['field']) {
  1594. $field = $template_field['field'];
  1595. }
  1596. else {
  1597. $field = current($chado_fields);
  1598. }
  1599. // Foreign key options
  1600. $foreign_table = $foreign_field2table[$field];
  1601. if ($foreign_table) {
  1602. foreach ($form_state['storage']['record2priority'] as $record_name_ => $priority_ ) {
  1603. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority_]['table'])) {
  1604. $fk_options[$record_name_] = $record_name_;
  1605. }
  1606. }
  1607. }
  1608. }
  1609. // the user wants to see all of the records
  1610. elseif ($field_type == 'foreign key' and $show_all) {
  1611. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  1612. $fk_options[$record_name] = $record_name;
  1613. }
  1614. }
  1615. // build the list of referrer table fields
  1616. if ($field_type == 'foreign key') {
  1617. $fk_rec = $form_state['values']['foreign_record'] ? $form_state['values']['foreign_record'] : $template_field['foreign key'];
  1618. if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') {
  1619. // first add in the foreign keys
  1620. $fk_priority = $form_state['storage']['record2priority'][$fk_rec];
  1621. $fk_table = $form_state['storage']['template_array'][$fk_priority]['table'];
  1622. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1623. foreach ($key_array['columns'] as $left_field => $right_field) {
  1624. if ($key_table == $fk_table and $left_field == $field) {
  1625. $ref_chado_fields['Foreign Key'][$right_field] = $right_field;
  1626. }
  1627. }
  1628. }
  1629. $fk_description = tripal_core_get_chado_table_schema($fk_table);
  1630. foreach ($fk_description['fields'] as $fk_field_name => $fk_farray) {
  1631. // don't include the FK field it's included above
  1632. if (in_array('Foreign Key', $ref_chado_fields) and
  1633. in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) {
  1634. continue;
  1635. }
  1636. $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name;
  1637. }
  1638. }
  1639. }
  1640. // Ensure other field type fields are reset/cleared except for those of the current type
  1641. // ie: if the type is changed from spreadsheet column to constant, the spreadsheet column
  1642. // fields should be cleared since they don't apply to a constant
  1643. switch ($field_type) {
  1644. case 'table field':
  1645. // clear constant
  1646. $form_state['values']['constant_value'] = NULL;
  1647. $template_field['constant value'] = NULL;
  1648. $form_state['values']['constant_exposed'] = NULL;
  1649. $template_field['exposed'] = NULL;
  1650. $form_state['values']['constant_validate'] = NULL;
  1651. $template_field['exposed_validate'] = NULL;
  1652. // clear foreign key
  1653. // clears by default :)
  1654. break;
  1655. case 'constant':
  1656. // clear spreadsheet column
  1657. $form_state['values']['column_number'] = NULL;
  1658. $form_state['values']['column_exposed'] = NULL;
  1659. $template_field['exposed'] = NULL;
  1660. $form_state['values']['column_exposed_desc'] = NULL;
  1661. $template_field['exposed_description'] = NULL;
  1662. // clear foreign key
  1663. // clears by default :)
  1664. break;
  1665. case 'foreign key':
  1666. // clear constant
  1667. $form_state['values']['constant_value'] = NULL;
  1668. $template_field['constant value'] = NULL;
  1669. $form_state['values']['constant_exposed'] = NULL;
  1670. $template_field['exposed'] = NULL;
  1671. $form_state['values']['constant_validate'] = NULL;
  1672. $template_field['exposed_validate'] = NULL;
  1673. // clear spreadsheet column
  1674. $form_state['values']['column_number'] = NULL;
  1675. $form_state['values']['column_exposed'] = NULL;
  1676. $template_field['exposed'] = NULL;
  1677. $form_state['values']['column_exposed_desc'] = NULL;
  1678. $template_field['exposed_description'] = NULL;
  1679. break;
  1680. }
  1681. // Start of Form Proper--------------------------------------------------------------
  1682. $form['template_name'] = array(
  1683. '#type' => 'item',
  1684. '#title' => 'Template',
  1685. '#value' => $template->name,
  1686. );
  1687. $form['template_id'] = array(
  1688. '#type' => 'hidden',
  1689. '#value' => $template_id,
  1690. );
  1691. $form['edit_fields'] = array(
  1692. '#type' => 'fieldset',
  1693. '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">',
  1694. '#suffix' => '</div>',
  1695. );
  1696. $form['edit_fields']['field_type'] = array(
  1697. '#type' => 'radios',
  1698. '#title' => t('Type of Field'),
  1699. '#options' => array(
  1700. 'table field' => t('Data Field: Fields which maps to a data file column'),
  1701. 'constant' => t('Constant: Field which remains Constant throughout the data file'),
  1702. 'foreign key' => t('Record Referral: Fields which map to a record in another table'),
  1703. ),
  1704. '#required' => TRUE,
  1705. '#default_value' => $field_type,
  1706. '#ahah' => array(
  1707. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1708. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1709. 'effect' => 'fade'
  1710. ),
  1711. );
  1712. // check template array for records then edit one more
  1713. if (!$form_state['storage']['record2priority']) {
  1714. $groups = array();
  1715. }
  1716. else {
  1717. $groups = array_flip($form_state['storage']['record2priority']);
  1718. }
  1719. $groups['NONE'] = 'Select a Record';
  1720. $groups['NEW'] = 'New Record';
  1721. $form['edit_fields']['field_group'] = array(
  1722. '#type' => 'select',
  1723. '#title' => 'Record',
  1724. '#description' => 'This is used to group a set of fields together allowing '
  1725. .'multiple records to be inserted into the same table per line of the data file',
  1726. '#options' => $groups,
  1727. '#default_value' => (preg_match('/(\d+|\w+)/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority,
  1728. '#ahah' => array(
  1729. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1730. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1731. 'effect' => 'fade'
  1732. ),
  1733. '#required' => TRUE,
  1734. );
  1735. $form['edit_fields']['record_name'] = array(
  1736. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  1737. '#title' => 'Unique Record Name',
  1738. '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">',
  1739. '#suffix' => '</div>',
  1740. '#default_value' => $form_state['values']['record_name'],
  1741. );
  1742. $form['edit_fields']['field_title'] = array(
  1743. '#type' => 'textfield',
  1744. '#title' => t('Human-readable Title for Field'),
  1745. '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'],
  1746. );
  1747. // Chado Field
  1748. $form['edit_fields']['chado'] = array(
  1749. '#type' => 'fieldset',
  1750. '#title' => t('Chado Field/Column Details'),
  1751. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1752. );
  1753. $form['edit_fields']['chado']['chado_table'] = array(
  1754. '#type' => 'select',
  1755. '#title' => t('Chado Table'),
  1756. '#options' => $tables,
  1757. '#default_value' => $table,
  1758. '#ahah' => array(
  1759. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1760. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1761. 'effect' => 'fade'
  1762. ),
  1763. );
  1764. $form['edit_fields']['chado']['chado_field'] = array(
  1765. '#type' => 'select',
  1766. '#title' => t('Chado Field/Column'),
  1767. '#options' => $chado_fields,
  1768. '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'],
  1769. '#ahah' => array(
  1770. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1771. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1772. 'effect' => 'fade'
  1773. ),
  1774. );
  1775. // data file column
  1776. $form['edit_fields']['columns'] = array(
  1777. '#type' => 'fieldset',
  1778. '#title' => t('Data File Column'),
  1779. '#collapsible' => TRUE,
  1780. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  1781. );
  1782. /**
  1783. $form['edit_fields']['columns']['sheet_name'] = array(
  1784. '#type' => 'textfield',
  1785. '#title' => t('Worksheet'),
  1786. '#description' => t('Specify the name of the worksheet.'),
  1787. '#size' => 5,
  1788. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : $template_field['spreadsheet sheet'],
  1789. );
  1790. */
  1791. $form['edit_fields']['columns']['column_number'] = array(
  1792. '#type' => 'textfield',
  1793. '#title' => t('Column'),
  1794. '#description' => t('Specify the column in the data file that this field maps to where the first column is 1.'),
  1795. '#size' => 5,
  1796. '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'],
  1797. );
  1798. $form['edit_fields']['columns']['column_exposed'] = array(
  1799. '#type' => 'checkbox',
  1800. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1801. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1802. '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
  1803. );
  1804. $form['edit_fields']['columns']['column_exposed_desc'] = array(
  1805. '#type' => 'textfield',
  1806. '#title' => t('Description for exposed field on bulk loading job'),
  1807. '#description' => t('This description should tell the user what column should be entered here.'),
  1808. '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
  1809. );
  1810. // Global Value
  1811. $form['edit_fields']['constant'] = array(
  1812. '#type' => 'fieldset',
  1813. '#title' => t('Constant'),
  1814. '#collapsible' => TRUE,
  1815. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1816. );
  1817. $form['edit_fields']['constant']['constant_value'] = array(
  1818. '#type' => 'textfield',
  1819. '#title' => t('Constant Value'),
  1820. '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
  1821. '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'],
  1822. );
  1823. $form['edit_fields']['constant']['constant_exposed'] = array(
  1824. '#type' => 'checkbox',
  1825. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1826. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'),
  1827. '#default_value' => ($form_state['values']['constant_exposed']) ? $form_state['values']['constant_exposed'] : $template_field['exposed'],
  1828. );
  1829. $form['edit_fields']['constant']['constant_validate'] = array(
  1830. '#type' => 'checkbox',
  1831. '#title' => t('Ensure value is in table'),
  1832. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1833. '#default_value' => ($form_state['values']['constant_validate']) ? $form_state['values']['constant_validate'] : $template_field['exposed_validate'],
  1834. );
  1835. // Foreign Key / Referrer
  1836. $form['edit_fields']['foreign_key'] = array(
  1837. '#type' => 'fieldset',
  1838. '#title' => 'Record Referral',
  1839. '#collapsible' => TRUE,
  1840. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1841. );
  1842. $form['edit_fields']['foreign_key']['show_all_records'] = array(
  1843. '#type' => 'checkbox',
  1844. '#title' => 'Refer to any record',
  1845. '#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'),
  1846. '#default_value' => $show_all,
  1847. '#ahah' => array(
  1848. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1849. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1850. 'effect' => 'fade'
  1851. ),
  1852. );
  1853. $form['edit_fields']['foreign_key']['foreign_record'] = array(
  1854. '#type' => 'select',
  1855. '#title' => 'Record to refer to',
  1856. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1857. '#options' => $fk_options,
  1858. '#ahah' => array(
  1859. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1860. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1861. 'effect' => 'fade'
  1862. ),
  1863. '#default_value' => ($form_state['values']['foreign_record']) ? $form_state['values']['foreign_record'] : $template_field['foreign key'],
  1864. );
  1865. $form['edit_fields']['foreign_key']['foreign_field'] = array(
  1866. '#type' => 'select',
  1867. '#title' => 'Field to refer to',
  1868. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1869. '#options' => $ref_chado_fields,
  1870. '#default_value' => ($form_state['values']['foreign_field']) ? $form_state['values']['foreign_field'] : $template_field['foreign field'],
  1871. );
  1872. $form['edit_fields']['additional'] = array(
  1873. '#type' => 'fieldset',
  1874. '#title' => 'Additional Options',
  1875. );
  1876. $form['edit_fields']['additional']['required'] = array(
  1877. '#type' => 'checkbox',
  1878. '#title' => 'Make this field required',
  1879. '#default_value' => (!empty($form_state['values']['required'])) ? $form_state['values']['required'] : $template_field['required'],
  1880. );
  1881. $form['edit_fields']['additional']['regex_transform'] = array(
  1882. '#type' => 'fieldset',
  1883. '#title' => 'Transform Data File Value Rules',
  1884. '#collapsible' => TRUE,
  1885. '#collapsed' => (!$template_field['regex']['pattern']) ? TRUE : FALSE,
  1886. );
  1887. $transformation_msg = '<p>A transformation rule allows you to transform the original value '
  1888. .'(usually from a user submitted data file) into the form you would like it stored '
  1889. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1890. .'which determines which replacement patterns are applied and captures regions of the '
  1891. .'original value) and a replacement pattern (a string which may contain capture references '
  1892. .'that describes what the new value should be). Each rule is applied to the result of the '
  1893. .'previous rule.<p>';
  1894. $form['edit_fields']['additional']['regex_transform']['regex_description'] = array(
  1895. '#type' => 'item',
  1896. '#value' => $transformation_msg,
  1897. );
  1898. $form['edit_fields']['additional']['regex_transform']['regex-data'] = array(
  1899. '#tree' => TRUE,
  1900. );
  1901. if (!is_array($template_field['regex']['pattern'])) {
  1902. $template_field['regex']['pattern'] = array();
  1903. }
  1904. $key_options = array();
  1905. foreach ($template_field['regex']['pattern'] as $k => $v) {
  1906. $key_options[$k] = $k;
  1907. }
  1908. foreach ($template_field['regex']['pattern'] as $index => $pattern) {
  1909. $data_element = array(
  1910. 'pattern' => array(
  1911. '#type' => 'item',
  1912. '#value' => $pattern,
  1913. ),
  1914. 'replace' => array(
  1915. '#type' => 'item',
  1916. '#value' => $template_field['regex']['replace'][$index],
  1917. ),
  1918. 'old_index' => array(
  1919. '#type' => 'hidden',
  1920. '#value' => $index,
  1921. ),
  1922. 'new_index' => array(
  1923. '#type' => 'select',
  1924. '#options' => $key_options,
  1925. '#default_value' => $index,
  1926. ),
  1927. 'id' => array(
  1928. '#type' => 'hidden',
  1929. '#value' => $index,
  1930. ),
  1931. 'submit-delete' => array(
  1932. '#type' => 'submit',
  1933. '#value' => 'Delete Transformation',
  1934. '#name' => $index,
  1935. ),
  1936. );
  1937. $form['edit_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1938. }
  1939. $form['edit_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1940. '#type' => 'submit',
  1941. '#value' => 'Save Transformation Rule Order'
  1942. );
  1943. $form['edit_fields']['additional']['regex_transform']['new_regex'] = array(
  1944. '#type' => 'fieldset',
  1945. '#title' => 'Add a new Transformation Rule',
  1946. );
  1947. $form['edit_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1948. '#type' => 'textfield',
  1949. '#title' => 'Match Pattern',
  1950. '#description' => 'You can use standard <b>php regular expressions</b> in this field to specify a '
  1951. .'pattern. Only if this pattern matches the value in the data file does the replacement '
  1952. .'pattern get applied to the value. To capture a section of your value for use in the '
  1953. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1954. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1955. .' To match and capture any value use <i>.*</i>',
  1956. );
  1957. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1958. '#type' => 'textfield',
  1959. '#title' => 'Replacement Pattern',
  1960. '#description' => '<p>This pattern should contain the text you want to replace the match pattern '
  1961. .'mentioned above. It can include references of the form <b>\n</b> where n is the number of the '
  1962. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1963. .'first set of round brackets.</p>',
  1964. );
  1965. if ($field_type == 'table field') {
  1966. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1967. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1968. .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
  1969. .'This allows you to substitute other data file values into the current field. For example, '
  1970. .'if you had the following line:<br />'
  1971. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1972. .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
  1973. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1974. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1975. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1976. }
  1977. $form['edit_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1978. '#type' => 'submit',
  1979. '#value' => 'Add Transformation',
  1980. );
  1981. $form['edit_fields']['additional']['regex_transform']['test_regex'] = array(
  1982. '#type' => 'fieldset',
  1983. '#title' => 'Test Transformation Rules',
  1984. );
  1985. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1986. '#type' => 'textfield',
  1987. '#title' => 'Test Value',
  1988. '#description' => 'This should be a value that you expect the above transformation rules '
  1989. .'to be applied to.',
  1990. '#default_value' => $form_state['storage']['test_regex_test'],
  1991. );
  1992. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1993. '#type' => 'textfield',
  1994. '#title' => 'Test Result',
  1995. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1996. .'riles were applied to the Test Value.',
  1997. '#default_value' => $form_state['storage']['test_regex_result'],
  1998. );
  1999. $form['edit_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  2000. '#type' => 'submit',
  2001. '#value' => 'Test Transformation Rules'
  2002. );
  2003. $form['edit_fields']['submit-edit_field'] = array(
  2004. '#type' => 'submit',
  2005. '#value' => 'Save Changes'
  2006. );
  2007. $form['edit_fields']['submit-cancel'] = array(
  2008. '#type' => 'submit',
  2009. '#value' => 'Cancel'
  2010. );
  2011. return $form;
  2012. }
  2013. function tripal_bulk_loader_edit_template_field_form_validate($form, $form_state) {
  2014. // Don't worry about validation when Cancel button is clicked
  2015. if ($form_state['clicked_button']['#value'] == 'Save Changes') {
  2016. $is_unique = tripal_bulk_loader_is_record_name_unique(
  2017. $form_state['values']['record_name'],
  2018. $form_state['values']['template_id'],
  2019. $form_state['storage']['template_array']
  2020. );
  2021. $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
  2022. if ((!$is_unique) AND $new_record) {
  2023. form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
  2024. }
  2025. }
  2026. }
  2027. /**
  2028. * Edit Field Form Submit
  2029. *
  2030. * @param $form
  2031. * The form that was submitted
  2032. * @param $form_state
  2033. * The values and storage for the form
  2034. */
  2035. function tripal_bulk_loader_edit_template_field_form_submit($form, &$form_state) {
  2036. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  2037. //Clear Test
  2038. $form_state['storage']['test_regex_result'] = NULL;
  2039. $form_state['storage']['test_regex_test'] = NULL;
  2040. if (!$form_state['ahah_submission']) {
  2041. if ($op == 'Save Changes') {
  2042. // If new record
  2043. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  2044. // add new record
  2045. $record_name = $form_state['values']['record_name'];
  2046. $priority = sizeof($form_state['storage']['template_array']) + 1;
  2047. $old_priority = $form_state['storage']['original_field']['priority'];
  2048. $field_index = $form_state['storage']['original_field']['field_index'];
  2049. $form_state['storage']['record2priority'][$record_name] = $priority;
  2050. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  2051. $form_state['storage']['template_array'][$priority]['record_id'] = $record_name;
  2052. }
  2053. else {
  2054. $priority = $form_state['values']['field_group'];
  2055. $old_priority = $form_state['storage']['original_field']['priority'];
  2056. $field_index = $form_state['storage']['original_field']['field_index'];
  2057. $record_name = $form_state['storage']['record2priority'][$priority];
  2058. }
  2059. $field = $form_state['storage']['original_field'];
  2060. if ($form_state['values']['field_type'] == 'table field') {
  2061. $field['type'] = 'table field';
  2062. $field['title'] = $form_state['values']['field_title'];
  2063. $field['field'] = $form_state['values']['chado_field'];
  2064. $field['required'] = $form_state['values']['required'];
  2065. //$field['allowed values'] = empty by default;
  2066. $field['spreadsheet column'] = $form_state['values']['column_number'];
  2067. $field['exposed'] = $form_state['values']['column_exposed'];
  2068. $field['exposed_description'] = $form_state['values']['column_exposed_desc'];
  2069. }
  2070. elseif ($form_state['values']['field_type'] == 'constant') {
  2071. $field['type'] = 'constant';
  2072. $field['title'] = $form_state['values']['field_title'];
  2073. $field['field'] = $form_state['values']['chado_field'];
  2074. $field['required'] = $form_state['values']['required'];
  2075. //$field['allowed values'] = empty by default;
  2076. $field['constant value'] = $form_state['values']['constant_value'];
  2077. $field['exposed_validate'] = $form_state['values']['constant_validate'];
  2078. $field['exposed'] = $form_state['values']['constant_exposed'];
  2079. }
  2080. elseif ($form_state['values']['field_type'] == 'foreign key') {
  2081. $field['type'] = 'foreign key';
  2082. $field['title'] = $form_state['values']['field_title'];
  2083. $field['field'] = $form_state['values']['chado_field'];
  2084. $field['show_all_records'] = $form_state['values']['show_all_records'];
  2085. $field['foreign key'] = $form_state['values']['foreign_record'];
  2086. $field['foreign field'] = $form_state['values']['foreign_field'];
  2087. $field['required'] = $form_state['values']['required'];
  2088. }
  2089. // Deal with any additional options
  2090. // if the record has changed...
  2091. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  2092. if ($old_priority != $priority) {
  2093. $form_state['storage']['template_array'][$priority]['fields'][] = $field;
  2094. unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]);
  2095. // if there are no fields left delete the old record
  2096. if (!$form_state['storage']['template_array'][$old_priority]['fields']) {
  2097. unset($form_state['storage']['template_array'][$old_priority]);
  2098. }
  2099. }
  2100. else {
  2101. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field;
  2102. }
  2103. // Save Template
  2104. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  2105. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  2106. if ($success) {
  2107. drupal_set_message(t('Successfully Updated Field'));
  2108. drupal_set_message(t('Template Saved.'));
  2109. $path = explode('?', $form_state['storage']['referring URL']);
  2110. parse_str($path[1], $query);
  2111. $query['template_id'] = $form_state['storage']['template']->template_id;
  2112. drupal_goto($path[0], $query);
  2113. }
  2114. else {
  2115. drupal_set_message(t('Unable to Save Template!'), 'error');
  2116. watchdog('T_bulk_loader',
  2117. 'Unable to save bulk loader template: %template',
  2118. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  2119. WATCHDOG_ERROR
  2120. );
  2121. }
  2122. }
  2123. elseif ($op == 'Cancel') {
  2124. $path = explode('?', $form_state['storage']['referring URL']);
  2125. parse_str($path[1], $query);
  2126. $query['template_id'] = $form_state['storage']['template']->template_id;
  2127. drupal_goto($path[0], $query);
  2128. }
  2129. elseif ($op == 'Add Transformation') {
  2130. // Add transformation rule to original field
  2131. $form_state['storage']['original_field']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  2132. $form_state['storage']['original_field']['regex']['replace'][] = $form_state['values']['replace'];
  2133. // Add original field back into template
  2134. $priority = $form_state['storage']['original_field']['priority'];
  2135. $field_index = $form_state['storage']['original_field']['field_index'];
  2136. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  2137. }
  2138. elseif ($op == 'Save Transformation Rule Order') {
  2139. // Generate new regex array
  2140. $new_regex = array();
  2141. $old_regex = $form_state['storage']['original_field']['regex'];
  2142. foreach ($form_state['values']['regex-data'] as $key => $element) {
  2143. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  2144. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  2145. }
  2146. // sort new regex arrays
  2147. ksort($new_regex['pattern']);
  2148. ksort($new_regex['replace']);
  2149. // Add back to original field
  2150. $form_state['storage']['original_field']['regex'] = $new_regex;
  2151. $priority = $form_state['storage']['original_field']['priority'];
  2152. $field_index = $form_state['storage']['original_field']['field_index'];
  2153. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  2154. /**
  2155. // Save Template
  2156. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  2157. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  2158. if ($success) {
  2159. drupal_set_message(t('Successfully Reordered Transformation Rules'));
  2160. drupal_set_message(t('Template Saved.'));
  2161. }
  2162. else {
  2163. drupal_set_message(t('Unable to Save Template!'), 'error');
  2164. watchdog('T_bulk_loader',
  2165. 'Unable to save bulk loader template: %template',
  2166. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  2167. WATCHDOG_ERROR
  2168. );
  2169. }
  2170. */
  2171. }
  2172. elseif ($op == 'Delete Transformation') {
  2173. // Unset regex rule
  2174. $index = $form_state['clicked_button']['#name'];
  2175. unset($form_state['storage']['original_field']['regex']['pattern'][$index]);
  2176. unset($form_state['storage']['original_field']['regex']['replace'][$index]);
  2177. $priority = $form_state['storage']['original_field']['priority'];
  2178. $field_index = $form_state['storage']['original_field']['field_index'];
  2179. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  2180. /**
  2181. // Save Template
  2182. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  2183. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  2184. if ($success) {
  2185. drupal_set_message(t('Successfully Reordered Transformation Rules'));
  2186. drupal_set_message(t('Template Saved.'));
  2187. }
  2188. else {
  2189. drupal_set_message(t('Unable to Save Template!'), 'error');
  2190. watchdog('T_bulk_loader',
  2191. 'Unable to save bulk loader template: %template',
  2192. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  2193. WATCHDOG_ERROR
  2194. );
  2195. }
  2196. */
  2197. }
  2198. elseif ($op == 'Test Transformation Rules') {
  2199. $patterns = $form_state['storage']['original_field']['regex']['pattern'];
  2200. $replaces = $form_state['storage']['original_field']['regex']['replace'];
  2201. $test_string = $form_state['values']['test_string'];
  2202. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  2203. $form_state['storage']['test_regex_test'] = $test_string;
  2204. }
  2205. }
  2206. }
  2207. /**
  2208. * @section
  2209. * Helper Functions
  2210. */
  2211. /**
  2212. * Meant to be called from a form_validate function to ensure a newly added bulk loader record
  2213. * name is unique and not empty.
  2214. *
  2215. * @param $new_record_name
  2216. * The record name to check for uniqueness
  2217. * @param $template_id
  2218. * The template_id of the template to add the record to
  2219. * @param $template_array
  2220. * The array describing the template. Optional -will be loaded using template_id if not provided
  2221. * @param $current_priority
  2222. * The priority of the already existing record -checks that the name only occurs on this particular record
  2223. *
  2224. * @return
  2225. * TRUE if the record name is not empty and not already in the template_array; FALSE otherwise
  2226. */
  2227. function tripal_bulk_loader_is_record_name_unique($new_record_name, $template_id, $template_array = NULL, $current_priority = NULL) {
  2228. // get the template array if it's not supplied
  2229. if (empty($template_array)) {
  2230. $template = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d", $template_id));
  2231. $template_array = unserialize($template->template_array);
  2232. if (!is_array($template_array)) {
  2233. watchdog(
  2234. 'tripal_bulk_loader',
  2235. 'Unable to retrieve template array from database where template_id=%template_id',
  2236. array('%template_id' => $template_id),
  2237. WATCHDOG_WARNING
  2238. );
  2239. return FALSE;
  2240. }
  2241. }
  2242. // Check that the new record name is not empty
  2243. if (empty($new_record_name)) {
  2244. return FALSE;
  2245. }
  2246. // Check the new record name is unique
  2247. foreach ($template_array as $priority => $t) {
  2248. if (strcmp($t['record_id'], $new_record_name) == 0) {
  2249. if (($priority != $current_priority) AND ($current_priority !== NULL)) {
  2250. return FALSE;
  2251. }
  2252. }
  2253. }
  2254. return TRUE;
  2255. }
  2256. /**
  2257. * @section
  2258. * AHAH Callbacks
  2259. */
  2260. /**
  2261. * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_field_form
  2262. *
  2263. * @return
  2264. * JSON Data printed to the screen
  2265. */
  2266. function tripal_bulk_loader_add_field_ahah() {
  2267. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  2268. $form_build_id = filter_xss($_POST['form_build_id']);
  2269. $form = form_get_cache($form_build_id, $form_state);
  2270. $args = $form['#parameters'];
  2271. $form_id = array_shift($args);
  2272. $form_state['post'] = $form['#post'] = $_POST;
  2273. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  2274. $form_state['ahah_submission'] = TRUE;
  2275. $form['#programmed'] = $form['#redirect'] = FALSE;
  2276. drupal_process_form($form_id, $form, $form_state);
  2277. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  2278. $form_element = $form['add_fields'];
  2279. // Remove the wrapper so we don't double it up.
  2280. //unset($form_element['#prefix'], $form_element['#suffix']);
  2281. $output = theme('status_messages');
  2282. $output .= drupal_render($form_element);
  2283. // Final rendering callback.
  2284. print drupal_json(array('status' => TRUE, 'data' => $output));
  2285. exit();
  2286. }
  2287. /**
  2288. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
  2289. *
  2290. * @return
  2291. * JSON Data printed to the screen
  2292. */
  2293. function tripal_bulk_loader_edit_field_ahah() {
  2294. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  2295. $form_build_id = filter_xss($_POST['form_build_id']);
  2296. $form = form_get_cache($form_build_id, $form_state);
  2297. $args = $form['#parameters'];
  2298. $form_id = array_shift($args);
  2299. $form_state['post'] = $form['#post'] = $_POST;
  2300. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  2301. $form_state['ahah_submission'] = TRUE;
  2302. $form['#programmed'] = $form['#redirect'] = FALSE;
  2303. drupal_process_form($form_id, $form, $form_state);
  2304. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  2305. $form_element = $form['edit_fields'];
  2306. // Remove the wrapper so we don't double it up.
  2307. unset($form_element['#prefix'], $form_element['#suffix']);
  2308. $output = theme('status_messages');
  2309. $output .= drupal_render($form_element);
  2310. // Final rendering callback.
  2311. print drupal_json(array('status' => TRUE, 'data' => $output));
  2312. exit();
  2313. }