tripal_bulk_loader.admin.templates.inc 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497
  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' => 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['new_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. * Import Template Form Submit
  645. *
  646. * @param $form
  647. * The form that was submitted
  648. * @param $form_state
  649. * The values and storage that were submitted
  650. *
  651. * @ingroup tripal_bulk_loader
  652. */
  653. function tripal_bulk_loader_import_template_form_submit($form, &$form_state) {
  654. $record = array(
  655. 'name' => $form_state['values']['new_template_name'],
  656. 'template_array' => $form_state['values']['template_array'],
  657. 'created' => time(),
  658. 'changed' => time()
  659. );
  660. $result = drupal_write_record('tripal_bulk_loader_template', $record);
  661. if ($result) {
  662. drupal_set_message(t('Successfully imported Tripal Bulk Loader Template.'));
  663. }
  664. $form_state['rebuild'] = FALSE;
  665. $form_state['redirect'] = 'admin/tripal/loaders/bulk/templates';
  666. }
  667. /**
  668. * Export Template Form
  669. *
  670. * On export, simply selects the serialized array from the db for a given template
  671. * and presents it to the user. On import, a serialized template array and a name is
  672. * supplied and a template record is created.
  673. *
  674. * @todo Make array presented to the user more readable. (ie: unserialize and print to the screen)
  675. *
  676. * @param $form_state
  677. * The values and storage for the form
  678. * @return
  679. * A form array to be rendered by drupal_get_form
  680. *
  681. * @ingroup tripal_bulk_loader
  682. */
  683. function tripal_bulk_loader_export_template_form($form, &$form_state) {
  684. // set the breadcrumb
  685. $breadcrumb = array();
  686. $breadcrumb[] = l('Home', '<front>');
  687. $breadcrumb[] = l('Administration', 'admin');
  688. $breadcrumb[] = l('Tripal', 'admin/tripal');
  689. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  690. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  691. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  692. drupal_set_breadcrumb($breadcrumb);
  693. $template_id = $form_state['build_info']['args'][0];
  694. $form_state['storage']['template_id'] = $template_id;
  695. if ($template_id > 0) {
  696. $result = db_select('tripal_bulk_loader_template','t')
  697. ->fields('t')
  698. ->condition('t.template_id',$template_id)
  699. ->execute();
  700. $template = $result->fetchObject();
  701. $form_state['storage']['template_array'] = $template->template_array;
  702. }
  703. $form['name'] = array(
  704. '#type' => 'item',
  705. '#title' => 'Template Name',
  706. '#markup' => $template->name,
  707. );
  708. $form['template_array'] = array(
  709. '#type' => 'textarea',
  710. '#title' => 'Export',
  711. '#default_value' => $form_state['storage']['template_array'],
  712. '#description' => t('Use this serialized array for import.'),
  713. '#rows' => 30,
  714. '#weight' => 5,
  715. );
  716. return $form;
  717. }
  718. /**
  719. * @section
  720. * Edit Record Form
  721. */
  722. /**
  723. * Edit Record Form
  724. *
  725. * D7 @todo: Needs to be debugged
  726. *
  727. * This form is meant to be called from a bulk loader form. The following should be set
  728. * in the query section of the path:
  729. * - template_id=\d+: the template which the edited record is part of
  730. * - record_id=\d+: the priority or key in the template array of the record to be edited
  731. *
  732. * @param $form_state
  733. * Contains the values and storage for the form
  734. * @return
  735. * A form array to be rendered by drupal_get_form
  736. *
  737. * @ingroup tripal_bulk_loader
  738. */
  739. function tripal_bulk_loader_edit_template_record_form($form, &$form_state) {
  740. $form['#cache'] = TRUE; // Make sure the form is cached.
  741. // get args from path
  742. $template_id = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : FALSE;
  743. $form_state['storage']['template_id'] = $template_id;
  744. $record_id = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  745. $form_state['storage']['record_id'] = $record_id;
  746. $form_state['values']['field_group'] = $record_id;
  747. $form_state['storage']['original_priority'] = $record_id;
  748. // if there is no template supplied don't return rest of form
  749. if (!$template_id) {
  750. return $form;
  751. }
  752. // set the breadcrumb
  753. $breadcrumb = array();
  754. $breadcrumb[] = l('Home', '<front>');
  755. $breadcrumb[] = l('Administration', 'admin');
  756. $breadcrumb[] = l('Tripal', 'admin/tripal');
  757. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  758. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  759. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  760. $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $template_id . '/edit');
  761. drupal_set_breadcrumb($breadcrumb);
  762. // Pre-process values/defaults ---------------------------
  763. // If this is the first load of the form (no form state) we need to initialize some variables
  764. if (!array_key_exists('template', $form_state['storage'])) {
  765. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  766. $template = db_query($sql, array(':template' => $template_id))->fetchObject();
  767. $form_state['storage']['template_array'] = unserialize($template->template_array);
  768. $form_state['storage']['template'] = $template;
  769. // rebuild the record2priority array in the $form_state['storage'] array
  770. $form_state['storage']['record2priority'] = array();
  771. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  772. if (!is_array($record_array)) {
  773. continue;
  774. }
  775. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  776. }
  777. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  778. }
  779. else {
  780. $template = $form_state['storage']['template'];
  781. }
  782. // Tables and default table
  783. $tables = chado_get_table_names(TRUE);
  784. if (isset($form_state['values']['chado_table'])) {
  785. $table = $form_state['values']['chado_table'];
  786. }
  787. else {
  788. $table = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['table'];
  789. }
  790. // get the default mode
  791. if (isset($form_state['storage']['original_priority'])) {
  792. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['mode'])) {
  793. $mode = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['mode'];
  794. }
  795. else {
  796. $mode = 'insert';
  797. }
  798. // get default for the select optional
  799. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_optional'])) {
  800. $select_optional = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_optional'];
  801. }
  802. else {
  803. $select_optional = 0;
  804. }
  805. // get default for the select if duplicate
  806. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_if_duplicate'])) {
  807. $select_if_duplicate = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_if_duplicate'];
  808. }
  809. else {
  810. $select_if_duplicate = 1;
  811. }
  812. // get default for the update if duplicate
  813. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['update_if_duplicate'])) {
  814. $update_if_duplicate = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['update_if_duplicate'];
  815. }
  816. else {
  817. $update_if_duplicate = 0;
  818. }
  819. // get default for the select if duplicate
  820. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['optional'])) {
  821. $optional = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['optional'];
  822. }
  823. else {
  824. $optional = 0;
  825. }
  826. // get the default for disabling the record
  827. if (isset($form_state['storage']['template_array'][$form_state['storage']['original_priority']]['disable'])) {
  828. $disable = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['disable'];
  829. }
  830. else {
  831. $disable = 0;
  832. }
  833. }
  834. else {
  835. $mode = 'insert';
  836. $select_optional = 0;
  837. $select_if_duplicate = 1;
  838. $update_if_duplicate = 0;
  839. $optional = 0;
  840. $disable = 0;
  841. }
  842. // this is just for backwards compatibility. the insert_unique mode type is no longer available
  843. if (strcmp($mode, 'insert_unique')==0) {
  844. $mode = 'insert';
  845. $select_if_duplicate = 1;
  846. }
  847. // this is just for backwards compatibility. the insert_unique mode type is no longer available
  848. if (strcmp($mode, 'optional')==0) {
  849. $mode = 'insert';
  850. $optional = 1;
  851. }
  852. // Form Proper -------------------------------------------
  853. $form['template_name'] = array(
  854. '#type' => 'item',
  855. '#title' => 'Template',
  856. '#markup' => $template->name,
  857. );
  858. $form['template_id'] = array(
  859. '#type' => 'hidden',
  860. '#value' => $template_id,
  861. );
  862. $form['edit_record'] = array(
  863. '#type' => 'markup',
  864. );
  865. // check template array for records then add one more
  866. if (!$form_state['storage']['record2priority']) {
  867. $groups = array();
  868. }
  869. else {
  870. $groups = array_flip($form_state['storage']['record2priority']);
  871. }
  872. $priority_default = $form_state['values']['field_group'];
  873. $form['edit_record']['field_group'] = array(
  874. '#type' => 'select',
  875. '#title' => 'Record',
  876. '#description' => 'By Changing the record here, you can move all the fields from the current record into the selected record.',
  877. '#options' => $groups,
  878. '#default_value' => $priority_default,
  879. '#required' => TRUE,
  880. );
  881. $form['edit_record']['record_name'] = array(
  882. '#type' => 'textfield',
  883. '#title' => 'Unique Record Name',
  884. '#default_value' => $groups[$priority_default],
  885. );
  886. $form['edit_record']['chado_table'] = array(
  887. '#type' => 'select',
  888. '#title' => t('Chado Table'),
  889. '#description' => 'This changes the chado table for all fields in this record.',
  890. '#options' => $tables,
  891. '#default_value' => $table,
  892. );
  893. $form['edit_record']['mode'] = array(
  894. '#type' => 'radios',
  895. '#title' => 'Action to take when Loading Record',
  896. '#options' => array(
  897. 'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record',
  898. 'select_once' => 'SELECT ONCE: Select the record only once for each constant set.',
  899. 'insert' => 'INSERT: Insert the record',
  900. '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.',
  901. ),
  902. '#default_value' => $mode
  903. );
  904. $form['edit_record']['select_options'] = array(
  905. '#type' => 'markup',
  906. '#markup' => t('Additional Select Options:'),
  907. );
  908. $form['edit_record']['select_optional'] = array(
  909. '#type' => 'checkbox',
  910. '#title' => t('Continue if no record exists or too many exist.'),
  911. '#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.'),
  912. '#default_value' => $select_optional
  913. );
  914. $form['edit_record']['insert_options'] = array(
  915. '#type' => 'markup',
  916. '#prefix' => '<br>',
  917. '#markup' => t('Additional Insert Options:'),
  918. );
  919. $form['edit_record']['select_if_duplicate'] = array(
  920. '#type' => 'checkbox',
  921. '#title' => t('SELECT if duplicate (no insert)'),
  922. '#description' => t('If this is not the first time this record has been added then perform a select rather than an insert.'),
  923. '#default_value' => $select_if_duplicate
  924. );
  925. $form['edit_record']['update_if_duplicate'] = array(
  926. '#type' => 'checkbox',
  927. '#title' => t('UPDATE if duplicate (no insert)'),
  928. '#description' => t('If this is not the first time this record has been added then perform an update rather than an insert.'),
  929. '#default_value' => $update_if_duplicate
  930. );
  931. $form['edit_record']['optional'] = array(
  932. '#type' => 'checkbox',
  933. '#title' => t('Optional'),
  934. '#description' => t('The insert, update or select will only be performed only if all required data are present'),
  935. '#default_value' => $optional
  936. );
  937. $form['edit_record']['disable'] = array(
  938. '#type' => 'checkbox',
  939. '#title' => t('Disable this record'),
  940. '#description' => t("Check this box to ignore this record (not perform select or insert) during template loading. Uncheck to re-enable the record"),
  941. '#default_value' => $disable,
  942. );
  943. $form['edit_record']['submit-edit_record'] = array(
  944. '#type' => 'submit',
  945. '#value' => 'Save Record'
  946. );
  947. $form['edit_record']['submit-cancel'] = array(
  948. '#type' => 'submit',
  949. '#value' => 'Cancel'
  950. );
  951. return $form;
  952. }
  953. /**
  954. * Implements hook_form_validate()
  955. *
  956. * D7 @todo: Needs to be debugged
  957. *
  958. * @ingroup tripal_bulk_loader
  959. */
  960. function tripal_bulk_loader_edit_template_record_form_validate($form, &$form_state) {
  961. // Don't worry about validation when Cancel button is clicked
  962. if ($form_state['clicked_button']['#value'] == 'Save Record') {
  963. $is_unique = tripal_bulk_loader_is_record_name_unique(
  964. $form_state['values']['record_name'],
  965. $form_state['values']['template_id'],
  966. $form_state['storage']['template_array'],
  967. $form_state['values']['field_group']
  968. );
  969. if (!$is_unique) {
  970. form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
  971. }
  972. }
  973. }
  974. /**
  975. * Edit Record Form Submit
  976. *
  977. * D7 @todo: Needs to be debugged
  978. *
  979. * @param $form
  980. * The form that was submitted
  981. * @param $form_state
  982. * Contains the values and storage for the form
  983. *
  984. * @ingroup tripal_bulk_loader
  985. */
  986. function tripal_bulk_loader_edit_template_record_form_submit($form, &$form_state) {
  987. if ($form_state['values']['op'] == 'Save Record') {
  988. $template = $form_state['storage']['template_array'];
  989. $original_record_name = $template[ $form_state['storage']['original_priority'] ]['record_id'];
  990. // Edit Record
  991. $record = $template[ $form_state['storage']['original_priority'] ];
  992. $record['record_id'] = $form_state['values']['record_name'];
  993. $record['mode'] = $form_state['values']['mode'];
  994. $record['table'] = $form_state['values']['chado_table'];
  995. $record['select_if_duplicate'] = $form_state['values']['select_if_duplicate'];
  996. $record['update_if_duplicate'] = $form_state['values']['update_if_duplicate'];
  997. $record['select_optional'] = $form_state['values']['select_optional'];
  998. $record['disable'] = $form_state['values']['disable'];
  999. $record['optional'] = $form_state['values']['optional'];
  1000. if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
  1001. $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
  1002. $template[ $form_state['values']['field_group'] ] = $record;
  1003. unset($template[ $form_state['storage']['original_priority'] ]);
  1004. }
  1005. else {
  1006. $template[ $form_state['storage']['original_priority'] ] = $record;
  1007. }
  1008. // Go through all records and update this record name where it was used for a foreign key
  1009. // Foreach field in each record that is of type: foreign key
  1010. foreach ($template as $priority => $record) {
  1011. foreach ($record['fields'] as $field_index => $field) {
  1012. if ($field['type'] === 'foreign key') {
  1013. // Check if this points to the old record name and if so, update it
  1014. if ($field['foreign key'] === $original_record_name) {
  1015. $template[$priority]['fields'][$field_index]['foreign key'] = $form_state['values']['record_name'];
  1016. }
  1017. }
  1018. }
  1019. }
  1020. // Save Template
  1021. $form_state['storage']['template']->template_array = serialize($template);
  1022. $form_state['storage']['template']->changed = time();
  1023. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1024. if ($success) {
  1025. drupal_set_message(t('Successfully Updated Template Record'));
  1026. drupal_set_message(t('Template Saved.'));
  1027. $form_state['rebuild'] = FALSE;
  1028. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/' . $form_state['storage']['template']->template_id . '/edit';
  1029. }
  1030. else {
  1031. drupal_set_message(t('Unable to Save Template!'), 'error');
  1032. tripal_report_error('T_bulk_loader',
  1033. TRIPAL_ERROR,
  1034. 'Unable to save bulk loader template: %template',
  1035. array('%template' => print_r($form_state['storage']['template'], TRUE))
  1036. );
  1037. }
  1038. }
  1039. elseif ($form_state['values']['op'] == 'Cancel') {
  1040. $form_state['rebuild'] = FALSE;
  1041. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/' . $form_state['storage']['template']->template_id . '/edit';
  1042. }
  1043. }
  1044. /**
  1045. * Delete Record Form
  1046. *
  1047. * This form is meant to be called from a bulk loader form
  1048. *
  1049. * D7 @todo: Needs to be debugged
  1050. *
  1051. * @param $form_state
  1052. * Contains the values and storage for the form
  1053. * @return
  1054. * A form array to be rendered by drupal_get_form
  1055. *
  1056. * @ingroup tripal_bulk_loader
  1057. */
  1058. function tripal_bulk_loader_delete_template_record_form($form, &$form_state) {
  1059. $form['#cache'] = TRUE; // Make sure the form is cached.
  1060. // get args from path
  1061. $template_id = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : FALSE;
  1062. $form_state['storage']['template_id'] = $template_id;
  1063. $record_id = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  1064. $form_state['storage']['record_id'] = $record_id;
  1065. // if there is no template supplied don't return rest of form
  1066. if (!$template_id) {
  1067. tripal_set_message('Unable to determine the template_id from the path.',TRIPAL_ERROR);
  1068. return $form;
  1069. }
  1070. elseif (!$record_id) {
  1071. tripal_set_message('Unable to determine the record_id from the path.',TRIPAL_ERROR);
  1072. return $form;
  1073. }
  1074. // set the breadcrumb
  1075. $breadcrumb = array();
  1076. $breadcrumb[] = l('Home', '<front>');
  1077. $breadcrumb[] = l('Administration', 'admin');
  1078. $breadcrumb[] = l('Tripal', 'admin/tripal');
  1079. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  1080. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  1081. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  1082. $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $template_id . '/edit');
  1083. drupal_set_breadcrumb($breadcrumb);
  1084. $_GET['record_name'] = (isset($_GET['record_name'])) ? $_GET['record_name'] : '';
  1085. $_GET['chado_table'] = (isset($_GET['chado_table'])) ? $_GET['chado_table'] : '';
  1086. $_GET['record_mode'] = (isset($_GET['record_mode'])) ? $_GET['record_mode'] : '';
  1087. $description = '';
  1088. $description .= '<table>';
  1089. $description .= '<tr><th></th><th>Record Name</th><th>Chado Table</th><th>Mode</th></tr>';
  1090. $description .= '<tr><td>' . $record_id
  1091. . '</td><td>' . $_GET['record_name']
  1092. . '</td><td>' . $_GET['chado_table']
  1093. . '</td><td>' . $_GET['record_mode']
  1094. . '</td></tr>';
  1095. $description .= '</table>';
  1096. $description .= '<p><strong>Are you sure you want to delete this record?</strong></p>';
  1097. $yes = 'Delete Record';
  1098. $no = 'Cancel';
  1099. $form['#attributes']['class'][] = 'confirmation';
  1100. $form['description'] = array('#markup' => $description);
  1101. $form['actions'] = array('#type' => 'actions');
  1102. $form['actions']['submit'] = array(
  1103. '#type' => 'submit',
  1104. '#value' => $yes ? $yes : t('Confirm'),
  1105. );
  1106. $form['actions']['cancel'] = array(
  1107. '#type' => 'link',
  1108. '#title' => $no ? $no : t('Cancel'),
  1109. '#href' => 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit',
  1110. );
  1111. // By default, render the form using theme_confirm_form().
  1112. if (!isset($form['#theme'])) {
  1113. $form['#theme'] = 'confirm_form';
  1114. }
  1115. return $form;
  1116. }
  1117. /**
  1118. * Delete Record Form
  1119. *
  1120. * This form is meant to be called from a bulk loader form
  1121. *
  1122. * D7 @todo: Needs to be debugged
  1123. *
  1124. * @param $form_state
  1125. * Contains the values and storage for the form
  1126. * @return
  1127. * A form array to be rendered by drupal_get_form
  1128. *
  1129. * @ingroup tripal_bulk_loader
  1130. */
  1131. function tripal_bulk_loader_delete_template_record_form_submit($form, &$form_state) {
  1132. $form_state['rebuild'] = FALSE;
  1133. $template_id = $form_state['storage']['template_id'];
  1134. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit';
  1135. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  1136. $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
  1137. $form_state['storage']['template'] = unserialize($result->template_array);
  1138. $form_state['storage']['record2priority'] = array();
  1139. $new_template = tripal_bulk_loader_delete_record($form_state['storage']['record_id'], $form_state['storage']['template']);
  1140. if (!empty($new_template)) {
  1141. $form_state['storage']['template'] = $new_template;
  1142. }
  1143. drupal_set_message(t('Deleted Field from Template.'));
  1144. $record = array(
  1145. 'template_id' => $form_state['storage']['template_id'],
  1146. 'template_array' => serialize($form_state['storage']['template']),
  1147. 'changed' => time()
  1148. );
  1149. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  1150. drupal_set_message(t('Template Saved.'));
  1151. }
  1152. /**
  1153. * Duplicate Record Form
  1154. *
  1155. * This form is meant to be called from a bulk loader form
  1156. *
  1157. * D7 @todo: Needs to be debugged
  1158. *
  1159. * @param $form_state
  1160. * Contains the values and storage for the form
  1161. * @return
  1162. * A form array to be rendered by drupal_get_form
  1163. *
  1164. * @ingroup tripal_bulk_loader
  1165. */
  1166. function tripal_bulk_loader_duplicate_template_record_form($form, &$form_state) {
  1167. $form['#cache'] = TRUE; // Make sure the form is cached.
  1168. // get args from path
  1169. $template_id = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : FALSE;
  1170. $form_state['storage']['template_id'] = $template_id;
  1171. $record_id = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  1172. $form_state['storage']['record_id'] = $record_id;
  1173. // if there is no template supplied don't return rest of form
  1174. if (!$template_id) {
  1175. tripal_set_message('Unable to determine the template_id from the path.',TRIPAL_ERROR);
  1176. return $form;
  1177. }
  1178. elseif (!$record_id) {
  1179. tripal_set_message('Unable to determine the record_id from the path.',TRIPAL_ERROR);
  1180. return $form;
  1181. }
  1182. // set the breadcrumb
  1183. $breadcrumb = array();
  1184. $breadcrumb[] = l('Home', '<front>');
  1185. $breadcrumb[] = l('Administration', 'admin');
  1186. $breadcrumb[] = l('Tripal', 'admin/tripal');
  1187. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  1188. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  1189. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  1190. $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $template_id . '/edit');
  1191. drupal_set_breadcrumb($breadcrumb);
  1192. $_GET['record_name'] = (isset($_GET['record_name'])) ? $_GET['record_name'] : '';
  1193. $_GET['chado_table'] = (isset($_GET['chado_table'])) ? $_GET['chado_table'] : '';
  1194. $_GET['record_mode'] = (isset($_GET['record_mode'])) ? $_GET['record_mode'] : '';
  1195. $description = '';
  1196. $description .= '<table>';
  1197. $description .= '<caption>Record to be Duplicated</caption>';
  1198. $description .= '<tr><th></th><th>Record Name</th><th>Chado Table</th><th>Mode</th></tr>';
  1199. $description .= '<tr><td>' . $record_id
  1200. . '</td><td>' . $_GET['record_name']
  1201. . '</td><td>' . $_GET['chado_table']
  1202. . '</td><td>' . $_GET['record_mode']
  1203. . '</td></tr>';
  1204. $description .= '</table>';
  1205. $form['description'] = array(
  1206. '#type' => 'markup',
  1207. '#markup' => $description,
  1208. );
  1209. $form['new_record_name'] = array(
  1210. '#type' => 'textfield',
  1211. '#title' => t('New Record Name'),
  1212. '#default_value' => $record_id . '_' . date('YzHi')
  1213. );
  1214. $form['actions'] = array('#type' => 'actions');
  1215. $form['actions']['submit'] = array(
  1216. '#type' => 'submit',
  1217. '#value' => t('Duplicate Record'),
  1218. );
  1219. $form['actions']['cancel'] = array(
  1220. '#type' => 'link',
  1221. '#title' => t('Cancel'),
  1222. '#href' => 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit',
  1223. );
  1224. return $form;
  1225. }
  1226. /**
  1227. * Delete Record Form
  1228. *
  1229. * This form is meant to be called from a bulk loader form
  1230. *
  1231. * D7 @todo: Needs to be debugged
  1232. *
  1233. * @param $form_state
  1234. * Contains the values and storage for the form
  1235. * @return
  1236. * A form array to be rendered by drupal_get_form
  1237. *
  1238. * @ingroup tripal_bulk_loader
  1239. */
  1240. function tripal_bulk_loader_duplicate_template_record_form_submit($form, &$form_state) {
  1241. $form_state['rebuild'] = FALSE;
  1242. $template_id = $form_state['storage']['template_id'];
  1243. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit';
  1244. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  1245. $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
  1246. $form_state['storage']['template'] = unserialize($result->template_array);
  1247. // original record (one to be duplicated)
  1248. $orig_priority = $form_state['storage']['record_id'];
  1249. $record = $form_state['storage']['template'][ $orig_priority ];
  1250. // new record
  1251. $record['record_id'] = $form_state['values']['new_record_name'];
  1252. $form_state['storage']['template'][] = $record;
  1253. $record = array(
  1254. 'template_id' => $form_state['storage']['template_id'],
  1255. 'template_array' => serialize($form_state['storage']['template']),
  1256. 'changed' => time()
  1257. );
  1258. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  1259. drupal_set_message(t('Template Saved.'));
  1260. }
  1261. /**
  1262. * @section
  1263. * Add/Edit Field Forms
  1264. */
  1265. /**
  1266. * Determine default values needed for add/edit field form
  1267. *
  1268. * @ingroup tripal_bulk_loader
  1269. */
  1270. function tripal_bulk_loader_template_field_form_default_values($mode, &$form_state) {
  1271. $v = array();
  1272. // mode can be 'create' or 'edit'
  1273. $v['mode'] = $mode;
  1274. // the template_id is the ID of the template if this is an edit, or FALSE if this is a create
  1275. $v['template_id'] = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  1276. $form_state['storage']['template_id'] = $v['template_id'];
  1277. $template_id = $v['template_id'];
  1278. $v['record_id'] = (isset($form_state['build_info']['args'][2])) ? $form_state['build_info']['args'][2] : FALSE;
  1279. $v['no_record_id'] = ($mode == 'create_record') ? TRUE : FALSE;
  1280. $form_state['storage']['record_id'] = $v['record_id'];
  1281. $record_id = $v['record_id'];
  1282. $priority = $record_id;
  1283. $v['field_index'] = (isset($form_state['build_info']['args'][3])) ? $form_state['build_info']['args'][3] : FALSE;
  1284. $v['no_field_index'] = (isset($form_state['build_info']['args'][3])) ? FALSE : TRUE;
  1285. $form_state['storage']['field_index'] = $v['field_index'];
  1286. $field_index = $v['field_index'];
  1287. if (!array_key_exists('values', $form_state)) {
  1288. $form_state['values'] = array();
  1289. }
  1290. // if we are editing an existing template then get the template array from the database
  1291. // and store it in the form_state['storage'] variable for later access
  1292. if ($mode == 'edit') {
  1293. if(!array_key_exists('template', $form_state['storage'])) {
  1294. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id = :template";
  1295. $template = db_query($sql, array(':template' => $template_id))->fetchObject();
  1296. // unserialize the template array. This array contains the PHP serilazed array
  1297. // describing the entire loader template
  1298. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1299. if (!$form_state['storage']['template_array']) {
  1300. $form_state['storage']['template_array'] = array();
  1301. }
  1302. // save the template record from the database for future use
  1303. $form_state['storage']['template'] = $template;
  1304. }
  1305. else {
  1306. $template = $form_state['storage']['template'];
  1307. }
  1308. }
  1309. // initialize the record2priority array which keeps track of the order by
  1310. // copying the priority information about each record from the template array
  1311. // into the $form_state['storage']['record2priority'] array
  1312. $form_state['storage']['record2priority'] = array();
  1313. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1314. if (!is_array($record_array)) {
  1315. continue;
  1316. }
  1317. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1318. }
  1319. // If we are editing then get the original values
  1320. if ($mode == 'edit' && !$v['no_field_index']) {
  1321. $template_field = $form_state['storage']['template_array'][$v['record_id']]['fields'][$field_index];
  1322. $original_field = $template_field;
  1323. $form_state['storage']['original_field'] = $template_field;
  1324. $form_state['storage']['original_field']['priority'] = $priority;
  1325. $form_state['storage']['original_field']['field_index'] = $field_index;
  1326. }
  1327. $v['template_name'] = (isset($template->name)) ? $template->name : '';
  1328. if (isset($form_state['values']['field_type'])) {
  1329. $v['field_type'] = $form_state['values']['field_type'];
  1330. }
  1331. elseif (isset($original_field['type'])) {
  1332. $v['field_type'] = $original_field['type'];
  1333. }
  1334. else {
  1335. $v['field_type'] = 'table field';
  1336. }
  1337. $field_type = $v['field_type'];
  1338. // Set field type options
  1339. // First set original field values in $form_state['values'] if not already set
  1340. switch ($v['field_type']) {
  1341. case 'table field':
  1342. if (!isset($form_state['values']['column_number'])) {
  1343. $form_state['values']['column_number'] = (isset($original_field['spreadsheet column'])) ? $original_field['spreadsheet column'] : '';
  1344. $form_state['values']['column_exposed'] = (isset($original_field['exposed'])) ? $original_field['exposed'] : FALSE;
  1345. $form_state['values']['column_exposed_desc'] = (isset($original_field['exposed_description'])) ? $original_field['exposed_description'] : FALSE;
  1346. }
  1347. break;
  1348. case 'constant':
  1349. if (!isset($form_state['values']['constant_value'])) {
  1350. $form_state['values']['constant_value'] = (isset($original_field['constant value'])) ? $original_field['constant value'] : '';
  1351. $form_state['values']['constant_exposed'] = (isset($original_field['exposed'])) ? $original_field['exposed'] : FALSE;
  1352. $form_state['values']['constant_validate'] = (isset($original_field['exposed_validate'])) ? $original_field['exposed_validate'] : FALSE;
  1353. }
  1354. break;
  1355. case 'foreign key':
  1356. if (!isset($form_state['values']['foreign_record'])) {
  1357. $form_state['values']['foreign_record'] = (isset($original_field['foreign key'])) ? $original_field['foreign key'] : FALSE;
  1358. $form_state['values']['foreign_field'] = (isset($original_field['foreign field'])) ? $original_field['foreign field'] : FALSE;
  1359. }
  1360. break;
  1361. }
  1362. // Ensure other field type fields are reset/cleared except for those of the current type
  1363. // ie: if the type is changed from spreadsheet column to constant, the spreadsheet column
  1364. // fields should be cleared since they don't apply to a constant
  1365. switch ($v['field_type']) {
  1366. case 'table field':
  1367. $v['column_number'] = (isset($form_state['values']['column_number'])) ? $form_state['values']['column_number'] : '';
  1368. $v['column_exposed'] = (isset($form_state['values']['column_exposed'])) ? $form_state['values']['column_exposed'] : FALSE;
  1369. $v['column_exposed_desc'] = (isset($form_state['values']['column_exposed_desc'])) ? $form_state['values']['column_exposed_desc'] : FALSE;
  1370. $v['constant_value'] = '';
  1371. $v['constant_exposed'] = FALSE;
  1372. $v['constant_validate'] = FALSE;
  1373. break;
  1374. case 'constant':
  1375. $v['column_number'] = '';
  1376. $v['column_exposed'] = FALSE;
  1377. $v['column_exposed_desc'] = FALSE;
  1378. $v['constant_value'] = (isset($form_state['values']['constant_value'])) ? $form_state['values']['constant_value'] : '';
  1379. $v['constant_exposed'] = (isset($form_state['values']['constant_exposed'])) ? $form_state['values']['constant_exposed'] : FALSE;
  1380. $v['constant_validate'] = (isset($form_state['values']['constant_validate'])) ? $form_state['values']['constant_validate'] : FALSE;
  1381. break;
  1382. case 'foreign key':
  1383. $v['column_number'] = '';
  1384. $v['column_exposed'] = FALSE;
  1385. $v['column_exposed_desc'] = FALSE;
  1386. $v['constant_value'] = '';
  1387. $v['constant_exposed'] = FALSE;
  1388. $v['constant_validate'] = FALSE;
  1389. break;
  1390. }
  1391. // Use the record_id as the priority/field_group
  1392. if ($v['no_record_id']) {
  1393. $form_state['values']['field_group'] = 'NEW';
  1394. $v['field_group'] = 'NEW';
  1395. }
  1396. else {
  1397. $form_state['values']['field_group'] = $record_id;
  1398. $v['field_group'] = $record_id;
  1399. if (preg_match('/\d+/', $record_id)) {
  1400. $priority = $form_state['values']['field_group'];
  1401. $table = $form_state['storage']['template_array'][$priority]['table'];
  1402. }
  1403. }
  1404. // Chado Tables and default table
  1405. $tables = chado_get_table_names(TRUE);
  1406. if (isset($form_state['values']['chado_table'])) {
  1407. if (!preg_match('/^' . current($tables) . '$/', $form_state['values']['chado_table'])) {
  1408. $table = $form_state['values']['chado_table'];
  1409. }
  1410. elseif (isset($form_state['values']['record_name'])) {
  1411. $record_name = $form_state['values']['record_name'];
  1412. $priority = $form_state['storage']['record2priority'][$record_name];
  1413. $table = $form_state['storage']['template_array'][$priority]['table'];
  1414. }
  1415. else {
  1416. $priority = $form_state['values']['field_group'];
  1417. $table = $form_state['storage']['template_array'][$priority]['table'];
  1418. }
  1419. }
  1420. if (!isset($table)) {
  1421. $table = reset($tables);
  1422. }
  1423. // Chado fields
  1424. $chado_fields = array();
  1425. $table_description = chado_get_schema($table);
  1426. foreach ($table_description['fields'] as $field_name => $field_array) {
  1427. $chado_fields[$field_name] = $field_name;
  1428. }
  1429. // Chado Default Field
  1430. if (empty($chado_fields)) {
  1431. $field = NULL;
  1432. }
  1433. elseif (isset($form_state['values']['chado_field'])) {
  1434. if (isset($chado_fields[$form_state['values']['chado_field']])) {
  1435. $field = $form_state['values']['chado_field'];
  1436. }
  1437. else {
  1438. $field = current($chado_fields);
  1439. }
  1440. }
  1441. elseif (isset($original_field['field'])) {
  1442. $field = $original_field['field'];
  1443. }
  1444. else {
  1445. $field = current($chado_fields);
  1446. }
  1447. // Field group options
  1448. if (!array_key_exists('record2priority', $form_state['storage'])) {
  1449. $groups = array();
  1450. }
  1451. else {
  1452. $groups = array_flip($form_state['storage']['record2priority']);
  1453. }
  1454. $groups['NONE'] = 'Select a Record';
  1455. $groups['NEW'] = 'New Record';
  1456. // Field Group Default
  1457. if ($record_id) {
  1458. $field_group = $record_id;
  1459. }
  1460. elseif (isset($form_state['values']['field_group'])) {
  1461. $field_group = $form_state['values']['field_group'];
  1462. }
  1463. else {
  1464. if ($mode == 'add') {
  1465. $field_group = 'NEW';
  1466. }
  1467. else {
  1468. $field_group = 'NONE';
  1469. }
  1470. }
  1471. $v['field_group'] = $field_group;
  1472. $v['field_group_options'] = $groups;
  1473. $v['chado_table'] = $table;
  1474. $v['chado_table_options'] = $tables;
  1475. $v['chado_field'] = $field;
  1476. $v['chado_field_options'] = $chado_fields;
  1477. $v['record_name'] = (isset($form_state['values']['record_name'])) ? $form_state['values']['record_name'] : '';
  1478. if (isset($form_state['values']['field_title'])) {
  1479. $v['field_title'] = $form_state['values']['field_title'];
  1480. }
  1481. elseif (isset($original_field['title'])) {
  1482. $v['field_title'] = $original_field['title'];
  1483. }
  1484. else {
  1485. $v['field_title'] = '';
  1486. }
  1487. $v['show_all_records'] = (isset($form_state['values']['show_all_records'])) ? $form_state['values']['show_all_records'] : FALSE;
  1488. // Foreign key options
  1489. $fk_options = array();
  1490. $fk_options['NULL'] = 'None';
  1491. $show_all = $v['show_all_records'];
  1492. if ($field_type == 'foreign key' and !$show_all) {
  1493. $foreign_field2table = array();
  1494. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1495. foreach ($key_array['columns'] as $left_field => $right_field) {
  1496. //$chado_fields[$left_field] = $left_field;
  1497. $foreign_field2table[$left_field] = $key_table;
  1498. }
  1499. }
  1500. // Foreign key options
  1501. $foreign_table = $foreign_field2table[$field];
  1502. if ($foreign_table) {
  1503. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  1504. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority]['table'])) {
  1505. $fk_options[$record_name] = $record_name;
  1506. }
  1507. }
  1508. }
  1509. }
  1510. // the user wants to see all of the records
  1511. elseif ($field_type == 'foreign key' and $show_all) {
  1512. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  1513. $fk_options[$record_name] = $record_name;
  1514. }
  1515. }
  1516. // build the list of referrer table fields. This list is used
  1517. // for the referring table field drop down. It has two groups
  1518. // one for foreign keys and another for additional fields.
  1519. $ref_chado_fields = array();
  1520. if ($field_type == 'foreign key') {
  1521. $fk_rec = $form_state['values']['foreign_record'];
  1522. if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') {
  1523. // first add in the foreign key field in the referring table
  1524. // that corresponds to this field (if one exists).
  1525. $fk_priority = $form_state['storage']['record2priority'][$fk_rec];
  1526. $fk_table = $form_state['storage']['template_array'][$fk_priority]['table'];
  1527. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1528. foreach ($key_array['columns'] as $left_field => $right_field) {
  1529. if ($key_table == $fk_table and $left_field == $field) {
  1530. $ref_chado_fields['Foreign Key'][$right_field] = $right_field;
  1531. }
  1532. }
  1533. }
  1534. // now add in the remaining fields of the referring record's table.
  1535. $fk_description = chado_get_schema($fk_table);
  1536. foreach ($fk_description['fields'] as $fk_field_name => $fk_farray) {
  1537. // don't include the FK field it's included above
  1538. if (in_array('Foreign Key', $ref_chado_fields)) {
  1539. if (in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) {
  1540. continue;
  1541. }
  1542. }
  1543. $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name;
  1544. }
  1545. }
  1546. }
  1547. $ref_chado_fields = (!empty($ref_chado_fields)) ? $ref_chado_fields : array('NULL', 'None');
  1548. $v['foreign_record_options'] = $fk_options;
  1549. $v['foreign_record'] = (isset($form_state['values']['foreign_record'])) ? $form_state['values']['foreign_record'] : 'NULL';
  1550. $v['foreign_field_options'] = $ref_chado_fields;
  1551. $v['foreign_field'] = (isset($form_state['values']['foreign_field'])) ? $form_state['values']['foreign_field'] : current($ref_chado_fields);
  1552. $v['required'] = (isset($form_state['values']['required'])) ? $form_state['values']['required'] : FALSE;
  1553. if (isset($original_field['regex']) && empty($form_state['storage']['regex']['pattern'])) {
  1554. $form_state['storage']['regex']['pattern'] = $original_field['regex']['pattern'];
  1555. $form_state['storage']['regex']['replace'] = $original_field['regex']['replace'];
  1556. }
  1557. $v['regex_are_set'] = (isset($form_state['storage']['regex']['pattern'])) ? TRUE : FALSE;
  1558. $v['regex-pattern'] = (isset($form_state['storage']['regex']['pattern'])) ? $form_state['storage']['regex']['pattern'] : array();
  1559. $v['regex-replace'] = (isset($form_state['storage']['regex']['replace'])) ? $form_state['storage']['regex']['replace'] : array();
  1560. $v['regex-test-string'] = (isset($form_state['storage']['test_regex_test'])) ? $form_state['storage']['test_regex_test'] : '';
  1561. $v['regex-test-result'] = (isset($form_state['storage']['test_regex_result'])) ? $form_state['storage']['test_regex_result'] : '';
  1562. return $v;
  1563. }
  1564. /**
  1565. * Add Field Form
  1566. *
  1567. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  1568. * can use the following in the query of the path to set defaults for a given template:
  1569. * - template_id=\d+: the template to add the field to
  1570. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  1571. *
  1572. * @param $form_state
  1573. * Contains the values and storage for the form
  1574. * @return
  1575. * A form array to be rendered by drupal_get_form
  1576. *
  1577. * @ingroup tripal_bulk_loader
  1578. */
  1579. function tripal_bulk_loader_template_field_form($form, &$form_state = NULL) {
  1580. $form['#cache'] = TRUE; // Make sure the form is cached.
  1581. // if there is no template supplied don't build the form
  1582. if (!isset($form_state['build_info']['args'][1])) {
  1583. return $form;
  1584. }
  1585. // if there is no template array saved then retrieve it
  1586. elseif (!isset($form_state['storage']['template_array'])) {
  1587. $form_state['storage']['template'] = db_select('tripal_bulk_loader_template', 't')
  1588. ->fields('t')
  1589. ->condition('t.template_id',$form_state['build_info']['args'][1],'=')
  1590. ->execute()
  1591. ->fetchObject();
  1592. $form_state['storage']['template_array'] = unserialize($form_state['storage']['template']->template_array);
  1593. $form_state['storage']['template_id'] = $form_state['build_info']['args'][1];
  1594. if (isset($form_state['build_info']['args'][2])) {
  1595. $form_state['storage']['record_id'] = $form_state['build_info']['args'][2];
  1596. }
  1597. if (isset($form_state['build_info']['args'][3])) {
  1598. $form_state['storage']['field_index'] = $form_state['build_info']['args'][3];
  1599. }
  1600. }
  1601. $v['mode'] = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : 'create';
  1602. $form_state['storage']['mode'] = $v['mode'];
  1603. $mode = $v['mode'];
  1604. $values = tripal_bulk_loader_template_field_form_default_values($mode, $form_state);
  1605. // set the breadcrumb
  1606. $breadcrumb = array();
  1607. $breadcrumb[] = l('Home', '<front>');
  1608. $breadcrumb[] = l('Administration', 'admin');
  1609. $breadcrumb[] = l('Tripal', 'admin/tripal');
  1610. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  1611. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  1612. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  1613. $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $values['template_id'] . '/edit');
  1614. drupal_set_breadcrumb($breadcrumb);
  1615. $form['template_name'] = array(
  1616. '#type' => 'item',
  1617. '#title' => 'Template',
  1618. '#markup' => $values['template_name'],
  1619. );
  1620. $form['template_id'] = array(
  1621. '#type' => 'hidden',
  1622. '#value' => $values['template_id'],
  1623. );
  1624. $form['fields'] = array(
  1625. '#type' => 'markup',
  1626. '#prefix' => '<div id="tripal_bulk_loader_template-template_fields">',
  1627. '#suffix' => '</div>',
  1628. );
  1629. $form['fields']['basic'] = array(
  1630. '#type' => 'markup',
  1631. '#prefix' => '<div class="basic">',
  1632. '#suffix' => '</div>'
  1633. );
  1634. $form['fields']['basic']['field_type'] = array(
  1635. '#type' => 'radios',
  1636. '#title' => t('Type of Field'),
  1637. '#options' => array(
  1638. 'table field' => t('Data: A Field which maps to a column in the supplied file.'),
  1639. 'constant' => t('Constant: Field which remains Constant throughout the file'),
  1640. 'foreign key' => t('Record Referral: Fields which map to a record in another table'),
  1641. ),
  1642. '#required' => TRUE,
  1643. '#default_value' => $values['field_type'],
  1644. '#ajax' => array(
  1645. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1646. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1647. 'effect' => 'fade'
  1648. ),
  1649. );
  1650. $form['fields']['basic']['field_group'] = array(
  1651. '#type' => 'select',
  1652. '#title' => 'Record',
  1653. '#description' => t(
  1654. 'This is used to group a set of fields together allowing ' .
  1655. 'multiple records to be inserted into the same table per line of the file'),
  1656. '#options' => $values['field_group_options'],
  1657. '#default_value' => $values['field_group'],
  1658. '#ajax' => array(
  1659. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1660. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1661. 'effect' => 'fade'
  1662. ),
  1663. '#required' => TRUE,
  1664. );
  1665. $form['fields']['basic']['record_name'] = array(
  1666. '#type' => ($values['no_record_id']) ? 'textfield' : 'hidden',
  1667. '#title' => 'Unique Record Name',
  1668. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  1669. '#suffix' => '</div>',
  1670. '#description' => 'A human-readable name for the record; it should be unique.',
  1671. '#default_value' => $values['record_name'],
  1672. );
  1673. $form['fields']['basic']['field_title'] = array(
  1674. '#type' => 'textfield',
  1675. '#title' => t('Human-readable Title for Field'),
  1676. '#default_value' => $values['field_title'],
  1677. );
  1678. // Chado Field
  1679. $form['fields']['chado'] = array(
  1680. '#type' => 'fieldset',
  1681. '#title' => t('Chado Field/Column Details'),
  1682. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1683. );
  1684. $form['fields']['chado']['chado_table'] = array(
  1685. '#type' => 'select',
  1686. '#title' => t('Chado Table'),
  1687. '#options' => $values['chado_table_options'],
  1688. '#default_value' => $values['chado_table'],
  1689. '#ajax' => array(
  1690. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1691. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1692. 'effect' => 'fade'
  1693. ),
  1694. );
  1695. $form['fields']['chado']['chado_field'] = array(
  1696. '#type' => 'select',
  1697. '#title' => t('Chado Field/Column'),
  1698. '#options' => $values['chado_field_options'],
  1699. '#default_value' => $values['chado_field'],
  1700. '#ajax' => array(
  1701. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1702. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1703. 'effect' => 'fade'
  1704. ),
  1705. );
  1706. // loading file data column
  1707. $form['fields']['columns'] = array(
  1708. '#type' => 'fieldset',
  1709. '#title' => t('Data File Column'),
  1710. '#collapsible' => TRUE,
  1711. '#collapsed' => ($values['field_type'] == 'table field')? FALSE : TRUE,
  1712. );
  1713. $form['fields']['columns']['column_number'] = array(
  1714. '#type' => 'textfield',
  1715. '#title' => t('Column'),
  1716. '#description' => t('Specify the column in the data that this field maps to where the first column is 1.'),
  1717. '#size' => 5,
  1718. '#default_value' => $values['column_number'],
  1719. );
  1720. $form['fields']['columns']['column_exposed'] = array(
  1721. '#type' => 'checkbox',
  1722. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1723. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1724. '#default_value' => $values['column_exposed'],
  1725. );
  1726. $form['fields']['columns']['column_exposed_desc'] = array(
  1727. '#type' => 'textfield',
  1728. '#title' => t('Description for exposed field on bulk loading job'),
  1729. '#description' => t('This description should tell the user what column should be entered here.'),
  1730. '#default_value' => $values['column_exposed_desc'],
  1731. );
  1732. // Global Value
  1733. $form['fields']['constant'] = array(
  1734. '#type' => 'fieldset',
  1735. '#title' => t('Constant'),
  1736. '#collapsible' => TRUE,
  1737. '#collapsed' => ($values['field_type'] == 'constant')? FALSE : TRUE,
  1738. );
  1739. $form['fields']['constant']['constant_value'] = array(
  1740. '#type' => 'textfield',
  1741. '#title' => t('Constant Value'),
  1742. '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
  1743. '#default_value' => $values['constant_value']
  1744. );
  1745. $form['fields']['constant']['constant_exposed'] = array(
  1746. '#type' => 'checkbox',
  1747. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1748. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'),
  1749. '#default_value' => $values['constant_exposed'],
  1750. );
  1751. $form['fields']['constant']['constant_validate'] = array(
  1752. '#type' => 'checkbox',
  1753. '#title' => t('Ensure value is in table'),
  1754. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1755. '#default_value' => $values['constant_validate'],
  1756. );
  1757. // Foreign Key / Referrer
  1758. $form['fields']['foreign_key'] = array(
  1759. '#type' => 'fieldset',
  1760. '#title' => 'Record Referral',
  1761. '#collapsible' => TRUE,
  1762. '#collapsed' => ($values['field_type'] == 'foreign key')? FALSE : TRUE,
  1763. );
  1764. $form['fields']['foreign_key']['show_all_records'] = array(
  1765. '#type' => 'checkbox',
  1766. '#title' => 'Refer to any record',
  1767. '#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'),
  1768. '#default_value' => $values['show_all_records'],
  1769. '#ajax' => array(
  1770. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1771. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1772. 'effect' => 'fade'
  1773. ),
  1774. );
  1775. $form['fields']['foreign_key']['foreign_record'] = array(
  1776. '#type' => 'select',
  1777. '#title' => 'Record to refer to',
  1778. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1779. '#options' => $values['foreign_record_options'],
  1780. '#ajax' => array(
  1781. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1782. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1783. 'effect' => 'fade'
  1784. ),
  1785. '#default_value' => $values['foreign_record'],
  1786. );
  1787. $form['fields']['foreign_key']['foreign_field'] = array(
  1788. '#type' => 'select',
  1789. '#title' => 'Field to refer to',
  1790. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1791. '#options' => $values['foreign_field_options'],
  1792. '#default_value' => $values['foreign_field'],
  1793. );
  1794. $collapsed = TRUE;
  1795. if ($values['required'] OR $values['regex_are_set']) {
  1796. $collapsed = FALSE;
  1797. }
  1798. $form['fields']['additional'] = array(
  1799. '#type' => 'fieldset',
  1800. '#title' => 'Additional Options',
  1801. '#collapsible' => TRUE,
  1802. '#collapsed' => $collapsed
  1803. );
  1804. $form['fields']['additional']['required'] = array(
  1805. '#type' => 'checkbox',
  1806. '#title' => 'Make this field required',
  1807. '#default_value' => $values['required'],
  1808. );
  1809. $form['fields']['additional']['regex_transform'] = array(
  1810. '#type' => 'fieldset',
  1811. '#title' => 'Transform Data File Value Rules',
  1812. '#collapsible' => TRUE,
  1813. '#collapsed' => !$values['regex_are_set'],
  1814. );
  1815. $form['fields']['additional']['regex_transform']['regex_description'] = array(
  1816. '#type' => 'item',
  1817. '#markup' => 'A transformation rule allows you to transform the original value '
  1818. .'(usually from a user submitted data file) into the form you would like it stored '
  1819. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1820. .'which determines which replacement patterns are applied and captures regions of the '
  1821. .'original value) and a replacement pattern (a string which may contain capture references '
  1822. .'that describes what the new value should be). Each rule is applied to the result of the '
  1823. .'previous rule.'
  1824. );
  1825. $form['fields']['additional']['regex_transform']['regex-data'] = array(
  1826. '#tree' => TRUE,
  1827. '#theme' => 'tripal_bulk_loader_field_regex_fieldset'
  1828. );
  1829. foreach ($values['regex-pattern'] as $index => $pattern) {
  1830. $data_element = array(
  1831. 'pattern' => array(
  1832. '#type' => 'item',
  1833. '#markup' => check_plain($pattern),
  1834. ),
  1835. 'replace' => array(
  1836. '#type' => 'item',
  1837. '#markup' => check_plain($values['regex-replace'][$index]),
  1838. ),
  1839. 'old_index' => array(
  1840. '#type' => 'hidden',
  1841. '#value' => $index,
  1842. ),
  1843. 'new_index' => array(
  1844. '#type' => 'select',
  1845. '#options' => range(0, sizeof($values['regex-pattern'])-1),
  1846. '#default_value' => $index,
  1847. '#attributes' => array('class' => array('rank-weight')), // needed for table dragging
  1848. ),
  1849. 'id' => array(
  1850. '#type' => 'hidden',
  1851. '#value' => $index,
  1852. ),
  1853. 'submit-delete' => array(
  1854. '#type' => 'submit',
  1855. '#value' => 'Delete Transformation',
  1856. '#name' => $index,
  1857. ),
  1858. );
  1859. $form['fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1860. }
  1861. $form['fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1862. '#type' => ($values['regex_are_set']) ? 'submit' : 'hidden',
  1863. '#value' => 'Save Transformation Rule Order'
  1864. );
  1865. $form['fields']['additional']['regex_transform']['new_regex'] = array(
  1866. '#type' => 'fieldset',
  1867. '#title' => 'Add a new Transformation Rule',
  1868. );
  1869. $form['fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1870. '#type' => 'textfield',
  1871. '#title' => 'Match Pattern',
  1872. '#description' => 'You can use standard php regular expressions in this field to specify a '
  1873. .'pattern. Only if this pattern matches the value in the data file does the replacement '
  1874. .'pattern get applied to the value. To capture a section of your value for use in the '
  1875. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1876. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1877. .' To match and capture any value use <i>.*</i>',
  1878. );
  1879. $form['fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1880. '#type' => 'textfield',
  1881. '#title' => 'Replacement Pattern',
  1882. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  1883. .'mentioned above. It can include references of the form \n where n is the number of the '
  1884. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1885. .'first set of round brackets.',
  1886. );
  1887. if ($values['field_type'] == 'table field') {
  1888. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1889. $form['fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1890. .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
  1891. .'This allows you to substitute other data file values into the current field. For example, '
  1892. .'if you had the following line:<br />'
  1893. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1894. .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
  1895. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1896. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1897. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1898. }
  1899. $form['fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1900. '#type' => 'submit',
  1901. '#value' => 'Add Transformation',
  1902. );
  1903. $form['fields']['additional']['regex_transform']['test_regex'] = array(
  1904. '#type' => 'fieldset',
  1905. '#title' => 'Test Transformation Rules',
  1906. );
  1907. $form['fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1908. '#type' => 'textfield',
  1909. '#title' => 'Test Value',
  1910. '#description' => 'This should be a value that you expect the above transformation rules '
  1911. .'to be applied to.',
  1912. '#default_value' => $values['regex-test-string'],
  1913. );
  1914. $form['fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1915. '#type' => 'textfield',
  1916. '#title' => 'Test Result',
  1917. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1918. .'riles were applied to the Test Value.',
  1919. '#default_value' => $values['regex-test-result'],
  1920. );
  1921. $form['fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1922. '#type' => 'submit',
  1923. '#value' => 'Test Transformation Rules'
  1924. );
  1925. $form['submit-save'] = array(
  1926. '#type' => 'submit',
  1927. '#value' => 'Save Changes'
  1928. );
  1929. $form['submit-cancel'] = array(
  1930. '#type' => 'submit',
  1931. '#value' => 'Cancel'
  1932. );
  1933. return $form;
  1934. }
  1935. /**
  1936. * Implements hook_form_validate().
  1937. *
  1938. * @ingroup tripal_bulk_loader
  1939. */
  1940. function tripal_bulk_loader_template_field_form_validate($form, &$form_state) {
  1941. // Don't worry about validation when Cancel button is clicked
  1942. if ($form_state['clicked_button']['#value'] == 'Save Changes') {
  1943. $is_unique = tripal_bulk_loader_is_record_name_unique(
  1944. $form_state['values']['record_name'],
  1945. $form_state['values']['template_id'],
  1946. $form_state['storage']['template_array']
  1947. );
  1948. $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
  1949. if ((!$is_unique) AND $new_record) {
  1950. form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
  1951. }
  1952. }
  1953. }
  1954. /**
  1955. * Add Field Submit
  1956. *
  1957. * @param $form
  1958. * The form that was submitted
  1959. * @param $form_state
  1960. * The values and storage for the form
  1961. *
  1962. * @ingroup tripal_bulk_loader
  1963. */
  1964. function tripal_bulk_loader_template_field_form_submit($form, &$form_state) {
  1965. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1966. if ($op == 'Save Changes') {
  1967. $template = $form_state['storage']['template_array'];
  1968. // If new record
  1969. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  1970. $record_name = $form_state['values']['record_name'];
  1971. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1972. $record2priority[$record_name] = $priority;
  1973. $template[$priority]['table'] = $form_state['values']['chado_table'];
  1974. $template[$priority]['record_id'] = $record_name;
  1975. }
  1976. else {
  1977. $priority = $form_state['storage']['record_id'];
  1978. }
  1979. // Add field to template array
  1980. if ($form_state['values']['field_type'] == 'table field') {
  1981. $field = array(
  1982. 'type' => 'table field',
  1983. 'title' => $form_state['values']['field_title'],
  1984. 'field' => $form_state['values']['chado_field'],
  1985. 'required' => $form_state['values']['required'],
  1986. //'allowed values' => empty by default,
  1987. 'spreadsheet column' => $form_state['values']['column_number'],
  1988. 'exposed' => $form_state['values']['column_exposed'],
  1989. 'exposed_description' => $form_state['values']['column_exposed_desc'],
  1990. );
  1991. }
  1992. elseif ($form_state['values']['field_type'] == 'constant') {
  1993. $field = array(
  1994. 'type' => 'constant',
  1995. 'title' => $form_state['values']['field_title'],
  1996. 'field' => $form_state['values']['chado_field'],
  1997. 'required' => $form_state['values']['required'],
  1998. //'allowed values' => empty by default,
  1999. 'constant value' => $form_state['values']['constant_value'],
  2000. 'exposed' => $form_state['values']['constant_exposed'],
  2001. 'exposed_validate' => $form_state['values']['constant_validate'],
  2002. );
  2003. }
  2004. elseif ($form_state['values']['field_type'] == 'foreign key') {
  2005. $field = array(
  2006. 'type' => 'foreign key',
  2007. 'title' => $form_state['values']['field_title'],
  2008. 'field' => $form_state['values']['chado_field'],
  2009. 'show_all_records' => $form_state['values']['show_all_records'],
  2010. 'foreign key' => $form_state['values']['foreign_record'],
  2011. 'foreign field' => $form_state['values']['foreign_field'],
  2012. 'required' => $form_state['values']['required'],
  2013. );
  2014. }
  2015. // Deal with any additional options
  2016. if (isset($form_state['storage']['regex'])) {
  2017. $field['regex'] = $form_state['storage']['regex'];
  2018. }
  2019. // Save Template
  2020. if ($form_state['storage']['mode'] == 'create') {
  2021. $template[$priority]['fields'][] = $field;
  2022. }
  2023. else {
  2024. $template[$priority]['fields'][$form_state['storage']['field_index']] = $field;
  2025. }
  2026. $form_state['storage']['template']->template_array = serialize($template);
  2027. $form_state['storage']['template']->changed = time();
  2028. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  2029. if ($success) {
  2030. drupal_set_message(t('Successfully Added Field to Template'));
  2031. drupal_set_message(t('Template Saved.'));
  2032. $form_state['rebuild'] = FALSE;
  2033. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/edit';
  2034. }
  2035. else {
  2036. drupal_set_message(t('Unable to Save Template!'), 'error');
  2037. tripal_report_error('T_bulk_loader',
  2038. TRIPAL_ERROR,
  2039. 'Unable to save bulk loader template: %template',
  2040. array('%template' => print_r($form_state['storage']['template'], TRUE))
  2041. );
  2042. }
  2043. }
  2044. elseif ($op == 'Cancel') {
  2045. $form_state['rebuild'] = FALSE;
  2046. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/edit';
  2047. }
  2048. elseif ($op == 'Add Transformation') {
  2049. // Add transformation rule to original field
  2050. $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  2051. $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
  2052. $form_state['rebuild'] = TRUE;
  2053. drupal_set_message(t('Successfully Added Transformation Rule'));
  2054. }
  2055. elseif ($op == 'Save Transformation Rule Order') {
  2056. // Generate new regex array
  2057. $new_regex = array();
  2058. $old_regex = $form_state['storage']['regex'];
  2059. foreach ($form_state['values']['regex-data'] as $key => $element) {
  2060. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  2061. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  2062. }
  2063. // sort new regex arrays
  2064. ksort($new_regex['pattern']);
  2065. ksort($new_regex['replace']);
  2066. $form_state['storage']['regex'] = $new_regex;
  2067. }
  2068. elseif ($op == 'Delete Transformation') {
  2069. // Unset regex rule
  2070. $index = $form_state['clicked_button']['#name'];
  2071. unset($form_state['storage']['regex']['pattern'][$index]);
  2072. unset($form_state['storage']['regex']['replace'][$index]);
  2073. }
  2074. elseif ($op == 'Test Transformation Rules') {
  2075. $patterns = $form_state['storage']['regex']['pattern'];
  2076. $replaces = $form_state['storage']['regex']['replace'];
  2077. $test_string = $form_state['values']['test_string'];
  2078. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  2079. $form_state['storage']['test_regex_test'] = $test_string;
  2080. }
  2081. }
  2082. /**
  2083. * Delete Field Form
  2084. *
  2085. * This form is meant to be called from a bulk loader form
  2086. *
  2087. * @param $form_state
  2088. * Contains the values and storage for the form
  2089. * @return
  2090. * A form array to be rendered by drupal_get_form
  2091. *
  2092. * @ingroup tripal_bulk_loader
  2093. */
  2094. function tripal_bulk_loader_delete_template_field_form($form, &$form_state) {
  2095. $form['#cache'] = TRUE; // Make sure the form is cached.
  2096. // get args from path
  2097. $template_id = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : FALSE;
  2098. $form_state['storage']['template_id'] = $template_id;
  2099. $record_id = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  2100. $form_state['storage']['record_id'] = $record_id;
  2101. $field_id = (isset($form_state['build_info']['args'][2])) ? $form_state['build_info']['args'][2] : FALSE;
  2102. $form_state['storage']['field_id'] = $field_id;
  2103. // if there is no template supplied don't return rest of form
  2104. if (!$template_id) {
  2105. tripal_set_message('Unable to determine the template_id from the path.',TRIPAL_ERROR);
  2106. return $form;
  2107. }
  2108. elseif (!$record_id) {
  2109. tripal_set_message('Unable to determine the record_id from the path.',TRIPAL_ERROR);
  2110. return $form;
  2111. }
  2112. elseif (!$field_id) {
  2113. tripal_set_message('Unable to determine the field_id from the path.',TRIPAL_ERROR);
  2114. return $form;
  2115. }
  2116. // set the breadcrumb
  2117. $breadcrumb = array();
  2118. $breadcrumb[] = l('Home', '<front>');
  2119. $breadcrumb[] = l('Administration', 'admin');
  2120. $breadcrumb[] = l('Tripal', 'admin/tripal');
  2121. $breadcrumb[] = l('Chado Data Loaders', 'admin/tripal/loaders');
  2122. $breadcrumb[] = l('Bulk Loader', 'admin/tripal/loaders/bulk');
  2123. $breadcrumb[] = l('Templates', 'admin/tripal/loaders/bulk/templates');
  2124. $breadcrumb[] = l('Edit Template', 'admin/tripal/loaders/bulk/template/' . $template_id . '/edit');
  2125. drupal_set_breadcrumb($breadcrumb);
  2126. $_GET['record_name'] = (isset($_GET['record_name'])) ? $_GET['record_name'] : '';
  2127. $_GET['field_name'] = (isset($_GET['field_name'])) ? $_GET['field_name'] : '';
  2128. $_GET['chado_table'] = (isset($_GET['chado_table'])) ? $_GET['chado_table'] : '';
  2129. $_GET['chado_field'] = (isset($_GET['chado_field'])) ? $_GET['chado_field'] : '';
  2130. $_GET['data_column'] = (isset($_GET['data_column'])) ? $_GET['data_column'] : '';
  2131. $_GET['constant_value'] = (isset($_GET['constant_value'])) ? $_GET['constant_value'] : '';
  2132. $_GET['foreign_record'] = (isset($_GET['foreign_record'])) ? $_GET['foreign_record'] : '';
  2133. $description = '';
  2134. $description .= '<table>';
  2135. $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>';
  2136. $description .= '<tr><td>' . $_GET['record_name']
  2137. . '</td><td>' . $_GET['field_name']
  2138. . '</td><td>' . $_GET['chado_table']
  2139. . '</td><td>' . $_GET['chado_field']
  2140. . '</td><td>' . $_GET['data_column']
  2141. . '</td><td>' . $_GET['constant_value']
  2142. . '</td><td>' . $_GET['foreign_record']
  2143. . '</td></tr>';
  2144. $description .= '</table>';
  2145. $description .= '<p><strong>Are you sure you want to delete this field?</strong></p>';
  2146. $yes = 'Delete Field';
  2147. $no = 'Cancel';
  2148. $form['#attributes']['class'][] = 'confirmation';
  2149. $form['description'] = array('#markup' => $description);
  2150. $form['actions'] = array('#type' => 'actions');
  2151. $form['actions']['submit'] = array(
  2152. '#type' => 'submit',
  2153. '#value' => $yes ? $yes : t('Confirm'),
  2154. );
  2155. $form['actions']['cancel'] = array(
  2156. '#type' => 'link',
  2157. '#title' => $no ? $no : t('Cancel'),
  2158. '#href' => 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit',
  2159. );
  2160. // By default, render the form using theme_confirm_form().
  2161. if (!isset($form['#theme'])) {
  2162. $form['#theme'] = 'confirm_form';
  2163. }
  2164. return $form;
  2165. }
  2166. /**
  2167. * Delete Field Form
  2168. *
  2169. * This form is meant to be called from a bulk loader form
  2170. *
  2171. * @param $form_state
  2172. * Contains the values and storage for the form
  2173. * @return
  2174. * A form array to be rendered by drupal_get_form
  2175. *
  2176. * @ingroup tripal_bulk_loader
  2177. */
  2178. function tripal_bulk_loader_delete_template_field_form_submit($form, &$form_state) {
  2179. $form_state['rebuild'] = FALSE;
  2180. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/edit';
  2181. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  2182. $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
  2183. $form_state['storage']['template'] = unserialize($result->template_array);
  2184. $new_template = tripal_bulk_loader_delete_field(
  2185. $form_state['storage']['record_id'],
  2186. $form_state['storage']['field_id'],
  2187. $form_state['storage']['template']
  2188. );
  2189. if (!empty($new_template)) {
  2190. $form_state['storage']['template'] = $new_template;
  2191. }
  2192. drupal_set_message(t('Deleted Field from Template.'));
  2193. $record = array(
  2194. 'template_id' => $form_state['storage']['template_id'],
  2195. 'template_array' => serialize($form_state['storage']['template']),
  2196. 'changed' => time()
  2197. );
  2198. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  2199. drupal_set_message(t('Template Saved.'));
  2200. }
  2201. /**
  2202. * @section
  2203. * AHAH Callbacks
  2204. */
  2205. /**
  2206. * AHAH Function: Replace $form['fields'] in tripal_bulk_loader_add_template_field_form
  2207. *
  2208. * @return
  2209. * JSON Data printed to the screen
  2210. *
  2211. * @ingroup tripal_bulk_loader
  2212. */
  2213. function tripal_bulk_loader_template_fields_ahah($form, $form_state) {
  2214. return $form['fields'];
  2215. }