context_ui.module

  1. cis7 sites/all/modules/ulmus/context/context_ui/context_ui.module
  2. cle7 sites/all/modules/ulmus/context/context_ui/context_ui.module
  3. ecd7 sites/all/modules/ulmus/context/context_ui/context_ui.module
  4. elmsmedia7 sites/all/modules/ulmus/context/context_ui/context_ui.module
  5. harmony7 sites/all/modules/ulmus/context/context_ui/context_ui.module
  6. icor7 sites/all/modules/ulmus/context/context_ui/context_ui.module
  7. meedjum_blog7 sites/all/modules/ulmus/context/context_ui/context_ui.module
  8. mooc7 sites/all/modules/ulmus/context/context_ui/context_ui.module

Functions

Namesort descending Description
context_ui_activate A page call back to activate the context_ui inline editor dialog.
context_ui_block_info Implementation of hook_block_info().
context_ui_block_view Implementation of hook_block_view().
context_ui_ctools_plugin_directory Implementation of hook_ctools_plugin_directory().
context_ui_deactivate A page call back to deactivate the context_ui inline editor dialog. This is semi unecessary as context editor will auto deactivate upon going to any page other than the destination from the start. However, its useful as a place to navigate to when…
context_ui_dialog_is_enabled context_ui_dialog_is_enabled test if the dialog is enabled
context_ui_editor Inline context editor form.
context_ui_editor_cancel Cancel handler for context_block_editor().
context_ui_editor_process Values processor for context_ui_editor_submit(). Split out for reuse by overriding submit handlers.
context_ui_editor_submit Save handler for context_block_editor().
context_ui_get_available_blocks Ajax callback to get the list of available blocks
context_ui_help Implementation of hook_help().
context_ui_init Implemenation of hook_init().
context_ui_menu Implementation of hook_menu().
context_ui_menu_contextual_links_alter Implementation of hook_menu_contextual_links_alter().
context_ui_page_alter Implementation of hook_page_alter().
context_ui_page_build Implementation of hook_page_build(). Turn off the context_ui functionality if we move to a different page
context_ui_permission Implementation of hook_permission().
context_ui_settings Settings form.
context_ui_settings_submit Extra submit handler for context_ui_settings. Mark the menu cache as needing a rebuild.
context_ui_theme Implementation of hook_theme().

File

