View source
<?php
define('SWEAVER_STYLE_SAVE', 0);
define('SWEAVER_STYLE_SAVE_PUBLISH', 1);
define('SWEAVER_STYLE_PUBLISH', 2);
class sweaver_plugin_styles extends sweaver_plugin {
public function sweaver_menu(&$weight, $page_arguments, $base) {
$items = array();
$page_arguments = array(
'plugin' => 'sweaver_plugin_styles',
);
$items['admin/settings/sweaver/styles'] = $base + array(
'title' => 'Styles',
'page arguments' => array(
$page_arguments,
),
'type' => MENU_LOCAL_TASK,
'weight' => $weight++,
);
$page_arguments['callback_method'] = 'sweaver_style_enable';
$items['admin/settings/sweaver/styles/enable'] = $base + array(
'title' => 'Enable style',
'page arguments' => array(
$page_arguments,
),
'type' => MENU_CALLBACK,
);
$page_arguments['callback_method'] = 'sweaver_style_disable';
$items['admin/settings/sweaver/styles/disable'] = $base + array(
'title' => 'Disable style',
'page arguments' => array(
$page_arguments,
),
'type' => MENU_CALLBACK,
);
$page_arguments['callback_method'] = 'sweaver_style_confirm_form_delete';
$items['admin/settings/sweaver/styles/delete'] = $base + array(
'title' => 'Delete style',
'page arguments' => array(
$page_arguments,
),
'type' => MENU_CALLBACK,
);
$base['access arguments'] = array(
'use editor',
);
$page_arguments['callback_method'] = 'sweaver_autosave';
$items['sweaver-autosave'] = $base + array(
'title' => 'Autosave',
'page arguments' => array(
$page_arguments,
),
'type' => MENU_CALLBACK,
);
return $items;
}
public function sweaver_form() {
$form = array();
$form['#popups'] = array();
$sweaver = Sweaver::get_instance();
$current_style = $sweaver
->get_current_style();
$existing_styles = FALSE;
$existing_styles_options = array();
$drafts = db_query("SELECT style_id, style FROM {sweaver_style_draft} where theme = '%s'", $sweaver
->get_theme_key());
while ($data = db_fetch_array($drafts)) {
$existing_styles_options[$data['style_id']] = $data['style'];
}
if (count($existing_styles_options) > 0) {
$existing_styles = TRUE;
}
$lives = db_query("SELECT style_id, style FROM {sweaver_style} where theme = '%s' AND active = 1", $sweaver
->get_theme_key());
while ($data = db_fetch_array($drafts)) {
$existing_styles_options[$data['style_id']] = $data['style'];
}
$form['save_style_popup']['save_style'] = array(
'#type' => 'textfield',
'#size' => 50,
'#weight' => 1,
);
if ($existing_styles) {
$form['save_style_popup']['save_style']['#attributes'] = array(
'style' => 'display:none',
);
$form['save_style_popup']['save_type'] = array(
'#type' => 'select',
'#options' => array(
0 => t('New style'),
1 => t('Existing style'),
),
'#default_value' => 1,
'#attributes' => array(
'class' => 'radio-style-save-type',
),
'#weight' => 0,
);
$form['save_style_popup']['style_existing_id'] = array(
'#type' => 'select',
'#options' => $existing_styles_options,
'#default_value' => isset($current_style->style_id) ? $current_style->style_id : 0,
'#weight' => 2,
);
}
else {
$form['save_style_popup']['save_type'] = array(
'#type' => 'value',
'#value' => 0,
);
$form['save_style_popup']['style_existing_id'] = array(
'#type' => 'value',
'#value' => 0,
);
}
$form['save_style_popup']['save']['save_continue'] = array(
'#type' => 'submit',
'#value' => t('Save and continue'),
'#weight' => 4,
);
$form['save_style_popup']['save']['save_publish'] = array(
'#type' => 'submit',
'#value' => t('Save and publish'),
'#weight' => 5,
);
$form['save_style_popup']['save']['#weight'] = 10;
$form['save_style_popup']['save']['#prefix'] = '<div class="save-publish-buttons clear-block">';
$form['save_style_popup']['save']['#suffix'] = '</div>';
if ($existing_styles) {
$load_style_options = $existing_styles_options;
$active_results = db_query("SELECT style_id, style FROM {sweaver_style} WHERE active = 1");
while ($active = db_fetch_object($active_results)) {
$load_style_options[$active->style_id . '_live_table'] = $active->style . ' (' . t('live') . ')';
}
$load_style_options[0] = t('Fresh style');
$form['load_style_popup']['load_style'] = array(
'#type' => 'select',
'#options' => $load_style_options,
);
$form['load_style_popup']['load_submit'] = array(
'#type' => 'submit',
'#value' => t('Load style'),
);
}
if (isset($current_style->style_id) && !empty($current_style->style_id)) {
$form['publish_style_popup']['publish_style'] = array(
'#type' => 'markup',
'#value' => t('Set style %stylename visible for your visitors. Any unsaved changes will also be saved when publishing.', array(
'%stylename' => $current_style->style,
)),
);
$form['publish_style_popup']['publish_id'] = array(
'#type' => 'hidden',
'#value' => $current_style->style_id,
);
$form['publish_style_popup']['publish_submit'] = array(
'#type' => 'submit',
'#value' => t('Publish style'),
);
}
if ($existing_styles && variable_get('sweaver_styles_delete_tab', FALSE)) {
$form['delete_style_popup']['question']['delete_style'] = array(
'#type' => 'select',
'#options' => $existing_styles_options,
);
$form['delete_style_popup']['question']['delete_confirm'] = array(
'#type' => 'button',
'#value' => t('Delete'),
);
$form['delete_style_popup']['question']['#prefix'] = '<div class="delete-style-confirm">';
$form['delete_style_popup']['question']['#suffix'] = '</div>';
$form['delete_style_popup']['warning'] = array(
'#type' => 'item',
'#value' => '<div>' . t('Are you sure you want to delete the style ? All changes and files will be lost. If the style is also active, your visitors will see the default theming.') . '</div>',
);
$form['delete_style_popup']['warning']['delete_submit'] = array(
'#type' => 'submit',
'#value' => t('Delete style'),
);
$form['delete_style_popup']['warning']['delete_cancel'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
);
$form['delete_style_popup']['warning']['#prefix'] = '<div class="delete-style-question clear-block" style="display:none">';
$form['delete_style_popup']['warning']['#suffix'] = '</div>';
}
$i = 0;
$form['#popups_links'] = '';
$form['#popups_styles'] = array(
'save_style_popup' => array(
'title' => t('Save'),
'description' => 'Save and keep working on your style. You can also publish it immediately for your visitors.',
),
'load_style_popup' => array(
'title' => t('Load'),
'description' => t('Load a style to continue working on it. It will only be visible for your visitors after publishing.'),
),
'publish_style_popup' => array(
'title' => t('Publish'),
'description' => '',
),
'delete_style_popup' => array(
'title' => t('Delete'),
'description' => t('Delete a style which you do not want to use anymore.'),
),
);
foreach ($form['#popups_styles'] as $key => $action) {
if (isset($form[$key])) {
++$i;
$form['#popups'][] = $key;
$form['#popups_links'] .= '<div class="style-actions-link"><a href="#" id="style-actions-link-' . $i . '">' . $action['title'] . '</a></div>';
$form[$key]['#prefix'] = '<div style="display: none;" class="' . str_replace('_', '-', $key) . '" id="style-actions-data-' . $i . '"><h2>' . t('@action style', array(
'@action' => $action['title'],
)) . '</h2><p>' . $action['description'] . '</p>';
$form[$key]['#suffix'] = '</div>';
}
}
return $form;
}
public function sweaver_form_render(&$vars, &$form, $plugin) {
$vars['style_actions'] = $form['sweaver_plugin_styles']['form']['#popups_links'];
$this
->sweaver_popups_render($vars, $form, $plugin['name']);
}
public function sweaver_form_css_js(&$inline_settings) {
if (variable_get('sweaver_styles_autosave', 0) > 0) {
$inline_settings['sweaver']['autosave'] = variable_get('sweaver_styles_autosave', 0);
}
drupal_add_js(drupal_get_path('module', 'sweaver') . '/plugins/sweaver_plugin_styles/sweaver_plugin_styles.js', 'module');
}
public function sweaver_form_submit($form, &$form_state) {
$path = file_create_path('sweaver');
file_check_directory($path, FILE_CREATE_DIRECTORY);
$styles_form = $form['sweaver_plugin_styles']['form'];
$clicked_button = $form_state['clicked_button']['#value'];
if ($clicked_button == t('Save and continue') || $clicked_button == t('Save and publish') || $clicked_button == t('Publish style')) {
$this
->sweaver_reset_style_session();
$update = array();
$style = new stdClass();
$theme_key = $form['#current_theme'];
$form_state['publish'] = TRUE;
if ($clicked_button == t('Save and publish')) {
$submit_type = SWEAVER_STYLE_SAVE_PUBLISH;
}
elseif ($clicked_button == t('Publish style')) {
$submit_type = SWEAVER_STYLE_PUBLISH;
}
else {
$form_state['publish'] = FALSE;
$submit_type = SWEAVER_STYLE_SAVE;
}
$style_name = $form_state['values']['save_style'];
$keep_working = $submit_type == SWEAVER_STYLE_PUBLISH ? FALSE : TRUE;
$save_type = $submit_type == SWEAVER_STYLE_PUBLISH ? TRUE : $form_state['values']['save_type'];
$style_existing_id = $submit_type == SWEAVER_STYLE_PUBLISH ? $form_state['values']['publish_id'] : $form_state['values']['style_existing_id'];
if ($save_type) {
$update = array(
'style_id',
);
$style->style_id = $style_existing_id;
$style->style = $styles_form['save_style_popup']['style_existing_id']['#options'][$style->style_id];
}
else {
$style->style = $style_name;
if (empty($style->style)) {
sweaver_session(t('You need to enter a name for your style.'));
$form_state['clicked_button']['#value'] = t('Unknown Sweaver button.');
return;
}
}
$style->theme = $theme_key;
$style->css = $form_state['values']['css'];
drupal_write_record('sweaver_style_draft', $style, $update);
$this
->sweaver_export_file($form_state['values']['css_rendered'], $style);
$form_state['style_id'] = $style->style_id;
if ($submit_type == SWEAVER_STYLE_PUBLISH || $submit_type == SWEAVER_STYLE_SAVE_PUBLISH) {
$style->active = 1;
$this
->sweaver_export_file($form_state['values']['css_rendered'], $style, 'live');
$message = 'The style @style has been published.';
db_query("UPDATE {sweaver_style} set active = 0 WHERE theme = '%s'", $style->theme);
if (db_result(db_query("SELECT style_id FROM {sweaver_style} WHERE style_id = %d", $style->style_id)) === FALSE) {
$update = array();
}
drupal_write_record('sweaver_style', $style, $update);
}
if ($submit_type != SWEAVER_STYLE_PUBLISH) {
sweaver_session(TRUE, 'draft_mode');
sweaver_session($style->style_id, 'loaded_style');
sweaver_session('draft', 'loaded_table');
$message = 'The style @style has been saved. You can keep working on your style.';
}
if ($submit_type == SWEAVER_STYLE_SAVE_PUBLISH) {
$message = 'The style @style has been saved and published. You can keep working on your style.';
}
sweaver_session(t($message, array(
'@style' => $style->style,
)));
}
if ($clicked_button == t('Load style')) {
$this
->sweaver_reset_style_session();
$theme_key = $form['#current_theme'];
$style_id = $form_state['values']['load_style'];
$style_name = $styles_form['load_style_popup']['load_style']['#options'][$style_id];
$table = 'draft';
$pos = strpos($style_id, '_live_table');
if ($pos !== FALSE) {
$table = 'live';
$style_id = str_replace('_live_table', '', $style_id);
}
sweaver_session(TRUE, 'draft_mode');
sweaver_session($style_id, 'loaded_style');
sweaver_session($table, 'loaded_table');
sweaver_session(t('The style @style has been loaded. It is only visible for you.', array(
'@style' => $style_name,
)));
}
if ($clicked_button == t('Delete style')) {
$this
->sweaver_reset_style_session();
$style_id = $form_state['values']['delete_style'];
$style = db_fetch_object(db_query("SELECT * FROM {sweaver_style_draft} WHERE style_id = %d", $style_id));
$style_live = db_fetch_object(db_query("SELECT * FROM {sweaver_style} WHERE style_id = %d", $style_id));
$form_state['style_active'] = isset($style_live->active) ? $style_live->active : FALSE;
$form_state['style_to_delete'] = $style;
$this
->sweaver_delete_style($style);
$style_name = $styles_form['delete_style_popup']['question']['delete_style']['#options'][$style_id];
sweaver_session(t('The style @style has been deleted.', array(
'@style' => $style_name,
)));
}
sweaver_clear_cache();
}
public function sweaver_export_file($css, $style, $type = 'draft') {
$css = str_replace('<style type="text/css">', '', $css);
$css = str_replace('</style>', '', $css);
$filename = 'sweaver_' . $style->theme . '_' . $style->style_id . '_' . $type . '.css';
$path = file_create_path('sweaver');
file_check_directory($path, FILE_CREATE_DIRECTORY);
$file = $path . '/' . $filename;
if (!($fp = fopen($file, 'w'))) {
sweaver_session(t('The css file could not be created.'));
}
else {
$css = $this
->sweaver_ctools_css_filter($css, FALSE);
fwrite($fp, $css);
}
@fclose($fp);
}
function sweaver_ctools_css_filter($css, $compressed = TRUE) {
ctools_include('css');
$css_data = ctools_css_disassemble($css);
$empty_array = array();
$allowed_properties = $this
->sweaver_ctools_allowed_properties();
$disallowed_values_regex = '/(expression)/';
$filtered = ctools_css_filter_css_data($css_data, $allowed_properties, $empty_array, '', $disallowed_values_regex);
return $compressed ? ctools_css_compress($filtered) : ctools_css_assemble($filtered);
}
function sweaver_ctools_allowed_properties() {
$allowed = ctools_css_filter_default_allowed_properties();
$allowed_properties = array();
$exploded = explode("\n", variable_get('sweaver_ctools_allowed_properties', SWEAVER_CTOOLS_ALLOWED_PROPERTIES));
foreach ($exploded as $key => $class) {
$trimmed = trim($class);
if (!empty($trimmed)) {
$allowed_properties[] = $trimmed;
}
}
return array_merge($allowed, $allowed_properties);
}
public function sweaver_delete_style($style) {
db_query("DELETE FROM {sweaver_style} WHERE style_id = %d", $style->style_id);
db_query("DELETE FROM {sweaver_style_draft} WHERE style_id = %d", $style->style_id);
$draft = file_directory_path() . '/sweaver/sweaver_' . $style->theme . '_' . $style->style_id . '_draft.css';
$live = file_directory_path() . '/sweaver/sweaver_' . $style->theme . '_' . $style->style_id . '_live.css';
file_delete($draft);
file_delete($live);
$dir = file_directory_path() . '/sweaver';
$mask = '(.*)' . $style->theme . '_' . $style->style_id . '_(.*)';
$files = file_scan_directory($dir, $mask);
$mask = '(.*)' . $style->theme . '_temp_(.*)';
$files += file_scan_directory($dir, $mask);
foreach ($files as $key => $file) {
file_delete($file->filename);
}
}
public function sweaver_reset_style_session() {
sweaver_session(NULL, 'loaded_table', TRUE);
sweaver_session(NULL, 'draft_mode', TRUE);
sweaver_session(NULL, 'loaded_style', TRUE);
sweaver_session(NULL, 'sweaver_temp', TRUE);
ctools_include('object-cache');
ctools_object_cache_clear('sweaver-styling', 'sweaver-styling');
}
public function sweaver_menu_callback() {
$form = array();
$form['sweaver_styles_delete_tab'] = array(
'#type' => 'checkbox',
'#title' => t('Show delete tab'),
'#description' => t('Show the delete tab in the frontend editor.'),
'#default_value' => variable_get('sweaver_styles_delete_tab', FALSE),
);
$form['sweaver_styles_autosave'] = array(
'#title' => t('Autosave'),
'#type' => 'select',
'#options' => array(
0 => t('Never'),
5 => t('Every 5 seconds'),
10 => t('Every 10 seconds'),
15 => t('Every 15 seconds'),
30 => t('Every 30 seconds'),
45 => t('Every 45 seconds'),
60 => t('Every minute'),
120 => t('Every two minutes'),
),
'#default_value' => variable_get('sweaver_styles_autosave', 0),
'#description' => t('Check for changes on your style and custom CSS every x seconds. If a change has been identified, sweaver will save those settings in a temporary cache table with AJAX. So leaving a page - or even worse, a browser crash - will make sure you keep your current configuration.'),
);
$form = system_settings_form($form);
$rows = array();
$result = db_query("SELECT ssd.*, ss.active FROM {sweaver_style_draft} ssd LEFT JOIN {sweaver_style} ss on ss.style_id = ssd.style_id ORDER BY ssd.style ASC, ssd.theme ASC, ss.active DESC");
while ($style = db_fetch_object($result)) {
$row = array();
$row[] = check_plain($style->style);
$row[] = str_replace('_', ' ', check_plain($style->theme));
switch ($style->active) {
case 0:
$row[] = t('Inactive');
break;
case 1:
$row[] = t('Active');
break;
}
$operations = '';
if (!$style->active) {
$operations .= l(t('Enable'), 'admin/settings/sweaver/styles/enable/' . $style->style_id) . ' - ';
}
else {
$operations .= l(t('Disable'), 'admin/settings/sweaver/styles/disable/' . $style->style_id) . ' - ';
}
$operations .= l(t('Delete'), 'admin/settings/sweaver/styles/delete/' . $style->style_id);
$row[] = $operations;
$rows[] = $row;
}
if (empty($rows)) {
$output = '<p>' . t('No styles found.') . '</p>';
}
else {
$header = array(
t('Style'),
t('Theme'),
t('Status'),
t('Operations'),
);
$output = theme('table', $header, $rows);
}
$form['item'] = array(
'#type' => 'item',
'#value' => $output,
);
return $form;
}
public function sweaver_style_enable() {
$style_id = arg(5);
$style = db_fetch_object(db_query('SELECT * FROM {sweaver_style} WHERE style_id = %d', $style_id));
$update = 'style_id';
if (!$style) {
$style = db_fetch_object(db_query('SELECT * FROM {sweaver_style_draft} WHERE style_id = %d', $style_id));
if (!$style) {
drupal_not_found();
exit;
}
$path = file_create_path('sweaver');
$filenamedraft = $path . '/' . 'sweaver_' . $style->theme . '_' . $style->style_id . '_draft.css';
$filenamelive = $path . '/' . 'sweaver_' . $style->theme . '_' . $style->style_id . '_live.css';
file_copy($filenamedraft, $filenamelive);
$update = array();
}
$style->active = 1;
db_query('UPDATE {sweaver_style} set active = 0 WHERE theme = "%s"', $style->theme);
drupal_write_record('sweaver_style', $style, $update);
$this
->sweaver_export_file($form_state['values']['css_rendered'], 'live');
drupal_set_message(t('Style enabled'));
drupal_goto('admin/settings/sweaver/styles');
}
public function sweaver_style_disable() {
$style_id = arg(5);
db_query('UPDATE {sweaver_style} set active = 0 WHERE style_id = %d', $style_id);
drupal_set_message(t('Style disabled'));
drupal_goto('admin/settings/sweaver/styles');
}
public function sweaver_style_confirm_form_delete() {
$style_id = arg(5);
$style = db_fetch_object(db_query('SELECT * FROM {sweaver_style_draft} WHERE style_id = %d', $style_id));
if ($style->style_id) {
$form['#style'] = $style;
return confirm_form($form, t('Are you sure you want to delete style %style?', array(
'%style' => $style->style,
)), 'admin/settings/sweaver/styles');
}
else {
drupal_set_message(t('Style not found.'));
drupal_goto('admin/settings/sweaver/styles');
}
}
public function sweaver_style_confirm_form_delete_submit($form, &$form_state) {
$style = $form['#style'];
$this
->sweaver_delete_style($style);
sweaver_clear_cache();
drupal_set_message(t('Style %style has been removed', array(
'%style' => $form['#style']->style,
)));
$form_state['redirect'] = 'admin/settings/sweaver/styles';
}
public function sweaver_autosave() {
$GLOBALS['conf']['cache'] = FALSE;
$GLOBALS['devel_shutdown'] = FALSE;
ctools_include('object-cache');
$style = new stdClass();
$style->style_id = 0;
$style->style = t('Temporary');
$style->css = $_POST['css'];
$style->customcss = $_POST['customcss'];
$style->palette = $_POST['palette'];
$sweaver = Sweaver::get_instance();
$working_style = $sweaver
->get_current_style();
if (isset($working_style->themesettings)) {
$style->theme = $working_style->theme;
$style->themesettings = $working_style->themesettings;
}
ctools_object_cache_set('sweaver-styling', 'sweaver-styling', $style);
sweaver_session(TRUE, 'sweaver_temp');
exit(drupal_json(array(
'error' => 0,
)));
}
}