tripal_entities.api.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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, $options = array()) {
  294. $tokens = array();
  295. // Set default options.
  296. $options['required only'] = (isset($options['required only'])) ? $options['required only'] : FALSE;
  297. $options['include id'] = (isset($options['include id'])) ? $options['include id'] : TRUE;
  298. if ($options['include id']) {
  299. $token = '[TripalBundle__bundle_id]';
  300. $tokens[$token] = array(
  301. 'label' => 'Bundle ID',
  302. 'description' => 'The unique identifier for this Tripal Content Type.',
  303. 'token' => $token,
  304. 'field_name' => NULL,
  305. 'required' => TRUE
  306. );
  307. $token = '[TripalEntity__entity_id]';
  308. $tokens[$token] = array(
  309. 'label' => 'Content/Entity ID',
  310. 'description' => 'The unique identifier for an individual piece of Tripal Content.',
  311. 'token' => $token,
  312. 'field_name' => NULL,
  313. 'required' => TRUE
  314. );
  315. }
  316. $fields = field_info_instances('TripalEntity', $entity->name);
  317. foreach ($fields as $f) {
  318. // Build the token from the field information.
  319. $token = '[' . $f['field_name'] . ']';
  320. $current_token = array(
  321. 'label' => $f['label'],
  322. 'description' => $f['description'],
  323. 'token' => $token,
  324. 'field_name' => $f['field_name'],
  325. 'required' => $f['required']
  326. );
  327. // If the required only option is set then we only want to add
  328. // required fields to the token list.
  329. if ($options['required only'] AND $current_token['required']) {
  330. $tokens[$token] = $current_token;
  331. }
  332. // If the required only option is not set then add everything.
  333. elseif (!$options['required only']) {
  334. $tokens[$token] = $current_token;
  335. }
  336. }
  337. return $tokens;
  338. }
  339. /**
  340. * Replace all Tripal Tokens in a given string.
  341. *
  342. * NOTE: If there is no value for a token then the token is removed.
  343. *
  344. * @param string $string
  345. * The string containing tokens.
  346. * @param TripalEntity $entity
  347. * The entity with field values used to find values of tokens.
  348. * @param TripalBundle $bundle_entity
  349. * The bundle enitity containing special values sometimes needed for token replacement.
  350. *
  351. * @return
  352. * The string will all tokens replaced with values.
  353. */
  354. function tripal_replace_tokens($string, $entity, $bundle_entity = NULL) {
  355. // Determine which tokens were used in the format string
  356. if (preg_match_all('/\[\w+\]/', $string, $matches)) {
  357. $used_tokens = $matches[0];
  358. foreach($used_tokens as $token) {
  359. $field = str_replace(array('.','[',']'),array('__','',''),$token);
  360. $value = '';
  361. if (isset($entity->{$field})) {
  362. // Render the value from the field.
  363. // @TODO: Handle the case where thefield is empty... currently returns error.
  364. $field_value = field_get_items('TripalEntity', $entity, $field);
  365. $field_render_arr = field_view_value('TripalEntity', $entity, $field, $field_value[0]);
  366. $value = render($field_render_arr);
  367. }
  368. elseif ($field === 'TripalBundle__bundle_id') {
  369. // Load the bundle entity if we weren't given it.
  370. if (!$bundle_entity) {
  371. $bundle_entity = tripal_load_bundle_entity($entity->bundle);
  372. }
  373. // This token should be the id of the TripalBundle.
  374. $value = $bundle_entity->id;
  375. }
  376. elseif ($field === 'TripalEntity__entity_id') {
  377. // This token should be the id of the TripalEntity.
  378. $value = $entity->id;
  379. }
  380. $string = str_replace($token, $value, $string);
  381. }
  382. }
  383. return $string;
  384. }
  385. /**
  386. * Formats the tokens for display.
  387. *
  388. * @param array $tokens
  389. * A list of tokens generated via tripal_get_tokens().
  390. * @return
  391. * Rendered output describing the available tokens.
  392. */
  393. function theme_token_list($tokens) {
  394. $header = array('Token', 'Name', 'Description');
  395. $rows = array();
  396. foreach ($tokens as $details) {
  397. $rows[] = array(
  398. $details['token'],
  399. $details['label'],
  400. $details['description'],
  401. );
  402. }
  403. return theme('table', array('header' => $header, 'rows' => $rows));
  404. }
  405. /**
  406. * @section
  407. * Vocabulary Hooks.
  408. */
  409. /**
  410. * A hook for specifying information about the data store for vocabularies.
  411. *
  412. * The storage backend for controlled vocabularies has traditionally been
  413. * the Chado CV term tables. However, Tripal v3.0 introduces APIs for supporting
  414. * other backends. Therefore, this function indicates to Tripal which
  415. * data stores are capable of providing support for terms.
  416. *
  417. * @return
  418. * An array describing the storage backends implemented by the module. The
  419. * keys are storage backend names. To avoid name clashes, storage
  420. * backend names should be prefixed with the name of the module that
  421. * exposes them. The values are arrays describing the storage backend,
  422. * with the following key/value pairs:
  423. *
  424. * label: The human-readable name of the storage backend.
  425. * module: The name of the module providing the support for this backend.
  426. * description: A short description for the storage backend.
  427. * settings: An array whose keys are the names of the settings available for
  428. * the storage backend, and whose values are the default values for
  429. * those settings.
  430. */
  431. function hook_vocab_storage_info() {
  432. return array(
  433. 'term_chado_storage' => array(
  434. 'label' => t('Chado storage'),
  435. 'description' => t('Integrates terms stored in the local Chado database with Tripal entities.'),
  436. 'settings' => array(),
  437. ),
  438. );
  439. }
  440. /**
  441. * Creates a form for specifying a term for TripalEntity creation.
  442. *
  443. * This hook allows the module that implements a vocabulary storage backend
  444. * to provide the form necessary to select a term that will then be used for
  445. * creating a new TripalEntity type. Tripal will expect that a 'namespace' and
  446. * 'accession' are in the $form_state['storage'] array. The 'namespace' and
  447. * must be the abbreviated uppercase namespace for the vocabulary (e.g. 'RO',
  448. * 'SO', 'PATO', etc.). The 'accession' must be the unique term ID (or
  449. * accession) for the term in the vocabulary.
  450. *
  451. * @param $form
  452. * @param $form_state
  453. *
  454. * @return
  455. * A form object.
  456. */
  457. function hook_vocab_select_term_form(&$form, &$form_state) {
  458. return $form;
  459. }
  460. /**
  461. * Validates the hook_vocab_select_term_form().
  462. *
  463. * @param $name
  464. */
  465. function hook_vocab_select_term_form_validate($form, &$form_state) {
  466. }
  467. /**
  468. * Retreives information about a term from the default term storage backend.
  469. *
  470. * This hook is called by the tripal_entity module to retrieve information
  471. * about the term from the storage backend. It must return an array with
  472. * a set of keys.
  473. *
  474. * @param $namespace
  475. * The namespace of the vocabulary in which the term is found.
  476. * @param $accession
  477. * The unique identifier (accession) for this term.
  478. *
  479. * @return
  480. * An array with at least the following keys:
  481. * namespace : The namespace of the vocabulary.
  482. * accession : The name unique ID of the term.
  483. * name : The name of the term.
  484. * definition : The term's description.
  485. * any other keys may be added as desired. Returns NULL if the term
  486. * cannot be found.
  487. */
  488. function hook_vocab_get_term($namespace, $accession) {
  489. }