tripal_bulk_loader.admin.inc 75 KB

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