aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2011-09-24 20:42:57 -0300
committerSilvio Rhatto <rhatto@riseup.net>2011-09-24 20:42:57 -0300
commit5cd0054058a718e5694c8542f72f586f06b69fc8 (patch)
tree25c384738c580bd5e9b05a0943be1e79b82dce9d
parent4f15de058dc6dadee6e79d16b441202b54161f02 (diff)
downloadmuamba-5cd0054058a718e5694c8542f72f586f06b69fc8.tar.gz
muamba-5cd0054058a718e5694c8542f72f586f06b69fc8.tar.bz2
Basic working code for an item request
-rw-r--r--muamba.business.inc39
-rw-r--r--muamba.install13
-rw-r--r--muamba.module5
3 files changed, 43 insertions, 14 deletions
diff --git a/muamba.business.inc b/muamba.business.inc
index f7b9e05..59e4bd1 100644
--- a/muamba.business.inc
+++ b/muamba.business.inc
@@ -12,30 +12,43 @@
* Requested item.
*/
function muamba_request($nid) {
- // Sanitize
+ global $user;
$nid = (int) $nid;
$node = node_load($nid);
- if (!$node || $node->type != MUAMBA_NODE_TYPE) {
+ // Access check
+ if (!$node || $node->type != MUAMBA_NODE_TYPE || !node_access('view', $node)) {
drupal_not_found();
}
- global $user;
-
- // TODO: check if user has permission to access the node.
- // TODO: check if user is not blocked by privatemsg?
+ // Check if user is blocked by the item owner
+ if (module_exists('pm_block_user')) {
+ $owner = user_load($node->uid);
+ if (pm_block_user_has_blocked($user, $owner)) {
+ return t('The item owner has blocked you from asking this item.');
+ }
+ }
// Check if user already requested the item
if (muamba_check_user_request($nid, $user->uid)) {
- // TODO
+ return t('You already requested this item.');
}
// Notify item owner
$thread = privatemsg_new_thread(array(user_load($node->uid)), t('Item request'), 'User has requested an item');
- $thread_id = $thread['message']['thread_id'];
+ $thread_id = $thread['message']->thread_id;
// Issue item request
- // TODO
+ $request = db_insert('muamba')
+ ->fields(
+ array(
+ 'nid' => $nid,
+ 'uid' => $user->uid,
+ 'status' => MUAMBA_REQUESTED,
+ 'thread_id' => $thread_id,
+ )
+ )
+ ->execute();
// User output
return t('You have requested an item');
@@ -54,15 +67,15 @@ function muamba_request($nid) {
* TRUE if user already requested an item, FALSE otherwise.
*/
function muamba_check_user_request($nid, $uid) {
- if (!is_int($nid) || !is_int($uid)) {
- return FALSE;
- }
+ $nid = (int) $nid;
+ $uid = (int) $uid;
$query = db_select('muamba', 'm');
$query
->condition('m.nid', $nid, '=')
- ->condition('m.uid', $uid, '=');
+ ->condition('m.uid', $uid, '=')
+ ->condition('m.status', MUAMBA_REQUESTED, '=');
$result = $query->countQuery()->execute()->fetchField();
diff --git a/muamba.install b/muamba.install
index e6b3ead..4d2ba42 100644
--- a/muamba.install
+++ b/muamba.install
@@ -89,7 +89,6 @@ function muamba_update_7000(&$sandbox) {
array(
'description' => t('The primary identifier for a muamba transaction.'),
'type' => 'int',
- 'unsigned' => TRUE,
'not null' => TRUE,
)
);
@@ -124,3 +123,15 @@ function muamba_update_7000(&$sandbox) {
)
);
}
+
+/**
+ * Adds autoincrement to mid field.
+ */
+function muamba_update_7001(&$sandbox) {
+ db_change_field('muamba', 'mid', 'mid',
+ array(
+ 'type' => 'serial',
+ 'not null' => TRUE,
+ )
+ );
+}
diff --git a/muamba.module b/muamba.module
index 411ee07..445b0c3 100644
--- a/muamba.module
+++ b/muamba.module
@@ -9,6 +9,11 @@
* Definitions.
*/
define('MUAMBA_NODE_TYPE', 'muamba');
+define('MUAMBA_REQUESTED', 0);
+define('MUAMBA_ACCEPTED', 1);
+define('MUAMBA_REJECTED', 2);
+define('MUAMBA_RELEASED', 3);
+define('MUAMBA_RETURNED', 4);
/**
* Implements hook_permission()