| 1 |
<?php |
<?php |
| 2 |
// $Id: fivestar_field.inc,v 1.1.4.21 2008/04/23 06:50:12 quicksketch Exp $ |
// $Id: fivestar_field.inc,v 1.1.4.22 2008/06/19 02:42:53 quicksketch Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 28 |
); |
); |
| 29 |
$form['stars'] = array( |
$form['stars'] = array( |
| 30 |
'#type' => 'select', |
'#type' => 'select', |
| 31 |
'#title' => t('Number of Options'), |
'#title' => $field['widget']['type'] == 'stars' ? t('Number of Stars') : t('Number of Options'), |
| 32 |
'#options' => drupal_map_assoc(range(1, 10)), |
'#options' => drupal_map_assoc(range(1, 10)), |
| 33 |
'#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.'), |
|
| 34 |
); |
); |
| 35 |
|
|
| 36 |
|
$dynamic_options = array(); |
| 37 |
if (module_exists('nodecomment')) { |
if (module_exists('nodecomment')) { |
| 38 |
$form['dynamic_target'] = array( |
$dynamic_options['comment_target_nid'] = t('Node Comment Parent'); |
| 39 |
'#title' => t('Use Node Comment Parent as Target'), |
} |
| 40 |
'#type' => 'checkbox', |
if (module_exists('nodereference')) { |
| 41 |
'#default_value' => $field['dynamic_target'], |
$fields = content_fields(NULL, $field['type_name']); |
| 42 |
'#return_value' => 'comment_target_nid', |
foreach ($fields as $content_field) { |
| 43 |
'#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']) { |
| 44 |
); |
$dynamic_options[$content_field['field_name']] = t('Node reference: @field', array('@field' => $content_field['field_name'])); |
| 45 |
|
} |
| 46 |
|
} |
| 47 |
} |
} |
| 48 |
|
|
| 49 |
$form['target'] = array( |
if (empty($dynamic_options)) { |
| 50 |
'#type' => 'textarea', |
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'); |
| 51 |
'#title' => t('Target Node ID'), |
} |
| 52 |
'#default_value' => $field['target'] ? $field['target'] : '', |
|
| 53 |
'#description' => t( |
$dynamic_options = array('' => t('<none>')) + $dynamic_options; |
| 54 |
'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.' |
$form['dynamic_target'] = array( |
| 55 |
), |
'#title' => t('Voting target'), |
| 56 |
|
'#type' => 'select', |
| 57 |
|
'#default_value' => $field['dynamic_target'], |
| 58 |
|
'#options' => $dynamic_options, |
| 59 |
|
'#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>.') |
| 60 |
); |
); |
| 61 |
|
|
| 62 |
if (user_access('use PHP for fivestar target')) { |
if (user_access('use PHP for fivestar target')) { |
| 63 |
$form['target']['#description'] .= ' '. t( |
$form['php_target'] = array( |
| 64 |
' 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', |
| 65 |
|
'#title' => t('Voting target PHP code'), |
| 66 |
|
'#collapsible' => TRUE, |
| 67 |
|
'#collapsed' => empty($field['php_target']), |
| 68 |
); |
); |
| 69 |
|
|
| 70 |
$form['php'] = array( |
$form['php_target']['php_target'] = array( |
| 71 |
|
'#title' => t('Code'), |
| 72 |
|
'#type' => 'textarea', |
| 73 |
|
'#default_value' => $field['php_target'] ? $field['php_target'] : '', |
| 74 |
|
'#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.'), |
| 75 |
|
); |
| 76 |
|
} |
| 77 |
|
else { |
| 78 |
|
$form['php_target'] = array( |
| 79 |
'#type' => 'value', |
'#type' => 'value', |
| 80 |
'#value' => 1, |
'#value' => $field['php_target'] ? $field['php_target'] : '', |
| 81 |
); |
); |
| 82 |
} |
} |
| 83 |
|
|
| 90 |
|
|
| 91 |
return $form; |
return $form; |
| 92 |
case 'save': |
case 'save': |
| 93 |
return array('stars', 'dynamic_target', 'target', 'php', 'axis'); |
return array('stars', 'dynamic_target', 'php_target', 'axis'); |
| 94 |
case 'database columns': |
case 'database columns': |
| 95 |
$columns = array( |
$columns = array( |
| 96 |
'rating' => array('type' => 'int', 'default' => 'NULL', 'sortable' => TRUE), |
'rating' => array('type' => 'int', 'default' => 'NULL', 'sortable' => TRUE), |
| 114 |
case 'insert': |
case 'insert': |
| 115 |
case 'update': |
case 'update': |
| 116 |
foreach ($items as $delta => $item) { |
foreach ($items as $delta => $item) { |
| 117 |
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) { |
|
| 118 |
// Use eval rather than drupal_eval to allow access to local variables. |
// Use eval rather than drupal_eval to allow access to local variables. |
| 119 |
$items[$delta]['target'] = eval('?>'. $item['target']); |
$items[$delta]['target'] = eval($field['php_target']); |
| 120 |
|
} |
| 121 |
|
elseif ($field['dynamic_target'] && !empty($node->$field['dynamic_target'])) { |
| 122 |
|
if (is_array($node->$field['dynamic_target']) && is_numeric($node->{$field['dynamic_target']}[0]['nid'])) { |
| 123 |
|
$items[$delta]['target'] = $node->{$field['dynamic_target']}[0]['nid']; |
| 124 |
|
} |
| 125 |
|
elseif (is_numeric($node->$field['dynamic_target'])) { |
| 126 |
|
$items[$delta]['target'] = $node->$field['dynamic_target']; |
| 127 |
|
} |
| 128 |
} |
} |
| 129 |
|
|
| 130 |
if (is_numeric($items[$delta]['target'])) { |
if (is_numeric($items[$delta]['target'])) { |
| 131 |
_fivestar_cast_vote('node', $items[$delta]['target'], $item['rating'], $items[$delta]['axis'], $node->uid); |
_fivestar_cast_vote('node', $items[$delta]['target'], $items[$delta]['rating'], $items[$delta]['axis'], $node->uid); |
| 132 |
votingapi_recalculate_results('node', $items[$delta]['target']); |
votingapi_recalculate_results('node', $items[$delta]['target']); |
| 133 |
} |
} |
| 134 |
} |
} |
| 159 |
*/ |
*/ |
| 160 |
function fivestar_widget_info() { |
function fivestar_widget_info() { |
| 161 |
return array( |
return array( |
|
'radios' => array( |
|
|
'label' => t('Radio Buttons'), |
|
|
'field types' => array('fivestar'), |
|
|
), |
|
| 162 |
'stars' => array( |
'stars' => array( |
| 163 |
'label' => t('Stars'), |
'label' => t('Stars'), |
| 164 |
'field types' => array('fivestar'), |
'field types' => array('fivestar'), |
| 165 |
), |
), |
| 166 |
|
'radios' => array( |
| 167 |
|
'label' => t('Select list'), |
| 168 |
|
'field types' => array('fivestar'), |
| 169 |
|
), |
| 170 |
); |
); |
| 171 |
} |
} |
| 172 |
|
|
| 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': |
| 253 |
if (!isset($item['rating'])) { |
if (!isset($item['rating'])) { |
| 254 |
$item['rating'] = 0; |
$item['rating'] = 0; |
| 255 |
} |
} |
| 256 |
|
if (empty($field['stars'])) { |
| 257 |
|
$field['stars'] = 5; |
| 258 |
|
} |
| 259 |
|
|
| 260 |
switch ($formatter) { |
switch ($formatter) { |
| 261 |
case 'rating': |
case 'rating': |
| 265 |
return $item['rating']; |
return $item['rating']; |
| 266 |
|
|
| 267 |
default: |
default: |
| 268 |
return theme('fivestar_static', $item['rating'], $field['stars'] ? $field['stars'] : 5); |
return theme('fivestar_static', $item['rating'], $field['stars']); |
| 269 |
} |
} |
| 270 |
} |
} |