tripal_entities.api.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. /**
  3. * Retrieves a TripalTerm entity that matches the given arguments.
  4. *
  5. * @param $namespace
  6. * The namespace for the vocabulary
  7. * @param $accession
  8. * The ID (accession) of the term in the vocabulary.
  9. *
  10. * @return
  11. * A TripalTerm entity object or NULL if not found.
  12. */
  13. function tripal_load_term_entity($namespace, $accession) {
  14. $query = db_select('tripal_term', 'tt');
  15. $query->join('tripal_vocab' ,'tv', 'tv.id = tt.vocab_id');
  16. $query->fields('tt', array('id', 'accession'))
  17. ->fields('tv', array('namespace'))
  18. ->condition('tv.namespace', $namespace)
  19. ->condition('tt.accession', $accession);
  20. $term = $query->execute()->fetchObject();
  21. if ($term) {
  22. $entity = entity_load('TripalTerm', array($term->id));
  23. return reset($entity);
  24. }
  25. return NULL;
  26. }
  27. /**
  28. * Retrieves a TripalVocab entity that maches the given arguments.
  29. *
  30. * @param $namespace
  31. *
  32. * @return
  33. * A TripalVocab entity object or NULL if not found.
  34. */
  35. function tripal_load_vocab_entity($namespace) {
  36. $vocab = db_select('tripal_vocab', 'tv')
  37. ->fields('tv')
  38. ->condition('tv.namespace', $namespace)
  39. ->execute()
  40. ->fetchObject();
  41. if ($vocab) {
  42. $entity = entity_load('TripalVocab', array($vocab->id));
  43. return reset($entity);
  44. }
  45. return NULL;
  46. }
  47. /**
  48. * Retrieves a TripalBundle entity that matches the given arguments.
  49. *
  50. * @param $name
  51. * The name the bundle (e.g. bio-data_234).
  52. *
  53. * @return
  54. * A TripalBundle entity object or NULL if not found.
  55. */
  56. function tripal_load_bundle_entity($name) {
  57. $bundle = db_select('tripal_bundle', 'tb')
  58. ->fields('tb')
  59. ->condition('tb.name', $name)
  60. ->execute()
  61. ->fetchObject();
  62. if ($bundle) {
  63. $entity = entity_load('TripalBundle', array($bundle->id));
  64. return reset($entity);
  65. }
  66. return NULL;
  67. }
  68. /**
  69. * Creates a new Tripal Entity type (i.e. bundle).
  70. *
  71. * @param $namespace
  72. * The abbreviated namespace for the vocabulary (e.g. RO, SO, PATO).
  73. * @param $accession
  74. * The unique term ID in the vocabulary $namespace (i.e. an accession).
  75. * @param $term_name
  76. * A human-readable name for this term. This will became the name that
  77. * appears for the content type. In practice, this should be the name
  78. * of the term. (E.g. the name for SO:0000704 is gene).
  79. * @param $error
  80. * A string, passed by reference, that is filled with the error message
  81. * if the function fails.
  82. *
  83. * @return
  84. * TRUE if the entity type (bundle) was succesfully created. FALSE otherwise.
  85. */
  86. function tripal_create_bundle($namespace, $accession, $term_name, &$error = '') {
  87. // First create the TripalVocab if it doesn't already exist.
  88. $vocab = tripal_load_vocab_entity($namespace);
  89. if (!$vocab) {
  90. $vocab = entity_get_controller('TripalVocab')->create(array('namespace' => $namespace));
  91. $vocab->save();
  92. }
  93. // Next create the TripalTerm if it doesn't already exist.
  94. $term = tripal_load_term_entity($namespace, $accession);
  95. if (!$term) {
  96. $args = array('vocab_id' => $vocab->id, 'accession' => $accession, 'name' => $term_name);
  97. $term = entity_get_controller('TripalTerm')->create($args);
  98. $term = $term->save();
  99. }
  100. // If the bundle doesn't already exist, then add it.
  101. $bundle_name = 'bio-data_' . $term->id;
  102. $einfo = entity_get_info('TripalEntity');
  103. if (!in_array($bundle_name, array_keys($einfo['bundles']))) {
  104. // Insert the bundle.
  105. db_insert('tripal_bundle')
  106. ->fields(array(
  107. 'label' => $term_name,
  108. 'type' => 'TripalEntity',
  109. 'name' => $bundle_name,
  110. 'term_id' => $term->id,
  111. ))
  112. ->execute();
  113. }
  114. // Clear the entity cache so that Drupal will read our
  115. // hook_entity_info() implementation.
  116. global $language;
  117. $langcode = $language->language;
  118. cache_clear_all("entity_info:$langcode", 'cache');
  119. variable_set('menu_rebuild_needed', TRUE);
  120. // Get the bundle object.
  121. $bundle = tripal_load_bundle_entity($bundle_name);
  122. // Allow modules to now add fields to the bundle
  123. module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle, $term);
  124. return TRUE;
  125. }
  126. /**
  127. * Allows a module to make changes to an entity object after creation.
  128. *
  129. * This function is added by Tripal to allow datastore backends to add
  130. * addition properties to the entity that they themselves will use later.
  131. *
  132. * @param $entity
  133. * @param $entity_type
  134. */
  135. function hook_entity_create(&$entity, $entity_type) {
  136. }
  137. /**
  138. * Allows a module to add fields to a bundle.
  139. *
  140. * This function is called after the bundle is created and allows any module
  141. * to add fields to it.
  142. *
  143. * @param $entity_type
  144. * The entity type (e.g. TripalEntity).
  145. * @param $bundle
  146. * A TripalBundle object.
  147. * @param $term
  148. * An instance of a TripalTerm object.
  149. *
  150. * @return
  151. * TRUE on success, FALSE on failure.
  152. */
  153. function hook_add_bundle_fields($entity_type, $bundle, $term) {
  154. }
  155. /**
  156. * @section
  157. * Bundle Variables.
  158. */
  159. /**
  160. * Fetch the value for a given tripal variable.
  161. *
  162. * @param string $variable_name
  163. * The name of the variable as in tripal_variables.name.
  164. * @param int $bundle_id
  165. * The unique identfier for the bundle you want the value for.
  166. * @return text
  167. * The value of the specified variable for the specified bundle.
  168. */
  169. function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE) {
  170. $variable = tripal_get_variable($variable_name);
  171. // Warn if we can't find the tripal_variable.
  172. if (!$variable) {
  173. // tripal_report_error(
  174. // 'trpbundle_var',
  175. // TRIPAL_WARNING,
  176. // 'Unable to fetch tripal bundle variable value due to missing tripal variable (%var).',
  177. // array('%var' => $variable_name)
  178. // );
  179. // return FALSE;
  180. return $default;
  181. }
  182. // Select the value for this variable.
  183. $value = db_select('tripal_bundle_variables', 'var')
  184. ->fields('var', array('value'))
  185. ->condition('var.bundle_id', $bundle_id)
  186. ->condition('var.variable_id', $variable->variable_id)
  187. ->execute()
  188. ->fetchField();
  189. // Warn if the value appears to be empty.
  190. if (!$value) {
  191. // tripal_report_error(
  192. // 'trpbundle_var',
  193. // TRIPAL_WARNING,
  194. // 'Unable to fetch tripal bundle variable (%var) value.',
  195. // array('%var' => $variable_name)
  196. // );
  197. return $default;
  198. }
  199. return $value;
  200. }
  201. /**
  202. * Save the value of a tripal variable for a given bundle.
  203. *
  204. * @param string $variable_name
  205. * The name of the variable as in tripal_variables.name.
  206. * @param int $bundle_id
  207. * The unique identfier for the bundle you want the value for.
  208. * @param $text $value
  209. * The value of the variable for the given bundle.
  210. */
  211. function tripal_set_bundle_variable($variable_name, $bundle_id, $value) {
  212. $variable = tripal_get_variable($variable_name);
  213. // And then we need to write the new format to the tripal_bundle_variables table.
  214. $record = array(
  215. 'bundle_id' => $bundle_id,
  216. 'variable_id' => $variable->variable_id,
  217. 'value' => $value,
  218. );
  219. // Check whether there is already a format saved.
  220. $bundle_variable_id = db_select('tripal_bundle_variables', 'var')
  221. ->fields('var', array('bundle_variable_id'))
  222. ->condition('var.bundle_id', $record['bundle_id'])
  223. ->condition('var.variable_id', $record['variable_id'])
  224. ->execute()
  225. ->fetchField();
  226. if ($bundle_variable_id) {
  227. $record['bundle_variable_id'] = $bundle_variable_id;
  228. return drupal_write_record('tripal_bundle_variables', $record, 'bundle_variable_id');
  229. }
  230. else {
  231. return drupal_write_record('tripal_bundle_variables', $record);
  232. }
  233. }
  234. /**
  235. * @section
  236. * Title & URL Formats.
  237. */
  238. /**
  239. * Get Page Title Format for a given Tripal Entity Type.
  240. *
  241. * @param TripalBundle $entity
  242. * The Entity object for the Tripal Bundle the title format is for.
  243. */
  244. function tripal_get_title_format($entity) {
  245. // Get the existing title format if it exists.
  246. $title_format = tripal_get_bundle_variable('title_format', $entity->id);
  247. // If there isn't yet a title format for this bundle/type then we should
  248. // determine the default.
  249. if (!$title_format) {
  250. $title_format = tripal_get_default_title_format($entity);
  251. tripal_save_title_format($entity, $title_format);
  252. }
  253. return $title_format;
  254. }
  255. /**
  256. * Save Page Title Format for a given Tripal Entity Type.
  257. *
  258. * @param TripalBundle $entity
  259. * The Entity object for the Tripal Bundle the title format is for.
  260. * @param string $format
  261. * The pattern to be used when generating entity titles for the above type.
  262. */
  263. function tripal_save_title_format($entity, $format) {
  264. return tripal_set_bundle_variable('title_format', $entity->id, $format);
  265. }
  266. /**
  267. * Determine the default pattern/format to use for an entity type.
  268. *
  269. * @param TripalBundle $entity
  270. * The Entity object for the Tripal Bundle the title format is for.
  271. * @return string
  272. * A default title format.
  273. */
  274. function tripal_get_default_title_format($entity) {
  275. $format = array();
  276. // Retrieve all available tokens.
  277. $tokens = tripal_get_tokens($entity);
  278. foreach($tokens as $token) {
  279. if ($token['required']) {
  280. $format[] = $token['token'];
  281. }
  282. }
  283. return implode(', ', $format);
  284. }
  285. /**
  286. * Returns an array of tokens based on Tripal Entity Fields.
  287. *
  288. * @param TripalBundle $entity
  289. * The bundle entity for which you want tokens.
  290. * @return
  291. * An array of tokens where the key is the machine_name of the token.
  292. */
  293. function tripal_get_tokens($entity) {
  294. $tokens = array();
  295. $fields = field_info_instances('TripalEntity', $entity->name);
  296. foreach ($fields as $f) {
  297. // Build the token from the field information.
  298. $token = '[' . $f['field_name'] . ']';
  299. $tokens[$token] = array(
  300. 'label' => $f['label'],
  301. 'description' => $f['description'],
  302. 'token' => $token,
  303. 'field_name' => $f['field_name'],
  304. 'required' => $f['required']
  305. );
  306. }
  307. return $tokens;
  308. }
  309. /**
  310. * Formats the tokens for display.
  311. *
  312. * @param array $tokens
  313. * A list of tokens generated via tripal_get_tokens().
  314. * @return
  315. * Rendered output describing the available tokens.
  316. */
  317. function theme_token_list($tokens) {
  318. $header = array('Token', 'Name', 'Description');
  319. $rows = array();
  320. foreach ($tokens as $details) {
  321. $rows[] = array(
  322. '[' . $details['field_name'] . ']',
  323. $details['label'],
  324. $details['description'],
  325. );
  326. }
  327. return theme('table', array('header' => $header, 'rows' => $rows));
  328. }
  329. /**
  330. * @section
  331. * Vocabulary Hooks.
  332. */
  333. /**
  334. * A hook for specifying information about the data store for vocabularies.
  335. *
  336. * The storage backend for controlled vocabularies has traditionally been
  337. * the Chado CV term tables. However, Tripal v3.0 introduces APIs for supporting
  338. * other backends. Therefore, this function indicates to Tripal which
  339. * data stores are capable of providing support for terms.
  340. *
  341. * @return
  342. * An array describing the storage backends implemented by the module. The
  343. * keys are storage backend names. To avoid name clashes, storage
  344. * backend names should be prefixed with the name of the module that
  345. * exposes them. The values are arrays describing the storage backend,
  346. * with the following key/value pairs:
  347. *
  348. * label: The human-readable name of the storage backend.
  349. * module: The name of the module providing the support for this backend.
  350. * description: A short description for the storage backend.
  351. * settings: An array whose keys are the names of the settings available for
  352. * the storage backend, and whose values are the default values for
  353. * those settings.
  354. */
  355. function hook_vocab_storage_info() {
  356. return array(
  357. 'term_chado_storage' => array(
  358. 'label' => t('Chado storage'),
  359. 'description' => t('Integrates terms stored in the local Chado database with Tripal entities.'),
  360. 'settings' => array(),
  361. ),
  362. );
  363. }
  364. /**
  365. * Creates a form for specifying a term for TripalEntity creation.
  366. *
  367. * This hook allows the module that implements a vocabulary storage backend
  368. * to provide the form necessary to select a term that will then be used for
  369. * creating a new TripalEntity type. Tripal will expect that a 'namespace' and
  370. * 'accession' are in the $form_state['storage'] array. The 'namespace' and
  371. * must be the abbreviated uppercase namespace for the vocabulary (e.g. 'RO',
  372. * 'SO', 'PATO', etc.). The 'accession' must be the unique term ID (or
  373. * accession) for the term in the vocabulary.
  374. *
  375. * @param $form
  376. * @param $form_state
  377. *
  378. * @return
  379. * A form object.
  380. */
  381. function hook_vocab_select_term_form(&$form, &$form_state) {
  382. return $form;
  383. }
  384. /**
  385. * Validates the hook_vocab_select_term_form().
  386. *
  387. * @param $name
  388. */
  389. function hook_vocab_select_term_form_validate($form, &$form_state) {
  390. }
  391. /**
  392. * Retreives information about a term from the default term storage backend.
  393. *
  394. * This hook is called by the tripal_entity module to retrieve information
  395. * about the term from the storage backend. It must return an array with
  396. * a set of keys.
  397. *
  398. * @param $namespace
  399. * The namespace of the vocabulary in which the term is found.
  400. * @param $accession
  401. * The unique identifier (accession) for this term.
  402. *
  403. * @return
  404. * An array with at least the following keys:
  405. * namespace : The namespace of the vocabulary.
  406. * accession : The name unique ID of the term.
  407. * name : The name of the term.
  408. * definition : The term's description.
  409. * any other keys may be added as desired. Returns NULL if the term
  410. * cannot be found.
  411. */
  412. function hook_vocab_get_term($namespace, $accession) {
  413. }