tripal_bulk_loader.admin.templates.inc 97 KB

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