]> gitweb.fluxo.info Git - drupal/muamba.git/commitdiff
Basic working code for an item request
authorSilvio Rhatto <rhatto@riseup.net>
Sat, 24 Sep 2011 23:42:57 +0000 (20:42 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Sat, 24 Sep 2011 23:42:57 +0000 (20:42 -0300)
muamba.business.inc
muamba.install
muamba.module

index f7b9e05e90ffc0b86378b8f69daa41d6f6f89673..59e4bd1945c31fa21193cf013cd9774f3323e6ea 100644 (file)
  *   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();
 
index e6b3ead1dfeaa16046ccd990f2096563071ebbec..4d2ba42869e4897334b778fe3b4e4eaea852b52c 100644 (file)
@@ -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,
+    )
+  );
+}
index 411ee0773cdb0e6f9f4654b1f1c942f439bc8f39..445b0c3cdd4901f7c598108bd42672268d0ad286 100644 (file)
@@ -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()