flag_lists.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /**
  3. * @file
  4. * The Flag lists module install file.
  5. */
  6. /**
  7. * Implementation of hook_install().
  8. */
  9. function flag_lists_schema() {
  10. $schema = array();
  11. $schema['flag_lists_flags'] = array(
  12. 'fields' => array(
  13. 'fid' => array(
  14. 'type' => 'serial',
  15. 'size' => 'small',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. ),
  19. 'pfid' => array(
  20. 'type' => 'int',
  21. 'size' => 'small',
  22. 'unsigned' => TRUE,
  23. 'not null' => TRUE,
  24. ),
  25. 'uid' => array(
  26. 'type' => 'int',
  27. 'unsigned' => TRUE,
  28. 'not null' => TRUE,
  29. ),
  30. 'entity_type' => array(
  31. 'type' => 'varchar',
  32. 'length' => '32',
  33. 'not null' => TRUE,
  34. 'default' => '',
  35. ),
  36. 'name' => array(
  37. 'type' => 'varchar',
  38. 'length' => '32',
  39. 'not null' => FALSE,
  40. 'default' => '',
  41. ),
  42. 'title' => array(
  43. 'type' => 'varchar',
  44. 'length' => '255',
  45. 'not null' => FALSE,
  46. 'default' => '',
  47. ),
  48. 'options' => array(
  49. 'type' => 'text',
  50. 'not null' => FALSE,
  51. ),
  52. ),
  53. 'primary key' => array('fid'),
  54. 'unique keys' => array(
  55. 'name' => array('name'),
  56. ),
  57. );
  58. $schema['flag_lists_content'] = array(
  59. 'fields' => array(
  60. 'fcid' => array(
  61. 'type' => 'serial',
  62. 'unsigned' => TRUE,
  63. 'not null' => TRUE,
  64. ),
  65. 'fid' => array(
  66. 'type' => 'int',
  67. 'size' => 'small',
  68. 'unsigned' => TRUE,
  69. 'not null' => TRUE,
  70. 'default' => 0,
  71. ),
  72. 'entity_type' => array(
  73. 'type' => 'varchar',
  74. 'length' => '32',
  75. 'not null' => TRUE,
  76. 'default' => '',
  77. ),
  78. 'entity_id' => array(
  79. 'type' => 'int',
  80. 'unsigned' => TRUE,
  81. 'not null' => TRUE,
  82. 'default' => 0,
  83. ),
  84. 'uid' => array(
  85. 'type' => 'int',
  86. 'unsigned' => TRUE,
  87. 'not null' => TRUE,
  88. 'default' => 0,
  89. ),
  90. 'sid' => array(
  91. 'type' => 'int',
  92. 'unsigned' => TRUE,
  93. 'not null' => TRUE,
  94. 'default' => 0,
  95. ),
  96. 'timestamp' => array(
  97. 'type' => 'int',
  98. 'unsigned' => TRUE,
  99. 'not null' => TRUE,
  100. 'default' => 0,
  101. 'disp-size' => 11,
  102. )
  103. ),
  104. 'primary key' => array('fcid'),
  105. 'unique keys' => array(
  106. 'fid_content_id_uid_sid' => array('fid', 'entity_id', 'uid', 'sid'),
  107. ),
  108. 'indexes' => array(
  109. 'entity_type_entity_id' => array('entity_type', 'entity_id'),
  110. 'entity_type_uid_sid' => array('entity_type', 'uid', 'sid'),
  111. ),
  112. );
  113. $schema['flag_lists_counts'] = array(
  114. 'fields' => array(
  115. 'fid' => array(
  116. 'type' => 'int',
  117. 'size' => 'small',
  118. 'unsigned' => TRUE,
  119. 'not null' => TRUE,
  120. 'default' => 0,
  121. ),
  122. 'entity_type' => array(
  123. 'type' => 'varchar',
  124. 'length' => '32',
  125. 'not null' => TRUE,
  126. 'default' => '',
  127. ),
  128. 'entity_id' => array(
  129. 'type' => 'int',
  130. 'unsigned' => TRUE,
  131. 'not null' => TRUE,
  132. 'default' => 0,
  133. 'disp-width' => '10',
  134. ),
  135. 'count' => array(
  136. 'type' => 'int',
  137. 'unsigned' => TRUE,
  138. 'not null' => TRUE,
  139. 'default' => 0,
  140. 'disp-width' => '10',
  141. )
  142. ),
  143. 'primary key' => array('fid', 'entity_id'),
  144. 'indexes' => array(
  145. 'fid_entity_type' => array('fid', 'entity_type'),
  146. 'entity_type_entity_id' => array('entity_type', 'entity_id'),
  147. 'count' => array('count'),
  148. ),
  149. );
  150. $schema['flag_lists_types'] = array(
  151. 'fields' => array(
  152. 'name' => array(
  153. 'type' => 'varchar',
  154. 'length' => '32',
  155. 'not null' => TRUE,
  156. 'default' => '',
  157. ),
  158. 'type' => array(
  159. 'type' => 'varchar',
  160. 'length' => '32',
  161. 'not null' => TRUE,
  162. 'default' => '')
  163. ),
  164. 'primary key' => array('name', 'type'),
  165. 'indexes' => array(
  166. 'name' => array('name'),
  167. ),
  168. );
  169. return $schema;
  170. }
  171. /**
  172. * Implements hook_install().
  173. */
  174. function flag_lists_install() {
  175. // Set up our default template.
  176. db_insert('flag_lists_types')
  177. ->fields(array(
  178. 'name' => 'fl_template',
  179. ))
  180. ->execute();
  181. }
  182. /**
  183. * Implements hook_uninstall().
  184. */
  185. function flag_lists_uninstall() {
  186. // Remove our template flags.
  187. $query = db_select('flag_lists_types', 'fl');
  188. $query->leftJoin('flag', 'f', 'fl.name = f.name');
  189. $query->addField('f', 'fid', 'fid');
  190. $query->distinct();
  191. $fids = $query->execute();
  192. foreach ($fids as $fid) {
  193. db_delete('flag')->condition('fid', $fid->fid)->execute();
  194. db_delete('flagging')->condition('fid', $fid->fid)->execute();
  195. db_delete('flag_types')->condition('fid', $fid->fid)->execute();
  196. db_delete('flag_counts')->condition('fid', $fid->fid)->execute();
  197. }
  198. db_delete('variable')->condition('name', 'flag_lists%', 'LIKE')->execute();
  199. $view_to_delete = views_get_view('flag_lists');
  200. if (!empty($view_to_delete)) {
  201. views_delete_view($view_to_delete);
  202. }
  203. $view_to_delete =views_get_view('flag_lists_content');
  204. if (!empty($view_to_delete)) {
  205. views_delete_view($view_to_delete);
  206. }
  207. $view_to_delete = views_get_view('flag_lists_user_lists');
  208. if (!empty($view_to_delete)) {
  209. views_delete_view($view_to_delete);
  210. }
  211. $view_to_delete = views_get_view('flag_lists_user_list');
  212. if (!empty($view_to_delete)) {
  213. views_delete_view($view_to_delete);
  214. }
  215. drupal_set_message(t('Flag lists has been uninstalled.'));
  216. }
  217. /**
  218. * Get rid of garbage list entries that are orphaned from a list
  219. */
  220. function flag_lists_update_7000() {
  221. $orphans = db_query("SELECT flc.fcid, flc.fid, flc.content_id, flc.uid, flcounts.content_type, count
  222. FROM {flag_lists_content} flc
  223. JOIN {flag_lists_counts} flcounts ON flcounts.fid=flc.fid AND flc.content_id=flcounts.content_id
  224. LEFT JOIN {flag_lists_flags} flf ON flf.fid=flc.fid
  225. WHERE flf.fid IS NULL");
  226. foreach ($orphans as $orphan) {
  227. $num_deleted = db_delete('flag_lists_content')
  228. ->condition('fid', $orphan->fid)
  229. ->condition('fcid', $orphan->fcid)
  230. ->condition('uid', $orphan->uid)
  231. ->execute();
  232. if (!empty($num_deleted)) {
  233. drupal_set_message("Deleting flag_id: $orphan->fid flag_content_id: $orphan->fcid");
  234. db_update('flag_lists_counts')
  235. ->fields(array(
  236. 'count' => ($orphan->count <= 1) ? 0 : $orphan->count - 1,
  237. ))
  238. ->condition('content_type', $orphan->content_type)
  239. ->condition('fid', $orphan->fid)
  240. ->condition('content_id', $orphan->content_id)
  241. ->execute();
  242. }
  243. }
  244. }
  245. /**
  246. * Update the flag_lists_flags table
  247. */
  248. function flag_lists_update_7301() {
  249. db_change_field('flag_lists_flags','content_type', 'entity_type',
  250. array(
  251. 'type' => 'varchar',
  252. 'length' => '32',
  253. 'not null' => TRUE,
  254. 'default' => '',
  255. ));
  256. }
  257. /**
  258. * Update the flag_lists_content table
  259. */
  260. function flag_lists_update_7302() {
  261. db_drop_unique_key('flag_lists_content','fid_content_id_uid_sid');
  262. db_drop_index('flag_lists_content','content_type_content_id');
  263. db_drop_index('flag_lists_content','content_type_uid_sid');
  264. db_change_field('flag_lists_content','content_type', 'entity_type',
  265. array(
  266. 'type' => 'varchar',
  267. 'length' => '32',
  268. 'not null' => TRUE,
  269. 'default' => '',
  270. ));
  271. db_change_field('flag_lists_content','content_id', 'entity_id',
  272. array(
  273. 'type' => 'int',
  274. 'unsigned' => TRUE,
  275. 'not null' => TRUE,
  276. 'default' => 0,
  277. ));
  278. db_add_unique_key('flag_lists_content',
  279. 'fid_entity_id_uid_sid',
  280. array('fid', 'entity_id', 'uid', 'sid'));
  281. db_add_index('flag_lists_content',
  282. 'entity_type_uid_sid',
  283. array('entity_type', 'uid', 'sid'));
  284. db_add_index('flag_lists_content',
  285. 'entity_type_entity_id',
  286. array('entity_type', 'entity_id'));
  287. }
  288. /**
  289. * Update the flag_lists_counts table
  290. */
  291. function flag_lists_update_7303() {
  292. db_drop_primary_key('flag_lists_counts');
  293. db_drop_index('flag_lists_counts','fid_content_type');
  294. db_drop_index('flag_lists_counts','content_type_content_id');
  295. db_change_field('flag_lists_counts','content_type', 'entity_type',
  296. array(
  297. 'type' => 'varchar',
  298. 'length' => '32',
  299. 'not null' => TRUE,
  300. 'default' => '',
  301. ));
  302. db_change_field('flag_lists_counts','content_id', 'entity_id',
  303. array(
  304. 'type' => 'int',
  305. 'unsigned' => TRUE,
  306. 'not null' => TRUE,
  307. 'default' => 0,
  308. 'disp-width' => '10',
  309. ),
  310. array('primary key' => array('fid', 'entity_id')));
  311. db_add_index('flag_lists_counts',
  312. 'fid_entity_type',
  313. array('fid', 'entity_type'));
  314. db_add_index('flag_lists_counts',
  315. 'entity_type_entity_id',
  316. array('entity_type', 'entity_id'));
  317. }
  318. /**
  319. * Update the views
  320. */
  321. function flag_lists_update_7304() {
  322. $myview = views_get_view('flag_lists', TRUE);
  323. $myview->display['default']->display_options['fields']['name_2']['table'] = 'flag';
  324. $myview->display['default']->display_options['filters']['name']['table'] = 'flag';
  325. views_save_view($myview);
  326. // Clear the cache for the pager
  327. $cache = $myview->name . ':block:results:';
  328. cache_clear_all($cache, 'cache_views_data', TRUE);
  329. $myview = views_get_view('flag_lists_content', TRUE);
  330. unset($myview->display['default']->display_options['relationships']['content_id']);
  331. $myview->display['default']->display_options['relationships']['entity_id']['id'] = 'entity_id';
  332. $myview->display['default']->display_options['relationships']['entity_id']['table'] = 'flag_lists_content';
  333. $myview->display['default']->display_options['relationships']['entity_id']['field'] = 'entity_id';
  334. $myview->display['default']->display_options['relationships']['entity_id']['label'] = 'Listed content';
  335. $myview->display['default']->display_options['relationships']['entity_id']['required'] = TRUE;
  336. views_save_view($myview);
  337. // Clear the cache for the pager
  338. $cache = $myview->name . ':block:results:';
  339. cache_clear_all($cache, 'cache_views_data', TRUE);
  340. $myview = views_get_view('flag_lists_user_list', TRUE);
  341. $myview->display['page_1']->display_options['path'] = 'flag/lists/%';
  342. views_save_view($myview);
  343. // Clear the cache for the pager
  344. $cache = $myview->name . ':block:results:';
  345. cache_clear_all($cache, 'cache_views_data', TRUE);
  346. }
  347. /**
  348. * Update the flag_lists_flags table
  349. */
  350. function flag_lists_update_7305() {
  351. db_change_field('flag_lists_flags','uid', 'uid',
  352. array(
  353. 'type' => 'int',
  354. 'unsigned' => TRUE,
  355. 'not null' => TRUE,
  356. ));
  357. }
  358. /**
  359. * Update the flag_lists_types table
  360. */
  361. function flag_lists_update_7306() {
  362. db_change_field('flag_lists_types','type', 'type',
  363. array(
  364. 'type' => 'varchar',
  365. 'length' => '32',
  366. 'not null' => TRUE,
  367. 'default' => '',
  368. ));
  369. }