tripal_bulk_loader.admin.templates.inc 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  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['mode'] = $mode;
  1130. $v['template_id'] = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  1131. $form_state['storage']['template_id'] = $v['template_id'];
  1132. $template_id = $v['template_id'];
  1133. $v['record_id'] = (isset($form_state['build_info']['args'][2])) ? $form_state['build_info']['args'][2] : FALSE;
  1134. $v['no_record_id'] = ($mode == 'create_record') ? TRUE : FALSE;
  1135. $form_state['storage']['record_id'] = $v['record_id'];
  1136. $record_id = $v['record_id'];
  1137. $priority = $record_id;
  1138. $v['field_index'] = (isset($form_state['build_info']['args'][3])) ? $form_state['build_info']['args'][3] : FALSE;
  1139. $v['no_field_index'] = (isset($form_state['build_info']['args'][3])) ? FALSE : TRUE;
  1140. $form_state['storage']['field_index'] = $v['field_index'];
  1141. $field_index = $v['field_index'];
  1142. if (!isset($form_state['values'])) {
  1143. $form_state['values'] = array();
  1144. }
  1145. // Get the template if not already stored
  1146. if (!isset($form_state['storage']['template'])) {
  1147. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  1148. $template = db_query($sql, array(':template' => $template_id))->fetchObject();
  1149. $form_state['storage']['template_array'] = unserialize($template->template_array);
  1150. if (!$form_state['storage']['template_array']) {
  1151. $form_state['storage']['template_array'] = array();
  1152. }
  1153. $form_state['storage']['template'] = $template;
  1154. $form_state['storage']['record2priority'] = array();
  1155. foreach ($form_state['storage']['template_array'] as $priority => $record_array) {
  1156. if (!is_array($record_array)) {
  1157. continue;
  1158. }
  1159. $form_state['storage']['record2priority'][$record_array['record_id']] = $priority;
  1160. }
  1161. }
  1162. else {
  1163. $template = $form_state['storage']['template'];
  1164. }
  1165. // If we are editing then get the original values
  1166. if ($mode == 'edit' && !$v['no_field_index']) {
  1167. $template_field = $form_state['storage']['template_array'][$v['record_id']]['fields'][$field_index];
  1168. $original_field = $template_field;
  1169. $form_state['storage']['original_field'] = $template_field;
  1170. $form_state['storage']['original_field']['priority'] = $priority;
  1171. $form_state['storage']['original_field']['field_index'] = $field_index;
  1172. }
  1173. $v['template_name'] = (isset($template->name)) ? $template->name : '';
  1174. if (isset($form_state['values']['field_type'])) {
  1175. $v['field_type'] = $form_state['values']['field_type'];
  1176. }
  1177. elseif (isset($original_field['type'])) {
  1178. $v['field_type'] = $original_field['type'];
  1179. }
  1180. else {
  1181. $v['field_type'] = 'table field';
  1182. }
  1183. $field_type = $v['field_type'];
  1184. // Set field type options
  1185. // First set original field values in $form_state['values'] if not already set
  1186. switch ($v['field_type']) {
  1187. case 'table field':
  1188. if (!isset($form_state['values']['column_number'])) {
  1189. $form_state['values']['column_number'] = (isset($original_field['spreadsheet column'])) ? $original_field['spreadsheet column'] : '';
  1190. $form_state['values']['column_exposed'] = (isset($original_field['exposed'])) ? $original_field['exposed'] : FALSE;
  1191. $form_state['values']['column_exposed_desc'] = (isset($original_field['exposed_description'])) ? $original_field['exposed_description'] : FALSE;
  1192. }
  1193. break;
  1194. case 'constant':
  1195. if (!isset($form_state['values']['constant_value'])) {
  1196. $form_state['values']['constant_value'] = (isset($original_field['constant value'])) ? $original_field['constant value'] : '';
  1197. $form_state['values']['constant_exposed'] = (isset($original_field['exposed'])) ? $original_field['exposed'] : FALSE;
  1198. $form_state['values']['constant_validate'] = (isset($original_field['exposed_validate'])) ? $original_field['exposed_validate'] : FALSE;
  1199. }
  1200. break;
  1201. case 'foreign key':
  1202. if (!isset($form_state['values']['foreign_record'])) {
  1203. $form_state['values']['foreign_record'] = (isset($original_field['foreign key'])) ? $original_field['foreign key'] : FALSE;
  1204. $form_state['values']['foreign_field'] = (isset($original_field['foreign field'])) ? $original_field['foreign field'] : FALSE;
  1205. }
  1206. break;
  1207. }
  1208. // Ensure other field type fields are reset/cleared except for those of the current type
  1209. // ie: if the type is changed from spreadsheet column to constant, the spreadsheet column
  1210. // fields should be cleared since they don't apply to a constant
  1211. switch ($v['field_type']) {
  1212. case 'table field':
  1213. $v['column_number'] = (isset($form_state['values']['column_number'])) ? $form_state['values']['column_number'] : '';
  1214. $v['column_exposed'] = (isset($form_state['values']['column_exposed'])) ? $form_state['values']['column_exposed'] : FALSE;
  1215. $v['column_exposed_desc'] = (isset($form_state['values']['column_exposed_desc'])) ? $form_state['values']['column_exposed_desc'] : FALSE;
  1216. $v['constant_value'] = '';
  1217. $v['constant_exposed'] = FALSE;
  1218. $v['constant_validate'] = FALSE;
  1219. break;
  1220. case 'constant':
  1221. $v['column_number'] = '';
  1222. $v['column_exposed'] = FALSE;
  1223. $v['column_exposed_desc'] = FALSE;
  1224. $v['constant_value'] = (isset($form_state['values']['constant_value'])) ? $form_state['values']['constant_value'] : '';
  1225. $v['constant_exposed'] = (isset($form_state['values']['constant_exposed'])) ? $form_state['values']['constant_exposed'] : FALSE;
  1226. $v['constant_validate'] = (isset($form_state['values']['constant_validate'])) ? $form_state['values']['constant_validate'] : FALSE;
  1227. break;
  1228. case 'foreign key':
  1229. $v['column_number'] = '';
  1230. $v['column_exposed'] = FALSE;
  1231. $v['column_exposed_desc'] = FALSE;
  1232. $v['constant_value'] = '';
  1233. $v['constant_exposed'] = FALSE;
  1234. $v['constant_validate'] = FALSE;
  1235. break;
  1236. }
  1237. // Use the record_id as the priority/field_group
  1238. if ($v['no_record_id']) {
  1239. $form_state['values']['field_group'] = 'NEW';
  1240. $v['field_group'] = 'NEW';
  1241. }
  1242. else {
  1243. $form_state['values']['field_group'] = $record_id;
  1244. $v['field_group'] = $record_id;
  1245. if (preg_match('/\d+/', $record_id)) {
  1246. $priority = $form_state['values']['field_group'];
  1247. $table = $form_state['storage']['template_array'][$priority]['table'];
  1248. }
  1249. }
  1250. // Chado Tables and default table
  1251. $tables = tripal_core_get_chado_tables(TRUE);
  1252. if (isset($form_state['values']['chado_table'])) {
  1253. if (!preg_match('/^' . current($tables) . '$/', $form_state['values']['chado_table'])) {
  1254. $table = $form_state['values']['chado_table'];
  1255. }
  1256. elseif (isset($form_state['values']['record_name'])) {
  1257. $record_name = $form_state['values']['record_name'];
  1258. $priority = $form_state['storage']['record2priority'][$record_name];
  1259. $table = $form_state['storage']['template_array'][$priority]['table'];
  1260. }
  1261. else {
  1262. $priority = $form_state['values']['field_group'];
  1263. $table = $form_state['storage']['template_array'][$priority]['table'];
  1264. }
  1265. }
  1266. if (!isset($table)) {
  1267. $table = reset($tables);
  1268. }
  1269. // Chado fields
  1270. $chado_fields = array();
  1271. $table_description = tripal_core_get_chado_table_schema($table);
  1272. foreach ($table_description['fields'] as $field_name => $field_array) {
  1273. $chado_fields[$field_name] = $field_name;
  1274. }
  1275. // Chado Default Field
  1276. if (empty($chado_fields)) {
  1277. $field = NULL;
  1278. }
  1279. elseif (isset($form_state['values']['chado_field'])) {
  1280. if (isset($chado_fields[$form_state['values']['chado_field']])) {
  1281. $field = $form_state['values']['chado_field'];
  1282. }
  1283. else {
  1284. $field = current($chado_fields);
  1285. }
  1286. }
  1287. elseif (isset($original_field['field'])) {
  1288. $field = $original_field['field'];
  1289. }
  1290. else {
  1291. $field = current($chado_fields);
  1292. }
  1293. // Field group options
  1294. if (!$form_state['storage']['record2priority']) {
  1295. $groups = array();
  1296. }
  1297. else {
  1298. $groups = array_flip($form_state['storage']['record2priority']);
  1299. }
  1300. $groups['NONE'] = 'Select a Record';
  1301. $groups['NEW'] = 'New Record';
  1302. // Field Group Default
  1303. if ($record_id) {
  1304. $field_group = $record_id;
  1305. }
  1306. elseif (isset($form_state['values']['field_group'])) {
  1307. $field_group = $form_state['values']['field_group'];
  1308. }
  1309. else {
  1310. if ($mode == 'add') {
  1311. $field_group = 'NEW';
  1312. }
  1313. else {
  1314. $field_group = 'NONE';
  1315. }
  1316. }
  1317. $v['field_group'] = $field_group;
  1318. $v['field_group_options'] = $groups;
  1319. $v['chado_table'] = $table;
  1320. $v['chado_table_options'] = $tables;
  1321. $v['chado_field'] = $field;
  1322. $v['chado_field_options'] = $chado_fields;
  1323. $v['record_name'] = (isset($form_state['values']['record_name'])) ? $form_state['values']['record_name'] : '';
  1324. if (isset($form_state['values']['field_title'])) {
  1325. $v['field_title'] = $form_state['values']['field_title'];
  1326. }
  1327. elseif (isset($original_field['title'])) {
  1328. $v['field_title'] = $original_field['title'];
  1329. }
  1330. else {
  1331. $v['field_title'] = '';
  1332. }
  1333. $v['show_all_records'] = (isset($form_state['values']['show_all_records'])) ? $form_state['values']['show_all_records'] : FALSE;
  1334. // Foreign key options
  1335. $fk_options = array();
  1336. $fk_options['NULL'] = 'None';
  1337. $show_all = $v['show_all_records'];
  1338. if ($field_type == 'foreign key' and !$show_all) {
  1339. $foreign_field2table = array();
  1340. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1341. foreach ($key_array['columns'] as $left_field => $right_field) {
  1342. //$chado_fields[$left_field] = $left_field;
  1343. $foreign_field2table[$left_field] = $key_table;
  1344. }
  1345. }
  1346. // Foreign key options
  1347. $foreign_table = $foreign_field2table[$field];
  1348. if ($foreign_table) {
  1349. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  1350. if (preg_match('/^' . $foreign_table . '$/', $form_state['storage']['template_array'][$priority]['table'])) {
  1351. $fk_options[$record_name] = $record_name;
  1352. }
  1353. }
  1354. }
  1355. }
  1356. // the user wants to see all of the records
  1357. elseif ($field_type == 'foreign key' and $show_all) {
  1358. foreach ($form_state['storage']['record2priority'] as $record_name => $priority ) {
  1359. $fk_options[$record_name] = $record_name;
  1360. }
  1361. }
  1362. // build the list of referrer table fields. This list is used
  1363. // for the referring table field drop down. It has two groups
  1364. // one for foreign keys and another for additional fields.
  1365. $ref_chado_fields = array();
  1366. if ($field_type == 'foreign key') {
  1367. $fk_rec = $form_state['values']['foreign_record'];
  1368. if ($fk_rec and $fk_rec != 'None' and $fk_rec != 'NULL') {
  1369. // first add in the foreign key field in the referring table
  1370. // that corresponds to this field (if one exists).
  1371. $fk_priority = $form_state['storage']['record2priority'][$fk_rec];
  1372. $fk_table = $form_state['storage']['template_array'][$fk_priority]['table'];
  1373. foreach ($table_description['foreign keys'] as $key_table => $key_array) {
  1374. foreach ($key_array['columns'] as $left_field => $right_field) {
  1375. if ($key_table == $fk_table and $left_field == $field) {
  1376. $ref_chado_fields['Foreign Key'][$right_field] = $right_field;
  1377. }
  1378. }
  1379. }
  1380. // now add in the remaining fields of the referring record's table.
  1381. $fk_description = tripal_core_get_chado_table_schema($fk_table);
  1382. foreach ($fk_description['fields'] as $fk_field_name => $fk_farray) {
  1383. // don't include the FK field it's included above
  1384. if (in_array('Foreign Key', $ref_chado_fields)) {
  1385. if (in_array($fk_field_name, $ref_chado_fields['Foreign Key'])) {
  1386. continue;
  1387. }
  1388. }
  1389. $ref_chado_fields['Additional Table Fields'][$fk_field_name] = $fk_field_name;
  1390. }
  1391. }
  1392. }
  1393. $ref_chado_fields = (!empty($ref_chado_fields)) ? $ref_chado_fields : array('NULL', 'None');
  1394. $v['foreign_record_options'] = $fk_options;
  1395. $v['foreign_record'] = (isset($form_state['values']['foreign_record'])) ? $form_state['values']['foreign_record'] : 'NULL';
  1396. $v['foreign_field_options'] = $ref_chado_fields;
  1397. $v['foreign_field'] = (isset($form_state['values']['foreign_field'])) ? $form_state['values']['foreign_field'] : current($ref_chado_fields);
  1398. $v['required'] = (isset($form_state['values']['required'])) ? $form_state['values']['required'] : FALSE;
  1399. $v['regex_are_set'] = (isset($form_state['storage']['regex']['pattern'])) ? TRUE : FALSE;
  1400. $v['regex-pattern'] = (isset($form_state['storage']['regex']['pattern'])) ? $form_state['storage']['regex']['pattern'] : array();
  1401. $v['regex-replace'] = (isset($form_state['storage']['regex']['replace'])) ? $form_state['storage']['regex']['replace'] : array();
  1402. $v['regex-test-string'] = (isset($form_state['storage']['test_regex_test'])) ? $form_state['storage']['test_regex_test'] : '';
  1403. $v['regex-test-result'] = (isset($form_state['storage']['test_regex_result'])) ? $form_state['storage']['test_regex_result'] : '';
  1404. return $v;
  1405. }
  1406. /**
  1407. * Add Field Form
  1408. *
  1409. * This form is meant to be called from a bulk loader form. Blank Defaults are in place but you
  1410. * can use the following in the query of the path to set defaults for a given template:
  1411. * - template_id=\d+: the template to add the field to
  1412. * - record_id=\d+: the priority or key in the template array of the record to add the field to
  1413. *
  1414. * @param $form_state
  1415. * Contains the values and storage for the form
  1416. * @return
  1417. * A form array to be rendered by drupal_get_form
  1418. *
  1419. * @ingroup tripal_bulk_loader
  1420. */
  1421. function tripal_bulk_loader_template_field_form($form, $form_state = NULL) {
  1422. $form['#cache'] = TRUE; // Make sure the form is cached.
  1423. // if there is no template supplied don't return rest of form
  1424. if (!isset($form_state['build_info']['args'][1])) {
  1425. return $form;
  1426. }
  1427. $v['mode'] = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : 'create';
  1428. $form_state['storage']['mode'] = $v['mode'];
  1429. $mode = $v['mode'];
  1430. $values = tripal_bulk_loader_template_field_form_default_values($mode, $form_state);
  1431. $form['template_name'] = array(
  1432. '#type' => 'item',
  1433. '#title' => 'Template',
  1434. '#markup' => $values['template_name'],
  1435. );
  1436. $form['template_id'] = array(
  1437. '#type' => 'hidden',
  1438. '#value' => $values['template_id'],
  1439. );
  1440. $form['fields'] = array(
  1441. '#type' => 'markup',
  1442. '#prefix' => '<div id="tripal_bulk_loader_template-template_fields">',
  1443. '#suffix' => '</div>',
  1444. );
  1445. $form['fields']['field_type'] = array(
  1446. '#type' => 'radios',
  1447. '#title' => t('Type of Field'),
  1448. '#options' => array(
  1449. 'table field' => t('Data: A Field which maps to a column in the supplied file.'),
  1450. 'constant' => t('Constant: Field which remains Constant throughout the file'),
  1451. 'foreign key' => t('Record Referral: Fields which map to a record in another table'),
  1452. ),
  1453. '#required' => TRUE,
  1454. '#default_value' => $values['field_type'],
  1455. '#ajax' => array(
  1456. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1457. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1458. 'effect' => 'fade'
  1459. ),
  1460. );
  1461. $form['fields']['field_group'] = array(
  1462. '#type' => 'select',
  1463. '#title' => 'Record',
  1464. '#description' => 'This is used to group a set of fields together allowing '
  1465. .'multiple records to be inserted into the same table per line of the file',
  1466. '#options' => $values['field_group_options'],
  1467. '#default_value' => $values['field_group'],
  1468. '#ajax' => array(
  1469. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1470. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1471. 'effect' => 'fade'
  1472. ),
  1473. '#required' => TRUE,
  1474. );
  1475. $form['fields']['record_name'] = array(
  1476. '#type' => ($values['no_record_id']) ? 'textfield' : 'hidden',
  1477. '#title' => 'Unique Record Name',
  1478. '#prefix' => '<div id="tripal_bulk_loader_template-add_record">',
  1479. '#suffix' => '</div>',
  1480. '#description' => 'A human-readable name for the record; it should be unique.',
  1481. '#default_value' => $values['record_name'],
  1482. );
  1483. $form['fields']['field_title'] = array(
  1484. '#type' => 'textfield',
  1485. '#title' => t('Human-readable Title for Field'),
  1486. '#default_value' => $values['field_title'],
  1487. );
  1488. // Chado Field
  1489. $form['fields']['chado'] = array(
  1490. '#type' => 'fieldset',
  1491. '#title' => t('Chado Field/Column Details'),
  1492. '#description' => t('Specify the Table/Field in chado that this field maps to.'),
  1493. );
  1494. $form['fields']['chado']['chado_table'] = array(
  1495. '#type' => 'select',
  1496. '#title' => t('Chado Table'),
  1497. '#options' => $values['chado_table_options'],
  1498. '#default_value' => $values['chado_table'],
  1499. '#ajax' => array(
  1500. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1501. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1502. 'effect' => 'fade'
  1503. ),
  1504. );
  1505. $form['fields']['chado']['chado_field'] = array(
  1506. '#type' => 'select',
  1507. '#title' => t('Chado Field/Column'),
  1508. '#options' => $values['chado_field_options'],
  1509. '#default_value' => $values['chado_field'],
  1510. '#ajax' => array(
  1511. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1512. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1513. 'effect' => 'fade'
  1514. ),
  1515. );
  1516. // loading file data column
  1517. $form['fields']['columns'] = array(
  1518. '#type' => 'fieldset',
  1519. '#title' => t('Data File Column'),
  1520. '#collapsible' => TRUE,
  1521. '#collapsed' => ($values['field_type'] == 'table field')? FALSE : TRUE,
  1522. );
  1523. $form['fields']['columns']['column_number'] = array(
  1524. '#type' => 'textfield',
  1525. '#title' => t('Column'),
  1526. '#description' => t('Specify the column in the data that this field maps to where the first column is 1.'),
  1527. '#size' => 5,
  1528. '#default_value' => $values['column_number'],
  1529. );
  1530. $form['fields']['columns']['column_exposed'] = array(
  1531. '#type' => 'checkbox',
  1532. '#title' => t('Allow Column to be set for each Bulk Loading Job'),
  1533. '#description' => t('Adds a textbox field to the Bulk Loader Page to allow users to set this value.'),
  1534. '#default_value' => $values['column_exposed'],
  1535. );
  1536. $form['fields']['columns']['column_exposed_desc'] = array(
  1537. '#type' => 'textfield',
  1538. '#title' => t('Description for exposed field on bulk loading job'),
  1539. '#description' => t('This description should tell the user what column should be entered here.'),
  1540. '#default_value' => $values['column_exposed_desc'],
  1541. );
  1542. // Global Value
  1543. $form['fields']['constant'] = array(
  1544. '#type' => 'fieldset',
  1545. '#title' => t('Constant'),
  1546. '#collapsible' => TRUE,
  1547. '#collapsed' => ($values['field_type'] == 'constant')? FALSE : TRUE,
  1548. );
  1549. $form['fields']['constant']['constant_value'] = array(
  1550. '#type' => 'textfield',
  1551. '#title' => t('Constant Value'),
  1552. '#description' => t('Specify the value you wish this field to have regardless of data file value.'),
  1553. '#default_value' => $values['constant_value']
  1554. );
  1555. $form['fields']['constant']['constant_exposed'] = array(
  1556. '#type' => 'checkbox',
  1557. '#title' => t('Allow Constant to be set for each Bulk Loading Job'),
  1558. '#description' => t('Adds a textbox field to the Create Bulk Loader Form to allow users to set this value.'),
  1559. '#default_value' => $values['constant_exposed'],
  1560. );
  1561. $form['fields']['constant']['constant_validate'] = array(
  1562. '#type' => 'checkbox',
  1563. '#title' => t('Ensure value is in table'),
  1564. '#description' => t('Checks the database when a bulk loading job is created to ensure the value entered already exists in the database.'),
  1565. '#default_value' => $values['constant_validate'],
  1566. );
  1567. // Foreign Key / Referrer
  1568. $form['fields']['foreign_key'] = array(
  1569. '#type' => 'fieldset',
  1570. '#title' => 'Record Referral',
  1571. '#collapsible' => TRUE,
  1572. '#collapsed' => ($values['field_type'] == 'foreign key')? FALSE : TRUE,
  1573. );
  1574. $form['fields']['foreign_key']['show_all_records'] = array(
  1575. '#type' => 'checkbox',
  1576. '#title' => 'Refer to any record',
  1577. '#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'),
  1578. '#default_value' => $values['show_all_records'],
  1579. '#ajax' => array(
  1580. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1581. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1582. 'effect' => 'fade'
  1583. ),
  1584. );
  1585. $form['fields']['foreign_key']['foreign_record'] = array(
  1586. '#type' => 'select',
  1587. '#title' => 'Record to refer to',
  1588. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1589. '#options' => $values['foreign_record_options'],
  1590. '#ajax' => array(
  1591. 'callback' => 'tripal_bulk_loader_template_fields_ahah',
  1592. 'wrapper' => 'tripal_bulk_loader_template-template_fields',
  1593. 'effect' => 'fade'
  1594. ),
  1595. '#default_value' => $values['foreign_record'],
  1596. );
  1597. $form['fields']['foreign_key']['foreign_field'] = array(
  1598. '#type' => 'select',
  1599. '#title' => 'Field to refer to',
  1600. '#descripion' => 'Select the record that this value should refer to. The record needs to already exist.',
  1601. '#options' => $values['foreign_field_options'],
  1602. '#default_value' => $values['foreign_field'],
  1603. );
  1604. $form['fields']['additional'] = array(
  1605. '#type' => 'fieldset',
  1606. '#title' => 'Additional Options',
  1607. '#collapsible' => TRUE,
  1608. '#collapsed' => TRUE
  1609. );
  1610. $form['fields']['additional']['required'] = array(
  1611. '#type' => 'checkbox',
  1612. '#title' => 'Make this field required',
  1613. '#default_value' => $values['required'],
  1614. );
  1615. $form['fields']['additional']['regex_transform'] = array(
  1616. '#type' => 'fieldset',
  1617. '#title' => 'Transform Data File Value Rules',
  1618. '#collapsible' => TRUE,
  1619. '#collapsed' => $values['regex_are_set'],
  1620. );
  1621. $form['fields']['additional']['regex_transform']['regex_description'] = array(
  1622. '#type' => 'item',
  1623. '#markup' => 'A transformation rule allows you to transform the original value '
  1624. .'(usually from a user submitted data file) into the form you would like it stored '
  1625. .'in the chado database. Each rule consists of a match pattern (a php regular expression '
  1626. .'which determines which replacement patterns are applied and captures regions of the '
  1627. .'original value) and a replacement pattern (a string which may contain capture references '
  1628. .'that describes what the new value should be). Each rule is applied to the result of the '
  1629. .'previous rule.'
  1630. );
  1631. $form['fields']['additional']['regex_transform']['regex-data'] = array(
  1632. '#tree' => TRUE,
  1633. );
  1634. foreach ($values['regex-pattern'] as $index => $pattern) {
  1635. $data_element = array(
  1636. 'pattern' => array(
  1637. '#type' => 'item',
  1638. '#markup' => $pattern,
  1639. ),
  1640. 'replace' => array(
  1641. '#type' => 'item',
  1642. '#markup' => $values['regex-replace'][$index],
  1643. ),
  1644. 'old_index' => array(
  1645. '#type' => 'hidden',
  1646. '#value' => $index,
  1647. ),
  1648. 'new_index' => array(
  1649. '#type' => 'select',
  1650. '#options' => range(0, sizeof($values['regex-pattern'])-1),
  1651. '#default_value' => $index,
  1652. ),
  1653. 'id' => array(
  1654. '#type' => 'hidden',
  1655. '#value' => $index,
  1656. ),
  1657. 'submit-delete' => array(
  1658. '#type' => 'submit',
  1659. '#value' => 'Delete Transformation',
  1660. '#name' => $index,
  1661. ),
  1662. );
  1663. $form['fields']['additional']['regex_transform']['regex-data'][$index] = $data_element;
  1664. }
  1665. $form['fields']['additional']['regex_transform']['submit-reorder_regex'] = array(
  1666. '#type' => ($values['regex_are_set']) ? 'submit' : 'hidden',
  1667. '#value' => 'Save Transformation Rule Order'
  1668. );
  1669. $form['fields']['additional']['regex_transform']['new_regex'] = array(
  1670. '#type' => 'fieldset',
  1671. '#title' => 'Add a new Transformation Rule',
  1672. );
  1673. $form['fields']['additional']['regex_transform']['new_regex']['pattern'] = array(
  1674. '#type' => 'textfield',
  1675. '#title' => 'Match Pattern',
  1676. '#description' => 'You can use standard php regular expressions in this field to specify a '
  1677. .'pattern. Only if this pattern matches the value in the data file does the replacement '
  1678. .'pattern get applied to the value. To capture a section of your value for use in the '
  1679. .'replacement patten surround with round brackets. For example, <i>GI:(\d+)</i> will match '
  1680. .' NCBI gi numbers and will capture the numerical digits for use in the replacement pattern. '
  1681. .' To match and capture any value use <i>.*</i>',
  1682. );
  1683. $form['fields']['additional']['regex_transform']['new_regex']['replace'] = array(
  1684. '#type' => 'textfield',
  1685. '#title' => 'Replacement Pattern',
  1686. '#description' => 'This pattern should contain the text you want to replace the match pattern '
  1687. .'mentioned above. It can include references of the form \n where n is the number of the '
  1688. .'capture in the match pattern. For example, \1 will be replaced with the text matched in your '
  1689. .'first set of round brackets.',
  1690. );
  1691. if ($values['field_type'] == 'table field') {
  1692. $tab = '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
  1693. $form['fields']['additional']['regex_transform']['new_regex']['replace']['#description'] .= '<p>'
  1694. .'The following references are also available for data file fields: <b><#column:<i>number</i>#></b>. '
  1695. .'This allows you to substitute other data file values into the current field. For example, '
  1696. .'if you had the following line:<br />'
  1697. . $tab . 'SNP' . $tab . '15-Jan-2011' . $tab . '1' . $tab . '54' . $tab . 'Contig34355'
  1698. .'<br /> and your current field is for column #1 and you\'re inserting into the chado field '
  1699. .'feature.uniquename then you might want to add in the data to ensure your uniquename is '
  1700. .'unique. The Match Pattern is (.*) to select all the first column and the Replacement '
  1701. .'Pattern could be \1_<#column:2#> which would insert SNP_15-Jan-2011 into the database.</p>';
  1702. }
  1703. $form['fields']['additional']['regex_transform']['new_regex']['submit-add_transform'] = array(
  1704. '#type' => 'submit',
  1705. '#value' => 'Add Transformation',
  1706. );
  1707. $form['fields']['additional']['regex_transform']['test_regex'] = array(
  1708. '#type' => 'fieldset',
  1709. '#title' => 'Test Transformation Rules',
  1710. );
  1711. $form['fields']['additional']['regex_transform']['test_regex']['test_string'] = array(
  1712. '#type' => 'textfield',
  1713. '#title' => 'Test Value',
  1714. '#description' => 'This should be a value that you expect the above transformation rules '
  1715. .'to be applied to.',
  1716. '#default_value' => $values['regex-test-string'],
  1717. );
  1718. $form['fields']['additional']['regex_transform']['test_regex']['test_result'] = array(
  1719. '#type' => 'textfield',
  1720. '#title' => 'Test Result',
  1721. '#description' => 'This is the value that would be saved to the database after the above transformation '
  1722. .'riles were applied to the Test Value.',
  1723. '#default_value' => $values['regex-test-result'],
  1724. );
  1725. $form['fields']['additional']['regex_transform']['test_regex']['submit-test'] = array(
  1726. '#type' => 'submit',
  1727. '#value' => 'Test Transformation Rules'
  1728. );
  1729. $form['fields']['submit-save'] = array(
  1730. '#type' => 'submit',
  1731. '#value' => 'Save Changes'
  1732. );
  1733. $form['fields']['submit-cancel'] = array(
  1734. '#type' => 'submit',
  1735. '#value' => 'Cancel'
  1736. );
  1737. return $form;
  1738. }
  1739. /**
  1740. * Implements hook_form_validate().
  1741. *
  1742. * @ingroup tripal_bulk_loader
  1743. */
  1744. function tripal_bulk_loader_template_field_form_validate($form, $form_state) {
  1745. // Don't worry about validation when Cancel button is clicked
  1746. if ($form_state['clicked_button']['#value'] == 'Save Changes') {
  1747. $is_unique = tripal_bulk_loader_is_record_name_unique(
  1748. $form_state['values']['record_name'],
  1749. $form_state['values']['template_id'],
  1750. $form_state['storage']['template_array']
  1751. );
  1752. $new_record = ($form_state['values']['field_group'] == 'NEW') ? TRUE : FALSE;
  1753. if ((!$is_unique) AND $new_record) {
  1754. form_set_error('record_name', "New Record Name must be unique. '" . $form_state['values']['record_name'] . "' is not unique.");
  1755. }
  1756. }
  1757. }
  1758. /**
  1759. * Add Field Submit
  1760. *
  1761. * @param $form
  1762. * The form that was submitted
  1763. * @param $form_state
  1764. * The values and storage for the form
  1765. *
  1766. * @ingroup tripal_bulk_loader
  1767. */
  1768. function tripal_bulk_loader_template_field_form_submit($form, &$form_state) {
  1769. $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
  1770. if ($op == 'Save Changes') {
  1771. $template = $form_state['storage']['template_array'];
  1772. // If new record
  1773. if (preg_match('/NEW/', $form_state['values']['field_group'])) {
  1774. $record_name = $form_state['values']['record_name'];
  1775. $priority = sizeof($form_state['storage']['template_array']) + 1;
  1776. $record2priority[$record_name] = $priority;
  1777. $template[$priority]['table'] = $form_state['values']['chado_table'];
  1778. $template[$priority]['record_id'] = $record_name;
  1779. }
  1780. else {
  1781. $priority = $form_state['values']['field_group'];
  1782. }
  1783. // Add field to template array
  1784. if ($form_state['values']['field_type'] == 'table field') {
  1785. $field = array(
  1786. 'type' => 'table field',
  1787. 'title' => $form_state['values']['field_title'],
  1788. 'field' => $form_state['values']['chado_field'],
  1789. 'required' => $form_state['values']['required'],
  1790. //'allowed values' => empty by default,
  1791. 'spreadsheet column' => $form_state['values']['column_number'],
  1792. 'exposed' => $form_state['values']['column_exposed'],
  1793. 'exposed_description' => $form_state['values']['column_exposed_desc'],
  1794. );
  1795. }
  1796. elseif ($form_state['values']['field_type'] == 'constant') {
  1797. $field = array(
  1798. 'type' => 'constant',
  1799. 'title' => $form_state['values']['field_title'],
  1800. 'field' => $form_state['values']['chado_field'],
  1801. 'required' => $form_state['values']['required'],
  1802. //'allowed values' => empty by default,
  1803. 'constant value' => $form_state['values']['constant_value'],
  1804. 'exposed' => $form_state['values']['constant_exposed'],
  1805. 'exposed_validate' => $form_state['values']['constant_validate'],
  1806. );
  1807. }
  1808. elseif ($form_state['values']['field_type'] == 'foreign key') {
  1809. $field = array(
  1810. 'type' => 'foreign key',
  1811. 'title' => $form_state['values']['field_title'],
  1812. 'field' => $form_state['values']['chado_field'],
  1813. 'show_all_records' => $form_state['values']['show_all_records'],
  1814. 'foreign key' => $form_state['values']['foreign_record'],
  1815. 'foreign field' => $form_state['values']['foreign_field'],
  1816. 'required' => $form_state['values']['required'],
  1817. );
  1818. }
  1819. // Deal with any additional options
  1820. if (isset($form_state['storage']['regex'])) {
  1821. $field['regex'] = $form_state['storage']['regex'];
  1822. }
  1823. // Save Template
  1824. if ($form_state['storage']['mode'] == 'create') {
  1825. $template[$priority]['fields'][] = $field;
  1826. }
  1827. else {
  1828. $template[$priority]['fields'][$form_state['storage']['field_index']] = $field;
  1829. }
  1830. $form_state['storage']['template']->template_array = serialize($template);
  1831. $success = drupal_write_record('tripal_bulk_loader_template', $form_state['storage']['template'], array('template_id'));
  1832. if ($success) {
  1833. drupal_set_message(t('Successfully Added Field to Template'));
  1834. drupal_set_message(t('Template Saved.'));
  1835. $form_state['rebuild'] = FALSE;
  1836. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/edit';
  1837. }
  1838. else {
  1839. drupal_set_message(t('Unable to Save Template!'), 'error');
  1840. watchdog('T_bulk_loader',
  1841. 'Unable to save bulk loader template: %template',
  1842. array('%template' => print_r($form_state['storage']['template'], TRUE)),
  1843. WATCHDOG_ERROR
  1844. );
  1845. }
  1846. }
  1847. elseif ($op == 'Cancel') {
  1848. $form_state['rebuild'] = FALSE;
  1849. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/edit';
  1850. }
  1851. elseif ($op == 'Add Transformation') {
  1852. // Add transformation rule to original field
  1853. $form_state['storage']['regex']['pattern'][] = '/' . $form_state['values']['pattern'] . '/';
  1854. $form_state['storage']['regex']['replace'][] = $form_state['values']['replace'];
  1855. drupal_set_message(t('Successfully Added Transformation Rule'));
  1856. }
  1857. elseif ($op == 'Save Transformation Rule Order') {
  1858. // Generate new regex array
  1859. $new_regex = array();
  1860. $old_regex = $form_state['storage']['regex'];
  1861. foreach ($form_state['values']['regex-data'] as $key => $element) {
  1862. $new_regex['pattern'][ $element['new_index'] ] = $old_regex['pattern'][ $element['old_index'] ];
  1863. $new_regex['replace'][ $element['new_index'] ] = $old_regex['replace'][ $element['old_index'] ];
  1864. }
  1865. // sort new regex arrays
  1866. ksort($new_regex['pattern']);
  1867. ksort($new_regex['replace']);
  1868. $form_state['storage']['regex'] = $new_regex;
  1869. }
  1870. elseif ($op == 'Delete Transformation') {
  1871. // Unset regex rule
  1872. $index = $form_state['clicked_button']['#name'];
  1873. unset($form_state['storage']['regex']['pattern'][$index]);
  1874. unset($form_state['storage']['regex']['replace'][$index]);
  1875. }
  1876. elseif ($op == 'Test Transformation Rules') {
  1877. $patterns = $form_state['storage']['regex']['pattern'];
  1878. $replaces = $form_state['storage']['regex']['replace'];
  1879. $test_string = $form_state['values']['test_string'];
  1880. $form_state['storage']['test_regex_result'] = preg_replace($patterns, $replaces, $test_string);
  1881. $form_state['storage']['test_regex_test'] = $test_string;
  1882. }
  1883. }
  1884. /**
  1885. * Delete Field Form
  1886. *
  1887. * This form is meant to be called from a bulk loader form
  1888. *
  1889. * @param $form_state
  1890. * Contains the values and storage for the form
  1891. * @return
  1892. * A form array to be rendered by drupal_get_form
  1893. *
  1894. * @ingroup tripal_bulk_loader
  1895. */
  1896. function tripal_bulk_loader_delete_template_field_form($form, $form_state) {
  1897. $form['#cache'] = TRUE; // Make sure the form is cached.
  1898. // get args from path
  1899. $template_id = (isset($form_state['build_info']['args'][0])) ? $form_state['build_info']['args'][0] : FALSE;
  1900. $form_state['storage']['template_id'] = $template_id;
  1901. $record_id = (isset($form_state['build_info']['args'][1])) ? $form_state['build_info']['args'][1] : FALSE;
  1902. $form_state['storage']['record_id'] = $record_id;
  1903. $field_id = (isset($form_state['build_info']['args'][2])) ? $form_state['build_info']['args'][2] : FALSE;
  1904. $form_state['storage']['field_id'] = $field_id;
  1905. // if there is no template supplied don't return rest of form
  1906. if (!$template_id) {
  1907. return $form;
  1908. }
  1909. $_GET['record_name'] = (isset($_GET['record_name'])) ? $_GET['record_name'] : '';
  1910. $_GET['field_name'] = (isset($_GET['field_name'])) ? $_GET['field_name'] : '';
  1911. $_GET['chado_table'] = (isset($_GET['chado_table'])) ? $_GET['chado_table'] : '';
  1912. $_GET['chado_field'] = (isset($_GET['chado_field'])) ? $_GET['chado_field'] : '';
  1913. $_GET['data_column'] = (isset($_GET['data_column'])) ? $_GET['data_column'] : '';
  1914. $_GET['constant_value'] = (isset($_GET['constant_value'])) ? $_GET['constant_value'] : '';
  1915. $_GET['foreign_record'] = (isset($_GET['foreign_record'])) ? $_GET['foreign_record'] : '';
  1916. $description = '';
  1917. $description .= '<table>';
  1918. $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>';
  1919. $description .= '<tr><td>' . $_GET['record_name']
  1920. . '</td><td>' . $_GET['field_name']
  1921. . '</td><td>' . $_GET['chado_table']
  1922. . '</td><td>' . $_GET['chado_field']
  1923. . '</td><td>' . $_GET['data_column']
  1924. . '</td><td>' . $_GET['constant_value']
  1925. . '</td><td>' . $_GET['foreign_record']
  1926. . '</td></tr>';
  1927. $description .= '</table>';
  1928. $description .= '<p><strong>Are you sure you want to delete this field?</strong></p>';
  1929. $yes = 'Delete Field';
  1930. $no = 'Cancel';
  1931. $form['#attributes']['class'][] = 'confirmation';
  1932. $form['description'] = array('#markup' => $description);
  1933. $form['actions'] = array('#type' => 'actions');
  1934. $form['actions']['submit'] = array(
  1935. '#type' => 'submit',
  1936. '#value' => $yes ? $yes : t('Confirm'),
  1937. );
  1938. $form['actions']['cancel'] = array(
  1939. '#type' => 'link',
  1940. '#title' => $no ? $no : t('Cancel'),
  1941. '#href' => 'admin/tripal/loaders/bulk/template/'.$template_id.'/edit',
  1942. );
  1943. // By default, render the form using theme_confirm_form().
  1944. if (!isset($form['#theme'])) {
  1945. $form['#theme'] = 'confirm_form';
  1946. }
  1947. return $form;
  1948. }
  1949. /**
  1950. * Delete Field Form
  1951. *
  1952. * This form is meant to be called from a bulk loader form
  1953. *
  1954. * @param $form_state
  1955. * Contains the values and storage for the form
  1956. * @return
  1957. * A form array to be rendered by drupal_get_form
  1958. *
  1959. * @ingroup tripal_bulk_loader
  1960. */
  1961. function tripal_bulk_loader_delete_template_field_form_submit($form, &$form_state) {
  1962. $form_state['rebuild'] = FALSE;
  1963. $form_state['redirect'] = 'admin/tripal/loaders/bulk/template/'.$form_state['storage']['template_id'].'/edit';
  1964. $sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=:template";
  1965. $result = db_query($sql, array(':template' => $form_state['storage']['template_id']))->fetchObject();
  1966. $form_state['storage']['template'] = unserialize($result->template_array);
  1967. $new_template = tripal_bulk_loader_delete_field(
  1968. $form_state['storage']['record_id'],
  1969. $form_state['storage']['field_id'],
  1970. $form_state['storage']['template']
  1971. );
  1972. if (!empty($new_template)) {
  1973. $form_state['storage']['template'] = $new_template;
  1974. }
  1975. drupal_set_message(t('Deleted Field from Template.'));
  1976. $record = array(
  1977. 'template_id' => $form_state['storage']['template_id'],
  1978. 'template_array' => serialize($form_state['storage']['template'])
  1979. );
  1980. drupal_write_record('tripal_bulk_loader_template', $record, array('template_id'));
  1981. drupal_set_message(t('Template Saved.'));
  1982. }
  1983. /**
  1984. * @section
  1985. * AHAH Callbacks
  1986. */
  1987. /**
  1988. * AHAH Function: Replace $form['fields'] in tripal_bulk_loader_add_template_field_form
  1989. *
  1990. * @return
  1991. * JSON Data printed to the screen
  1992. *
  1993. * @ingroup tripal_bulk_loader
  1994. */
  1995. function tripal_bulk_loader_template_fields_ahah($form, $form_state) {
  1996. return $form['fields'];
  1997. }