summaryrefslogtreecommitdiffstats
path: root/sigmaone/vtcore/plugins/regionarea/regionarea.admin
blob: 407df041582be671ce27fe021f77a5904f63cb40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php

/**
 * Implement hook_vtcore_settings()
 *
 * Notice that in this form, although
 * we can use #theme but we opt not to
 * because using #theme with theme registered
 * to this theme only will not work if
 * user change the admin theme to another theme
 *
 * Notice that we load all includes file by invoking
 * vtcore_form_load_includes, that function will also
 * register the file path so Drupal will reload it
 * during ajax callback or form rebuild
 *
 * Notice that we use value in form state for the theme_key
 * instead of grabbing one from $global.
 */
function regionarea_vtcore_settings_alter_process(&$form, &$form_state) {

  // Serve the correct theme on ajax request
  if (arg(0) != 'system' && arg(1) != 'ajax') {
    $admin_theme = variable_get('admin_theme', '0');
    $settings = array(
    	'adminTheme' => array(
    		'theme' => $admin_theme,
        'theme_token' => drupal_get_token($admin_theme),
      ),
    );
    drupal_add_js($settings, 'setting');
  }

  // Don't rely on global $theme_key
  // Use one in form state instead
  $theme_key = $form_state['build_info']['args'][0];
  $theme_path = $form_state['build_info']['theme']['theme_path'];
  $base_theme_path = $form_state['build_info']['theme']['base_theme_path'];

  // Load all includes file and record it in $form_state['build_info']
  // So Drupal will reload them during ajax process or form rebuild
  $files = array(
    $base_theme_path . '/vtcore/plugins/regionarea/regionarea.admin',
    $base_theme_path . '/vtcore/plugins/regionarea/inc/helper_function.inc.php',
    $base_theme_path . '/vtcore/plugins/regionarea/inc/subform.inc.php',
  );

  foreach ($files as $file) {
    vtcore_form_load_includes($file, $form, $form_state);
  }

  // User area specific settings
  $form['region'] = array(
    '#type' => 'fieldset',
    '#title' => t('Region Manager'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#weight' => -19,
    '#group' => 'theme_core',
    '#attached' => array(
     'library' => array(
        array('system', 'ui.sortable'),
        array('system', 'ui.core')),
     'js' => array(
        $base_theme_path . '/vtcore/plugins/regionarea/js/regionarea.js',
        $base_theme_path . '/vtcore/plugins/regionarea/js/regionareaPlugin.js',
        ),
  		'css' => array($base_theme_path . '/vtcore/plugins/regionarea/css/regionarea.css'),
    ),
    '#tree' => FALSE,
  );

  // Build container for configurator
  $form['region']['configurator'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'regionarea-configurator',
    ),
  );

  // Give user select box if not on create new template mode
  $options =  array(
  	'' => '-- ' . t('Select') . ' --',
  	'create_new' => t('Create New Layout'),
  );

  $options += _regionarea_get_all_layout_files($theme_key);
  $options += _regionarea_get_all_layout_database($theme_key);

  // @easter_egg
  // You can set default layout when loading here
  // $build_mode = 'create_new';
  $build_mode = '';
  if (isset($form_state['values'])) {
    $build_mode = vtcore_get_default_value('layout_select', $form_state['values'], '');
  }

  $form['region']['configurator']['layout_select'] = array(
    '#type' => 'select',
    '#title' => t('Select layout file to process'),
    '#options' => $options,
    '#default_value' => $build_mode,
  );

  $form['region']['configurator']['layout_delete'] = array(
    '#type' => 'submit',
    '#name' => 'delete',
    '#value' => t('Delete'),
    '#submit' => array('regionarea_layout_delete_submit'),
    '#weight' => 19,
  );

  $form['region']['configurator']['layout_process'] = array(
    '#type' => 'submit',
    '#name' => 'process',
    '#value' => t('Process'),
    '#submit' => array('regionarea_layout_process_submit'),
    '#weight' => 19,
    '#ajax' => array(
      'callback' => 'regionarea_workarea_ajax_refresh',
      'wrapper' => 'regionarea-workarea',
      'method' => 'replace',
      'effect' => 'slide',
      'path' => 'system/ajax/theme-' . $theme_key,
      'ajax_page_state' => BASE_THEME,
    ),
  );

  // Build container for workspace
  $form['region']['workarea'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'regionarea-workarea',
    ),
  );

  // Don't process any further if user hasn't select the correct configuration
  if (empty($build_mode)) {
    return $form;
  }

  // Container for settings
  $form['region']['workarea']['settings'] = array(
    '#type' => 'container',
    '#attributes' => array(
    	'id' => 'workarea-settings',
    ),
  );

  // Configuration for this layout
  if ($build_mode == 'create_new') {
    $form['region']['workarea']['settings']['layout_new'] = array(
      '#type' => 'textfield',
      '#title' => t('New layout filename'),
      '#default_value' => '',
      '#field_suffix' => '.layout',
      '#description' => t('Enter valid default layout filename, such as page--node--1 for url node/1'),
    );
  }

  $form['region']['workarea']['settings']['layout_storing'] = array(
    '#type' => 'select',
    '#title' => t('Layout file store mode'),
    '#options' => array(
    	'database' => t('Store in database, variable table'),
    	'theme' => t('Store as layout file in !theme layout theme dynamic directory', array('!theme' => $theme_key)),
      'plugin' => t('Store as layout file in region area plugin layouts directory'),
    ),
  );

  // Build layout_data and layout_data
  $layout_data = _regionarea_build_layouts_array($build_mode, $theme_key);

  // Quit on empty $layout_data
  if (empty($layout_data)) {
    $form['region']['workarea']['empty'] = array(
      '#markup' => '<div class="empty-layout">' . t('Failed to retrieve valid layout structure.') . '</div>',
    );

    return $form;
  }

  // Build place holder for drag and drop
  // Enabled Areas Container
  $form['region']['workarea']['enabled_area'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'target--enabled--area',
      'class' => array(
        'drag-area-target',
        'enabled-area',
        'layout-parent',
      ),
    ),
  );

  $form['region']['workarea']['enabled_area']['element_title'] = array(
    '#type' => 'markup',
    '#markup' => '<h3 class="region-title">' . t('Workspace') . '</h3>',
  );

  $form['region']['workarea']['enabled_area']['empty'] = array(
    '#type' => 'markup',
    '#markup' => '<p class="emptyslot">' . t('There is no enabled areas available') . '</p>',
  );

  // Disabled Areas Container
  $form['region']['workarea']['disabled_area'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'target--disabled--area',
      'class' => array(
        'drag-area-target',
        'disabled-area',
        'layout-parent',
      ),
    ),
  );

  $form['region']['workarea']['disabled_area']['element_title'] = array(
    '#type' => 'markup',
    '#markup' => '<h3 class="region-title">' . t('Disabled Areas') . '</h3>',
  );

  $form['region']['workarea']['disabled_area']['empty'] = array(
    '#type' => 'markup',
    '#markup' => '<p class="emptyslot">' . t('There is no disabled areas available') . '</p>',
  );

  // Disabled Region Container
  $form['region']['workarea']['disabled_region'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'target--disabled--region',
      'class' => array(
        'drag-region-target',
        'disabled-region',
        'layout-parent',
      ),
    ),
  );

  $form['region']['workarea']['disabled_region']['element_title'] = array(
    '#type' => 'markup',
    '#markup' => '<h3 class="region-title">' . t('Disabled Regions') . '</h3>',
  );

  $form['region']['workarea']['disabled_region']['empty'] = array(
    '#type' => 'markup',
    '#markup' => '<p class="emptyslot">' . t('There is no disabled regions available') . '</p>',
  );

  // Disabled Block Container
  $form['region']['workarea']['disabled_block'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'target--disabled--block',
      'class' => array(
        'drag-block-target',
        'disabled-block',
        'layout-parent',
      ),
    ),
  );

  $form['region']['workarea']['disabled_block']['element_title'] = array(
    '#type' => 'markup',
    '#markup' => '<h3 class="region-title">' . t('Disabled Blocks') . '</h3>',
  );

  $form['region']['workarea']['disabled_block']['empty'] = array(
    '#type' => 'markup',
    '#markup' => '<p class="emptyslot">' . t('There is no disabled blocks available') . '</p>',
  );

  // Build all available tags
  $available_tags = array('div' => 'div');
  vtcore_alter_process('regionarea_available_tags', $available_tags);

  // Build the areas from the layout_data first.
  foreach ($layout_data['area'] as $area => $area_data) {
    // Skip if no #enabled key, most likely we got
    // broken .layout entry
    if (!isset($area_data['#enabled'])) {
      continue;
    }

    if ($area_data['#enabled'] == 1) {
      $form['region']['workarea']['enabled_area'][$area] = _regionarea_build_area_forms($area, $area_data, $available_tags);
    }
    else {
      $form['region']['workarea']['disabled_area'][$area] = _regionarea_build_area_forms($area, $area_data, $available_tags);
    }
  }

  // Build the regions from the layout_data
  $all_area = array(
    'disabled' => t('Disabled'),
  );
  $all_area += _regionarea_build_parent_select_options($layout_data['area']);

  foreach ($layout_data['region'] as $region => $region_data) {
    if (isset($layout_data['area'][$region_data['#parent']])
        && $layout_data['area'][$region_data['#parent']]['#enabled'] == 1
        && $region_data['#parent'] != 'disabled') {
      $form['region']['workarea']['enabled_area'][$region_data['#parent']][$region] = _regionarea_build_region_forms($region, $region_data, $all_area, $available_tags);
    }
    else {
      $form['region']['workarea']['disabled_region'][$region] = _regionarea_build_region_forms($region, $region_data, $all_area, $available_tags);
    }
  }


  // Build the blocks from the layout_data
  $all_region = array(
    'disabled' => t('Disabled'),
  );

  $all_region += _regionarea_build_parent_select_options($layout_data['region']);

  foreach ($layout_data['block'] as $block => $block_data) {
    $area_parent = '';
    if ($block_data['#parent'] != 'disabled' && isset($layout_data['region'][$block_data['#parent']])) {
      $area_parent = $layout_data['region'][$block_data['#parent']]['#parent'];
    }

    if (isset($layout_data['area'][$area_parent])
        && isset($layout_data['region'][$block_data['#parent']])
        && $layout_data['area'][$area_parent]['#enabled'] == 1
        && $block_data['#parent'] != 'disabled') {
      $form['region']['workarea']['enabled_area'][$area_parent][$block_data['#parent']][$block] = _regionarea_build_block_forms($block, $block_data, $all_region, $block_data['#block_type']);
    }
    else {
      $form['region']['workarea']['disabled_block'][$block] = _regionarea_build_block_forms($block, $block_data, $all_region, $block_data['#block_type']);
    }
  }

  // Add custom submit function
  if (!in_array('regionarea_submit', $form['#submit'])) {
    $form['#validate'][] = 'regionarea_validation';
    $form['#submit'][] = 'regionarea_submit';
  }
}

