/[drupal]/contributions/modules/buddylist2/buddylist_ui/buddylist_ui.module
ViewVC logotype

Diff of /contributions/modules/buddylist2/buddylist_ui/buddylist_ui.module

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

revision 1.6.2.3, Tue Jul 29 07:32:59 2008 UTC revision 1.6.2.4, Tue Sep 2 13:16:04 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: buddylist_ui.module,v 1.6.2.2 2008/07/25 16:49:43 nodestroy Exp $  // $Id: buddylist_ui.module,v 1.6.2.3 2008/07/29 07:32:59 nodestroy Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 22  function buddylist_ui_menu() { Line 22  function buddylist_ui_menu() {
22    $items = array();    $items = array();
23    
24    /*    /*
25       * Buddy action links
26       */
27      $items['buddylist/add/%user'] = array(
28        'title'  => 'Add relationship',
29        'access callback' => 'user_access',
30        'access arguments' => array('use buddylist'),
31        'page callback' => 'drupal_get_form',
32        'page arguments' => array('buddylist_ui_add_form', 2),
33        'type' => MENU_CALLBACK,
34      );
35      $items['buddylist/delete/%user/%relation'] = array(
36        'title'  => 'Delete relationship',
37        'access callback' => 'user_access',
38        'access arguments' => array('use buddylist'),
39        'page callback' => 'drupal_get_form',
40        'page arguments' => array('buddylist_ui_delete_form', 2, 3),
41        'type' => MENU_CALLBACK,
42      );
43      $items['buddylist/cancel/%user/%relation'] = array(
44        'title'  => 'Cancel relationship request',
45        'access callback' => 'user_access',
46        'access arguments' => array('use buddylist'),
47        'page callback' => 'drupal_get_form',
48        'page arguments' => array('buddylist_ui_cancel_request_form', 2, 3),
49        'type' => MENU_CALLBACK,
50      );
51      $items['buddylist/deny/%user/%relation'] = array(
52        'title'  => 'Deny relationship request',
53        'access callback' => 'user_access',
54        'access arguments' => array('use buddylist'),
55        'page callback' => 'drupal_get_form',
56        'page arguments' => array('buddylist_ui_deny_request_form', 2, 3),
57        'type' => MENU_CALLBACK,
58      );
59        $items['buddylist/accept/%user/%relation'] = array(
60        'title'  => 'Deny relationship request',
61        'access callback' => 'user_access',
62        'access arguments' => array('use buddylist'),
63        'page callback' => 'drupal_get_form',
64        'page arguments' => array('buddylist_ui_accept_request_form', 2, 3),
65        'type' => MENU_CALLBACK,
66      );
67    
68      /*
69     * Admin Items     * Admin Items
70     */     */
71    $items['admin/buddylist'] = array(    $items['admin/buddylist'] = array(
# Line 76  function buddylist_ui_menu() { Line 120  function buddylist_ui_menu() {
120   * Implementation of hook_perm().   * Implementation of hook_perm().
121   */   */
122  function buddylist_ui_perm() {  function buddylist_ui_perm() {
123    return array('administer buddylist');    return array('administer buddylist', 'use buddylist');
124    }
125    
126    /**
127     * Implementation of hook_user()
128     */
129    function buddylist_ui_user($op, &$edit, &$account, $category = NULL) {
130    
131      switch($op) {
132        case 'view':
133          global $user;
134          $viewer =&$user;
135    
136          if ($account->uid == $viewer->uid) {
137            break;
138          }
139    
140          $output = array();
141    
142          if ($actions = buddylist_ui_actions($viewer, $account)) {
143            $output['actions'] = array(
144              '#title'      => t('Buddylist actions'),
145              '#type'       => 'user_profile_item',
146              '#value'      => theme('item_list', $actions),
147            );
148          }
149    
150          if ($relations = buddylist_ui_relations($viewer, $account)) {
151            $output['relation'] = array(
152              '#title'      => t('Your relations to this user'),
153              '#type'       => 'user_profile_item',
154              '#value'      => theme('item_list', $relations),
155            );
156          }
157    
158          if ($pending = buddylist_ui_pending_relations($viewer, $account)) {
159            $output['pending_relation'] = array(
160              '#title'      => t('Your pending relations to this user'),
161              '#type'       => 'user_profile_item',
162              '#value'      => theme('item_list', $pending),
163            );
164          }
165    
166          if(sizeof($output)) {
167            $account->content['buddylist_ui'] = array(
168              '#type'   => 'user_profile_category',
169              '#title'  => t('Buddylist'),
170            );
171            $account->content['buddylist_ui'] = array_merge($account->content['buddylist_ui'], $output);
172          }
173        break;
174      }
175    }
176    
177    function buddylist_ui_pending_relations(&$viewer, &$viewed) {
178      $relations = buddylist_api_load_relationships_between($viewer, $viewed, FALSE, FALSE);
179      $output = array();
180    
181      foreach($relations as $relation) {
182        $rtype = buddylist_api_rtype_load($relation->rtid);
183    
184        if ($viewer->uid == $relation->init_id)
185          $output[] = $rtype->name .': '. l(t('Cancel Request'), 'buddylist/cancel/'. $viewed->uid .'/'. $relation->rid, array('query' => drupal_get_destination()));
186        else
187          $output[] = $rtype->name .': '. l(t('Accept Request'), 'buddylist/accept/'. $viewed->uid .'/'. $relation->rid, array('query' => drupal_get_destination())) .' | '. l(t('Deny Request'), 'buddylist/deny/'. $viewed->uid .'/'. $relation->rid, array('query' => drupal_get_destination()));
188      }
189    
190      return $output;
191    }
192    
193    function buddylist_ui_relations(&$viewer, &$viewed) {
194      $relations = buddylist_api_load_relationships_between($viewer, $viewed);
195      $output = array();
196    
197      foreach($relations as $relation) {
198        $rtype = buddylist_api_rtype_load($relation->rtid);
199        $output[] = $rtype->name .': '. l(t('Delete'), 'buddylist/delete/'. $viewed->uid .'/'. $relation->rid, array('query' => drupal_get_destination()));
200      }
201    
202      return $output;
203    }
204    
205    function buddylist_ui_actions(&$viewer, &$viewed) {
206      $output = array();
207    
208      $count_rtypes = sizeof(buddylist_api_rtypes_load());
209      if(buddylist_api_load_relationships_between($viewer, $viewed, TRUE) < $count_rtypes)
210        $output[] = l(t('Add a relation'), 'buddylist/add/'. $viewed->uid, array('query' => drupal_get_destination()));
211    
212      return $output;
213  }  }
214    
215  /**  /**
# Line 97  function buddylist_ui_disable() { Line 230  function buddylist_ui_disable() {
230      buddylist_api_rtype_delete($rtype);      buddylist_api_rtype_delete($rtype);
231  }  }
232    
233    /**
234     * Implementation of hook_load().
235     */
236    function rtype_load($rtid) {
237      static $rtypes = array();
238      $rtypes = buddylist_api_rtypes_load();
239    
240      if (!isset($rtypes[$rtid])) {
241        return FALSE;
242      }
243      return $rtypes[$rtid];
244    }
245    
246    function relation_load($rid) {
247      $relation = buddylist_api_load_relationships($rid, 'rid');
248      return $relation;
249    }
250    
251    
252  /**  /**
253   * FUNCTIONS   * FUNCTIONS
# Line 106  function buddylist_ui_disable() { Line 257  function buddylist_ui_disable() {
257   * Returns default relationtypes   * Returns default relationtypes
258   */   */
259  function buddylist_ui_get_default_rtypes() {  function buddylist_ui_get_default_rtypes() {
260    return array(    return (object)array(
261      'buddy' => array(      'buddy' => array(
262          'mname'  => 'buddylist_ui_buddy',
263        'name'   => 'buddy',        'name'   => 'buddy',
264        'oneway' => 1,        'oneway' => 0,
265      ),      ),
266      'fan' => array(      'fan' => array(
267          'mname'  => 'buddylist_ui_fan',
268        'name'   => 'fan',        'name'   => 'fan',
269        'oneway' => 0,        'oneway' => 1,
270      )      )
271    );    );
272  }  }
273    
274    function buddylist_ui_add_form($form_state, $account) {
275      global $user;
276      $count_rtypes = sizeof(buddylist_api_rtypes_load());
277    
278      if ($user->uid == $account->uid) {
279        drupal_set_message(t('Cannot add yourself to buddylist'));
280      } elseif (buddylist_api_load_relationships_between($user, $account, TRUE) == $count_rtypes) {
281        drupal_set_message(t('You have allready all possible buddy connections with this user'));
282      } else {
283              $current_relationships = buddylist_api_load_relationships_between($user, $account);
284    
285              $results = db_query(
286                "SELECT * FROM {buddylist_relationtypes}" . ($current_relationships ? " WHERE rtid NOT IN (%s) ORDER BY name" : ''),
287                implode(',', array_keys($current_relationships))
288              );
289    
290              $relationships = array();
291              while($result = db_fetch_object($results)) {
292                $relationships[$result->rtid] = $result->name;
293              }
294    
295              $form['rtid'] = array(
296                '#title'          => t('Relationship'),
297                '#type'           => 'select',
298                '#options'        => $relationships,
299                '#default_value'  => $form_state['rtid'],
300              );
301              $form['requester'] = array(
302                '#type'   => 'value',
303                '#value'  => $user
304              );
305              $form['requestee'] = array(
306                '#type'   => 'value',
307                '#value'  => $account
308              );
309    
310          $output = confirm_form($form, t('Add user to your buddylist?'), $_GET['destination'], NULL, t('Add'), t('Cancel'));
311    
312              return $output;
313      }
314    }
315    
316  /**  /**
317   * Loads rtype wildcard   * Process relationship request.
318   */   */
319  function rtype_load($rtid) {  function buddylist_ui_add_form_submit($form, &$form_state) {
320    static $rtypes = array();    $requester          = $form_state['values']['requester'];
321    $rtypes = buddylist_api_rtypes_load();    $requestee          = $form_state['values']['requestee'];
322      $relationship_type  = buddylist_api_rtype_load($form_state['values']['rtid']);
323    
324      $relationship = buddylist_api_request_relationship($requester, $requestee, $relationship_type);
325    
326    if (!isset($rtypes[$rtid])) {    if ($relationship === FALSE) {
327      return FALSE;      // error
328    }    }
329    return $rtypes[$rtid];  
330      $relationship->requester = $requester;
331      $relationship->requestee = $requestee;
332      $relationship->type = $relationship_type;
333    
334      // approv message
335      //drupal_set_message(user_relationships_ui_get_message(($relationship->approved ? 'pre_approved' : 'submitted'), $relationship));
336    
337      $form_state['redirect'] = $_GET['destination'];
338  }  }
339    
340  /**  function buddylist_ui_delete_form($form_state, $account, $relationship) {
341   * THEME    global $user;
342   */  
343      if (buddylist_api_load_relationships_between($user, $account, TRUE) == 0) {
344        drupal_set_message(t('You have no relationships to this user'));
345      } else {
346    
347          $form['rid'] = array(
348                '#type' => 'value',
349            '#value' => $relationship->rid,
350              );
351          $form['deleter'] = array (
352            '#type' => 'value',
353            '#value' => $user
354          );
355          $rtype = buddylist_api_rtype_load($relationship->rtid);
356          $output = confirm_form($form, t('Remove relation %relation to %username?', array('%relation' => $rtype->name, '%username' => $account->name)), $_GET['destination'], NULL, t('Delete'), t('Cancel'));
357          return $output;
358      }
359    }
360    
361  function buddylist_ui_theme() {  function buddylist_ui_delete_form_submit($form, &$form_state) {
362    return array(    $relation = buddylist_api_load_relationships($form_state['values']['rid'], 'rid');
363      buddylist_api_delete_relationship($relation, $form_state['values']['deleter']);
364    );  
365      $form_state['redirect'] = $_GET['destination'];
366    }
367    
368    function buddylist_ui_cancel_request_form($form_state, $account, $relationship) {
369      global $user;
370    
371      if (buddylist_api_load_relationships_between($user, $account, TRUE, FALSE ) == 0) {
372        drupal_set_message(t('You have no pending relationship with this user'));
373      } else {
374    
375          $form['rid'] = array(
376                '#type' => 'value',
377            '#value' => $relationship->rid,
378              );
379          $form['deleter'] = array (
380            '#type' => 'value',
381            '#value' => $user
382          );
383          $rtype = buddylist_api_rtype_load($relationship->rtid);
384          $output = confirm_form($form, t('Remove relation-request %relation to %username?', array('%relation' => $rtype->name, '%username' => $account->name)), $_GET['destination'], NULL, t('Delete'), t('Cancel'));
385          return $output;
386      }
387    }
388    
389    function buddylist_ui_cancel_request_form_submit($form, &$form_state) {
390      $relation = buddylist_api_load_relationships($form_state['values']['rid'], 'rid');
391      buddylist_api_delete_relationship($relation, $form_state['values']['deleter']);
392    
393      $form_state['redirect'] = $_GET['destination'];
394    }
395    
396    function buddylist_ui_deny_request_form($form_state, $account, $relationship) {
397      global $user;
398    
399      if (buddylist_api_load_relationships_between($account, $user, TRUE, FALSE ) == 0) {
400        drupal_set_message(t('You have no pending relationship with this user'));
401      } else {
402    
403          $form['rid'] = array(
404                '#type' => 'value',
405            '#value' => $relationship->rid,
406              );
407          $form['deleter'] = array (
408            '#type' => 'value',
409            '#value' => $user
410          );
411          $rtype = buddylist_api_rtype_load($relationship->rtid);
412          $output = confirm_form($form, t('Deny relation-request %relation to %username?', array('%relation' => $rtype->name, '%username' => $account->name)), $_GET['destination'], NULL, t('Deny'), t('Cancel'));
413          return $output;
414      }
415    }
416    
417    function buddylist_ui_deny_request_form_submit($form, &$form_state) {
418      $relation = buddylist_api_load_relationships($form_state['values']['rid'], 'rid');
419      buddylist_api_delete_relationship($relation, $form_state['values']['deleter']);
420    
421      $form_state['redirect'] = $_GET['destination'];
422    }
423    
424    function buddylist_ui_accept_request_form($form_state, $account, $relationship) {
425      global $user;
426    
427      if (buddylist_api_load_relationships_between($account, $user, TRUE, FALSE ) == 0) {
428        drupal_set_message(t('You have no pending relationship with this user'));
429      } else {
430    
431          $form['rid'] = array(
432                '#type' => 'value',
433            '#value' => $relationship->rid,
434              );
435          $form['deleter'] = array (
436            '#type' => 'value',
437            '#value' => $user
438          );
439    
440          $rtype = buddylist_api_rtype_load($relationship->rtid);
441    
442          drupal_set_message(print_r($rtype, TRUE));
443    
444          if($rtype->backrelation) {
445            $current_relationships = buddylist_api_load_relationships_between($user, $account);
446    
447                $results = db_query(
448                  "SELECT * FROM {buddylist_relationtypes}" . ($current_relationships ? " WHERE rtid NOT IN (%s) AND oneway = 0 ORDER BY name" : ' WHERE oneway = 0'),
449                  implode(',', array_keys($current_relationships))
450                );
451    
452                $relationships = array();
453                while($result = db_fetch_object($results)) {
454                  $relationships[$result->rtid] = $result->name;
455                }
456    
457            $form['rtid'] = array(
458                  '#title'          => t('Relationship'),
459                  '#type'           => 'select',
460                  '#options'        => $relationships,
461                  '#default_value'  => $form_state['rtid'],
462                );
463          }
464    
465          $output = confirm_form($form, t('Accept relation-request %relation from %username?', array('%relation' => $rtype->name, '%username' => $account->name)), $_GET['destination'], NULL, t('Accept'), t('Cancel'));
466          return $output;
467      }
468    }
469    
470    function buddylist_ui_accept_request_form_submit($form, &$form_state) {
471      $relation = buddylist_api_load_relationships($form_state['values']['rid'], 'rid');
472      $relation->state = 1;
473      buddylist_api_save_relationship($relation, 'accept');
474    
475      $form_state['redirect'] = $_GET['destination'];
476  }  }

Legend:
Removed from v.1.6.2.3  
changed lines
  Added in v.1.6.2.4

  ViewVC Help
Powered by ViewVC 1.1.2