219 lines
6.5 KiB
PHP
Executable file
219 lines
6.5 KiB
PHP
Executable file
<?php
|
|
|
|
require_once(__DIR__ . '/../wp-load.php');
|
|
|
|
DRPerformProcess();
|
|
|
|
function DRPerformProcess(){
|
|
set_time_limit(0);
|
|
|
|
// update des datas
|
|
DRUpdate();
|
|
// récupération des jobs
|
|
DRGetDailyJobs(0, 0);
|
|
// envoi des alertes
|
|
//DRMailAlert();
|
|
|
|
die();
|
|
}
|
|
|
|
function DRUpdate()
|
|
{
|
|
global $wpdb;
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_URL, 'https://recrutement.ldc.fr/fr/careers-sites/job-ads-filters/28691/0/0/0?complete=1');
|
|
$result = curl_exec($ch);
|
|
|
|
if (!curl_errno($ch)) {
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
if ($http_code !== 200) {
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Digital Recruiter : DRUpdate', 'Code HTTP de l\'erreur : ' . $http_code);
|
|
die();
|
|
}
|
|
}else{
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Digital Recruiter : DRUpdate', 'Erreur cURL');
|
|
die();
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
$result = json_decode($result);
|
|
|
|
if (!isset($result->jobs) || !isset($result->contracts) || !isset($result->workingTimes)){
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Digital Recruiter : DRUpdate', 'Les variables obligatoires ne sont pas présentes.');
|
|
die();
|
|
}
|
|
|
|
foreach ($result->jobs as $k => $job)
|
|
{
|
|
$wpdb->replace(
|
|
'wp_digitalrecruiter_jobs',
|
|
array(
|
|
'id' => $k,
|
|
'name' => $job,
|
|
),
|
|
array(
|
|
'%d',
|
|
'%s',
|
|
)
|
|
);
|
|
}
|
|
|
|
foreach ($result->contracts as $k => $v)
|
|
{
|
|
$wpdb->replace(
|
|
'wp_digitalrecruiter_contracts',
|
|
array(
|
|
'id' => $k,
|
|
'name' => $v,
|
|
),
|
|
array(
|
|
'%d',
|
|
'%s',
|
|
)
|
|
);
|
|
}
|
|
|
|
foreach ($result->workingTimes as $k => $v)
|
|
{
|
|
$wpdb->replace(
|
|
'wp_digitalrecruiter_workingtimes',
|
|
array(
|
|
'id' => $k,
|
|
'name' => $v,
|
|
),
|
|
array(
|
|
'%d',
|
|
'%s',
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
function DRGetDailyJobs($offset, $total)
|
|
{
|
|
global $wpdb;
|
|
|
|
if ($total === 0)
|
|
$wpdb->query("TRUNCATE TABLE `wp_digitalrecruiter_dailyjobs`");
|
|
|
|
$reset = ($total === 0 ? 1 : 0);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_URL, "https://recrutement.ldc.fr/fr/careers-sites/job-ads-more/28691/0/$offset/0/0?reset=$reset");
|
|
$result = curl_exec($ch);
|
|
|
|
if (!curl_errno($ch)) {
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
if ($http_code !== 200) {
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Digital Recruiter : DRGetDailyJobs', 'Code HTTP de l\'erreur : ' . $http_code);
|
|
die();
|
|
}
|
|
}else{
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Digital Recruiter : DRGetDailyJobs', 'Erreur cURL');
|
|
die();
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
$result = json_decode($result);
|
|
|
|
if ($total === 0){
|
|
if (!isset($result->jobAds) || !isset($result->joined) || !isset($result->total)){
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Digital Recruiter : DRGetDailyJobs', 'Les variables obligatoires ne sont pas présentes (1).');
|
|
die();
|
|
}
|
|
}else{
|
|
if (!isset($result->jobAds) || !isset($result->joined)){
|
|
email(MAIL_ADMIN, '[ERROR] Erreur "API" Digital Recruiter : DRGetDailyJobs', 'Les variables obligatoires ne sont pas présentes (2).');
|
|
die();
|
|
}
|
|
}
|
|
|
|
foreach ($result->jobAds as $job) {
|
|
$wpdb->insert(
|
|
'wp_digitalrecruiter_dailyjobs',
|
|
array(
|
|
'title' => $job->title,
|
|
'url' => $result->joined->apply_url->{$job->id}->{$job->address_id},
|
|
'contract_id' => $job->contract_type_id,
|
|
'workingtime_id' => $job->working_time,
|
|
'job_id' => $job->referentiel_id
|
|
),
|
|
array(
|
|
'%s',
|
|
'%s',
|
|
'%d',
|
|
'%d',
|
|
'%d',
|
|
)
|
|
);
|
|
}
|
|
|
|
if ($total === 0){
|
|
if ($result->total > $offset){
|
|
return DRGetDailyJobs($offset + 20, $result->total);
|
|
}
|
|
}else{
|
|
if ($total > $offset){
|
|
return DRGetDailyJobs($offset + 20, $total);
|
|
}
|
|
}
|
|
}
|
|
|
|
function DRMailAlert() {
|
|
global $wpdb;
|
|
|
|
//Récupère les informations des personnes enregistrés pour l'alerte
|
|
$alertData = $wpdb->get_results("SELECT * FROM wp_digitalrecruiter_jobalert");
|
|
|
|
foreach ($alertData as $alert)
|
|
{
|
|
$filters = [];
|
|
if ($alert->contract_id !== NULL)
|
|
$filters[] = "contract_id = ". $alert->contract_id;
|
|
if ($alert->workingtime_id !== NULL)
|
|
$filters[] = "workingtime_id = ". $alert->workingtime_id;
|
|
if ($alert->job_id !== NULL)
|
|
$filters[] = "job_id = ". $alert->job_id;
|
|
|
|
if (!empty($filters)){
|
|
$jobs = $wpdb->get_results("SELECT * FROM wp_digitalrecruiter_dailyjobs WHERE " . implode(" AND ", $filters));
|
|
}else{
|
|
$jobs = $wpdb->get_results("SELECT * FROM wp_digitalrecruiter_dailyjobs");
|
|
}
|
|
|
|
//Envoi du mail s'il y a des annonces correspondantes
|
|
if(count($jobs) > 0){
|
|
|
|
$body = "<html>";
|
|
$body .= "<head></head>";
|
|
$body .= "<body>";
|
|
$body .= "<p>Bonjour,</p>";
|
|
$body .= "<p>Vous vous êtes inscrits pour recevoir des alertes aux annonces LDC correspondantes a votre profil.</p>";
|
|
$body .= "<p>De nouveaux résultats (".count($jobs).") sont disponibles :</p>";
|
|
$body .= "<ul>";
|
|
foreach ($jobs as $oJob) {
|
|
$body .= "<li><a href='".$oJob->url."'>".$oJob->title."</a></li>";
|
|
}
|
|
$body .= "</ul>";
|
|
$body .= "<p>Merci</p>";
|
|
$body .= "<br><br>";
|
|
$body .= "<p><a href='https://www.ldc.fr/unsubdr.php?email=". $alert->email ."'>Se désinscrire des alertes</a></p>";
|
|
$body .= "</body></html>";
|
|
|
|
email($alert->email, "[LDC] Poste(s) correspondant(s) à vos critères de recherche", $body);
|
|
}
|
|
}
|
|
}
|
|
|
|
function email($email, $subject, $message)
|
|
{
|
|
$headers[] = 'From: Groupe LDC <nepasrepondre@ldc.fr>';
|
|
wp_mail($email, $subject, $message, $headers);
|
|
}
|