sites/all/modules/ulmus/context/context_ui/context_ui.module
View source
  1. <?php
  2. /**
  3. * Implementation of hook_ctools_plugin_directory().
  4. */
  5. function context_ui_ctools_plugin_directory($module, $plugin) {
  6. if ($module == 'ctools' && $plugin == 'export_ui') {
  7. return 'export_ui';
  8. }
  9. }
  10. /**
  11. * Implementation of hook_theme().
  12. */
  13. function context_ui_theme() {
  14. $items = array();
  15. $items['context_ui_form'] = array(
  16. 'render element' => 'form',
  17. 'path' => drupal_get_path('module', 'context_ui') .'/theme',
  18. 'template' => 'context-ui-form',
  19. 'file' => 'theme.inc',
  20. );
  21. $items['context_ui_plugins'] = array(
  22. 'render element' => 'form',
  23. 'path' => drupal_get_path('module', 'context_ui') .'/theme',
  24. 'template' => 'context-ui-plugins',
  25. 'file' => 'theme.inc',
  26. );
  27. $items['context_ui_editor'] = array(
  28. 'render element' => 'form',
  29. 'path' => drupal_get_path('module', 'context_ui') .'/theme',
  30. 'template' => 'context-ui-editor',
  31. 'file' => 'theme.inc',
  32. );
  33. return $items;
  34. }
  35. /**
  36. * Implementation of hook_block_info().
  37. */
  38. function context_ui_block_info() {
  39. $blocks = array();
  40. $blocks['editor'] = array('info' => t('Context editor'), 'admin' => TRUE);
  41. if (module_exists('devel')) {
  42. $blocks['devel'] = array('info' => t('Context inspector'), 'admin' => TRUE);
  43. }
  44. return $blocks;
  45. }
  46. /**
  47. * Implementation of hook_block_view().
  48. */
  49. function context_ui_block_view($delta = '') {
  50. switch ($delta) {
  51. case 'editor':
  52. if (user_access('administer contexts') && strpos($_GET['q'], 'admin/structure/context') === FALSE && $contexts = context_active_contexts()) {
  53. return array(
  54. 'subject' => t('Context editor'),
  55. 'content' => drupal_get_form('context_ui_editor', $contexts),
  56. );
  57. }
  58. break;
  59. case 'devel':
  60. if (module_exists('devel') && $all = context_get()) {
  61. return array(
  62. 'subject' => t('Context inspector'),
  63. 'content' => kdevel_print_object($all),
  64. );
  65. }
  66. break;
  67. }
  68. }
  69. /**
  70. * Implementation of hook_permission().
  71. */
  72. function context_ui_permission() {
  73. $permissions = array();
  74. $permissions['administer contexts'] = array(
  75. 'title' => 'Administer contexts',
  76. 'description' => 'Associate menus, views, blocks, etc. with different contexts to structure your site.'
  77. );
  78. $permissions['context ajax block access'] = array(
  79. 'title' => t('Access All Blocks'),
  80. 'description' => t('Allows users to access all rendered blocks via an AJAX callback. If you have some blocks that should not be rendered for some users but need those users to be able to use context UI, then implement hook_context_allow_ajax_block_access with the necessary logic.'),
  81. );
  82. return $permissions;
  83. }
  84. /**
  85. * Implementation of hook_menu().
  86. */
  87. function context_ui_menu() {
  88. $items = array();
  89. $items['admin/structure/context/settings'] = array(
  90. 'title' => 'Settings',
  91. 'access callback' => 'user_access',
  92. 'access arguments' => array('administer contexts'),
  93. 'page callback' => 'drupal_get_form',
  94. 'page arguments' => array('context_ui_settings'),
  95. 'type' => MENU_LOCAL_TASK,
  96. 'weight' => 3,
  97. );
  98. $items['context-ui/activate'] = array(
  99. 'title' => 'Activate Context UI',
  100. 'access arguments' => array('administer contexts'),
  101. 'page callback' => 'context_ui_activate',
  102. 'type' => MENU_CALLBACK
  103. );
  104. $items['context-ui/deactivate'] = array(
  105. 'title' => 'Deactivate Context UI',
  106. 'access arguments' => array('administer contexts'),
  107. 'page callback' => 'context_ui_deactivate',
  108. 'type' => MENU_CALLBACK
  109. );
  110. return $items;
  111. }
  112. /**
  113. * Implementation of hook_help().
  114. */
  115. function context_ui_help($path, $arg) {
  116. switch ($path) {
  117. case 'admin/help#context_ui':
  118. $output = file_get_contents(drupal_get_path('module', 'context_ui') .'/README.txt');
  119. return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>'. check_plain($output) .'</pre>';
  120. case 'admin/structure/context':
  121. 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>';
  122. }
  123. }
  124. /**
  125. * Inline context editor form.
  126. */
  127. function context_ui_editor($form, &$form_state, $contexts) {
  128. $form = array(
  129. '#attributes' => array('class' => array('context-editor')),
  130. '#theme' => array('context_ui_editor'),
  131. 'editables' => array(),
  132. 'contexts' => array('#tree' => TRUE),
  133. 'buttons' => array('#tree' => FALSE),
  134. );
  135. $form['title'] = array(
  136. '#prefix' => '<h2 class="context-editor-title">',
  137. '#markup' => t('Select the Context/Layer to Edit'),
  138. '#suffix' => '</h2>',
  139. '#weight' => -2,
  140. );
  141. //add some help text to the top of the form
  142. $form['help'] = array (
  143. '#prefix' => '<p class="context-help help">',
  144. '#markup' => t('Select which context, or layer of blocks, to edit.
  145. Each context is configured to appear on different sets of pages so read the description carefully.
  146. When you are done editing click Done and save your changes.
  147. You may use the Stop Editing Layout link to close the editor.'),
  148. '#suffix' => '</p>',
  149. '#weight' => -1,
  150. );
  151. $items = array();
  152. $form_context = array();
  153. ksort($contexts);
  154. foreach ($contexts as $context) {
  155. $edit = l(t('Edit'), $_GET['q'], array('fragment' => $context->name, 'attributes' => array('class' => array('edit'))));
  156. $done = l(t('Done'), $_GET['q'], array('fragment' => $context->name, 'attributes' => array('class' => array('done'))));
  157. $readable_name = ucwords(str_replace('_', ' ', $context->name));
  158. $description = empty($context->description) ? '' :
  159. "<br/><div class='label bottom'>".check_plain($context->description)."</div>";
  160. $items[] = array(
  161. 'data' => "<div class='label top'>" . $readable_name. "</div><div class='links'>{$edit} {$done}</div>" . $description,
  162. 'class' => array('context-editable clearfix'),
  163. 'id' => "context-editable-trigger-{$context->name}",
  164. );
  165. $form_context = array(
  166. '#tree' => TRUE,
  167. '#type' => module_exists('admin') ? 'admin_panes' : NULL,
  168. 'context' => array('#type' => 'value', '#value' => $context),
  169. );
  170. // Edit context reactions.
  171. foreach (array_keys(context_reactions()) as $reaction) {
  172. $plugin = context_get_plugin('reaction', $reaction);
  173. if (method_exists($plugin, 'editor_form') && ($plugin_form = $plugin->editor_form($context))) {
  174. $form_context["reaction-{$reaction}"] = $plugin_form + array('#title' => $plugin->title);
  175. }
  176. }
  177. // Add to main form.
  178. $form['contexts'][$context->name] = $form_context;
  179. }
  180. // Display editable contexts in list.
  181. $form['editables']['#markup'] = theme('item_list', array('items' => $items));
  182. // Buttons.
  183. $form['buttons']['save'] = array(
  184. '#type' => 'submit',
  185. '#value' => t('Save changes'),
  186. '#submit' => array('context_ui_editor_submit'),
  187. );
  188. $form['buttons']['cancel'] = array(
  189. '#type' => 'submit',
  190. '#value' => t('Reset'),
  191. '#submit' => array('context_ui_editor_cancel'),
  192. );
  193. $form['stop'] = array(
  194. '#markup' => l(t('Stop Editing Layout'), 'context-ui/deactivate', array(
  195. 'query' => array('destination' => current_path()),
  196. 'attributes' => array('class' => array('context_ui_dialog-stop')),
  197. )
  198. ),
  199. );
  200. return $form;
  201. }
  202. /**
  203. * Values processor for context_ui_editor_submit().
  204. * Split out for reuse by overriding submit handlers.
  205. */
  206. function context_ui_editor_process($values) {
  207. $context = $values['context'];
  208. foreach (array_keys(context_conditions()) as $condition) {
  209. if (isset($values['condition'][$condition])) {
  210. $plugin = context_get_plugin('condition', $condition);
  211. if ($plugin && method_exists($plugin, 'editor_form_submit')) {
  212. $context->conditions[$condition]['values'] = $plugin->editor_form_submit($context, $values['condition'][$condition]);
  213. }
  214. }
  215. if (isset($context->conditions[$condition]) && context_empty($context->conditions[$condition]['values'])) {
  216. unset($context->conditions[$condition]);
  217. }
  218. }
  219. foreach (array_keys(context_reactions()) as $reaction) {
  220. if (isset($values["reaction-{$reaction}"])) {
  221. $plugin = context_get_plugin('reaction', $reaction);
  222. if ($plugin && method_exists($plugin, 'editor_form_submit')) {
  223. $context->reactions[$reaction] = $plugin->editor_form_submit($context, $values["reaction-{$reaction}"]);
  224. }
  225. }
  226. if (isset($context->reactions[$reaction]) && context_empty($context->reactions[$reaction])) {
  227. unset($context->reactions[$reaction]);
  228. }
  229. }
  230. return $context;
  231. }
  232. /**
  233. * Save handler for context_block_editor().
  234. */
  235. function context_ui_editor_submit(&$form, &$form_state) {
  236. foreach ($form_state['values']['contexts'] as $name => $values) {
  237. $original_reactions = var_export($values['context']->reactions, TRUE);
  238. $context = context_ui_editor_process($values);
  239. //compare string values instead of actual objects to avoid problems with aliasing
  240. if (($original_reactions !== var_export($context->reactions, TRUE))) {
  241. if (context_save($context)) {
  242. drupal_set_message(t('Saved %title.', array(
  243. '%title' => (!empty($context->description) ? $context->description : $context->name)
  244. )));
  245. }
  246. else {
  247. drupal_set_message(t('Could not save context %title.', array('%title' => $context->name)), 'error');
  248. }
  249. }
  250. }
  251. return;
  252. }
  253. /**
  254. * Cancel handler for context_block_editor().
  255. */
  256. function context_ui_editor_cancel(&$form, &$form_state) {
  257. return;
  258. }
  259. /**
  260. * Settings form.
  261. */
  262. function context_ui_settings($form, &$form_state) {
  263. $form = array();
  264. foreach (context_conditions() as $condition => $info) {
  265. if ($plugin = context_get_plugin('condition', $condition)) {
  266. $settings_form = $plugin->settings_form();
  267. if ($settings_form) {
  268. $form['conditions'][$condition] = $settings_form;
  269. $form['conditions'][$condition]['#tree'] = FALSE;
  270. $form['conditions'][$condition]['#type'] = 'fieldset';
  271. $form['conditions'][$condition]['#title'] = $info['title'];
  272. }
  273. }
  274. }
  275. foreach (context_reactions() as $reaction => $info) {
  276. if ($plugin = context_get_plugin('reaction', $reaction)) {
  277. $settings_form = $plugin->settings_form();
  278. if ($settings_form) {
  279. $form['reactions'][$reaction] = $settings_form;
  280. $form['reactions'][$reaction]['#tree'] = FALSE;
  281. $form['reactions'][$reaction]['#type'] = 'fieldset';
  282. $form['reactions'][$reaction]['#title'] = $info['title'];
  283. }
  284. }
  285. }
  286. $form['context_ui_dialog_enabled'] = array(
  287. '#type' => 'checkbox',
  288. '#title' => t('Use Context Editor Dialog'),
  289. '#default_value' => context_ui_dialog_is_enabled(),
  290. '#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.'),
  291. );
  292. $form = system_settings_form($form);
  293. $form['#submit'][] = 'context_ui_settings_submit';
  294. return $form;
  295. }
  296. /**
  297. * Extra submit handler for context_ui_settings.
  298. * Mark the menu cache as needing a rebuild.
  299. */
  300. function context_ui_settings_submit($form, &$form_state) {
  301. variable_set('menu_rebuild_needed', TRUE);
  302. }
  303. /**
  304. * context_ui_dialog_is_enabled test if the dialog is enabled
  305. */
  306. function context_ui_dialog_is_enabled() {
  307. return variable_get("context_ui_dialog_enabled", FALSE);
  308. }
  309. /**
  310. * Implementation of hook_page_alter().
  311. *
  312. * If we have the dialog enabled and active build the dialog
  313. * and add to the page
  314. */
  315. function context_ui_page_alter(&$page) {
  316. $contexts = context_active_contexts();
  317. if (
  318. context_ui_dialog_is_enabled() &&
  319. context_isset('context_ui', 'context_ui_editor_present')
  320. ) {
  321. $contexts = context_active_contexts();
  322. $form = drupal_get_form('context_ui_editor', $contexts);
  323. $path = drupal_get_path('module', 'context_ui');
  324. drupal_add_library('system', 'ui.dialog');
  325. drupal_add_js($path . '/context_ui_dialog.js', array('type' => 'file', 'weight' => 50));
  326. drupal_add_css($path . '/context_ui_dialog.css');
  327. //figure out which region to put it in - allow it to be configured for themes using different regions
  328. $placement = variable_get('context_ui_editor_block_region', 'content');
  329. $page[$placement]['context_ui_editor'] = array(
  330. 0 => array(
  331. '#type' => 'markup',
  332. '#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>',
  333. ),
  334. );
  335. }
  336. }
  337. /**
  338. * Implementation of hook_menu_contextual_links_alter().
  339. *
  340. * we we have the dialog enabled lets add a link to all contextual links
  341. * to activate it.
  342. */
  343. function context_ui_menu_contextual_links_alter(&$links, $router_item, $root_path) {
  344. if(context_ui_dialog_is_enabled() &&
  345. !context_isset('context_ui', 'context_ui_editor_present') && user_access('administer contexts')) {
  346. $links['layout'] = array(
  347. 'href' => 'context-ui/activate',
  348. 'title' => t('Configure Layout'),
  349. 'localized_options' => array(
  350. 'query' => array('destination'=> $_GET['q']),
  351. 'options' => array('html' => FALSE, 'attributes' => array()),
  352. ),
  353. );
  354. }
  355. }
  356. /**
  357. * A page call back to activate the context_ui inline editor dialog.
  358. */
  359. function context_ui_activate() {
  360. $_SESSION['context_ui_active'] = $_GET['destination'];
  361. drupal_goto($_GET['destination']);
  362. }
  363. /**
  364. * A page call back to deactivate the context_ui inline editor dialog.
  365. * This is semi unecessary as context editor will auto deactivate upon going to any
  366. * page other than the destination from the start. However, its useful as a place
  367. * to navigate to when deactivating context_ui_editor
  368. */
  369. function context_ui_deactivate() {
  370. $_SESSION['context_ui_active'] = FALSE;
  371. drupal_goto($_GET['destination']);
  372. }
  373. /**
  374. * Implemenation of hook_init().
  375. *
  376. * If the session says we should have an active dialog set a context variable to tell everything else
  377. * Ignores ajax requests.
  378. */
  379. function context_ui_init() {
  380. if (!empty($_SESSION['context_ui_active'])) {
  381. $path = $_SESSION['context_ui_active'];
  382. if( $path == request_path() || $path == drupal_get_path_alias() || $path == drupal_get_normal_path(request_path()) ) {
  383. context_set('context_ui', 'context_ui_editor_present', TRUE);
  384. }
  385. }
  386. // Turn off functionality has been moved to hook_page_build() to prevent non-pages from triggering it
  387. }
  388. /**
  389. * Implementation of hook_page_build().
  390. * Turn off the context_ui functionality if we move to a different page
  391. */
  392. function context_ui_page_build(&$page) {
  393. if (!context_get('context_ui', 'context_ui_editor_present') && isset($_SESSION['context_ui_active'])) {
  394. $_SESSION['context_ui_active'] = FALSE;
  395. }
  396. }
  397. /**
  398. * Ajax callback to get the list of available blocks
  399. *
  400. */
  401. function context_ui_get_available_blocks() {
  402. drupal_json_output(array('lols' => 'testing'));
  403. }