tripal_bulk_loader.admin.templates.inc 94 KB

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