Added the Porter Stemmer module to improve searches. This doesn't deal with some...
[yaffs-website] / web / modules / contrib / front / front_page.module
1 <?php
2
3 /**
4  * @file
5  * This module allows the site admin to set advanced front page settings.
6  *
7  * This version is for Drupal 8.
8  * Earlier versions can be found at http://drupal.org/project/front.
9  *
10  * If you have any ideas/patches or requests,
11  * please post them at http://drupal.org/project/issues/front.
12  */
13
14 use Drupal\Core\Database\Database;
15 use Drupal\Core\Render\Element;
16 use Drupal\Core\Routing\RouteMatchInterface;
17
18 /**
19  * Implements hook_help().
20  */
21 function front_page_help($route_name, RouteMatchInterface $route_match) {
22   switch ($route_name) {
23     case 'front_page.settings':
24       return t('<p>Setup custom front pages for your site.</p>');
25
26     case 'help.page.front_page':
27       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>');
28   }
29 }
30
31 /**
32  * Parse URL including GET and fragment.
33  *
34  * Function to parse a full URL including GET variables and fragment
35  * to an array ready for drupal_goto(), url(), or l() functions.
36  */
37 function front_page_parse_url($path) {
38   $url['path'] = $path;
39   $url['options'] = [];
40   if (preg_match('@^(?P<path>[^?#]+)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$@', $path, $match)) {
41     $url['path'] = $match['path'];
42     if (!empty($match['query'])) {
43       foreach (explode('&', $match['query']) as $query_part) {
44         list($key, $value) = explode('=', $query_part);
45         $url['options']['query'][$key] = $value;
46       }
47     }
48     if (!empty($match['fragment'])) {
49       $url['options']['fragment'] = $match['fragment'];
50     }
51   }
52   return $url;
53 }
54
55 /**
56  * Function to return the first role enabled in front page, ordered by weight.
57  */
58 function front_page_get_by_role($index = 0, $number = 1) {
59   $roles = \Drupal::currentUser()->getRoles();
60   $result = Database::getConnection()->select('front_page', 'fp')
61     ->fields('fp')
62     ->condition('rid', $roles, 'IN')
63     ->condition('mode', '', '<>')
64     ->orderBy('weight', 'ASC')
65     ->orderBy('rid', 'DESC')
66     ->range($index, $number)
67     ->execute()
68     ->fetchAssoc();
69   return $result;
70 }
71
72 /**
73  * Function to return the first role enabled in front page, ordered by weight.
74  */
75 function front_page_get_by_rid($rid) {
76
77   $result = Database::getConnection()->select('front_page', 'fp')
78     ->fields('fp')
79     ->condition('rid', $rid)
80     ->condition('mode', '', '<>')
81     ->execute()
82     ->fetchAssoc();
83   return $result;
84 }
85
86 /**
87  * Function to return all the roles in front page, ordered by weight.
88  */
89 function front_page_get_all() {
90   $result = Database::getConnection()->select('front_page', 'fp')
91     ->fields('fp')
92     ->orderBy('weight', 'ASC')
93     ->orderBy('rid', 'DESC')
94     ->execute()
95     ->fetchAllAssoc('rid', PDO::FETCH_ASSOC);
96   return $result;
97 }
98
99 /**
100  * Implements hook_theme().
101  */
102 function front_page_theme() {
103   return [
104     'front_page_admin_arrange_form' => [
105       'render element' => 'form',
106       'function' => 'theme_front_page_admin_arrange_form',
107     ],
108   ];
109 }
110
111 /**
112  * Implements hook_user_role_delete().
113  */
114 function front_page_user_role_delete($role) {
115   // Delete Front configuration for the role being deleted.
116   Database::getConnection()->delete('front_page')
117     ->condition('rid', $role->rid)
118     ->execute();
119 }
120
121 /**
122  * Returns HTML for the front page arrange form into a table.
123  *
124  * @param array $variables
125  *   An associative array containing:
126  *   - form: A render element representing the form.
127  *
128  * @return string
129  *   Rendered admin form.
130  *
131  * @todo Refactor this function, it's a legacy way.
132  */
133 function theme_front_page_admin_arrange_form($variables) {
134   $form = $variables['form'];
135
136   // Enable the drag handles.
137   drupal_attach_tabledrag($form['roles'], [
138     'table_id' => 'front-page-arrange',
139     'action' => 'order',
140     'relationship' => 'sibling',
141     'group' => 'front-page-weight',
142   ]);
143
144   $header = [
145     t('Role'),
146     t('Mode'),
147     t('Preview'),
148     t('Enabled'),
149     t('Weight'),
150   ];
151
152   $rows = [];
153   $renderer = \Drupal::service('renderer');
154   foreach (Element::children($form['roles']) as $rid) {
155     $element = &$form['roles'][$rid];
156
157     // Add special classes to be used for tabledrag.js.
158     $element['weight']['#attributes']['class'] = ['front-page-weight'];
159
160     $row = [];
161     $row[] = $renderer->render($element['title'], FALSE);
162     $row[] = $renderer->render($element['mode'], FALSE);
163     $row[] = $renderer->render($element['preview'], FALSE);
164     $row[] = $renderer->render($element['enabled'], FALSE);
165     $row[] = $renderer->render($element['weight'], FALSE);
166
167     $row = array_merge(['data' => $row], $element['#attributes']);
168     $row['class'][] = 'draggable';
169     $rows[] = $row;
170   }
171   $output = '';
172   if (empty($rows)) {
173     $rows[] = [['data' => 'no roles', 'colspan' => '5']];
174   }
175
176   $front_page_arrange = [
177     '#theme' => 'table',
178     '#header' => $header,
179     '#rows' => $rows,
180     'attributes' => ['id' => 'front-page-arrange'],
181   ];
182   $output .= $renderer->render($front_page_arrange);
183   $output .= drupal_render_children($form);
184
185   return $output;
186 }