tripal_bulk_loader.admin.templates.inc 89 KB

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