| 1 |
<?php |
<?php |
| 2 |
// $Id: cck_field_privacy.module,v 1.3.2.1 2008/06/28 06:23:21 obsidiandesign Exp $ |
// $Id: cck_field_privacy.module,v 1.5 2008/10/01 02:33:07 obsidiandesign Exp $ |
|
|
|
|
/* TODO Automatically add Drupal.settings.basePath |
|
|
In Drupal 5, you would have to add the base path to Drupal.settings yourself |
|
|
if you needed it (it's needed for just about every AHAH/AJAX enabled module |
|
|
if you did it right). Now in Drupal 6, it's added automatically. You can always |
|
|
find it at Drupal.settings.basePath (actually, as soon as drupal_add_js() is |
|
|
called at least once, so this is similar to the way we automatically add |
|
|
drupal.js and jquery.js. */ |
|
|
|
|
|
/* TODO db_next_id() is gone, and replaced as db_last_insert_id() |
|
|
Since db_next_id() introduce some problems, and the use of this function |
|
|
can be replaced by database level auto increment handling, db_next_id() |
|
|
is now gone and replaced as db_last_insert_id() with help of serial type |
|
|
under Schema API (check out http://drupal.org/node/149176 for more details). |
|
|
Please refer to drupal_write_record() as demonstration. */ |
|
|
|
|
|
/* TODO FormAPI image buttons are now supported. |
|
|
FormAPI now offers the 'image_button' element type, allowing developers to |
|
|
use icons or other custom images in place of traditional HTML submit buttons. |
|
|
|
|
|
$form['my_image_button'] = array( |
|
|
'#type' => 'image_button', |
|
|
'#title' => t('My button'), |
|
|
'#return_value' => 'my_data', |
|
|
'#src' => 'my/image/path.jpg', |
|
|
); */ |
|
|
|
|
|
/* TODO hook_nodeapi('submit') has been replaced by op='presave' |
|
|
There is no longer a 'submit' op for nodeapi. Instead you may use the newly |
|
|
created 'presave' op. Note, however, that this op is invoked at the beginning |
|
|
of node_save(), in contrast to op='submit' which was invoked at the end of |
|
|
node_submit(). Thus 'presave' operations will be performed on nodes that are |
|
|
saved programatically via node_save(), while in Drupal 5.x op='submit' was |
|
|
only applied to nodes saved via the node form. Note that the node form is now, |
|
|
in effect, a multistep form (for example when previewing), so if you need to |
|
|
fix up the data in the node for re-building the form, use a #submit function |
|
|
added to the node form's $form array. */ |
|
|
|
|
|
/* TODO Change 'Submit' to 'Save' on buttons |
|
|
It has been agreed on that the description 'Submit' for a button is not a |
|
|
good choice since it does not indicate what actually happens. While for |
|
|
example on node editing forms, 'Preview' and 'Delete' describe exactly what |
|
|
will happen when the user clicks on the button, 'Submit' only gives a vague |
|
|
idea. When labelling your buttons, make sure that it is clear what this |
|
|
button does when the user clicks on it. */ |
|
|
|
|
|
/* TODO Node previews and adding form fields to the node form. |
|
|
There is a subtle but important difference in the way node previews (and other |
|
|
such operations) are carried out when adding or editing a node. With the new |
|
|
Forms API, the node form is handled as a multi-step form. When the node form |
|
|
is previewed, all the form values are submitted, and the form is rebuilt with |
|
|
those form values put into $form['#node']. Thus, form elements that are added |
|
|
to the node form will lose any user input unless they set their '#default_value' |
|
|
elements using this embedded node object. */ |
|
|
|
|
|
/* TODO New user_mail_tokens() method may be useful. |
|
|
user.module now provides a user_mail_tokens() function to return an array |
|
|
of the tokens available for the email notification messages it sends when |
|
|
accounts are created, activated, blocked, etc. Contributed modules that |
|
|
wish to make use of the same tokens for their own needs are encouraged |
|
|
to use this function. */ |
|
|
|
|
|
/* TODO |
|
|
There is a new hook_watchdog in core. This means that contributed modules |
|
|
can implement hook_watchdog to log Drupal events to custom destinations. |
|
|
Two core modules are included, dblog.module (formerly known as watchdog.module), |
|
|
and syslog.module. Other modules in contrib include an emaillog.module, |
|
|
included in the logging_alerts module. See syslog or emaillog for an |
|
|
example on how to implement hook_watchdog. |
|
|
function example_watchdog($log = array()) { |
|
|
if ($log['severity'] == WATCHDOG_ALERT) { |
|
|
mysms_send($log['user']->uid, |
|
|
$log['type'], |
|
|
$log['message'], |
|
|
$log['variables'], |
|
|
$log['severity'], |
|
|
$log['referer'], |
|
|
$log['ip'], |
|
|
format_date($log['timestamp'])); |
|
|
} |
|
|
} */ |
|
|
|
|
|
/* TODO Implement the hook_theme registry. Combine all theme registry entries |
|
|
into one hook_theme function in each corresponding module file. |
|
|
function cck_field_privacy_theme() { |
|
|
return array( |
|
|
); |
|
|
}; */ |
|
|
|
|
|
|
|
|
/* TODO |
|
|
An argument for replacements has been added to format_plural(), |
|
|
escaping and/or theming the values just as done with t().*/ |
|
|
|
|
|
/* TODO You may want to take advantage of new form-specific alter hooks. |
|
|
The hook_form_alter() is complemented by hook_form_$form-id_alter(). |
|
|
Optionally, modules can implement form-specific alteration functions rather |
|
|
than a single hook_form_alter() with many conditional switch statements. |
|
|
This is optional, and is most useful for tidying the code of modules that |
|
|
alter many forms to customize a site's operations. |
|
|
The new function names are built using the following format: |
|
|
[your-module-name]_form_[$form_id]_alter. */ |
|
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Display help and module information. |
* Display help and module information. |
| 9 |
* @return String |
* @return String |
| 10 |
* Help text for section. |
* Help text for section. |
| 11 |
*/ |
*/ |
| 12 |
function cck_field_privacy_help($path, $arg) { |
function cck_field_privacy_help($section = '') { |
| 13 |
$output = ''; |
$output = ''; |
| 14 |
|
|
| 15 |
switch ($path) { |
switch ($section) { |
| 16 |
case 'admin/help#cck_field_privacy': |
case 'admin/help#cck_field_privacy': |
| 17 |
$output = '<p>'. t('Allows users to set privacy settings for specific CCK fields.') .'</p>'; |
$output = '<p>'. t('Allows users to set privacy settings for specific CCK fields.') .'</p>'; |
| 18 |
break; |
break; |
| 38 |
/** |
/** |
| 39 |
* Implementation of hook_menu(). |
* Implementation of hook_menu(). |
| 40 |
*/ |
*/ |
| 41 |
function cck_field_privacy_menu() { |
function cck_field_privacy_menu($may_cache) { |
| 42 |
$items = array(); |
$items = array(); |
| 43 |
|
|
| 44 |
$items['admin/content/cck_field_privacy'] = array( |
if ($may_cache) { |
| 45 |
'title' => 'Content field privacy', |
$items[] = array( |
| 46 |
'description' => 'Configure field privacy controls.', |
'path' => 'admin/content/cck_field_privacy', |
| 47 |
'page callback' => 'cck_field_privacy_admin_settings', |
'title' => t('Content field privacy'), |
| 48 |
'access callback' => user_access('administer cck field privacy'), |
'description' => t('Configure field privacy controls.'), |
| 49 |
|
'callback' => 'cck_field_privacy_admin_settings', |
| 50 |
|
'access' => user_access('administer cck field privacy'), |
| 51 |
'type' => MENU_NORMAL_ITEM, |
'type' => MENU_NORMAL_ITEM, |
| 52 |
); |
); |
| 53 |
$items['cck_field_privacy/ajax'] = array( |
$items[] = array( |
| 54 |
'page callback' => 'cck_field_privacy_ajax', |
'path' => 'cck_field_privacy/ajax', |
| 55 |
'access callback' => TRUE, |
'callback' => 'cck_field_privacy_ajax', |
| 56 |
|
'access' => TRUE, |
| 57 |
'type' => MENU_CALLBACK, |
'type' => MENU_CALLBACK, |
| 58 |
); |
); |
| 59 |
|
} |
| 60 |
|
|
| 61 |
return $items; |
return $items; |
| 62 |
} |
} |
| 78 |
if ($result) { |
if ($result) { |
| 79 |
while ($permissions = db_fetch_object($result)) { |
while ($permissions = db_fetch_object($result)) { |
| 80 |
if ($permissions->permission == 'b') { |
if ($permissions->permission == 'b') { |
| 81 |
if (array_key_exists($user->uid, $buddies)) { |
if (array_key_exists($user->uid, $buddies)) { continue; } |
|
continue; |
|
|
} |
|
| 82 |
else { |
else { |
| 83 |
$deny = TRUE; |
$deny = TRUE; |
| 84 |
} |
} |
| 101 |
} |
} |
| 102 |
|
|
| 103 |
/** |
/** |
| 104 |
|
* Implementation of hook_views_post_view(). |
| 105 |
|
* Removes fields from a view if a user does not have perms to view it. |
| 106 |
|
*/ |
| 107 |
|
|
| 108 |
|
function cck_field_privacy_views_post_view(&$view) { |
| 109 |
|
global $user; |
| 110 |
|
$deny = FALSE; |
| 111 |
|
|
| 112 |
|
foreach($view->field as $view_array=>$view_params) { |
| 113 |
|
print_r($view_params); |
| 114 |
|
echo "</br>"; |
| 115 |
|
} |
| 116 |
|
die(); |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
/** |
| 120 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 121 |
*/ |
*/ |
| 122 |
function cck_field_privacy_form_alter(&$form, &$form_state, $form_id) { |
function cck_field_privacy_form_alter($form_id, &$form) { |
| 123 |
global $user; |
global $user; |
| 124 |
|
|
| 125 |
$uid = (int) isset($form['#node']->uid)? $form['#node']->uid : $user->uid; // original author or current user (authoring new node) |
$uid = (int) isset($form['#node']->uid)? $form['#node']->uid : $user->uid; // original author or current user (authoring new node) |
| 126 |
$types = variable_get('cckfp_types', NULL); |
$types = variable_get('cckfp_types', null); |
| 127 |
if (is_array($types)) { |
|
| 128 |
if (!array_key_exists($form['#node']->type, $types) && !in_array($form['page']['#value']->options['content-type'], $types)) { |
if(is_array($types)) { |
| 129 |
|
if (!array_key_exists($form['#node']->type, $types) || $types[$form['#id']] == '0') { |
| 130 |
return; // abort |
return; // abort |
| 131 |
} |
} |
| 132 |
|
|
| 133 |
// Get the stored fields |
// Get the stored fields |
| 134 |
$fields = variable_get('cckfp_types', NULL); |
$fields = variable_get('cckfp_types', null); |
| 135 |
$values = unserialize(variable_get('cckfp_values', '')); |
$values = unserialize(variable_get('cckfp_values', '')); |
| 136 |
|
|
| 137 |
$cckfp_enabled_fields = array(); |
$cckfp_enabled_fields = array(); |
| 138 |
|
|
| 139 |
// Figure out what node type (via name) we are accessing. |
// Figure out what node type (via name) we are accessing. |
|
|
|
| 140 |
$node_name_array = explode('_', $form_id); |
$node_name_array = explode('_', $form_id); |
| 141 |
if (array_key_exists($node_name_array[0], $fields)) { |
if (array_key_exists($node_name_array[0], $fields)) { |
| 142 |
if ($fields[$node_name_array[0]] != '0') { |
if ($fields[$node_name_array[0]] != '0') { |
| 147 |
if (array_key_exists($field, $form)) { |
if (array_key_exists($field, $form)) { |
| 148 |
if (isset($form[$field][0]['value'])) { |
if (isset($form[$field][0]['value'])) { |
| 149 |
$form_field =& $form[$field][0]['value']; |
$form_field =& $form[$field][0]['value']; |
| 150 |
} |
} elseif (isset($form[$field]['keys'])) { |
|
elseif (isset($form[$field]['keys'])) { |
|
| 151 |
$form_field =& $form[$field]['keys']; |
$form_field =& $form[$field]['keys']; |
| 152 |
} |
} elseif (isset($form[$field]['key'])) { |
|
elseif (isset($form[$field]['key'])) { |
|
| 153 |
$form_field =& $form[$field]['key']; |
$form_field =& $form[$field]['key']; |
| 154 |
} |
} else { |
|
else { |
|
| 155 |
$form_field =& $form[$field]; |
$form_field =& $form[$field]; |
| 156 |
} |
} |
| 157 |
|
|
| 165 |
// Add the padlock icon & link to the JQuery function |
// Add the padlock icon & link to the JQuery function |
| 166 |
// @TODO: Add this via jQuery to degrade gracefully in browsers without JavaScript enabled. |
// @TODO: Add this via jQuery to degrade gracefully in browsers without JavaScript enabled. |
| 167 |
// @TODO: Get the padlock to also appear on field group titles (fieldset legends), if selected. |
// @TODO: Get the padlock to also appear on field group titles (fieldset legends), if selected. |
| 168 |
$form_field['#title'] .= "<a href='#' id='". $field ."link' ><img src='". base_path() . drupal_get_path('module', 'cck_field_privacy') ."/padlock.png' /></a>"; |
$form_field['#title'] .= "<a href='#' id='".$field."link' ><img src='". base_path() . drupal_get_path('module', 'cck_field_privacy'). "/padlock.png' /></a>"; |
| 169 |
} |
} |
| 170 |
} |
} |
| 171 |
} |
} |
| 206 |
$field = $_POST['field']; |
$field = $_POST['field']; |
| 207 |
$type = $_POST['type']; |
$type = $_POST['type']; |
| 208 |
$setting = $_POST['setting']; |
$setting = $_POST['setting']; |
| 209 |
if ($userinfo != 'NULL' && $field != 'NULL' && $type != 'NULL' && $setting != 'NULL') { |
if ($userinfo != 'null' && $field != 'null' && $type != 'null' && $setting != 'null') { |
| 210 |
$sql = "INSERT INTO {cckfp} (uid, field_name, type_name, permission) VALUES(%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE uid=%d, field_name='%s', type_name='%s', permission='%s'"; |
$sql = "INSERT INTO {cckfp} (uid, field_name, type_name, permission) VALUES(%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE uid=%d, field_name='%s', type_name='%s', permission='%s'"; |
| 211 |
$result = db_query($sql, $userinfo['uid'], $field, $type, $setting, $userinfo['uid'], $field, $type, $setting); |
$result = db_query($sql, $userinfo['uid'], $field, $type, $setting, $userinfo['uid'], $field, $type, $setting); |
| 212 |
if (!db_error()) { |
if (!db_error()) { |
| 213 |
} |
} else { |
|
else { |
|
| 214 |
print db_error(); |
print db_error(); |
| 215 |
} |
} |
| 216 |
} |
} |
| 248 |
foreach ($types as $key => $value) { |
foreach ($types as $key => $value) { |
| 249 |
$options[$key] = $value['name']; |
$options[$key] = $value['name']; |
| 250 |
} |
} |
| 251 |
$enabled_types = variable_get('cckfp_types', NULL); |
$enabled_types = variable_get('cckfp_types', null); |
| 252 |
$form['field_perms']['cckfp_types'] = array( |
$form['field_perms']['cckfp_types'] = array( |
| 253 |
'#type' => 'checkboxes', |
'#type' => 'checkboxes', |
| 254 |
'#title' => t('Content types'), |
'#title' => t('Content types'), |
| 264 |
if ($enabled_type) { |
if ($enabled_type) { |
| 265 |
$form['field_fields'][$enabled_type] = array( |
$form['field_fields'][$enabled_type] = array( |
| 266 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 267 |
'#title' => $types[$enabled_type]['name'] .' content fields', |
'#title' => $types[$enabled_type]['name'] . ' content fields', |
| 268 |
'#description' => t('Checking this box will hide this complete field group'), |
'#description' => t('Checking this box will hide this complete field group'), |
| 269 |
); |
); |
| 270 |
|
|
| 312 |
if (module_exists('fieldgroup')) { |
if (module_exists('fieldgroup')) { |
| 313 |
$groups = fieldgroup_groups($type); |
$groups = fieldgroup_groups($type); |
| 314 |
foreach ($groups as $group_name => $data) { |
foreach ($groups as $group_name => $data) { |
| 315 |
$form['field_fields'][$type][$group_name .'_fields'] = array( |
$form['field_fields'][$type][$group_name . '_fields'] = array( |
| 316 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 317 |
'#title' => $data['label'] .' field group', |
'#title' => $data['label'] . ' field group', |
| 318 |
); |
); |
| 319 |
$form['field_fields'][$type][$group_name .'_fields'][$type .'_'. $group_name] = array( |
$form['field_fields'][$type][$group_name . '_fields'][$type .'_'. $group_name] = array( |
| 320 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 321 |
'#title' => $data['label'], |
'#title' => $data['label'], |
| 322 |
'#default_value' => $values[$type][$group_name], |
'#default_value' => $values[$type][$group_name], |
| 340 |
* Implementation of hook_submit(). |
* Implementation of hook_submit(). |
| 341 |
* Save the incoming values. |
* Save the incoming values. |
| 342 |
*/ |
*/ |
| 343 |
function cck_field_privacy_admin_settings_form_submit($form, &$form_state) { |
function cck_field_privacy_admin_settings_form_submit($form_id, $form_values) { |
| 344 |
// @TODO: Figure out why the hell it is neccessary to do this. |
// @TODO: Figure out why the hell it is neccessary to do this. |
| 345 |
if ($form_state['values']['cckfp_types'][0]) { |
if ($form_values['cckfp_types'][0]) { unset($form_values['cckfp_types'][0]); } |
| 346 |
unset($form_state['values']['cckfp_types'][0]); |
if ($form_values['cckfp_types'][1]) { unset($form_values['cckfp_types'][1]); } |
|
} |
|
|
if ($form_state['values']['cckfp_types'][1]) { |
|
|
unset($form_state['values']['cckfp_types'][1]); |
|
|
} |
|
| 347 |
|
|
| 348 |
variable_set('cckfp_types', $form_state['values']['cckfp_types']); |
variable_set('cckfp_types', $form_values['cckfp_types']); |
| 349 |
|
|
| 350 |
foreach ($form_state['values'] as $key => $value) { |
foreach ($form_values as $key => $value) { |
| 351 |
if (strstr($key, '_field_')) { |
if (strstr($key, '_field_')) { |
| 352 |
$type = substr($key, 0, strpos($key, '_field')); |
$type = substr($key, 0, strpos($key, '_field')); |
| 353 |
$field = substr($key, strpos($key, 'field')); |
$field = substr($key, strpos($key, 'field')); |