wysiwyg_button_order.module 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * Main Wysiwyg Button Order system
  6. *
  7. * The Wysiwyg Button Order system integrates with the Wysiwyg module admin page
  8. * to provide the functionality of re-ordering buttons and adding separators
  9. * between them.
  10. */
  11. /**
  12. * Implements hook_form_FORMID_alter().
  13. */
  14. function wysiwyg_button_order_form_wysiwyg_profile_form_alter(&$form, &$form_state) {
  15. // Define currently supported editors.
  16. $supported = array(
  17. 'tinymce',
  18. 'fckeditor',
  19. 'ckeditor',
  20. );
  21. // Only do something when the editor is supported.
  22. if (in_array($form['editor']['#value'], $supported)) {
  23. // Add javascript.
  24. drupal_add_js(drupal_get_path('module', 'wysiwyg_button_order') . '/wysiwyg_button_order.js');
  25. // Get the plugins, the format and the profile.
  26. $plugins = wysiwyg_get_plugins($form['editor']['#value']);
  27. $format = $form['format']['#value'];
  28. $profile = wysiwyg_get_profile($format);
  29. // Build the form.
  30. $form['buttonorder'] = array(
  31. '#type' => 'fieldset',
  32. '#title' => t('Order and separators'),
  33. '#collapsible' => TRUE,
  34. '#collapsed' => TRUE,
  35. '#tree' => TRUE,
  36. '#theme' => 'buttonorder_form',
  37. '#weight' => -1,
  38. );
  39. // Change the weight of existing items so that our form fits in a logical place.
  40. $form['buttons']['#weight'] = -2;
  41. $form['basic']['#weight'] = -3;
  42. // Get buttonorder settings if they have been saved already.
  43. if (isset($profile->settings['buttonorder']) && !empty($profile->settings['buttonorder'])) {
  44. $o_buttons = $profile->settings['buttonorder'];
  45. $o_buttons = explode(',', $o_buttons);
  46. // add increment to separators
  47. $count_sep = 0;
  48. foreach ($o_buttons as $k => $b) {
  49. $o_buttons[$k] = ($b == 'separator') ? $b . '-' . ++$count_sep : $b;
  50. }
  51. $o_buttons = array_flip($o_buttons);
  52. }
  53. // Get active buttons.
  54. foreach ($plugins as $name => $meta) {
  55. if (isset($meta['buttons']) && is_array($meta['buttons'])) {
  56. foreach ($meta['buttons'] as $button => $title) {
  57. if (!empty($profile->settings['buttons'][$name][$button])) {
  58. $u_buttons[$button] = array(
  59. 'group' => $name,
  60. 'name' => $title
  61. );
  62. }
  63. }
  64. }
  65. elseif (isset($meta['extensions']) && is_array($meta['extensions'])) {
  66. foreach ($meta['extensions'] as $extension => $title) {
  67. if (!empty($profile->settings['buttons'][$name][$extension])) {
  68. $u_buttons[$extension] = array(
  69. 'group' => $name,
  70. 'name' => $title
  71. );
  72. }
  73. }
  74. }
  75. }
  76. if (isset($u_buttons)) {
  77. if (isset($o_buttons)) {
  78. foreach ($o_buttons as $key => $val) {
  79. if (isset($u_buttons[$key])) {
  80. $buttonset[$key] = $u_buttons[$key];
  81. }
  82. elseif ((substr($key, 0, 9) == 'separator')) {
  83. $buttonset[$key] = array(
  84. 'group' => '---------------',
  85. 'name' => t('Separator'),
  86. );
  87. }
  88. }
  89. }
  90. else {
  91. $buttonset = $u_buttons;
  92. }
  93. $order = 0;
  94. foreach ($buttonset as $name => $val) {
  95. $form['buttonorder'][$name]['name'] = array(
  96. '#markup' => $val['name'],
  97. );
  98. $form['buttonorder'][$name]['group'] = array(
  99. '#markup' => $val['group'],
  100. '#prefix' => '<em>',
  101. '#suffix' => '</em>',
  102. );
  103. $form['buttonorder'][$name]['weight'] = array(
  104. '#type' => 'hidden',
  105. '#title' => t('Weight for @title', array('@title' => $val['name'])),
  106. '#title_display' => 'invisible',
  107. '#default_value' => $order++,
  108. );
  109. $form['buttonorder'][$name]['remove'] = array(
  110. '#type' => 'link',
  111. '#title' => t('Remove'),
  112. '#href' => '#',
  113. );
  114. }
  115. }
  116. // add a submit function before the usual submit
  117. array_unshift($form['#submit'], 'wysiwyg_button_order_submit');
  118. }
  119. }
  120. /**
  121. * Form submission handler for wysiwyw_buttonorder_form().
  122. */
  123. function wysiwyg_button_order_submit($form, &$form_state) {
  124. // drupal_set_message('<pre>' . print_r($form_state['input'], TRUE) . '</pre>');
  125. if (!empty($form_state['input'])) {
  126. if (isset($form_state['input']['buttonorder'])) {
  127. $input_format = $form_state['values']['format'];
  128. $buttonorder = FALSE;
  129. foreach ($form_state['input']['buttonorder'] as $key => $val) {
  130. if (!is_null($val['weight'])) {
  131. if (substr($key, 0, 9) == 'separator') {
  132. $buttonorder[] = substr($key, 0, 9);
  133. }
  134. else {
  135. $buttonorder[] = $key;
  136. }
  137. }
  138. }
  139. // make our ordered buttonarray into a comma separated string
  140. $buttonorder = implode(',', $buttonorder);
  141. unset($form_state['values']['buttonorder']);
  142. $form_state['values']['buttonorder'] = $buttonorder;
  143. drupal_set_message(t('Buttons have be re-ordered for %format.', array('%format' => $input_format)));
  144. }
  145. }
  146. }
  147. /**
  148. * Implements hook_theme().
  149. */
  150. function wysiwyg_button_order_theme() {
  151. return array(
  152. 'buttonorder_form' => array(
  153. 'render element' => 'theme',
  154. ),
  155. );
  156. }
  157. /**
  158. * Returns HTML for the draggable wysiwyg buttonorder table.
  159. */
  160. function theme_buttonorder_form($variables) {
  161. $form = $variables['theme'];
  162. $rows = array();
  163. if (element_children($form)) {
  164. foreach (element_children($form) as $name) {
  165. $form[$name]['weight']['#attributes']['class'] = array('buttons-weight');
  166. $form[$name]['group']['#attributes']['class'] = array('buttons-group');
  167. $form[$name]['name']['#attributes']['class'] = array('buttons-name');
  168. $form[$name]['remove']['#attributes']['class'] = array('removelink');
  169. $rows[] = array(
  170. 'data' => array(
  171. drupal_render($form[$name]['name']),
  172. drupal_render($form[$name]['group']),
  173. drupal_render($form[$name]['weight']),
  174. drupal_render($form[$name]['remove']),
  175. ),
  176. 'class' => (substr($name, 0, 9) == 'separator' ? array('draggable', 'separator') : array('draggable')),
  177. 'id' => 'order-' . $name,
  178. );
  179. }
  180. $header = array(t('Name'), t('Group'), t('Weight'), array('data' => t('Operations')));
  181. $output = theme('table', array(
  182. 'header' => $header,
  183. 'rows' => $rows,
  184. 'attributes' => array('id' => 'buttonorder'),
  185. ));
  186. $output .= drupal_render_children($form);
  187. drupal_add_tabledrag('buttonorder', 'order', 'sibling', 'buttons-weight');
  188. return $output;
  189. }
  190. else {
  191. drupal_set_message(t('To use the Wysiwyg Button Order module, enable some buttons, save, then return to this page.'));
  192. }
  193. }
  194. /**
  195. * Implements hook_settings_alter().
  196. */
  197. function wysiwyg_button_order_wysiwyg_editor_settings_alter(&$settings, $context) {
  198. // TinyMCE.
  199. if ($context['profile']->editor == 'tinymce') {
  200. $which = $context['profile']->format;
  201. $row = db_query("SELECT w.format, ff.name, w.settings FROM {wysiwyg} w INNER JOIN {filter_format} ff ON ff.format=w.format WHERE w.format = :which", array(':which' => $which))->fetch();
  202. if (isset($row->settings)) {
  203. $things = unserialize($row->settings);
  204. // check if a button order has been set, if true, load the buttonorder
  205. if (isset($things['buttonorder'])) {
  206. $settings['theme_advanced_buttons1'] = $things['buttonorder'];
  207. }
  208. }
  209. }
  210. // FCKeditor and CKeditor.
  211. if ($context['profile']->editor == 'fckeditor' || $context['profile']->editor == 'ckeditor') {
  212. $which = $context['profile']->format;
  213. $row = db_query("SELECT w.format, ff.name, w.settings FROM {wysiwyg} w INNER JOIN {filter_format} ff ON ff.format=w.format WHERE w.format = :which", array(':which' => $which))->fetch();
  214. if (isset($row->settings)) {
  215. $things = unserialize($row->settings);
  216. // check if a button order has been set, if true, load the buttonorder
  217. if (isset($things['buttonorder'])) {
  218. $things = explode(',', $things['buttonorder']);
  219. array_walk($things, create_function('&$val', 'if($val=="separator") $val = "-";'));
  220. switch ($context['profile']->editor) {
  221. case 'fckeditor' : $settings['buttons'][0] = $things;
  222. break;
  223. case 'ckeditor' : $settings['toolbar'][0] = $things;
  224. break;
  225. }
  226. }
  227. }
  228. }
  229. }