ats-1.0.x-dev/ats.install
ats.install
<?php
/**
* @file
* Contains install and update functions for ATS module.
*/
use Drupal\taxonomy\Entity\Term;
/**
* Add terms to the ATS vocabularies.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function ats_install($is_syncing) {
module_set_weight('ats', 1);
if (!$is_syncing) {
$vocabularies = get_vocabularies();
foreach ($vocabularies as $vid) {
foreach (get_terms_by_vid($vid) as $name => $parent_term) {
if (is_array($parent_term)) {
// Save parent term.
create_term($vid, $name);
$parent_id = \Drupal::entityQuery('taxonomy_term')
->condition('vid', $vid)
->condition('name', $name)
->execute();
foreach ($parent_term as $child_term) {
// Save child terms.
create_term($vid, $child_term, key($parent_id));
}
}
else {
create_term($vid, $parent_term);
}
}
}
}
}
/**
* Get vocabulary machine names.
*
* @return array
* Vocabulary machine names.
*/
function get_vocabularies(): array {
return [
'ats_candidate_source',
'ats_candidate_status',
'ats_employee_type',
'ats_candidate_experience',
'ats_job_status',
'ats_job_type',
'ats_job_priority',
'ats_job_benefits',
'ats_job_category',
'ats_job_work_mode',
'ats_job_degree',
];
}
/**
* Get taxonomy terms by vocabulary name.
*
* @param string $vocabulary_name
* The vocabulary name.
*
* @return array
* Vocabulary terms.
*/
function get_terms_by_vid(string $vocabulary_name): array {
$terms = [
'ats_candidate_source' => [
'Referral' => [
'Linkedin Network',
'Facebook Network',
'Internal Referral',
],
'Job Board' => [
'Esteemed',
'Monster',
'Career Builder',
'Dice',
'Linkedin',
'Facebook',
'Indeed',
'Other (offer field to list)',
],
'Event' => [],
],
'ats_candidate_status' => [
'New lead',
'Active Employee',
'Former Employee',
'Interviewing',
'DNU',
'Archive',
],
'ats_employee_type' => [
'W2',
'1099',
'Corp-to-Corp',
],
'ats_candidate_experience' => [
'Intern (0-1 year)',
'Junior (1-3 years)',
'Mid-level (4-6 years)',
'Senior (7+ years)',
'Executive',
],
'ats_job_status' => [
'Accepting Candidates',
'Covered',
'Offer Out',
'Placed',
'Filled',
'Lost',
'Archive',
],
'ats_job_type' => [
'Direct hire',
'Contract',
'Contract to hire',
'Internship',
],
'ats_job_priority' => [
'Hot',
'Warm',
'Cold',
],
'ats_job_benefits' => [
'Dental',
'Healthcare',
'Vision',
'Life Insurance',
'Stock Options',
'401k',
'Tuition Reimbursement',
],
'ats_job_category' => [
'Administration',
'Operations / HR',
'Finance',
'Technology',
'Production',
'Management',
'Marketing',
'Public Relations',
'Research and Development',
],
'ats_job_work_mode' => [
'100% remote' => [],
'100% on-site' => [],
'Hybrid' => [
'Hybrid (occasional on-site meetings)',
'Hybrid (25% on-site)',
'Hybrid (50% on-site)',
'Hybrid (75% on-site)',
],
],
'ats_job_degree' => [
'Associates',
'Bachelors',
'Masters',
'Doctorate',
],
];
return $terms[$vocabulary_name];
}
/**
* Save term.
*
* @param string $vocabulary
* The vocabulary name.
* @param string $term
* The term name.
* @param string $parent
* (Optional) The parent term name.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function create_term(string $vocabulary, string $term, string $parent = '') {
Term::create([
'parent' => $parent,
'name' => $term,
'vid' => $vocabulary,
])->save();
}
