/[drupal]/contributions/modules/civinode/civinode_views.inc
ViewVC logotype

Contents of /contributions/modules/civinode/civinode_views.inc

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


Revision 1.3 - (show annotations) (download) (as text)
Thu Jun 21 04:40:59 2007 UTC (2 years, 5 months ago) by torenware
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +18 -3 lines
File MIME type: text/x-php
Checking Comm Init changes into head
1 <?php
2
3 /**
4 * CiviNode Views related utilities and hacks
5 */
6 function civinode_views_field_item_table($field) {
7 //Grab the default from content.module
8 $tables = content_views_field_tables($field);
9 //We need to modify in two specific places:
10 //we need to change the options, and we need to
11 //change the handlers. We grab refs to both,
12 //to make the code easier to read.
13 $tab_names = array_keys($tables);
14 $field_names = array_keys($tables[$tab_names[0]]['fields']);
15 $field_params =& $tables[$tab_names[0]]['fields'][$field_names[0]];
16 $type = $field['type'];
17 //TODO make this general and not contact specific
18 //$field_params['option'] = civinode_views_contact_field_options();
19
20 if ($type == 'civicrm_contact') {
21 $field_params['option'] = 'string';
22 $field_params['help'] =
23 t("Use the 'options' field to customize the way a contact displays. For the handler 'Profiled Contact', you can specify the ID of the CiviCRM Profile you want to use for display. For 'Contact Field', you can specify the CiviCRM field name you want to use (e.g., 'first_name', 'phone', 'city')");
24 $field_params['handler'] =
25 array(
26 'civinode_view_profiled_contact_view_handler' => t('Profiled Contact'),
27 'civinode_view_contact_field_view_handler' => t('Contact Field'),
28 'civinode_view_contact_link_view_handler' => t('Contact Field Link'),
29 'civinode_view_named_object_view_handler' => t('Object Name'),
30 );
31 }
32 else if ($type == 'civicrm_group') {
33 $field_params['handler'] =
34 array(
35 'civinode_views_group_view_handler' => t('Group Title'),
36 'civinode_view_named_object_view_handler' => t('Object Name'),
37 );
38
39 }
40 else {
41 $field_params['handler'] =
42 array(
43 'civinode_view_named_object_view_handler' => t('Object Name'),
44 );
45
46 }
47 return $tables;
48 }
49
50
51
52 function civinode_views_field_item_table_old($field) {
53 $fieldname = $field['field_name'];
54 $fieldtype = $field['type'];
55 $db_info = content_database_info($field);
56 $table_name = $db_info['table'];
57 $tables[$fieldname] =
58 array(
59 'name' => $table_name,
60 'provider' => 'civinode_cck',
61 'join' =>
62 array(
63 'left' =>
64 array(
65 'table' => 'node',
66 'field' => 'nid'),
67 'right' =>
68 array('field' => 'nid')
69 ),
70 'fields' =>
71 array(
72 $fieldname =>
73 _civinode_views_field_def($field),
74 ),
75
76 );
77 return $tables;
78 }
79
80 function civinode_view_contact_field_view_handler($fieldinfo, $fielddata, $value, $data) {
81 if (!is_numeric($value) or $value == 0)
82 return FALSE;
83 $contact = array();
84 _civinode_cck_cache_mgr('fetch', '_crm_views', $value,$contact);
85 if (!$contact)
86 return FALSE;
87 $field_name = $fielddata['options'] ?
88 $fielddata['options']:
89 'display_name';
90 $modified = $contact[$field_name];
91 if ($modified)
92 return $modified;
93 else
94 return $value;
95 }
96
97 function civinode_view_contact_link_view_handler($fieldinfo, $fielddata, $value, $data) {
98 //We wrap the formatted value into a link
99 $text = civinode_view_contact_field_view_handler($fieldinfo,
100 $fielddata, $value,
101 $data);
102 $link = l($text, "civicrm/contact/view", array(),
103 "reset=1&cid=$value", NULL, FALSE, TRUE);
104 return $link;
105 }
106
107 function civinode_view_profiled_contact_view_handler($fieldinfo, $fielddata, $value, $data) {
108 if (!is_numeric($value) or $value == 0)
109 return FALSE;
110 $contact = array();
111 _civinode_cck_cache_mgr('fetch', '_crm_views', $value,$contact);
112 if (!$contact)
113 return FALSE;
114 $profile = $fielddata['options'];
115 if (!$profile) {
116 $profile = civinode_get_default_profile_id();
117 }
118 $profile = $profile and is_numeric($profile) ?
119 $profile : 1;
120 //$modified = "profiled contact for $value";
121 $modified = theme('crm_profile_cid', $profile, $value);
122 return $modified;
123 }
124
125 function civinode_view_named_object_view_handler($fieldinfo, $fielddata, $value, $data) {
126 if (!is_numeric($value) or $value == 0)
127 return FALSE;
128 $modified = "object title for $value";
129 return $modified;
130 }
131
132
133 function civinode_views_contact_view_handler($fieldinfo, $fielddata, $value, $data){
134 if (!is_numeric($value) or $value == 0)
135 return FALSE;
136 $contact = array();
137 _civinode_cck_cache_mgr('fetch', '_crm_views', $value,$contact);
138 if (!$contact)
139 return FALSE;
140 $modified = "crm field data for $value";
141 return $modified;
142 }
143
144
145 function civinode_views_group_view_handler($fieldinfo, $fielddata, $value, $data) {
146 if ($value) {
147 $group = civinode_get_group_by_id($value);
148 if (civinode_check_error($group))
149 return $value;
150 return $group->title;
151 }
152 return $value;
153 }
154
155
156 function _civinode_views_field_def($field) {
157 $fieldtype = $field['type'];
158 $fieldname = $field['field_name'] . "_object_id";
159 $label = $field['widget']['label'];
160 $t_args = array('%name' => $label);
161 $options = NULL;
162 $help = t('CiviCRM linked field');
163 if ($fieldtype == 'civicrm_contact') {
164 $name = t('CiviCRM Contact: %name', $t_args);
165 $options = civinode_views_contact_field_options();
166 $handler = 'civinode_views_contact_view_handler';
167 $help = t('CiviCRM Contact');
168 }
169 else if ($fieldtype == 'civicrm_group') {
170 $name = t('CiviCRM Group: %name', $t_args);
171 $handler = 'civinode_views_group_view_handler';
172 $help = t('CiviCRM Group');
173 }
174 else
175 $name = t('CiviCRM: %name', $t_args);
176 $def = array('name' => $name,
177 'help' => $help,
178 'field' => $fieldname,
179 );
180 if ($options)
181 $def['option'] = $options;
182 if ($handler)
183 $def['handler'] = $handler;
184
185 return $def;
186 }
187
188
189 /**
190 * Options fields
191 *
192 */
193
194 function civinode_views_contact_field_options() {
195
196 $field_options =
197 array(
198 'display_name' => t('Display Name'),
199 'sort_name' => t('Sort Name'),
200 'last_name' => t('Last Name'),
201 'first_name' => t('First Name'),
202 'street_address' => t('Street Address'),
203 'city' => t('City'),
204 'postal_code' => t('Zip/Postal Code'),
205 'other' => t('Other (Enter Below)')
206 );
207 $form =
208 array(
209 'main_option' =>
210 array(
211 '#type' => 'select',
212 '#options' => $field_options,
213 ),
214 'other_option' =>
215 array(
216 '#type' => 'textfield',
217 '#size' => 20,
218 ),
219 '#type' => 'civinode_views_option',
220 '#process' => array('civinode_views_option_process' => NULL),
221 '#after_build' => array('civinode_views_option_builder')
222
223 );
224
225 return $form;
226 }
227
228
229 /**
230 * The #process takes the serialized #default_value and feeds
231 * the forms beneath it.
232 */
233 function civinode_views_option_process($element) {
234 $values = unserialize($element['#default_value']);
235 if (!$element) {
236 $element = civinode_views_contact_field_options();
237 }
238 if (!is_array($values)) {
239 // set default values for options that have no stored value.
240 $values = array('main_option' => 'display_name', 'other_option' => '');
241 }
242 $element['main_option']['#default_value'] = $values['main_option'];
243 $element['other_option']['#default_value'] = $values['other_option'];
244 return $element;
245 }
246
247 /**
248 * Put the value back.
249 */
250 function civinode_views_option_builder($element) {
251 global $form_values;
252
253 $op = isset($form_values['op']) ? $form_values['op'] : '';
254
255 $values = array();
256 $values['main_option'] = $element['main_option']['#value'];
257 if ($values['main_option'] != 'other')
258 $other = '';
259 else
260 $other = $element['other_option']['#value'];
261 $values['other_option'] = $other;
262 $element['#value'] = serialize($values);
263 form_set_value($element, $element['#value']);
264 return $element;
265 }
266
267 /**
268 * this forces the #value to not be printed as it would be if we put in
269 * no #type.
270 */
271 function theme_civinode_views_option($element) {
272 return $element['#children'];
273 }

  ViewVC Help
Powered by ViewVC 1.1.2