tripal_chado.semweb.inc 22 KB

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