Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. <?php
  2. /**
  3. * Implementation of hook_init().
  4. *calling this hook causes the code below to execute before the page is loaded
  5. */
  6.  
  7.  
  8. function region_filter_init() {}
  9. //hook "nodeapi" is called when we are doing something to the node, in this "case" we are "viewing" the node.
  10.  
  11.  
  12.  
  13. function region_filter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  14.  
  15. switch ($op) {
  16.  
  17. case 'view':
  18.  
  19.  
  20. 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.
  21.  
  22.  
  23. else {
  24.  
  25. $region = getRegion(); //get current region from deli.mmodule
  26.  
  27. $national = 'national'; //store string 'national' in variable
  28.  
  29. $count = count($node->taxonomy);//nstores number of key/value pairs in array, if two terms are selected with the node/post then count == 2
  30.  
  31. $terms = array();//initialize terms to array
  32. foreach($node->taxonomy as $term) {
  33. $terms[$term->name] = $term->name; //prints array of individual terms
  34.  
  35.  
  36.  
  37.  
  38. }
  39.  
  40.  
  41. /*case 1 new england*/
  42.  
  43. //if there are less than 9 terms and more than 2 and new england is in the array redirect to new england
  44. if( $count < 9 && $count > 2 && in_array('newengland', $terms) && $region != 'newengland')
  45. {
  46. $link = 'http://newengland.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');
  47. drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);
  48. }
  49. /*END case 1 new england*/
  50.  
  51.  
  52.  
  53. /*case 2*/
  54. 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)
  55. unset($terms['national']); //remove national from array
  56. $value = array_shift($terms) ;//set value to the other term left. in array
  57. if($value == 'los angeles') {//fix for los angelas, because in deli module taxonomy term and subdomain are different only for los angeles
  58. $value = 'la';
  59. }
  60.  
  61. if($region != $value) { //if region does not already = term (would cause redirect loop)
  62.  
  63. $link = 'http://' . $value . '.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');//go to term left in $value
  64.  
  65. drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);//drupal redirect function
  66. }
  67. }
  68.  
  69. /* ++ END case 2++*/
  70.  
  71.  
  72. /* ++case 3 national+++*/
  73. else {
  74.  
  75. 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
  76. $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.
  77. drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301); //go to the $link
  78. }
  79. /* ++ END case 3 national++*/
  80.  
  81.  
  82.  
  83. }
  84. }
  85. break;
  86. }
  87. }
  88.  
  89. function region_filter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  90.  
  91. switch ($op) {
  92.  
  93. case 'view':
  94.  
  95.  
  96. 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.
  97.  
  98. else {
  99.  
  100. $region = getRegion(); //get current region from deli.mmodule
  101.  
  102. $national = 'national'; //store string 'national' in variable
  103.  
  104. $count = count($node->taxonomy);//nstores number of key/value pairs in array, if two terms are selected with the node/post then count == 2
  105.  
  106. $terms = array();//initialize terms to array
  107. foreach($node->taxonomy as $term) {
  108. $terms[$term->name] = $term->name; //prints array of individual terms
  109.  
  110.  
  111.  
  112.  
  113. }
  114.  
  115. /*case 1 new england*/
  116.  
  117. //if there are less than 9 terms and more than 2 and new england is in the array redirect to new england
  118. if( $count < 9 && $count > 2 && in_array('newengland', $terms) && $region != 'newengland')
  119.  
  120. {
  121. $link = 'http://newengland.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');
  122. drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);
  123.  
  124. /*case 2*/
  125. 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)
  126.  
  127. unset($terms['national']); //remove national from array
  128. $value = array_shift($terms) ;//set value to the other term left. in array
  129. if($value == 'los angeles') {//fix for los angelas, because in deli module taxonomy term and subdomain are different only for los angeles
  130.  
  131. $value = 'la';
  132. }
  133.  
  134. if($region != $value) { //if region does not already = term (would cause redirect loop)
  135.  
  136. $link = 'http://' . $value . '.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');//go to term left in $value
  137.  
  138. drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);//drupal redirect function
  139.  
  140. /* ++ END case 2++*/
  141.  
  142.  
  143. /* ++case 3 national+++*/
  144. else {
  145.  
  146. 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
  147. $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.
  148. drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301); //go to the $link
  149. }
  150. /* ++ END case 3 national++*/
  151.  
  152.  
  153.  
  154. }
  155. }
  156. break;
  157. }
  158. }
  159.  
  160. <?php
  161. // return the region that the user should be on for this node
  162. function determine_region($node)
  163. {
  164.  
  165. $taxonomy_count = count($node->taxonomy);
  166.  
  167. // I doubt this is neccesary, I don't know what
  168. // $node->taxonomy is, but perhaps you can use that rather
  169. // then copying the data into an array.
  170. $terms = array();
  171. foreach($node->taxonomy as $term) {
  172. $terms[$term->name] = $term->name;
  173. }
  174.  
  175. // I really have no idea what the logic behind the region is
  176. if($taxonomy_count == 2 && in_array('national', $terms) && is_numeric(arg(1)))
  177. {
  178. $region = array_shift($terms);
  179. // los ageneles needs special case
  180. if($region == "los angeles")
  181. {
  182. $region = 'la';
  183. }
  184. return $region;
  185. }
  186. else if($taxonomy_count > 2 && $taxonomy_count < 9 && in_array('newengland', $terms)
  187. {
  188. return 'newengland';
  189. }
  190. else if($taxonomy_count > 10 && in_array('national', $terms) )
  191. {
  192. return 'national';
  193. }
  194. else
  195. {
  196. // none of the cases fit, we'll just keep the current region
  197. return getRegion();
  198. }
  199.  
  200. }
  201.  
  202. // redirect the user to a particular region and node
  203. function send_redirect($region, $node)
  204. {
  205. $link = 'http://' . $region . '.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');//go to term left in $value
  206. drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);
  207. }
  208.  
  209. function region_filter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  210. // if viewing something other then the home page, consider redirecting
  211. if($op == 'view' && arg(1) != 0)
  212. {
  213. $correct_region = determine_region($node);
  214. // redirect to correct region if necessary.
  215. if( getRegion() != $correct_region )
  216. {
  217. send_redirect($correct_region, $node);
  218. }
  219. }
  220. }
  221. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement