flag.views.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * @file
  4. * Provides support for the Views module.
  5. */
  6. /**
  7. * Implements hook_views_plugins().
  8. */
  9. function flag_views_plugins() {
  10. return array(
  11. 'argument validator' => array(
  12. 'flag_flaggable_node' => array(
  13. 'title' => t('Flaggable node'),
  14. 'flag type' => 'node',
  15. 'handler' => 'flag_plugin_argument_validate_flaggability',
  16. 'path' => drupal_get_path('module', 'flag') . '/includes',
  17. ),
  18. 'flag_flaggable_user' => array(
  19. 'title' => t('Flaggable user'),
  20. 'flag type' => 'user',
  21. 'handler' => 'flag_plugin_argument_validate_flaggability',
  22. 'path' => drupal_get_path('module', 'flag') . '/includes',
  23. ),
  24. // A comment validator won't be very useful. Moreover, having it will
  25. // contribute to the argument object's $options polution, so let's skip
  26. // it.
  27. ),
  28. );
  29. }
  30. /**
  31. * Implements hook_views_data().
  32. */
  33. function flag_views_data() {
  34. $data = array();
  35. $data['flagging']['table']['group'] = t('Flags');
  36. $data['flag_counts']['table']['group'] = t('Flags');
  37. // Notify views from flag 2.x of our changes to views data.
  38. // @see flag_update_7301().
  39. $data['flag_content']['moved to'] = 'flagging';
  40. // @see flag_update_7303().
  41. $data['flag_content']['content_id']['moved to'] = array('flagging', 'entity_id');
  42. $data['flagging']['uid'] = array(
  43. 'title' => t('User uid'),
  44. 'help' => t('The user that flagged an item. If you need more fields than the uid add the "Flags: User" relationship.'),
  45. 'relationship' => array(
  46. 'base' => 'users',
  47. 'title' => t('User'),
  48. 'help' => t('Relate an item to the user that flagged it.'),
  49. 'handler' => 'views_handler_relationship',
  50. 'label' => t('Flag user'),
  51. ),
  52. 'filter' => array(
  53. 'handler' => 'views_handler_filter_user_name',
  54. ),
  55. 'argument' => array(
  56. 'handler' => 'views_handler_argument_numeric',
  57. ),
  58. 'field' => array(
  59. 'handler' => 'views_handler_field_user',
  60. ),
  61. );
  62. $data['flagging']['timestamp'] = array(
  63. 'title' => t('Flagged time'),
  64. 'help' => t('Display the time the content was flagged by a user.'),
  65. 'field' => array(
  66. 'handler' => 'views_handler_field_date',
  67. 'click sortable' => TRUE,
  68. ),
  69. 'sort' => array(
  70. 'handler' => 'views_handler_sort_date',
  71. ),
  72. 'filter' => array(
  73. 'handler' => 'views_handler_filter_date',
  74. ),
  75. 'argument' => array(
  76. 'handler' => 'views_handler_argument_date',
  77. ),
  78. );
  79. // Argument for content ID, used for "Who's flagged this" views.
  80. $data['flagging']['entity_id'] = array(
  81. 'title' => t('Entity ID'),
  82. 'help' => t('The unique ID of the object that has been flagged.'),
  83. 'argument' => array(
  84. 'handler' => 'flag_handler_argument_entity_id',
  85. ),
  86. );
  87. // Specialized is null/is not null filter.
  88. $data['flagging']['flagged'] = array(
  89. 'title' => t('Flagged'),
  90. 'real field' => 'uid',
  91. 'field' => array(
  92. 'handler' => 'flag_handler_field_flagged',
  93. 'label' => t('Flagged'),
  94. 'help' => t('A boolean field to show whether the flag is set or not.'),
  95. ),
  96. 'filter' => array(
  97. 'handler' => 'flag_handler_filter_flagged',
  98. 'label' => t('Flagged'),
  99. 'help' => t('Filter to ensure content has or has not been flagged.'),
  100. ),
  101. 'sort' => array(
  102. 'handler' => 'flag_handler_sort_flagged',
  103. 'label' => t('Flagged'),
  104. 'help' => t('Sort by whether entities have or have not been flagged.'),
  105. ),
  106. );
  107. // Flag content links.
  108. $data['flagging']['ops'] = array(
  109. 'title' => t('Flag link'),
  110. 'help' => t('Display flag/unflag link.'),
  111. 'field' => array(
  112. 'handler' => 'flag_handler_field_ops',
  113. ),
  114. );
  115. $data['flagging']['sid'] = array(
  116. 'title' => t('Flagging session ID'),
  117. 'help' => t('The session ID of the flagging.'),
  118. 'field' => array(
  119. 'handler' => 'views_handler_field_numeric',
  120. 'click sortable' => TRUE,
  121. ),
  122. 'sort' => array(
  123. 'handler' => 'views_handler_sort',
  124. ),
  125. 'filter' => array(
  126. 'handler' => 'views_handler_filter_numeric',
  127. ),
  128. 'argument' => array(
  129. 'handler' => 'views_handler_argument_numeric',
  130. ),
  131. );
  132. $data['flag_counts']['count'] = array(
  133. 'title' => t('Flag counter'),
  134. 'help' => t('The number of times a piece of content is flagged by any user.'),
  135. 'field' => array(
  136. 'handler' => 'views_handler_field_numeric',
  137. 'click sortable' => TRUE,
  138. ),
  139. 'sort' => array(
  140. 'handler' => 'views_handler_sort',
  141. ),
  142. 'filter' => array(
  143. 'handler' => 'views_handler_filter_numeric',
  144. ),
  145. 'argument' => array(
  146. 'handler' => 'views_handler_argument_numeric',
  147. ),
  148. );
  149. $data['flag_counts']['last_updated'] = array(
  150. 'title' => t('Time last flagged'),
  151. 'help' => t('The time a piece of content was most recently flagged by any user.'),
  152. 'field' => array(
  153. 'handler' => 'views_handler_field_date',
  154. 'click sortable' => TRUE,
  155. ),
  156. 'sort' => array(
  157. 'handler' => 'views_handler_sort_date',
  158. ),
  159. 'filter' => array(
  160. 'handler' => 'views_handler_filter_date',
  161. ),
  162. 'argument' => array(
  163. 'handler' => 'views_handler_argument_date',
  164. ),
  165. );
  166. return $data;
  167. }
  168. /**
  169. * Implements hook_views_data_alter().
  170. */
  171. function flag_views_data_alter(&$data) {
  172. foreach (array_keys(flag_fetch_definition()) as $flag_type) {
  173. $flag = flag_flag::factory_by_entity_type($flag_type);
  174. $info = $flag->get_views_info();
  175. if (!isset($info)) {
  176. continue;
  177. }
  178. if (!empty($info['join field'])) {
  179. // Add the flag relationship.
  180. $data[$info['views table']]['flag_content_rel'] = array(
  181. 'group' => t('Flags'),
  182. 'title' => $info['title'],
  183. 'help' => $info['help'],
  184. 'relationship' => array(
  185. 'flag type' => $flag_type,
  186. 'handler' => 'flag_handler_relationship_content',
  187. 'label' => t('flag'),
  188. 'base' => 'flagging',
  189. 'base field' => 'entity_id',
  190. 'relationship field' => $info['join field'],
  191. ),
  192. );
  193. // Add the flag counter relationship.
  194. $data[$info['views table']]['flag_count_rel'] = array(
  195. 'group' => t('Flags'),
  196. 'title' => $info['counter title'],
  197. 'help' => $info['counter help'],
  198. 'relationship' => array(
  199. 'flag type' => $flag_type,
  200. 'handler' => 'flag_handler_relationship_counts',
  201. 'label' => t('counter'),
  202. 'base' => 'flag_counts',
  203. 'base field' => 'entity_id',
  204. 'relationship field' => $info['join field'],
  205. ),
  206. );
  207. }
  208. }
  209. // Add a relationship for the user that flagged any type of content.
  210. $data['users']['flag_user_content_rel'] = array(
  211. 'group' => t('Flags'),
  212. 'title' => t("User's flaggings"),
  213. 'help' => t('Relate users to the flaggings they have made on objects, using a particular flag.'),
  214. 'relationship' => array(
  215. 'base' => 'flagging',
  216. 'base field' => 'uid',
  217. 'relationship field' => 'uid',
  218. 'handler' => 'flag_handler_relationship_user_content',
  219. 'label' => t('user flagged content'),
  220. ),
  221. );
  222. }
  223. /**
  224. * Implements hook_views_query_substitutions().
  225. *
  226. * Allow replacement of current user's session id so we can cache these queries.
  227. */
  228. function flag_views_query_substitutions() {
  229. return array(
  230. '***FLAG_CURRENT_USER_SID***' => flag_get_sid(),
  231. );
  232. }
  233. /**
  234. * A helper function that creates a radio list of available flags.
  235. *
  236. * This function is used to select the desired flag when setting up flag
  237. * relationships and fields.
  238. */
  239. function flag_views_flag_config_form($form_type, $entity_type, $current_flag) {
  240. $flags = flag_get_flags($entity_type);
  241. $options = array();
  242. foreach ($flags as $flag) {
  243. $options[$flag->name] = $flag->get_title();
  244. }
  245. $form = array(
  246. '#type' => $form_type,
  247. '#title' => t('Flag'),
  248. '#options' => $options,
  249. '#default_value' => $current_flag,
  250. '#required' => TRUE,
  251. );
  252. return $form;
  253. }
  254. /**
  255. * Helper function that gets the first defined flag and returns its name.
  256. */
  257. function flag_views_flag_default($entity_type) {
  258. $default_flag = &drupal_static(__FUNCTION__, array());
  259. if (!array_key_exists($entity_type, $default_flag)) {
  260. $flags = flag_get_flags($entity_type);
  261. $flag = array_shift($flags);
  262. $default_flag[$entity_type] = $flag ? $flag->name : NULL;
  263. }
  264. return $default_flag[$entity_type];
  265. }