aboutsummaryrefslogtreecommitdiff
path: root/muamba.misc.inc
blob: 70aa5988369ca517a59d84d68a4d11d3be6afc07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

/**
 * @fils
 * Miscelaneous functions.
 */

// Load requirements.
include_once('muamba.misc.inc');

/**
 * Builds detailed transaction information.
 *
 * @param $transactions
 *   Array with transactions.
 *
 * @return
 *   Array with summarized transactions.
 */
function muamba_transactions_summary($transactions) {
  for ($n = 0; $n < sizeof($transactions); $n++) {
    $transactions[$n]['node']  = node_load($transactions[$n]['nid']);
    $transactions[$n]['user']  = user_load($transactions[$n]['uid']);
    $transactions[$n]['owner'] = user_load($transactions[$n]['owner']);
  }

  return $transactions;
}

/**
 * Get status descriptions.
 *
 * @param $code
 *   Optional status code.
 *
 * @return
 *   Array of status descriptions or code
 *   description if $code is supplied.
 */
function muamba_statuses($code = NULL) {
  $status = array(
    MUAMBA_REQUESTED => 'requested',
    MUAMBA_ACCEPTED  => 'accepted',
    MUAMBA_REJECTED  => 'rejected',
    MUAMBA_CANCELLED => 'cancelled',
    MUAMBA_RETURNED  => 'returned',
    MUAMBA_RECOVERED => 'recovered',
    MUAMBA_LOST      => 'lost',
  );

  if ($code == NULL) {
    return $status;
  }

  return $status[$code];
}

/**
 * Get status callback name.
 *
 * @param $code
 *   Optional status code.
 *
 * @return
 *   Array of callback names or callback
 *   name if $code is supplied.
 */
function muamba_actions($code = NULL) {
  $status = array(
    MUAMBA_REQUESTED => 'request',
    MUAMBA_ACCEPTED  => 'accept',
    MUAMBA_REJECTED  => 'reject',
    MUAMBA_CANCELLED => 'cancel',
    MUAMBA_RETURNED  => 'return',
    MUAMBA_RECOVERED => 'recover',
    MUAMBA_LOST      => 'lost',
  );

  if ($code === NULL) {
    return $status;
  }

  return $status[$code];
}

/**
 * Check if an user has request access.
 *
 * @param $node
 *   Requested node.
 *
 * @return
 *   TRUE on success, FALSE otherwise.
 */
function muamba_has_request_access($node) {
  if (!$node || $node->type != MUAMBA_NODE_TYPE || !node_access('view', $node)) {
    return FALSE;
  }

  return TRUE;
}

/**
 * Check if user has been blocked by an item owner.
 *
 * @param $node
 *   Requested node.
 *
 * @param $user
 *   User to be checked against.
 *
 * @return
 *   TRUE if block exists, FALSE otherwise.
 */
function muamba_user_has_blocked($node, $user) {
  if (module_exists('pm_block_user')) {
    $owner = user_load($node->uid);
    if (pm_block_user_has_blocked($user, $owner)) {
      return TRUE;
    }
  }

  return FALSE;
}

/**
 * Check if an user has management privileges for an item.
 *
 * @param $node
 *   Requested node.
 *
 * @param $user
 *   Optional user object, defaults to the logged in user.
 *
 * @return
 *   TRUE on access, FALSE otherwise.
 */
function muamba_has_management_access($node, $user = NULL) {
  if ($user == NULL) {
    global $user;
  }

  if (!$node || $node->type != MUAMBA_NODE_TYPE || $node->uid != $user->uid) {
    return FALSE;
  }

  return TRUE;
}