tripal_entities.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php
  2. /**
  3. * @file
  4. * Install for a tripal data entity - creates the base table for our entity.
  5. */
  6. function tripal_entities_install() {
  7. // add any external databases used by the file module.
  8. // tripal_entities_add_dbs();
  9. // add any controlled vocabularies used by the file module.
  10. // tripal_entities_add_cvs();
  11. // add any controlled vocabulary terms
  12. // tripal_entities_add_cvterms();
  13. // add any custom tables. For this case we will add an 'file' table to the
  14. // chado schema
  15. tripal_entities_add_custom_tables();
  16. }
  17. /**
  18. *
  19. */
  20. function tripal_entities_add_custom_tables() {
  21. tripal_entities_add_tripal_entity_type_table();
  22. tripal_entities_add_tripal_entity_type_source_table();
  23. tripal_entities_add_tripal_entity_bundle_table();
  24. tripal_entities_add_tripal_entity_bundle_source_table();
  25. tripal_entities_add_tripal_entity_relationship_table();
  26. }
  27. /**
  28. *
  29. */
  30. function tripal_entities_add_tripal_entity_type_table() {
  31. $schema = array (
  32. 'table' => 'tripal_entity_type',
  33. 'fields' => array (
  34. 'vocab_id' => array(
  35. 'type' => 'serial',
  36. 'not null' => TRUE
  37. ),
  38. 'cv_id' => array (
  39. 'type' => 'int',
  40. 'not null' => TRUE
  41. ),
  42. 'db_id' => array (
  43. 'type' => 'int',
  44. 'not null' => TRUE
  45. ),
  46. 'publish' => array (
  47. 'type' => 'int',
  48. 'not null' => TRUE,
  49. 'default' => 0
  50. ),
  51. ),
  52. 'primary key' => array (
  53. 0 => 'vocab_id'
  54. ),
  55. 'foreign keys' => array (
  56. 'cv' => array (
  57. 'table' => 'cv',
  58. 'columns' => array (
  59. 'cv_id' => 'cv_id'
  60. )
  61. ),
  62. 'db' => array (
  63. 'table' => 'db',
  64. 'columns' => array (
  65. 'db_id' => 'db_id'
  66. )
  67. ),
  68. ),
  69. 'unique keys' => array (
  70. 'vocab_cvdb' => array (
  71. 'cv_id', 'db_id'
  72. ),
  73. ),
  74. 'indexes' => array(
  75. 'tripal_entity_type_cv_id' => array('cv_id'),
  76. 'tripal_entity_type_db_id' => array('db_id'),
  77. )
  78. );
  79. chado_create_custom_table('tripal_entity_type', $schema, TRUE);
  80. }
  81. /**
  82. *
  83. */
  84. function tripal_entities_add_tripal_entity_bundle_table() {
  85. $schema = array (
  86. 'table' => 'tripal_entity_bundle',
  87. 'fields' => array (
  88. 'bundle_id' => array(
  89. 'type' => 'serial',
  90. 'not null' => TRUE
  91. ),
  92. 'vocab_id' => array (
  93. 'type' => 'int',
  94. 'not null' => TRUE
  95. ),
  96. 'cvterm_id' => array (
  97. 'type' => 'int',
  98. 'not null' => TRUE
  99. ),
  100. 'publish' => array (
  101. 'type' => 'int',
  102. 'not null' => TRUE,
  103. 'default' => 0
  104. ),
  105. ),
  106. 'primary key' => array (
  107. 0 => 'bundle_id'
  108. ),
  109. 'foreign keys' => array (
  110. 'cvterm' => array (
  111. 'table' => 'cvterm',
  112. 'columns' => array (
  113. 'cvterm_id' => 'cvterm_id'
  114. )
  115. ),
  116. 'tripal_entity_type' => array (
  117. 'table' => 'tripal_entity_type',
  118. 'columns' => array (
  119. 'vocab_id' => 'vocab_id'
  120. )
  121. ),
  122. ),
  123. 'unique keys' => array (
  124. 'tripal_entity_bundle_unq' => array (
  125. 'vocab_id', 'cvterm_id'
  126. ),
  127. ),
  128. 'indexes' => array(
  129. 'tripal_entity_bundle_vocab_id' => array('vocab_id'),
  130. 'tripal_entity_bundle_cvterm_id' => array('cvterm_id'),
  131. ),
  132. );
  133. chado_create_custom_table('tripal_entity_bundle', $schema, TRUE);
  134. }
  135. /**
  136. *
  137. */
  138. function tripal_entities_add_tripal_entity_type_source_table(){
  139. $schema = array (
  140. 'table' => 'tripal_entity_type_source',
  141. 'fields' => array (
  142. 'vocab_table_id' => array(
  143. 'type' => 'serial',
  144. 'not null' => TRUE
  145. ),
  146. 'vocab_id' => array (
  147. 'type' => 'int',
  148. 'not null' => TRUE
  149. ),
  150. 'data_table' => array (
  151. 'type' => 'varchar',
  152. 'length' => 128,
  153. 'not null' => TRUE
  154. ),
  155. 'type_table' => array (
  156. 'type' => 'varchar',
  157. 'length' => 128,
  158. 'not null' => TRUE
  159. ),
  160. 'field' => array (
  161. 'type' => 'varchar',
  162. 'length' => 128,
  163. 'not null' => TRUE
  164. ),
  165. ),
  166. 'primary key' => array (
  167. 0 => 'vocab_table_id'
  168. ),
  169. 'foreign keys' => array (
  170. 'tripal_entity_type' => array (
  171. 'table' => 'tripal_entity_type',
  172. 'columns' => array (
  173. 'vocab_id' => 'vocab_id'
  174. ),
  175. ),
  176. ),
  177. 'unique keys' => array (
  178. 'tripal_entity_type_ridbase' => array (
  179. 'vocab_id', 'data_table'
  180. ),
  181. ),
  182. 'indexes' => array(
  183. 'tripal_entity_type_vocab_id' => array('vocab_id'),
  184. 'tripal_entity_type_data_table' => array('data_table'),
  185. 'tripal_entity_type_type_table' => array('type_table'),
  186. ),
  187. );
  188. chado_create_custom_table('tripal_entity_type_source', $schema, TRUE);
  189. }
  190. /**
  191. *
  192. */
  193. function tripal_entities_add_tripal_entity_bundle_source_table(){
  194. $schema = array (
  195. 'table' => 'tripal_entity_bundle_source',
  196. 'fields' => array (
  197. 'bundle_source_id' => array(
  198. 'type' => 'serial',
  199. 'not null' => TRUE
  200. ),
  201. 'bundle_id' => array (
  202. 'type' => 'int',
  203. 'not null' => TRUE
  204. ),
  205. 'data_table' => array (
  206. 'type' => 'varchar',
  207. 'length' => 128,
  208. 'not null' => TRUE
  209. ),
  210. 'type_table' => array (
  211. 'type' => 'varchar',
  212. 'length' => 128,
  213. 'not null' => TRUE
  214. ),
  215. 'field' => array (
  216. 'type' => 'varchar',
  217. 'length' => 128,
  218. 'not null' => TRUE
  219. ),
  220. ),
  221. 'primary key' => array (
  222. 0 => 'bundle_source_id'
  223. ),
  224. 'foreign keys' => array (
  225. 'tripal_entity_bundle' => array (
  226. 'table' => 'tripal_entity_bundle',
  227. 'columns' => array (
  228. 'bundle_id' => 'bundle_id'
  229. ),
  230. ),
  231. ),
  232. 'unique keys' => array (
  233. 'tripal_entity_bundle_source_ridbase' => array (
  234. 'bundle_id', 'type_table', 'field'
  235. ),
  236. ),
  237. 'indexes' => array(
  238. 'tripal_entity_bundle_source_bundle_id' => array('bundle_id'),
  239. ),
  240. );
  241. chado_create_custom_table('tripal_entity_bundle_source', $schema, TRUE);
  242. }
  243. /**
  244. *
  245. */
  246. function tripal_entities_add_tripal_entity_relationship_table(){
  247. $schema = array (
  248. 'table' => 'tripal_entity_relationship',
  249. 'fields' => array (
  250. 'relationship_id' => array(
  251. 'type' => 'serial',
  252. 'not null' => TRUE
  253. ),
  254. 'subject_id' => array (
  255. 'type' => 'int',
  256. 'not null' => TRUE
  257. ),
  258. 'type_id' => array (
  259. 'type' => 'int',
  260. 'not null' => TRUE
  261. ),
  262. 'object_id' => array (
  263. 'type' => 'int',
  264. 'not null' => FALSE
  265. ),
  266. 'fieldname' => array(
  267. 'type' => 'varchar',
  268. 'length' => 128,
  269. 'not null' => FALSE,
  270. )
  271. ),
  272. 'primary key' => array (
  273. 0 => 'relationship_id'
  274. ),
  275. 'foreign keys' => array (
  276. 'tripal_entity_bundle' => array (
  277. 'table' => 'tripal_entity_bundle',
  278. 'columns' => array (
  279. 'subject_id' => 'bundle_id',
  280. 'object_id' => 'bundle_id',
  281. ),
  282. ),
  283. ),
  284. 'unique keys' => array (
  285. 'tripal_entity_relationship_unq' => array (
  286. 'subject_id', 'type_id', 'object_id'
  287. ),
  288. ),
  289. 'indexes' => array(
  290. 'tripal_entity_relationship_subject_id' => array('subject_id'),
  291. 'tripal_entity_relationship_object_id' => array('object_id'),
  292. 'tripal_entity_relationship_type_id' => array('type_id'),
  293. ),
  294. );
  295. chado_create_custom_table('tripal_entity_relationship', $schema, TRUE);
  296. }
  297. /**
  298. * Implements hook_schema().
  299. *
  300. * @ingroup entity_example
  301. */
  302. function tripal_entities_schema() {
  303. $schema['tripal_data'] = array(
  304. 'description' => 'The base table for Tripal Vocabulary-based entities.',
  305. 'fields' => array(
  306. 'entity_id' => array(
  307. 'description' => 'The primary identifier for a vocabulary entity.',
  308. 'type' => 'serial',
  309. 'unsigned' => TRUE,
  310. 'not null' => TRUE,
  311. ),
  312. 'type' => array(
  313. 'description' => 'The type of entity. This should be an official term ID.',
  314. 'type' => 'varchar',
  315. 'length' => 64,
  316. 'not null' => TRUE,
  317. 'default' => '',
  318. ),
  319. 'cvterm_id' => array(
  320. 'description' => 'The cvterm_id for the type of entity. This cvterm_id should match a record in the Chado cvterm table.',
  321. 'type' => 'varchar',
  322. 'length' => 32,
  323. 'not null' => TRUE,
  324. 'default' => '',
  325. ),
  326. 'tablename' => array(
  327. 'description' => 'The Chado table that contains the record that this entity is associated with.',
  328. 'type' => 'varchar',
  329. 'length' => 128,
  330. 'not null' => TRUE,
  331. 'default' => ''
  332. ),
  333. 'record_id' => array(
  334. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  335. 'type' => 'int',
  336. 'not null' => TRUE,
  337. ),
  338. 'title' => array(
  339. 'description' => 'The title of this node, always treated as non-markup plain text.',
  340. 'type' => 'text',
  341. 'not null' => TRUE,
  342. 'default' => '',
  343. ),
  344. 'uid' => array(
  345. 'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
  346. 'type' => 'int',
  347. 'not null' => TRUE,
  348. 'default' => 0,
  349. ),
  350. 'status' => array(
  351. 'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
  352. 'type' => 'int',
  353. 'not null' => TRUE,
  354. 'default' => 1,
  355. ),
  356. 'created' => array(
  357. 'description' => 'The Unix timestamp when the node was created.',
  358. 'type' => 'int',
  359. 'not null' => TRUE,
  360. 'default' => 0,
  361. ),
  362. 'changed' => array(
  363. 'description' => 'The Unix timestamp when the node was most recently saved.',
  364. 'type' => 'int',
  365. 'not null' => TRUE,
  366. 'default' => 0,
  367. ),
  368. ),
  369. 'indexes' => array(
  370. 'entity_changed' => array('changed'),
  371. 'entity_created' => array('created'),
  372. 'tablename' => array('tablename'),
  373. 'record_id' => array('record_id'),
  374. 'tripal_record' => array('tablename', 'record_id'),
  375. 'type' => array('type'),
  376. 'cvterm_id' => array('cvterm_id'),
  377. 'uid' => array('uid'),
  378. ),
  379. 'unique keys' => array(
  380. 'record' => array('tablename', 'record_id'),
  381. ),
  382. 'primary key' => array('entity_id'),
  383. );
  384. $schema['tripal_data_type'] = array(
  385. 'description' => 'Stores information about defined tripal data types.',
  386. 'fields' => array(
  387. 'id' => array(
  388. 'type' => 'serial',
  389. 'not null' => TRUE,
  390. 'description' => 'Primary Key: Unique Chado data type identifier.',
  391. ),
  392. 'type' => array(
  393. 'description' => 'The machine-readable name of this tripal data type.',
  394. 'type' => 'varchar',
  395. 'length' => 255,
  396. 'not null' => TRUE,
  397. ),
  398. 'label' => array(
  399. 'description' => 'The human-readable name of this tripal data type.',
  400. 'type' => 'varchar',
  401. 'length' => 255,
  402. 'not null' => TRUE,
  403. 'default' => '',
  404. ),
  405. 'weight' => array(
  406. 'type' => 'int',
  407. 'not null' => TRUE,
  408. 'default' => 0,
  409. 'size' => 'tiny',
  410. 'description' => 'The weight of this tripal data type in relation to others.',
  411. ),
  412. 'data' => array(
  413. 'type' => 'text',
  414. 'not null' => FALSE,
  415. 'size' => 'big',
  416. 'serialize' => TRUE,
  417. 'description' => 'A serialized array of additional data related to this tripal data type.',
  418. ),
  419. ) + entity_exportable_schema_fields(),
  420. 'primary key' => array('id'),
  421. 'unique keys' => array(
  422. 'type' => array('type'),
  423. ),
  424. );
  425. return $schema;
  426. }
  427. /**
  428. * Implements hook_uninstall().
  429. *
  430. * At uninstall time we'll notify field.module that the entity was deleted
  431. * so that attached fields can be cleaned up.
  432. *
  433. * @ingroup entity_example
  434. */
  435. function tripal_entities_uninstall() {
  436. // TODO: make this dynamic (not hardcoded bundle).
  437. field_attach_delete_bundle('tripal_data', 'gene');
  438. }