tripal_bulk_loader.admin.templates.inc 98 KB

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