tripal_entities.api.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 $term_id
  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, $term_id) {
  14. $query = db_select('tripal_term', 'tt');
  15. $query->join('tripal_vocab' ,'tv', 'tv.id = tt.vocab_id');
  16. $query->fields('tt', array('id', 'term_id'))
  17. ->fields('tv', array('namespace'))
  18. ->condition('tv.namespace', $namespace)
  19. ->condition('tt.term_id', $term_id);
  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.bundle', $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 $term_id
  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, $term_id, $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, $term_id);
  95. if (!$term) {
  96. $args = array('vocab_id' => $vocab->id, 'term_id' => $term_id, '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_id = 'bio-data_' . $term->id;
  102. $einfo = entity_get_info('TripalEntity');
  103. if (!in_array($bundle_id, array_keys($einfo['bundles']))) {
  104. // Insert the bundle.
  105. db_insert('tripal_bundle')
  106. ->fields(array(
  107. 'label' => $term_name,
  108. 'type' => 'TripalEntity',
  109. 'bundle' => $bundle_id,
  110. ))
  111. ->execute();
  112. }
  113. // Clear the entity cache so that Drupal will read our
  114. // hook_entity_info() implementation.
  115. global $language;
  116. $langcode = $language->language;
  117. cache_clear_all("entity_info:$langcode", 'cache');
  118. variable_set('menu_rebuild_needed', TRUE);
  119. // Get the bundle object.
  120. $bundle = tripal_load_bundle_entity($bundle_id);
  121. // Allow modules to now add fields to the bundle
  122. module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle, $term);
  123. return TRUE;
  124. }
  125. /**
  126. * Allows a module to add fields to a bundle.
  127. *
  128. * This function is called after the bundle is created and allows any module
  129. * to add fields to it.
  130. *
  131. * @param $entity_type
  132. * The entity type (e.g. TripalEntity).
  133. * @param $bundle
  134. * A TripalBundle object.
  135. * @param $term
  136. * An instance of a TripalTerm object.
  137. *
  138. * @return
  139. * TRUE on success, FALSE on failure.
  140. */
  141. function hook_add_bundle_fields($entity_type, $bundle, $term) {
  142. }
  143. /**
  144. * @section
  145. * Bundle Variables.
  146. */
  147. /**
  148. * Fetch the value for a given tripal variable.
  149. *
  150. * @param string $variable_name
  151. * The name of the variable as in tripal_variables.name.
  152. * @param int $bundle_id
  153. * The unique identfier for the bundle you want the value for.
  154. * @return text
  155. * The value of the specified variable for the specified bundle.
  156. */
  157. function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE) {
  158. $variable = tripal_get_variable($variable_name);
  159. // Warn if we can't find the tripal_variable.
  160. if (!$variable) {
  161. // tripal_report_error(
  162. // 'trpbundle_var',
  163. // TRIPAL_WARNING,
  164. // 'Unable to fetch tripal bundle variable value due to missing tripal variable (%var).',
  165. // array('%var' => $variable_name)
  166. // );
  167. // return FALSE;
  168. return $default;
  169. }
  170. // Select the value for this variable.
  171. $value = db_select('tripal_bundle_variables', 'var')
  172. ->fields('var', array('value'))
  173. ->condition('var.bundle_id', $bundle_id)
  174. ->condition('var.variable_id', $variable->variable_id)
  175. ->execute()
  176. ->fetchField();
  177. // Warn if the value appears to be empty.
  178. if (!$value) {
  179. // tripal_report_error(
  180. // 'trpbundle_var',
  181. // TRIPAL_WARNING,
  182. // 'Unable to fetch tripal bundle variable (%var) value.',
  183. // array('%var' => $variable_name)
  184. // );
  185. return $default;
  186. }
  187. return $value;
  188. }
  189. /**
  190. * Save the value of a tripal variable for a given bundle.
  191. *
  192. * @param string $variable_name
  193. * The name of the variable as in tripal_variables.name.
  194. * @param int $bundle_id
  195. * The unique identfier for the bundle you want the value for.
  196. * @param $text $value
  197. * The value of the variable for the given bundle.
  198. */
  199. function tripal_set_bundle_variable($variable_name, $bundle_id, $value) {
  200. $variable = tripal_get_variable($variable_name);
  201. // And then we need to write the new format to the tripal_bundle_variables table.
  202. $record = array(
  203. 'bundle_id' => $bundle_id,
  204. 'variable_id' => $variable->variable_id,
  205. 'value' => $value,
  206. );
  207. // Check whether there is already a format saved.
  208. $bundle_variable_id = db_select('tripal_bundle_variables', 'var')
  209. ->fields('var', array('bundle_variable_id'))
  210. ->condition('var.bundle_id', $record['bundle_id'])
  211. ->condition('var.variable_id', $record['variable_id'])
  212. ->execute()
  213. ->fetchField();
  214. if ($bundle_variable_id) {
  215. $record['bundle_variable_id'] = $bundle_variable_id;
  216. return drupal_write_record('tripal_bundle_variables', $record, 'bundle_variable_id');
  217. }
  218. else {
  219. return drupal_write_record('tripal_bundle_variables', $record);
  220. }
  221. }
  222. /**
  223. * @section
  224. * Title & URL Formats.
  225. */
  226. /**
  227. * Get Page Title Format for a given Tripal Entity Type.
  228. *
  229. * @param TripalBundle $entity
  230. * The Entity object for the Tripal Bundle the title format is for.
  231. */
  232. function tripal_get_title_format($entity) {
  233. // Get the existing title format if it exists.
  234. $title_format = tripal_get_bundle_variable('title_format', $entity->id);
  235. // If there isn't yet a title format for this bundle/type then we should
  236. // determine the default.
  237. if (!$title_format) {
  238. $title_format = tripal_get_default_title_format($entity);
  239. tripal_save_title_format($entity, $title_format);
  240. }
  241. return $title_format;
  242. }
  243. /**
  244. * Save Page Title Format for a given Tripal Entity Type.
  245. *
  246. * @param TripalBundle $entity
  247. * The Entity object for the Tripal Bundle the title format is for.
  248. * @param string $format
  249. * The pattern to be used when generating entity titles for the above type.
  250. */
  251. function tripal_save_title_format($entity, $format) {
  252. return tripal_set_bundle_variable('title_format', $entity->id, $format);
  253. }
  254. /**
  255. * Determine the default pattern/format to use for an entity type.
  256. *
  257. * @param TripalBundle $entity
  258. * The Entity object for the Tripal Bundle the title format is for.
  259. * @return string
  260. * A default title format.
  261. */
  262. function tripal_get_default_title_format($entity) {
  263. $format = array();
  264. // Retrieve all available tokens.
  265. $tokens = tripal_get_tokens($entity);
  266. foreach($tokens as $token) {
  267. if ($token['required']) {
  268. $format[] = $token['token'];
  269. }
  270. }
  271. return implode(', ', $format);
  272. }
  273. /**
  274. * Returns an array of tokens based on Tripal Entity Fields.
  275. *
  276. * @param TripalBundle $entity
  277. * The bundle entity for which you want tokens.
  278. * @return
  279. * An array of tokens where the key is the machine_name of the token.
  280. */
  281. function tripal_get_tokens($entity) {
  282. $tokens = array();
  283. $fields = field_info_instances('TripalEntity', $entity->bundle);
  284. foreach ($fields as $f) {
  285. // Build the token from the field information.
  286. $token = '[' . $f['field_name'] . ']';
  287. $tokens[$token] = array(
  288. 'label' => $f['label'],
  289. 'description' => $f['description'],
  290. 'token' => $token,
  291. 'field_name' => $f['field_name'],
  292. 'required' => $f['required']
  293. );
  294. }
  295. return $tokens;
  296. }
  297. /**
  298. * Formats the tokens for display.
  299. *
  300. * @param array $tokens
  301. * A list of tokens generated via tripal_get_tokens().
  302. * @return
  303. * Rendered output describing the available tokens.
  304. */
  305. function theme_token_list($tokens) {
  306. $header = array('Token', 'Name', 'Description');
  307. $rows = array();
  308. foreach ($tokens as $details) {
  309. $rows[] = array(
  310. '[' . $details['field_name'] . ']',
  311. $details['label'],
  312. $details['description'],
  313. );
  314. }
  315. return theme('table', array('header' => $header, 'rows' => $rows));
  316. }
  317. /**
  318. * @section
  319. * Vocabulary Hooks.
  320. */
  321. /**
  322. * A hook for specifying information about the data store for vocabularies.
  323. *
  324. * The storage backend for controlled vocabularies has traditionally been
  325. * the Chado CV term tables. However, Tripal v3.0 introduces APIs for supporting
  326. * other backends. Therefore, this function indicates to Tripal which
  327. * data stores are capable of providing support for terms.
  328. *
  329. * @return
  330. * An array describing the storage backends implemented by the module. The
  331. * keys are storage backend names. To avoid name clashes, storage
  332. * backend names should be prefixed with the name of the module that
  333. * exposes them. The values are arrays describing the storage backend,
  334. * with the following key/value pairs:
  335. *
  336. * label: The human-readable name of the storage backend.
  337. * module: The name of the module providing the support for this backend.
  338. * description: A short description for the storage backend.
  339. * settings: An array whose keys are the names of the settings available for
  340. * the storage backend, and whose values are the default values for
  341. * those settings.
  342. */
  343. function hook_vocab_storage_info() {
  344. return array(
  345. 'term_chado_storage' => array(
  346. 'label' => t('Chado storage'),
  347. 'description' => t('Integrates terms stored in the local Chado database with Tripal entities.'),
  348. 'settings' => array(),
  349. ),
  350. );
  351. }
  352. /**
  353. * Creates a form for specifying a term for TripalEntity creation.
  354. *
  355. * This hook allows the module that implements a vocabulary storage backend
  356. * to provide the form necessary to select a term that will then be used for
  357. * creating a new TripalEntity type. Tripal will expect that a 'namespace' and
  358. * 'term_id' are in the $form_state['storage'] array. The 'namespace' and
  359. * must be the abbreviated uppercase namespace for the vocabulary (e.g. 'RO',
  360. * 'SO', 'PATO', etc.). The 'term_id' must be the unique term ID (or
  361. * accession) for the term in the vocabulary.
  362. *
  363. * @param $form
  364. * @param $form_state
  365. *
  366. * @return
  367. * A form object.
  368. */
  369. function hook_vocab_select_term_form(&$form, &$form_state) {
  370. return $form;
  371. }
  372. /**
  373. * Validates the hook_vocab_select_term_form().
  374. *
  375. * @param $name
  376. */
  377. function hook_vocab_select_term_form_validate($form, &$form_state) {
  378. }