flag_handler_relationships.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * @file
  4. * Contains various relationship handlers.
  5. */
  6. /**
  7. * Base class for all our relationship classes.
  8. *
  9. * @ingroup views
  10. */
  11. abstract class flag_handler_relationship extends views_handler_relationship {
  12. /**
  13. * Every relationship has a 'flag' option.
  14. */
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['flag'] = array('default' => NULL);
  18. $options['required'] = array('default' => 1);
  19. return $options;
  20. }
  21. /**
  22. * Make sure the flag exists.
  23. *
  24. * When importing views, or when deleting flags, inconsistent views may
  25. * result. This validator is called by Views before saving or previewing a
  26. * view.
  27. */
  28. function validate() {
  29. $errors = array();
  30. $tokens = array(
  31. '@relationship-name' => $this->ui_name() . ' ' . $this->admin_summary(),
  32. '@flag-name' => $this->options['flag'],
  33. );
  34. if (!$this->options['flag']) {
  35. $errors[] = t('You must pick a flag to use for the relationship "@relationship-name".', $tokens);
  36. }
  37. elseif (!flag_get_flag($this->options['flag'])) {
  38. $errors[] = t('This view is looking for a flag by the name "@flag-name", but there is no such flag. Perhaps it was deleted. Please update the relationship "@relationship-name" in this view to use an existing flag.', $tokens);
  39. }
  40. return $errors;
  41. }
  42. /**
  43. * Get the type of the flag this relationship uses.
  44. *
  45. * This looks at the data set in the relationship definition in Views data.
  46. *
  47. * @return
  48. * The flag's type, e.g., 'node' or 'taxonomy_term', or NULL if this is not
  49. * set in data from hook_views_data().
  50. *
  51. * @see flag_views_data_alter()
  52. */
  53. function get_flag_type() {
  54. return isset($this->definition['flag type']) ? $this->definition['flag type'] : NULL;
  55. }
  56. /**
  57. * Returns the flag object.
  58. */
  59. function get_flag() {
  60. // Backward compatibility: There may exist old views on the system whose
  61. // 'flag' option isn't set. (This happens if the admin had skippped
  62. // clicking the 'Update' button.) When run, these views should behave as
  63. // if the first flag was selected.
  64. if (!isset($this->options['flag'])) {
  65. $this->options['flag'] = flag_views_flag_default($this->get_flag_type());
  66. }
  67. // Validation: Since validate() is called only when in Views's
  68. // administrative UI, we need to do validation at "run time" ourselves.
  69. if (($errors = $this->validate())) {
  70. foreach ($errors as $error) {
  71. drupal_set_message($error, 'error');
  72. }
  73. }
  74. return flag_get_flag($this->options['flag']);
  75. }
  76. // @todo: It's logical that this class should also implement options_form(),
  77. // to show the flag selector, and query(), to filter on the flag.
  78. }
  79. /**
  80. * Views relationship handler associating flags and content.
  81. *
  82. * This forms a bridge from the entity table to the {flagging} table, with
  83. * options to restrict the join to only flagged content, and to flagged content
  84. * by the current user.
  85. *
  86. * @ingroup views
  87. */
  88. class flag_handler_relationship_content extends flag_handler_relationship {
  89. function option_definition() {
  90. $options = parent::option_definition();
  91. $options['user_scope'] = array('default' => 'current');
  92. return $options;
  93. }
  94. function options_form(&$form, &$form_state) {
  95. parent::options_form($form, $form_state);
  96. $entity_type = $this->definition['flag type'];
  97. $form['label']['#description'] .= ' ' . t('The name of the selected flag makes a good label.');
  98. $form['flag'] = flag_views_flag_config_form('radios', $entity_type, $this->options['flag']);
  99. $form['user_scope'] = array(
  100. '#type' => 'radios',
  101. '#title' => t('By'),
  102. '#options' => array('current' => t('Current user'), 'any' => t('Any user')),
  103. '#default_value' => $this->options['user_scope'],
  104. );
  105. $form['required']['#title'] = t('Include only flagged content');
  106. $form['required']['#description'] = t('If checked, only content that has this flag will be included. Leave unchecked to include all content; or, in combination with the <em>Flagged</em> filter, <a href="@unflagged-url">to limit the results to specifically unflagged content</a>.', array('@unflagged-url' => 'http://drupal.org/node/299335'));
  107. if (!$form['flag']['#options']) {
  108. $form = array(
  109. 'error' => array(
  110. '#markup' => '<p class="error form-item">' . t('No %type flags exist. You must first <a href="@create-url">create a %type flag</a> before being able to use this relationship type.', array('%type' => $entity_type, '@create-url' => url(FLAG_ADMIN_PATH))) . '</p>',
  111. ),
  112. );
  113. $form_state['no flags exist'] = TRUE;
  114. }
  115. if (module_exists('session_api')) {
  116. $form['session_warning'] = array(
  117. '#markup' => '<p class="warning form-item">' . t('<strong>Warning</strong>: Adding this relationship for any flag that contains <strong>anonymous flagging access</strong> will disable page caching for anonymous users when this view is executed. (But this is only true when the relationship is constrained to "Current user", not to "Any user".) It is recommended to create a dedicated page for views containing anonymous user data.') . '</p>',
  118. );
  119. }
  120. }
  121. function options_validate(&$form, &$form_state) {
  122. if (!empty($form_state['no flags exist'])) {
  123. form_error($form, t('You must first create a flag'));
  124. }
  125. }
  126. function admin_summary() {
  127. return $this->options['user_scope'] == 'current' ? t('by current user') : t('by any user');
  128. }
  129. function ui_name($short = FALSE) {
  130. // We put the bookmark name in the UI string to save space.
  131. return t('!group: !title', array('!group' => $this->definition['group'], '!title' => empty($this->options['flag']) ? t('(Please select a flag)') : $this->options['flag']));
  132. }
  133. /**
  134. * Called to implement a relationship in a query.
  135. */
  136. function query() {
  137. if (!($flag = $this->get_flag())) {
  138. return;
  139. }
  140. $this->definition['extra'][] = array(
  141. 'field' => 'fid',
  142. 'value' => $flag->fid,
  143. 'numeric' => TRUE,
  144. );
  145. if ($this->options['user_scope'] == 'current' && !$flag->global) {
  146. $this->definition['extra'][] = array(
  147. 'field' => 'uid',
  148. 'value' => '***CURRENT_USER***',
  149. 'numeric' => TRUE,
  150. );
  151. $flag_roles = user_roles(FALSE, "flag $flag->name");
  152. if (isset($flag_roles[DRUPAL_ANONYMOUS_RID])) {
  153. // Disable page caching for anonymous users.
  154. drupal_page_is_cacheable(FALSE);
  155. // Add in the SID from Session API for anonymous users.
  156. $this->definition['extra'][] = array(
  157. 'field' => 'sid',
  158. 'value' => '***FLAG_CURRENT_USER_SID***',
  159. 'numeric' => TRUE,
  160. );
  161. }
  162. }
  163. parent::query();
  164. }
  165. }
  166. /**
  167. * Views relationship handler associating flag counts and content.
  168. *
  169. * This forms a bridge from the entity table to the {flag_counts} table, with
  170. * the option to restrict the join to include only flagged content.
  171. *
  172. * @ingroup views
  173. */
  174. class flag_handler_relationship_counts extends flag_handler_relationship {
  175. function options_form(&$form, &$form_state) {
  176. parent::options_form($form, $form_state);
  177. $entity_type = $this->definition['flag type'];
  178. $form['flag'] = flag_views_flag_config_form('radios', $entity_type, $this->options['flag']);
  179. $form['required']['#title'] = t('Include only flagged content');
  180. $form['required']['#description'] = t('If checked, only content that is flagged will be included.');
  181. }
  182. function admin_summary() {
  183. // Nothing to show.
  184. }
  185. function ui_name($short = FALSE) {
  186. // We put the bookmark name in the UI string to save space.
  187. return t('!group: !title counter', array('!group' => $this->definition['group'], '!title' => $this->options['flag']));
  188. }
  189. /**
  190. * Called to implement a relationship in a query.
  191. */
  192. function query() {
  193. if (!($flag = $this->get_flag())) {
  194. return;
  195. }
  196. $this->definition['extra'][] = array(
  197. 'field' => 'fid',
  198. 'value' => $flag->fid,
  199. 'numeric' => TRUE,
  200. );
  201. if (!empty($this->options['required'])) {
  202. // Unfortunately, we may have zeros in our table, so having
  203. // parent::query() do INNER JOIN doesn't suffice. We need to filter these
  204. // zeros out.
  205. // @todo Make sure zero records aren't written in the first place, and
  206. // remove this code.
  207. $this->definition['extra'][] = array(
  208. 'field' => 'count',
  209. 'operator' => '>',
  210. 'value' => '0',
  211. 'numeric' => TRUE,
  212. );
  213. }
  214. parent::query();
  215. }
  216. }
  217. /**
  218. * Views relationship handler associating flags and users.
  219. *
  220. * This forms a bridge from the the {users} table to the {flagging} table.
  221. *
  222. * @ingroup views
  223. */
  224. class flag_handler_relationship_user_content extends flag_handler_relationship {
  225. function options_form(&$form, &$form_state) {
  226. parent::options_form($form, $form_state);
  227. $form['label']['#description'] .= ' ' . t('Including name of the selected flag helps identify this relationship.');
  228. $form['flag'] = flag_views_flag_config_form('radios', NULL, $this->options['flag']);
  229. $form['flag']['#title'] = t('Flagged');
  230. $form['required']['#title'] = t('Include only users who have flagged content.');
  231. $form['required']['#description'] = t('If checked, only users that have flagged any content with this flag will be included.');
  232. }
  233. function admin_summary() {
  234. return $this->options['flag'];
  235. }
  236. /**
  237. * Called to implement a relationship in a query.
  238. */
  239. function query() {
  240. if (!($flag = $this->get_flag())) {
  241. return;
  242. }
  243. $this->definition['extra'][] = array(
  244. 'field' => 'fid',
  245. 'value' => $flag->fid,
  246. 'numeric' => TRUE,
  247. );
  248. parent::query();
  249. }
  250. }