tripal_bulk_loader.admin.templates.inc 113 KB

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