View source
<?php
function _domain_taxonomy_is_debug() {
return variable_get('domain_taxonomy_debug', 0);
}
function domain_taxonomy_menu() {
$items = array();
$items['admin/build/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 = %d AND (realm = '%s' OR realm = '%s')", $term->tid, 'domain_id', 'domain_site');
while ($data = db_fetch_object($result)) {
$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'));
}
}
else {
if ($data->realm == 'domain_site') {
$term->domain_site = TRUE;
$term->subdomains[] = t('All affiliates');
}
}
}
if ($term->tid) {
$source = array();
$source = db_fetch_array(db_query("SELECT domain_id FROM {domain_taxonomy_source} WHERE tid = %d", $term->tid));
}
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 ($form['#id'] == 'node-form' && !isset($form['#node']->cck_dummy_node_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',
'#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') . ': ' . drupal_strtoupper($vocs[$parent_vid]->name),
'#default_value' => $form['#node']->nid ? false : true,
'#weight' => -3,
);
}
else {
$form['domain']['domain_load_fromparent'] = array(
'#type' => 'value',
'#value' => $form['#node']->nid ? false : true,
);
}
}
}
if (in_array($form['#id'], array(
'taxonomy-form-term',
'forum-form-forum',
'forum-form-container',
))) {
$term = taxonomy_get_term($form['tid']['#value']);
$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,
);
$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' => $term->tid ? false : true,
);
$form['domain']['domain_site'] = array(
'#type' => 'checkbox',
'#prefix' => '<div id="' . $dt_edit_id . '" ' . (!$term->tid ? 'style="display:none;"' : '') . '><strong>' . t('Publishing options') . ':</strong>',
'#suffix' => '</p>',
'#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 content.'),
'#default_value' => isset($term->tid) ? $term->domains : $default,
);
$form['domain']['domain_source'] = array(
'#type' => 'select',
'#title' => t('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 content from other affiliates, as needed.'),
);
$form['domain']['domain_update_subterm'] = array(
'#type' => 'checkbox',
'#title' => t('Override all child terms ON SUBMIT'),
'#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 ON SUBMIT'),
'#description' => t('This option is not for saving: it only processes child nodes when this form is submitted.'),
'#default_value' => false,
);
}
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());
}
else {
if ($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 content.'),
'#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_query("DELETE FROM {domain_taxonomy_access} WHERE tid = %d", $tid);
foreach ($grants as $grant) {
db_query("INSERT INTO {domain_taxonomy_access} (tid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, '%s', %d, %d, %d)", $tid, $grant['gid'], $grant['realm'], $grant['grant_view'], $grant['grant_update'], $grant['grant_delete']);
}
}
}
function _domain_taxonomy_update_source($term) {
db_query("DELETE FROM {domain_taxonomy_source} WHERE tid = %d", $term->tid);
db_query("INSERT INTO {domain_taxonomy_source} (tid, domain_id) VALUES (%d, %d)", $term->tid, $term->domain_source);
}
function domain_taxonomy_taxonomy($op, $type, $object = null) {
global $domtax_presaved_node;
$tid = $object['tid'];
if (empty($tid) || $op != 'insert' || $type != 'term') {
return;
}
$term = taxonomy_get_term($tid);
if (isset($term->domain_source) || isset($term->domains)) {
return;
}
if ($domtax_presaved_node) {
$term->domains = $domtax_presaved_node->domains;
$term->domain_site = $domtax_presaved_node->domain_site;
$term->domain_source = $domtax_presaved_node->domain_source;
$term->domains_raw = $domtax_presaved_node->domains_raw;
domain_taxonomy_save_term($term);
}
else {
$parents = taxonomy_get_parents($term->tid);
if (count($parents) > 0) {
$parent = array_shift($parents);
domain_taxonomy_load_term($parent);
$term->domains = $parent->domains;
$term->domain_site = $parent->domain_site;
$term->domain_source = $parent->domain_source;
domain_taxonomy_save_term($term);
}
}
}
function domain_taxonomy_nodeapi($node, $op) {
global $domtax_presaved_node;
if ($op != 'presave') {
return;
}
$domtax_presaved_node = $node;
}
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' => TRUE,
'grant_update' => FALSE,
'grant_delete' => FALSE,
'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' => TRUE,
'grant_update' => TRUE,
'grant_delete' => TRUE,
'priority' => 0,
);
}
}
}
if (empty($grants)) {
$grants[] = array(
'realm' => 'domain_id',
'gid' => 0,
'grant_view' => TRUE,
'grant_update' => TRUE,
'grant_delete' => TRUE,
'priority' => 0,
);
}
_domain_taxonomy_store_grants($term->tid, $grants);
_domain_taxonomy_update_source($term);
}
function domain_taxonomy_form_node_submit($form, &$form_state) {
if ($form_state['values']['domain_load_fromparent']) {
$parent_vid = variable_get('domain_inherit_type_voc_' . $form['#node']->type, null);
if ($parent_vid > 0) {
if ($form_state['values']['taxonomy'] && count($form_state['values']['taxonomy'][$parent_vid])) {
$parent_tids = $form_state['values']['taxonomy'][$parent_vid];
if (is_array($parent_tids)) {
$parent_tid = array_shift($parent_tids);
}
else {
$parent_tid = $parent_tids;
}
$parent_term = taxonomy_get_term($parent_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_get_term($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') . ': <br />' . _domain_taxonomy_get_access_info($term->domains ? $term->domains : $term->domains_raw, $term->domain_site, $term->domain_source), 'status');
}
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('Saving domain rules for child term ' . $subterm->name, 'status');
}
$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');
}
$result = domain_taxonomy_select_nodes(array(
$term->tid,
), 'or', 'all');
$items = array();
while ($node = db_fetch_object($result)) {
$node = node_load($node->nid);
if (_domain_taxonomy_is_debug()) {
drupal_set_message('Saving domain rules for child node ' . $node->nid . ': ' . $node->title, 'status');
}
$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();
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') . ': ' . ($domain_site ? t('yes') : t('no')) . '<br />';
$out .= t('Term source domain') . ': ' . $all_domains[$domain_source]['sitename'] . '<br />';
$out .= t('Term published on these 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_access_join_sql($term_alias = 't', $term_access_alias = 'dta') {
return 'LEFT JOIN {domain_taxonomy_access} ' . $term_access_alias . ' ON ' . $term_access_alias . '.tid = ' . $term_alias . '.tid';
}
function _domain_taxonomy_access_where_sql($table, $op = 'view', $term_access_alias = 'dta', $account = NULL) {
global $user;
$grants = array();
$account = $user;
foreach (domain_node_grants($account, $op) as $realm => $gids) {
foreach ($gids as $gkey => $gid) {
if (is_numeric($gkey)) {
$grants[] = "({$term_access_alias}.gid = {$gid} AND {$term_access_alias}.realm = '{$realm}')";
}
}
}
$grants_sql = '';
if (count($grants)) {
$grants_sql = 'AND (' . implode(' OR ', $grants) . ')';
}
$sql = "{$term_access_alias}.grant_{$op} >= 1 {$grants_sql}";
$disabled_vocs = _domain_taxonomy_get_disabled_vocabulares();
if (count($disabled_vocs)) {
$sql = '(' . $sql . ') OR ' . $table . '.vid IN(' . join(',', $disabled_vocs) . ')';
}
return $sql;
}
function domain_taxonomy_db_rewrite_sql($query, $table, $primary_field) {
if (arg(0) == 'admin') {
return;
}
if ($primary_field == 'tid' && ($table == 't' || $table == 'term_data')) {
$return['join'] = _domain_taxonomy_access_join_sql($table);
$return['where'] = _domain_taxonomy_access_where_sql($table);
$return['distinct'] = 1;
return $return;
}
}
function domain_taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0) {
if (count($tids) > 0) {
$descendant_tids = array();
if ($depth === 'all') {
$depth = NULL;
}
foreach ($tids as $index => $tid) {
$term = taxonomy_get_term($tid);
$tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
$descendant_tids[] = array_merge(array(
$tid,
), array_map('_taxonomy_get_tid_from_term', $tree));
}
if ($operator == 'or') {
$args = call_user_func_array('array_merge', $descendant_tids);
$placeholders = db_placeholders($args, 'int');
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN (' . $placeholders . ')';
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN (' . $placeholders . ')';
}
else {
$joins = '';
$wheres = '';
$args = array();
foreach ($descendant_tids as $index => $tids) {
$joins .= ' INNER JOIN {term_node} tn' . $index . ' ON n.vid = tn' . $index . '.vid';
$wheres .= ' AND tn' . $index . '.tid IN (' . db_placeholders($tids, 'int') . ')';
$args = array_merge($args, $tids);
}
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n ' . $joins . ' WHERE ' . $wheres;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n ' . $joins . ' WHERE ' . $wheres;
}
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql($sql_count);
$result = db_query($sql, $args);
}
return $result;
}
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'];
}
}
else {
if ($root != -1 && $seo && $_domain['domain_id'] != $root['domain_id']) {
$options['absolute'] = TRUE;
$options['base_url'] = rtrim($root['path'], '/');
$target_domain_id = $root['domain_id'];
}
}
}
}
}