]> gitweb.fluxo.info Git - drupal/muamba.git/commitdiff
Enhancing request/accept
authorSilvio Rhatto <rhatto@riseup.net>
Tue, 4 Oct 2011 16:10:59 +0000 (13:10 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Tue, 4 Oct 2011 16:10:59 +0000 (13:10 -0300)
muamba.business.inc
muamba.db.inc
muamba.misc.inc
muamba.module
muamba.theme.inc

index b68443c559d6ab96ac931e717519cca38b365d72..4d4bc5be53c0dbb595f816ffed66814c66de437a 100644 (file)
@@ -5,10 +5,6 @@
  * Business logic for Muamba.
  */
 
-// Load requirements.
-include_once('muamba.db.inc');
-include_once('muamba.misc.inc');
-
 /**
  * Determine possible actions for a transaction.
  *
@@ -96,16 +92,13 @@ function muamba_request($nid) {
   $node = node_load($nid);
 
   // Access check
-  if (!$node || $node->type != MUAMBA_NODE_TYPE || !node_access('view', $node)) {
+  if (!muamba_has_request_access($node)) {
     drupal_not_found();
   }
 
   // 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.');
-    }
+  if (muamba_user_has_blocked($node, $user)) {
+    return t('The item owner has blocked you from asking this item.');
   }
 
   // Check if user already requested the item
@@ -113,21 +106,31 @@ function muamba_request($nid) {
     return t('You already requested this item.');
   }
 
+  // Create transaction
+  $transaction = array(
+    'nid'    => $nid,
+    'owner'  => $node->uid,
+    'uid'    => $user->uid,
+    'status' => MUAMBA_REQUESTED,
+  );
+
+  // Issue item request
+  $transaction['mid'] = db_insert('muamba')
+    ->fields($transaction)
+    ->execute();
+
   // Notify item owner
-  $thread = privatemsg_new_thread(array(user_load($node->uid)), t('Item request'), 'User has requested an item');
+  $subject   = t('Item request: @title', array('@title' => check_plain($node->title)));
+  $message   = theme('muamba_request_message', array('transaction' => $transaction));
+  $thread    = privatemsg_new_thread(array(user_load($node->uid)), $subject , $message);
   $thread_id = $thread['message']->thread_id;
 
-  // Issue item request
-  $request = db_insert('muamba')
-    ->fields(
-      array(
-        'nid'       => $nid,
-        'owner'     => $node->uid,
-        'uid'       => $user->uid,
-        'status'    => MUAMBA_REQUESTED,
-        'thread_id' => $thread_id,
-      )
-    )
+  // Update request with thread id
+  $update = db_update('muamba')
+    ->fields(array(
+      'thread_id' => $thread_id,
+    ))
+    ->condition('mid', $transaction['mid'], '=')
     ->execute();
 
   // User output
@@ -146,17 +149,28 @@ function muamba_accept($mid) {
   global $user;
   $mid         = (int) $mid;
   $transaction = muamba_get_transaction($mid);
+  $node        = node_load($transaction->nid);
 
   // Access check
-  // TODO: also check if user owns the item
-  $node = node_load($transaction->nid);
-  if (!$node || $node->type != MUAMBA_NODE_TYPE || !node_access('view', $node)) {
+  if (!muamba_has_management_access($node)) {
+    drupal_not_found();
+  }
+
+  // Status check
+  if ($transaction->status != MUAMBA_REQUESTED) {
     drupal_not_found();
   }
 
-  // TODO
   // Update database
+  $update = db_update('muamba')
+    ->fields(array(
+      'status' => MUAMBA_ACCEPTED,
+    ))
+    ->condition('mid', $mid, '=')
+    ->execute();
+
   // Notify item owner
+  // TODO
 
   return t('Accepted item request.');
 }
index e8e1c708b9b554736461867ca9bdd6880bfeedec..224132f202aac82575f3a14bc0dac4a3a8275fc9 100644 (file)
@@ -82,17 +82,22 @@ function muamba_get_transactions($uid, $type = 'sent', $status = NULL) {
 /**
  * Get a single transaction.
  *
- * @param $mid
- *   Transaction id.
+ * @param $data
+ *   Transaction id or node object.
  *
  * @return
  *   Transaction data.
  */
-function muamba_get_transaction($mid) {
-  $mid = (int) $mid;
+function muamba_get_transaction($data) {
   $query = db_select('muamba', 'm');
   $query->fields('m', array('mid', 'nid', 'uid', 'owner', 'status', 'thread_id'));
-  $query->condition('m.mid', $mid, '=');
+
+  if (is_object($data)) {
+    $query->condition('m.nid', $data->nid, '=');
+  }
+  else {
+    $query->condition('m.mid', (int) $data, '=');
+  }
 
   $rows    = array();
   $results = $query->execute()->fetchAll();
@@ -102,5 +107,7 @@ function muamba_get_transaction($mid) {
     $rows[] = array_map('check_plain', (array) $entry);
   }
 
-  return $rows[0];
+  if (isset($rows[0])) {
+    return $rows[0];
+  }
 }
index 4b2d2e65921793e5a36662d055e422691c7b6161..8c26159f9b7affd996e32f02a40e8ce234339ce9 100644 (file)
@@ -78,3 +78,67 @@ function muamba_actions($code = NULL) {
 
   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;
+}
index 4e86abd34ab4c9fa1736b9100eb14a464c00c139..2a45c709679330be5ff7dfde50ce0558c5a4bf19 100644 (file)
@@ -16,6 +16,10 @@ define('MUAMBA_RELEASED', 3);
 define('MUAMBA_RETURNED', 4);
 define('MUAMBA_CANCELLED', 4);
 
+// Load requirements.
+include_once('muamba.misc.inc');
+include_once('muamba.db.inc');
+
 /**
  * Implements hook_permission()
  */
@@ -93,22 +97,19 @@ function muamba_menu() {
 
 /**
  * Implements hook_node_view()
- *
- * @todo
- *   Check widget status
- *   Check permissions
  */
 function muamba_node_view($node, $view_mode, $langcode) {
   global $user;
 
-  // Do not show widget to the owner or on non-muamba content types
-  if ($node->uid == $user->uid || $node->type != MUAMBA_NODE_TYPE) {
+  // Check if widget can be shown
+  if ($node->uid == $user->uid || $node->type != MUAMBA_NODE_TYPE || !muamba_has_request_access($node)) {
     return;
   }
 
   if ($view_mode == 'full') {
+    $transaction             = muamba_get_transaction($node);
     $node->content['muamba'] = array(
-      '#markup' => theme('muamba_widget', array('nid' => $node->nid)),
+      '#markup' => theme('muamba_widget', array('nid' => $node->nid, 'transaction' => $transaction)),
       '#weight' => 100,
     );
 
@@ -123,7 +124,10 @@ function muamba_theme($existing, $type, $theme, $path) {
   return array(
     'muamba_widget' => array(
       'template'  => 'muamba-widget',
-      'variables' => array('nid' => NULL),
+      'variables' => array(
+        'nid'         => NULL,
+        'transaction' => NULL, 
+      ),
     ),
     'muamba_powered' => array(
       'template' => 'muamba-powered',
@@ -133,14 +137,20 @@ function muamba_theme($existing, $type, $theme, $path) {
         'transactions' => NULL,
         'type'         => NULL,
       ),
-      'file'      => 'muamba.theme.inc',
+      'file' => 'muamba.theme.inc',
     ),
     'muamba_colorbox_link' => array(
       'variables' => array(
         'path' => NULL,
         'text' => NULL,
       ),
-      'file'      => 'muamba.theme.inc',
+      'file' => 'muamba.theme.inc',
+    ),
+    'muamba_request_message' => array(
+      'variables' => array(
+        'transaction' => NULL,
+      ),
+      'file' => 'muamba.theme.inc',
     ),
   );
 }
index 625407464dbb4d914b3d001555ff6f84ef972182..5fc3b192ddb41b7033721c5b8162d56637cec64d 100644 (file)
@@ -81,3 +81,12 @@ function theme_muamba_colorbox_link($variables) {
 
   return $output;
 }
+
+/**
+ * Theme callback.
+ *
+ * @todo
+ */
+function theme_muamba_request_message($transaction = NULL) {
+  return t('User has requested an item');
+}