tripal_bulk_loader.admin.templates.inc 90 KB

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