summaryrefslogtreecommitdiffstats
path: root/content_types/profile_fields.inc
blob: b9ba18ddd2fcf31f99631ad957fec34b1f634761 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
// $Id$


/**
 * Callback function to supply a list of content types.
 */
function panels_profile_fields_panels_content_types() {
  $items = array();
  if (module_exists('profile') && !is_null(profile_categories())) {
    $items['profile_fields'] = array(
      'title' => t('Profile Fields'),
      'content_types' => 'panels_profile_fields_content_type',
      // only provides a single content type
      'single' => TRUE,
      'render callback' => 'panels_profile_fields_content',
      'add callback' => 'panels_profile_fields_configure',
      'edit callback' => 'panels_profile_fields_configure',
      'title callback' => 'panels_profile_fields_configure_title',
    );
  }
  return $items;
}

/**
 * 'Render' callback for the 'profile fields' content type.
 */
function panels_profile_fields_content($subtype, $conf, $panel_args, $context) {
  $account = isset($context->data) ? drupal_clone($context->data) : NULL;
  $block = new stdClass();
  $block->module = 'profile fields';

  if ($account) {
    // Get the category from the options
    $category = str_replace("_", " ", $conf['category']);

    // Set the subject to the name of the category
    $block->subject = $category;

    // Put all the fields in the category into an array
    profile_view_profile($account);

    if (is_array($account->content[$category])) {
      foreach ($account->content[$category] as $field) {
        if (is_array($field['#attributes'])) {
          $vars[$field['#attributes']['class']]['title'] = $field['#title'];
          $vars[$field['#attributes']['class']]['value'] = $field['#value'];
        }
      }
    }

    if (count($vars) == 0) {
      // Output the given empty text
      $output = $conf['empty'];
    }
    else {
      // Call the theme function with the field vars
      $output = theme('profile_fields_pane', $category, $vars);
    }

    $block->content = $output;
    $block->delta = $account->uid;
  }
  else {
    $block->subject = $conf['category'];
    $block->content = t('Profile content goes here.');
    $block->delta   = 'unknown';
  }

  return $block;
}

/**
 * Return all content types available.
 */
function panels_profile_fields_content_type() {
  return array(
    'description' => array(
      'title' => t('Profile Category'),
      'icon' => 'icon_user.png',
      'path' => panels_get_path('content_types/user'),
      'description' => t('Profile category contents.'),
      'required context' => new panels_required_context(t('User'), 'user'),
      'category' => array(t('User context'), -9),
    ),
  );
}

/**
 * Helper function : build the list of categories for the 'edit' form.
 */
function _panels_profile_fields_options() {
  $cat_list = array();

  $categories = profile_categories();
  foreach ($categories as $key => $value) {
    $cat_list[str_replace(" ", "_", $value['name'])] = $value['title'];
  }

  return $cat_list;
}

/**
 * 'Edit' callback for the 'profile fields' content type.
 */
function panels_profile_fields_configure($id, $parents, $conf = array()) {
  // Apply defaults
  if (empty($conf)) {
    $conf = array('title' => '', 'category' => '', 'empty' => '');
  }

  $form['category'] = array(
    '#type' => 'radios',
    '#title' => t('Which category'),
    '#options' => _panels_profile_fields_options(),
    '#default_value' => $conf['category'],
    '#prefix' => '<div class="clear-block no-float">',
    '#suffix' => '</div>',
  );

  $form['empty'] = array(
    '#type' => 'textarea',
    '#title' => 'Empty text',
    '#description' => t('Text to display if category has no data. Note that title will not display unless overridden.'),
    '#rows' => 5,
    '#default_value' => $conf['empty'],
    '#prefix' => '<div class="clear-block no-float">',
    '#suffix' => '</div>',
  );

  return $form;
}

/**
 * 'Title' callback for the 'profile fields' content type.
 */
function panels_profile_fields_configure_title($subtype, $conf, $context) {
  return t('"@s" profile fields', array('@s' => $conf['category']));
}