Index: ilovethis.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ilovethis/ilovethis.module,v
retrieving revision 1.1
diff -u -r1.1 ilovethis.module
--- ilovethis.module	15 Nov 2007 13:44:55 -0000	1.1
+++ ilovethis.module	4 Jan 2008 13:57:17 -0000
@@ -142,7 +142,7 @@
       break;
    case 'view':
       // add widget to the begin of the node body	
-      $node->content['body']['#value'] =  ilovethis_widget($node->type, $node->nid, $view_all = 1). $node->content['body']['#value'];
+      $node->content['body']['#value'] =  ilovethis_widget($node, $teaser, true) . $node->content['body']['#value'];
       break; 
       }
 }
@@ -230,8 +230,29 @@
  * Page to display a user's loved node list.
  */
 function ilovethis_view_page() {
-  $nid  = arg(2);
-  $output = theme('ilovethis_view_' . variable_get(ILOVETHIS_PAGE_TYPE, 'table'), ilovethis_get($nid), $nid);
+  $node = node_load($nid = arg(2));
+
+  $header = array(t('User'), t('Added'));
+
+  $rows = array();
+  $row = ilovethis_get($nid);
+  if (isset($row)) {
+    foreach ($row as $nid => $data) {
+      $username = db_fetch_object(db_query("SELECT name FROM {users} WHERE uid = %d", $data->uid));
+      $title = array(l($username->name, 'user/'. $data->uid), format_date($data->last, 'custom', 'Y-m-d H:i'));
+      global $user;
+      if ($user->uid == $data->uid || $user->uid == 1) {
+        $delete = array(l(t('delete'), 'ilovethis/delete/'. $node->nid. '/'. $data->uid));
+        $result = array_merge($title, $delete);
+        $header[] = t('Action');
+      }
+      else {
+        $result = $title;
+      }
+      $rows[] = array('data' => $result);
+    }
+  }
+  $output = theme('ilovethis_view_' . variable_get(ILOVETHIS_PAGE_TYPE, 'table'), $node, $header, $rows);
   print theme('page', $output);
 }
 
@@ -258,79 +279,41 @@
 /**
  * Table which displays loved node lists.
  */
