front_page.module 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. *
  4. * This module allows the site admin to set advanced front page settings.
  5. *
  6. * This version is for Drupal 7. Earlier versions can be found at http://drupal.org/project/front.
  7. *
  8. * This module version was developed by timhilliard and various members of the drupal community.
  9. *
  10. * If you have any ideas/patches or requests, please post them at http://drupal.org/project/issues/front.
  11. *
  12. */
  13. /**
  14. * Implements hook_help().
  15. */
  16. function front_page_help($section) {
  17. switch ($section) {
  18. case 'admin/config/front/settings':
  19. return t('<p>Setup custom front pages for your site.</p>');
  20. case 'admin/config/front/arrange':
  21. return t('<p>Arrange the order in which roles will be checked for custom front page settings. Roles will be processed from top to bottom. To enable other roles you must first enable them in the !link.</p>', array('!link' => l(t('Settings tab'), 'admin/config/front/settings')));
  22. case 'admin/config/front/home-links':
  23. return t('<p>If a HOME link is set, the &lt;front&gt; placeholder will be replaced with this value instead of the standard front page.</p>');
  24. }
  25. }
  26. /**
  27. * Implements hook_menu().
  28. */
  29. function front_page_menu() {
  30. // Front page settings.
  31. $items['admin/config/front'] = array(
  32. 'title' => 'Front Page',
  33. 'description' => 'Configure front page.',
  34. 'position' => 'right',
  35. 'weight' => -15,
  36. 'page callback' => 'system_admin_menu_block_page',
  37. 'access arguments' => array('administer front page'),
  38. 'file' => 'system.admin.inc',
  39. 'file path' => drupal_get_path('module', 'system'),
  40. );
  41. $items['admin/config/front/settings'] = array(
  42. 'title' => 'Settings',
  43. 'description' => 'Administer custom front page settings.',
  44. 'weight' => 0,
  45. 'page callback' => 'drupal_get_form',
  46. 'page arguments' => array('front_page_admin'),
  47. 'access arguments' => array('administer front page'),
  48. 'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
  49. 'file' => 'front_page.admin.inc',
  50. );
  51. $items['admin/config/front/arrange'] = array(
  52. 'title' => 'Arrange',
  53. 'description' => 'Ability to re-arrange what order front page roles are processed.',
  54. 'page callback' => 'drupal_get_form',
  55. 'page arguments' => array('front_page_admin_arrange_form'),
  56. 'access arguments' => array('administer front page'),
  57. 'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
  58. 'file' => 'front_page.admin.inc',
  59. 'weight' => 1,
  60. );
  61. $items['admin/config/front/home-links'] = array(
  62. 'title' => 'Home links',
  63. 'description' => 'Allows you to change the location of the &lt;front&gt; placeholder.',
  64. 'page callback' => 'drupal_get_form',
  65. 'page arguments' => array('front_page_admin_home_links'),
  66. 'access arguments' => array('administer front page'),
  67. 'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
  68. 'file' => 'front_page.admin.inc',
  69. 'weight' => 2,
  70. );
  71. $items['front_page'] = array(
  72. 'title' => '',
  73. 'page callback' => 'front_page',
  74. 'access callback' => TRUE,
  75. 'type' => MENU_SUGGESTED_ITEM,
  76. );
  77. return $items;
  78. }
  79. /**
  80. * Implements hook_permission().
  81. */
  82. function front_page_permission() {
  83. return array(
  84. 'administer front page' => array(
  85. 'title' => t('Administer front page'),
  86. 'description' => t('Allow users to make changes to the front page settings.'),
  87. ),
  88. );
  89. }
  90. /**
  91. * Implements hook_init().
  92. */
  93. function front_page_init() {
  94. // Make sure front page module is not run when using cli (drush).
  95. // Make sur front page module does not run when installing Drupal either.
  96. if (drupal_is_cli() || drupal_installation_attempted()) {
  97. return;
  98. }
  99. // Don't run when site is in maintenance mode
  100. if (variable_get('maintenance_mode', 0)) {
  101. return;
  102. }
  103. // Ignore non index.php requests (like cron)
  104. if (!empty($_SERVER['SCRIPT_FILENAME']) && realpath(DRUPAL_ROOT . '/index.php') != realpath($_SERVER['SCRIPT_FILENAME'])) {
  105. return;
  106. }
  107. global $_front_page, $conf;
  108. // let administrator know that there is a config error.
  109. if (variable_get('site_frontpage', '') == 'front_page' && user_access('administer menu')) {
  110. drupal_set_message(t('There is a configuration error. The home page should not be set to the path "front_page". Please change this !link', array('!link' => l(t('here'), 'admin/config/system/site-information'))), 'error');
  111. }
  112. if (variable_get('front_page_enable', 0) && drupal_is_front_page()) {
  113. $_front_page = front_page_get_by_role();
  114. }
  115. if (user_access('administer menu') && preg_match('@^front_page/preview/([0-9]+)$@', $_GET['q'], $match)) {
  116. $_front_page = front_page_get_by_rid($match[1]);
  117. }
  118. if ($_front_page) {
  119. switch ($_front_page['mode']) {
  120. case 'themed':
  121. case 'full':
  122. $_GET['q'] = 'front_page';
  123. // need to set variable site_frontpage to current path so that it thinks it is the front page.
  124. $conf['site_frontpage'] = $_GET['q'];
  125. break;
  126. case 'redirect':
  127. $url = front_page_parse_url($_front_page['data']);
  128. drupal_goto($url['path'], $url['options']);
  129. break;
  130. case 'alias':
  131. $url = front_page_parse_url($_front_page['data']);
  132. $_GET['q'] = drupal_get_normal_path($url['path']);
  133. // need to set variable site_frontpage to current path so that it thinks it is the front page.
  134. $conf['site_frontpage'] = $_GET['q'];
  135. break;
  136. }
  137. // turn caching off for this page as it is dependant on role.
  138. $GLOBALS['conf']['cache'] = FALSE;
  139. }
  140. }
  141. /**
  142. * Function to handle the display of the front page themed and full types.
  143. */
  144. function front_page() {
  145. // $_front_page variable should already have been loaded in front_page_init() function.
  146. global $_front_page;
  147. if ($_front_page) {
  148. switch ($_front_page['mode']) {
  149. case 'themed':
  150. return check_markup($_front_page['data'], $_front_page['filter_format']);
  151. case 'full':
  152. print check_markup($_front_page['data'], $_front_page['filter_format']);
  153. exit;
  154. }
  155. }
  156. // set page not found as there was no themed or full option set for the front page.
  157. drupal_not_found();
  158. exit;
  159. }
  160. /**
  161. * Function to parse a full URL including GET variables and fragment
  162. * to an array ready for drupal_goto(), url(), or l() functions.
  163. */
  164. function front_page_parse_url($path) {
  165. $url['path'] = $path;
  166. $url['options'] = array();
  167. if (preg_match('@^(?P<path>[^?#]+)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$@', $path, $match)) {
  168. $url['path'] = $match['path'];
  169. if (!empty($match['query'])) {
  170. foreach (explode('&', $match['query']) as $query_part) {
  171. list($key, $value) = explode('=', $query_part);
  172. $url['options']['query'][$key] = $value;
  173. }
  174. }
  175. if (!empty($match['fragment'])) {
  176. $url['options']['fragment'] = $match['fragment'];
  177. }
  178. }
  179. return $url;
  180. }
  181. /**
  182. * Function to return the first role enabled in front page, ordered by weight.
  183. */
  184. function front_page_get_by_role($index = 0, $number = 1) {
  185. global $user;
  186. $result = db_select('front_page', 'fp')
  187. ->fields('fp')
  188. ->condition('rid', array_keys($user->roles))
  189. ->condition('mode', '', '<>')
  190. ->orderBy('weight', 'ASC')
  191. ->orderBy('rid', 'DESC')
  192. ->range($index, $number)
  193. ->execute()
  194. ->fetchAssoc();
  195. return $result;
  196. }
  197. /**
  198. * Function to return the first role enabled in front page, ordered by weight.
  199. */
  200. function front_page_get_by_rid($rid) {
  201. $result = db_select('front_page', 'fp')
  202. ->fields('fp')
  203. ->condition('rid', $rid)
  204. ->condition('mode', '', '<>')
  205. ->execute()
  206. ->fetchAssoc();
  207. return $result;
  208. }
  209. /**
  210. * Function to return all the roles in front page, ordered by weight.
  211. */
  212. function front_page_get_all() {
  213. global $user;
  214. $result = db_select('front_page', 'fp')
  215. ->fields('fp')
  216. ->orderBy('weight', 'ASC')
  217. ->orderBy('rid', 'DESC')
  218. ->execute()
  219. ->fetchAllAssoc('rid', PDO::FETCH_ASSOC);
  220. return $result;
  221. }
  222. /**
  223. * Implements hook_page_url_outbound_alter().
  224. *
  225. * This alters anything parsed through the url() function to replace the <front> string
  226. * with an alternative string instead of the site_frontpage setting.
  227. */
  228. function front_page_url_outbound_alter(&$path, &$options, &$original_path) {
  229. // check if path is set to 'front_page' as this is our virtual home
  230. // page and doesn't work properly unless used from home page.
  231. if ($path == 'front_page') {
  232. $original_path = $path = '';
  233. }
  234. $newpath = variable_get('front_page_home_link_path', '');
  235. if (($path == '<front>' || empty($path)) && !empty($newpath)) {
  236. $original_path = $path = $newpath;
  237. }
  238. }
  239. /**
  240. * Implements hook_theme().
  241. */
  242. function front_page_theme() {
  243. return array(
  244. 'front_page_admin_arrange_form' => array(
  245. 'file' => 'front_page.admin.inc',
  246. 'render element' => 'form',
  247. ),
  248. );
  249. }
  250. /**
  251. * Implements hook_user_role_delete().
  252. */
  253. function front_page_user_role_delete($role) {
  254. // Delete Front configuration for the role being deleted.
  255. db_delete('front_page')
  256. ->condition('rid', $role->rid)
  257. ->execute();
  258. }