flag_lists.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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' => FALSE,
  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. //
  227. // foreach ($orphans as $orphan) {
  228. // $num_deleted = db_delete('flag_lists_content')
  229. // ->condition('fid', $orphan->fid)
  230. // ->condition('fcid', $orphan->fcid)
  231. // ->condition('uid', $orphan->uid)
  232. // ->execute();
  233. //
  234. // if (!empty($num_deleted)) {
  235. // drupal_set_message("Deleting flag_id: $orphan->fid flag_content_id: $orphan->fcid");
  236. //
  237. // db_update('flag_lists_counts')
  238. // ->fields(array(
  239. // 'count' => ($orphan->count <= 1) ? 0 : $orphan->count - 1,
  240. // ))
  241. // ->condition('content_type', $orphan->content_type)
  242. // ->condition('fid', $orphan->fid)
  243. // ->condition('content_id', $orphan->content_id)
  244. // ->execute();
  245. // }
  246. // }
  247. }
  248. /**
  249. * Update the flag_lists_flags table
  250. */
  251. function flag_lists_update_7301() {
  252. // db_change_field('flag_lists_flags','content_type', 'entity_type',
  253. // array(
  254. // 'type' => 'varchar',
  255. // 'length' => '32',
  256. // 'not null' => TRUE,
  257. // 'default' => '',
  258. // ));
  259. }
  260. /**
  261. * Update the flag_lists_content table
  262. */
  263. function flag_lists_update_7302() {
  264. // db_drop_unique_key('flag_lists_content','fid_content_id_uid_sid');
  265. // db_drop_index('flag_lists_content','content_type_content_id');
  266. // db_drop_index('flag_lists_content','content_type_uid_sid');
  267. //
  268. // db_change_field('flag_lists_content','content_type', 'entity_type',
  269. // array(
  270. // 'type' => 'varchar',
  271. // 'length' => '32',
  272. // 'not null' => TRUE,
  273. // 'default' => '',
  274. // ));
  275. //
  276. // db_change_field('flag_lists_content','content_id', 'entity_id',
  277. // array(
  278. // 'type' => 'int',
  279. // 'unsigned' => TRUE,
  280. // 'not null' => TRUE,
  281. // 'default' => 0,
  282. // ));
  283. //
  284. // db_add_unique_key('flag_lists_content',
  285. // 'fid_entity_id_uid_sid',
  286. // array('fid', 'entity_id', 'uid', 'sid'));
  287. // db_add_index('flag_lists_content',
  288. // 'entity_type_uid_sid',
  289. // array('entity_type', 'uid', 'sid'));
  290. // db_add_index('flag_lists_content',
  291. // 'entity_type_entity_id',
  292. // array('entity_type', 'entity_id'));
  293. }
  294. /**
  295. * Update the flag_lists_counts table
  296. */
  297. function flag_lists_update_7303() {
  298. db_drop_primary_key('flag_lists_counts');
  299. db_drop_index('flag_lists_counts','fid_content_type');
  300. db_drop_index('flag_lists_counts','content_type_content_id');
  301. // db_change_field('flag_lists_counts','content_type', 'entity_type',
  302. // array(
  303. // 'type' => 'varchar',
  304. // 'length' => '32',
  305. // 'not null' => TRUE,
  306. // 'default' => '',
  307. // ));
  308. // db_change_field('flag_lists_counts','content_id', 'entity_id',
  309. // array(
  310. // 'type' => 'int',
  311. // 'unsigned' => TRUE,
  312. // 'not null' => TRUE,
  313. // 'default' => 0,
  314. // 'disp-width' => '10',
  315. // ),
  316. // array('primary key' => array('fid', 'entity_id')));
  317. // db_add_index('flag_lists_counts',
  318. // 'fid_entity_type',
  319. // array('fid', 'entity_type'));
  320. // db_add_index('flag_lists_counts',
  321. // 'entity_type_entity_id',
  322. // array('entity_type', 'entity_id'));
  323. }
  324. /**
  325. * Update the views
  326. */
  327. function flag_lists_update_7304() {
  328. $myview = views_get_view('flag_lists', TRUE);
  329. $myview->display['default']->display_options['fields']['name_2']['table'] = 'flag';
  330. $myview->display['default']->display_options['filters']['name']['table'] = 'flag';
  331. views_save_view($myview);
  332. // Clear the cache for the pager
  333. $cache = $myview->name . ':block:results:';
  334. cache_clear_all($cache, 'cache_views_data', TRUE);
  335. $myview = views_get_view('flag_lists_content', TRUE);
  336. unset($myview->display['default']->display_options['relationships']['content_id']);
  337. $myview->display['default']->display_options['relationships']['entity_id']['id'] = 'entity_id';
  338. $myview->display['default']->display_options['relationships']['entity_id']['table'] = 'flag_lists_content';
  339. $myview->display['default']->display_options['relationships']['entity_id']['field'] = 'entity_id';
  340. $myview->display['default']->display_options['relationships']['entity_id']['label'] = 'Listed content';
  341. $myview->display['default']->display_options['relationships']['entity_id']['required'] = TRUE;
  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. $myview = views_get_view('flag_lists_user_list', TRUE);
  347. $myview->display['page_1']->display_options['path'] = 'flag/lists/%';
  348. views_save_view($myview);
  349. // Clear the cache for the pager
  350. $cache = $myview->name . ':block:results:';
  351. cache_clear_all($cache, 'cache_views_data', TRUE);
  352. }
  353. /**
  354. * Update the flag_lists_flags table
  355. */
  356. function flag_lists_update_7305() {
  357. db_change_field('flag_lists_flags','uid', 'uid',
  358. array(
  359. 'type' => 'int',
  360. 'unsigned' => TRUE,
  361. 'not null' => TRUE,
  362. ));
  363. }