flag.views_convert.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * @file
  4. * Update Views 1 views provided by both views_bookmark and flag modules.
  5. */
  6. /**
  7. * Implements hook_views_convert().
  8. *
  9. * Intervene to convert field values from the Views 1 format to the
  10. * Views 2 format. Intervene only if $view->add_item() won't produce
  11. * the right results, usually needed to set field options or values.
  12. */
  13. function flag_views_convert($display, $type, &$view, $field, $id) {
  14. static $flag_name;
  15. // First, replace any sign of views_bookmark with flag's Views 1 equivelant.
  16. $key_search = array(
  17. '^views_bookmark_ops_([0-9]+)' => 'flag_ops_',
  18. '^views_bookmark_nodes_([0-9]+)' => 'flag_content_',
  19. '^views_bookmark_users_([0-9]+)' => 'flag_users_',
  20. '^views_bookmark_node_count_([0-9]+)' => 'flag_counts_',
  21. );
  22. foreach ($field as $property => $value) {
  23. foreach ($key_search as $search => $replace) {
  24. if (!empty($value) && is_string($value) && preg_match('/' . $search . '/', $value, $matches)) {
  25. $flag = flag_get_flag(NULL, $matches[1]);
  26. $replace = $replace . $flag->name;
  27. $field[$property] = preg_replace('/' . $search . '/', $replace, $value);
  28. }
  29. }
  30. }
  31. // Create a table/field identifier for this field.
  32. foreach (array('flag_ops_', 'flag_content_', 'flag_users_', 'flag_counts_') as $table) {
  33. if (strpos($field['tablename'], $table) === 0) {
  34. $name = str_replace($table, '', $field['tablename']);
  35. if (!empty($name) && !isset($flag_name)) {
  36. $flag_name = $name;
  37. }
  38. }
  39. }
  40. // Now update values, options, etc. to those selected in the imported view.
  41. switch ($type) {
  42. case 'field':
  43. switch ($id) {
  44. case 'ops':
  45. $new_field = array(
  46. 'label' => $field['label'],
  47. 'id' => 'ops',
  48. 'table' => 'flagging',
  49. 'field' => 'ops',
  50. 'relationship' => 'flag_content_rel',
  51. );
  52. $new_rel = 'flag_content_rel';
  53. $flag_content_rel_user = 'current';
  54. break;
  55. case 'count':
  56. $new_field = array(
  57. 'label' => $field['label'],
  58. 'id' => 'count',
  59. 'table' => 'flag_counts',
  60. 'field' => 'count',
  61. 'relationship' => 'flag_counts_rel',
  62. );
  63. $new_rel = 'flag_counts_rel';
  64. break;
  65. case 'name':
  66. $new_field = array(
  67. 'label' => $field['label'],
  68. 'link_to_user' => 1,
  69. 'id' => 'name',
  70. 'table' => 'users',
  71. 'field' => 'name',
  72. 'relationship' => 'uid',
  73. );
  74. $new_rel = 'uid';
  75. break;
  76. }
  77. break;
  78. case 'filter':
  79. case 'exposed_filter':
  80. switch ($id) {
  81. case 'uid':
  82. // The flagging uid filter means "Include content only flagged by
  83. // the current user". In D6, this is no longer a filter, but instead
  84. // part of the relationship. Make the relationship join on the uid.
  85. if ($field['value'] == '***CURRENT_USER***') {
  86. $new_rel = 'flag_content_rel';
  87. $flag_content_rel_user = 'current';
  88. }
  89. // Remove the old filter.
  90. $view->set_item('default', $type, $id, NULL);
  91. break;
  92. case 'timestamp':
  93. $new_field = array(
  94. 'operator' => $field['operator'],
  95. 'value' => flag_views_convert_timestamp_value($field['value']),
  96. 'group' => 0,
  97. 'id' => 'timestamp',
  98. 'table' => 'flagging',
  99. 'field' => 'timestamp',
  100. 'relationship' => 'flag_content_rel',
  101. 'exposed' => $type == 'exposed_filter' ? 1 : 0,
  102. );
  103. $new_rel = 'flag_content_rel';
  104. drupal_set_message(t('Flag is not able to convert the <em>Flagged time</em> filter. It\'s value is currently empty, but needs to be populated to work properly.'), 'warning');
  105. break;
  106. case 'count':
  107. $new_field = array(
  108. 'operator' => $field['operator'],
  109. 'value' => array('value' => $field['value']),
  110. 'group' => 0,
  111. 'id' => 'count',
  112. 'table' => 'flag_counts',
  113. 'field' => 'count',
  114. 'relationship' => 'flag_counts_rel',
  115. 'exposed' => $type == 'exposed_filter' ? 1 : 0,
  116. );
  117. $new_rel = 'flag_counts_rel';
  118. break;
  119. }
  120. break;
  121. case 'argument':
  122. // Flag in Drupal 5 only provides one argument, and in Views 1 arguments
  123. // weren't given and ID, so we use the field type.
  124. if (strpos($field['type'], 'flag_content_') === 0) {
  125. $new_field = array(
  126. 'id' => 'uid',
  127. 'table' => 'users',
  128. 'field' => 'uid',
  129. 'relationship' => 'uid',
  130. );
  131. $new_rel = 'uid';
  132. }
  133. break;
  134. case 'sort':
  135. switch ($id) {
  136. case 'count':
  137. $new_field = array(
  138. 'order' => $field['sortorder'],
  139. 'id' => 'count',
  140. 'table' => 'flag_counts',
  141. 'field' => 'count',
  142. 'relationship' => 'flag_counts_rel',
  143. );
  144. $new_rel = 'flag_counts_rel';
  145. break;
  146. case 'timestamp':
  147. $new_field = array(
  148. 'order' => $field['sortorder'],
  149. 'id' => 'timestamp',
  150. 'table' => 'flagging',
  151. 'field' => 'timestamp',
  152. 'relationship' => 'flag_content_rel',
  153. );
  154. $new_rel = 'flag_content_rel';
  155. break;
  156. }
  157. break;
  158. }
  159. // Add any new fields.
  160. if (isset($new_field)) {
  161. // Exposed filters are now wrapped into filters.
  162. $type = $type == 'exposed_filter' ? 'filter' : $type;
  163. // Update the type in the view options.
  164. $view->set_item('default', $type, $id, $new_field);
  165. }
  166. // Add any new relationships (but only once per view).
  167. if (isset($new_rel) && !isset($view->display[$display]->display_options['relationships'][$new_rel])) {
  168. if ($new_rel == 'flag_content_rel') {
  169. $view->display[$display]->display_options['relationships'][$new_rel] = array(
  170. 'label' => $flag_name,
  171. 'required' => 1,
  172. 'flag' => $flag_name,
  173. 'user_scope' => 'any',
  174. 'id' => 'flag_content_rel',
  175. 'table' => 'node',
  176. 'field' => 'flag_content_rel',
  177. 'relationship' => 'none',
  178. );
  179. }
  180. elseif ($new_rel == 'flag_counts_rel') {
  181. $view->display[$display]->display_options['relationships'][$new_rel] = array(
  182. 'label' => $flag_name . ' counts',
  183. 'required' => 0,
  184. 'flag' => $flag_name,
  185. 'id' => 'flag_counts_rel',
  186. 'table' => 'node',
  187. 'field' => 'flag_counts_rel',
  188. 'relationship' => 'none',
  189. );
  190. }
  191. elseif ($new_rel == 'uid') {
  192. $view->display[$display]->display_options['relationships'][$new_rel] = array(
  193. 'label' => isset($flag_name) ? $flag_name . ' user' : 'user',
  194. 'required' => 0,
  195. 'id' => 'uid',
  196. 'table' => 'flagging',
  197. 'field' => 'uid',
  198. 'relationship' => 'flag_content_rel',
  199. );
  200. }
  201. }
  202. // Modify relationships if needed.
  203. if (isset($flag_content_rel_user) && isset($view->display[$display]->display_options['relationships']['flag_content_rel'])) {
  204. $view->display[$display]->display_options['relationships']['flag_content_rel']['user_scope'] = 'current';
  205. }
  206. }
  207. /**
  208. * In Views 1, dates were simply stored as a single string. In Views 2, the
  209. * value is stored as an array of options.
  210. */
  211. function flag_views_convert_timestamp_value($value) {
  212. $type = 'date';
  213. if (stripos($value, 'now') !== FALSE) {
  214. $type = 'offset';
  215. $value = trim(str_ireplace('now', '', $value));
  216. }
  217. return array(
  218. 'type' => $type,
  219. 'value' => $value,
  220. 'min' => '',
  221. 'max' => '',
  222. );
  223. }