stylizer.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /**
  3. * @file
  4. * Definition of the 'stylizer' panel style.
  5. */
  6. if (module_exists('stylizer')) {
  7. // Plugin definition
  8. $plugin = array(
  9. 'title' => t('Custom style'),
  10. 'weight' => -10,
  11. 'description' => t('Allows choice of a stylizer style'),
  12. 'render pane' => 'panels_stylizer_stylizer_style_render_pane',
  13. 'pane settings form' => 'panels_stylizer_stylizer_style_settings_form',
  14. 'render region' => 'panels_stylizer_stylizer_style_render_region',
  15. 'settings form' => 'panels_stylizer_stylizer_style_settings_form',
  16. // We offer substyles so provide callbacks to do so.
  17. 'get child' => 'panels_stylizer_get_substyle',
  18. 'get children' => 'panels_stylizer_get_substyles',
  19. // Set up an AJAX callback for the style
  20. 'ajax' => array(
  21. 'custom' => 'panels_stylizer_pane_add_style',
  22. ),
  23. // 'settings validate' => 'panels_stylizer_stylizer_style_settings_validate',
  24. );
  25. }
  26. /**
  27. * Merge the main stylizer plugin with a style to create a sub plugin.
  28. *
  29. * This is used for both panels_stylizer_get_substyle and
  30. * panels_stylizer_get_substyles.
  31. */
  32. function panels_stylizer_merge_plugin($plugin, $style) {
  33. $plugin['name'] = 'stylizer:' . $style->name;
  34. $plugin['title'] = check_plain($style->admin_title);
  35. $plugin['description'] = check_plain($style->admin_description);
  36. $plugin['style'] = $style;
  37. $plugin['weight'] = 0;
  38. ctools_include('stylizer');
  39. $base = ctools_get_style_base($style->settings['style_base']);
  40. if ($base['type'] == 'pane') {
  41. unset($plugin['render region']);
  42. }
  43. else {
  44. unset($plugin['render pane']);
  45. }
  46. unset($plugin['settings form']);
  47. unset($plugin['pane settings form']);
  48. return $plugin;
  49. }
  50. /**
  51. * Callback to provide a single stored stylizer style.
  52. */
  53. function panels_stylizer_get_substyle($plugin, $style_name, $substyle_name) {
  54. // Do not worry about caching; Panels is handling that for us.
  55. ctools_include('export');
  56. $item = ctools_export_crud_load('stylizer', $substyle_name);
  57. if ($item) {
  58. return panels_stylizer_merge_plugin($plugin, $item);
  59. }
  60. }
  61. /**
  62. * Callback to provide all stored stylizer styles.
  63. */
  64. function panels_stylizer_get_substyles($plugin, $style_name) {
  65. $styles[$style_name] = $plugin;
  66. ctools_include('export');
  67. ctools_include('stylizer');
  68. $items = ctools_export_crud_load_all('stylizer');
  69. foreach ($items as $name => $item) {
  70. $base = ctools_get_style_base($item->settings['style_base']);
  71. if ($base && $base['module'] == 'panels') {
  72. $styles['stylizer:' . $name] = panels_stylizer_merge_plugin($plugin, $item);
  73. }
  74. }
  75. return $styles;
  76. }
  77. /**
  78. * Get style settings for a stylizer style.
  79. *
  80. * Because style settings can come from a couple of different places,
  81. * depending on if it's a custom style in the panel or a custom style
  82. * in the database, we have a tricky way of looking for this info.
  83. */
  84. function _panels_stylizer_get_style($plugin, $style_settings) {
  85. if (!empty($plugin['style'])) {
  86. return $plugin['style']->settings;
  87. }
  88. if (empty($style_settings)) {
  89. return array();
  90. }
  91. if ($style_settings['style'] == '$') {
  92. return $style_settings['settings'];
  93. }
  94. ctools_include('export');
  95. $style = ctools_export_crud_load('stylizer', $style_settings['style']);
  96. if ($style) {
  97. return $style->settings;
  98. }
  99. }
  100. /**
  101. * Region render theme.
  102. */
  103. function theme_panels_stylizer_stylizer_style_render_region($vars) {
  104. $display = $vars['display'];
  105. $panes = $vars['panes'];
  106. $style_settings = $vars['settings'];
  107. $region_id = $vars['region_id'];
  108. $plugin = $vars['style'];
  109. $output = '';
  110. foreach ($panes as $pane_id => $pane_output) {
  111. $output .= $pane_output;
  112. }
  113. $settings = _panels_stylizer_get_style($plugin, $style_settings);
  114. if (!empty($settings)) {
  115. ctools_include('stylizer');
  116. $plugin = ctools_get_style_base($settings['style_base']);
  117. ctools_stylizer_add_css($plugin, $settings);
  118. return theme($plugin['theme'], array('settings' => $settings, 'class' => ctools_stylizer_get_css_class($plugin, $settings), 'content' => $output));
  119. }
  120. else {
  121. // if the style is gone, just display the output.
  122. return $output;
  123. }
  124. }
  125. /**
  126. * Pane render theme
  127. */
  128. function theme_panels_stylizer_stylizer_style_render_pane($vars) {
  129. $content = $vars['content'];
  130. $pane = $vars['pane'];
  131. $display = $vars['display'];
  132. $plugin = $vars['style'];
  133. $settings = _panels_stylizer_get_style($plugin, $vars['settings']);
  134. if ($settings) {
  135. ctools_include('stylizer');
  136. $plugin = ctools_get_style_base($settings['style_base']);
  137. if (empty($content->css_class)) {
  138. $content->css_class = ctools_stylizer_get_css_class($plugin, $settings);
  139. }
  140. else {
  141. $content->css_class .= ' ' . ctools_stylizer_get_css_class($plugin, $settings);
  142. }
  143. ctools_stylizer_add_css($plugin, $settings);
  144. if (isset($plugin['theme'])) {
  145. return theme($plugin['theme'], array('settings' => $settings, 'content' => $content, 'pane' => $pane, 'display' => $display));
  146. }
  147. }
  148. // if the style is gone or has no theme of its own, just display the output.
  149. return theme('panels_pane', array('content' => $content, 'pane' => $pane, 'display' => $display));
  150. }
  151. /**
  152. * Settings form callback.
  153. */
  154. function panels_stylizer_stylizer_style_settings_form($style_settings, $display, $pid, $type, $form_state) {
  155. // Just redirect this to the custom style settings ajax.
  156. panels_stylizer_pane_add_style($form_state['renderer'], array(), $style_settings, $type, $pid);
  157. print ajax_render($form_state['renderer']->commands);
  158. ajax_footer();
  159. exit;
  160. }
  161. /**
  162. * Allow on-the-fly creation of styles in panes.
  163. */
  164. function panels_stylizer_pane_add_style(&$renderer, $plugin, &$conf, $type, $pid, $step = NULL) {
  165. if (!user_access('administer panels styles')) {
  166. return;
  167. }
  168. // Reset the $_POST['ajax_html_ids'] values to preserve
  169. // proper IDs on form elements when auto-submit is used.
  170. $_POST['ajax_html_ids'] = array();
  171. ctools_include('stylizer');
  172. $js = FALSE;
  173. $path = $renderer->get_url('style', 'custom', $type, $pid, '%step');
  174. $info = array(
  175. 'module' => 'panels',
  176. 'type' => $type,
  177. 'path' => $path,
  178. 'modal' => t('Create custom style'),
  179. 'owner form' => 'panels_stylizer_edit_pane_style_form',
  180. 'owner form validate' => 'panels_stylizer_edit_pane_style_form_validate',
  181. 'owner form submit' => 'panels_stylizer_edit_pane_style_form_submit',
  182. 'owner settings' => array('preconfigured' => FALSE, 'name' => '', 'admin_title' => '', 'admin_description' => ''),
  183. 'cache' => &$renderer->cache,
  184. 'conf' => &$conf,
  185. 'pid' => $pid,
  186. );
  187. if (!empty($conf['settings'])) {
  188. $info['settings'] = $conf['settings'];
  189. }
  190. $output = ctools_stylizer_edit_style($info, TRUE, $step);
  191. if (!empty($info['complete'])) {
  192. if (!empty($info['owner settings']['preconfigured'])) {
  193. ctools_include('export');
  194. $style = ctools_export_crud_new('stylizer');
  195. $style->name = $info['settings']['name'];
  196. $style->admin_title = $info['owner settings']['admin_title'];
  197. $style->admin_description = $info['owner settings']['admin_description'];
  198. $style->settings = $info['settings'];
  199. ctools_export_crud_save('stylizer', $style);
  200. $conf['style'] = $info['settings']['name'];
  201. if (isset($conf['settings'])) {
  202. unset($conf['settings']);
  203. }
  204. }
  205. else {
  206. $conf['style'] = '$';
  207. $conf['settings'] = $info['settings'];
  208. }
  209. // Be sure to unset the temporary if the style was just changed.
  210. if (isset($renderer->cache->style)) {
  211. unset($renderer->cache->style);
  212. }
  213. // $conf was a reference so it should just modify.
  214. panels_edit_cache_set($renderer->cache);
  215. $renderer->commands[] = ctools_modal_command_dismiss();
  216. if ($type == 'pane') {
  217. $renderer->command_update_pane($pid);
  218. }
  219. else if ($type == 'region') {
  220. $renderer->command_update_region_links($pid);
  221. }
  222. else {
  223. $renderer->command_update_display_links();
  224. }
  225. }
  226. else {
  227. $renderer->commands = $output;
  228. }
  229. }
  230. /**
  231. * The form for determining if a pane should create a local style or a
  232. * preconfigured style.
  233. */
  234. function panels_stylizer_edit_pane_style_form(&$form, &$form_state) {
  235. if (!user_access('administer panels styles') || !module_exists('stylizer')) {
  236. return;
  237. }
  238. ctools_include('dependent');
  239. $settings = $form_state['owner info']['owner settings'];
  240. $form['panels']['admin_title'] = array(
  241. '#type' => 'textfield',
  242. '#title' => t('Administrative title'),
  243. '#description' => t('The name of this style. This will appear in the administrative interface to easily identify it.'),
  244. '#default_value' => $settings['admin_title'],
  245. '#process' => array('ctools_dependent_process'),
  246. '#dependency' => array('edit-preconfigured' => array(1)),
  247. );
  248. $form['panels']['name'] = array(
  249. '#type' => 'textfield',
  250. '#title' => t('Machine name'),
  251. '#description' => t('The machine readable name of this page. It must be unique, and it must contain only alphanumeric characters and underscores. Once created, you will not be able to change this value!'),
  252. '#default_value' => $settings['name'],
  253. '#process' => array('ctools_dependent_process'),
  254. '#dependency' => array('edit-preconfigured' => array(1)),
  255. );
  256. $form['panels']['admin_description'] = array(
  257. '#type' => 'textarea',
  258. '#title' => t('Administrative description'),
  259. '#description' => t('A description of what this style is, does or is for, for administrative use.'),
  260. '#default_value' => $settings['admin_description'],
  261. '#process' => array('ctools_dependent_process'),
  262. '#dependency' => array('edit-preconfigured' => array(1)),
  263. );
  264. // Add the checkbox, set the weight early
  265. $form['panels']['preconfigured'] = array(
  266. '#type' => 'checkbox',
  267. '#title' => t('Make this style available to other regions or panes'),
  268. '#default_value' => $settings['name'],
  269. '#weight' => -1,
  270. );
  271. }
  272. /**
  273. * Validate to see if we need to check the preconfigured values.
  274. */
  275. function panels_stylizer_edit_pane_style_form_validate(&$form, &$form_state) {
  276. if (!user_access('administer panels styles')) {
  277. return;
  278. }
  279. // Only validate if preconfigured is checked.
  280. if ($form_state['values']['preconfigured'] && !empty($form_state['clicked_button']['#wizard type'])) {
  281. if (empty($form_state['values']['admin_title'])) {
  282. form_error($form['panels']['admin_title'], t('You must choose an administrative title.'));
  283. }
  284. // If this is new, make sure the name is unique:
  285. if ($form_state['op'] == 'add') {
  286. if (empty($form_state['values']['name'])) {
  287. form_error($form['panels']['name'], t('You must choose a machine name.'));
  288. }
  289. ctools_include('export');
  290. $test = ctools_export_crud_load('stylizer', $form_state['values']['name']);
  291. if ($test) {
  292. form_error($form['panels']['name'], t('That name is used by another style: @page', array('@page' => $test->admin_title)));
  293. }
  294. // Ensure name fits the rules:
  295. if (preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) {
  296. form_error($form['panels']['name'], t('Name must be alphanumeric or underscores only.'));
  297. }
  298. }
  299. }
  300. }
  301. /**
  302. * Store the preconfigured values.
  303. */
  304. function panels_stylizer_edit_pane_style_form_submit(&$form, &$form_state) {
  305. if (!user_access('administer panels styles')) {
  306. return;
  307. }
  308. // Only validate if preconfigured is checked.
  309. if ($form_state['values']['preconfigured'] && !empty($form_state['clicked_button']['#wizard type'])) {
  310. $form_state['owner info']['owner settings']['admin_title'] = $form_state['values']['admin_title'];
  311. $form_state['owner info']['owner settings']['admin_description'] = $form_state['values']['admin_description'];
  312. // Clean up preview files before we set the name
  313. ctools_stylizer_cleanup_style($form_state['plugin'], $form_state['settings']);
  314. $form_state['settings']['name'] = $form_state['values']['name'];
  315. $form_state['name'] = $form_state['values']['name'];
  316. $form_state['owner info']['owner settings']['preconfigured'] = $form_state['values']['preconfigured'];
  317. }
  318. }