View source
<?php
function _domain_taxonomy_is_debug() {
return variable_get('domain_taxonomy_debug', 0);
}
function domain_taxonomy_menu() {
$items = array();
$items['admin/structure/domain/taxonomy'] = array(
'title' => 'Taxonomy',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_taxonomy_configure_form',
),
'file' => 'domain_taxonomy.admin.inc',
'weight' => -10,
);
return $items;
}
function domain_taxonomy_load_term(&$term) {
if (!isset($term->tid)) {
return;
}
$term->domains = array();
$term->domain_site = FALSE;
$result = db_query("SELECT gid, realm FROM {domain_taxonomy_access} WHERE tid = :tid AND (realm = 'domain_id' OR realm = 'domain_site')", array(
':tid' => $term->tid,
));
foreach ($result as $data) {
$data->gid == 0 ? $gid = -1 : ($gid = $data->gid);
if ($data->realm == 'domain_id') {
$term->domains[$gid] = $gid;
if ($gid > 0) {
$domain = domain_lookup($gid);
$term->subdomains[] = $domain['sitename'];
}
else {
$term->subdomains[] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
}
}
elseif ($data->realm == 'domain_site') {
$term->domain_site = TRUE;
$term->subdomains[] = t('All affiliates');
}
}
if ($term->tid) {
$source = array();
$source = db_query("SELECT domain_id FROM {domain_taxonomy_source} WHERE tid = :tid", array(
':tid' => $term->tid,
))
->fetchAssoc();
}
if (empty($source)) {
$term->domain_source = variable_get('domain_default_source', 0);
}
else {
$term->domain_source = $source['domain_id'];
}
}
function domain_taxonomy_form_alter(&$form, &$form_state, $form_id) {
global $_domain, $user;
$forms = module_invoke_all('domainignore');
if (in_array($form_id, $forms)) {
return;
}
if (isset($form['#node']) && isset($form['#node_edit_form'])) {
$form['#submit'][] = 'domain_taxonomy_form_node_submit';
$parent_vid = variable_get('domain_inherit_type_voc_' . $form['#node']->type, 0);
if ($parent_vid) {
$vocs = taxonomy_get_vocabularies();
if (user_access('set domain access')) {
$form['domain']['domain_load_fromparent'] = array(
'#type' => 'checkbox',
'#prefix' => '<b>' . t('Actions on form submit') . ':</b>',
'#title' => t('Load domain access options for node from parent term'),
'#description' => t('This option overrides domain access options below (except <em>Source domain</em>). Parent vocabulary @v', array(
'@v' => drupal_strtoupper($vocs[$parent_vid]->name),
)),
'#default_value' => TRUE,
'#weight' => -3,
);
}
else {
$form['domain']['domain_load_fromparent'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
}
}
if (in_array($form['#id'], array(
'taxonomy-form-term',
'forum-form-forum',
'forum-form-container',
))) {
if (!isset($form['tid'])) {
return;
}
$term = taxonomy_term_load($form['tid']['#value']);
if (!isset($form['vid'])) {
return;
}
$vocabulary = taxonomy_vocabulary_load($form['vid']['#value']);
$disabled_vocs = _domain_taxonomy_get_disabled_vocabulares();
if (in_array($vocabulary->vid, $disabled_vocs)) {
return;
}
domain_taxonomy_load_term($term);
$form['#submit'][] = 'domain_taxonomy_form_term_submit';
$default = array(
$_domain['domain_id'],
);
$behavior = variable_get('domain_taxonomy_behavior', DOMAIN_INSTALL_RULE);
if ($_domain['domain_id'] == 0) {
$default[] = -1;
}
if (isset($term->tid)) {
$raw = $term->domains;
}
else {
$raw = $default;
}
$options = array();
foreach (domain_domains() as $data) {
$data['domain_id'] == 0 ? $key = -1 : ($key = $data['domain_id']);
if ($data['valid'] || user_access('administer domains')) {
$options[$key] = $data['sitename'];
$source_options[$data['domain_id']] = $data['sitename'];
}
}
if (user_access('set domain access')) {
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Domain access options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 0,
);
$dt_edit_id = 'domain-taxonomy-term-edit-' . (isset($term->tid) ? $term->tid : '');
$form['domain']['domain_update_subterm'] = array(
'#type' => 'checkbox',
'#prefix' => '<strong>' . t('Actions on form submit') . ':</strong>',
'#title' => t('Override all child terms'),
'#description' => t('This option is not for saving: it only processes subterms when this form is submitted.'),
'#default_value' => FALSE,
);
$form['domain']['domain_update_subnodes'] = array(
'#type' => 'checkbox',
'#title' => t('Override all child nodes'),
'#description' => t('This option is not for saving: it only processes child nodes when this form is submitted.'),
'#default_value' => FALSE,
);
$form['domain']['domain_load_fromparent'] = array(
'#type' => 'checkbox',
'#title' => t('Load domain access options for terms from parent term'),
'#description' => t('This option overrides the domain access options below. It is not for saving: the inherit action is applied only when this form is submitted.'),
'#default_value' => isset($term->tid) ? FALSE : TRUE,
'#attributes' => array(
'onclick' => 'jQuery(this).is(":checked") ? jQuery(\'#' . $dt_edit_id . '\').hide():jQuery(\'#' . $dt_edit_id . '\').show();',
),
);
$form['domain']['domain_site'] = array(
'#type' => 'checkbox',
'#prefix' => '<div id="' . $dt_edit_id . '" ' . (!isset($term->tid) ? 'style="display:none;"' : '') . '><strong>' . t('Publishing options') . ':</strong>',
'#title' => t('Send to all affiliates'),
'#required' => FALSE,
'#description' => t('Select if this term can be shown to all affiliates. This setting will override the options below.'),
'#default_value' => isset($term->tid) ? $term->domain_site : variable_get('domain_share_voc_' . $vocabulary->vid, $behavior),
);
$form['domain']['domains'] = array(
'#type' => 'checkboxes',
'#title' => t('Publish to'),
'#options' => $options,
'#required' => TRUE,
'#description' => t('Select which affiliates can access this term.'),
'#default_value' => isset($term->tid) ? $term->domains : $default,
);
$form['domain']['domain_source'] = array(
'#type' => 'select',
'#suffix' => '</div>',
'#title' => t('Term source domain'),
'#options' => $source_options,
'#default_value' => isset($term->tid) ? $term->domain_source : $_domain['domain_id'],
'#description' => t('This affiliate will be used to write links to this term from other affiliates, as needed.'),
);
}
else {
$form['domain']['domain_load_fromparent'] = array(
'#type' => 'value',
'#value' => $term->tid ? false : true,
);
if (user_access('view domain publishing')) {
$user->domain_user = domain_get_user_domains($user);
$action = variable_get('domain_options', 0);
$user_domains = array();
$default_options = array();
$user_options = array();
if (!empty($user->domain_user)) {
foreach ($user->domain_user as $key => $value) {
if (abs($value) > 0) {
$user_domains[] = $value;
}
}
$first_domain = current($user_domains);
foreach ($options as $key => $value) {
if (in_array($key, $user_domains)) {
$user_options[$key] = $value;
}
}
}
foreach ($raw as $key => $value) {
if (in_array($value, $user_domains)) {
$default_options[] = $value;
}
else {
$raw_options[] = $value;
}
}
switch ($action) {
case 1:
$root = domain_default();
if ($root['domain_id'] != $_domain['domain_id']) {
domain_goto($root);
}
break;
case 2:
$domain = domain_lookup($first_domain);
if ($domain == -1 || $domain['valid'] == 0) {
domain_goto(domain_default());
}
elseif ($domain['domain_id'] != $_domain['domain_id']) {
domain_goto($domain);
}
break;
case 3:
if (empty($user_options)) {
$form = array();
return drupal_access_denied();
}
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Affiliate publishing options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => $weight,
);
if ($term->tid) {
$raw = $raw_options;
}
else {
$raw = array();
}
empty($raw) ? $required = TRUE : ($required = FALSE);
$form['domain']['domains'] = array(
'#type' => 'checkboxes',
'#title' => t('Publish to'),
'#options' => $user_options,
'#required' => $required,
'#description' => t('Select which affiliates can access this term.'),
'#default_value' => isset($term->tid) ? $term->domains : $default_options,
);
$list = array();
if ($term->domain_site) {
$list[]['data'] = t('All affiliates');
}
if (!empty($raw)) {
foreach ($raw as $did) {
$did == -1 ? $id = 0 : ($id = $did);
$raw_domains = domain_lookup($id);
$list[]['data'] = $raw_domains['sitename'];
}
}
if (!empty($list)) {
$form['domain']['domains_notes'] = array(
'#value' => '<label><strong>' . t('Publishing status') . ':</strong></label>' . theme('item_list', $list) . '<div class="description">' . t('This term has also been published to these affiliates.') . '</div>',
);
}
break;
}
}
$form['domain_site'] = array(
'#type' => 'value',
'#value' => isset($term->tid) ? $term->domain_site : variable_get('domain_share_voc_' . $vocabulary->vid, $behavior),
);
$form['domains_raw'] = array(
'#type' => 'value',
'#value' => $raw,
);
}
}
}
function _domain_taxonomy_store_grants($tid, $grants = array()) {
if ($tid > 0 && !empty($grants)) {
db_delete('domain_taxonomy_access')
->condition('tid', $tid)
->execute();
foreach ($grants as $grant) {
db_insert('domain_taxonomy_access')
->fields(array(
'tid' => $tid,
'gid' => $grant['gid'],
'realm' => $grant['realm'],
'grant_view' => $grant['grant_view'],
'grant_update' => $grant['grant_update'],
'grant_delete' => $grant['grant_delete'],
))
->execute();
}
}
}
function _domain_taxonomy_update_source($term) {
db_delete('domain_taxonomy_source')
->condition('tid', $term->tid)
->execute();
db_insert('domain_taxonomy_source')
->fields(array(
'tid' => $term->tid,
'domain_id' => $term->domain_source,
))
->execute();
}
function domain_taxonomy_save_term($term) {
$grants = array();
if (!empty($term->domains_raw)) {
if (!isset($term->domains)) {
$term->domains = array();
}
foreach ($term->domains_raw as $value) {
if (!in_array($value, $term->domains)) {
$term->domains[$value] = $value;
}
}
}
if (!empty($term->domain_site)) {
$grants[] = array(
'realm' => 'domain_site',
'gid' => 0,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
);
}
if (!empty($term->domains)) {
foreach ($term->domains as $key => $value) {
if (abs($value) > 0) {
$key == -1 ? $key = 0 : ($key = $key);
$grants[] = array(
'realm' => 'domain_id',
'gid' => $key,
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 1,
'priority' => 0,
);
}
}
}
if (empty($grants)) {
$grants[] = array(
'realm' => 'domain_id',
'gid' => 0,
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 1,
'priority' => 0,
);
}
_domain_taxonomy_store_grants($term->tid, $grants);
_domain_taxonomy_update_source($term);
}
function domain_taxonomy_form_node_submit($form, &$form_state) {
if (isset($form_state['values']['domain_load_fromparent']) && $form_state['values']['domain_load_fromparent']) {
$parent_vid = variable_get('domain_inherit_type_voc_' . $form['#node']->type, NULL);
if ($parent_vid > 0) {
$fields = field_info_fields();
$field_name = '';
foreach ($fields as $field) {
if ($field['type'] == 'taxonomy_term_reference') {
$voc = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']);
if ($parent_vid == $voc->vid) {
$field_name = $field['field_name'];
}
}
}
if (count($form_state['values'][$field_name]['und'])) {
$parent_tids = $form_state['values'][$field_name]['und'];
if (is_array($parent_tids)) {
$parent_tid = array_shift($parent_tids);
}
else {
$parent_tid = $parent_tids;
}
$parent_term = taxonomy_term_load($parent_tid['tid']);
if (!$parent_term) {
return;
}
domain_taxonomy_load_term($parent_term);
if ($parent_term) {
$form_state['values']['domains'] = $parent_term->domains;
$form_state['values']['domain_site'] = $parent_term->domain_site;
}
}
}
}
}
function domain_taxonomy_form_term_submit($form, $form_state) {
$term = (object) $form_state['values'];
if ($form_state['values']['domain_load_fromparent']) {
$parent_tids = $form_state['values']['parent'];
if (is_array($parent_tids)) {
$parent_tid = array_shift($parent_tids);
}
else {
$parent_tid = $parent_tids;
}
$parent_term = taxonomy_term_load($parent_tid);
if ($parent_term) {
domain_taxonomy_load_term($parent_term);
if ($parent_term) {
$term->domains = $parent_term->domains;
$term->domain_site = $parent_term->domain_site;
$term->domain_source = $parent_term->domain_source;
}
}
}
if (_domain_taxonomy_is_debug()) {
drupal_set_message(t('Saving term with options:'));
drupal_set_message(_domain_taxonomy_get_access_info($term->domains ? $term->domains : $term->domains_raw, $term->domain_site, $term->domain_source));
}
domain_taxonomy_save_term($term);
if ($term->domain_update_subterm) {
if (_domain_taxonomy_is_debug()) {
drupal_set_message(t('Domain rules inheritance applied from current term to child terms'), 'status');
}
$terms = taxonomy_get_tree($term->vid, $term->tid);
if (count($terms) > 0) {
foreach ($terms as $subterm) {
if (_domain_taxonomy_is_debug()) {
drupal_set_message(t('Saving domain rules for child term @name', array(
'@name' => $subterm->name,
)));
}
$subterm->domains = $term->domains;
$subterm->domain_site = $term->domain_site;
$subterm->domain_source = $term->domain_source;
domain_taxonomy_save_term($subterm);
}
}
}
if ($term->domain_update_subnodes) {
if (_domain_taxonomy_is_debug()) {
drupal_set_message(t('Domain rules inheritance applied from current term to child nodes'), 'status');
}
$nodes = domain_taxonomy_select_nodes(array(
$term->tid,
), 'all');
foreach ($nodes as $node) {
if (_domain_taxonomy_is_debug()) {
drupal_set_message(t('Saving domain rules for child node @nid: @title', array(
'@nid' => $node->nid,
'@title' => $node->title,
)));
}
$node->domains = $term->domains;
$node->domain_site = $term->domain_site;
$node->domain_source = $term->domain_source;
node_access_acquire_grants($node);
}
}
}
function _domain_taxonomy_get_access_info($domains, $domain_site, $domain_source) {
$all_domains = domain_domains();
$domains_text = '';
$out = '';
foreach ($domains as $did => $v) {
if ($did == -1) {
$did = 0;
}
if (!$v || !$all_domains[$did]) {
continue;
}
$domains_text .= '<br /> - ' . $all_domains[$did]['sitename'];
}
$out .= t('Term published to all affiliates: @yes_no', array(
'@yes_no' => $domain_site ? t('yes') : t('no'),
)) . '<br />';
$out .= t('Term source domain') . ': ' . $all_domains[$domain_source]['sitename'] . '<br />';
$out .= t('Term published on this domains') . ': ' . $domains_text . '<br />';
return $out;
}
function _domain_taxonomy_get_disabled_vocabulares() {
$vocs = taxonomy_get_vocabularies();
$items = array();
if (count($vocs) > 0) {
foreach ($vocs as $voc) {
if (variable_get('domain_disable_voc_' . $voc->vid, FALSE)) {
$items[$voc->vid] = $voc->vid;
}
}
}
return $items;
}
function domain_taxonomy_query_term_access_alter($query) {
if (path_is_admin(current_path()) && arg(0) != 'node') {
return;
}
$tables = $query
->getTables();
$query
->getMetaData('vid');
foreach ($tables as $alias => $tableinfo) {
$table = $tableinfo['table'];
if ($table == 'taxonomy_term_data') {
$query
->join('domain_taxonomy_access', 'dta', $alias . '.tid = dta.tid');
global $user, $_domain;
static $_modules;
$grants = array();
$grants_list = array();
$account = $user;
if ($_domain['site_grant']) {
$grants['domain_site'][] = 0;
}
$grants['domain_id'][] = $_domain['domain_id'];
$or = db_or();
foreach ($grants as $realm => $gids) {
foreach ($gids as $gkey => $gid) {
$or
->condition(db_and()
->condition('dta.gid', $gid)
->condition('dta.realm', $realm));
}
}
$condition = db_and()
->condition('dta.grant_view', 1)
->condition($or);
$disabled_vocs = _domain_taxonomy_get_disabled_vocabulares();
if (count($disabled_vocs)) {
$condition = db_or()
->condition($alias . '.vid', $disabled_vocs, 'IN')
->condition($condition);
}
$query
->condition($condition);
}
}
$query
->distinct();
}
function domain_taxonomy_select_nodes($tids = array(), $depth = 0) {
if (count($tids) > 0) {
$descendant_tids = array();
if ($depth === 'all') {
$depth = NULL;
}
foreach ($tids as $index => $tid) {
$child_terms = taxonomy_get_children($tid);
$descendant_tids = array_merge(array(
$tid,
), array_map('_taxonomy_get_tid_from_term', $child_terms), $descendant_tids);
}
$result = db_query('SELECT ti.nid FROM {taxonomy_index} ti WHERE ti.tid in (:tid)', array(
':tid' => implode(',', $descendant_tids),
));
$nids = $result
->fetchAllAssoc('nid');
$nodes = node_load_multiple(array_keys($nids));
}
return $nodes;
}
function domain_taxonomy_url_rewrite_outbound(&$path, &$options, $original_path) {
global $_domain;
if (!isset($_domain['domain_id'])) {
return;
}
static $domain_site, $domain, $nodepaths, $path_rewrite;
$check = domain_grant_all();
$seo = variable_get('domain_taxonomy_seo', 0);
$use_source = TRUE;
if ($check || $seo || $use_source) {
$pattern = explode('/', $original_path);
if (!isset($nodepaths)) {
$pathdata = variable_get('domain_taxonomy_paths', "taxonomy/term/%t\r\ntaxonomy/edit/term/%t\r\nforum/%t");
$path_match = preg_replace('/(\\r\\n?|\\n)/', '|', $pathdata);
$nodepaths = explode("|", $path_match);
}
$tid = FALSE;
foreach ($nodepaths as $match) {
$match_array = explode('/', $match);
$placeholder = array_search('%t', $match_array);
if (isset($pattern[$placeholder])) {
$match_array[$placeholder] = $pattern[$placeholder];
if (is_numeric($pattern[$placeholder]) && $match_array == $pattern) {
$tid = (int) $pattern[$placeholder];
break;
}
}
}
$target_domain_id = $_domain['domain_id'];
if ($tid) {
$root = domain_lookup(variable_get('domain_default_source', 0));
if (!isset($domain_site[$tid])) {
$domain_site[$tid] = db_result(db_query("SELECT grant_view FROM {domain_taxonomy_access} WHERE tid = %d AND gid = 0 AND realm = '%s'", $tid, 'domain_site'));
}
if (!$domain_site[$tid] || $use_source) {
if (!isset($domain[$tid])) {
if ($use_source) {
$source = db_result(db_query("SELECT domain_id FROM {domain_taxonomy_source} WHERE tid = %d", $tid));
$domain[$tid] = domain_lookup($source);
}
else {
$id = db_result(db_query_range("SELECT gid FROM {domain_taxonomy_access} WHERE tid = %d AND realm = '%s' AND grant_view = 1 ORDER BY gid", $tid, 'domain_id', 0, 1));
$domain[$tid] = domain_lookup($id);
}
}
if ($domain[$tid] != -1 && $domain[$tid]['domain_id'] != $_domain['domain_id']) {
$options['absolute'] = TRUE;
$options['base_url'] = rtrim($domain[$tid]['path'], '/');
if (isset($source)) {
$seo = FALSE;
}
$target_domain_id = $domain[$tid]['domain_id'];
}
}
elseif ($root != -1 && $seo && $_domain['domain_id'] != $root['domain_id']) {
$options['absolute'] = TRUE;
$options['base_url'] = rtrim($root['path'], '/');
$target_domain_id = $root['domain_id'];
}
}
}
}