tripal_bulk_loader.admin.templates.inc 97 KB

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