tripal_example.install 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * @file
  4. * Installation of the example module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. *
  9. * Perform actions when the module is disabled by the site administrator
  10. *
  11. * @ingroup tripal_example
  12. */
  13. function tripal_example_disable() {
  14. // EXPLANATION: If you are using Drupal Views you want to ensure
  15. // that any default views that your module provides are disabled
  16. // when the module is disabled. Default views are specified in the
  17. // [module name].views.default.inc file. The following code will disable
  18. // these views. If your module does not create any default views you
  19. // can remove the following code.
  20. // Disable all default views provided by this module
  21. require_once("tripal_example.views_default.inc");
  22. $views = tripal_example_views_default_views();
  23. foreach (array_keys($views) as $view_name) {
  24. tripal_views_admin_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  25. }
  26. }
  27. /**
  28. * Implements hook_requirements().
  29. *
  30. * Performs check to see if all required dependencies are met. Drupal will
  31. * automatically check for module dependencies but here you can check for
  32. * other requirements.
  33. *
  34. * @ingroup tripal_example
  35. */
  36. function tripal_example_requirements($phase) {
  37. $requirements = array();
  38. if ($phase == 'install') {
  39. // EXPLANATION: It is essential that Chado be installed for almost all
  40. // Tripal modules. Therefore, the following code checks to ensure Chado
  41. // is installed and available. If your module does not require that
  42. // Chado be installed, you can remove the following check.
  43. // make sure chado is installed
  44. if (!$GLOBALS["chado_is_installed"]) {
  45. $requirements ['tripal_example'] = array(
  46. 'title' => "tripal_example",
  47. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  48. 'severity' => REQUIREMENT_ERROR,
  49. );
  50. }
  51. }
  52. return $requirements;
  53. }
  54. /**
  55. * Implements hook_install().
  56. *
  57. * Performs actions when the modules is first installed.
  58. *
  59. * @ingroup tripal_example
  60. */
  61. function tripal_example_install() {
  62. // EXPLANATION: If your module will making data publicly available for
  63. // download or use by the site you can create the directory using the
  64. // tripal_create_files_dir() function. This will create a directory
  65. // in the public access directory which will typcially be in
  66. // sites/default/files/tripal/[module name]/
  67. // create the module's data directory
  68. tripal_create_files_dir('tripal_example');
  69. // EXPLANATION: Here is a good place to add any materialized views,
  70. // controlled vocabularies CV, databases or CV terms needed by your module.
  71. // To keep this module code short, create functions to do each of those
  72. // tasks
  73. // add any materialized view
  74. tripal_example_add_mviews();
  75. // add any external databases used by the example module.
  76. tripal_example_add_dbs();
  77. // add any controlled vocabularies used by the example module. You may need
  78. // to add a vocabulary if you to set it as default (see next lines of code).
  79. // For example, the Sequence Ontology (SO) is used by the feature module as the
  80. // default vocabulary for the feature type_id field. But, that vocabulary
  81. // does not yet exist in Chado until after the SO is loaded using the
  82. // Tripal OBO loader. But, we can add it here as a placeholder so that we can
  83. // then set it as a default vocabulary (see below).
  84. tripal_example_add_cvs();
  85. // EXPLANATION: Many tables in Chado have a 'type_id' column which allows for
  86. // association of controlled vocabulries to describe the record. Chado
  87. // places no restrictions on which vocabularies can be used, but Tripal can
  88. // be instructed to provide a default vocabulary for any given field. For
  89. // example, the feature.type_id column will typically use the Sequence Ontology
  90. // In that case, we can use the tripal_set_default_cv() function to specify
  91. // the Sequence Ontology (sequence) as the default vocabulary.
  92. tripal_set_default_cv('example', 'type_id', 'sequence');
  93. tripal_set_default_cv('exampleprop', 'type_id', 'example_property');
  94. tripal_set_default_cv('example_relationship', 'type_id', 'example_relationship');
  95. }
  96. /**
  97. * Implements hook_uninstall().
  98. *
  99. * Performs actions when the modules is uninstalled.
  100. *
  101. * @ingroup tripal_example
  102. */
  103. function tripal_example_uninstall() {
  104. }
  105. /**
  106. * Implementation of hook_schema().
  107. *
  108. * Provides a list of tables to be created inside of the Drupal schema
  109. * (the 'public' schema by default). It uses the Drupal Schema API
  110. * array structure to define the table, its indexes and constraints.
  111. *
  112. * Schema API documentation is here:
  113. * https://api.drupal.org/api/drupal/includes%21database%21schema.inc/group/schemaapi/7
  114. *
  115. * @ingroup tripal_example
  116. */
  117. function tripal_example_schema() {
  118. // EXPLANATION: If your module creates a node type for data in the Chado
  119. // database then you probably need to link Drupal nodes with a respective
  120. // ID in the Chado table. The following is an example array for a table
  121. // that will link the 'chado_example' node type (created by this example
  122. // module) with a record in the fake Chado example table. This table
  123. // will link the 'nid' of the node with the 'example_id' of the eample
  124. // record.
  125. $schema['chado_example'] = array(
  126. 'fields' => array(
  127. 'vid' => array(
  128. 'type' => 'int',
  129. 'unsigned' => TRUE,
  130. 'not null' => TRUE,
  131. 'default' => 0
  132. ),
  133. 'nid' => array(
  134. 'type' => 'int',
  135. 'unsigned' => TRUE,
  136. 'not null' => TRUE,
  137. 'default' => 0
  138. ),
  139. 'example_id' => array(
  140. 'type' => 'int',
  141. 'not null' => TRUE,
  142. 'default' => 0
  143. ),
  144. 'sync_date' => array(
  145. 'type' => 'int',
  146. 'not null' => FALSE,
  147. 'description' => 'UNIX integer sync date/time'
  148. ),
  149. ),
  150. 'indexes' => array(
  151. 'chado_example_idx1' => array('example_id')
  152. ),
  153. 'unique keys' => array(
  154. 'chado_example_uq1' => array('nid', 'vid'),
  155. 'chado_example_uq2' => array('vid')
  156. ),
  157. 'primary key' => array('nid'),
  158. );
  159. return $schema;
  160. };
  161. /**
  162. * Creates a materialized view that stores the type & number of examples per organism
  163. *
  164. * @ingroup tripal_example
  165. */
  166. function tripal_example_add_mviews() {
  167. // EXPLANATION: use the tripal_add_mview() function to add a materialized
  168. // view needed by your module. If you have more than one materialized view
  169. // it is best to create a single function for each one and call each
  170. // function here. Otherwise this function can become quite long.
  171. }
  172. /**
  173. * Add cvs related to publications
  174. *
  175. * @ingroup tripal_pub
  176. */
  177. function tripal_example_add_cvs() {
  178. // EXPLANATION: use the tripal_cv_add_cv() function to add any
  179. // controlled vocabularies needed by your module. If the vocabulary already
  180. // exists then the function will gracefully return.
  181. }
  182. /**
  183. * This is the required update for tripal_example.
  184. */
  185. function tripal_example_update_7200() {
  186. // EXPLANATION: as you create new releases of your module you may find that
  187. // tables your module created, or data may need to be adjusted. This function
  188. // allows you to do that. This function is executed using the
  189. // http://[your site]/update.php URL or using the drush command 'updatedb'.
  190. // This function should be named according to the instructions provided here:
  191. // https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_update_N/7
  192. //
  193. // It is best not to use Tripal API calls inside of this function because an
  194. // upgarde from Drupal 6 to Drupal 7 requires that all modules be disabled
  195. // which means the Tripal API is not available. This is an unfortunate
  196. // requirement, but will prevent errors during a major upgrade.
  197. // it is good to wrap any database changes inside of a try catch block:
  198. try {
  199. // perform database changes
  200. }
  201. catch (\PDOException $e) {
  202. $error = $e->getMessage();
  203. throw new DrupalUpdateException('Could not apply updates: '. $error);
  204. }
  205. }
  206. /**
  207. * Implementation of hook_update_dependencies(). It specifies a list of
  208. * other modules whose updates must be run prior to this one.
  209. */
  210. function tripal_example_update_dependencies() {
  211. $dependencies = array();
  212. // EXPLANATION: here we can specify which modules must be updated prior
  213. // to applying the updates in this module. This is useful because it
  214. // prevents updates from being executed out of order. The following
  215. // example code shows that the 'tripal_example' module update number 7200
  216. // must be executed after the 'tripal_cv' module's 7200 update.
  217. $dependencies['tripal_example'][7200] = array(
  218. 'tripal_cv' => 7200
  219. );
  220. return $dependencies;
  221. }