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

Contents of /contributions/modules/flag_weights/flag_weights.module

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


Revision 1.1 - (show annotations) (download) (as text)
Thu Oct 9 10:19:50 2008 UTC (13 months, 2 weeks ago) by lyricnz
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Branch point for: DRUPAL-6--1
File MIME type: text/x-php
Initial commit of a module that adds a Weight to Flags module items
1 <?php
2 // $Id: flag_weights.module,v 1.43.2.7 2008/09/09 14:39:01 sun Exp $
3
4 /**
5 * @file
6 * Add flag-weights to the Flag module.
7 */
8
9 /**
10 * Implementation of hook_schema_alter(). We alter $schema by reference.
11 *
12 * @param $schema
13 * The system-wide schema collected by drupal_get_schema().
14 */
15 function flag_weights_schema_alter(&$schema) {
16 // Add field to existing schema.
17 $schema['flag_content']['fields']['weight'] = array(
18 'type' => 'int',
19 'not null' => TRUE,
20 'default' => 0,
21 'size' => 'tiny',
22 // 'description' => t('Flag weight within region.'),
23 );
24 }
25
26 /**
27 * Implementation of hook_views_data().
28 *
29 * Provide a field that can be used to sort a view.
30 */
31 function flag_weights_views_data() {
32 $data = array();
33
34 $data['flag_content']['weight'] = array(
35 'title' => t('Weight'),
36 'help' => t('Used for sorting the list of flagged items.'),
37 'real field' => 'weight',
38 'field' => array(
39 'handler' => 'views_handler_field_numeric',
40 'click sortable' => TRUE,
41 ),
42 'sort' => array(
43 'handler' => 'views_handler_sort',
44 ),
45 );
46
47 return $data;
48 }
49
50 /**
51 * Flags an item, and set it's weight.
52 * @param $content_type
53 * The name of the content-type to flag (eg: 'node' or 'comment')
54 * @param $content_id
55 * The ID of the item to flag or unflag.
56 * @param $account
57 * The user on whose behalf to flag. Leave empty for the current user.
58 * @param $weight
59 * A weight used for ordering the item within the flagged-items list.
60 * @return
61 * FALSE if some error occured (e.g., user has no permission, flag isn't
62 * applicable to the item, etc.), TRUE otherwise.
63 */
64 function flag_weights_flag($content_type, $content_id, $account = NULL, $weight = 0) {
65 $handler = flag_create_handler($content_type);
66 $ok = $handler->flag('flag', $content_id, $account);
67 if ($ok && $weight != 0) {
68 // We don't need to check access control etc because $handler->flag just checked it
69 if (!isset($account)) {
70 $account = $GLOBALS['user'];
71 }
72 flag_weights_set_weight($handler->fid, $handlers->content_type, $content_id, $account->uid, $weight);
73 }
74 return $ok;
75 }
76
77 /**
78 * Update the weight of an existing flagged item.
79 */
80 function flag_weights_set_weight($fid, $content_type, $content_id, $uid, $weight) {
81 db_query("UPDATE {flag_content} SET weight = %d WHERE fid = %d AND content_type = '%s' AND content_id = %d AND uid = %d",
82 $weight, $fid, $content_type, $content_id, $uid);
83 }

  ViewVC Help
Powered by ViewVC 1.1.2