fivestar.install 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions the Fivestar module.
  5. */
  6. /**
  7. * Implements hook_uninstall().
  8. */
  9. function fivestar_uninstall() {
  10. db_query("DELETE FROM {variable} WHERE name LIKE 'fivestar_%'");
  11. }
  12. /**
  13. * Hook_field_schema().
  14. */
  15. function fivestar_field_schema() {
  16. return array(
  17. 'columns' => array(
  18. 'rating' => array(
  19. 'type' => 'int',
  20. 'unsigned' => TRUE,
  21. 'not null' => FALSE,
  22. 'sortable' => TRUE,
  23. ),
  24. 'target' => array(
  25. 'type' => 'int',
  26. 'unsigned' => TRUE,
  27. 'not null' => FALSE,
  28. ),
  29. ),
  30. );
  31. }
  32. /**
  33. * Fixes the axis value stored for fivestar fields.
  34. */
  35. function fivestar_update_7201() {
  36. drupal_load('module', 'fivestar');
  37. $fields = field_read_fields(array('module' => 'fivestar'));
  38. $tags_numeric = array_values(fivestar_get_tags());
  39. foreach ($fields as $field) {
  40. if (is_numeric($field['settings']['axis'])) {
  41. $field['settings']['axis'] = $tags_numeric[$field['settings']['axis']];
  42. }
  43. }
  44. }
  45. /**
  46. * Moves the field settings to field instance settings.
  47. */
  48. function fivestar_update_7202() {
  49. $fields = field_read_fields(array('module' => 'fivestar'));
  50. foreach ($fields as $field) {
  51. $instances = field_read_instances(array('field_name' => $field['field_name']));
  52. foreach ($instances as $instance) {
  53. $instance['settings'] = $field['settings'];
  54. field_update_instance($instance);
  55. }
  56. }
  57. }
  58. /**
  59. * Converts all existing fivestar/node_type settings into fields with exposed fivestar formatters.
  60. */
  61. function fivestar_update_7203() {
  62. // Gather the node types.
  63. $query = db_select('node_type', 'n');
  64. $query->addField('n', 'type');
  65. $result = $query->execute();
  66. $types = $result->fetchCol();
  67. // Gather the tags. In the case that fivestar_get_tags() is ever removed from
  68. // the module, this update still needs to run.
  69. $tags_txt = variable_get('fivestar_tags', 'vote');
  70. $tags_exploded = explode(',', $tags_txt);
  71. $tags = array();
  72. $got_vote = FALSE;
  73. foreach ($tags_exploded as $tag) {
  74. $tag_trimmed = trim($tag);
  75. if ($tag_trimmed) {
  76. $tags[] = $tag_trimmed;
  77. if ($tag_trimmed == 'vote') {
  78. $got_vote = TRUE;
  79. }
  80. }
  81. }
  82. if (!$got_vote) {
  83. $tags[] = 'vote';
  84. }
  85. $tags;
  86. foreach ($tags as $tag) {
  87. $suffix = '';
  88. foreach ($types as $type) {
  89. $var_suffix = $type . ($tag == 'vote' ? '' : '_' . $tag);
  90. $settings = array(
  91. 'stars' => variable_get('fivestar_stars_' . $var_suffix, 6),
  92. 'allow_clear' => variable_get('fivestar_unvote_' . $var_suffix, 0),
  93. 'feedback_enable' => variable_get('fivestar_feedback_' . $var_suffix, 1),
  94. 'style' => variable_get('fivestar_style_' . $var_suffix, 'average'),
  95. 'text' => variable_get('fivestar_text_' . $var_suffix, 'dual'),
  96. 'title' => variable_get('fivestar_title_' . $var_suffix, 1),
  97. );
  98. if (variable_get('fivestar_' . $var_suffix, FALSE)) {
  99. // Check to see if a field for this tag exists and create one if needed.
  100. $field_name = 'field_' . $tag;
  101. $field = field_read_field($field_name . $suffix, array('include_deleted' => TRUE));
  102. $i = 0;
  103. while (!empty($field) && $field['type'] != 'fivestar') {
  104. $suffix = '_' . $i;
  105. $field = field_read_field($field_name . $suffix, array('include_deleted' => TRUE));
  106. $i++;
  107. }
  108. if (empty($field)) {
  109. $field_values = array(
  110. 'field_name' => $field_name . $suffix,
  111. 'type' => 'fivestar',
  112. 'settings' => array(
  113. 'axis' => $tag,
  114. ),
  115. );
  116. $field = field_create_field($field_values);
  117. }
  118. // Create an instance of the field in this bundle.
  119. $instance = field_read_instance('node', $field['field_name'], $type, array('include_deleted' => TRUE));
  120. if (empty($instance)) {
  121. $instance_info = array(
  122. 'field_name' => $field['field_name'],
  123. 'entity_type' => 'node',
  124. 'bundle' => $type,
  125. 'widget' => array(
  126. 'type' => 'stars',
  127. ),
  128. 'display' => array(
  129. 'default' => array(
  130. 'type' => 'fivestar_formatter_exposed_stars',
  131. 'settings' => $settings,
  132. ),
  133. ),
  134. 'settings' => array(
  135. 'stars' => $settings['stars'],
  136. 'target' => 'self',
  137. ),
  138. );
  139. if (variable_get('fivestar_position_teaser_' . $var_suffix, 'hidden') != 'hidden') {
  140. $instance_info['display']['teaser'] = array(
  141. 'type' => 'fivestar_formatter_exposed_stars',
  142. 'settings' => $settings,
  143. );
  144. }
  145. // Set the widget.
  146. $widget = variable_get('fivestar_widget' . $var_suffix, 'default');
  147. $instance_info['widget']['settings']['widget']['fivestar_widget'] = $widget;
  148. field_create_instance($instance_info);
  149. }
  150. }
  151. }
  152. }
  153. // Rebuild the menu to remove the node type tag form paths.
  154. menu_rebuild();
  155. _field_info_collate_fields(TRUE);
  156. }
  157. /**
  158. * Preserve settings from fivestar_formatter_exposed_stars and convert to fivestar_formatter_default.
  159. */
  160. function fivestar_update_7204() {
  161. $fields = field_read_fields(array('type' => 'fivestar'));
  162. foreach ($fields as $field) {
  163. // Iterate through the instances of the field.
  164. $instances = field_read_instances(array('field_name' => $field['field_name']));
  165. foreach ($instances as $instance) {
  166. // The default should be to not allow clearing.
  167. $instance['settings']['allow_clear'] = FALSE;
  168. // Check each of the displays on the field instance an convert the formatter
  169. // from fivestar_formatter_exposed_stars to fivestar_formatter_default.
  170. foreach ($instance['display'] as $key => $display) {
  171. if ($display['type'] == 'fivestar_formatter_exposed_stars') {
  172. // Convert the formatter and set the exposed settings.
  173. $instance['display'][$key]['type'] == 'fivestar_formatter_default';
  174. $instance['display'][$key]['settings']['expose'] = TRUE;
  175. // The widget type needs to be exposed for the widget to be exposed.
  176. $instance['widget']['type'] = 'exposed';
  177. // If one of the displays allowed clearing change the field settings
  178. // to allow clearing.
  179. if ($display['settings']['allow_clear'] == TRUE) {
  180. $instance['settings']['allow_clear'] = TRUE;
  181. }
  182. }
  183. }
  184. // Update the instance.
  185. field_update_instance($instance);
  186. }
  187. }
  188. }
  189. /**
  190. * Rename fivestar 'select' widget to 'fivestar_select'.
  191. *
  192. * @see http://drupal.org/node/1285456
  193. */
  194. function fivestar_update_7205() {
  195. $fields = field_read_fields(array('type' => 'fivestar'));
  196. foreach ($fields as $field) {
  197. // Iterate through the instances of the field.
  198. $instances = field_read_instances(array('field_name' => $field['field_name']));
  199. foreach ($instances as $instance) {
  200. // If the widget type is select, lets change it.
  201. if ($instance['widget']['type'] == 'select') {
  202. $instance['widget']['type'] = 'fivestar_select';
  203. // Update the instance.
  204. field_update_instance($instance);
  205. }
  206. }
  207. }
  208. }
  209. /**
  210. * Preserve setting after new feature preventing re-votes.
  211. *
  212. * @see http://drupal.org/node/356605
  213. */
  214. function fivestar_update_7206() {
  215. $fields = field_read_fields(array('type' => 'fivestar'));
  216. foreach ($fields as $field) {
  217. // Iterate through the instances of the field.
  218. $instances = field_read_instances(array('field_name' => $field['field_name']));
  219. foreach ($instances as $instance) {
  220. // The default should be to allow re-voting.
  221. $instance['settings']['allow_revote'] = TRUE;
  222. // Update the instance.
  223. field_update_instance($instance);
  224. }
  225. }
  226. }
  227. /**
  228. * Preserve setting after new feature preventing own votes.
  229. *
  230. * @see http://drupal.org/node/189527
  231. */
  232. function fivestar_update_7207() {
  233. $fields = field_read_fields(array('type' => 'fivestar'));
  234. foreach ($fields as $field) {
  235. // Iterate through the instances of the field.
  236. $instances = field_read_instances(array('field_name' => $field['field_name']));
  237. foreach ($instances as $instance) {
  238. // The default should be to allow own votes.
  239. $instance['settings']['allow_ownvote'] = TRUE;
  240. // Update the instance.
  241. field_update_instance($instance);
  242. }
  243. }
  244. }
  245. /**
  246. * Change field formatters to ensure unique.
  247. *
  248. * @see http://drupal.org/node/1063754
  249. */
  250. function fivestar_update_7208() {
  251. $fields = field_read_fields(array('type' => 'fivestar'));
  252. foreach ($fields as $field) {
  253. // Iterate through the instances of the field.
  254. $instances = field_read_instances(array('field_name' => $field['field_name']));
  255. foreach ($instances as $instance) {
  256. $updated = FALSE;
  257. foreach ($instance['display'] as &$display) {
  258. if (in_array($display['type'], array('default', 'percentage', 'rating'))) {
  259. $updated = TRUE;
  260. $display['type'] = 'fivestar_formatter_' . $display['type'];
  261. }
  262. }
  263. if ($updated) {
  264. // Only trigger instance update if we actually changed anything.
  265. field_update_instance($instance);
  266. }
  267. }
  268. }
  269. }