tripal.entities.api.inc 34 KB

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