tripal_bulk_loader.admin.templates.inc 88 KB

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