00001 <?php
00002
00003
00004
00005
00006 function context_ui_ctools_plugin_directory($module, $plugin) {
00007 if ($module == 'ctools' && $plugin == 'export_ui') {
00008 return 'export_ui';
00009 }
00010 }
00011
00012
00013
00014
00015 function context_ui_theme() {
00016 $items = array();
00017 $items['context_ui_form'] = array(
00018 'render element' => 'form',
00019 'path' => drupal_get_path('module', 'context_ui') .'/theme',
00020 'template' => 'context-ui-form',
00021 'file' => 'theme.inc',
00022 );
00023 $items['context_ui_plugins'] = array(
00024 'render element' => 'form',
00025 'path' => drupal_get_path('module', 'context_ui') .'/theme',
00026 'template' => 'context-ui-plugins',
00027 'file' => 'theme.inc',
00028 );
00029 $items['context_ui_editor'] = array(
00030 'render element' => 'form',
00031 'path' => drupal_get_path('module', 'context_ui') .'/theme',
00032 'template' => 'context-ui-editor',
00033 'file' => 'theme.inc',
00034 );
00035 return $items;
00036 }
00037
00038
00039
00040
00041 function context_ui_block_info() {
00042 $blocks = array();
00043 $blocks['editor'] = array('info' => t('Context editor'), 'admin' => TRUE);
00044 if (module_exists('devel')) {
00045 $blocks['devel'] = array('info' => t('Context inspector'), 'admin' => TRUE);
00046 }
00047 return $blocks;
00048 }
00049
00050
00051
00052
00053 function context_ui_block_view($delta = '') {
00054 switch ($delta) {
00055 case 'editor':
00056 if (user_access('administer contexts') && strpos($_GET['q'], 'admin/structure/context') === FALSE && $contexts = context_active_contexts()) {
00057 return array(
00058 'subject' => t('Context editor'),
00059 'content' => drupal_get_form('context_ui_editor', $contexts),
00060 );
00061 }
00062 break;
00063 case 'devel':
00064 if (module_exists('devel') && $all = context_get()) {
00065 return array(
00066 'subject' => t('Context inspector'),
00067 'content' => kdevel_print_object($all),
00068 );
00069 }
00070 break;
00071 }
00072 }
00073
00074
00075
00076
00077 function context_ui_permission() {
00078 $permissions = array();
00079 $permissions['administer contexts'] = array(
00080 'title' => 'Administer contexts',
00081 'description' => 'Associate menus, views, blocks, etc. with different contexts to structure your site.'
00082 );
00083 return $permissions;
00084 }
00085
00086
00087
00088
00089 function context_ui_menu() {
00090 $items = array();
00091 $items['admin/structure/context/settings'] = array(
00092 'title' => 'Settings',
00093 'access callback' => 'user_access',
00094 'access arguments' => array('administer contexts'),
00095 'page callback' => 'drupal_get_form',
00096 'page arguments' => array('context_ui_settings'),
00097 'type' => MENU_LOCAL_TASK,
00098 'weight' => 3,
00099 );
00100 $items['context-ui/activate'] = array(
00101 'title' => 'Activate Context UI',
00102 'access arguments' => array('administer contexts'),
00103 'page callback' => 'context_ui_activate',
00104 'type' => MENU_CALLBACK
00105 );
00106 $items['context-ui/deactivate'] = array(
00107 'title' => 'Deactivate Context UI',
00108 'access arguments' => array('administer contexts'),
00109 'page callback' => 'context_ui_deactivate',
00110 'type' => MENU_CALLBACK
00111 );
00112 return $items;
00113 }
00114
00115
00116
00117
00118 function context_ui_help($path, $arg) {
00119 switch ($path) {
00120 case 'admin/help#context_ui':
00121 $output = file_get_contents(drupal_get_path('module', 'context_ui') .'/README.txt');
00122 return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>'. check_plain($output) .'</pre>';
00123 case 'admin/structure/context':
00124 return '<p>'. t('Context allows you to manage contextual conditions and reactions for different portions of your site. You can think of each context as representing a "section" of your site. For each context, you can choose the conditions that trigger this context to be active and choose different aspects of Drupal that should react to this active context.') .'</p>';
00125 }
00126 }
00127
00128
00129
00130
00131 function context_ui_editor($form, &$form_state, $contexts) {
00132 $form = array(
00133 '#attributes' => array('class' => array('context-editor')),
00134 '#theme' => array('context_ui_editor'),
00135 'editables' => array(),
00136 'contexts' => array('#tree' => TRUE),
00137 'buttons' => array('#tree' => FALSE),
00138 );
00139
00140 $form['title'] = array(
00141 '#prefix' => '<h2 class="context-editor-title">',
00142 '#markup' => t('Select the Context/Layer to Edit'),
00143 '#suffix' => '</h2>',
00144 '#weight' => -2,
00145 );
00146
00147
00148 $form['help'] = array (
00149 '#prefix' => '<p class="context-help help">',
00150 '#markup' => t('Select which context, or layer of blocks, to edit.
00151 Each context is configured to appear on different sets of pages so read the description carefully.
00152 When you are done editing click Done and save your changes.
00153 You may use the Stop Editing Layout link to close the editor.'),
00154 '#suffix' => '</p>',
00155 '#weight' => -1,
00156 );
00157
00158 $items = array();
00159 $form_context = array();
00160 ksort($contexts);
00161 foreach ($contexts as $context) {
00162 $edit = l(t('Edit'), $_GET['q'], array('fragment' => $context->name, 'attributes' => array('class' => array('edit'))));
00163 $done = l(t('Done'), $_GET['q'], array('fragment' => $context->name, 'attributes' => array('class' => array('done'))));
00164 $readable_name = ucwords(str_replace('_', ' ', $context->name));
00165 $description = empty($context->description) ? '' :
00166 "<br/><div class='label bottom'>".check_plain($context->description)."</div>";
00167 $items[] = array(
00168 'data' => "<div class='label top'>" . $readable_name. "</div><div class='links'>{$edit} {$done}</div>" . $description,
00169 'class' => array('context-editable clearfix'),
00170 'id' => "context-editable-trigger-{$context->name}",
00171 );
00172 $form_context = array(
00173 '#tree' => TRUE,
00174 '#type' => module_exists('admin') ? 'admin_panes' : NULL,
00175 'context' => array('#type' => 'value', '#value' => $context),
00176 );
00177
00178
00179 foreach (array_keys(context_reactions()) as $reaction) {
00180 $plugin = context_get_plugin('reaction', $reaction);
00181 if (method_exists($plugin, 'editor_form') && ($plugin_form = $plugin->editor_form($context))) {
00182 $form_context["reaction-{$reaction}"] = $plugin_form + array('#title' => $plugin->title);
00183 }
00184 }
00185
00186
00187 $form['contexts'][$context->name] = $form_context;
00188 }
00189
00190
00191 $form['editables']['#markup'] = theme('item_list', array('items' => $items));
00192
00193
00194 $form['buttons']['save'] = array(
00195 '#type' => 'submit',
00196 '#value' => t('Save changes'),
00197 '#submit' => array('context_ui_editor_submit'),
00198 );
00199 $form['buttons']['cancel'] = array(
00200 '#type' => 'submit',
00201 '#value' => t('Reset'),
00202 '#submit' => array('context_ui_editor_cancel'),
00203 );
00204
00205 $form['stop'] = array(
00206 '#markup' => l(t('Stop Editing Layout'), 'context-ui/deactivate', array(
00207 'query' => array('destination' => current_path()),
00208 'attributes' => array('class' => array('context_ui_dialog-stop')),
00209 )
00210 ),
00211 );
00212
00213 return $form;
00214 }
00215
00216
00217
00218
00219
00220 function context_ui_editor_process($values) {
00221 $context = $values['context'];
00222 foreach (array_keys(context_conditions()) as $condition) {
00223 if (isset($values['condition'][$condition])) {
00224 $plugin = context_get_plugin('condition', $condition);
00225 if ($plugin && method_exists($plugin, 'editor_form_submit')) {
00226 $context->conditions[$condition]['values'] = $plugin->editor_form_submit($context, $values['condition'][$condition]);
00227 }
00228 }
00229 if (isset($context->conditions[$condition]) && context_empty($context->conditions[$condition]['values'])) {
00230 unset($context->conditions[$condition]);
00231 }
00232 }
00233 foreach (array_keys(context_reactions()) as $reaction) {
00234 if (isset($values["reaction-{$reaction}"])) {
00235 $plugin = context_get_plugin('reaction', $reaction);
00236 if ($plugin && method_exists($plugin, 'editor_form_submit')) {
00237 $context->reactions[$reaction] = $plugin->editor_form_submit($context, $values["reaction-{$reaction}"]);
00238 }
00239 }
00240 if (isset($context->reactions[$reaction]) && context_empty($context->reactions[$reaction])) {
00241 unset($context->reactions[$reaction]);
00242 }
00243 }
00244 return $context;
00245 }
00246
00247
00248
00249
00250 function context_ui_editor_submit(&$form, &$form_state) {
00251 foreach ($form_state['values']['contexts'] as $name => $values) {
00252 $original_reactions = var_export($values['context']->reactions, TRUE);
00253 $context = context_ui_editor_process($values);
00254
00255 if (($original_reactions !== var_export($context->reactions, TRUE))) {
00256 if (context_save($context)) {
00257 drupal_set_message(t('Saved %title.', array(
00258 '%title' => (!empty($context->description) ? $context->description : $context->name)
00259 )));
00260 }
00261 else {
00262 drupal_set_message(t('Could not save context %title.', array('%title' => $context->name)), 'error');
00263 }
00264 }
00265 }
00266 return;
00267 }
00268
00269
00270
00271
00272 function context_ui_editor_cancel(&$form, &$form_state) {
00273 return;
00274 }
00275
00276
00277
00278
00279 function context_ui_settings($form, &$form_state) {
00280 $form = array();
00281 foreach (context_conditions() as $condition => $info) {
00282 if ($plugin = context_get_plugin('condition', $condition)) {
00283 $settings_form = $plugin->settings_form();
00284 if ($settings_form) {
00285 $form['conditions'][$condition] = $settings_form;
00286 $form['conditions'][$condition]['#tree'] = FALSE;
00287 $form['conditions'][$condition]['#type'] = 'fieldset';
00288 $form['conditions'][$condition]['#title'] = $info['title'];
00289 }
00290 }
00291 }
00292 foreach (context_reactions() as $reaction => $info) {
00293 if ($plugin = context_get_plugin('reaction', $reaction)) {
00294 $settings_form = $plugin->settings_form();
00295 if ($settings_form) {
00296 $form['reactions'][$reaction] = $settings_form;
00297 $form['reactions'][$reaction]['#tree'] = FALSE;
00298 $form['reactions'][$reaction]['#type'] = 'fieldset';
00299 $form['reactions'][$reaction]['#title'] = $info['title'];
00300 }
00301 }
00302 }
00303 $form['context_ui_dialog_enabled'] = array(
00304 '#type' => 'checkbox',
00305 '#title' => t('Use Context Editor Dialog'),
00306 '#default_value' => context_ui_dialog_is_enabled(),
00307 '#description' => t('When enabled all contextual links will have a Edit Layout link that will refresh the page with the context editor in a dialog box.'),
00308 );
00309 $form = system_settings_form($form);
00310 $form['#submit'][] = 'context_ui_settings_submit';
00311 return $form;
00312 }
00313
00314
00315
00316
00317
00318 function context_ui_settings_submit($form, &$form_state) {
00319 variable_set('menu_rebuild_needed', TRUE);
00320 }
00321
00322
00323
00324
00325
00326 function context_ui_dialog_is_enabled() {
00327
00328 return variable_get("context_ui_dialog_enabled", FALSE);
00329 }
00330
00331
00332
00333
00334
00335
00336
00337 function context_ui_page_alter(&$page) {
00338 $contexts = context_active_contexts();
00339 if (
00340 context_ui_dialog_is_enabled() &&
00341 context_isset('context_ui', 'context_ui_editor_present')
00342 ) {
00343 $contexts = context_active_contexts();
00344 $form = drupal_get_form('context_ui_editor', $contexts);
00345
00346 $path = drupal_get_path('module', 'context_ui');
00347 drupal_add_library('system', 'ui.dialog');
00348 drupal_add_js($path . '/context_ui_dialog.js', array('type' => 'file', 'weight' => 50));
00349 drupal_add_css($path . '/context_ui_dialog.css');
00350
00351
00352 $placement = variable_get('context_ui_editor_block_region', 'content');
00353 $page[$placement]['context_ui_editor'] = array(
00354 0 => array(
00355 '#type' => 'markup',
00356 '#markup' => '<div style="display:none;" id="context_ui_dialog-context-ui">' . drupal_render($form) . '<!--[if IE 8 ]><div id="context_ui_dialog-shadow"></div><![endif]--></div>',
00357 ),
00358 );
00359 }
00360 }
00361
00362
00363
00364
00365
00366
00367
00368 function context_ui_menu_contextual_links_alter(&$links, $router_item, $root_path) {
00369 if(context_ui_dialog_is_enabled() &&
00370 !context_isset('context_ui', 'context_ui_editor_present')) {
00371 $links['layout'] = array(
00372 'href' => 'context-ui/activate',
00373 'title' => t('Configure Layout'),
00374 'localized_options' => array(
00375 'query' => array('destination'=> $_GET['q']),
00376 'options' => array('html' => FALSE, 'attributes' => array()),
00377 ),
00378 );
00379 }
00380 }
00381
00382
00383
00384
00385 function context_ui_activate() {
00386 $_SESSION['context_ui_active'] = $_GET['destination'];
00387 drupal_goto($_GET['destination']);
00388 }
00389
00390
00391
00392
00393
00394
00395
00396 function context_ui_deactivate() {
00397 $_SESSION['context_ui_active'] = FALSE;
00398 drupal_goto($_GET['destination']);
00399 }
00400
00401
00402
00403
00404
00405
00406
00407 function context_ui_init() {
00408 if (!empty($_SESSION['context_ui_active'])) {
00409 $path = $_SESSION['context_ui_active'];
00410 if( $path == request_path() || $path == drupal_get_path_alias() || $path == drupal_get_normal_path(request_path()) ) {
00411 context_set('context_ui', 'context_ui_editor_present', TRUE);
00412 }
00413 }
00414
00415 }
00416
00417
00418
00419
00420
00421 function context_ui_page_build(&$page) {
00422 if (!context_get('context_ui', 'context_ui_editor_present') && isset($_SESSION['context_ui_active'])) {
00423 $_SESSION['context_ui_active'] = FALSE;
00424 }
00425 }
00426
00427
00428
00429
00430
00431 function context_ui_get_available_blocks() {
00432 drupal_json_output(array('lols' => 'testing'));
00433 }