tripal_chado.semweb.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. <?php
  2. /**
  3. * Adds defaults to the chado_semweb table.
  4. */
  5. function tripal_chado_populate_chado_semweb_table() {
  6. // Add in all tables and fields into the chado_semweb table.
  7. $chado_tables = chado_get_table_names(TRUE);
  8. foreach ($chado_tables as $chado_table) {
  9. tripal_add_chado_semweb_table($chado_table);
  10. }
  11. // Now set defaults!
  12. //
  13. // VOCABUARIES:
  14. // Add in vocabularies of terms that will be used for the semantic web
  15. //
  16. tripal_insert_db(array(
  17. 'name' => 'foaf',
  18. 'description' => 'Friend of a Friend. A dictionary of people-related terms that can be used in structured data).',
  19. 'url' => 'http://www.foaf-project.org/',
  20. 'urlprefix' => 'http://xmlns.com/foaf/spec/#',
  21. ));
  22. tripal_insert_cv('foaf','Friend of a Friend');
  23. tripal_insert_db(array(
  24. 'name' => 'schema',
  25. 'description' => 'Schema.org. Schema.org is sponsored by Google, Microsoft, Yahoo and Yandex. The vocabularies are developed by an open community process.',
  26. 'url' => 'https://schema.org/',
  27. 'urlprefix' => 'https://schema.org/',
  28. ));
  29. tripal_insert_cv('schema','Schema.org');
  30. tripal_insert_db(array(
  31. 'name' => 'TAXRANK',
  32. 'description' => 'Taxonomic rank vocabulary. A vocabulary of taxonomic ranks (species, family, phylum, etc).',
  33. 'url' => 'https://github.com/phenoscape/taxrank',
  34. 'urlprefix' => 'http://purl.obolibrary.org/obo/TAXRANK_',
  35. ));
  36. tripal_insert_cv('taxrank','Taxonomic rank vocabulary');
  37. tripal_insert_db(array(
  38. 'name' => 'NCBITaxon',
  39. 'description' => 'NCBI organismal classification. An ontology representation of the NCBI organismal taxonomy.',
  40. 'url' => 'http://www.berkeleybop.org/ontologies/ncbitaxon/',
  41. 'urlprefix' => 'http://purl.obolibrary.org/obo/ncbitaxon#',
  42. ));
  43. tripal_insert_cv('ncbitaxon','NCBI organismal classification');
  44. tripal_insert_db(array(
  45. 'name' => 'SWO',
  46. 'description' => 'Software Ontology. An ontology representation of the NCBI organismal taxonomy.',
  47. 'url' => 'http://theswo.sourceforge.net/',
  48. 'urlprefix' => '',
  49. ));
  50. tripal_insert_cv('swo','Software Ontology');
  51. tripal_insert_db(array(
  52. 'name' => 'IAO',
  53. 'description' => 'The Information Artifact Ontology (IAO) is a new ' .
  54. 'ontology of information entities, originally driven by work by the ' .
  55. 'OBI digital entity and realizable information entity branch.',
  56. 'url' => 'https://github.com/information-artifact-ontology/IAO/',
  57. 'urlprefix' => 'http://purl.obolibrary.org/obo/IAO_',
  58. ));
  59. tripal_insert_cv('IAO','Information Artifact Ontology');
  60. tripal_insert_db(array(
  61. 'name' => 'SBO',
  62. 'description' => 'Systems Biology. Terms commonly used in Systems Biology, and in particular in computational modeling.',
  63. 'url' => 'http://www.ebi.ac.uk/sbo/main/',
  64. 'urlprefix' => 'http://purl.obolibrary.org/obo/SBO_',
  65. ));
  66. tripal_insert_cv('sbo','Systems Biology');
  67. //
  68. // SET TERM DEFAULTS
  69. //
  70. $name = tripal_insert_cvterm(array(
  71. 'id' => 'schema:name',
  72. 'name' => 'name',
  73. 'cv_name' => 'schema',
  74. 'definition' => 'The name of the item.',
  75. ));
  76. tripal_associate_chado_semweb_term(NULL, 'uniquename', $name);
  77. tripal_associate_chado_semweb_term(NULL, 'name', $name);
  78. $alternate_name = tripal_insert_cvterm(array(
  79. 'id' => 'schema:alternateName',
  80. 'name' => 'alternateName',
  81. 'cv_name' => 'schema',
  82. 'definition' => 'The name of the item.',
  83. ));
  84. $description = tripal_insert_cvterm(array(
  85. 'id' => 'schema:description',
  86. 'name' => 'description',
  87. 'cv_name' => 'schema',
  88. 'definition' => 'A description of the item.',
  89. ));
  90. tripal_associate_chado_semweb_term(NULL, 'description', $description);
  91. $definition = tripal_insert_cvterm(array(
  92. 'id' => 'IAO:0000115',
  93. 'name' => 'definition',
  94. 'cv_name' => 'iao',
  95. 'definition' => 'The official OBI definition, explaining the meaning of ' .
  96. 'a class or property. Shall be Aristotelian, formalized and normalized. ' .
  97. 'Can be augmented with colloquial definitions.',
  98. ));
  99. tripal_associate_chado_semweb_term(NULL, 'definition', $definition);
  100. $comment = tripal_insert_cvterm(array(
  101. 'id' => 'schema:comment',
  102. 'name' => 'comment',
  103. 'cv_name' => 'schema',
  104. 'definition' => 'Comments, typically from users.',
  105. ));
  106. tripal_associate_chado_semweb_term(NULL, 'comment', $comment);
  107. $time_last_modified = tripal_insert_cvterm(array(
  108. 'id' => 'local:timelastmodified',
  109. 'name' => 'time_last_modified',
  110. 'cv_name' => 'local',
  111. 'definition' => 'The time at which a record for an item was first added.',
  112. ));
  113. tripal_associate_chado_semweb_term(NULL, 'timelastmodified', $time_last_modified);
  114. $time_accessioned = tripal_insert_cvterm(array(
  115. 'id' => 'local:timeaccessioned',
  116. 'name' => 'time_accessioned',
  117. 'cv_name' => 'local',
  118. 'definition' => 'The time at which a record for an item was last upated or modified.',
  119. ));
  120. tripal_associate_chado_semweb_term(NULL, 'timeaccessioned', $time_accessioned);
  121. $time_executed = tripal_insert_cvterm(array(
  122. 'id' => 'local:timeexecuted',
  123. 'name' => 'time_executed',
  124. 'cv_name' => 'local',
  125. 'definition' => 'The time at which a task was executed.',
  126. ));
  127. tripal_associate_chado_semweb_term(NULL, 'timeaccessioned', $time_executed);
  128. $dbxref = tripal_insert_cvterm(array(
  129. 'id' => 'SBO:0000554',
  130. 'name' => 'database cross reference',
  131. 'cv_name' => 'sbo',
  132. 'definition' => 'An annotation which directs one to information contained within a database.',
  133. ));
  134. $relationship = tripal_insert_cvterm(array(
  135. 'id' => 'SBO:0000374',
  136. 'name' => 'relationship',
  137. 'cv_name' => 'sbo',
  138. 'definition' => 'connectedness between entities and/or interactions representing their relatedness or influence. [ src_code:NR ]',
  139. ));
  140. //
  141. // ANALYSIS TABLE
  142. //
  143. $term = tripal_insert_cvterm(array(
  144. 'id' => 'SWO:0000001',
  145. 'name' => 'software',
  146. 'cv_name' => 'schema',
  147. 'definition' => 'Computer software, or generally just software, is any ' .
  148. 'set of machine-readable instructions (most often in the form of a ' .
  149. 'computer program) that conform to a given syntax (sometimes ' .
  150. 'referred to as a language) that is interpretable by a given ' .
  151. 'processor and that directs a computer\'s processor to perform ' .
  152. 'specific operations.',
  153. ));
  154. tripal_associate_chado_semweb_term('analysis', 'program', $term);
  155. $term = tripal_insert_cvterm(array(
  156. 'id' => 'IAO:0000129',
  157. 'name' => 'version number',
  158. 'cv_name' => 'IAO',
  159. 'definition' => 'A version number is an ' .
  160. 'information content entity which is a sequence of characters ' .
  161. 'borne by part of each of a class of manufactured products or its ' .
  162. 'packaging and indicates its order within a set of other products ' .
  163. 'having the same name.',
  164. ));
  165. tripal_associate_chado_semweb_term('analysis', 'programversion', $term);
  166. $term = tripal_insert_cvterm(array(
  167. 'id' => 'IAO:0000064',
  168. 'name' => 'algorithm',
  169. 'cv_name' => 'IAO',
  170. 'definition' => 'An algorithm is a set of instructions for performing a paticular calculation.',
  171. ));
  172. tripal_associate_chado_semweb_term('analysis', 'algorithm', $term);
  173. //
  174. // ORGANISM TABLE
  175. //
  176. $term = tripal_insert_cvterm(array(
  177. 'id' => 'TAXRANK:0000005',
  178. 'name' => 'genus',
  179. 'cv_name' => 'taxrank',
  180. ));
  181. tripal_associate_chado_semweb_term('organism', 'genus', $term);
  182. $term = tripal_insert_cvterm(array(
  183. 'id' => 'TAXRANK:0000006',
  184. 'name' => 'species',
  185. 'cv_name' => 'taxrank',
  186. ));
  187. tripal_associate_chado_semweb_term('organism', 'species', $term);
  188. $term = tripal_insert_cvterm(array(
  189. 'id' => 'TAXRANK:0000045',
  190. 'name' => 'infraspecies',
  191. 'cv_name' => 'taxrank',
  192. ));
  193. tripal_associate_chado_semweb_term('organism', 'infraspecific_name', $term);
  194. $term = tripal_insert_cvterm(array(
  195. 'id' => 'local:infraspecific_type',
  196. 'name' => 'infraspecific_type',
  197. 'definition' => 'The connector type for the infraspecific name',
  198. 'cv_name' => 'local',
  199. ));
  200. tripal_associate_chado_semweb_term('organism', 'type_id', $term);
  201. $term = tripal_insert_cvterm(array(
  202. 'id' => 'NCBITaxon:common_name',
  203. 'name' => 'common name',
  204. 'cv_name' => 'ncbitaxon',
  205. ));
  206. tripal_associate_chado_semweb_term('organism', 'common_name', $term);
  207. $term = tripal_insert_cvterm(array(
  208. 'id' => 'local:abbreviation',
  209. 'name' => 'abbreviation',
  210. 'cv_name' => 'local',
  211. ));
  212. tripal_associate_chado_semweb_term('organism', 'abbreviation', $term);
  213. //
  214. // FEATURE TABLE
  215. //
  216. tripal_associate_chado_semweb_term('feature', 'name', $alternate_name);
  217. //
  218. // PUB TABLE
  219. //
  220. tripal_associate_chado_semweb_term('pub', 'uniquename', $comment);
  221. //
  222. // STOCK TABLE
  223. //
  224. tripal_associate_chado_semweb_term('stock', 'name', $alternate_name);
  225. }
  226. /**
  227. * Adds defaults to the chado_semweb table.
  228. */
  229. function tripal_chado_semweb_form($form, &$form_state, $chado_table = NULL) {
  230. if (array_key_exists('values', $form_state)) {
  231. $chado_table = $form_state['values']['chado_table'];
  232. }
  233. $chado_tables = chado_get_table_names(TRUE);
  234. $chado_tables = array_merge(array('Select a Chado Table'), $chado_tables);
  235. // Make sure the table name exists. If not, reset it.
  236. $chado_table = in_array($chado_table, $chado_tables) ? $chado_table: NULL;
  237. $form['chado_table'] = array(
  238. '#type' => 'select',
  239. '#title' => 'Chado Table',
  240. '#description' => t('Select a chado table to set web services terms used for its columns.'),
  241. '#options' => $chado_tables,
  242. '#default_value' => $chado_table,
  243. '#ajax' => array(
  244. 'callback' => "tripal_chado_semweb_form_ajax_callback",
  245. 'wrapper' => "tripal-chado-semweb-form",
  246. 'effect' => 'fade',
  247. 'method' => 'replace'
  248. ),
  249. );
  250. // If the user has selected a chado table, then we need to
  251. // show the columns for setting terms.
  252. if ($chado_table) {
  253. $schema = chado_get_schema($chado_table);
  254. $pk = $schema['primary key'][0];
  255. $cv_default =
  256. db_select('tripal_cv_defaults', 'tc')
  257. ->fields('tc', array('field_name'))
  258. ->condition('table_name', $chado_table)
  259. ->execute()
  260. ->fetchField();
  261. $columns = $schema['fields'];
  262. $headers = array('Field Name', 'Vocabulary', 'Term Name', 'Term Description', 'Action');
  263. $rows = array();
  264. foreach ($columns AS $column => $detail) {
  265. // Do not show column if it's the primary key or default cv
  266. if ($column != $pk && $column != $cv_default) {
  267. $cvterm_id =
  268. db_select('chado_semweb', 'cs')
  269. ->fields('cs', array('cvterm_id'))
  270. ->condition('chado_table', $chado_table)
  271. ->condition('chado_column', $column)
  272. ->execute()
  273. ->fetchField();
  274. $sw_voc = '';
  275. $sw_term = '';
  276. $sw_desc = '';
  277. if($cvterm_id) {
  278. $term = tripal_get_cvterm(array(
  279. 'cvterm_id' => $cvterm_id
  280. ));
  281. $sw_voc = $term->cv_id->name;
  282. $sw_term = $term->name;
  283. $sw_desc = $term->definition;
  284. }
  285. $rows[] = array(
  286. $column,
  287. $sw_voc,
  288. $sw_term,
  289. $sw_desc,
  290. l('Edit', '/admin/tripal/storage/chado/semweb/edit/' . $chado_table . '/' . $column) . ' | ' .
  291. l('Reset', '/admin/tripal/storage/chado/semweb/reset/' . $chado_table . '/' . $column)
  292. );
  293. }
  294. }
  295. $output = theme('table', array(
  296. 'header' => $headers,
  297. 'rows' => $rows,
  298. ));
  299. $form['table'] = array(
  300. '#markup' => $output,
  301. '#title' => 'Table',
  302. '#description' => t('Please provide any filters for limiting
  303. the records. Only those that match the filters specified
  304. below will be published. To publish all records of this
  305. type, leave all filters blank.'),
  306. );
  307. }
  308. $form['#prefix'] = '<div id="tripal-chado-semweb-form">';
  309. $form['#suffix'] = '</div>';
  310. return $form;
  311. }
  312. /**
  313. * Implements hook_form()
  314. * Edit terms used by the semantic web
  315. *
  316. * @param $form
  317. * @param $form_state
  318. * @param $table
  319. * @param $column
  320. * @return $form
  321. */
  322. function tripal_chado_semweb_edit_form($form, &$form_state, $table = NULL, $column = NULL) {
  323. $term_name = array_key_exists('values', $form_state) ? $form_state['values']['term_name'] : '';
  324. $form['chado_table'] = array(
  325. '#markup' => 'Term used for the <strong>' . t($column) . '</strong> column of the chado <strong>' . t($table) . '</strong> table:',
  326. );
  327. $form['table_name'] = array(
  328. '#type' => 'value',
  329. '#value' => $table
  330. );
  331. $form['column'] = array(
  332. '#type' => 'value',
  333. '#value' => $column
  334. );
  335. // If no term has been selected yet then provide the auto complete field.
  336. $form['term_name'] = array(
  337. '#title' => t('Term'),
  338. '#type' => 'textfield',
  339. '#description' => t("The content type must be the name of a term in
  340. a controlled vocabulary and the controlled vocabulary should
  341. already be loaded into Tripal. For example, to create a content
  342. type for storing 'genes', use the 'gene' term from the
  343. Sequence Ontology (SO)."),
  344. '#required' => TRUE,
  345. '#default_value' => $term_name,
  346. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  347. );
  348. $form['select_button'] = array(
  349. '#type' => 'button',
  350. '#value' => t('Lookup Term'),
  351. '#name' => 'select_cvterm',
  352. '#ajax' => array(
  353. 'callback' => "tripal_chado_semweb_form_ajax_callback",
  354. 'wrapper' => "tripal-chado-semweb-edit-form",
  355. 'effect' => 'fade',
  356. 'method' => 'replace'
  357. ),
  358. );
  359. if ($term_name) {
  360. $form['terms_list'] = array(
  361. '#type' => 'fieldset',
  362. '#title' => t('Matching Terms'),
  363. '#description' => t('Please select the term the best matches the
  364. content type you want to create. If the same term exists in
  365. multiple vocabularies you will see more than one option below.')
  366. );
  367. $match = array(
  368. 'name' => $term_name,
  369. );
  370. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  371. $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
  372. $num_terms = 0;
  373. foreach ($terms as $term) {
  374. // Save the user a click by setting the default value as 1 if there's
  375. // only one matching term.
  376. $default = FALSE;
  377. $attrs = array();
  378. if ($num_terms == 0 and count($terms) == 1) {
  379. $default = TRUE;
  380. $attrs = array('checked' => 'checked');
  381. }
  382. $form['terms_list']['term-' . $term->cvterm_id] = array(
  383. '#type' => 'checkbox',
  384. '#title' => $term->name,
  385. '#default_value' => $default,
  386. '#attributes' => $attrs,
  387. '#description' => '<b>Vocabulary:</b> ' . $term->cv_id->name .
  388. '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '. ' .
  389. '<br><b>Definition:</b> ' . $term->definition,
  390. );
  391. $num_terms++;
  392. }
  393. if ($num_terms == 0) {
  394. $form['terms_list']['none'] = array(
  395. '#type' => 'item',
  396. '#markup' => '<i>' . t('There is no term that matches the entered text.') . '</i>'
  397. );
  398. }
  399. // Add in the button for the cases of no terms or too many.
  400. $form['submit_button'] = array(
  401. '#type' => 'submit',
  402. '#value' => t('Use this term'),
  403. '#name' => 'use_cvterm'
  404. );
  405. }
  406. $form['cancel_button'] = array(
  407. '#type' => 'button',
  408. '#value' => t('Cancel'),
  409. '#name' => 'cancel_button',
  410. '#limit_validation_errors' => array()
  411. );
  412. $form['#prefix'] = '<div id = "tripal-chado-semweb-edit-form">';
  413. $form['#suffix'] = '</div>';
  414. return $form;
  415. }
  416. /**
  417. * Implements hook_form_validate()
  418. *
  419. * Validate function for editing the semantic web term
  420. *
  421. * @param unknown $form
  422. * @param unknown $form_state
  423. */
  424. function tripal_chado_semweb_edit_form_validate($form, &$form_state) {
  425. if (array_key_exists('clicked_button', $form_state)) {
  426. if ($form_state['clicked_button']['#name'] =='use_cvterm') {
  427. $cvterm_id = NULL;
  428. // Make sure we have a cvterm selected
  429. $num_selected = 0;
  430. foreach ($form_state['values'] as $key => $value) {
  431. $matches = array();
  432. if (preg_match("/^term-(\d+)$/", $key, $matches) and
  433. $form_state['values']['term-' . $matches[1]]) {
  434. $cvterm_id = $matches[1];
  435. $num_selected++;
  436. }
  437. }
  438. if ($num_selected == 0) {
  439. form_set_error('', 'Please select at least one term.');
  440. }
  441. else if ($num_selected > 1) {
  442. form_set_error('term-' . $cvterm_id, 'Please select only one term from the list below.');
  443. }
  444. else {
  445. $form_state['values']['#selected_cvterm_id'] = $cvterm_id;
  446. }
  447. }
  448. else if ($form_state['clicked_button']['#name'] =='cancel_button') {
  449. $table_name = $form_state['values']['table_name'];
  450. drupal_goto('/admin/tripal/storage/chado/semweb/' . $table_name);
  451. }
  452. }
  453. }
  454. /**
  455. * Implements hook_form_submit()
  456. *
  457. * Submit function for editing the semantic web term
  458. *
  459. * @param unknown $form
  460. * @param unknown $form_state
  461. */
  462. function tripal_chado_semweb_edit_form_submit($form, &$form_state) {
  463. if (array_key_exists('clicked_button', $form_state) && $form_state['clicked_button']['#name'] =='use_cvterm') {
  464. $table_name = $form_state['values']['table_name'];
  465. $column = $form_state['values']['column'];
  466. $cvterm_id = $form_state['values']['#selected_cvterm_id'];
  467. // Check if there is already a record
  468. $record_id =
  469. db_select('chado_semweb', 'cs')
  470. ->fields('cs', array('chado_semweb_id'))
  471. ->condition('chado_table', $table_name)
  472. ->condition('chado_column', $column)
  473. ->execute()
  474. ->fetchField();
  475. // If the record exists, update it
  476. if ($record_id) {
  477. db_update('chado_semweb')
  478. ->fields(array(
  479. 'cvterm_id' => $cvterm_id
  480. ))
  481. ->condition('chado_semweb_id', $record_id)
  482. ->execute();
  483. }
  484. // Otherwise, insert a new record
  485. else {
  486. db_insert('chado_semweb')
  487. ->fields(array(
  488. 'chado_table' => $table_name,
  489. 'chado_column' => $column,
  490. 'cvterm_id' => $cvterm_id
  491. ))
  492. ->execute();
  493. }
  494. drupal_set_message('The term settings have been saved.');
  495. drupal_goto('/admin/tripal/storage/chado/semweb/' . $table_name);
  496. }
  497. }
  498. /**
  499. * Implements hook_form()
  500. * Reset term used by semantic web
  501. *
  502. * @param $form
  503. * @param $form_state
  504. * @param $table
  505. * @param $column
  506. * @return $form
  507. */
  508. function tripal_chado_semweb_reset_form($form, &$form_state, $table = NULL, $column = NULL) {
  509. $term_name = array_key_exists('values', $form_state) ? $form_state['values']['term_name'] : '';
  510. $form['chado_table'] = array(
  511. '#markup' => 'Are you sure you want to remove the use of this term? ',
  512. );
  513. $form['table_name'] = array(
  514. '#type' => 'value',
  515. '#value' => $table
  516. );
  517. $form['column'] = array(
  518. '#type' => 'value',
  519. '#value' => $column
  520. );
  521. $form['submit_button'] = array(
  522. '#type' => 'submit',
  523. '#value' => t('Reset'),
  524. '#name' => 'reset_term'
  525. );
  526. $form['cancel_button'] = array(
  527. '#type' => 'button',
  528. '#value' => t('Cancel'),
  529. '#name' => 'cancel_button',
  530. '#limit_validation_errors' => array()
  531. );
  532. return $form;
  533. }
  534. /**
  535. * Implements hook_form_validate()
  536. *
  537. * Validate function for resetting the semantic web term
  538. *
  539. * @param unknown $form
  540. * @param unknown $form_state
  541. */
  542. function tripal_chado_semweb_reset_form_validate($form, &$form_state) {
  543. if (array_key_exists('clicked_button', $form_state)) {
  544. if ($form_state['clicked_button']['#name'] =='use_cvterm') {
  545. $cvterm_id = NULL;
  546. // Make sure we have a cvterm selected
  547. $num_selected = 0;
  548. foreach ($form_state['values'] as $key => $value) {
  549. $matches = array();
  550. if (preg_match("/^term-(\d+)$/", $key, $matches) and
  551. $form_state['values']['term-' . $matches[1]]) {
  552. $cvterm_id = $matches[1];
  553. $num_selected++;
  554. }
  555. }
  556. if ($num_selected == 0) {
  557. form_set_error('', 'Please select at least one term.');
  558. }
  559. else if ($num_selected > 1) {
  560. form_set_error('term-' . $cvterm_id, 'Please select only one term from the list below.');
  561. }
  562. else {
  563. $form_state['values']['#selected_cvterm_id'] = $cvterm_id;
  564. }
  565. }
  566. else if ($form_state['clicked_button']['#name'] =='cancel_button') {
  567. $table_name = $form_state['values']['table_name'];
  568. drupal_goto('/admin/tripal/storage/chado/semweb/' . $table_name);
  569. }
  570. }
  571. }
  572. /**
  573. * Implements hook_form_submit()
  574. *
  575. * Submit function for editing the semantic web term
  576. *
  577. * @param unknown $form
  578. * @param unknown $form_state
  579. */
  580. function tripal_chado_semweb_reset_form_submit($form, &$form_state) {
  581. if (array_key_exists('clicked_button', $form_state) && $form_state['clicked_button']['#name'] =='reset_term') {
  582. $table_name = $form_state['values']['table_name'];
  583. $column = $form_state['values']['column'];
  584. // Check if there is already a record
  585. $record_id =
  586. db_select('chado_semweb', 'cs')
  587. ->fields('cs', array('chado_semweb_id'))
  588. ->condition('chado_table', $table_name)
  589. ->condition('chado_column', $column)
  590. ->execute()
  591. ->fetchField();
  592. // If the record exists, reset it
  593. if ($record_id) {
  594. db_update('chado_semweb')
  595. ->fields(array(
  596. 'cvterm_id' => NULL
  597. ))
  598. ->condition('chado_semweb_id', $record_id)
  599. ->execute();
  600. }
  601. drupal_set_message('The term settings have been reset.');
  602. drupal_goto('/admin/tripal/storage/chado/semweb/' . $table_name);
  603. }
  604. }
  605. /**
  606. *
  607. */
  608. function tripal_chado_semweb_form_ajax_callback($form, $form_state) {
  609. return $form;
  610. }