/**
 * Submit callback function to delete the layout
 */
function regionarea_layout_delete_submit(&$form, &$form_state) {
  $theme_key = $form_state['build_info']['args'][0];
  $file = $form_state['values']['layout_select'];
  $paths = pathinfo($file);

  // Layout stored in database, clean out the database
  if (empty($paths['extension']) && $paths['dirname'] == '.' && isset($form_state['storage']['old_value']['layouts'][$paths['filename']])) {
    $stored = variable_get('theme_' . $theme_key . '_settings');
    unset($stored['layouts'][$paths['filename']]);

    variable_set('theme_' . $theme_key . '_settings', $stored);
    drupal_set_message(t('@file deleted from database.', array('@file' => $file)));
  }
  else {
    // Don't allow to delete default files and plugin specified files
    $banned = array('theme_default', 'layout', 'adminpage');
    $path = explode('/', $paths['dirname']);
    $remove = TRUE;
    foreach ($banned as $ban) {
      if (in_array($ban, $path)) {
        $remove = FALSE;
      }
    }

    if ($remove) {
      $delete = file_unmanaged_delete($file);
      if ($delete != FALSE) {
        drupal_set_message(t('@file deleted.', array('@file' => $file)));
      }
    }
    else {
      drupal_set_message(t('@file cannot be deleted.', array('@file' => $file)));
    }
  }
  // Unset these so form will destroy workarea
  unset($form_state['values']['layout_select']);
  unset($form_state['values']['layout_delete']);
  unset($form_state['values']['layout_process']);

  $form_state['submitted'] = TRUE;
  $form_state['rebuild'] = TRUE;
}

