| 1 |
<?php |
<?php |
| 2 |
// $Id: fivestar_field.inc,v 1.3.2.7 2008/05/29 00:06:43 quicksketch Exp $ |
// $Id: fivestar_field.inc,v 1.3.2.8 2008/06/19 02:42:55 quicksketch Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 31 |
); |
); |
| 32 |
$form['stars'] = array( |
$form['stars'] = array( |
| 33 |
'#type' => 'select', |
'#type' => 'select', |
| 34 |
'#title' => t('Number of Options'), |
'#title' => $field['widget']['type'] == 'stars' ? t('Number of Stars') : t('Number of Options'), |
| 35 |
'#options' => drupal_map_assoc(range(1, 10)), |
'#options' => drupal_map_assoc(range(1, 10)), |
| 36 |
'#default_value' => $field['stars'] ? $field['stars'] : 5, |
'#default_value' => $field['stars'] ? $field['stars'] : 5, |
|
'#description' => t('The number of stars or radio buttons to display.'), |
|
| 37 |
); |
); |
| 38 |
|
|
| 39 |
|
$dynamic_options = array(); |
| 40 |
if (module_exists('nodecomment')) { |
if (module_exists('nodecomment')) { |
| 41 |
$form['dynamic_target'] = array( |
$dynamic_options['comment_target_nid'] = t('Node Comment Parent'); |
| 42 |
'#title' => t('Use Node Comment Parent as Target'), |
} |
| 43 |
'#type' => 'checkbox', |
if (module_exists('nodereference')) { |
| 44 |
'#default_value' => $field['dynamic_target'], |
$fields = content_fields(NULL, $field['type_name']); |
| 45 |
'#return_value' => 'comment_target_nid', |
foreach ($fields as $content_field) { |
| 46 |
'#description' => t('Use this option to easily make a Fivestar field affect the comment parent for nodes of this content type.') |
if ($content_field['type'] == 'nodereference' && $content_field['type_name'] == $field['type_name']) { |
| 47 |
); |
$dynamic_options[$content_field['field_name']] = t('Node reference: @field', array('@field' => $content_field['field_name'])); |
| 48 |
|
} |
| 49 |
|
} |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
if (empty($dynamic_options)) { |
| 53 |
|
drupal_set_message(t('No potential target fields are available for the %type type. Create a node reference field in this node type to make it easier to assign a vote to a node.', array('%type' => node_get_types('name', $field['type_name']))), 'warning'); |
| 54 |
} |
} |
| 55 |
|
|
| 56 |
$form['target'] = array( |
$dynamic_options = array('' => t('<none>')) + $dynamic_options; |
| 57 |
'#type' => 'textarea', |
$form['dynamic_target'] = array( |
| 58 |
'#title' => t('Target Node ID'), |
'#title' => t('Voting target'), |
| 59 |
'#default_value' => $field['target'] ? $field['target'] : '', |
'#type' => 'select', |
| 60 |
'#description' => t( |
'#default_value' => $field['dynamic_target'], |
| 61 |
'A single node ID on which this field will register the vote. If no NID is specified, the field\'s value will be saved, but no vote will be registered with the Voting API.' |
'#options' => $dynamic_options, |
| 62 |
), |
'#description' => t('The voting target will make the value of this field cast a vote on another node. Use node reference fields (part of CCK core) or <a href="http://drupal.org/project/nodecomment">Node Comments</a> module to create advanced reviews. More information available on the <a href="http://drupal.org/handbook/modules/fivestar">Fivestar handbook page</a>.') |
| 63 |
); |
); |
| 64 |
|
|
| 65 |
if (user_access('use PHP for fivestar target')) { |
if (user_access('use PHP for fivestar target')) { |
| 66 |
$form['target']['#description'] .= ' '. t( |
$form['php_target'] = array( |
| 67 |
' Return the target node ID or use plain text. Enter PHP code between <em><?php ?></em>. Note that executing incorrect PHP-code can break your Drupal site.' |
'#type' => 'fieldset', |
| 68 |
|
'#title' => t('Voting target PHP code'), |
| 69 |
|
'#collapsible' => TRUE, |
| 70 |
|
'#collapsed' => empty($field['php_target']), |
| 71 |
|
); |
| 72 |
|
|
| 73 |
|
$form['php_target']['php_target'] = array( |
| 74 |
|
'#title' => t('Code'), |
| 75 |
|
'#type' => 'textarea', |
| 76 |
|
'#default_value' => $field['php_target'] ? $field['php_target'] : '', |
| 77 |
|
'#description' => t('Advanced usage only: PHP code that returns a target node ID. Should not include <?php ?> delimiters. If this field is filled out, the value returned by this code will override any value specified above. Note that executing incorrect PHP-code can break your Drupal site.'), |
| 78 |
); |
); |
| 79 |
|
} |
| 80 |
$form['php'] = array( |
else { |
| 81 |
|
$form['php_target'] = array( |
| 82 |
'#type' => 'value', |
'#type' => 'value', |
| 83 |
'#value' => 1, |
'#value' => $field['php_target'] ? $field['php_target'] : '', |
| 84 |
); |
); |
| 85 |
} |
} |
| 86 |
|
|
| 93 |
|
|
| 94 |
return $form; |
return $form; |
| 95 |
case 'save': |
case 'save': |
| 96 |
return array('stars', 'dynamic_target', 'target', 'php', 'axis'); |
return array('stars', 'dynamic_target', 'php_target', 'axis'); |
| 97 |
case 'database columns': |
case 'database columns': |
| 98 |
return array( |
return array( |
| 99 |
'rating' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'sortable' => TRUE), |
'rating' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE, 'sortable' => TRUE), |
| 112 |
case 'insert': |
case 'insert': |
| 113 |
case 'update': |
case 'update': |
| 114 |
foreach ($items as $delta => $item) { |
foreach ($items as $delta => $item) { |
| 115 |
if ($field['dynamic_target'] && !empty($node->$field['dynamic_target'])) { |
if (!empty($field['php_target'])) { |
|
$items[$delta]['target'] = $node->$field['dynamic_target']; |
|
|
} |
|
|
elseif (is_numeric($item['target'])) { |
|
|
$items[$delta]['target'] = $item['target']; |
|
|
} |
|
|
elseif ($field['php'] && strpos($item['target'], '<?php') === 0) { |
|
| 116 |
// Use eval rather than drupal_eval to allow access to local variables. |
// Use eval rather than drupal_eval to allow access to local variables. |
| 117 |
$items[$delta]['target'] = eval('?>'. $item['target']); |
$items[$delta]['target'] = eval($field['php_target']); |
| 118 |
} |
} |
| 119 |
|
elseif ($field['dynamic_target'] && !empty($node->$field['dynamic_target'])) { |
| 120 |
|
if (is_array($node->$field['dynamic_target']) && is_numeric($node->{$field['dynamic_target']}[0]['nid'])) { |
| 121 |
|
$items[$delta]['target'] = $node->{$field['dynamic_target']}[0]['nid']; |
| 122 |
|
} |
| 123 |
|
elseif (is_numeric($node->$field['dynamic_target'])) { |
| 124 |
|
$items[$delta]['target'] = $node->$field['dynamic_target']; |
| 125 |
|
} |
| 126 |
|
} |
| 127 |
|
|
| 128 |
if (is_numeric($items[$delta]['target'])) { |
if (is_numeric($items[$delta]['target'])) { |
| 129 |
_fivestar_cast_vote('node', $items[$delta]['target'], $item['rating'], $item['axis'], $node->uid); |
_fivestar_cast_vote('node', $items[$delta]['target'], $items[$delta]['rating'], $items[$delta]['axis'], $node->uid); |
| 130 |
votingapi_recalculate_results('node', $items[$delta]['target']); |
votingapi_recalculate_results('node', $items[$delta]['target']); |
| 131 |
} |
} |
| 132 |
} |
} |
|
$items = array($item); |
|
| 133 |
break; |
break; |
| 134 |
case 'delete': |
case 'delete': |
| 135 |
foreach ($items as $delta => $item) { |
foreach ($items as $delta => $item) { |
| 136 |
if ($field['dynamic_target'] && !empty($node->$field['dynamic_target'])) { |
if ($field['dynamic_target'] && !empty($node->$field['dynamic_target'])) { |
| 137 |
$items[$delta]['target'] = $node->$field['dynamic_target']; |
$items[$delta]['target'] = $node->$field['dynamic_target']; |
| 138 |
} |
} |
| 139 |
elseif (is_numeric($item['target'])) { |
elseif (!empty($item['php_target'])) { |
|
$items[$delta]['target'] = $item['target']; |
|
|
} |
|
|
elseif ($field['php'] && strpos($item['target'], '<?php') === 0) { |
|
| 140 |
// Use eval rather than drupal_eval to allow access to local variables. |
// Use eval rather than drupal_eval to allow access to local variables. |
| 141 |
$items[$delta]['target'] = eval('?>'. $item['target']); |
$items[$delta]['target'] = eval($item['target']); |
| 142 |
} |
} |
| 143 |
if (is_numeric($item['target'])) { |
if (is_numeric($items[$delta]['target'])) { |
| 144 |
_fivestar_cast_vote('node', $item['target'], 0, $item['axis'], $node->uid); |
_fivestar_cast_vote('node', $items[$delta]['target'], 0, $items[$delta]['axis'], $node->uid); |
| 145 |
votingapi_recalculate_results('node', $item['target']); |
votingapi_recalculate_results('node', $items[$delta]['target']); |
| 146 |
} |
} |
| 147 |
} |
} |
| 148 |
break; |
break; |
| 157 |
*/ |
*/ |
| 158 |
function fivestar_widget_info() { |
function fivestar_widget_info() { |
| 159 |
return array( |
return array( |
| 160 |
'radios' => array( |
'stars' => array( |
| 161 |
'label' => t('Radio Buttons'), |
'label' => t('Stars'), |
| 162 |
'field types' => array('fivestar'), |
'field types' => array('fivestar'), |
| 163 |
'multiple values' => CONTENT_HANDLE_MODULE, |
'multiple values' => CONTENT_HANDLE_MODULE, |
| 164 |
), |
), |
| 165 |
'stars' => array( |
'radios' => array( |
| 166 |
'label' => t('Stars'), |
'label' => t('Select list'), |
| 167 |
'field types' => array('fivestar'), |
'field types' => array('fivestar'), |
| 168 |
'multiple values' => CONTENT_HANDLE_MODULE, |
'multiple values' => CONTENT_HANDLE_MODULE, |
| 169 |
), |
), |
| 178 |
case 'form': |
case 'form': |
| 179 |
$form = array(); |
$form = array(); |
| 180 |
$form['allow_clear'] = array( |
$form['allow_clear'] = array( |
| 181 |
'#type' => 'select', |
'#type' => 'checkbox', |
| 182 |
'#title' => t('Allow Clearing'), |
'#title' => t('Allow user to clear value'), |
|
'#options' => array(1 => t('True'), 0 => t('False')), |
|
| 183 |
'#default_value' => $widget['allow_clear'] ? $widget['allow_clear'] : 1, |
'#default_value' => $widget['allow_clear'] ? $widget['allow_clear'] : 1, |
|
'#description' => t("Display a button to clear the user's current vote."), |
|
| 184 |
); |
); |
| 185 |
return $form; |
return $form; |
| 186 |
case 'save': |
case 'save': |
| 243 |
} |
} |
| 244 |
|
|
| 245 |
/** |
/** |
|
* Implementation of hook_field_formatter(). |
|
|
* |
|
|
* The $node argument is necessary so that filter access can be checked on |
|
|
* node preview. |
|
|
*/ |
|
|
function fivestar_field_formatter($field, $item, $formatter, $node) { |
|
|
if (!isset($item['rating'])) { |
|
|
$item['rating'] = 0; |
|
|
} |
|
|
|
|
|
switch ($formatter) { |
|
|
case 'rating': |
|
|
return round(100/$item['rating'], 1)/$field['stars']; |
|
|
|
|
|
case 'percentage': |
|
|
return $item['rating']; |
|
|
|
|
|
default: |
|
|
return theme('fivestar_static', $item['rating'], $field['stars']); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
| 246 |
* Theme function for 'default' fivestar field formatter. |
* Theme function for 'default' fivestar field formatter. |
| 247 |
*/ |
*/ |
| 248 |
function theme_fivestar_formatter_default($element) { |
function theme_fivestar_formatter_default($element) { |
| 249 |
if (!isset($element['#item']['rating'])) { |
if (!isset($element['#item']['rating'])) { |
| 250 |
$element['#item']['rating'] = 0; |
$element['#item']['rating'] = 0; |
| 251 |
} |
} |
| 252 |
|
if (empty($element['#item']['stars'])) { |
| 253 |
|
$element['#item']['stars'] = 5; |
| 254 |
|
} |
| 255 |
|
|
| 256 |
return theme('fivestar_static', $element['#item']['rating'], $element['#item']['stars']); |
return theme('fivestar_static', $element['#item']['rating'], $element['#item']['stars']); |
| 257 |
} |
} |
| 258 |
|
|
| 259 |
/** |
/** |
| 260 |
* Theme function for 'default' fivestar field formatter. |
* Theme function for 'rating' fivestar field formatter. |
| 261 |
*/ |
*/ |
| 262 |
function theme_fivestar_formatter_rating($element) { |
function theme_fivestar_formatter_rating($element) { |
| 263 |
if (!isset($element['#item']['rating'])) { |
if (!isset($element['#item']['rating'])) { |
| 264 |
$element['#item']['rating'] = 0; |
$element['#item']['rating'] = 0; |
| 265 |
} |
} |
| 266 |
|
|
| 267 |
return theme('fivestar_static', $element['#item']['rating'], $element['#field']['stars']); |
return $element['#item']['rating']; |
| 268 |
} |
} |
| 269 |
|
|
| 270 |
/** |
/** |
| 271 |
* Theme function for 'default' fivestar field formatter. |
* Theme function for 'percentage' fivestar field formatter. |
| 272 |
*/ |
*/ |
| 273 |
function theme_fivestar_formatter_percentage($element) { |
function theme_fivestar_formatter_percentage($element) { |
| 274 |
if (!isset($element['#item']['rating'])) { |
if (!isset($element['#item']['rating'])) { |
| 275 |
$element['#item']['rating'] = 0; |
$element['#item']['rating'] = 0; |
| 276 |
} |
} |
| 277 |
|
if (empty($element['#item']['stars'])) { |
| 278 |
|
$element['#item']['stars'] = 5; |
| 279 |
|
} |
| 280 |
|
|
| 281 |
return round(100/$element['#item']['rating'], 1)/$element['#field']['stars']; |
return round(100/$element['#item']['rating'], 1)/$element['#field']['stars']; |
| 282 |
} |
} |