tripal_bulk_loader.admin.inc.orig 90 KB

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