-function theme_ilovethis_view_table($row = array(), $nid) {
-  global $user;
-
-  $node = array();
-  $node= node_load($nid);
-
-  $header = array( t('User'), t('Added'));
-
-  $rows = array();
-  if (isset($row)) {
-
-  unset ($you_love_this);
-  foreach($row as $nid => $data) {
-    $tmp_sql = "SELECT name FROM {users} WHERE uid = '".$data->uid."'";
-    $tmp_res = db_query(db_rewrite_sql($tmp_sql));
-    $username = db_fetch_object($tmp_res);
-    $username = $username->name;
-
-    $title = array(  l($username, "user/".$data->uid) , format_date($data->last, 'custom', 'Y-m-d H:i'));
-    if ($user->uid == $data->uid || $user->uid == 1) {
-      $delete = array(l(t('delete'), "ilovethis/delete/".$node->nid. "/". $data->uid ));
-      $result = array_merge($title, $delete);
-      $header = array( t('User'), t('Added'), t('Action'));
-      }
-      else {
-      $result = $title;
-      }
-    $rows[] = array('data' => $result);
+function theme_ilovethis_view_table($node, $header, $rows) {
+  if (count($rows) > 0) {
+    $ilovethis .= theme('table', $header, $rows);
+    $ilovethis .= theme('pager');
+  }
+  else {
+    $ilovethis .= t('None (yet) loves this !type', array('!type' => $node->type));
+  }
+  $ilovethis .= '<div style="float:left">'. ilovethis_widget($node) .'</div>';
+  $output .= '<h2 class=right>'. $node->title. '</h2>';
+  $output .= '<div id="loved_list">'. $ilovethis .'</div>';
+  $output .= '<div id="loved_text"><div class="content">'. $node->body .'</div></div>';
+  return $output;
+}
 
-    if ($user->uid == $data->uid){
-	$you_love_this=1;
-      }	
-    }
-  }
-  $output .= '<h2 class=right>'. $node->title.'</h2>'."\n";
-  if (count($rows)>0) {
-    $output .="<div id='loved_list'>";
-    #print row below if you want to give an header to the "lovers" list
-    #$output .= '<h3>List of the user who joined this '.$node->type.'</h3>'."\n";  
-    $output .= theme('table', $header, $rows);
-    $output .= theme('pager');
-  } else {
-    $output .="<div id='loved_list'>";
-    $output .="". t("None (yet) loves this" . " " . $node->type)."";	
+function ilovethis_widget($node, $teaser = false, $view_all = false) {
+  if (!$teaser && variable_get(ILOVETHIS_NODE_TYPE . $node->type, 0) && user_access(ILOVETHIS_PERM_ADD)) {
+    return theme('ilovethis_widget', $node, $view_all && user_access(ILOVETHIS_PERM_LIST), _ilovethis_check($node->nid));
   }
-  $output .= "<div style='float:left'>".ilovethis_widget($node->type, $node->nid, $view_all = false)."</div>";
-  $output .="</div>";
-  $output .="<div id='loved_text'>";
-  #print row below if you want to give an header to the "text node
-  #$output .="<h3>".t("Text")."</h3>";
-  $output .="<div class='content'>".$node->body."</div>";
-  $output .="</div>";
-
-  return $output;
 }
 
-function ilovethis_widget($type, $node = null, $view_all = false) {
+function theme_ilovethis_widget($node, $view_all, $check) {
   global $user;
-  if ( !$teaser) {
-    if (variable_get(ILOVETHIS_NODE_TYPE . $type, 0)) {
-      $widget ="<div id=ilovethis>";
-      $widget .="<div  class='ext' ><div class='int' >";
-      if (!_ilovethis_check($node)) {
-        $widget .= l(t('I love this '.ucfirst ($type)), 'ilovethis/add/'. $node, array('class' => '', 'title' => t('Click here to give your love to this ' . ' '.  ucfirst ($type))));
-      } else {
-	$widget .= l(t('I don\'t love this') ." " .  ucfirst ($type)   , 'ilovethis/delete/'. $node ."/". $user->uid, array('class' => '',  'title' => t('Click here to remove your love from this'. " " .ucfirst ($type))));
-      }
-      $widget .="</div></div>";
-      if ($view_all==1){
-        $widget .="<p>(".l(t('who loves this')." " .  ucfirst ($type)  ,"ilovethis/view/".$node, array('class' => '', 'title' => t('List of people loving this '. ucfirst ($type)))) .")</p>";
-      }
-      $widget .= "</div>";
-    }
+  if ($check) {
+    $link = l(t("I don't love this !type", array('!type' => ucfirst($node->type))), 'ilovethis/delete/'. $node->nid ."/". $user->uid, array('class' => '',  'title' => t('Click here to remove your love from this !type', array('!type' => ucfirst($node->type)))));
   }
-  return $widget;
+  else {
+    $link = l(t('I love this !type', array('!type' => ucfirst($node->type))), 'ilovethis/add/'. $node->nid, array('class' => '', 'title' => t('Click here to give your love to this !type', array('!type' => ucfirst($node->type)))));
+  }
+  $output = '<div class="ext"><div class="int">'. $link .'</div></div>';
+
+  if ($view_all) {
+    $link = l(t('who loves this !type', array('!type' => ucfirst($node->type))), 'ilovethis/view/'. $node->nid, array('class' => '', 'title' => t('List of people loving this !type', array('!type' => ucfirst($node->type)))));
+    $output .= '<p>('. $link .')</p>';
+  }
+
+  return '<div id="ilovethis">'. $output .'</div>';
 }
