root/drupal-6/sites/all/modules/flag/includes/flag.services.inc @ 867

Revision 867, 2.3 KB (checked in by frans, 6 months ago)

update: flag to 6.x-2.x-dev version

Line 
1<?php
2// $Id: flag.services.inc,v 1.1.2.1 2009/11/02 21:20:06 quicksketch Exp $
3
4/**
5 * @file
6 * Services integration for the Flag module.
7 */
8
9/**
10 * Access callback to check a user has access to a flag operation via Services.
11 *
12 * @param $flag_name
13 *   The flag name.
14 * @param $content_id
15 *   The content ID that should be flagged.
16 * @param $uid
17 *   Optional. The user ID on behalf to flag the content.
18 * @param $action
19 *   Optional. If the method is "flag", then pass the desired action which
20 *   should be "flag" or "unflag".
21 * @return
22 *   TRUE if access is allowed.
23 */
24function flag_service_flag_access($flag_name, $content_id, $uid = NULL, $action = NULL) {
25  $flag = flag_get_flag($flag_name);
26  if (!$flag) {
27    // Flag does not exist.
28    return FALSE;
29  }
30  if (empty($action)) {
31    return TRUE;
32  }
33  else {
34    // Check action is valid.
35    if (!in_array($action, array('flag', 'unflag'))) {
36      return FALSE;
37    }
38    if ($uid) {
39      $account = user_load($uid);
40    }
41    else {
42      global $user;
43      $account = $user;
44    }
45
46    if (empty($account) || !$flag->access($content_id, $action, $account)) {
47      // User has no permission to use this flag.
48      return FALSE;
49    }
50    return TRUE;
51  }
52}
53
54/**
55 * Service wrapper to flag a content.
56 *
57 * @param $flag_name
58 *   The flag name.
59 * @param $content_id
60 *   The content ID to check if it was flagged.
61 * @param $uid
62 *   The user ID to check if they flagged the content.
63 * @param $action
64 *   The action. Should be "flag" or "unflag".
65 * @return
66 *   TRUE if content was flagged.
67 */
68function flag_service_flag($flag_name, $content_id, $uid = NULL, $action = 'flag', $skip_permission_check = FALSE) {
69  global $user;
70  $account = empty($uid) ? $user : user_load($uid);
71  $flag = flag_get_flag($flag_name);
72  $skip_permission_check = (boolean) $skip_permission_check;
73  return $flag->flag($action, $content_id, $account, $skip_permission_check);
74}
75
76/**
77 * Service wrapper to check if a content is flagged by a user.
78 *
79 * @param $flag_name
80 *   The flag name.
81 * @param $content_id
82 *   The content ID to check if it was flagged.
83 * @param $uid
84 *   The user ID to check if they flagged the content.
85 * @return
86 *   TRUE if content was flagged.
87 */
88function flag_service_is_flagged($flag_name, $content_id, $uid = NULL) {
89  $flag = flag_get_flag($flag_name);
90  return $flag->is_flagged($content_id, $uid);
91}
Note: See TracBrowser for help on using the browser.