/**
 * Submit callback function to build the form
 * based on selected layout
 */
function regionarea_layout_process_submit(&$form, &$form_state) {
  $form_state['storage']['old_value']['layout_select'] = $form_state['values']['layout_select'];
  $form_state['submitted'] = FALSE;
  $form_state['rebuild'] = TRUE;
}

/**
 * Validation function for regionarea submit
 */
function regionarea_validation(&$form, &$form_state) {
  // Give error on empty filename when in create
  // new layout mode
  if ($form_state['values']['layout_select'] == 'create_new'
      && empty($form_state['values']['layout_new'])
      && isset($form['region']['workarea']['settings']['layout_new'])) {
    form_set_error('layout_new', t('Filename required'));
  }

  // Give error on no layout at all
  $area_enabled = FALSE;
  foreach ($form_state['values']['area'] as $area => $areadata) {
    if ($areadata['enabled'] == 1) {
      $area_enabled = TRUE;
      break;
    }
  }

  if ($area_enabled == FALSE) {
    form_set_error('workarea', t('At least 1 enabled area required for a valid layout files.'));
  }

}

/**
 * Main submit function to actually save the
 * layout arrays either as database or as
 * file
 */
function regionarea_submit(&$form, &$form_state) {
  global $vtcore;

  $theme_key = $form_state['build_info']['args'][0];
  $theme_path = $form_state['build_info']['theme']['theme_path'];
  $base_theme_path = $form_state['build_info']['theme']['base_theme_path'];

  $data = array();
  // Move data out from $form_state;
  _regionarea_build_data_for_saving('area', $data, $form_state);
  _regionarea_build_data_for_saving('region', $data, $form_state);
  _regionarea_build_data_for_saving('block', $data, $form_state);

  // Let other plugin connect with this form
  vtcore_alter_process('regionarea_vtcore_build_data_for_saving', $data, $form_state);

  $storing = $form_state['values']['layout_storing'];
  $file = $form_state['values']['layout_select'];
  if (!empty($form_state['values']['layout_new'])) {
    $file = $form_state['values']['layout_new'];
  }

  if ($storing == 'database') {
    // Remove file path from $file
    $file = basename($file);
    $file = str_replace('.layout', '', $file);
    $form_state['values']['layouts'][$file] = $data;

    // Bug fix http://drupal.org/node/1883460
    $form_state['storage']['old_value']['layouts'][$file] = $data;

    drupal_set_message(t('@file saved to database.', array('@file' => $file)));
  }

  if ($storing == 'theme') {
    $file = basename($file);
    $file = str_replace('.layout', '', $file);
    $filepath = $theme_path . '/layouts/theme_dynamic';
    file_prepare_directory($filepath, $options = FILE_CREATE_DIRECTORY);
    _regionarea_save_layout_file($filepath, $file, $data);
  }

  if ($storing == 'plugin') {
    $file = basename($file);
    $file = str_replace('.layout', '', $file);
    $filepath = $base_theme_path . '/vtcore/plugins/regionarea/layouts';
    file_prepare_directory($filepath, $options = FILE_CREATE_DIRECTORY);
    _regionarea_save_layout_file($filepath, $file, $data);
  }

  // Do Clean up for sane form rebuilding
  unset($form_state['values']['area']);
  unset($form_state['values']['region']);
  unset($form_state['values']['block']);
  unset($form_state['values']['layout_new']);
  unset($form_state['values']['layout_storing']);
  unset($form_state['values']['layout_delete']);
  unset($form_state['values']['layout_process']);
  unset($form_state['values']['layout_select']);

  unset($form_state['storage']['old_value']['area']);
  unset($form_state['storage']['old_value']['region']);
  unset($form_state['storage']['old_value']['block']);
  unset($form_state['storage']['old_value']['layout_new']);
  unset($form_state['storage']['old_value']['layout_storing']);
  unset($form_state['storage']['old_value']['layout_delete']);
  unset($form_state['storage']['old_value']['layout_process']);
  unset($form_state['storage']['old_value']['layout_select']);

  // Clear stored cache
  if ($vtcore->cache) {
    cache_clear_all($theme_key . '--', 'cache', TRUE);
  }
}

