content_menu_domain_filter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Created by dreizwo.de.
  4. * User: jakobs
  5. * Date: 16.08.2012
  6. * Time: 13:59:48
  7. * @author markus jakobs <jakobs@dreizwo.de>
  8. */
  9. require_once 'content_menu_filter.php';
  10. class content_menu_domain_filter implements content_menu_filter {
  11. private $active;
  12. private $domain;
  13. function __construct() {
  14. $this->domain = $this->_content_menu_domainacccess();
  15. $this->active = $this->domain ? TRUE : FALSE;
  16. }
  17. public function addFilterWidget(&$form, &$form_state, $form_id) {
  18. // add a selection filter based only if multilang{
  19. if ($this->active) {
  20. foreach ($this->_content_menu_domain_list() as $did => $dom) {
  21. $options[$did] = $dom['sitename'];
  22. }
  23. $form['domainselect'] = array(
  24. '#type' => 'select',
  25. '#title' => t('Filter menu by domain'),
  26. '#options' => $options,
  27. '#default_value' => $this->domain['domain_id'],
  28. '#ajax' => array(
  29. 'callback' => '_content_menu_filter_elements_by_domain',
  30. )
  31. );
  32. $form['#content_menu_filter_widget'][] = 'domainselect';
  33. }
  34. }
  35. public function hideElement($el) {
  36. if ($this->active) {
  37. //hide on different lang
  38. if (isset($el['#item']['options']['domain_menu_access'])) {
  39. //unset if explicit hidden
  40. if (isset($el['#item']['options']['domain_menu_access']['hide']) &&
  41. in_array(
  42. 'd' . $this->domain['domain_id'], $el['#item']['options']['domain_menu_access']['hide'])
  43. ) {
  44. return TRUE;
  45. }
  46. //if shown empty && not in shown => unset too
  47. if (!empty($el['#item']['options']['domain_menu_access']['show']) &&
  48. !in_array(
  49. 'd' . $this->domain['domain_id'], $el['#item']['options']['domain_menu_access']['show'])
  50. ) {
  51. return TRUE;
  52. }
  53. }
  54. }
  55. return FALSE;
  56. }
  57. private function _content_menu_domainacccess() {
  58. if (module_exists('domain')) {
  59. $domains = $this->_content_menu_domain_list();
  60. if (isset($_SESSION['content_menu_domain_filter'])) {
  61. return $domains[$_SESSION['content_menu_domain_filter']];
  62. }
  63. if (count($domains) > 1) {
  64. return domain_default(FALSE, FALSE);
  65. }
  66. }
  67. return FALSE;
  68. }
  69. private function _content_menu_domain_list() {
  70. $cache = &drupal_static(__FUNCTION__, array());
  71. if (empty($cache['domains'])) {
  72. $domains = array();
  73. $query = db_select('domain', 'd')
  74. ->fields('d',
  75. array(
  76. 'domain_id',
  77. 'sitename',
  78. 'subdomain',
  79. 'scheme',
  80. 'valid',
  81. 'weight',
  82. 'is_default'
  83. ))
  84. ->orderBy('weight');
  85. // Get the domains.
  86. $result = $query->execute();
  87. while ($domain = $result->fetchAssoc()) {
  88. $domains[$domain['domain_id']] = domain_api($domain);
  89. }
  90. $cache['domains'] = $domains;
  91. return $domains;
  92. }
  93. return $cache['domains'];
  94. }
  95. }
  96. function _content_menu_filter_elements_by_domain($form, &$form_state) {
  97. $domain = $form_state['values']['domainselect'];
  98. ctools_include('ajax');
  99. $_SESSION['content_menu_domain_filter'] = $domain;
  100. $commands[] = ctools_ajax_command_reload();
  101. print ajax_render($commands);
  102. exit;
  103. }