tripal_bulk_loader.admin.templates.inc 83 KB

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