tripal_bulk_loader.admin.templates.inc 114 KB

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