tripal_bulk_loader.admin.templates.inc 83 KB

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