profile2.install 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the profile module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function profile2_install() {
  10. // Add an initial profile type, but only if installed manually. In case the
  11. // module is installed via an installation profile, skip that.
  12. if (!drupal_installation_attempted()) {
  13. $type = entity_create('profile2_type', array(
  14. 'type' => 'main',
  15. 'label' => t('Main profile'),
  16. 'weight' => 0,
  17. 'data' => array('registration' => TRUE, 'use_page' => TRUE),
  18. ));
  19. $type->save();
  20. user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('edit own main profile', 'view own main profile'));
  21. drupal_set_message(t('A main profile type has been created and assigned to all users. Go to the <a href="!url">Profile types</a> page to add some fields or to configure further profile types.', array('!url' => url('admin/structure/profiles'))));
  22. }
  23. }
  24. /**
  25. * Implements hook_schema().
  26. */
  27. function profile2_schema() {
  28. $schema['profile'] = array(
  29. 'description' => 'Stores profile items.',
  30. 'fields' => array(
  31. 'pid' => array(
  32. 'type' => 'serial',
  33. 'not null' => TRUE,
  34. 'description' => 'Primary Key: Unique profile item ID.',
  35. ),
  36. 'type' => array(
  37. 'description' => 'The {profile_type}.type of this profile.',
  38. 'type' => 'varchar',
  39. 'length' => 32,
  40. 'not null' => TRUE,
  41. 'default' => '',
  42. ),
  43. 'uid' => array(
  44. 'type' => 'int',
  45. 'unsigned' => TRUE,
  46. 'not null' => FALSE,
  47. 'default' => NULL,
  48. 'description' => "The {users}.uid of the associated user.",
  49. ),
  50. 'label' => array(
  51. 'description' => 'A human-readable label for this profile.',
  52. 'type' => 'varchar',
  53. 'length' => 255,
  54. 'not null' => TRUE,
  55. 'default' => '',
  56. ),
  57. 'created' => array(
  58. 'description' => 'The Unix timestamp when the profile was created.',
  59. 'type' => 'int',
  60. 'not null' => FALSE,
  61. ),
  62. 'changed' => array(
  63. 'description' => 'The Unix timestamp when the profile was most recently saved.',
  64. 'type' => 'int',
  65. 'not null' => FALSE,
  66. ),
  67. ),
  68. 'indexes' => array(
  69. 'uid' => array('uid'),
  70. ),
  71. 'foreign keys' => array(
  72. 'uid' => array(
  73. 'table' => 'users',
  74. 'columns' => array('uid' => 'uid'),
  75. ),
  76. 'type' => array(
  77. 'table' => 'profile_type',
  78. 'columns' => array('type' => 'type'),
  79. ),
  80. ),
  81. 'primary key' => array('pid'),
  82. );
  83. $schema['profile_type'] = array(
  84. 'description' => 'Stores information about all defined profile types.',
  85. 'fields' => array(
  86. 'id' => array(
  87. 'type' => 'serial',
  88. 'not null' => TRUE,
  89. 'description' => 'Primary Key: Unique profile type ID.',
  90. ),
  91. 'type' => array(
  92. 'description' => 'The machine-readable name of this profile type.',
  93. 'type' => 'varchar',
  94. 'length' => 32,
  95. 'not null' => TRUE,
  96. ),
  97. 'label' => array(
  98. 'description' => 'The human-readable name of this profile type.',
  99. 'type' => 'varchar',
  100. 'length' => 255,
  101. 'not null' => TRUE,
  102. 'default' => '',
  103. ),
  104. 'weight' => array(
  105. 'type' => 'int',
  106. 'not null' => TRUE,
  107. 'default' => 0,
  108. 'size' => 'tiny',
  109. 'description' => 'The weight of this profile type in relation to others.',
  110. ),
  111. 'data' => array(
  112. 'type' => 'text',
  113. 'not null' => FALSE,
  114. 'size' => 'big',
  115. 'serialize' => TRUE,
  116. 'description' => 'A serialized array of additional data related to this profile type.',
  117. ),
  118. 'status' => array(
  119. 'type' => 'int',
  120. 'not null' => TRUE,
  121. // Set the default to ENTITY_CUSTOM without using the constant as it is
  122. // not safe to use it at this point.
  123. 'default' => 0x01,
  124. 'size' => 'tiny',
  125. 'description' => 'The exportable status of the entity.',
  126. ),
  127. 'module' => array(
  128. 'description' => 'The name of the providing module if the entity has been defined in code.',
  129. 'type' => 'varchar',
  130. 'length' => 255,
  131. 'not null' => FALSE,
  132. ),
  133. ),
  134. 'primary key' => array('id'),
  135. 'unique keys' => array(
  136. 'type' => array('type'),
  137. ),
  138. );
  139. return $schema;
  140. }
  141. /**
  142. * Add in the exportable entity db columns as required by the entity API.
  143. */
  144. function profile2_update_7100() {
  145. db_add_field('profile_type', 'status', array(
  146. 'type' => 'int',
  147. 'not null' => TRUE,
  148. 'default' => ENTITY_CUSTOM,
  149. 'size' => 'tiny',
  150. 'description' => 'The exportable status of the entity.',
  151. ));
  152. db_add_field('profile_type', 'module', array(
  153. 'description' => 'The name of the providing module if the entity has been defined in code.',
  154. 'type' => 'varchar',
  155. 'length' => 255,
  156. 'not null' => FALSE,
  157. ));
  158. }
  159. /**
  160. * Add a label column to profiles.
  161. */
  162. function profile2_update_7101() {
  163. db_add_field('profile', 'label', array(
  164. 'description' => 'A human-readable label for this profile.',
  165. 'type' => 'varchar',
  166. 'length' => 255,
  167. 'not null' => TRUE,
  168. 'default' => '',
  169. ));
  170. $types = db_select('profile_type', 'p')
  171. ->fields('p')
  172. ->execute()
  173. ->fetchAllAssoc('type');
  174. // Initialize with the type label.
  175. foreach ($types as $type_name => $type) {
  176. db_update('profile')
  177. ->fields(array(
  178. 'label' => $type->label,
  179. ))
  180. ->condition('type', $type_name)
  181. ->execute();
  182. }
  183. }
  184. /**
  185. * Add a created and a changed column to profiles.
  186. */
  187. function profile2_update_7102() {
  188. db_add_field('profile', 'created', array(
  189. 'description' => 'The Unix timestamp when the profile was created.',
  190. 'type' => 'int',
  191. 'not null' => FALSE,
  192. ));
  193. db_add_field('profile', 'changed', array(
  194. 'description' => 'The Unix timestamp when the profile was most recently saved.',
  195. 'type' => 'int',
  196. 'not null' => FALSE,
  197. ));
  198. }