aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2011-10-04 13:10:59 -0300
committerSilvio Rhatto <rhatto@riseup.net>2011-10-04 13:10:59 -0300
commitf47b73d4bc46351a2ed6fdb842ce452b815df58e (patch)
treeb3618ceb3918b13661d0e394b8cd32cdfcac5b5a
parenta3336da7dd92a2b8d31f2f33a6106a1802b5a387 (diff)
downloadmuamba-f47b73d4bc46351a2ed6fdb842ce452b815df58e.tar.gz
muamba-f47b73d4bc46351a2ed6fdb842ce452b815df58e.tar.bz2
Enhancing request/accept
-rw-r--r--muamba.business.inc66
-rw-r--r--muamba.db.inc19
-rw-r--r--muamba.misc.inc64
-rw-r--r--muamba.module30
-rw-r--r--muamba.theme.inc9
5 files changed, 146 insertions, 42 deletions
diff --git a/muamba.business.inc b/muamba.business.inc
index b68443c..4d4bc5b 100644
--- a/muamba.business.inc
+++ b/muamba.business.inc
@@ -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.');
}
diff --git a/muamba.db.inc b/muamba.db.inc
index e8e1c70..224132f 100644
--- a/muamba.db.inc
+++ b/muamba.db.inc
@@ -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];
+ }
}
diff --git a/muamba.misc.inc b/muamba.misc.inc
index 4b2d2e6..8c26159 100644
--- a/muamba.misc.inc
+++ b/muamba.misc.inc
@@ -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;
+}
diff --git a/muamba.module b/muamba.module
index 4e86abd..2a45c70 100644
--- a/muamba.module
+++ b/muamba.module
@@ -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',
),
);
}
diff --git a/muamba.theme.inc b/muamba.theme.inc
index 6254074..5fc3b19 100644
--- a/muamba.theme.inc
+++ b/muamba.theme.inc
@@ -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');
+}