tripal_bulk_loader.admin.templates.inc 86 KB

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