summaryrefslogtreecommitdiffstats
path: root/drop_jobs.profile
blob: e6423f15c66668cd2cbb8d66f56349f4302d35c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
 * @file
 * Drop Jobs profile.
 */

/**
 * Implements hook_admin_paths().
 *
 * Add all user-related pages to admin paths, which will make them use the
 * admin theme, given the user has that permission.
 */
function drop_jobs_admin_paths() {
  return array(
    'user' => TRUE,
    'user/*' => TRUE,
  );
}

/**
 * Implements hook_admin_paths_alter().
 *
 * A trick to enforce page refresh when theme is changed from an overlay.
 */
function drop_jobs_admin_paths_alter(&$paths) {
  $paths['admin/appearance/default*'] = FALSE;
}

/**
 * Set Drop Jobs as default install profile.
 *
 * Must use system as the hook module because drop_jobs is not active yet
 */
function system_form_install_select_profile_form_alter(&$form, $form_state) {
  foreach($form['profile'] as $key => $element) {
    $form['profile'][$key]['#value'] = 'drop_jobs';
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alter the site configuration form.
 */
function drop_jobs_form_install_configure_form_alter(&$form, $form_state) {
  // Many modules set messages during installation that are very annoying.
  // Yeah, we're looking at you Date and iToggle.
  // Let's clear these messages to avoid the false impression that something
  // went wrong when it didn't.
  drupal_get_messages('status');
  drupal_get_messages('warning');

  drupal_add_css(drupal_get_path('profile', 'drop_jobs') . '/drop_jobs_install.css');

  // Warn about settings.php permissions risk
  $settings_dir = conf_path();
  $settings_file = $settings_dir . '/settings.php';
  // Check that $_POST is empty so we only show this message when the form is
  // first displayed, not on the next page after it is submitted. (We do not
  // want to repeat it multiple times because it is a general warning that is
  // not related to the rest of the installation process; it would also be
  // especially out of place on the last page of the installer, where it would
  // distract from the message that the Drupal installation has completed
  // successfully.)
  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'))) {
    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');
  }

  // Pre-populate some fields.
  $form['site_information']['site_name']['#default_value'] = 'Drop Jobs'; // We don't use t() intentionally.
  $form['site_information']['site_mail']['#default_value'] = 'admin@' . $_SERVER['HTTP_HOST'];
  $form['admin_account']['account']['name']['#default_value'] = 'admin';
  $form['admin_account']['account']['mail']['#default_value'] = 'admin@' . $_SERVER['HTTP_HOST'];

  // Add checkbox for example content.
  $form['drop_jobs'] = array(
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#title' => t('Drop Jobs'),
  );

  $form['drop_jobs']['drop_jobs_demo_content'] = array(
    '#type' => 'checkbox',
    '#title' => t('Install demo content'),
    '#description' => t('Check this option to enable demonstration content for Drop Jobs to get your site up and running quickly.'),
    '#default_value' => TRUE,
  );

  // Add select box for search provider.
  $form['drop_jobs']['drop_jobs_search_provider'] = array(
    '#type' => 'select',
    '#title' => t('Search provider'),
    '#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."),
    '#options' => drop_jobs_get_search_providers(),
    '#default_value' => 'drop_jobs_solr',
  );

  // Add checkboxes for submodules.
  $form['drop_jobs']['drop_jobs_features'] = array(
    '#type' => 'fieldset',
    '#title' => t('Features'),
    '#description' => t('Enable additional Drop Jobs Features'),
    '#collapsible' => FALSE,
    '#tree' => TRUE,
  );

  // Exclude core features.
  $exclude = drop_jobs_core_features();

  // Demo module gets special treatment so exclude it from this list.
  $exclude[] = 'drop_jobs_demo';

  // These are not so commonly used and get disabled by default.
  $disabled = array(
    'drop_jobs_dev',
  );

  // Add feature form items.
  foreach (drop_jobs_get_features() as $module => $info) {
    // Exclude certain modules.
    if (!in_array($module, $exclude)) {
      $form['drop_jobs']['drop_jobs_features'][$module] = array(
        '#type' => 'checkbox',
        '#title' => $info->info['name'],
        '#description' => $info->info['description'],
        '#default_value' => !in_array($module, $disabled),
      );
    }
  }

  $form['#submit'][] = 'drop_jobs_install_configure_form_submit';
}

/**
 * Submit callback.
 *
 * Installs Drop Jobs demonstration content.
 */
function drop_jobs_install_configure_form_submit(&$form, &$form_state) {
  // Alias for convenience.
  $values =& $form_state['values'];

  // Enable additional modules.
  if ($values['drop_jobs_features']) {
    $modules = array();

    foreach ($values['drop_jobs_features'] as $module => $modulesd) {
      if ($modulesd) {
        $modules[] = $module;
      }
    }
  }

  // Enable search provider.
  $modules[] = $form_state['values']['drop_jobs_search_provider'];

  // Set variable to enable additional modules.
  variable_set('drop_jobs_install_extra_modules', $modules);

  // Set variable to install or not demo content.
  variable_set('drop_jobs_install_demo_content', $values['drop_jobs_demo_content']);

  // For whatever reason we can't rely on roles always being created in the
  // same order and receiving the same IDs, therefore find the role IDs and set
  // them as variables we'll use throughout.
  $employer_rid = reset(
    db_select('role', 'r')
    ->fields('r', array('rid'))
    ->condition('name', DROP_JOBS_TYPE_EMPLOYER, '=')
    ->execute()
    ->fetchCol());
  variable_set('drop_jobs_role_employer', $employer_rid);

  $candidate_rid = reset(
    db_select('role', 'r')
    ->fields('r', array('rid'))
    ->condition('name', DROP_JOBS_TYPE_CANDIDATE, '=')
    ->execute()
    ->fetchCol());
  variable_set('drop_jobs_role_candidate', $candidate_rid);

  $admin_rid = reset(
    db_select('role', 'r')
    ->fields('r', array('rid'))
    ->condition('name', 'administrator', '=')
    ->execute()
    ->fetchCol());
  variable_set('drop_jobs_role_admin', $admin_rid);

  $webmaster_rid = reset(
    db_select('role', 'r')
    ->fields('r', array('rid'))
    ->condition('name', 'webmaster', '=')
    ->execute()
    ->fetchCol());
  variable_set('drop_jobs_role_webmaster', $webmaster_rid);
}

/**
 * Implements hook_block_info().
 */
function drop_jobs_block_info() {
  return array(
    'powered-by' => array(
      'info' => t('Powered by Drop Jobs'),
      'weight' => '10',
      'cache' => DRUPAL_CACHE_GLOBAL,
    ),
  );
}

/**
 * Implements hook_block_view().
 */
function drop_jobs_block_view($delta = '') {
  if ($delta === 'powered-by') {
    return array(
      'subject' => NULL,
      '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>',
    );
  }
}

/**
 * Returns an array of Drop Jobs modules (features) that are considered "core".
 *
 * @return array
 *   An array of the core modules' machine names.
 */
function drop_jobs_core_features() {
  $core_features = array(
    'drop_jobs_admin',
    'drop_jobs_core',
    'drop_jobs_candidate',
    'drop_jobs_employer',
    'drop_jobs_job',
    'drop_jobs_organization',
  );

  // Allow list of core features to be altered.
  drupal_alter('drop_jobs_core_features', $core_features);

  return $core_features;
}

/**
 * Returns an array of Drop Jobs "Features" that can be turned on or off.
 * These are Drop Jobs submodules, excluding those considered to be "core"
 * by Drop Jobs or excluded by other modules.
 *
 * @return array
 *   An array of available modules, keyed by machine name.
 */
function drop_jobs_get_features() {
  $core_features = drop_jobs_core_features();

  // Add search providers to list of core features.
  $search_providers = array_keys(drop_jobs_get_search_providers());
  $filtered_features = array_merge($core_features, $search_providers);

  // Build list of features.
  $features = array();

  foreach (system_rebuild_module_data() as $module => $info) {
    if (strpos($module, 'drop_jobs_') === 0 && !in_array($module, $filtered_features)) {
      $features[$module] = $info;
    }
  }

  return $features;
}

/**
 * Returns a list of Drop Jobs Search providers that can be used.
 *
 * @return array
 *   An array of available module names.
 */
function drop_jobs_get_search_providers() {
  $search_providers = &drupal_static(__FUNCTION__, NULL);

  if (!isset($search_providers)) {
    // Hard-coded because these are part of Drop Jobs Core.
    $search_providers = array(
      'drop_jobs_db' => t('Database search'),
      'drop_jobs_solr' => t('Apache Solr'),
    );

    // Allow other modules to change this.
    drupal_alter('drop_jobs_search_providers', $search_providers);
  }

  return $search_providers;
}

/**
 * Returns the active Drop Jobs Search provider.
 */
function drop_jobs_search_provider() {
  $providers = drop_jobs_get_search_providers();
  $modules = module_list();
  return array_intersect($modules, array_keys($providers));
}

/**
 * Implements hook_form_FORMID_alter().
 *
 * Adds a checkbox for controlling field view access to certain fields added to
 * profiles and job nodes.
 */
function drop_jobs_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  if ($form['instance']['entity_type']['#value'] == 'profile2' || ($form['instance']['entity_type']['#value'] === 'node' && $form['instance']['bundle']['#value'] === 'job')) {
    // Add hidden field option.
    $form['field']['settings']['drop_jobs_hidden'] = array(
      '#type' => 'checkbox',
      '#title' => t('Make the content of this field hidden.'),
      '#default_value' => !empty($form['#field']['settings']['drop_jobs_hidden']),
      '#description' => t('If checked, the content of this field is only shown to administrators.'),
    );

    // Change Profile2 private field description.
    if (isset($form['field']['settings']['profile2_private'])) {
      $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.');
    }
  }
  else {
    // Add the value to the form so it isn't lost.
    $form['field']['settings']['drop_jobs_hidden'] = array(
      '#type' => 'value',
      '#value' => !empty($form['#field']['settings']['drop_jobs_hidden']),
    );
  }
}

/**
 * Implements hook_field_access().
 */
function drop_jobs_field_access($op, $field, $entity_type, $entity = NULL, $account = NULL) {
  if ($op == 'view'
    && ($entity_type == 'profile2'
      || ($entity_type === 'node' && isset($entity)
      && $entity->type === 'job'))
    && !empty($field['settings']['drop_jobs_hidden'])
    && !user_access('administer drop_jobs', $account)) {
    return FALSE;
  }
}

/**
 * Determine whether a user has a particular role.
 * If no user is specified we use the current user.
 *
 * @param mixed
 *   The role, can be either the numeric id or the machine name.
 * @param int
 *   The user, if none is specified checks against the current user.
 * @return boolean
 *   TRUE if the user has that role, FALSE otherwise.
 */
function drop_jobs_user_has_role($rid, $uid = NULL) {
  $user = user_uid_optional_load($uid);

  // Make sure we have a valid user object.
  if (is_object($user) && isset($user->roles) && is_array($user->roles)) {
    return (is_numeric($rid)) ? in_array($rid, array_keys($user->roles)) : in_array($rid, $user->roles);
  }

  return FALSE;
}