tripal_entities.api.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 = NULL;
  276. // Retrieve all available tokens.
  277. $tokens = tripal_get_tokens($entity);
  278. // A) Check to see if more informed modules have suggested a title for this type.
  279. // Invoke hook_tripal_default_title_format() to get all suggestions from other modules.
  280. $suggestions = module_invoke_all('tripal_default_title_format', $entity, $tokens);
  281. if ($suggestions) {
  282. // Use the suggestion with the lightest weight.
  283. $lightest_key = NULL;
  284. foreach ($suggestions as $k => $s) {
  285. if ($lightest_key === NULL) $lightest_key = $k;
  286. if ($s['weight'] < $lightest_key) $lightest_key = $k;
  287. }
  288. $format = $suggestions[$lightest_key]['format'];
  289. }
  290. // B) Check to see if any fields contain "name" in the machine name and if so, use them.
  291. $name_fields = preg_grep('/name/', array_keys($tokens));
  292. if ($name_fields AND !$format) {
  293. $format = implode(', ', $name_fields);
  294. }
  295. // C) Generate our own ugly title by simply comma-separating all the required fields.
  296. if (!$format) {
  297. $tmp = array();
  298. // Check which tokens are required fields and join them into a default format.
  299. foreach($tokens as $token) {
  300. if ($token['required']) {
  301. $tmp[] = $token['token'];
  302. }
  303. }
  304. $format = implode(', ', $tmp);
  305. }
  306. return $format;
  307. }
  308. /**
  309. * Implement this hook to define default formats for Tripal Content Types.
  310. *
  311. * @param TripalBundle $entity
  312. * A tripal content type entity with information to be used for determining the default title format.
  313. * @param array $available_tokens
  314. * An array of available tokens for this particular tripal content type.
  315. *
  316. * @return array
  317. * An array of potential formats. The lightest weighted format suggested by all modules will be chosen.
  318. * Each array item should consist of a 'weight' and 'format'. See the hook implementation below
  319. * for examples.
  320. * - weight: an integer used to determine priority of suggestions.
  321. * The smaller/lighter the number the higher the priority.
  322. * Best practice is to use a weight less than 0 for extension modules.
  323. * specifically, -2 is a good weight for calculated formats and -5 is a
  324. * good weight for hard-coded formats specific to a given type.
  325. * - format: a string including approved tokens used to determine the title
  326. * on Tripal content pages.
  327. */
  328. function hook_tripal_default_title_format($entity, $available_tokens) {
  329. $format = array();
  330. // If you want to suggest a default format for a particular vocabulary term:
  331. //---------------------------------------------------------------------------
  332. // Load the term associated with this Tripal Content type.
  333. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  334. $term = reset($term);
  335. // If it's the term you are interested in then suggest a format.
  336. if ($term->name == 'organism') {
  337. // To suggest a format, add an element to the array with a format & weight key.
  338. $format[] = array(
  339. // This is the format/pattern you suggest be used to determine the title of organism pages.
  340. 'format' => '[organism__genus] [organism__species]',
  341. // The weight/priority of your suggestion.
  342. 'weight' => -5
  343. );
  344. }
  345. // Say you know that in your particular site, all 'names' are required
  346. // and you want to only use the human-readable name:
  347. //---------------------------------------------------------------------------
  348. $name_field = preg_grep('/__name]$/', array_keys($available_tokens));
  349. $name_field = reset($name_field);
  350. if (is_string($name_field)) {
  351. $format[] = array(
  352. 'format' => $name_field,
  353. 'weight' => -2,
  354. );
  355. }
  356. return $format;
  357. }
  358. /**
  359. * Returns an array of tokens based on Tripal Entity Fields.
  360. *
  361. * @param TripalBundle $entity
  362. * The bundle entity for which you want tokens.
  363. * @return
  364. * An array of tokens where the key is the machine_name of the token.
  365. */
  366. function tripal_get_tokens($entity, $options = array()) {
  367. $tokens = array();
  368. // Set default options.
  369. $options['required only'] = (isset($options['required only'])) ? $options['required only'] : FALSE;
  370. $options['include id'] = (isset($options['include id'])) ? $options['include id'] : TRUE;
  371. if ($options['include id']) {
  372. $token = '[TripalBundle__bundle_id]';
  373. $tokens[$token] = array(
  374. 'label' => 'Bundle ID',
  375. 'description' => 'The unique identifier for this Tripal Content Type.',
  376. 'token' => $token,
  377. 'field_name' => NULL,
  378. 'required' => TRUE
  379. );
  380. $token = '[TripalEntity__entity_id]';
  381. $tokens[$token] = array(
  382. 'label' => 'Content/Entity ID',
  383. 'description' => 'The unique identifier for an individual piece of Tripal Content.',
  384. 'token' => $token,
  385. 'field_name' => NULL,
  386. 'required' => TRUE
  387. );
  388. }
  389. $fields = field_info_instances('TripalEntity', $entity->name);
  390. foreach ($fields as $f) {
  391. // Build the token from the field information.
  392. $token = '[' . $f['field_name'] . ']';
  393. $current_token = array(
  394. 'label' => $f['label'],
  395. 'description' => $f['description'],
  396. 'token' => $token,
  397. 'field_name' => $f['field_name'],
  398. 'required' => $f['required']
  399. );
  400. // If the required only option is set then we only want to add
  401. // required fields to the token list.
  402. if ($options['required only'] AND $current_token['required']) {
  403. $tokens[$token] = $current_token;
  404. }
  405. // If the required only option is not set then add everything.
  406. elseif (!$options['required only']) {
  407. $tokens[$token] = $current_token;
  408. }
  409. }
  410. return $tokens;
  411. }
  412. /**
  413. * Replace all Tripal Tokens in a given string.
  414. *
  415. * NOTE: If there is no value for a token then the token is removed.
  416. *
  417. * @param string $string
  418. * The string containing tokens.
  419. * @param TripalEntity $entity
  420. * The entity with field values used to find values of tokens.
  421. * @param TripalBundle $bundle_entity
  422. * The bundle enitity containing special values sometimes needed for token replacement.
  423. *
  424. * @return
  425. * The string will all tokens replaced with values.
  426. */
  427. function tripal_replace_tokens($string, $entity, $bundle_entity = NULL) {
  428. // Determine which tokens were used in the format string
  429. if (preg_match_all('/\[\w+\]/', $string, $matches)) {
  430. $used_tokens = $matches[0];
  431. foreach($used_tokens as $token) {
  432. $field = str_replace(array('.','[',']'),array('__','',''),$token);
  433. $value = '';
  434. if (isset($entity->{$field})) {
  435. // Render the value from the field.
  436. // First get the items to be rendered.
  437. $field_value = field_get_items('TripalEntity', $entity, $field);
  438. if (isset($field_value[0])) {
  439. // Then get a render array for just the value of the first item (no markup).
  440. $field_render_arr = field_view_value('TripalEntity', $entity, $field, $field_value[0]);
  441. // Finally render the value from the render array.
  442. $value = render($field_render_arr);
  443. }
  444. }
  445. elseif ($field === 'TripalBundle__bundle_id') {
  446. // Load the bundle entity if we weren't given it.
  447. if (!$bundle_entity) {
  448. $bundle_entity = tripal_load_bundle_entity($entity->bundle);
  449. }
  450. // This token should be the id of the TripalBundle.
  451. $value = $bundle_entity->id;
  452. }
  453. elseif ($field === 'TripalEntity__entity_id') {
  454. // This token should be the id of the TripalEntity.
  455. $value = $entity->id;
  456. }
  457. $string = str_replace($token, $value, $string);
  458. }
  459. }
  460. return $string;
  461. }
  462. /**
  463. * Formats the tokens for display.
  464. *
  465. * @param array $tokens
  466. * A list of tokens generated via tripal_get_tokens().
  467. * @return
  468. * Rendered output describing the available tokens.
  469. */
  470. function theme_token_list($tokens) {
  471. $header = array('Token', 'Name', 'Description');
  472. $rows = array();
  473. foreach ($tokens as $details) {
  474. $rows[] = array(
  475. $details['token'],
  476. $details['label'],
  477. $details['description'],
  478. );
  479. }
  480. return theme('table', array('header' => $header, 'rows' => $rows));
  481. }
  482. /**
  483. * @section
  484. * Vocabulary Hooks.
  485. */
  486. /**
  487. * A hook for specifying information about the data store for vocabularies.
  488. *
  489. * The storage backend for controlled vocabularies has traditionally been
  490. * the Chado CV term tables. However, Tripal v3.0 introduces APIs for supporting
  491. * other backends. Therefore, this function indicates to Tripal which
  492. * data stores are capable of providing support for terms.
  493. *
  494. * @return
  495. * An array describing the storage backends implemented by the module. The
  496. * keys are storage backend names. To avoid name clashes, storage
  497. * backend names should be prefixed with the name of the module that
  498. * exposes them. The values are arrays describing the storage backend,
  499. * with the following key/value pairs:
  500. *
  501. * label: The human-readable name of the storage backend.
  502. * module: The name of the module providing the support for this backend.
  503. * description: A short description for the storage backend.
  504. * settings: An array whose keys are the names of the settings available for
  505. * the storage backend, and whose values are the default values for
  506. * those settings.
  507. */
  508. function hook_vocab_storage_info() {
  509. return array(
  510. 'term_chado_storage' => array(
  511. 'label' => t('Chado storage'),
  512. 'description' => t('Integrates terms stored in the local Chado database with Tripal entities.'),
  513. 'settings' => array(),
  514. ),
  515. );
  516. }
  517. /**
  518. * Creates a form for specifying a term for TripalEntity creation.
  519. *
  520. * This hook allows the module that implements a vocabulary storage backend
  521. * to provide the form necessary to select a term that will then be used for
  522. * creating a new TripalEntity type. Tripal will expect that a 'namespace' and
  523. * 'accession' are in the $form_state['storage'] array. The 'namespace' and
  524. * must be the abbreviated uppercase namespace for the vocabulary (e.g. 'RO',
  525. * 'SO', 'PATO', etc.). The 'accession' must be the unique term ID (or
  526. * accession) for the term in the vocabulary.
  527. *
  528. * @param $form
  529. * @param $form_state
  530. *
  531. * @return
  532. * A form object.
  533. */
  534. function hook_vocab_select_term_form(&$form, &$form_state) {
  535. return $form;
  536. }
  537. /**
  538. * Validates the hook_vocab_select_term_form().
  539. *
  540. * @param $name
  541. */
  542. function hook_vocab_select_term_form_validate($form, &$form_state) {
  543. }
  544. /**
  545. * Retreives information about a term from the default term storage backend.
  546. *
  547. * This hook is called by the tripal_entity module to retrieve information
  548. * about the term from the storage backend. It must return an array with
  549. * a set of keys.
  550. *
  551. * @param $namespace
  552. * The namespace of the vocabulary in which the term is found.
  553. * @param $accession
  554. * The unique identifier (accession) for this term.
  555. *
  556. * @return
  557. * An array with at least the following keys:
  558. * namespace : The namespace of the vocabulary.
  559. * accession : The name unique ID of the term.
  560. * name : The name of the term.
  561. * definition : The term's description.
  562. * any other keys may be added as desired. Returns NULL if the term
  563. * cannot be found.
  564. */
  565. function hook_vocab_get_term($namespace, $accession) {
  566. }