tripal.entities.api.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. <?php
  2. /**
  3. * Retrieves a TripalTerm entity that matches the given arguments.
  4. *
  5. * @param $values
  6. * An associative array used to match a term. Valid keys may be 'namespace',
  7. * 'accession, or 'term_id'. The keys 'namespace' and 'accession' must
  8. * always be used together to uniquely identify a term. The key 'term_id'
  9. * can be used alone to uniquely identify a term.
  10. *
  11. * @return
  12. * A TripalTerm entity object or NULL if not found.
  13. */
  14. function tripal_load_term_entity($values) {
  15. $namespace = array_key_exists('namespace', $values) ? $values['namespace'] : '';
  16. $accession = array_key_exists('accession', $values) ? $values['accession'] : '';
  17. $term_id = array_key_exists('term_id', $values) ? $values['term_id'] : '';
  18. $term = NULL;
  19. if ($namespace and $accession) {
  20. $query = db_select('tripal_term', 'tt');
  21. $query->join('tripal_vocab', 'tv', 'tv.id = tt.vocab_id');
  22. $query->fields('tt', array('id'))
  23. ->fields('tv', array('namespace'))
  24. ->condition('tv.namespace', $namespace)
  25. ->condition('tt.accession', $accession);
  26. $term = $query->execute()->fetchObject();
  27. }
  28. else if ($term_id) {
  29. $query = db_select('tripal_term', 'tt');
  30. $query->fields('tt', array('id'))
  31. ->condition('tt.id', $term_id);
  32. $term = $query->execute()->fetchObject();
  33. }
  34. if ($term) {
  35. $entity = entity_load('TripalTerm', array($term->id));
  36. return reset($entity);
  37. }
  38. return NULL;
  39. }
  40. /**
  41. * Retrieves a TripalVocab entity that maches the given arguments.
  42. *
  43. * @param $values
  44. * An associative array used to match a term. Currently, the only valid keys
  45. * is 'namespace'.
  46. *
  47. * @return
  48. * A TripalVocab entity object or NULL if not found.
  49. */
  50. function tripal_load_vocab_entity($values) {
  51. $namespace = array_key_exists('namespace', $values) ? $values['namespace'] : '';
  52. $vocab = NULL;
  53. if ($namespace) {
  54. $vocab = db_select('tripal_vocab', 'tv')
  55. ->fields('tv')
  56. ->condition('tv.namespace', $namespace)
  57. ->execute()
  58. ->fetchObject();
  59. }
  60. if ($vocab) {
  61. $entity = entity_load('TripalVocab', array($vocab->id));
  62. return reset($entity);
  63. }
  64. return NULL;
  65. }
  66. /**
  67. * Retrieves a TripalBundle entity that matches the given arguments.
  68. *
  69. * @param $values
  70. * An associative array used to match a bundle. Valid keys may:
  71. * - name: the bundle name (e.g. 'bio-data_234')
  72. * - label: the bundle label (e.g. 'Organism')
  73. * - term_id: the term ID to which the bundle belongs
  74. *
  75. * @return
  76. * A TripalBundle entity object or NULL if not found.
  77. */
  78. function tripal_load_bundle_entity($values) {
  79. $query = db_select('tripal_bundle', 'tb');
  80. $query->fields('tb');
  81. if (array_key_exists('name', $values)) {
  82. $query->condition('tb.name', $values['name']);
  83. }
  84. if (array_key_exists('label', $values)) {
  85. $query->condition('tb.label', $values['label']);
  86. }
  87. if (array_key_exists('term_id', $values)) {
  88. $query->condition('tb.term_id', $values['term_id']);
  89. }
  90. $bundle = $query->execute()->fetchObject();
  91. if ($bundle) {
  92. $entity = entity_load('TripalBundle', array($bundle->id));
  93. return reset($entity);
  94. }
  95. return NULL;
  96. }
  97. /**
  98. * Creates a new Tripal Entity type (i.e. bundle).
  99. *
  100. * @param $namespace
  101. * The abbreviated namespace for the vocabulary (e.g. RO, SO, PATO).
  102. * @param $accession
  103. * The unique term ID in the vocabulary $namespace (i.e. an accession).
  104. * @param $term_name
  105. * A human-readable name for this term. This will became the name that
  106. * appears for the content type. In practice, this should be the name
  107. * of the term. (E.g. the name for SO:0000704 is gene).
  108. * @param $error
  109. * A string, passed by reference, that is filled with the error message
  110. * if the function fails.
  111. *
  112. * @return
  113. * TRUE if the entity type (bundle) was succesfully created. FALSE otherwise.
  114. */
  115. function tripal_create_bundle($namespace, $accession, $term_name, &$error = '') {
  116. // First create the TripalVocab if it doesn't already exist.
  117. $vocab = tripal_load_vocab_entity(array('namespace' => $namespace));
  118. if (!$vocab) {
  119. $vocab = entity_get_controller('TripalVocab')->create(array('namespace' => $namespace));
  120. $vocab->save();
  121. }
  122. // Next create the TripalTerm if it doesn't already exist.
  123. $term = tripal_load_term_entity(array('namespace' => $namespace, 'accession' => $accession));
  124. if (!$term) {
  125. $args = array('vocab_id' => $vocab->id, 'accession' => $accession, 'name' => $term_name);
  126. $term = entity_get_controller('TripalTerm')->create($args);
  127. $term = $term->save();
  128. }
  129. // If the bundle doesn't already exist, then add it.
  130. $bundle_name = 'bio-data_' . $term->id;
  131. $einfo = entity_get_info('TripalEntity');
  132. if (!in_array($bundle_name, array_keys($einfo['bundles']))) {
  133. // Insert the bundle.
  134. db_insert('tripal_bundle')
  135. ->fields(array(
  136. 'label' => $term_name,
  137. 'type' => 'TripalEntity',
  138. 'name' => $bundle_name,
  139. 'term_id' => $term->id,
  140. ))
  141. ->execute();
  142. }
  143. // Clear the entity cache so that Drupal will read our
  144. // hook_entity_info() implementation.
  145. global $language;
  146. $langcode = $language->language;
  147. cache_clear_all("entity_info:$langcode", 'cache');
  148. variable_set('menu_rebuild_needed', TRUE);
  149. // Get the bundle object.
  150. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  151. // Allow modules now add fields to the bundle
  152. module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle, $term);
  153. return TRUE;
  154. }
  155. /**
  156. * Refreshes the bundle such that new fields added by modules will be found.
  157. *
  158. * @param $bundle_name
  159. * The name of the bundle to refresh (e.g. bio-data_4).
  160. */
  161. function tripal_refresh_bundle_fields($bundle_name) {
  162. // Get the bundle object.
  163. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  164. if (!$bundle) {
  165. tripal_report_error('tripal', TRIPAL_ERROR, "Unrecognized bundle name '%bundle'.",
  166. array('%bundle' => $bundle_name));
  167. return FALSE;
  168. }
  169. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  170. // Allow modules now add fields to the bundle
  171. module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle, $term);
  172. }
  173. /**
  174. * Adds a field to an Entity bundle.
  175. *
  176. * @param $field_name
  177. * The name of the field.
  178. * @param $field_info
  179. * An associative array containing the field information. The following
  180. * key/value pairs are supported:
  181. * 'field_type' : a valid field type. May be any of the Drupal default
  182. * fields, one created by the tripal_chado module or another custom module.
  183. * 'widget_type' : a valid widget type. May be any of the Drupal default
  184. * fields, one created by the tripal_chado module or another custom module.
  185. * 'field_settings' : an array of settings that are appropriate for the
  186. * selected field type.
  187. * 'widget_settings' : an array of settings that are appropriate for the
  188. * selected widget type.
  189. * 'description' : a default description for this field.
  190. * 'label' : a label used as a header for this field.
  191. * 'is_required' : indicates if the field is required in the edit form.
  192. * 'cardinality' : indicates the number of values this field can support.
  193. * the default is 1 (meaning only one value). Use a value of
  194. * FIELD_CARDINALITY_UNLIMITED for unlimited number of values.
  195. * 'default_value' : A default value for the field.
  196. * 'format' : A string indicating the format for the field. Must be
  197. * specific to the field.
  198. * @param $entity_type_name
  199. * The entity type name.
  200. * @param $bundle_name
  201. * The bundle name.
  202. *
  203. */
  204. function tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name) {
  205. $field = field_info_field($field_name);
  206. // If the field is not present something is wrong and we should return.
  207. // if (!$field) {
  208. // tripal_report_error('tripal', TRIPAL_ERROR,
  209. // "The field, '%name', cannot be found. Cannot add it to the bundle.",
  210. // array('%name' => $field_name));
  211. // return;
  212. // }
  213. // If the field exists and is attached to this bundle then just return,
  214. // there is nothing left to do.
  215. if ($field and array_key_exists('bundles', $field) and
  216. array_key_exists($entity_type_name, $field['bundles']) and
  217. in_array($bundle_name, $field['bundles'][$entity_type_name])) {
  218. return;
  219. }
  220. $cardinality = 1;
  221. if (array_key_exists('cardinality', $field_info) and is_numeric($field_info['cardinality'])) {
  222. $cardinality = $field_info['cardinality'];
  223. }
  224. // If the field doesn't exist then create it.
  225. if (!$field) {
  226. $field = array(
  227. 'field_name' => $field_name,
  228. 'type' => $field_info['field_type'],
  229. 'cardinality' => $cardinality,
  230. 'locked' => FALSE,
  231. 'storage' => array(
  232. 'type' => $field_info['storage']
  233. ),
  234. 'settings' => $field_info['field_settings'],
  235. );
  236. field_create_field($field);
  237. }
  238. // Attach the field to the bundle.
  239. $field_instance = array(
  240. 'field_name' => $field_name,
  241. 'label' => $field_info['label'],
  242. 'description' => $field_info['description'],
  243. 'widget' => array(
  244. 'type' => $field_info['widget_type'],
  245. 'settings' => $field_info['widget_settings'],
  246. ),
  247. 'entity_type' => $entity_type_name,
  248. 'required' => $field_info['is_required'],
  249. 'settings' => $field_info['field_settings'],
  250. 'bundle' => $bundle_name,
  251. 'default_value' => array_key_exists('default_value', $field_info) ? $field_info['default_value'] : '',
  252. 'format' => array_key_exists('format', $field_info) ? $field_info['format'] : '',
  253. );
  254. field_create_instance($field_instance);
  255. }
  256. /**
  257. * Allows a module to make changes to an entity object after creation.
  258. *
  259. * This function is added by Tripal to allow datastore backends to add
  260. * addition properties to the entity that they themselves will use later.
  261. *
  262. * @param $entity
  263. * @param $entity_type
  264. */
  265. function hook_entity_create(&$entity, $entity_type) {
  266. }
  267. /**
  268. * Adds fields to a bundle type.
  269. *
  270. * When a new bundle (Tripal content type) is created, the tripal module
  271. * allows any other module the implements this hook to add fields too it. This
  272. * hook is called automatically.
  273. *
  274. * @param $entity_type
  275. * The type of entity (e.g. 'TripalEntity')
  276. * @param $bundle
  277. * An instance of a TripalBundle object. This is the bundle to which
  278. * the fields will be added.
  279. * @param $term
  280. * An instance of a TripalTerm object. Each TripalBundle is associated with
  281. * a controlled vocabulary term. This is the term object for the bundle.
  282. *
  283. * @return
  284. * There is no return value.
  285. */
  286. function hook_add_bundle_fields($entity_type, $bundle, $term) {
  287. //
  288. // The example code below is derived from the tripal_chado modules and
  289. // adds a 'synonym' field to the bundle.
  290. //
  291. $bundle_name = $bundle->name;
  292. // This array will hold details that map the bundle to tables in Chado.
  293. $bundle_data = array();
  294. // Get the cvterm that corresponds to this TripalTerm object.
  295. $vocab = entity_load('TripalVocab', array($term->vocab_id));
  296. $vocab = reset($vocab);
  297. $match = array(
  298. 'dbxref_id' => array(
  299. 'db_id' => array(
  300. 'name' => $vocab->namespace,
  301. ),
  302. 'accession' => $term->accession
  303. ),
  304. );
  305. $cvterm = chado_generate_var('cvterm', $match);
  306. // Get the default table that this term maps to.
  307. $default = db_select('tripal_cv_defaults', 't')
  308. ->fields('t')
  309. ->condition('cv_id', $cvterm->cv_id->cv_id)
  310. ->execute()
  311. ->fetchObject();
  312. if ($default) {
  313. $bundle_data = array(
  314. 'cv_id' => $cvterm->cv_id->cv_id,
  315. 'cvterm_id' => $cvterm->cvterm_id,
  316. 'data_table' => $default->table_name,
  317. 'type_table' => $default->table_name,
  318. 'field' => $default->field_name,
  319. );
  320. }
  321. else {
  322. return;
  323. }
  324. // Formulate the name of the synonym table.
  325. $syn_table = $bundle_data['data_table'] . '_synonym';
  326. // Don't add a field to this bundle if
  327. if (!chado_table_exists($syn_table)) {
  328. return;
  329. }
  330. $field_name = $syn_table;
  331. $schema = chado_get_schema($syn_table);
  332. $pkey = $schema['primary key'][0];
  333. // Tripal wants to map fields to a controlled vocabulary terms
  334. // (e.g SO:0000704 for a 'gene'). This is not required, but is highly
  335. // recommended to support mashups of data via web services.
  336. $semantic_web = array(
  337. // The type is the term from a vocabulary that desribes this field..
  338. 'type' => '',
  339. // The namepsace for the vocabulary (e.g. 'foaf').
  340. 'ns' => '',
  341. // The URL for the namespace. It must be that the type can be
  342. // appended to the URL.
  343. 'nsurl' => '',
  344. );
  345. // The Tripal Chado module needs to know what Chado table and field
  346. // this field maps to, as well as the base table that it maps to
  347. // if this field will map to a record in a linking table.
  348. $field_settings = array(
  349. // The Chado table that this field maps to.
  350. 'chado_table' => $syn_table,
  351. // The column in the chado table that this field maps to.
  352. 'chado_column' => $pkey,
  353. // The base table that this field is connected to.
  354. 'base_table' => $base_table,
  355. 'semantic_web' => $semantic_web,
  356. );
  357. // Finally set the field values.
  358. $field_info = array(
  359. 'field_type' => 'synonym',
  360. 'widget_type' => 'tripal_fields_synonym_widget',
  361. 'widget_settings' => array('display_label' => 1),
  362. 'description' => '',
  363. 'label' => 'Synonyms',
  364. 'is_required' => 0,
  365. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  366. 'storage' => 'field_chado_storage',
  367. 'field_settings' => $field_settings,
  368. );
  369. // Call the Tripal API function tripal_add_bundle_field to complete the
  370. // addition of the field to the bundle.
  371. tripal_add_bundle_field($field_name, $field_info, $entity_type_name, $bundle_name);
  372. }
  373. /**
  374. * @section
  375. * Bundle Variables.
  376. */
  377. /**
  378. * Fetch the value for a given tripal variable.
  379. *
  380. * @param string $variable_name
  381. * The name of the variable as in tripal_variables.name.
  382. * @param int $bundle_id
  383. * The unique identfier for the bundle you want the value for.
  384. * @return text
  385. * The value of the specified variable for the specified bundle.
  386. */
  387. function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE) {
  388. $variable = tripal_get_variable($variable_name);
  389. // Warn if we can't find the tripal_variable.
  390. if (!$variable) {
  391. return $default;
  392. }
  393. // Select the value for this variable.
  394. $value = db_select('tripal_bundle_variables', 'var')
  395. ->fields('var', array('value'))
  396. ->condition('var.bundle_id', $bundle_id)
  397. ->condition('var.variable_id', $variable->variable_id)
  398. ->execute()
  399. ->fetchField();
  400. // Warn if the value appears to be empty.
  401. if (!$value) {
  402. return $default;
  403. }
  404. return $value;
  405. }
  406. /**
  407. * Save the value of a tripal variable for a given bundle.
  408. *
  409. * @param string $variable_name
  410. * The name of the variable as in tripal_variables.name.
  411. * @param int $bundle_id
  412. * The unique identfier for the bundle you want the value for.
  413. * @param $text $value
  414. * The value of the variable for the given bundle.
  415. */
  416. function tripal_set_bundle_variable($variable_name, $bundle_id, $value) {
  417. $variable = tripal_get_variable($variable_name);
  418. // And then we need to write the new format to the tripal_bundle_variables table.
  419. $record = array(
  420. 'bundle_id' => $bundle_id,
  421. 'variable_id' => $variable->variable_id,
  422. 'value' => $value,
  423. );
  424. // Check whether there is already a format saved.
  425. $bundle_variable_id = db_select('tripal_bundle_variables', 'var')
  426. ->fields('var', array('bundle_variable_id'))
  427. ->condition('var.bundle_id', $record['bundle_id'])
  428. ->condition('var.variable_id', $record['variable_id'])
  429. ->execute()
  430. ->fetchField();
  431. if ($bundle_variable_id) {
  432. $record['bundle_variable_id'] = $bundle_variable_id;
  433. return drupal_write_record('tripal_bundle_variables', $record, 'bundle_variable_id');
  434. }
  435. else {
  436. return drupal_write_record('tripal_bundle_variables', $record);
  437. }
  438. }
  439. /**
  440. * @section
  441. * Title & URL Formats.
  442. */
  443. /**
  444. * Get Page Title Format for a given Tripal Entity Type.
  445. *
  446. * @param TripalBundle $entity
  447. * The Entity object for the Tripal Bundle the title format is for.
  448. */
  449. function tripal_get_title_format($entity) {
  450. // Get the existing title format if it exists.
  451. $title_format = tripal_get_bundle_variable('title_format', $entity->id);
  452. // If there isn't yet a title format for this bundle/type then we should
  453. // determine the default.
  454. if (!$title_format) {
  455. $title_format = tripal_get_default_title_format($entity);
  456. tripal_save_title_format($entity, $title_format);
  457. }
  458. return $title_format;
  459. }
  460. /**
  461. * Save Page Title Format for a given Tripal Entity Type.
  462. *
  463. * @param TripalBundle $entity
  464. * The Entity object for the Tripal Bundle the title format is for.
  465. * @param string $format
  466. * The pattern to be used when generating entity titles for the above type.
  467. */
  468. function tripal_save_title_format($entity, $format) {
  469. return tripal_set_bundle_variable('title_format', $entity->id, $format);
  470. }
  471. /**
  472. * Determine the default pattern/format to use for an entity type.
  473. *
  474. * @param TripalBundle $entity
  475. * The Entity object for the Tripal Bundle the title format is for.
  476. * @return string
  477. * A default title format.
  478. */
  479. function tripal_get_default_title_format($entity) {
  480. $format = NULL;
  481. // Retrieve all available tokens.
  482. $tokens = tripal_get_tokens($entity);
  483. // A) Check to see if more informed modules have suggested a title for this type.
  484. // Invoke hook_tripal_default_title_format() to get all suggestions from other modules.
  485. $suggestions = module_invoke_all('tripal_default_title_format', $entity, $tokens);
  486. if ($suggestions) {
  487. // Use the suggestion with the lightest weight.
  488. $lightest_key = NULL;
  489. foreach ($suggestions as $k => $s) {
  490. if ($lightest_key === NULL) $lightest_key = $k;
  491. if ($s['weight'] < $lightest_key) $lightest_key = $k;
  492. }
  493. $format = $suggestions[$lightest_key]['format'];
  494. }
  495. // B) Check to see if any fields contain "name" in the machine name and if so, use them.
  496. $name_fields = preg_grep('/name/', array_keys($tokens));
  497. if ($name_fields AND !$format) {
  498. $format = implode(', ', $name_fields);
  499. }
  500. // C) Generate our own ugly title by simply comma-separating all the required fields.
  501. if (!$format) {
  502. $tmp = array();
  503. // Check which tokens are required fields and join them into a default format.
  504. foreach($tokens as $token) {
  505. if ($token['required']) {
  506. $tmp[] = $token['token'];
  507. }
  508. }
  509. $format = implode(', ', $tmp);
  510. }
  511. return $format;
  512. }
  513. /**
  514. * Implement this hook to define default formats for Tripal Content Types.
  515. *
  516. * @param TripalBundle $entity
  517. * A tripal content type entity with information to be used for determining the default title format.
  518. * @param array $available_tokens
  519. * An array of available tokens for this particular tripal content type.
  520. *
  521. * @return array
  522. * An array of potential formats. The lightest weighted format suggested by all modules will be chosen.
  523. * Each array item should consist of a 'weight' and 'format'. See the hook implementation below
  524. * for examples.
  525. * - weight: an integer used to determine priority of suggestions.
  526. * The smaller/lighter the number the higher the priority.
  527. * Best practice is to use a weight less than 0 for extension modules.
  528. * specifically, -2 is a good weight for calculated formats and -5 is a
  529. * good weight for hard-coded formats specific to a given type.
  530. * - format: a string including approved tokens used to determine the title
  531. * on Tripal content pages.
  532. */
  533. function hook_tripal_default_title_format($entity, $available_tokens) {
  534. $format = array();
  535. // If you want to suggest a default format for a particular vocabulary term:
  536. //---------------------------------------------------------------------------
  537. // Load the term associated with this Tripal Content type.
  538. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  539. $term = reset($term);
  540. // If it's the term you are interested in then suggest a format.
  541. if ($term->name == 'organism') {
  542. // To suggest a format, add an element to the array with a format & weight key.
  543. $format[] = array(
  544. // This is the format/pattern you suggest be used to determine the title of organism pages.
  545. 'format' => '[organism__genus] [organism__species]',
  546. // The weight/priority of your suggestion.
  547. 'weight' => -5
  548. );
  549. }
  550. // Say you know that in your particular site, all 'names' are required
  551. // and you want to only use the human-readable name:
  552. //---------------------------------------------------------------------------
  553. $name_field = preg_grep('/__name]$/', array_keys($available_tokens));
  554. $name_field = reset($name_field);
  555. if (is_string($name_field)) {
  556. $format[] = array(
  557. 'format' => $name_field,
  558. 'weight' => -2,
  559. );
  560. }
  561. return $format;
  562. }
  563. /**
  564. * Returns an array of tokens based on Tripal Entity Fields.
  565. *
  566. * @param TripalBundle $entity
  567. * The bundle entity for which you want tokens.
  568. * @return
  569. * An array of tokens where the key is the machine_name of the token.
  570. */
  571. function tripal_get_tokens($entity, $options = array()) {
  572. $tokens = array();
  573. // Set default options.
  574. $options['required only'] = (isset($options['required only'])) ? $options['required only'] : FALSE;
  575. $options['include id'] = (isset($options['include id'])) ? $options['include id'] : TRUE;
  576. if ($options['include id']) {
  577. $token = '[TripalBundle__bundle_id]';
  578. $tokens[$token] = array(
  579. 'label' => 'Bundle ID',
  580. 'description' => 'The unique identifier for this Tripal Content Type.',
  581. 'token' => $token,
  582. 'field_name' => NULL,
  583. 'required' => TRUE
  584. );
  585. $token = '[TripalEntity__entity_id]';
  586. $tokens[$token] = array(
  587. 'label' => 'Content/Entity ID',
  588. 'description' => 'The unique identifier for an individual piece of Tripal Content.',
  589. 'token' => $token,
  590. 'field_name' => NULL,
  591. 'required' => TRUE
  592. );
  593. }
  594. $fields = field_info_instances('TripalEntity', $entity->name);
  595. foreach ($fields as $f) {
  596. // Build the token from the field information.
  597. $token = '[' . $f['field_name'] . ']';
  598. $current_token = array(
  599. 'label' => $f['label'],
  600. 'description' => $f['description'],
  601. 'token' => $token,
  602. 'field_name' => $f['field_name'],
  603. 'required' => $f['required']
  604. );
  605. // If the required only option is set then we only want to add
  606. // required fields to the token list.
  607. if ($options['required only'] AND $current_token['required']) {
  608. $tokens[$token] = $current_token;
  609. }
  610. // If the required only option is not set then add everything.
  611. elseif (!$options['required only']) {
  612. $tokens[$token] = $current_token;
  613. }
  614. }
  615. return $tokens;
  616. }
  617. /**
  618. * Replace all Tripal Tokens in a given string.
  619. *
  620. * NOTE: If there is no value for a token then the token is removed.
  621. *
  622. * @param string $string
  623. * The string containing tokens.
  624. * @param TripalEntity $entity
  625. * The entity with field values used to find values of tokens.
  626. * @param TripalBundle $bundle_entity
  627. * The bundle enitity containing special values sometimes needed for token replacement.
  628. *
  629. * @return
  630. * The string will all tokens replaced with values.
  631. */
  632. function tripal_replace_tokens($string, $entity, $bundle_entity = NULL) {
  633. // Determine which tokens were used in the format string
  634. if (preg_match_all('/\[\w+\]/', $string, $matches)) {
  635. $used_tokens = $matches[0];
  636. foreach($used_tokens as $token) {
  637. $field = str_replace(array('.','[',']'),array('__','',''),$token);
  638. $value = '';
  639. if (isset($entity->{$field})) {
  640. // Render the value from the field.
  641. // First get the items to be rendered.
  642. $field_value = field_get_items('TripalEntity', $entity, $field);
  643. if (isset($field_value[0])) {
  644. // Then get a render array for just the value of the first item (no markup).
  645. $field_render_arr = field_view_value('TripalEntity', $entity, $field, $field_value[0]);
  646. // Finally render the value from the render array.
  647. $value = render($field_render_arr);
  648. }
  649. }
  650. elseif ($field === 'TripalBundle__bundle_id') {
  651. // Load the bundle entity if we weren't given it.
  652. if (!$bundle_entity) {
  653. $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
  654. }
  655. // This token should be the id of the TripalBundle.
  656. $value = $bundle_entity->id;
  657. }
  658. elseif ($field === 'TripalEntity__entity_id') {
  659. // This token should be the id of the TripalEntity.
  660. $value = $entity->id;
  661. }
  662. $string = str_replace($token, $value, $string);
  663. }
  664. }
  665. return $string;
  666. }
  667. /**
  668. * Formats the tokens for display.
  669. *
  670. * @param array $tokens
  671. * A list of tokens generated via tripal_get_tokens().
  672. * @return
  673. * Rendered output describing the available tokens.
  674. */
  675. function theme_token_list($tokens) {
  676. $header = array('Token', 'Name', 'Description');
  677. $rows = array();
  678. foreach ($tokens as $details) {
  679. $rows[] = array(
  680. $details['token'],
  681. $details['label'],
  682. $details['description'],
  683. );
  684. }
  685. return theme('table', array('header' => $header, 'rows' => $rows));
  686. }
  687. /**
  688. * @section
  689. * Vocabulary Hooks.
  690. */
  691. /**
  692. * A hook for specifying information about the data store for vocabularies.
  693. *
  694. * The storage backend for controlled vocabularies has traditionally been
  695. * the Chado CV term tables. However, Tripal v3.0 introduces APIs for supporting
  696. * other backends. Therefore, this function indicates to Tripal which
  697. * data stores are capable of providing support for terms.
  698. *
  699. * @return
  700. * An array describing the storage backends implemented by the module. The
  701. * keys are storage backend names. To avoid name clashes, storage
  702. * backend names should be prefixed with the name of the module that
  703. * exposes them. The values are arrays describing the storage backend,
  704. * with the following key/value pairs:
  705. *
  706. * label: The human-readable name of the storage backend.
  707. * module: The name of the module providing the support for this backend.
  708. * description: A short description for the storage backend.
  709. * settings: An array whose keys are the names of the settings available for
  710. * the storage backend, and whose values are the default values for
  711. * those settings.
  712. */
  713. function hook_vocab_storage_info() {
  714. return array(
  715. 'term_chado_storage' => array(
  716. 'label' => t('Chado storage'),
  717. 'description' => t('Integrates terms stored in the local Chado database with Tripal entities.'),
  718. 'settings' => array(),
  719. ),
  720. );
  721. }
  722. /**
  723. * Creates a form for specifying a term for TripalEntity creation.
  724. *
  725. * This hook allows the module that implements a vocabulary storage backend
  726. * to provide the form necessary to select a term that will then be used for
  727. * creating a new TripalEntity type. Tripal will expect that a 'namespace' and
  728. * 'accession' are in the $form_state['storage'] array. The 'namespace' and
  729. * must be the abbreviated uppercase namespace for the vocabulary (e.g. 'RO',
  730. * 'SO', 'PATO', etc.). The 'accession' must be the unique term ID (or
  731. * accession) for the term in the vocabulary.
  732. *
  733. * @param $form
  734. * @param $form_state
  735. *
  736. * @return
  737. * A form object.
  738. */
  739. function hook_vocab_select_term_form(&$form, &$form_state) {
  740. return $form;
  741. }
  742. /**
  743. * Validates the hook_vocab_select_term_form().
  744. *
  745. * @param $name
  746. */
  747. function hook_vocab_select_term_form_validate($form, &$form_state) {
  748. }
  749. /**
  750. * Provides a form for importing vocabularies and their terms.
  751. *
  752. * Tripal allows for vocabularies to be stored separately from the biological
  753. * data. This hook allows the default term storage backend to provide an
  754. * approprite form for importing ontologies (either in OBO or OWL format).
  755. *
  756. * @param $form
  757. * @param $form_state
  758. *
  759. */
  760. function hook_vocab_import_form($form, &$form_state) {
  761. return $form;
  762. }
  763. function hook_vocab_import_form_validate($form, &$form_state) {
  764. }
  765. function hook_vocab_import_form_submit($form, &$form_state) {
  766. }
  767. /**
  768. * Hook used by the default term storage backend to provide details for a term.
  769. *
  770. * This hook is called by the tripal_entity module to retrieve information
  771. * about the term from the storage backend. It must return an array with
  772. * a set of keys.
  773. *
  774. * @param $namespace
  775. * The namespace of the vocabulary in which the term is found.
  776. * @param $accession
  777. * The unique identifier (accession) for this term.
  778. *
  779. * @return
  780. * An array with at least the following keys:
  781. * namespace : The namespace of the vocabulary.
  782. * accession : The name unique ID of the term.
  783. * url : The URL for the term.
  784. * name : The name of the term.
  785. * definition : The term's description.
  786. * any other keys may be added as desired. Returns NULL if the term
  787. * cannot be found.
  788. */
  789. function hook_vocab_get_term($namespace, $accession) {
  790. // See the tripal_chado_vocab_get_term() function for an example.
  791. }
  792. /**
  793. * Retrieves full information about a vocabulary term.
  794. *
  795. * Vocabularies are stored in a database backend. Tripal has no requirements
  796. * for how terms are stored. By default, the tripal_chado modules provides
  797. * storage for vocabularies and terms. This function will call the
  798. * hook_vocab_get_term() function for the database backend that is housing the
  799. * vocabularies and allow it to return the details about the term.
  800. *
  801. * @param $namespace
  802. * The namespace of the vocabulary in which the term is found.
  803. * @param $accession
  804. * The unique identifier (accession) for this term.
  805. *
  806. * @return
  807. * An array with at least the following keys:
  808. * namespace : The namespace of the vocabulary.
  809. * accession : The name unique ID of the term.
  810. * url : The URL for the term.
  811. * name : The name of the term.
  812. * definition : The term's description.
  813. * any other keys may be added as desired. Returns NULL if the term
  814. * cannot be found.
  815. */
  816. function tripal_get_term_details($namespace, $accession) {
  817. // TODO: we need some sort of administrative interface that lets the user
  818. // switch to the desired vocabulary type. For now, we'll just use the
  819. // first one in the list.
  820. $stores = module_invoke_all('vocab_storage_info');
  821. if (is_array($stores) and count($stores) > 0) {
  822. $keys = array_keys($stores);
  823. $module = $stores[$keys[0]]['module'];
  824. $function = $module . '_vocab_get_term';
  825. if (function_exists($function)) {
  826. return $function($vocab->namespace, $this->accession);
  827. }
  828. }
  829. }