/[drupal]/contributions/modules/ilovethis/ilovethis.module
ViewVC logotype

Contents of /contributions/modules/ilovethis/ilovethis.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Thu Nov 15 13:44:55 2007 UTC (2 years ago) by finley
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit of ilovethis module. This module allows user to say 'I love this page'.
1 <?php
2 // $Id: ilovethis.module,v 1.5 2007/01/23 15:27:08 finley Exp $
3
4
5 define('ILOVETHIS_PERM_ADD', 'create ilovethis');
6 define('ILOVETHIS_PERM_ADMINISTER', 'administer ilovethis');
7 define('ILOVETHIS_PERM_LIST', 'read \'who love this page\' list');
8
9
10 define('ILOVETHIS_NODE_TYPE', 'ilovethis_node_type_');
11 define('ILOVETHIS_PERM_VIEW', 'view ilovethis');
12
13 define('ILOVETHIS_BLOCK', 'ilovethis_block_type_');
14 define('ILOVETHIS_BLOCK_LIMIT', 'ilovethis_block_limit');
15 define('ILOVETHIS_BLOCK_TITLE', 'ilovethis_block_title');
16 define('ILOVETHIS_PAGE_LIMIT', 'ilovethis_page_limit');
17 define('ILOVETHIS_PROFILE_LIMIT', 'ilovethis_profile_limit');
18 define('ILOVETHIS_PAGE_TYPE', 'ilovethis_page_type');
19
20
21 function ilovethis_perm() {
22 return array( ILOVETHIS_PERM_ADD, ILOVETHIS_PERM_ADMINISTER, ILOVETHIS_PERM_LIST);
23 }
24
25
26 /**
27 * Display help and module information
28 * @param section which section of the site we're displaying help
29 * @return help text for section
30 */
31 function ilovethis_help($section='') {
32 $output = '';
33 switch ($section) {
34 case "admin/help#ilovethis":
35 $output = '<p>'. t("Allow user say I love this node"). '</p>';
36 break;
37 }
38
39 return $output;
40 }
41
42 function ilovethis_settings() {
43 $set = 'page';
44 $form[$set] = array(
45 '#type' => 'fieldset',
46 '#title' => t('I love this Page Settings'),
47 '#collapsible' => true,
48 '#collapsed' => false,
49 );
50 $form[$set][ILOVETHIS_PAGE_LIMIT] = array(
51 '#type' => 'textfield',
52 '#title' => t('I love this Page Limit'),
53 '#default_value' => variable_get(ILOVETHIS_PAGE_LIMIT, 10),
54 '#description' => t('How many items to display on a single page of marked loved nodes.'),
55 );
56 $form[$set][ILOVETHIS_PROFILE_LIMIT] = array(
57 '#type' => 'textfield',
58 '#title' => t('I love this Profile Limit'),
59 '#default_value' => variable_get(ILOVETHIS_PROFILE_LIMIT, 3),
60 '#description' => t('How many items per type to display on the profile page.'),
61 );
62 $form[$set][ILOVETHIS_PAGE_TYPE] = array(
63 '#type' => 'select',
64 '#title' => t('Type of Page Display for Loved Nodes'),
65 '#options' => array(
66 'table' => 'Table',
67 'teasers' => 'Teasers',
68 ),
69 '#default_value' => variable_get(ILOVETHIS_PAGE_TYPE, 'table'),
70 '#description' => t('How should loved nodes be displayed on the i love these nodes page?'),
71 );
72
73 $set = 'types';
74 $form[$set] = array(
75 '#type' => 'fieldset',
76 '#title' => t('Enable \'I love this\' for these node types'),
77 '#collapsible' => true,
78 '#collapsed' => false,
79 );
80 foreach(node_get_types() as $type) {
81 $form[$set][ILOVETHIS_NODE_TYPE . $type->type] = array(
82 '#type' => 'checkbox',
83 '#title' => $type->name,
84 '#return_value' => 1,
85 '#default_value' => variable_get(ILOVETHIS_NODE_TYPE . $type->type, 0),
86 );
87 }
88 return system_settings_form($form);
89 }
90
91
92 function ilovethis_menu($may_cache) {
93 $items = array();
94
95 if ($may_cache) {
96 $items[] = array(
97 'path' => 'ilovethis/add',
98 'callback' => 'ilovethis_add_page',
99 'type' => MENU_CALLBACK,
100 'access' => user_access(ILOVETHIS_PERM_ADD),
101 );
102
103 $items[] = array(
104 'path' => 'ilovethis/delete',
105 'callback' => 'ilovethis_delete_page',
106 'type' => MENU_CALLBACK,
107 'access' => user_access(ILOVETHIS_PERM_ADD),
108 );
109
110 $items[] = array(
111 'path' => 'ilovethis/view',
112 'callback' => 'ilovethis_view_page',
113 'type' => MENU_NORMAL_ITEM,
114 'access' => user_access(ILOVETHIS_PERM_LIST),
115 );
116
117
118
119
120 $items[] = array(
121 'title' => 'ilovethis settings',
122 'path' => 'admin/settings/ilovethis',
123 'description' => 'Settings for ilovethis',
124 'callback' => 'drupal_get_form',
125 'callback arguments' => 'ilovethis_settings',
126 'type' => MENU_NORMAL_ITEM,
127 'access' => user_access(ILOVETHIS_PERM_ADMINISTER),
128 );
129 }
130 return $items;
131 }
132
133 /**
134 * Implementation of hook_nodeapi().
135 */
136 function ilovethis_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
137 drupal_add_css(drupal_get_path('module', 'ilovethis') .'/ilovethis.css');
138 switch ($op) {
139 case 'delete':
140 // Delete all loved entries of the node being deleted.
141 db_query("DELETE FROM {ilovethis} WHERE nid = %d", $node->nid);
142 break;
143 case 'view':
144 // add widget to the begin of the node body
145 $node->content['body']['#value'] = ilovethis_widget($node->type, $node->nid, $view_all = 1). $node->content['body']['#value'];
146 break;
147 }
148 }
149
150
151 /**
152 * Add a loved node.
153 */
154 function ilovethis_add($nid) {
155 global $user;
156
157 $node = node_load($nid);
158
159 if ($node->nid) {
160 db_query("DELETE FROM {ilovethis} WHERE nid = %d AND uid = %d", $nid, $user->uid);
161 db_query("INSERT INTO {ilovethis} (nid, uid, last) VALUES (%d, %d, %d)", $nid, $user->uid, time());
162
163 return true;
164 } else {
165 return false;
166 }
167 }
168
169
170 /**
171 * Delete a loved node.
172 */
173 function ilovethis_delete($nid, $voter) {
174 global $user;
175 db_query("DELETE FROM {ilovethis} WHERE nid = %d AND uid = %d", $nid, $voter);
176 }
177
178
179 /**
180 * Select all the loved nodes a user has.
181 */
182 function ilovethis_get($nid) {
183 if ($nid)
184 {
185 if (is_null($limit)) {
186 $limit = variable_get(ILOVETHIS_PAGE_LIMIT, 10);
187 }
188 $row = array();
189 $sql = "SELECT i.uid, i.last FROM {ilovethis} i WHERE i.nid = '". $nid."' ORDER by i.last DESC";
190 $result = pager_query($sql, $limit);
191 if ($result && db_num_rows($result) > 0) {
192 while ($data = db_fetch_object($result)) {
193 $row[$data->nid.$data->last] = $data;
194 }
195 }
196 }
197 return $row;
198 }
199
200 /**
201 * Page to add a loved node.
202 */
203 function ilovethis_add_page() {
204 $nid = arg(2);
205 if (is_numeric($nid)) {
206 $result = ilovethis_add($nid);
207 if ($result) {
208 drupal_set_message(t('The node was added to your loved pages.'));
209 }
210 drupal_goto('node/'. $nid);
211 }
212 drupal_not_found();
213 }
214
215
216 /**
217 * Page to delete a loved node.
218 */
219 function ilovethis_delete_page() {
220 global $user;
221 $nid = arg(2);
222 $voter = arg(3);
223 ilovethis_delete($nid, $voter);
224 drupal_set_message(t('The node was deleted from your loved pages'));
225 drupal_goto('node/'. $nid);
226 }
227
228
229 /**
230 * Page to display a user's loved node list.
231 */
232 function ilovethis_view_page() {
233 $nid = arg(2);
234 $output = theme('ilovethis_view_' . variable_get(ILOVETHIS_PAGE_TYPE, 'table'), ilovethis_get($nid), $nid);
235 print theme('page', $output);
236 }
237
238
239
240 /**
241 * Delete loved nodes a user has.
242 */
243 function ilovethis_delete_loved($uid) {
244 db_query("DELETE FROM {ilovethis} WHERE uid = %d", $uid);
245 }
246
247
248 /**
249 * Check if a user already has $nid node in their loved list.
250 */
251 function _ilovethis_check($nid) {
252 global $user;
253 $sql = "SELECT COUNT(*) FROM {ilovethis} WHERE uid = %d AND nid = %d";
254 return db_result(db_query($sql, $user->uid, $nid));
255 }
256
257
258 /**
259 * Table which displays loved node lists.
260 */
261 function theme_ilovethis_view_table($row = array(), $nid) {
262 global $user;
263
264 $node = array();
265 $node= node_load($nid);
266
267 $header = array( t('User'), t('Added'));
268
269 $rows = array();
270 if (isset($row)) {
271
272 unset ($you_love_this);
273 foreach($row as $nid => $data) {
274 $tmp_sql = "SELECT name FROM {users} WHERE uid = '".$data->uid."'";
275 $tmp_res = db_query(db_rewrite_sql($tmp_sql));
276 $username = db_fetch_object($tmp_res);
277 $username = $username->name;
278
279 $title = array( l($username, "user/".$data->uid) , format_date($data->last, 'custom', 'Y-m-d H:i'));
280 if ($user->uid == $data->uid || $user->uid == 1) {
281 $delete = array(l(t('delete'), "ilovethis/delete/".$node->nid. "/". $data->uid ));
282 $result = array_merge($title, $delete);
283 $header = array( t('User'), t('Added'), t('Action'));
284 }
285 else {
286 $result = $title;
287 }
288 $rows[] = array('data' => $result);
289
290 if ($user->uid == $data->uid){
291 $you_love_this=1;
292 }
293 }
294 }
295 $output .= '<h2 class=right>'. $node->title.'</h2>'."\n";
296 if (count($rows)>0) {
297 $output .="<div id='loved_list'>";
298 #print row below if you want to give an header to the "lovers" list
299 #$output .= '<h3>List of the user who joined this '.$node->type.'</h3>'."\n";
300 $output .= theme('table', $header, $rows);
301 $output .= theme('pager');
302 } else {
303 $output .="<div id='loved_list'>";
304 $output .="". t("None (yet) loves this" . " " . $node->type)."";
305 }
306 $output .= "<div style='float:left'>".ilovethis_widget($node->type, $node->nid, $view_all = false)."</div>";
307 $output .="</div>";
308 $output .="<div id='loved_text'>";
309 #print row below if you want to give an header to the "text node
310 #$output .="<h3>".t("Text")."</h3>";
311 $output .="<div class='content'>".$node->body."</div>";
312 $output .="</div>";
313
314 return $output;
315 }
316
317 function ilovethis_widget($type, $node = null, $view_all = false) {
318 global $user;
319 if ( !$teaser) {
320 if (variable_get(ILOVETHIS_NODE_TYPE . $type, 0)) {
321 $widget ="<div id=ilovethis>";
322 $widget .="<div class='ext' ><div class='int' >";
323 if (!_ilovethis_check($node)) {
324 $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))));
325 } else {
326 $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))));
327 }
328 $widget .="</div></div>";
329 if ($view_all==1){
330 $widget .="<p>(".l(t('who loves this')." " . ucfirst ($type) ,"ilovethis/view/".$node, array('class' => '', 'title' => t('List of people loving this '. ucfirst ($type)))) .")</p>";
331 }
332 $widget .= "</div>";
333 }
334 }
335 return $widget;
336 }

  ViewVC Help
Powered by ViewVC 1.1.2