tripal_bulk_loader.admin.templates.inc 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367
  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 or too many exist.'),
  680. '#description' => t('By default if a select does not find a match the loader will fail, or if it finds too many matches it will fail. Check here to allow the loader to continue when no match is found. In either case no value is passed on.'),
  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. // TODO: finish coding up the update_if_duplicate functionality
  695. /* $form['edit_record']['update_if_duplicate'] = array(
  696. '#type' => 'checkbox',
  697. '#title' => t('UPDATE if duplicate (no insert)'),
  698. '#description' => t('If this is not the first time this record has been added then perform an update rather than an insert.'),
  699. '#default_value' => $update_if_duplicate
  700. );
  701. */
  702. $form['edit_record']['optional'] = array(
  703. '#type' => 'checkbox',
  704. '#title' => t('Optional'),
  705. '#description' => t('The insert, update or select will only be performed if all required data are present'),
  706. '#default_value' => $optional
  707. );
  708. $form['edit_record']['disable'] = array(
  709. '#type' => 'checkbox',
  710. '#title' => t('Disable this record'),
  711. '#description' => t("Check this box to ignore this record (not perform select or insert) during template loading. Uncheck to re-enable the record"),
  712. '#default_value' => $disable,
  713. );
  714. $form['edit_record']['submit-edit_record'] = array(
  715. '#type' => 'submit',
  716. '#value' => 'Edit Record'
  717. );
  718. $form['edit_record']['submit-cancel'] = array(
  719. '#type' => 'submit',
  720. '#value' => 'Cancel'
  721. );
  722. return $form;
  723. }
  724. /**
  725. * Edit Record Form Submit
  726. *
  727. * @param $form
  728. * The form that was submitted
  729. * @param $form_state
  730. * Contains the values and storage for the form
  731. */
  732. function tripal_bulk_loader_edit_template_record_form_submit($form, &$form_state) {
  733. //dpm($form_state, 'form state -start submit');
  734. if (!$form_state['ahah_submission']) {
  735. if ($form_state['values']['op'] == 'Edit Record') {
  736. $template = $form_state['storage']['template_array'];
  737. // Edit Record
  738. $record = $template[ $form_state['storage']['original_priority'] ];
  739. $record['record_id'] = $form_state['values']['record_name'];
  740. $record['mode'] = $form_state['values']['mode'];
  741. $record['table'] = $form_state['values']['chado_table'];
  742. $record['select_if_duplicate'] = $form_state['values']['select_if_duplicate'];
  743. $record['update_if_duplicate'] = $form_state['values']['update_if_duplicate'];
  744. $record['select_optional'] = $form_state['values']['select_optional'];
  745. $record['disable'] = $form_state['values']['disable'];
  746. $record['optional'] = $form_state['values']['optional'];
  747. if ($form_state['storage']['original_priority'] != $form_state['values']['field_group']) {
  748. $record['fields'] = array_merge($record['fields'], $template[ $form_state['values']['field_group'] ]['fields']);
  749. $template[ $form_state['values']['field_group'] ] = $record;
  750. unset($template[ $form_state['storage']['original_priority'] ]);
  751. }
  752. else {
  753. $template[ $form_state['storage']['original_priority'] ] = $record;
  754. }
  755. // Save Template
  756. $form_state['storage']['template']->template_array = serialize($template);
  757. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  758. if ($success) {
  759. drupal_set_message(t('Successfully Updated Template Record'));
  760. drupal_set_message(t('Template Saved.'));
  761. $path = explode('?', $form_state['storage']['referring URL']);
  762. parse_str($path[1], $query);
  763. $query['template_id'] = $form_state['storage']['template']->template_id;
  764. drupal_goto($path[0], $query);
  765. }
  766. else {
  767. drupal_set_message(t('Unable to Save Template!'), 'error');
  768. watchdog('T_bulk_loader',
  769. 'Unable to save bulk loader template: %template',
  770. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  771. WATCHDOG_ERROR
  772. );
  773. }
  774. }
  775. elseif ($form_state['values']['op'] == 'Cancel') {
  776. $path = explode('?', $form_state['storage']['referring URL']);
  777. parse_str($path[1], $query);
  778. $query['template_id'] = $form_state['storage']['template']->template_id;
  779. //dpm('Redirecting to: '.$path[0].'?'.print_r($query,TRUE).' where the referring URL:'.$form_state['storage']['referring URL']);
  780. drupal_goto($path[0], $query);
  781. }
  782. }
  783. }
  784. /**
  785. * @section
  786. * Add/Edit Field Forms
  787. */
  788. /**
  789. * Add Field Form
  790. *
  791. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  792. * can use the following in the query of the path to set defaults for a given template:
  793. * - template_id=\d+: the template to add the field to
  794. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  795. *
  796. * @param $form_state
  797. * Contains the values and storage for the form
  798. * @return
  799. * A form array to be rendered by drupal_get_form
  800. */
  801. function tripal_bulk_loader_add_template_field_form(&$form_state = NULL) {
  802. $form = array();
  803. $form['#cache'] = TRUE; // Make sure the form is cached.
  804. // get template id from path
  805. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  806. // if there is no template supplied don't return rest of form
  807. if (!$template_id) {
  808. return $form;
  809. }
  810. // Pre-set Variables needed for form proper------------------------------------------
  811. // If this is the first load of the form (no form state) we need to initialize some variables
  812. if (!$form_state['storage']['template']) {
  813. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  814. $template = db_fetch_object(db_query($sql, $template_id));
  815. $form_state['storage']['template_array'] = unserialize($template->template_array);
  816. $form_state['storage']['template'] = $template;
  817. $form_state['storage']['record2priority'] = array();
  818. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  819. if (!is_array($record_array)) {
  820. continue;
  821. }
  822. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  823. }
  824. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  825. }
  826. else {
  827. $template = $form_state['storage']['template'];
  828. }
  829. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : 'table field';
  830. // Tables and default table
  831. $tables = tripal_core_get_chado_tables(TRUE);
  832. if ($form_state['values']) {
  833. if (!preg_match('/^' . current($tables) . '$/', $form_state['values']['chado_table'])) {
  834. $table = $form_state['values']['chado_table'];
  835. }
  836. elseif ($form_state['values']['record_name']) {
  837. $record_name = $form_state['values']['record_name'];
  838. $priority = $form_state['storage']['record2priority'][$record_name];
  839. $table = $form_state['storage']['template_array'][$priority]['table'];
  840. }
  841. else {
  842. $priority = $form_state['values']['field_group'];
  843. $table = $form_state['storage']['template_array'][$priority]['table'];
  844. }
  845. }
  846. if (!$table) {
  847. $table = reset($tables);
  848. }
  849. // get the record_id from the path
  850. if ($_GET['record_id'] !== NULL) {
  851. $form_state['values']['field_group'] = $_GET['record_id'];
  852. if (preg_match('/\d+/', $_GET['record_id'])) {
  853. $priority = $form_state['values']['field_group'];
  854. $table = $form_state['storage']['template_array'][$priority]['table'];
  855. }
  856. }
  857. // Fields and foreign key mappings
  858. $chado_fields = array();
  859. $fk_options = array();
  860. $fk_options['NULL'] = 'None';
  861. $table_description = tripal_core_get_chado_table_schema($table);
  862. //dpm($table_description, 'table description for |'.$table.'|');
  863. if ($field_type == 'foreign key') {
  864. $foreign_field2table = array();
  865. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  866. foreach ($key_array['columns'] as $left_field => $right_field) {
  867. $chado_fields[$left_field] = $left_field;
  868. $foreign_field2table[$left_field] = $key_table;
  869. }
  870. }
  871. reset($chado_fields);
  872. // set default field
  873. if (empty($chado_fields)) {
  874. $field = NULL;
  875. }
  876. elseif ($chado_fields[$form_state['values']['chado_field']]) {
  877. $field = $form_state['values']['chado_field'];
  878. }
  879. else {
  880. $field = current($chado_fields);
  881. }
  882. // Foreign key options
  883. $foreign_table = $foreign_field2table[$field];
  884. if ($foreign_table) {
  885. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  886. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority]['table'])) {
  887. $fk_options[$record_name] = $record_name;
  888. }
  889. }
  890. }
  891. }
  892. else {
  893. foreach ($table_description['fields'] as $field_name => $field_array) {
  894. $chado_fields[$field_name] = $field_name;
  895. }
  896. }
  897. $variables = array(
  898. 'form_state' => $form_state,
  899. 'tables' => $tables,
  900. 'default table' => $table,
  901. 'fields' => $chado_fields,
  902. 'default_field' => $field,
  903. 'priority' => $priority,
  904. 'record_name' => $record_name,
  905. 'table description' => $table_description,
  906. 'foreign key options' => $fk_options,
  907. 'foreign field=>table' => $foreign_field2table,
  908. 'foreign table' => $foreign_table,
  909. );
  910. //dpm($variables, 'variables');
  911. // Start of Form Proper--------------------------------------------------------------
  912. //dpm($form_state, 'Form State');
  913. $form['template_name'] = array(
  914. '#type' => 'item',
  915. '#title' => 'Template',
  916. '#value' => $template->name,
  917. );
  918. $form['template_id'] = array(
  919. '#type' => 'hidden',
  920. '#value' => $template_id,
  921. );
  922. $form['add_fields'] = array(
  923. '#type' => 'fieldset',
  924. '#prefix' => '<div id="tripal_bulk_loader_template-add_field">',
  925. '#suffix' => '</div>',
  926. );
  927. $form['add_fields']['field_type'] = array(
  928. '#type' => 'radios',
  929. '#title' => t('Type of Field'),
  930. '#options' => array(
  931. 'table field' => t('Data: A Field which maps to a column in the supplied file.'),
  932. 'constant' => t('Constant: Field which remains Constant throughout the file'),
  933. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  934. ),
  935. '#required' => TRUE,
  936. '#default_value' => $field_type,
  937. '#ahah' => array(
  938. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  939. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  940. 'effect' => 'fade'
  941. ),
  942. );
  943. // check template array for records then add one more
  944. if (!$form_state['storage']['record2priority']) {
  945. $groups = array();
  946. }
  947. else {
  948. $groups = array_flip($form_state['storage']['record2priority']);
  949. }
  950. $groups['NONE'] = 'Select a Record';
  951. $groups['NEW'] = 'New Record';
  952. $form['add_fields']['field_group'] = array(
  953. '#type' => 'select',
  954. '#title' => 'Record',
  955. '#description' => 'This is used to group a set of fields together allowing '
  956. .'multiple records to be inserted into the same table per line of the file',
  957. '#options' => $groups,
  958. '#default_value' => (preg_match('/\w+.*/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : 'NONE',
  959. '#ahah' => array(
  960. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  961. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  962. 'effect' => 'fade'
  963. ),
  964. '#required' => TRUE,
  965. );
  966. $form['add_fields']['record_name'] = array(
  967. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  968. '#title' => 'Unique Record Name',
  969. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  970. '#suffix' => '</div>',
  971. '#default_value' => $form_state['values']['record_name'],
  972. );
  973. $form['add_fields']['field_title'] = array(
  974. '#type' => 'textfield',
  975. '#title' => t('Human-readable Title for Field'),
  976. '#default_value' => $form_state['values']['field_title'],
  977. );
  978. // loading file data column
  979. $form['add_fields']['columns'] = array(
  980. '#type' => 'fieldset',
  981. '#title' => t('Data File Column'),
  982. '#collapsible' => TRUE,
  983. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  984. );
  985. /**
  986. $form['add_fields']['columns']['sheet_name'] = array(
  987. '#type' => 'textfield',
  988. '#title' => t('Worksheet'),
  989. '#description' => t('Specify the name of the worksheet.'),
  990. '#size' => 5,
  991. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : 'Sheet1',
  992. );
  993. */
  994. $form['add_fields']['columns']['column_number'] = array(
  995. '#type' => 'textfield',
  996. '#title' => t('Column'),
  997. '#description' => t('Specify the column in the data that this field maps to where the first column is 1.'),
  998. '#size' => 5,
  999. '#default_value' => $form_state['values']['column_number'],
  1000. );
  1001. $form['add_fields']['columns']['column_exposed'] = array(
  1002. '#type' => 'checkbox',
  1003. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1004. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1005. '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
  1006. );
  1007. $form['add_fields']['columns']['column_exposed_desc'] = array(
  1008. '#type' => 'textfield',
  1009. '#title' => t('Description for exposed field on bulk loading job'),
  1010. '#description' => t('This description should tell the user what column should be entered here.'),
  1011. '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
  1012. );
  1013. // Global Value
  1014. $form['add_fields']['constant'] = array(
  1015. '#type' => 'fieldset',
  1016. '#title' => t('Constant'),
  1017. '#collapsible' => TRUE,
  1018. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1019. );
  1020. $form['add_fields']['constant']['constant_value'] = array(
  1021. '#type' => 'textfield',
  1022. '#title' => t('Constant Value'),
  1023. '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
  1024. '#default_value' => $form_state['values']['constant_value']
  1025. );
  1026. $form['add_fields']['constant']['constant_exposed'] = array(
  1027. '#type' => 'checkbox',
  1028. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1029. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.')
  1030. );
  1031. $form['add_fields']['constant']['constant_validate'] = array(
  1032. '#type' => 'checkbox',
  1033. '#title' => t('Ensure value is in table'),
  1034. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1035. );
  1036. // Foreign Key
  1037. $form['add_fields']['foreign_key'] = array(
  1038. '#type' => 'fieldset',
  1039. '#title' => 'Foreign Key',
  1040. '#collapsible' => TRUE,
  1041. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1042. );
  1043. $form['add_fields']['foreign_key']['foreign_record'] = array(
  1044. '#type' => 'select',
  1045. '#title' => 'Record to refer to',
  1046. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  1047. '#options' => $fk_options,
  1048. );
  1049. // Chado Field
  1050. $form['add_fields']['chado'] = array(
  1051. '#type' => 'fieldset',
  1052. '#title' => t('Chado Field/Column Details'),
  1053. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1054. );
  1055. $form['add_fields']['chado']['chado_table'] = array(
  1056. '#type' => 'select',
  1057. '#title' => t('Chado Table'),
  1058. '#options' => $tables,
  1059. '#default_value' => $table,
  1060. '#ahah' => array(
  1061. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1062. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1063. 'effect' => 'fade'
  1064. ),
  1065. );
  1066. $form['add_fields']['chado']['chado_field'] = array(
  1067. '#type' => 'select',
  1068. '#title' => t('Chado Field/Column'),
  1069. '#options' => $chado_fields,
  1070. '#default_value' => $form_state['values']['chado_field'],
  1071. '#ahah' => array(
  1072. 'path' => 'admin/tripal/tripal_bulk_loader_template/add_field_ahah',
  1073. 'wrapper' => 'tripal_bulk_loader_template-add_field',
  1074. 'effect' => 'fade'
  1075. ),
  1076. );
  1077. $form['add_fields']['additional'] = array(
  1078. '#type' => 'fieldset',
  1079. '#title' => 'Additional Options',
  1080. );
  1081. $form['add_fields']['additional']['required'] = array(
  1082. '#type' => 'checkbox',
  1083. '#title' => 'Make this file required',
  1084. );
  1085. $form['add_fields']['additional']['regex_transform'] = array(
  1086. '#type' => 'fieldset',
  1087. '#title' => 'Transform Data File Value Rules',
  1088. '#collapsible' => TRUE,
  1089. '#collapsed' => (!$form_state['storage']['regex']['pattern']) ? TRUE : FALSE,
  1090. );
  1091. $form['add_fields']['additional']['regex_transform']['regex_description'] = array(
  1092. '#type' => 'item',
  1093. '#value' => 'A transformation rule allows you to transform the original value '
  1094. .'(usually from a user submitted data file) into the form you would like it stored '
  1095. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1096. .'which determines which replacement patterns are applied and captures regions of the '
  1097. .'original value) and a replacement pattern (a string which may contain capture references '
  1098. .'that describes what the new value should be). Each rule is applied to the result of the '
  1099. .'previous rule.'
  1100. );
  1101. $form['add_fields']['additional']['regex_transform']['regex-data'] = array(
  1102. '#tree' => TRUE,
  1103. );
  1104. if (!$form_state['storage']['regex']['pattern']) {
  1105. $form_state['storage']['regex']['pattern'] = array();
  1106. }
  1107. foreach ($form_state['storage']['regex']['pattern'] as $index => $pattern) {
  1108. $data_element = array(
  1109. 'pattern' => array(
  1110. '#type' => 'item',
  1111. '#value' => $pattern,
  1112. ),
  1113. 'replace' => array(
  1114. '#type' => 'item',
  1115. '#value' => $form_state['storage']['regex']['replace'][$index],
  1116. ),
  1117. 'old_index' => array(
  1118. '#type' => 'hidden',
  1119. '#value' => $index,
  1120. ),
  1121. 'new_index' => array(
  1122. '#type' => 'select',
  1123. '#options' => range(0, sizeof($form_state['storage']['regex']['pattern'])-1),
  1124. '#default_value' => $index,
  1125. ),
  1126. 'id' => array(
  1127. '#type' => 'hidden',
  1128. '#value' => $index,
  1129. ),
  1130. 'submit-delete' => array(
  1131. '#type' => 'submit',
  1132. '#value' => 'Delete Transformation',
  1133. '#name' => $index,
  1134. ),
  1135. );
  1136. $form['add_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1137. }
  1138. $form['add_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1139. '#type' => ($form_state['storage']['regex']['pattern']) ? 'submit' : 'hidden',
  1140. '#value' => 'Save Transformation Rule Order'
  1141. );
  1142. $form['add_fields']['additional']['regex_transform']['new_regex'] = array(
  1143. '#type' => 'fieldset',
  1144. '#title' => 'Add a new Transformation Rule',
  1145. );
  1146. $form['add_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1147. '#type' => 'textfield',
  1148. '#title' => 'Match Pattern',
  1149. '#description' => 'You can use standard php regular expressions in this field to specify a '
  1150. .'pattern. Only if this pattern matches the value in the data file does the replacement '
  1151. .'pattern get applied to the value. To capture a section of your value for use in the '
  1152. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1153. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1154. .' To match and capture any value use <i>.*</i>',
  1155. );
  1156. $form['add_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1157. '#type' => 'textfield',
  1158. '#title' => 'Replacement Pattern',
  1159. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  1160. .'mentioned above. It can include references of the form \n where n is the number of the '
  1161. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1162. .'first set of round brackets.',
  1163. );
  1164. if ($field_type == 'table field') {
  1165. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1166. $form['add_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1167. .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
  1168. .'This allows you to substitute other data file values into the current field. For example, '
  1169. .'if you had the following line:<br />'
  1170. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1171. .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
  1172. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1173. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1174. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1175. }
  1176. $form['add_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1177. '#type' => 'submit',
  1178. '#value' => 'Add Transformation',
  1179. );
  1180. $form['add_fields']['additional']['regex_transform']['test_regex'] = array(
  1181. '#type' => 'fieldset',
  1182. '#title' => 'Test Transformation Rules',
  1183. );
  1184. $form['add_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1185. '#type' => 'textfield',
  1186. '#title' => 'Test Value',
  1187. '#description' => 'This should be a value that you expect the above transformation rules '
  1188. .'to be applied to.',
  1189. '#default_value' => $form_state['storage']['test_regex_test'],
  1190. );
  1191. $form['add_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1192. '#type' => 'textfield',
  1193. '#title' => 'Test Result',
  1194. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1195. .'riles were applied to the Test Value.',
  1196. '#default_value' => $form_state['storage']['test_regex_result'],
  1197. );
  1198. $form['add_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1199. '#type' => 'submit',
  1200. '#value' => 'Test Transformation Rules'
  1201. );
  1202. $form['add_fields']['submit-add_field'] = array(
  1203. '#type' => 'submit',
  1204. '#value' => 'Save Changes'
  1205. );
  1206. $form['add_fields']['submit-cancel'] = array(
  1207. '#type' => 'submit',
  1208. '#value' => 'Cancel'
  1209. );
  1210. return $form;
  1211. }
  1212. function tripal_bulk_loader_add_template_field_form_validate($form, $form_state) {
  1213. // Don't worry about validation when Cancel button is clicked
  1214. if ($form_state['clicked_button']['#value'] == 'Save Changes') {
  1215. $is_unique = tripal_bulk_loader_is_record_name_unique(
  1216. $form_state['values']['record_name'],
  1217. $form_state['values']['template_id'],
  1218. $form_state['storage']['template_array']
  1219. );
  1220. $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
  1221. if ((!$is_unique) AND $new_record) {
  1222. form_set_error('record_name', "New Record Name must be unique. '".$form_state['values']['record_name']."' is not unique.");
  1223. }
  1224. }
  1225. }
  1226. /**
  1227. * Add Field Submit
  1228. *
  1229. * @param $form
  1230. * The form that was submitted
  1231. * @param $form_state
  1232. * The values and storage for the form
  1233. */
  1234. function tripal_bulk_loader_add_template_field_form_submit($form, &$form_state) {
  1235. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1236. if (!$form_state['ahah_submission']) {
  1237. if ($op == 'Save Changes') {
  1238. $template = $form_state['storage']['template_array'];
  1239. // If new record
  1240. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  1241. $record_name = $form_state['values']['record_name'];
  1242. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1243. $record2priority[$record_name] = $priority;
  1244. $template[$priority]['table'] = $form_state['values']['chado_table'];
  1245. $template[$priority]['record_id'] = $record_name;
  1246. }
  1247. else {
  1248. $priority = $form_state['values']['field_group'];
  1249. $record_name = $record2priority[$priority];
  1250. }
  1251. // Add field to template array
  1252. if ($form_state['values']['field_type'] == 'table field') {
  1253. $field = array(
  1254. 'type' => 'table field',
  1255. 'title' => $form_state['values']['field_title'],
  1256. 'field' => $form_state['values']['chado_field'],
  1257. 'required' => $form_state['values']['required'],
  1258. //'allowed values' => empty by default,
  1259. 'spreadsheet column' => $form_state['values']['column_number'],
  1260. 'exposed' => $form_state['values']['column_exposed'],
  1261. 'exposed_description' => $form_state['values']['column_exposed_desc'],
  1262. );
  1263. }
  1264. elseif ($form_state['values']['field_type'] == 'constant') {
  1265. $field = array(
  1266. 'type' => 'constant',
  1267. 'title' => $form_state['values']['field_title'],
  1268. 'field' => $form_state['values']['chado_field'],
  1269. 'required' => $form_state['values']['required'],
  1270. //'allowed values' => empty by default,
  1271. 'constant value' => $form_state['values']['constant_value'],
  1272. 'exposed' => $form_state['values']['constant_exposed'],
  1273. 'exposed_validate' => $form_state['values']['constant_validate'],
  1274. );
  1275. }
  1276. elseif ($form_state['values']['field_type'] == 'foreign key') {
  1277. $field = array(
  1278. 'type' => 'foreign key',
  1279. 'title' => $form_state['values']['field_title'],
  1280. 'field' => $form_state['values']['chado_field'],
  1281. 'foreign key' => $form_state['values']['foreign_record'],
  1282. 'required' => $form_state['values']['required'],
  1283. );
  1284. }
  1285. // Deal with any additional options
  1286. if ($form_state['storage']['regex']) {
  1287. $field['regex'] = $form_state['storage']['regex'];
  1288. }
  1289. // Save Template
  1290. $template[$priority]['fields'][] = $field;
  1291. $form_state['storage']['template']->template_array = serialize($template);
  1292. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1293. if ($success) {
  1294. drupal_set_message(t('Successfully Added Field to Template'));
  1295. drupal_set_message(t('Template Saved.'));
  1296. $path = explode('?', $form_state['storage']['referring URL']);
  1297. parse_str($path[1], $query);
  1298. $query['template_id'] = $form_state['storage']['template']->template_id;
  1299. drupal_goto($path[0], $query);
  1300. }
  1301. else {
  1302. drupal_set_message(t('Unable to Save Template!'), 'error');
  1303. watchdog('T_bulk_loader',
  1304. 'Unable to save bulk loader template: %template',
  1305. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1306. WATCHDOG_ERROR
  1307. );
  1308. }
  1309. }
  1310. elseif ($op == 'Cancel') {
  1311. $path = explode('?', $form_state['storage']['referring URL']);
  1312. parse_str($path[1], $query);
  1313. $query['template_id'] = $form_state['storage']['template']->template_id;
  1314. drupal_goto($path[0], $query);
  1315. }
  1316. elseif ($op == 'Add Transformation') {
  1317. // Add transformation rule to original field
  1318. $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1319. $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
  1320. drupal_set_message(t('Successfully Added Transformation Rule'));
  1321. }
  1322. elseif ($op == 'Save Transformation Rule Order') {
  1323. // Generate new regex array
  1324. $new_regex = array();
  1325. $old_regex = $form_state['storage']['regex'];
  1326. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1327. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1328. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1329. }
  1330. // sort new regex arrays
  1331. asort($new_regex['pattern']);
  1332. asort($new_regex['replace']);
  1333. $form_state['storage']['regex'] = $new_regex;
  1334. }
  1335. elseif ($op == 'Delete Transformation') {
  1336. // Unset regex rule
  1337. $index = $form_state['clicked_button']['#name'];
  1338. unset($form_state['storage']['regex']['pattern'][$index]);
  1339. unset($form_state['storage']['regex']['replace'][$index]);
  1340. }
  1341. elseif ($op == 'Test Transformation Rules') {
  1342. $patterns = $form_state['storage']['regex']['pattern'];
  1343. $replaces = $form_state['storage']['regex']['replace'];
  1344. $test_string = $form_state['values']['test_string'];
  1345. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1346. $form_state['storage']['test_regex_test'] = $test_string;
  1347. }
  1348. }
  1349. }
  1350. /**
  1351. * Edit Field Form
  1352. *
  1353. * This form is meant to be called from a bulk loader form. The following should be set
  1354. * in the query section of the path:
  1355. * - template_id=\d+: the template which the edited field is part of
  1356. * - record_id=\d+: the priority or key in the template array of the record the field
  1357. * is currently part of
  1358. * - field_index=\d+: the key of the field in the fields array of the previously
  1359. * specified record
  1360. *
  1361. * @param $form_state
  1362. * Contains the values and storage for the form
  1363. * @return
  1364. * A form array to be rendered by drupal_get_form
  1365. */
  1366. function tripal_bulk_loader_edit_template_field_form(&$form_state = NULL) {
  1367. $form = array();
  1368. $form['#cache'] = TRUE; // Make sure the form is cached.
  1369. // get template id from path
  1370. $template_id = ($_GET['template_id']) ? $_GET['template_id'] : $form_state['values']['template_id'];
  1371. // if there is no template supplied don't return rest of form
  1372. if (!$template_id) {
  1373. return $form;
  1374. }
  1375. // Pre-set Variables needed for form proper------------------------------------------
  1376. // If this is the first load of the form (no form state) we need to initialize some variables
  1377. if (!$form_state['storage']['template']) {
  1378. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";
  1379. $template = db_fetch_object(db_query($sql, $template_id));
  1380. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1381. $form_state['storage']['template'] = $template;
  1382. $form_state['storage']['record2priority'] = array();
  1383. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1384. if (!is_array($record_array)) {
  1385. continue;
  1386. }
  1387. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1388. }
  1389. $form_state['storage']['referring URL'] = $_SERVER["HTTP_REFERER"];
  1390. }
  1391. else {
  1392. $template = $form_state['storage']['template'];
  1393. }
  1394. // get the field from the path
  1395. if (!($_GET['record_id']===NULL || $_GET['field_index']===NULL)) {
  1396. $priority = $_GET['record_id'];
  1397. $field_index = $_GET['field_index'];
  1398. $template_field = $form_state['storage']['template_array'][$priority]['fields'][$field_index];
  1399. $form_state['storage']['original_field'] = $template_field;
  1400. $form_state['storage']['original_field']['priority'] = $priority;
  1401. $form_state['storage']['original_field']['field_index'] = $field_index;
  1402. }
  1403. $field_type = ($form_state['values']['field_type'])? $form_state['values']['field_type'] : $template_field['type'];
  1404. // Tables and default table
  1405. $tables = tripal_core_get_chado_tables(TRUE);
  1406. if ($form_state['values']) {
  1407. $table = $form_state['values']['chado_table'];
  1408. }
  1409. else {
  1410. $table = $form_state['storage']['template_array'][$priority]['table'];
  1411. }
  1412. // Fields and foreign key mappings
  1413. $chado_fields = array();
  1414. $fk_options = array();
  1415. $fk_options['NULL'] = 'None';
  1416. $table_description = tripal_core_get_chado_table_schema($table);
  1417. if ($field_type == 'foreign key') {
  1418. $foreign_field2table = array();
  1419. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1420. foreach ($key_array['columns'] as $left_field => $right_field) {
  1421. $chado_fields[$left_field] = $left_field;
  1422. $foreign_field2table[$left_field] = $key_table;
  1423. }
  1424. }
  1425. reset($chado_fields);
  1426. // set default field
  1427. if (empty($chado_fields)) {
  1428. $field = NULL;
  1429. }
  1430. elseif ($chado_fields[$form_state['values']['chado_field']]) {
  1431. $field = $form_state['values']['chado_field'];
  1432. }
  1433. elseif ($template_field['field']) {
  1434. $field = $template_field['field'];
  1435. }
  1436. else {
  1437. $field = current($chado_fields);
  1438. }
  1439. //dpm($field, 'field');
  1440. // Foreign key options
  1441. $foreign_table = $foreign_field2table[$field];
  1442. if ($foreign_table) {
  1443. foreach ($form_state['storage']['record2priority'] as $record_name_ => $priority_ ) {
  1444. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority_]['table'])) {
  1445. $fk_options[$record_name_] = $record_name_;
  1446. }
  1447. }
  1448. }
  1449. }
  1450. else {
  1451. foreach ($table_description['fields'] as $field_name => $field_array) {
  1452. $chado_fields[$field_name] = $field_name;
  1453. }
  1454. }
  1455. // dpm(array( 'tables' => $tables, 'default table' => $table, 'record name' => $record_name, 'priority' => $priority,
  1456. // 'fields' => $chad_fields, 'default field' => $field, 'foreign field=>table' => $foreign_field2table,
  1457. // 'table desc' => $table_description, 'foreign record options' => $fk_options, 'foreign table' => $foreign_table
  1458. // ), 'Variables');
  1459. // Start of Form Proper--------------------------------------------------------------
  1460. $form['template_name'] = array(
  1461. '#type' => 'item',
  1462. '#title' => 'Template',
  1463. '#value' => $template->name,
  1464. );
  1465. $form['template_id'] = array(
  1466. '#type' => 'hidden',
  1467. '#value' => $template_id,
  1468. );
  1469. $form['edit_fields'] = array(
  1470. '#type' => 'fieldset',
  1471. '#prefix' => '<div id="tripal_bulk_loader_template-edit_field">',
  1472. '#suffix' => '</div>',
  1473. );
  1474. $form['edit_fields']['field_type'] = array(
  1475. '#type' => 'radios',
  1476. '#title' => t('Type of Field'),
  1477. '#options' => array(
  1478. 'table field' => t('Data Field: Fields which maps to a data file column'),
  1479. 'constant' => t('Constant: Field which remains Constant throughout the data file'),
  1480. 'foreign key' => t('Foreign Key: Fields which map to a record in another table'),
  1481. ),
  1482. '#required' => TRUE,
  1483. '#default_value' => $field_type,
  1484. '#ahah' => array(
  1485. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1486. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1487. 'effect' => 'fade'
  1488. ),
  1489. );
  1490. // check template array for records then edit one more
  1491. if (!$form_state['storage']['record2priority']) {
  1492. $groups = array();
  1493. }
  1494. else {
  1495. $groups = array_flip($form_state['storage']['record2priority']);
  1496. }
  1497. $groups['NONE'] = 'Select a Record';
  1498. $groups['NEW'] = 'New Record';
  1499. $form['edit_fields']['field_group'] = array(
  1500. '#type' => 'select',
  1501. '#title' => 'Record',
  1502. '#description' => 'This is used to group a set of fields together allowing '
  1503. .'multiple records to be inserted into the same table per line of the data file',
  1504. '#options' => $groups,
  1505. '#default_value' => (preg_match('/(\d+|\w+)/', $form_state['values']['field_group'])) ? $form_state['values']['field_group'] : $priority,
  1506. '#ahah' => array(
  1507. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1508. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1509. 'effect' => 'fade'
  1510. ),
  1511. '#required' => TRUE,
  1512. );
  1513. $form['edit_fields']['record_name'] = array(
  1514. '#type' => (preg_match('/NEW/', $form_state['values']['field_group'])) ? 'textfield' : 'hidden',
  1515. '#title' => 'Unique Record Name',
  1516. '#prefix' => '<div id="tripal_bulk_loader_template-edit_record">',
  1517. '#suffix' => '</div>',
  1518. '#default_value' => $form_state['values']['record_name'],
  1519. );
  1520. $form['edit_fields']['field_title'] = array(
  1521. '#type' => 'textfield',
  1522. '#title' => t('Human-readable Title for Field'),
  1523. '#default_value' => ($form_state['values']['field_title']) ? $form_state['values']['field_title'] : $template_field['title'],
  1524. );
  1525. // data file column
  1526. $form['edit_fields']['columns'] = array(
  1527. '#type' => 'fieldset',
  1528. '#title' => t('Data File Column'),
  1529. '#collapsible' => TRUE,
  1530. '#collapsed' => ($field_type == 'table field')? FALSE : TRUE,
  1531. );
  1532. /**
  1533. $form['edit_fields']['columns']['sheet_name'] = array(
  1534. '#type' => 'textfield',
  1535. '#title' => t('Worksheet'),
  1536. '#description' => t('Specify the name of the worksheet.'),
  1537. '#size' => 5,
  1538. '#default_value' => ($form_state['values']['sheet_name'])? $form_state['values']['sheet_name'] : $template_field['spreadsheet sheet'],
  1539. );
  1540. */
  1541. $form['edit_fields']['columns']['column_number'] = array(
  1542. '#type' => 'textfield',
  1543. '#title' => t('Column'),
  1544. '#description' => t('Specify the column in the data file that this field maps to where the first column is 1.'),
  1545. '#size' => 5,
  1546. '#default_value' => ($form_state['values']['column_number']) ? $form_state['values']['column_number'] : $template_field['spreadsheet column'],
  1547. );
  1548. $form['edit_fields']['columns']['column_exposed'] = array(
  1549. '#type' => 'checkbox',
  1550. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1551. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1552. '#default_value' => ($form_state['values']['column_exposed']) ? $form_state['values']['column_exposed'] : $template_field['exposed'],
  1553. );
  1554. $form['edit_fields']['columns']['column_exposed_desc'] = array(
  1555. '#type' => 'textfield',
  1556. '#title' => t('Description for exposed field on bulk loading job'),
  1557. '#description' => t('This description should tell the user what column should be entered here.'),
  1558. '#default_value' => ($form_state['values']['column_exposed_desc']) ? $form_state['values']['column_exposed_desc'] : $template_field['exposed_description'],
  1559. );
  1560. // Global Value
  1561. $form['edit_fields']['constant'] = array(
  1562. '#type' => 'fieldset',
  1563. '#title' => t('Constant'),
  1564. '#collapsible' => TRUE,
  1565. '#collapsed' => ($field_type == 'constant')? FALSE : TRUE,
  1566. );
  1567. $form['edit_fields']['constant']['constant_value'] = array(
  1568. '#type' => 'textfield',
  1569. '#title' => t('Constant Value'),
  1570. '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
  1571. '#default_value' => ($form_state['values']['constant_value']) ? $form_state['values']['constant_value'] : $template_field['constant value'],
  1572. );
  1573. $form['edit_fields']['constant']['constant_exposed'] = array(
  1574. '#type' => 'checkbox',
  1575. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1576. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'),
  1577. '#default_value' => ($form_state['values']['constant_exposed']) ? $form_state['values']['constant_exposed'] : $template_field['exposed'],
  1578. );
  1579. $form['edit_fields']['constant']['constant_validate'] = array(
  1580. '#type' => 'checkbox',
  1581. '#title' => t('Ensure value is in table'),
  1582. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1583. '#default_value' => ($form_state['values']['constant_validate']) ? $form_state['values']['constant_validate'] : $template_field['exposed_validate'],
  1584. );
  1585. // Foreign Key
  1586. $form['edit_fields']['foreign_key'] = array(
  1587. '#type' => 'fieldset',
  1588. '#title' => 'Foreign Key',
  1589. '#collapsible' => TRUE,
  1590. '#collapsed' => ($field_type == 'foreign key')? FALSE : TRUE,
  1591. );
  1592. $form['edit_fields']['foreign_key']['foreign_record'] = array(
  1593. '#type' => 'select',
  1594. '#title' => 'Record to refer to',
  1595. '#descripion' => 'Select the record that this foreign key shouold refer to. The record needs to already exist and be of the correct table.',
  1596. '#options' => $fk_options,
  1597. '#default_value' => ($form_state['values']['foreign_record']) ? $form_state['values']['foreign_record'] : $template_field['foreign key'],
  1598. );
  1599. // Chado Field
  1600. $form['edit_fields']['chado'] = array(
  1601. '#type' => 'fieldset',
  1602. '#title' => t('Chado Field/Column Details'),
  1603. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1604. );
  1605. $form['edit_fields']['chado']['chado_table'] = array(
  1606. '#type' => 'select',
  1607. '#title' => t('Chado Table'),
  1608. '#options' => $tables,
  1609. '#default_value' => $table,
  1610. '#ahah' => array(
  1611. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1612. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1613. 'effect' => 'fade'
  1614. ),
  1615. );
  1616. $form['edit_fields']['chado']['chado_field'] = array(
  1617. '#type' => 'select',
  1618. '#title' => t('Chado Field/Column'),
  1619. '#options' => $chado_fields,
  1620. '#default_value' => ($form_state['values']['chado_field']) ? $form_state['values']['chado_field'] : $template_field['field'],
  1621. '#ahah' => array(
  1622. 'path' => 'admin/tripal/tripal_bulk_loader_template/edit_field_ahah',
  1623. 'wrapper' => 'tripal_bulk_loader_template-edit_field',
  1624. 'effect' => 'fade'
  1625. ),
  1626. );
  1627. $form['edit_fields']['additional'] = array(
  1628. '#type' => 'fieldset',
  1629. '#title' => 'Additional Options',
  1630. );
  1631. $form['edit_fields']['additional']['required'] = array(
  1632. '#type' => 'checkbox',
  1633. '#title' => 'Make this file required',
  1634. '#default_value' => $template_field['required'],
  1635. );
  1636. $form['edit_fields']['additional']['regex_transform'] = array(
  1637. '#type' => 'fieldset',
  1638. '#title' => 'Transform Data File Value Rules',
  1639. '#collapsible' => TRUE,
  1640. '#collapsed' => (!$template_field['regex']['pattern']) ? TRUE : FALSE,
  1641. );
  1642. $transformation_msg = '<p>A transformation rule allows you to transform the original value '
  1643. .'(usually from a user submitted data file) into the form you would like it stored '
  1644. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1645. .'which determines which replacement patterns are applied and captures regions of the '
  1646. .'original value) and a replacement pattern (a string which may contain capture references '
  1647. .'that describes what the new value should be). Each rule is applied to the result of the '
  1648. .'previous rule.<p>';
  1649. $form['edit_fields']['additional']['regex_transform']['regex_description'] = array(
  1650. '#type' => 'item',
  1651. '#value' => $transformation_msg,
  1652. );
  1653. $form['edit_fields']['additional']['regex_transform']['regex-data'] = array(
  1654. '#tree' => TRUE,
  1655. );
  1656. foreach ($template_field['regex']['pattern'] as $index => $pattern) {
  1657. $data_element = array(
  1658. 'pattern' => array(
  1659. '#type' => 'item',
  1660. '#value' => $pattern,
  1661. ),
  1662. 'replace' => array(
  1663. '#type' => 'item',
  1664. '#value' => $template_field['regex']['replace'][$index],
  1665. ),
  1666. 'old_index' => array(
  1667. '#type' => 'hidden',
  1668. '#value' => $index,
  1669. ),
  1670. 'new_index' => array(
  1671. '#type' => 'select',
  1672. '#options' => range(0, sizeof($template_field['regex']['pattern'])-1),
  1673. '#default_value' => $index,
  1674. ),
  1675. 'id' => array(
  1676. '#type' => 'hidden',
  1677. '#value' => $index,
  1678. ),
  1679. 'submit-delete' => array(
  1680. '#type' => 'submit',
  1681. '#value' => 'Delete Transformation',
  1682. '#name' => $index,
  1683. ),
  1684. );
  1685. $form['edit_fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1686. }
  1687. $form['edit_fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1688. '#type' => 'submit',
  1689. '#value' => 'Save Transformation Rule Order'
  1690. );
  1691. $form['edit_fields']['additional']['regex_transform']['new_regex'] = array(
  1692. '#type' => 'fieldset',
  1693. '#title' => 'Add a new Transformation Rule',
  1694. );
  1695. $form['edit_fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1696. '#type' => 'textfield',
  1697. '#title' => 'Match Pattern',
  1698. '#description' => 'You can use standard <b>php regular expressions</b> in this field to specify a '
  1699. .'pattern. Only if this pattern matches the value in the data file does the replacement '
  1700. .'pattern get applied to the value. To capture a section of your value for use in the '
  1701. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1702. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1703. .' To match and capture any value use <i>.*</i>',
  1704. );
  1705. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1706. '#type' => 'textfield',
  1707. '#title' => 'Replacement Pattern',
  1708. '#description' => '<p>This pattern should contain the text you want to replace the match pattern '
  1709. .'mentioned above. It can include references of the form <b>\n</b> where n is the number of the '
  1710. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1711. .'first set of round brackets.</p>',
  1712. );
  1713. if ($field_type == 'table field') {
  1714. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1715. $form['edit_fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1716. .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
  1717. .'This allows you to substitute other data file values into the current field. For example, '
  1718. .'if you had the following line:<br />'
  1719. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1720. .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
  1721. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1722. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1723. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1724. }
  1725. $form['edit_fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1726. '#type' => 'submit',
  1727. '#value' => 'Add Transformation',
  1728. );
  1729. $form['edit_fields']['additional']['regex_transform']['test_regex'] = array(
  1730. '#type' => 'fieldset',
  1731. '#title' => 'Test Transformation Rules',
  1732. );
  1733. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1734. '#type' => 'textfield',
  1735. '#title' => 'Test Value',
  1736. '#description' => 'This should be a value that you expect the above transformation rules '
  1737. .'to be applied to.',
  1738. '#default_value' => $form_state['storage']['test_regex_test'],
  1739. );
  1740. $form['edit_fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1741. '#type' => 'textfield',
  1742. '#title' => 'Test Result',
  1743. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1744. .'riles were applied to the Test Value.',
  1745. '#default_value' => $form_state['storage']['test_regex_result'],
  1746. );
  1747. $form['edit_fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1748. '#type' => 'submit',
  1749. '#value' => 'Test Transformation Rules'
  1750. );
  1751. $form['edit_fields']['submit-edit_field'] = array(
  1752. '#type' => 'submit',
  1753. '#value' => 'Save Changes'
  1754. );
  1755. $form['edit_fields']['submit-cancel'] = array(
  1756. '#type' => 'submit',
  1757. '#value' => 'Cancel'
  1758. );
  1759. return $form;
  1760. }
  1761. function tripal_bulk_loader_edit_template_field_form_validate($form, $form_state) {
  1762. // Don't worry about validation when Cancel button is clicked
  1763. if ($form_state['clicked_button']['#value'] == 'Save Changes') {
  1764. $is_unique = tripal_bulk_loader_is_record_name_unique(
  1765. $form_state['values']['record_name'],
  1766. $form_state['values']['template_id'],
  1767. $form_state['storage']['template_array']
  1768. );
  1769. $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
  1770. if ((!$is_unique) AND $new_record) {
  1771. form_set_error('record_name', "New Record Name must be unique. '".$form_state['values']['record_name']."' is not unique.");
  1772. }
  1773. }
  1774. }
  1775. /**
  1776. * Edit Field Form Submit
  1777. *
  1778. * @param $form
  1779. * The form that was submitted
  1780. * @param $form_state
  1781. * The values and storage for the form
  1782. */
  1783. function tripal_bulk_loader_edit_template_field_form_submit($form, &$form_state) {
  1784. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1785. //dpm($op, 'Operation Submitted');
  1786. //Clear Test
  1787. $form_state['storage']['test_regex_result'] = NULL;
  1788. $form_state['storage']['test_regex_test'] = NULL;
  1789. if (!$form_state['ahah_submission']) {
  1790. if ($op == 'Save Changes') {
  1791. // If new record
  1792. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  1793. // add new record
  1794. $record_name = $form_state['values']['record_name'];
  1795. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1796. $old_priority = $form_state['storage']['original_field']['priority'];
  1797. $field_index = $form_state['storage']['original_field']['field_index'];
  1798. $form_state['storage']['record2priority'][$record_name] = $priority;
  1799. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1800. $form_state['storage']['template_array'][$priority]['record_id'] = $record_name;
  1801. }
  1802. else {
  1803. $priority = $form_state['values']['field_group'];
  1804. $old_priority = $form_state['storage']['original_field']['priority'];
  1805. $field_index = $form_state['storage']['original_field']['field_index'];
  1806. $record_name = $form_state['storage']['record2priority'][$priority];
  1807. }
  1808. $field = $form_state['storage']['original_field'];
  1809. if ($form_state['values']['field_type'] == 'table field') {
  1810. $field['type'] = 'table field';
  1811. $field['title'] = $form_state['values']['field_title'];
  1812. $field['field'] = $form_state['values']['chado_field'];
  1813. $field['required'] = $form_state['values']['required'];
  1814. //$field['allowed values'] = empty by default;
  1815. $field['spreadsheet column'] = $form_state['values']['column_number'];
  1816. $field['exposed'] = $form_state['values']['column_exposed'];
  1817. $field['exposed_description'] = $form_state['values']['column_exposed_desc'];
  1818. }
  1819. elseif ($form_state['values']['field_type'] == 'constant') {
  1820. $field['type'] = 'constant';
  1821. $field['title'] = $form_state['values']['field_title'];
  1822. $field['field'] = $form_state['values']['chado_field'];
  1823. $field['required'] = $form_state['values']['required'];
  1824. //$field['allowed values'] = empty by default;
  1825. $field['constant value'] = $form_state['values']['constant_value'];
  1826. $field['exposed_validate'] = $form_state['values']['constant_validate'];
  1827. $field['exposed'] = $form_state['values']['constant_exposed'];
  1828. }
  1829. elseif ($form_state['values']['field_type'] == 'foreign key') {
  1830. $field['type'] = 'foreign key';
  1831. $field['title'] = $form_state['values']['field_title'];
  1832. $field['field'] = $form_state['values']['chado_field'];
  1833. $field['foreign key'] = $form_state['values']['foreign_record'];
  1834. $field['required'] = $form_state['values']['required'];
  1835. }
  1836. // Deal with any additional options
  1837. // if the record has changed...
  1838. $form_state['storage']['template_array'][$priority]['table'] = $form_state['values']['chado_table'];
  1839. if ($old_priority != $priority) {
  1840. $form_state['storage']['template_array'][$priority]['fields'][] = $field;
  1841. unset($form_state['storage']['template_array'][$old_priority]['fields'][$field_index]);
  1842. // if there are no fields left delete the old record
  1843. if (!$form_state['storage']['template_array'][$old_priority]['fields']) {
  1844. unset($form_state['storage']['template_array'][$old_priority]);
  1845. }
  1846. }
  1847. else {
  1848. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $field;
  1849. }
  1850. // Save Template
  1851. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1852. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1853. if ($success) {
  1854. drupal_set_message(t('Successfully Updated Field'));
  1855. drupal_set_message(t('Template Saved.'));
  1856. $path = explode('?', $form_state['storage']['referring URL']);
  1857. parse_str($path[1], $query);
  1858. $query['template_id'] = $form_state['storage']['template']->template_id;
  1859. drupal_goto($path[0], $query);
  1860. }
  1861. else {
  1862. drupal_set_message(t('Unable to Save Template!'), 'error');
  1863. watchdog('T_bulk_loader',
  1864. 'Unable to save bulk loader template: %template',
  1865. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1866. WATCHDOG_ERROR
  1867. );
  1868. }
  1869. }
  1870. elseif ($op == 'Cancel') {
  1871. $path = explode('?', $form_state['storage']['referring URL']);
  1872. parse_str($path[1], $query);
  1873. $query['template_id'] = $form_state['storage']['template']->template_id;
  1874. drupal_goto($path[0], $query);
  1875. }
  1876. elseif ($op == 'Add Transformation') {
  1877. // Add transformation rule to original field
  1878. $form_state['storage']['original_field']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1879. $form_state['storage']['original_field']['regex']['replace'][] = $form_state['values']['replace'];
  1880. // Add original field back into template
  1881. $priority = $form_state['storage']['original_field']['priority'];
  1882. $field_index = $form_state['storage']['original_field']['field_index'];
  1883. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1884. // Save Template
  1885. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1886. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1887. if ($success) {
  1888. drupal_set_message(t('Successfully Added Transformation Rule'));
  1889. drupal_set_message(t('Template Saved.'));
  1890. }
  1891. else {
  1892. drupal_set_message(t('Unable to Save Template!'), 'error');
  1893. watchdog('T_bulk_loader',
  1894. 'Unable to save bulk loader template: %template',
  1895. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1896. WATCHDOG_ERROR
  1897. );
  1898. }
  1899. }
  1900. elseif ($op == 'Save Transformation Rule Order') {
  1901. // Generate new regex array
  1902. $new_regex = array();
  1903. $old_regex = $form_state['storage']['original_field']['regex'];
  1904. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1905. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1906. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1907. }
  1908. // sort new regex arrays
  1909. asort($new_regex['pattern']);
  1910. asort($new_regex['replace']);
  1911. // Add back to original field
  1912. $form_state['storage']['original_field']['regex'] = $new_regex;
  1913. $priority = $form_state['storage']['original_field']['priority'];
  1914. $field_index = $form_state['storage']['original_field']['field_index'];
  1915. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1916. // Save Template
  1917. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1918. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1919. if ($success) {
  1920. drupal_set_message(t('Successfully Reordered Transformation Rules'));
  1921. drupal_set_message(t('Template Saved.'));
  1922. }
  1923. else {
  1924. drupal_set_message(t('Unable to Save Template!'), 'error');
  1925. watchdog('T_bulk_loader',
  1926. 'Unable to save bulk loader template: %template',
  1927. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1928. WATCHDOG_ERROR
  1929. );
  1930. }
  1931. }
  1932. elseif ($op == 'Delete Transformation') {
  1933. // Unset regex rule
  1934. $index = $form_state['clicked_button']['#name'];
  1935. unset($form_state['storage']['original_field']['regex']['pattern'][$index]);
  1936. unset($form_state['storage']['original_field']['regex']['replace'][$index]);
  1937. $priority = $form_state['storage']['original_field']['priority'];
  1938. $field_index = $form_state['storage']['original_field']['field_index'];
  1939. $form_state['storage']['template_array'][$priority]['fields'][$field_index] = $form_state['storage']['original_field'];
  1940. // Save Template
  1941. $form_state['storage']['template']->template_array = serialize($form_state['storage']['template_array']);
  1942. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1943. if ($success) {
  1944. drupal_set_message(t('Successfully Reordered Transformation Rules'));
  1945. drupal_set_message(t('Template Saved.'));
  1946. }
  1947. else {
  1948. drupal_set_message(t('Unable to Save Template!'), 'error');
  1949. watchdog('T_bulk_loader',
  1950. 'Unable to save bulk loader template: %template',
  1951. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1952. WATCHDOG_ERROR
  1953. );
  1954. }
  1955. }
  1956. elseif ($op == 'Test Transformation Rules') {
  1957. $patterns = $form_state['storage']['original_field']['regex']['pattern'];
  1958. $replaces = $form_state['storage']['original_field']['regex']['replace'];
  1959. $test_string = $form_state['values']['test_string'];
  1960. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1961. $form_state['storage']['test_regex_test'] = $test_string;
  1962. }
  1963. }
  1964. }
  1965. /**
  1966. * @section
  1967. * Helper Functions
  1968. */
  1969. /**
  1970. * Meant to be called from a form_validate function to ensure a newly added bulk loader record
  1971. * name is unique and not empty.
  1972. *
  1973. * @param $new_record_name
  1974. * The record name to check for uniqueness
  1975. * @param $template_id
  1976. * The template_id of the template to add the record to
  1977. * @param $template_array
  1978. * The array describing the template. Optional -will be loaded using template_id if not provided
  1979. *
  1980. * @return
  1981. * TRUE if the record name is not empty and not already in the template_array; FALSE otherwise
  1982. */
  1983. function tripal_bulk_loader_is_record_name_unique($new_record_name, $template_id, $template_array = NULL) {
  1984. // get the template array if it's not supplied
  1985. if (empty($template_array)) {
  1986. $template = db_fetch_object(db_query("SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d",$template_id));
  1987. $template_array = unserialize($template->template_array);
  1988. if (!is_array($template_array)) {
  1989. watchdog(
  1990. 'tripal_bulk_loader',
  1991. 'Unable to retrieve template array from database where template_id=%template_id',
  1992. array('%template_id' => $template_id),
  1993. WATCHDOG_WARNING
  1994. );
  1995. return FALSE;
  1996. }
  1997. }
  1998. // Check that the new record name is not empty
  1999. if (empty($new_record_name)) {
  2000. return FALSE;
  2001. }
  2002. // Check the new record name is unique
  2003. foreach ($template_array as $t) {
  2004. if (strcmp($t['record_id'], $new_record_name) == 0) {
  2005. return FALSE;
  2006. }
  2007. }
  2008. return TRUE;
  2009. }
  2010. /**
  2011. * @section
  2012. * AHAH Callbacks
  2013. */
  2014. /**
  2015. * AHAH Function: Replace $form['add_fields'] in tripal_bulk_loader_add_template_field_form
  2016. *
  2017. * @return
  2018. * JSON Data printed to the screen
  2019. */
  2020. function tripal_bulk_loader_add_field_ahah() {
  2021. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  2022. $form_build_id = filter_xss($_POST['form_build_id']);
  2023. $form = form_get_cache($form_build_id, $form_state);
  2024. $args = $form['#parameters'];
  2025. $form_id = array_shift($args);
  2026. $form_state['post'] = $form['#post'] = $_POST;
  2027. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  2028. $form_state['ahah_submission'] = TRUE;
  2029. $form['#programmed'] = $form['#redirect'] = FALSE;
  2030. drupal_process_form($form_id, $form, $form_state);
  2031. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  2032. $form_element = $form['add_fields'];
  2033. // Remove the wrapper so we don't double it up.
  2034. //unset($form_element['#prefix'], $form_element['#suffix']);
  2035. $output = theme('status_messages');
  2036. $output .= drupal_render($form_element);
  2037. // Final rendering callback.
  2038. print drupal_json(array('status' => TRUE, 'data' => $output));
  2039. exit();
  2040. }
  2041. /**
  2042. * AHAH Function: Replace $form['edit_fields'] in tripal_bulk_loader_edit_template_field_form
  2043. *
  2044. * @return
  2045. * JSON Data printed to the screen
  2046. */
  2047. function tripal_bulk_loader_edit_field_ahah() {
  2048. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  2049. $form_build_id = filter_xss($_POST['form_build_id']);
  2050. $form = form_get_cache($form_build_id, $form_state);
  2051. $args = $form['#parameters'];
  2052. $form_id = array_shift($args);
  2053. $form_state['post'] = $form['#post'] = $_POST;
  2054. // Enable the submit/validate handlers to determine whether AHAH-submittted.
  2055. $form_state['ahah_submission'] = TRUE;
  2056. $form['#programmed'] = $form['#redirect'] = FALSE;
  2057. drupal_process_form($form_id, $form, $form_state);
  2058. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  2059. $form_element = $form['edit_fields'];
  2060. // Remove the wrapper so we don't double it up.
  2061. unset($form_element['#prefix'], $form_element['#suffix']);
  2062. $output = theme('status_messages');
  2063. $output .= drupal_render($form_element);
  2064. // Final rendering callback.
  2065. print drupal_json(array('status' => TRUE, 'data' => $output));
  2066. exit();
  2067. }