flag_is_flagged.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Plugin definition.
  4. */
  5. $plugin = array(
  6. 'title' => t("Entity is flagged"),
  7. 'description' => t('Control access by whether or not an entity is flagged.'),
  8. 'callback' => 'flag_flag_is_flagged_access_check',
  9. 'default' => array('flag_name' => ''),
  10. 'settings form' => 'flag_flag_is_flagged_access_settings',
  11. 'summary' => 'flag_flag_is_flagged_access_summary',
  12. 'get child' => 'flag_flag_is_flagged_access_get_child',
  13. 'get children' => 'flag_flag_is_flagged_access_get_children',
  14. );
  15. /*
  16. * Implements plugin get_child callback.
  17. */
  18. function flag_flag_is_flagged_access_get_child($plugin, $parent, $child) {
  19. $plugins = flag_flag_is_flagged_access_get_children($plugin, $parent);
  20. return $plugins[$parent . ':' . $child];
  21. }
  22. /*
  23. * Implements plugin get_children callback.
  24. */
  25. function flag_flag_is_flagged_access_get_children($plugin, $parent) {
  26. $plugins = array();
  27. $entities = entity_get_info();
  28. foreach ($entities as $entity_type => $info) {
  29. if (entity_type_supports($entity_type, 'view')) {
  30. $plugin['title'] = t('@entity_type is flagged', array('@entity_type' => $info['label']));
  31. $plugin['keyword'] = $entity_type;
  32. $plugin['name'] = $parent . ':' . $entity_type;
  33. $plugin['required context'] = new ctools_context_required(t('Entity'), $entity_type);
  34. $plugins[$parent . ':' . $entity_type] = $plugin;
  35. }
  36. }
  37. return $plugins;
  38. }
  39. /**
  40. * Settings form.
  41. */
  42. function flag_flag_is_flagged_access_settings(&$form, &$form_state, $conf) {
  43. $flag_name_options = array();
  44. $plugin = $form_state['plugin'];
  45. $entity_type = explode(':', $plugin['name']);
  46. $entity_type = $entity_type[1];
  47. foreach (flag_get_flags($entity_type) as $flag) {
  48. $flag_name_options[$flag->name] = check_plain($flag->title);
  49. }
  50. $flag_user_options = array(
  51. 'any' => t('Flagged by anyone'),
  52. 'user' => t('Flagged by current user'),
  53. );
  54. $flag_user_default = isset($conf['flag_user']) ? $conf['flag_user'] : 'user';
  55. $form['settings']['flag_name'] = array(
  56. '#title' => t('Flag name'),
  57. '#type' => 'radios',
  58. '#options' => $flag_name_options,
  59. '#description' => t('Include only flagged content.'),
  60. '#default_value' => $conf['flag_name'],
  61. );
  62. $form['settings']['flag_user'] = array(
  63. '#title' => t('Filter by flag owner'),
  64. '#type' => 'radios',
  65. '#options' => $flag_user_options,
  66. '#description' => t('Show content flagged by anyone or only by current user.'),
  67. '#default_value' => $flag_user_default,
  68. );
  69. return $form;
  70. }
  71. /**
  72. * Check for access.
  73. */
  74. function flag_flag_is_flagged_access_check($conf, $context) {
  75. $flag = flag_get_flag($conf['flag_name']);
  76. if (!$flag || empty($context->data)) {
  77. return FALSE;
  78. }
  79. // Get the ID of the entity.
  80. list($id) = entity_extract_ids($flag->entity_type, $context->data);
  81. // Get either the count of users who have flagged this entity or find out
  82. // whether the current user has flagged this node, depending on settings.
  83. if (isset($conf['flag_user']) && $conf['flag_user'] == 'any') {
  84. $count = count(flag_get_entity_flags($flag->entity_type, $id, $conf['flag_name']));
  85. return $count;
  86. }
  87. else {
  88. return $flag->is_flagged($id);
  89. }
  90. }
  91. /**
  92. * Provide a summary description based upon the specified context.
  93. */
  94. function flag_flag_is_flagged_access_summary($conf, $context) {
  95. $flag = flag_get_flag($conf['flag_name']);
  96. if ($flag) {
  97. $flag_limit_by = '';
  98. if (isset($conf['flag_user']) && $conf['flag_user'] == 'user') {
  99. return t('@identifier is flagged with "@flag" by current user.', array('@flag' => $flag->title, '@identifier' => $context->identifier));
  100. }
  101. elseif (isset($conf['flag_user']) && $conf['flag_user'] == 'any') {
  102. return t('@identifier is flagged with "@flag" by anyone.', array('@flag' => $flag->title, '@identifier' => $context->identifier));
  103. }
  104. }
  105. else {
  106. return t('Missing/deleted flag "@flag"', array('@flag' => $conf['flag_name']));
  107. }
  108. }