tripal_bulk_loader.admin.templates.inc 94 KB

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