Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Implementation of hook_init().
- *calling this hook causes the code below to execute before the page is loaded
- */
- function region_filter_init() {}
- //hook "nodeapi" is called when we are doing something to the node, in this "case" we are "viewing" the node.
- function region_filter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
- switch ($op) {
- case 'view':
- if( (arg(1) == 0)){ } //do not execute on homepage. if there are no aruments after the base url in arument one is thedelimagazine.com/argument1, if this is 0 then do nothing.
- else {
- $region = getRegion(); //get current region from deli.mmodule
- $national = 'national'; //store string 'national' in variable
- $count = count($node->taxonomy);//nstores number of key/value pairs in array, if two terms are selected with the node/post then count == 2
- $terms = array();//initialize terms to array
- foreach($node->taxonomy as $term) {
- $terms[$term->name] = $term->name; //prints array of individual terms
- }
- /*case 1 new england*/
- //if there are less than 9 terms and more than 2 and new england is in the array redirect to new england
- if( $count < 9 && $count > 2 && in_array('newengland', $terms) && $region != 'newengland')
- {
- $link = 'http://newengland.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');
- drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);
- }
- /*END case 1 new england*/
- /*case 2*/
- if($count == 2 && in_array('national', $terms) && is_numeric(arg(1))) { //if there are 2 terms in taxonomy && national is one of them & the first argument in url is a number such as thedelimagazine.com/12345 (before alias redirect)
- unset($terms['national']); //remove national from array
- $value = array_shift($terms) ;//set value to the other term left. in array
- if($value == 'los angeles') {//fix for los angelas, because in deli module taxonomy term and subdomain are different only for los angeles
- $value = 'la';
- }
- if($region != $value) { //if region does not already = term (would cause redirect loop)
- $link = 'http://' . $value . '.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');//go to term left in $value
- drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);//drupal redirect function
- }
- }
- /* ++ END case 2++*/
- /* ++case 3 national+++*/
- else {
- if($count > 10 && in_array('national', $terms) && $region != 'national' ) { //if there are moer than 10 terms and national is in the array $terms, and the current region does not already =national
- $link = 'http://national.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');//then set $link to national.thedelimagazine.com. drupl get path alias gets the path to the node and node id and converts it to clean readable urls that i implemented last year.
- drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301); //go to the $link
- }
- /* ++ END case 3 national++*/
- }
- }
- break;
- }
- }
- function region_filter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
- switch ($op) {
- case 'view':
- if( (arg(1) == 0)){ } //do not execute on homepage. if there are no aruments after the base url in arument one is thedelimagazine.com/argument1, if this is 0 then do nothing.
- else {
- $region = getRegion(); //get current region from deli.mmodule
- $national = 'national'; //store string 'national' in variable
- $count = count($node->taxonomy);//nstores number of key/value pairs in array, if two terms are selected with the node/post then count == 2
- $terms = array();//initialize terms to array
- foreach($node->taxonomy as $term) {
- $terms[$term->name] = $term->name; //prints array of individual terms
- }
- /*case 1 new england*/
- //if there are less than 9 terms and more than 2 and new england is in the array redirect to new england
- if( $count < 9 && $count > 2 && in_array('newengland', $terms) && $region != 'newengland')
- {
- $link = 'http://newengland.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');
- drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);
- /*case 2*/
- if($count == 2 && in_array('national', $terms) && is_numeric(arg(1))) { //if there are 2 terms in taxonomy && national is one of them & the first argument in url is a number such as thedelimagazine.com/12345 (before alias redirect)
- unset($terms['national']); //remove national from array
- $value = array_shift($terms) ;//set value to the other term left. in array
- if($value == 'los angeles') {//fix for los angelas, because in deli module taxonomy term and subdomain are different only for los angeles
- $value = 'la';
- }
- if($region != $value) { //if region does not already = term (would cause redirect loop)
- $link = 'http://' . $value . '.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');//go to term left in $value
- drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);//drupal redirect function
- /* ++ END case 2++*/
- /* ++case 3 national+++*/
- else {
- if($count > 10 && in_array('national', $terms) && $region != 'national' ) { //if there are moer than 10 terms and national is in the array $terms, and the current region does not already =national
- $link = 'http://national.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');//then set $link to national.thedelimagazine.com. drupl get path alias gets the path to the node and node id and converts it to clean readable urls that i implemented last year.
- drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301); //go to the $link
- }
- /* ++ END case 3 national++*/
- }
- }
- break;
- }
- }
- <?php
- // return the region that the user should be on for this node
- function determine_region($node)
- {
- $taxonomy_count = count($node->taxonomy);
- // I doubt this is neccesary, I don't know what
- // $node->taxonomy is, but perhaps you can use that rather
- // then copying the data into an array.
- $terms = array();
- foreach($node->taxonomy as $term) {
- $terms[$term->name] = $term->name;
- }
- // I really have no idea what the logic behind the region is
- if($taxonomy_count == 2 && in_array('national', $terms) && is_numeric(arg(1)))
- {
- $region = array_shift($terms);
- // los ageneles needs special case
- if($region == "los angeles")
- {
- $region = 'la';
- }
- return $region;
- }
- else if($taxonomy_count > 2 && $taxonomy_count < 9 && in_array('newengland', $terms)
- {
- return 'newengland';
- }
- else if($taxonomy_count > 10 && in_array('national', $terms) )
- {
- return 'national';
- }
- else
- {
- // none of the cases fit, we'll just keep the current region
- return getRegion();
- }
- }
- // redirect the user to a particular region and node
- function send_redirect($region, $node)
- {
- $link = 'http://' . $region . '.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');//go to term left in $value
- drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);
- }
- function region_filter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
- // if viewing something other then the home page, consider redirecting
- if($op == 'view' && arg(1) != 0)
- {
- $correct_region = determine_region($node);
- // redirect to correct region if necessary.
- if( getRegion() != $correct_region )
- {
- send_redirect($correct_region, $node);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement