Projet

Général

Profil

Paste
Télécharger (23,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / sweaver / plugins / sweaver_plugin_styles / sweaver_plugin_styles.inc @ 13755f8d

1
<?php
2

    
3
/**
4
 * @file
5
 * Styles plugin.
6
 */
7

    
8
define('SWEAVER_STYLE_SAVE', 0);
9
define('SWEAVER_STYLE_SAVE_PUBLISH', 1);
10
define('SWEAVER_STYLE_PUBLISH', 2);
11

    
12
class sweaver_plugin_styles extends sweaver_plugin {
13

    
14
  /**
15
   * Menu registry.
16
   */
17
  public function sweaver_menu(&$weight, $page_arguments, $base) {
18

    
19
    $items = array();
20
    $page_arguments = array(
21
      'plugin' => 'sweaver_plugin_styles',
22
    );
23

    
24
    // Styles administration.
25
    $items['admin/config/user-interface/sweaver/styles'] = $base + array(
26
      'title' => 'Styles',
27
      'page arguments' => array($page_arguments),
28
      'type' => MENU_LOCAL_TASK,
29
      'weight' => $weight++,
30
    );
31

    
32
    // Delete a style.
33
    $page_arguments['callback_method'] = 'sweaver_style_confirm_form_delete';
34
    $items['admin/config/user-interface/sweaver/styles/delete'] = $base + array(
35
      'title' => 'Delete style',
36
      'page arguments' => array($page_arguments),
37
      'type' => MENU_CALLBACK,
38
    );
39

    
40
    // Autosave callback.
41
    $base['access arguments'] = array('use editor');
42
    $page_arguments['callback_method'] = 'sweaver_autosave';
43
    $items['sweaver-autosave'] = $base + array(
44
      'title' => 'Autosave',
45
      'page arguments' => array($page_arguments),
46
      'type' => MENU_CALLBACK,
47
    );
48

    
49
    return $items;
50
  }
51

    
52
  /**
53
   * Frontend form: add styles form.
54
   */
55
  public function sweaver_form() {
56
    $form = array();
57
    $form['#popups'] = array();
58

    
59
    // Current style.
60
    $sweaver = Sweaver::get_instance();
61
    $current_style = $sweaver->get_current_style();
62

    
63
    // Saved styles in database.
64
    $existing_styles = FALSE;
65
    $existing_styles_options = array();
66
    // Draft versions.
67
    $drafts = db_query("SELECT style_id, style FROM {sweaver_style_draft} where theme = :theme", array(':theme' => $sweaver->get_theme_key()))->fetchAll();
68
    foreach ($drafts as $draft) {
69
      $existing_styles_options[$draft->style_id] = $draft->style;
70
    }
71
    if (count($existing_styles_options) > 0) {
72
      $existing_styles = TRUE;
73
    }
74
    // Live versions.
75
    $lives = db_query("SELECT style_id, style FROM {sweaver_style} where theme = :theme AND active = 1", array(':theme' =>  $sweaver->get_theme_key()))->fetchAll();
76
    foreach ($lives as $live) {
77
      $existing_styles_options[$live->style_id] = $live->style;
78
    }
79

    
80
    // Save popup.
81
    $form['save_style_popup']['save_style'] = array(
82
      '#type' => 'textfield',
83
      '#size' => 50,
84
      '#weight' => 1,
85
    );
86
    if ($existing_styles) {
87

    
88
      // Hide new style name by default.
89
      $form['save_style_popup']['save_style']['#attributes'] = array('style' => 'display:none');
90

    
91
      $form['save_style_popup']['save_type'] = array(
92
        '#type' => 'select',
93
        '#options' => array(
94
          0 => t('New style'),
95
          1 => t('Existing style'),
96
        ),
97
        '#default_value' => 1,
98
        '#attributes' => array('class' => array('radio-style-save-type')),
99
        '#weight' => 0,
100
      );
101

    
102
      $form['save_style_popup']['style_existing_id'] = array(
103
        '#type' => 'select',
104
        '#options' => $existing_styles_options,
105
        '#default_value' => (isset($current_style->style_id)) ? $current_style->style_id : 0,
106
        '#weight' => 2,
107
      );
108
    }
109
    else {
110
      // If no existing styles, set style save type to new.
111
      $form['save_style_popup']['save_type'] = array(
112
        '#type' => 'value',
113
        '#value' => 0,
114
      );
115
      $form['save_style_popup']['style_existing_id'] = array(
116
        '#type' => 'value',
117
        '#value' => 0,
118
      );
119
    }
120

    
121
    // Save buttons for save popup.
122
    $form['save_style_popup']['save']['save_continue'] = array(
123
      '#type' => 'submit',
124
      '#value' => t('Save and continue'),
125
      '#weight' => 4,
126
    );
127
    $form['save_style_popup']['save']['save_publish'] = array(
128
      '#type' => 'submit',
129
      '#value' => t('Save and publish'),
130
      '#weight' => 5,
131
    );
132
    $form['save_style_popup']['save']['#weight'] = 10;
133
    $form['save_style_popup']['save']['#prefix'] = '<div class="save-publish-buttons clearfix">';
134
    $form['save_style_popup']['save']['#suffix'] = '</div>';
135

    
136
    // Load popup.
137
    if ($existing_styles) {
138

    
139
      $load_style_options = $existing_styles_options;
140
      // Add the active versions of a style.
141
      $active_results = db_query("SELECT style_id, style FROM {sweaver_style} WHERE active = 1")->fetchAll();
142
      foreach ($active_results as $active) {
143
        $load_style_options[$active->style_id . '_live_table'] = $active->style . ' (' . t('live') . ')';
144
      }
145
      // Fresh start.
146
      $load_style_options[0] = t('Fresh style');
147

    
148
      $form['load_style_popup']['load_style'] = array(
149
        '#type' => 'select',
150
        '#options' => $load_style_options,
151
      );
152
      $form['load_style_popup']['load_submit'] = array(
153
        '#type' => 'submit',
154
        '#value' => t('Load style'),
155
      );
156
    }
157

    
158
    // Publish popup. Make sure we don't save the temporary one.
159
    if (isset($current_style->style_id) && !empty($current_style->style_id)) {
160
      $form['publish_style_popup']['publish_style'] = array(
161
        '#markup' => t('Set style %stylename visible for your visitors. Any unsaved changes will also be saved when publishing.', array('%stylename' => $current_style->style))
162
      );
163
      $form['publish_style_popup']['publish_id'] = array(
164
        '#type' => 'hidden',
165
        '#value' => $current_style->style_id,
166
      );
167
      $form['publish_style_popup']['publish_submit'] = array(
168
        '#type' => 'submit',
169
        '#value' => t('Publish style'),
170
      );
171
    }
172

    
173
    // Delete popup.
174
    if ($existing_styles && variable_get('sweaver_styles_delete_tab', FALSE)) {
175

    
176
      // Question.
177
      $form['delete_style_popup']['question']['delete_style'] = array(
178
        '#type' => 'select',
179
        '#options' => $existing_styles_options,
180
      );
181
      $form['delete_style_popup']['question']['delete_confirm'] = array(
182
        '#type' => 'button',
183
        '#value' => t('Delete'),
184
      );
185
      $form['delete_style_popup']['question']['#prefix'] = '<div class="delete-style-confirm">';
186
      $form['delete_style_popup']['question']['#suffix'] = '</div>';
187

    
188
      // Confirmation.
189
      $form['delete_style_popup']['warning']['delete_submit'] = array(
190
        '#type' => 'submit',
191
        '#value' => t('Delete style'),
192
      );
193
      $form['delete_style_popup']['warning']['delete_cancel'] = array(
194
        '#type' => 'button',
195
        '#value' => t('Cancel'),
196
      );
197
      $form['delete_style_popup']['warning']['#prefix'] = '<div class="delete-style-question clearfix" style="display:none">';
198
      $form['delete_style_popup']['warning']['#prefix'] .= '<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>';
199

    
200
      $form['delete_style_popup']['warning']['#suffix'] = '</div>';
201
    }
202

    
203
    // Build the popup links & content.
204
    $i = 0;
205
    $form['#popups_links'] = '';
206
    $form['#popups_styles'] = array(
207
      'save_style_popup' => array(
208
        'title' => t('Save'),
209
        'description' => 'Save and keep working on your style. You can also publish it immediately for your visitors.',
210
      ),
211
      'load_style_popup' => array(
212
        'title' => t('Load'),
213
        'description' => t('Load a style to continue working on it. It will only be visible for your visitors after publishing.'),
214
      ),
215
      'publish_style_popup' => array(
216
        'title' => t('Publish'),
217
        'description' => '',
218
      ),
219
      'delete_style_popup' => array(
220
        'title' => t('Delete'),
221
        'description' => t('Delete a style which you do not want to use anymore.')
222
      ),
223
    );
224
    foreach ($form['#popups_styles'] as $key => $action) {
225
      if (isset($form[$key])) {
226
        ++$i;
227
        $form['#popups'][] = $key;
228
        $form['#popups_links'] .= '<div class="style-actions-link"><a href="#" id="style-actions-link-'. $i .'">'. $action['title'] .'</a></div>';
229
        $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>';
230
        $form[$key]['#suffix'] = '</div>';
231
      }
232
    }
233

    
234
    return $form;
235
  }
236

    
237
  /**
238
   * Frontend form render.
239
   */
240
  public function sweaver_form_render(&$vars, &$form, $plugin) {
241
    $vars['style_actions'] = $form['sweaver_plugin_styles']['form']['#popups_links'];
242
    $this->sweaver_popups_render($vars, $form, $plugin['name']);
243
  }
244

    
245
  /**
246
   * Frontend css and js.
247
   */
248
  public function sweaver_form_css_js(&$inline_settings) {
249
    if (variable_get('sweaver_styles_autosave', 0) > 0) {
250
      $inline_settings['sweaver']['autosave'] = variable_get('sweaver_styles_autosave', 0);
251
    }
252
    drupal_add_js(drupal_get_path('module', 'sweaver') .'/plugins/sweaver_plugin_styles/sweaver_plugin_styles.js');
253
  }
254

    
255
  /**
256
   * Frontend form submit.
257
   */
258
  public function sweaver_form_submit($form, &$form_state) {
259
      
260
    $sweaver_directory = 'public://sweaver';
261
    file_prepare_directory($sweaver_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
262

    
263
    $styles_form = $form['sweaver_plugin_styles']['form'];
264
    $clicked_button = $form_state['clicked_button']['#value'];
265

    
266
    // Save style.
267
    if ($clicked_button == t('Save and continue') || $clicked_button == t('Save and publish') || $clicked_button == t('Publish style')) {
268

    
269
      // Reset session.
270
      $this->sweaver_reset_style_session();
271

    
272
      $update = array();
273
      $style = new stdClass;
274
      $theme_key = $form['#current_theme'];
275
      $form_state['publish'] = TRUE;
276

    
277
      // Submit type.
278
      if ($clicked_button == t('Save and publish')) {
279
        $submit_type = SWEAVER_STYLE_SAVE_PUBLISH;
280
      }
281
      elseif ($clicked_button == t('Publish style')) {
282
        $submit_type = SWEAVER_STYLE_PUBLISH;
283
      }
284
      else {
285
        $form_state['publish'] = FALSE;
286
        $submit_type = SWEAVER_STYLE_SAVE;
287
      }
288

    
289
      // Create variables from form_state, easier to re-use them in the flow.
290
      $style_name = $form_state['values']['save_style'];
291
      $keep_working = ($submit_type == SWEAVER_STYLE_PUBLISH) ? FALSE : TRUE;
292
      $save_type = ($submit_type == SWEAVER_STYLE_PUBLISH) ? TRUE : $form_state['values']['save_type'];
293
      $style_existing_id = ($submit_type == SWEAVER_STYLE_PUBLISH) ? $form_state['values']['publish_id'] : $form_state['values']['style_existing_id'];
294

    
295
      if ($save_type) {
296
        $update = array('style_id');
297
        $style->style_id = $style_existing_id;
298
        $style->style = $styles_form['save_style_popup']['style_existing_id']['#options'][$style->style_id];
299
      }
300
      else {
301
        $style->style = $style_name;
302
        if (empty($style->style)) {
303
          $style->style = t('Undefined');
304
        }
305
        
306
        // Check if this name is already used
307
        $styles = db_select('sweaver_style_draft', 's')
308
          ->fields('s', array('style'))
309
          ->execute()
310
          ->fetchAll();       
311
        $i = '';
312
        do {
313
          $exist = false;
314
          if ($i == '') {
315
            $name = $style->style;
316
          }
317
          else {
318
            $name = $style->style.' '.$i;
319
          }
320
          foreach($styles as $db_style){
321
            if ($name == $db_style->style)
322
              $exist = TRUE;
323
          }
324
          $i = ($i == '') ? 2 : $i + 1;
325
        } while ($exist);
326
        
327
        $style->style = $name;        
328
      }
329

    
330
      // Always save the draft version.
331
      $style->theme = $theme_key;
332
      
333
      $sweaver = Sweaver::get_instance();
334
      $working_style = $sweaver->get_current_style();
335
      
336
      $sweaver_css = isset($form_state['values']['sweaver-css']) ? $form_state['values']['sweaver-css'] : $working_style->css;
337
      $css_rendered = isset($form_state['values']['css-rendered']) ? $form_state['values']['css-rendered'] : '';
338
      
339
      $style->css = $sweaver_css;
340
      drupal_write_record('sweaver_style_draft', $style, $update);
341
      $this->sweaver_export_file($css_rendered, $style);
342
      $form_state['style_id'] = $style->style_id;
343

    
344
      // Publish style.
345
      if ($submit_type == SWEAVER_STYLE_PUBLISH || $submit_type == SWEAVER_STYLE_SAVE_PUBLISH) {
346
        $style->active = 1;
347
        $this->sweaver_export_file($css_rendered, $style, 'live');
348
        $message = 'The style @style has been published.';
349
        db_query("UPDATE {sweaver_style} set active = 0 WHERE theme = :theme", array(':theme' => $style->theme));
350
        // Find out first if this style already exists or not.
351
        if (db_query("SELECT style_id FROM {sweaver_style} WHERE style_id = :style_id", array(':style_id' => $style->style_id))->fetchField() === FALSE) {
352
          $update = array();
353
        }
354
        drupal_write_record('sweaver_style', $style, $update);
355
      }
356

    
357
      // Draft only save.
358
      if ($submit_type != SWEAVER_STYLE_PUBLISH) {
359
        sweaver_session(TRUE, 'draft_mode');
360
        sweaver_session($style->style_id, 'loaded_style');
361
        sweaver_session('draft', 'loaded_table');
362
        $message = 'The style @style has been saved. You can keep working on your style.';
363
      }
364

    
365
      // Draft & published save.
366
      if ($submit_type == SWEAVER_STYLE_SAVE_PUBLISH)  {
367
        $message = 'The style @style has been saved and published. You can keep working on your style.';
368
      }
369

    
370
      sweaver_session(t($message, array('@style' => $style->style)));
371
    }
372

    
373
    // Load style.
374
    if ($clicked_button == t('Load style')) {
375
      // Reset session.
376
      $this->sweaver_reset_style_session();
377

    
378
      // Build message and session variables.
379
      $theme_key = $form['#current_theme'];
380
      $style_id = $form_state['values']['load_style'];
381
      $style_name = $styles_form['load_style_popup']['load_style']['#options'][$style_id];
382

    
383
      // Load from draft or live table ?
384
      $table = 'draft';
385
      $pos = strpos($style_id, '_live_table');
386
      if ($pos !== FALSE) {
387
        $table = 'live';
388
        $style_id = str_replace('_live_table', '', $style_id);
389
      }
390

    
391
      sweaver_session(TRUE, 'draft_mode');
392
      sweaver_session($style_id, 'loaded_style');
393
      sweaver_session($table, 'loaded_table');
394
      sweaver_session(t('The style @style has been loaded. It is only visible for you.', array('@style' => $style_name)));
395
    }
396

    
397
    // Delete style.
398
    if ($clicked_button == t('Delete style')) {
399

    
400
      // Reset session.
401
      $this->sweaver_reset_style_session();
402

    
403
      // Get info from db and store in form_state so other modules can profit from it.
404
      $style_id = $form_state['values']['delete_style'];
405
      $style = db_query("SELECT * FROM {sweaver_style_draft} WHERE style_id = :style_id", array(':style_id' => $style_id))->fetchObject();
406
      $style_live = db_query("SELECT * FROM {sweaver_style} WHERE style_id = :style_id", array(':style_id' => $style_id))->fetchObject();
407
      $form_state['style_active'] = isset($style_live->active) ? $style_live->active : FALSE;
408
      $form_state['style_to_delete'] = $style;
409

    
410
      // Delete entries from tables, files.
411
      $this->sweaver_delete_style($style);
412

    
413
      // Message.
414
      $style_name = $styles_form['delete_style_popup']['question']['delete_style']['#options'][$style_id];
415
      sweaver_session(t('The style @style has been deleted.', array('@style' => $style_name)));
416
    }
417

    
418
    // Clear page & block cache and page requisites.
419
    sweaver_clear_cache();
420
  }
421

    
422
  /**
423
   * Export css to file.
424
   */
425
  public function sweaver_export_file($css, $style, $type = 'draft') {
426
    $css = str_replace('<style type="text/css">', '', $css);
427
    $css = str_replace('</style>', '', $css);
428
    $filename = 'sweaver_' . $style->theme . '_'. $style->style_id .'_'. $type .'.css';
429

    
430
    // Create the css within the files folder.
431
    $sweaver_directory = 'public://sweaver';
432
    file_prepare_directory($sweaver_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
433
    $file = $sweaver_directory . '/' . $filename;
434
    if (!$fp = fopen($file, 'w')) {
435
      sweaver_session(t('The css file could not be created.'));
436
    }
437
    else {
438
      // Clean the css before writing it.
439
      $css = $this->sweaver_ctools_css_filter($css, FALSE);
440
      fwrite($fp, $css);
441
    }
442
    @fclose($fp);
443
  }
444

    
445
  /**
446
   * Filter a chunk of CSS text. Based on CTools, but we want the
447
   * regex for url out. This might become a setting too later.
448
   */
449
  function sweaver_ctools_css_filter($css, $compressed = TRUE) {
450
    ctools_include('css');
451
    $css_data = ctools_css_disassemble($css);
452

    
453
    $empty_array = array();
454
    $allowed_properties = $this->sweaver_ctools_allowed_properties();
455
    $disallowed_values_regex = '/(expression)/';
456
    $filtered = ctools_css_filter_css_data($css_data, $allowed_properties, $empty_array, '', $disallowed_values_regex);
457

    
458
    return $compressed ? ctools_css_compress($filtered) : ctools_css_assemble($filtered);
459
  }
460

    
461
  /**
462
   * Add more allowed properties.
463
   */
464
  function sweaver_ctools_allowed_properties() {
465
    $allowed = ctools_css_filter_default_allowed_properties();
466
    $allowed_properties = array();
467
    $exploded = explode("\n", variable_get('sweaver_ctools_allowed_properties', SWEAVER_CTOOLS_ALLOWED_PROPERTIES));
468
    foreach ($exploded as $key => $class) {
469
      $trimmed = trim($class);
470
      if (!empty($trimmed)) {
471
        $allowed_properties[] = $trimmed;
472
      }
473
    }
474

    
475
    return array_merge($allowed, $allowed_properties);
476
  }
477

    
478
  /**
479
   * Delete a complete style.
480
   */
481
  public function sweaver_delete_style($style) {
482
    db_query("DELETE FROM {sweaver_style} WHERE style_id = :style_id", array(':style_id' => $style->style_id));
483
    db_query("DELETE FROM {sweaver_style_draft} WHERE style_id = :style_id", array(':style_id' => $style->style_id));
484
    $draft = 'public://sweaver/sweaver_' . $style->theme . '_' . $style->style_id . '_draft.css';
485
    $live = 'public://sweaver/sweaver_' . $style->theme . '_' . $style->style_id . '_live.css';
486
    file_unmanaged_delete($draft);
487
    file_unmanaged_delete($live);
488

    
489
    // Remove files tied to theme.
490
    $dir = 'public://sweaver';
491
    $mask = '/(.*)' . $style->theme . '_' . $style->style_id . '_(.*)/';
492
    $files = file_scan_directory($dir, $mask);
493
    // Get temp files.
494
    $mask = '/(.*)'. $style->theme .'_temp_(.*)/';
495
    $files += file_scan_directory($dir, $mask);
496
    foreach ($files as $key => $file) {
497
      file_unmanaged_delete($file->uri);
498
    }
499
  }
500

    
501
  /**
502
   * Reset style session.
503
   */
504
  public function sweaver_reset_style_session() {
505
    sweaver_session(NULL, 'loaded_table', TRUE);
506
    sweaver_session(NULL, 'draft_mode', TRUE);
507
    sweaver_session(NULL, 'loaded_style', TRUE);
508
    sweaver_session(NULL, 'sweaver_temp', TRUE);
509
    ctools_include('object-cache');
510
    ctools_object_cache_clear('sweaver-styling', 'sweaver-styling');
511
  }
512

    
513
  /**
514
   * Menu callback
515
   */
516
  public function sweaver_menu_callback() {
517
    $form = array();
518

    
519
    // Settings.
520
    $form['sweaver_styles_delete_tab'] = array(
521
      '#type' => 'checkbox',
522
      '#title' => t('Show delete tab'),
523
      '#description' => t('Show the delete tab in the frontend editor.'),
524
      '#default_value' => variable_get('sweaver_styles_delete_tab', FALSE),
525
    );
526
    $form['sweaver_styles_autosave'] = array(
527
      '#title' => t('Autosave'),
528
      '#type' => 'select',
529
      '#options' => array(
530
        0 => t('Never'),
531
        5 => t('Every 5 seconds'),
532
        10 => t('Every 10 seconds'),
533
        15 => t('Every 15 seconds'),
534
        30 => t('Every 30 seconds'),
535
        45 => t('Every 45 seconds'),
536
        60 => t('Every minute'),
537
        120 => t('Every two minutes'),
538
      ),
539
      '#default_value' => variable_get('sweaver_styles_autosave', 0),
540
      '#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.'),
541
    );
542
    $form = system_settings_form($form);
543

    
544
    // Styles list.
545
    $rows = array();
546
    $styles = 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")->fetchAll();
547
    foreach ($styles as $style) {
548
      $row = array();
549
      $row[] = check_plain($style->style);
550
      $row[] = str_replace('_', ' ', check_plain($style->theme));
551
      $row[] = ($style->active) ? t('active') : t('inactive');
552
      $operations = l(t('Delete'), 'admin/config/user-interface/sweaver/styles/delete/'. $style->style_id);
553
      $row[] = $operations;
554
      $rows[] = $row;
555
    }
556

    
557
    if (empty($rows)) {
558
      $output = '<p>' . t('No styles found.') . '</p>';
559
    }
560
    else {
561

    
562
      $header = array(
563
        t('Style'),
564
        t('Theme'),
565
        t('Status'),
566
        t('Operations'),
567
      );
568

    
569
      $variables = array(
570
        'header' => $header,
571
        'rows' => $rows,
572
      );
573

    
574
      // Styles list.
575
      $output = theme('table', $variables);
576

    
577
    }
578

    
579
    $form['item'] = array(
580
      '#markup' => $output,
581
    );
582

    
583
    return $form;
584
  }
585

    
586
  /**
587
   * Menu callback, delete style.
588
   */
589
  public function sweaver_style_confirm_form_delete() {
590
    $style_id = arg(6);
591
    $style = db_query('SELECT * FROM {sweaver_style_draft} WHERE style_id = :style_id', array(':style_id' => $style_id))->fetchObject();
592
    if ($style->style_id) {
593
      $form['#style'] = $style;
594
      return confirm_form($form, t('Are you sure you want to delete style %style?', array('%style' => $style->style)), 'admin/settings/sweaver/styles');
595
    }
596
    else {
597
      drupal_set_message(t('Style not found.'));
598
      drupal_goto('admin/config/user-interface/sweaver/styles');
599
    }
600
  }
601

    
602
  /**
603
   * Submit callback, delete style.
604
   */
605
  public function sweaver_style_confirm_form_delete_submit($form, &$form_state) {
606
    $style = $form['#style'];
607
    $this->sweaver_delete_style($style);
608
    sweaver_clear_cache();
609
    drupal_set_message(t('Style %style has been removed', array('%style' => $form['#style']->style)));
610
    $form_state['redirect'] = 'admin/config/user-interface/sweaver/styles';
611
  }
612

    
613
  /**
614
   * Autosave post.
615
   */
616
  public function sweaver_autosave() {
617
    // Prevent caching of JS output.
618
    $GLOBALS['conf']['cache'] = FALSE;
619
    // Prevent Devel from hi-jacking our output in any case.
620
    $GLOBALS['devel_shutdown'] = FALSE;
621
    // Load CTools object cache.
622
    ctools_include('object-cache');
623
    
624
    $sweaver = Sweaver::get_instance();
625
    $working_style = $sweaver->get_current_style();
626
        
627
    // Save current styling.
628
    $style = new stdClass;
629
    $style->style_id = 0;
630
    $style->style = t('Temporary');
631
    $style->css = isset($_POST['css']) ? $_POST['css'] : $working_style->css;
632
    $style->customcss = isset($_POST['customcss']) ? $_POST['customcss'] : $working_style->customcss;
633
    $style->palette = isset($_POST['palette']) ? $_POST['palette'] : $working_style->palette;
634
    
635
    // Get the themesettings if applicable and overwrite style id & style name.
636
    if (isset($working_style->themesettings)) {
637
      $style->theme = $working_style->theme;
638
      $style->themesettings = $working_style->themesettings;
639
    }
640

    
641
    // An image might have been uploaded by a managed_file field, permanant status needs to be set up 
642
    if (!empty($_POST['managed_file_fid'])) {
643
      $fid = filter_input(INPUT_POST, 'managed_file_fid', FILTER_SANITIZE_NUMBER_INT);
644
      if ($fid > 0){
645
        $file = file_load($fid);
646
        $file->status = FILE_STATUS_PERMANENT;
647
        $file->description = $file->filename;
648
        file_save($file);
649
        drupal_write_record('sweaver_image', $file);
650
      }
651
    }
652

    
653
    // Save to CTools object cache.
654
    ctools_object_cache_set('sweaver-styling', 'sweaver-styling', $style);
655

    
656
    // Set session variable.
657
    sweaver_session(TRUE, 'sweaver_temp');
658

    
659
    // Exit.
660
    exit(drupal_json_encode(array('error' => 0)));
661
  }
662
}
663