tripal_bulk_loader.admin.templates.inc 93 KB

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