profile2.install 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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),
  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. 'unique keys' => array(
  82. 'user_profile_type' => array('type', 'uid'),
  83. ),
  84. 'primary key' => array('pid'),
  85. );
  86. $schema['profile_type'] = array(
  87. 'description' => 'Stores information about all defined profile types.',
  88. 'fields' => array(
  89. 'id' => array(
  90. 'type' => 'serial',
  91. 'not null' => TRUE,
  92. 'description' => 'Primary Key: Unique profile type ID.',
  93. ),
  94. 'type' => array(
  95. 'description' => 'The machine-readable name of this profile type.',
  96. 'type' => 'varchar',
  97. 'length' => 32,
  98. 'not null' => TRUE,
  99. ),
  100. 'label' => array(
  101. 'description' => 'The human-readable name of this profile type.',
  102. 'type' => 'varchar',
  103. 'length' => 255,
  104. 'not null' => TRUE,
  105. 'default' => '',
  106. ),
  107. 'weight' => array(
  108. 'type' => 'int',
  109. 'not null' => TRUE,
  110. 'default' => 0,
  111. 'size' => 'tiny',
  112. 'description' => 'The weight of this profile type in relation to others.',
  113. ),
  114. 'data' => array(
  115. 'type' => 'text',
  116. 'not null' => FALSE,
  117. 'size' => 'big',
  118. 'serialize' => TRUE,
  119. 'description' => 'A serialized array of additional data related to this profile type.',
  120. ),
  121. 'status' => array(
  122. 'type' => 'int',
  123. 'not null' => TRUE,
  124. // Set the default to ENTITY_CUSTOM without using the constant as it is
  125. // not safe to use it at this point.
  126. 'default' => 0x01,
  127. 'size' => 'tiny',
  128. 'description' => 'The exportable status of the entity.',
  129. ),
  130. 'module' => array(
  131. 'description' => 'The name of the providing module if the entity has been defined in code.',
  132. 'type' => 'varchar',
  133. 'length' => 255,
  134. 'not null' => FALSE,
  135. ),
  136. ),
  137. 'primary key' => array('id'),
  138. 'unique keys' => array(
  139. 'type' => array('type'),
  140. ),
  141. );
  142. return $schema;
  143. }
  144. /**
  145. * Implements hook_uninstall()
  146. */
  147. function profile2_uninstall() {
  148. // Select all available profile2 bundles from the database directly, instead
  149. // of entity_load() call. See https://drupal.org/node/1330598 for details.
  150. $types = db_select('profile_type', 'p')
  151. ->fields('p')
  152. ->execute()
  153. ->fetchAllAssoc('type');
  154. foreach ($types as $name => $type) {
  155. field_attach_delete_bundle('profile2', $name);
  156. }
  157. }
  158. /**
  159. * Add in the exportable entity db columns as required by the entity API.
  160. */
  161. function profile2_update_7100() {
  162. db_add_field('profile_type', 'status', array(
  163. 'type' => 'int',
  164. 'not null' => TRUE,
  165. 'default' => ENTITY_CUSTOM,
  166. 'size' => 'tiny',
  167. 'description' => 'The exportable status of the entity.',
  168. ));
  169. db_add_field('profile_type', 'module', array(
  170. 'description' => 'The name of the providing module if the entity has been defined in code.',
  171. 'type' => 'varchar',
  172. 'length' => 255,
  173. 'not null' => FALSE,
  174. ));
  175. }
  176. /**
  177. * Add a label column to profiles.
  178. */
  179. function profile2_update_7101() {
  180. db_add_field('profile', 'label', array(
  181. 'description' => 'A human-readable label for this profile.',
  182. 'type' => 'varchar',
  183. 'length' => 255,
  184. 'not null' => TRUE,
  185. 'default' => '',
  186. ));
  187. $types = db_select('profile_type', 'p')
  188. ->fields('p')
  189. ->execute()
  190. ->fetchAllAssoc('type');
  191. // Initialize with the type label.
  192. foreach ($types as $type_name => $type) {
  193. db_update('profile')
  194. ->fields(array(
  195. 'label' => $type->label,
  196. ))
  197. ->condition('type', $type_name)
  198. ->execute();
  199. }
  200. }
  201. /**
  202. * Add a created and a changed column to profiles.
  203. */
  204. function profile2_update_7102() {
  205. db_add_field('profile', 'created', array(
  206. 'description' => 'The Unix timestamp when the profile was created.',
  207. 'type' => 'int',
  208. 'not null' => FALSE,
  209. ));
  210. db_add_field('profile', 'changed', array(
  211. 'description' => 'The Unix timestamp when the profile was most recently saved.',
  212. 'type' => 'int',
  213. 'not null' => FALSE,
  214. ));
  215. }
  216. /**
  217. * Remove duplicate profile records in batches of 50.
  218. */
  219. function profile2_update_7103(&$sandbox) {
  220. // Query to get duplicate profiles.
  221. $query = db_select('profile', 'p1');
  222. $query->distinct();
  223. $query->fields('p1', array('pid'));
  224. $query->join('profile', 'p2', 'p1.type = p2.type AND p1.uid = p2.uid AND p1.label = p2.label AND p1.pid < p2.pid');
  225. // Setup initial batch variables.
  226. if (!isset($sandbox['progress'])) {
  227. // The number of duplicate profiles deleted so far.
  228. $sandbox['progress'] = 0;
  229. // Total number of duplicate profiles that will be deleted.
  230. $sandbox['total'] = $query->execute()->rowCount();
  231. }
  232. // Query the next 50 profiles to be deleted.
  233. $query->range(0, 50);
  234. $result = $query->execute();
  235. // Update progress of removing duplicate profiles.
  236. $sandbox['progress'] = $sandbox['progress'] + $result->rowCount();
  237. // Delete duplicate profiles.
  238. profile2_delete_multiple($result->fetchCol());
  239. // Update batch status.
  240. $sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['total']) ? TRUE : ($sandbox['progress'] / $sandbox['total']);
  241. if ($sandbox['#finished']) {
  242. return t('@total duplicate profiles were removed from the system.', array('@total' => $sandbox['progress']));
  243. }
  244. }
  245. /**
  246. * The combination of profile type and uid should be unique.
  247. */
  248. function profile2_update_7104() {
  249. db_add_unique_key('profile', 'user_profile_type', array('type', 'uid'));
  250. }