/**
 * Ajax callback function
 */
function regionarea_configurator_ajax_refresh($form, &$form_state) {
  return $form['region']['configurator'];
}

/**
 * Ajax callback function
 */
function regionarea_workarea_ajax_refresh($form, &$form_state) {
  return $form['region']['workarea'];
}

/**
 * Implements hook_regionarea_vtcore_block_settings_alter_process().
 *
 * We implement this hook to serve the layout plugin default vtcore-block blocks
 */
function regionarea_regionarea_vtcore_block_settings_alter_process(&$form, $block, $block_data, $all_regions = FALSE) {
  // Skip if not vtcore block
  if ($block_data['#block_type'] != 'vtcore-block') {
    return;
  }
  switch ($block) {
    // Additional form setting for logo
    case 'logo' :
      $form['block'][$block]['site_title_enable'] = array(
        '#type' => 'checkbox',
        '#title' => t('Custom Site Title'),
        '#default_value' => vtcore_get_default_value('#site_title_enable', $block_data, 1),
      );

      $form['block'][$block]['site_title'] = array(
        '#type' => 'textfield',
        '#default_value' => vtcore_get_default_value('#site_title', $block_data, ''),
        '#description' => t('Enabling the custom site title by checking the checkbox above will override Drupal standard site name settings as found in admin/config/system/site-information'),
      );

      $form['block'][$block]['site_slogan_enable'] = array(
        '#type' => 'checkbox',
        '#title' => t('Custom site slogan'),
        '#default_value' => vtcore_get_default_value('#site_slogan_enable', $block_data, 1),
      );

      $form['block'][$block]['site_slogan'] = array(
        '#type' => 'textarea',
        '#default_value' => vtcore_get_default_value('#site_slogan', $block_data, ''),
        '#description' => t('Enabling the custom site slogan by checking the checkbox above will override Drupal standard site slogan settings as found in admin/config/system/site-information'),
      );
    break;

    // Additional form setting for breadcrumb
    case 'breadcrumb' :
      $form['block'][$block]['breadcrumb_separator'] = array(
        '#type' => 'textfield',
        '#title' => t('Breadcrumb separator'),
        '#default_value' => vtcore_get_default_value('#breadcrumb_separator', $block_data, ' | '),
      );
    break;

    // Additional form setting for tabs
    case 'tabs' :
      $form['block'][$block]['tabs_js'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable js'),
        '#default_value' => vtcore_get_default_value('#tab_js', $block_data, 1),
      );

      $form['block'][$block]['tabs_js_user'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable js on user page'),
        '#default_value' =>  vtcore_get_default_value('#tab_js_user', $block_data, 0),
      );

      $form['block'][$block]['tabs_js_overlay'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable js on overlay'),
        '#default_value' =>  vtcore_get_default_value('#tab_js_overlay', $block_data, 0),
      );
    break;
  }
}