fix drupal_alter() implementations
[project/drop_jobs.git] / drop_jobs.profile
1 <?php
2 /**
3 * @file
4 * Drop Jobs profile.
5 */
6
7 /**
8 * Implements hook_admin_paths().
9 *
10 * Add all user-related pages to admin paths, which will make them use the
11 * admin theme, given the user has that permission.
12 */
13 function drop_jobs_admin_paths() {
14 return array(
15 'user' => TRUE,
16 'user/*' => TRUE,
17 );
18 }
19
20 /**
21 * Implements hook_admin_paths_alter().
22 *
23 * A trick to enforce page refresh when theme is changed from an overlay.
24 */
25 function drop_jobs_admin_paths_alter(&$paths) {
26 $paths['admin/appearance/default*'] = FALSE;
27 }
28
29 /**
30 * Set Drop Jobs as default install profile.
31 *
32 * Must use system as the hook module because drop_jobs is not active yet
33 */
34 function system_form_install_select_profile_form_alter(&$form, $form_state) {
35 foreach($form['profile'] as $key => $element) {
36 $form['profile'][$key]['#value'] = 'drop_jobs';
37 }
38 }
39
40 /**
41 * Implements hook_form_FORM_ID_alter().
42 *
43 * Alter the site configuration form.
44 */
45 function drop_jobs_form_install_configure_form_alter(&$form, $form_state) {
46 // Many modules set messages during installation that are very annoying.
47 // Yeah, we're looking at you Date and iToggle.
48 // Let's clear these messages to avoid the false impression that something
49 // went wrong when it didn't.
50 drupal_get_messages('status');
51 drupal_get_messages('warning');
52
53 drupal_add_css(drupal_get_path('profile', 'drop_jobs') . '/drop_jobs_install.css');
54
55 // Warn about settings.php permissions risk
56 $settings_dir = conf_path();
57 $settings_file = $settings_dir . '/settings.php';
58 // Check that $_POST is empty so we only show this message when the form is
59 // first displayed, not on the next page after it is submitted. (We do not
60 // want to repeat it multiple times because it is a general warning that is
61 // not related to the rest of the installation process; it would also be
62 // especially out of place on the last page of the installer, where it would
63 // distract from the message that the Drupal installation has completed
64 // successfully.)
65 if (empty($_POST) && (!drupal_verify_install_file(DRUPAL_ROOT . '/' . $settings_file, FILE_EXIST | FILE_READABLE | FILE_NOT_WRITABLE) || !drupal_verify_install_file(DRUPAL_ROOT . '/' . $settings_dir, FILE_NOT_WRITABLE, 'dir'))) {
66 drupal_set_message(st('All necessary changes to %dir and %file have been made, so you should remove write permissions to them now in order to avoid security risks. If you are unsure how to do so, consult the <a href="@handbook_url">online handbook</a>.', array('%dir' => $settings_dir, '%file' => $settings_file, '@handbook_url' => 'http://drupal.org/server-permissions')), 'warning');
67 }
68
69 // Pre-populate some fields.
70 $form['site_information']['site_name']['#default_value'] = 'Drop Jobs'; // We don't use t() intentionally.
71 $form['site_information']['site_mail']['#default_value'] = 'admin@' . $_SERVER['HTTP_HOST'];
72 $form['admin_account']['account']['name']['#default_value'] = 'admin';
73 $form['admin_account']['account']['mail']['#default_value'] = 'admin@' . $_SERVER['HTTP_HOST'];
74
75 // Add checkbox for example content.
76 $form['drop_jobs'] = array(
77 '#type' => 'fieldset',
78 '#collapsible' => FALSE,
79 '#title' => t('Drop Jobs'),
80 );
81
82 $form['drop_jobs']['drop_jobs_demo_content'] = array(
83 '#type' => 'checkbox',
84 '#title' => t('Install demo content'),
85 '#description' => t('Check this option to enable demonstration content for Drop Jobs to get your site up and running quickly.'),
86 '#default_value' => TRUE,
87 );
88
89 // Add select box for search provider.
90 $form['drop_jobs']['drop_jobs_search_provider'] = array(
91 '#type' => 'select',
92 '#title' => t('Search provider'),
93 '#description' => t("Apache Solr is recommended but if you don't have it available, things will break!. You might be better off installing with Database search, getting a Solr instance up and running and then switching to Solr search later using the Drop Jobs settings page."),
94 '#options' => drop_jobs_get_search_providers(),
95 '#default_value' => 'drop_jobs_solr',
96 );
97
98 // Add checkboxes for submodules.
99 $form['drop_jobs']['drop_jobs_features'] = array(
100 '#type' => 'fieldset',
101 '#title' => t('Features'),
102 '#description' => t('Enable additional Drop Jobs Features'),
103 '#collapsible' => FALSE,
104 '#tree' => TRUE,
105 );
106
107 // Exclude core features.
108 $exclude = drop_jobs_core_features();
109
110 // Demo module gets special treatment so exclude it from this list.
111 $exclude[] = 'drop_jobs_demo';
112
113 // These are not so commonly used and get disabled by default.
114 $disabled = array(
115 'drop_jobs_dev',
116 );
117
118 // Add feature form items.
119 foreach (drop_jobs_get_features() as $module => $info) {
120 // Exclude certain modules.
121 if (!in_array($module, $exclude)) {
122 $form['drop_jobs']['drop_jobs_features'][$module] = array(
123 '#type' => 'checkbox',
124 '#title' => $info->info['name'],
125 '#description' => $info->info['description'],
126 '#default_value' => !in_array($module, $disabled),
127 );
128 }
129 }
130
131 $form['#submit'][] = 'drop_jobs_install_configure_form_submit';
132 }
133
134 /**
135 * Submit callback.
136 *
137 * Installs Drop Jobs demonstration content.
138 */
139 function drop_jobs_install_configure_form_submit(&$form, &$form_state) {
140 // Alias for convenience.
141 $values =& $form_state['values'];
142
143 // Enable additional modules.
144 if ($values['drop_jobs_features']) {
145 $modules = array();
146
147 foreach ($values['drop_jobs_features'] as $module => $modulesd) {
148 if ($modulesd) {
149 $modules[] = $module;
150 }
151 }
152 }
153
154 // Enable search provider.
155 $modules[] = $form_state['values']['drop_jobs_search_provider'];
156
157 // Set variable to enable additional modules.
158 variable_set('drop_jobs_install_extra_modules', $modules);
159
160 // Set variable to install or not demo content.
161 variable_set('drop_jobs_install_demo_content', $values['drop_jobs_demo_content']);
162
163 // For whatever reason we can't rely on roles always being created in the
164 // same order and receiving the same IDs, therefore find the role IDs and set
165 // them as variables we'll use throughout.
166 $employer_rid = reset(
167 db_select('role', 'r')
168 ->fields('r', array('rid'))
169 ->condition('name', DROP_JOBS_TYPE_EMPLOYER, '=')
170 ->execute()
171 ->fetchCol());
172 variable_set('drop_jobs_role_employer', $employer_rid);
173
174 $candidate_rid = reset(
175 db_select('role', 'r')
176 ->fields('r', array('rid'))
177 ->condition('name', DROP_JOBS_TYPE_CANDIDATE, '=')
178 ->execute()
179 ->fetchCol());
180 variable_set('drop_jobs_role_candidate', $candidate_rid);
181
182 $admin_rid = reset(
183 db_select('role', 'r')
184 ->fields('r', array('rid'))
185 ->condition('name', 'administrator', '=')
186 ->execute()
187 ->fetchCol());
188 variable_set('drop_jobs_role_admin', $admin_rid);
189
190 $webmaster_rid = reset(
191 db_select('role', 'r')
192 ->fields('r', array('rid'))
193 ->condition('name', 'webmaster', '=')
194 ->execute()
195 ->fetchCol());
196 variable_set('drop_jobs_role_webmaster', $webmaster_rid);
197 }
198
199 /**
200 * Implements hook_block_info().
201 */
202 function drop_jobs_block_info() {
203 return array(
204 'powered-by' => array(
205 'info' => t('Powered by Drop Jobs'),
206 'weight' => '10',
207 'cache' => DRUPAL_CACHE_GLOBAL,
208 ),
209 );
210 }
211
212 /**
213 * Implements hook_block_view().
214 */
215 function drop_jobs_block_view($delta = '') {
216 if ($delta === 'powered-by') {
217 return array(
218 'subject' => NULL,
219 'content' => '<span>' . t('Powered by <a href="@drop_jobs">Drop Jobs</a>, a <a href="@drupal">Drupal</a> distribution by <a href="@alex">Alex</a> and <a href="@friends">friends</a>.', array('@drop_jobs' => 'http://drupal.org/project/drop_jobs', '@drupal' => 'http://drupal.org', '@alex' => 'http://alexweber.com.br', '@friends' => 'http://groups.drupal.org/drop-jobs-distribution')) . '</span>',
220 );
221 }
222 }
223
224 /**
225 * Returns an array of Drop Jobs modules (features) that are considered "core".
226 *
227 * @return array
228 * An array of the core modules' machine names.
229 */
230 function drop_jobs_core_features() {
231 $core_features = array(
232 'drop_jobs_admin',
233 'drop_jobs_core',
234 'drop_jobs_candidate',
235 'drop_jobs_employer',
236 'drop_jobs_job',
237 'drop_jobs_organization',
238 );
239
240 // Allow list of core features to be altered.
241 drupal_alter('drop_jobs_core_features', $core_features);
242
243 return $core_features;
244 }
245
246 /**
247 * Returns an array of Drop Jobs "Features" that can be turned on or off.
248 * These are Drop Jobs submodules, excluding those considered to be "core"
249 * by Drop Jobs or excluded by other modules.
250 *
251 * @return array
252 * An array of available modules, keyed by machine name.
253 */
254 function drop_jobs_get_features() {
255 $core_features = drop_jobs_core_features();
256
257 // Add search providers to list of core features.
258 $search_providers = array_keys(drop_jobs_get_search_providers());
259 $filtered_features = array_merge($core_features, $search_providers);
260
261 // Build list of features.
262 $features = array();
263
264 foreach (system_rebuild_module_data() as $module => $info) {
265 if (strpos($module, 'drop_jobs_') === 0 && !in_array($module, $filtered_features)) {
266 $features[$module] = $info;
267 }
268 }
269
270 return $features;
271 }
272
273 /**
274 * Returns a list of Drop Jobs Search providers that can be used.
275 *
276 * @return array
277 * An array of available module names.
278 */
279 function drop_jobs_get_search_providers() {
280 $search_providers = &drupal_static(__FUNCTION__, NULL);
281
282 if (!isset($search_providers)) {
283 // Hard-coded because these are part of Drop Jobs Core.
284 $search_providers = array(
285 'drop_jobs_db' => t('Database search'),
286 'drop_jobs_solr' => t('Apache Solr'),
287 );
288
289 // Allow other modules to change this.
290 drupal_alter('drop_jobs_search_providers', $search_providers);
291 }
292
293 return $search_providers;
294 }
295
296 /**
297 * Returns the active Drop Jobs Search provider.
298 */
299 function drop_jobs_search_provider() {
300 $providers = drop_jobs_get_search_providers();
301 $modules = module_list();
302 return array_intersect($modules, array_keys($providers));
303 }
304
305 /**
306 * Implements hook_form_FORMID_alter().
307 *
308 * Adds a checkbox for controlling field view access to certain fields added to
309 * profiles and job nodes.
310 */
311 function drop_jobs_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
312 if ($form['instance']['entity_type']['#value'] == 'profile2' || ($form['instance']['entity_type']['#value'] === 'node' && $form['instance']['bundle']['#value'] === 'job')) {
313 // Add hidden field option.
314 $form['field']['settings']['drop_jobs_hidden'] = array(
315 '#type' => 'checkbox',
316 '#title' => t('Make the content of this field hidden.'),
317 '#default_value' => !empty($form['#field']['settings']['drop_jobs_hidden']),
318 '#description' => t('If checked, the content of this field is only shown to administrators.'),
319 );
320
321 // Change Profile2 private field description.
322 if (isset($form['field']['settings']['profile2_private'])) {
323 $form['field']['settings']['profile2_private']['#description'] = t('If checked, the content of this field is only shown to the profile owner, administrators and employers with active subscriptions.');
324 }
325 }
326 else {
327 // Add the value to the form so it isn't lost.
328 $form['field']['settings']['drop_jobs_hidden'] = array(
329 '#type' => 'value',
330 '#value' => !empty($form['#field']['settings']['drop_jobs_hidden']),
331 );
332 }
333 }
334
335 /**
336 * Implements hook_field_access().
337 */
338 function drop_jobs_field_access($op, $field, $entity_type, $entity = NULL, $account = NULL) {
339 if ($op == 'view'
340 && ($entity_type == 'profile2'
341 || ($entity_type === 'node' && isset($entity)
342 && $entity->type === 'job'))
343 && !empty($field['settings']['drop_jobs_hidden'])
344 && !user_access('administer drop_jobs', $account)) {
345 return FALSE;
346 }
347 }
348
349 /**
350 * Determine whether a user has a particular role.
351 * If no user is specified we use the current user.
352 *
353 * @param mixed
354 * The role, can be either the numeric id or the machine name.
355 * @param int
356 * The user, if none is specified checks against the current user.
357 * @return boolean
358 * TRUE if the user has that role, FALSE otherwise.
359 */
360 function drop_jobs_user_has_role($rid, $uid = NULL) {
361 $user = user_uid_optional_load($uid);
362
363 // Make sure we have a valid user object.
364 if (is_object($user) && isset($user->roles) && is_array($user->roles)) {
365 return (is_numeric($rid)) ? in_array($rid, array_keys($user->roles)) : in_array($rid, $user->roles);
366 }
367
368 return FALSE;
369 }