tripal_bulk_loader.admin.templates.inc 84 KB

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